2 Copyright (C) 2001-2003, The Perl Foundation.
7 src/test_main.c - A sample test program
11 C<examples/c/test_main.c> is being retained as an example of a non-trivial, but
12 still clean, Parrot embedding.
22 #include "parrot/embed.h"
23 #include "parrot/longopt.h"
28 #define setopt(flag) Parrot_setflag(interp, flag, (*argv)[0]+2);
29 #define unsetopt(flag) Parrot_setflag(interp, flag, 0)
31 char *parseflags(Parrot_Interp interp
, int *argc
, char **argv
[]);
33 #define OPT_GC_DEBUG 128
34 #define OPT_DESTROY_FLAG 129
36 static struct longopt_opt_decl options
[] = {
37 { 'b', 'b', 0, { "--bounds-checks" } },
38 { 'd', 'd', 0, { "--debug" } },
39 { 'h', 'h', 0, { "--help" } },
40 { 'j', 'j', 0, { "--jit" } },
41 { 'o', 'o', 0, { "--output-file" } },
42 { 'p', 'p', 0, { "--profile" } },
43 { 'P', 'P', 0, { "--prederefrenced-core" } },
44 { 'S', 'S', 0, { "--switched-core" } },
45 { 'g', 'g', 0, { "--no-computed-goto" } },
46 { 't', 't', 0, { "--trace" } },
47 { 'v', 'v', 0, { "--version" } },
48 { '.', '.', 0, { "--wait" } },
49 {'\0', OPT_GC_DEBUG
, 0, { "--gc-debug" } },
50 {'\0', OPT_DESTROY_FLAG
, 0, { "--leak-test", "--destroy-at-end" } },
51 {'\0', 0, 0, { NULL
} }
54 static void usage(void);
56 static void version(void);
61 main(int argc, char *argv[])>
63 Loads the file and runs the code.
70 main(int argc
, char *argv
[])
76 interp
= Parrot_new(NULL
);
81 filename
= parseflags(interp
, &argc
, &argv
);
83 pf
= Parrot_readbc(interp
, filename
);
89 Parrot_loadbc(interp
, pf
);
90 Parrot_runcode(interp
, argc
, argv
);
91 Parrot_destroy(interp
);
93 Parrot_exit(interp
, 0);
100 parseflags(Parrot_Interp interp, int *argc, char **argv[])>
102 Parses the command-line.
109 parseflags(Parrot_Interp interp
, int *argc
, char **argv
[])
111 struct longopt_opt_info opt
= LONGOPT_OPT_INFO_INIT
;
122 #ifdef HAVE_COMPUTED_GOTO
123 setopt(PARROT_CGOTO_FLAG
);
126 while (longopt_get(interp
, *argc
, *argv
, options
, &opt
)) {
127 if (opt
.opt_id
== -1) {
128 fprintf(stderr
, "parrot: %s\n", opt
.opt_error
);
129 Parrot_exit(interp
, 1);
132 switch (opt
.opt_id
) {
134 setopt(PARROT_BOUNDS_FLAG
);
137 setopt(PARROT_JIT_FLAG
);
140 setopt(PARROT_EXEC_FLAG
);
143 setopt(PARROT_PROFILE_FLAG
);
146 setopt(PARROT_PREDEREF_FLAG
);
149 setopt(PARROT_SWITCH_FLAG
);
152 unsetopt(PARROT_CGOTO_FLAG
);
155 setopt(PARROT_TRACE_FLAG
);
158 setopt(PARROT_DEBUG_FLAG
);
167 Parrot_setwarnings(interp
, PARROT_WARNINGS_ALL_FLAG
);
170 case '.': /* Give Windows Parrot hackers an opportunity to
171 * attach a debuggger. */
176 Parrot_warn(interp
, PARROT_WARNINGS_ALL_FLAG
,
177 "PARROT_GC_DEBUG is set but the binary was "
178 "compiled with DISABLE_GC_DEBUG.");
180 setopt(PARROT_GC_DEBUG_FLAG
);
182 case OPT_DESTROY_FLAG
:
183 setopt(PARROT_DESTROY_FLAG
);
187 *argv
+= opt
.opt_index
;
188 *argc
-= opt
.opt_index
;
194 return 0; /* This won't happen */
203 Returns the user help.
212 #ifdef HAVE_COMPUTED_GOTO
213 const char* cgoto_info
= "Deactivate computed goto";
215 const char* cgoto_info
=
216 "Deactivate computed goto (not available on this platform)";
220 "Usage: parrot [switches] [--] programfile [arguments]\n\
221 -b --bounds-checks Activate bounds checks\n\
222 -d --debug Activate debugging\n\
223 -h --help Display this message\n\
224 -j --jit Activate Just-In-Time compiler\n\
225 -p --profile Activate profiling\n\
226 -P --predereferenced_core Activate predereferencing\n\
227 -S --switched_core Activate switched core\n\
228 -g --no-computed-goto %s\n\
229 -t --trace Activate tracing\n\
230 -v --version Display version information\n\
231 -. --wait Wait for a keypress (gives Windows users\n\
232 time to attach a debugger)\n\
234 Enable garbage collection debugging mode. This may also be enabled\n\
235 by setting the environment variable $PARROT_GC_DEBUG to 1.\n\
239 Parrot_exit(interp
, 0);
247 Returns the version information.
257 "This is parrot version " PARROT_VERSION
" built for "
259 Copyright (C) 2001-2003, The Perl Foundation.\n\
261 Parrot may be copied only under the terms of either the Artistic License or the\
263 GNU General Public License, which may be found in the Parrot source kit.\n\
265 This program is distributed in the hope that it will be useful,\n\
266 but WITHOUT ANY WARRANTY; without even the implied warranty of\n\
267 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See either\n\
268 the GNU General Public License or the Artistic License for more details.\n\n");
270 Parrot_exit(interp
, 0);
283 This file used to be the C<parrot> executable, but F<imcc/main.c>
292 * c-file-style: "parrot"
294 * vim: expandtab shiftwidth=4: