+ Correct docs/project/roles_responsibilities.pod location.
[parrot.git] / examples / c / test_main.c
blob8ab79f88e4b6678b2317981a2576c6315575a9f3
1 /*
2 Copyright (C) 2001-2003, The Perl Foundation.
3 $Id$
5 =head1 NAME
7 src/test_main.c - A sample test program
9 =head1 DESCRIPTION
11 C<examples/c/test_main.c> is being retained as an example of a non-trivial, but
12 still clean, Parrot embedding.
14 =head2 Functions
16 =over 4
18 =cut
22 #include "parrot/embed.h"
23 #include "parrot/longopt.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.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);
60 =item C<int
61 main(int argc, char *argv[])>
63 Loads the file and runs the code.
65 =cut
69 int
70 main(int argc, char *argv[])
72 Parrot_Interp interp;
73 char *filename;
74 Parrot_PackFile pf;
76 interp = Parrot_new(NULL);
77 if (!interp) {
78 return 1;
81 filename = parseflags(interp, &argc, &argv);
83 pf = Parrot_readbc(interp, filename);
85 if (!pf) {
86 return 1;
89 Parrot_loadbc(interp, pf);
90 Parrot_runcode(interp, argc, argv);
91 Parrot_destroy(interp);
93 Parrot_exit(interp, 0);
94 return 0;
99 =item C<char *
100 parseflags(Parrot_Interp interp, int *argc, char **argv[])>
102 Parses the command-line.
104 =cut
108 char *
109 parseflags(Parrot_Interp interp, int *argc, char **argv[])
111 struct longopt_opt_info opt = LONGOPT_OPT_INFO_INIT;
113 if (*argc == 1) {
114 usage();
117 ++*argv;
118 --*argc;
119 opt.opt_index = 0;
122 #ifdef HAVE_COMPUTED_GOTO
123 setopt(PARROT_CGOTO_FLAG);
124 #endif
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) {
133 case 'b':
134 setopt(PARROT_BOUNDS_FLAG);
135 break;
136 case 'j':
137 setopt(PARROT_JIT_FLAG);
138 break;
139 case 'o':
140 setopt(PARROT_EXEC_FLAG);
141 break;
142 case 'p':
143 setopt(PARROT_PROFILE_FLAG);
144 break;
145 case 'P':
146 setopt(PARROT_PREDEREF_FLAG);
147 break;
148 case 'S':
149 setopt(PARROT_SWITCH_FLAG);
150 break;
151 case 'g':
152 unsetopt(PARROT_CGOTO_FLAG);
153 break;
154 case 't':
155 setopt(PARROT_TRACE_FLAG);
156 break;
157 case 'd':
158 setopt(PARROT_DEBUG_FLAG);
159 break;
160 case 'h':
161 usage();
162 break;
163 case 'v':
164 version();
165 break;
166 case 'w':
167 Parrot_setwarnings(interp, PARROT_WARNINGS_ALL_FLAG);
168 break;
170 case '.': /* Give Windows Parrot hackers an opportunity to
171 * attach a debuggger. */
172 fgetc(stdin);
173 break;
174 case OPT_GC_DEBUG:
175 #if DISABLE_GC_DEBUG
176 Parrot_warn(interp, PARROT_WARNINGS_ALL_FLAG,
177 "PARROT_GC_DEBUG is set but the binary was "
178 "compiled with DISABLE_GC_DEBUG.");
179 #endif
180 setopt(PARROT_GC_DEBUG_FLAG);
181 break;
182 case OPT_DESTROY_FLAG:
183 setopt(PARROT_DESTROY_FLAG);
184 break;
187 *argv += opt.opt_index;
188 *argc -= opt.opt_index;
190 if ((*argv)[0])
191 return (*argv)[0];
192 else {
193 usage();
194 return 0; /* This won't happen */
200 =item C<static void
201 usage(void)>
203 Returns the user help.
205 =cut
209 static void
210 usage(void)
212 #ifdef HAVE_COMPUTED_GOTO
213 const char* cgoto_info = "Deactivate computed goto";
214 #else
215 const char* cgoto_info =
216 "Deactivate computed goto (not available on this platform)";
217 #endif
219 fprintf(stderr,
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\
233 --gc-debug\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\
236 \n",
237 cgoto_info);
239 Parrot_exit(interp, 0);
244 =item C<static void
245 version(void)>
247 Returns the version information.
249 =cut
253 static void
254 version(void)
256 fprintf(stderr,
257 "This is parrot version " PARROT_VERSION " built for "
258 PARROT_ARCHNAME "\n\
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);
275 =back
277 =head1 SEE ALSO
279 F<imcc/main.c>.
281 =head1 HISTORY
283 This file used to be the C<parrot> executable, but F<imcc/main.c>
284 performs that role.
286 =cut
291 * Local variables:
292 * c-file-style: "parrot"
293 * End:
294 * vim: expandtab shiftwidth=4: