Debian Codegraph @

vendor/mruby-source/mrbgems/mruby-bin-mruby/tools/mruby/mruby.c

raw
1 #include <mruby.h>
2
3 #ifdef MRB_NO_STDIO
4 # error mruby-bin-mruby conflicts 'MRB_NO_STDIO' in your build configuration
5 #endif
6
7 #include <stdlib.h>
8 #include <string.h>
9 #include <mruby/array.h>
10 #include <mruby/compile.h>
11 #include <mruby/dump.h>
12 #include <mruby/variable.h>
13 #include <mruby/proc.h>
14 #include <mruby/error.h>
15 #include <mruby/presym.h>
16
17 #if defined(_WIN32) || defined(_WIN64)
18 # include <io.h> /* for setmode */
19 # include <fcntl.h>
20 #endif
21
22 struct _args {
23 FILE *rfp;
24 char *cmdline;
25 mrb_bool fname : 1;
26 mrb_bool mrbfile : 1;
27 mrb_bool check_syntax : 1;
28 mrb_bool verbose : 1;
29 mrb_bool version : 1;
30 mrb_bool debug : 1;
31 int argc;
32 char **argv;
33 int libc;
34 char **libv;
35 };
36
37 struct options {
38 int argc;
39 char **argv;
40 char *program;
41 char *opt;
42 char short_opt[2];
43 };
44
45 static void
46 usage(const char *name)
47 {
48 static const char *const usage_msg[] = {
49 "switches:",
50 "-b load and execute RiteBinary (mrb) file",
51 "-c check syntax only",
52 "-d set debugging flags (set $DEBUG to true)",
53 "-e 'command' one line of script",
54 "-r library load the library before executing your script",
55 "-v print version number, then run in verbose mode",
56 "--verbose run in verbose mode",
57 "--version print the version",
58 "--copyright print the copyright",
59 NULL
60 };
61 const char *const *p = usage_msg;
62
63 printf("Usage: %s [switches] [programfile] [arguments]\n", name);
64 while (*p)
65 printf(" %s\n", *p++);
66 }
67
68 static mrb_bool
69 mrb_extension_p(const char *path)
70 {
71 const char *e = strrchr(path, '.');
72 if (e && e[1] == 'm' && e[2] == 'r' && e[3] == 'b' && e[4] == '\0') {
73 return TRUE;
74 }
75 return FALSE;
76 }
77
78 static void
79 options_init(struct options *opts, int argc, char **argv)
80 {
81 opts->argc = argc;
82 opts->argv = argv;
83 opts->program = *argv;
84 *opts->short_opt = 0;
85 }
86
87 static const char *
88 options_opt(struct options *opts)
89 {
90 /* concatenated short options (e.g. `-cv`) */
91 if (*opts->short_opt && *++opts->opt) {
92 short_opt:
93 opts->short_opt[0] = *opts->opt;
94 opts->short_opt[1] = 0;
95 return opts->short_opt;
96 }
97
98 while (++opts->argv, --opts->argc) {
99 opts->opt = *opts->argv;
100
101 /* empty || not start with `-` || `-` */
102 if (!opts->opt[0] || opts->opt[0] != '-' || !opts->opt[1]) return NULL;
103
104 if (opts->opt[1] == '-') {
105 /* `--` */
106 if (!opts->opt[2]) {
107 opts->argv++, opts->argc--;
108 return NULL;
109 }
110 /* long option */
111 opts->opt += 2;
112 *opts->short_opt = 0;
113 return opts->opt;
114 }
115 else {
116 /* short option */
117 opts->opt++;
118 goto short_opt;
119 }
120 }
121 return NULL;
122 }
123
124 static const char *
125 options_arg(struct options *opts)
126 {
127 if (*opts->short_opt && opts->opt[1]) {
128 /* concatenated short option and option argument (e.g. `-rLIBRARY`) */
129 *opts->short_opt = 0;
130 return opts->opt + 1;
131 }
132 --opts->argc, ++opts->argv;
133 return opts->argc ? *opts->argv : NULL;
134 }
135
136 static char *
137 dup_arg_item(mrb_state *mrb, const char *item)
138 {
139 size_t buflen = strlen(item) + 1;
140 char *buf = (char*)mrb_malloc(mrb, buflen);
141 memcpy(buf, item, buflen);
142 return buf;
143 }
144
145 static int
146 parse_args(mrb_state *mrb, int argc, char **argv, struct _args *args)
147 {
148 static const struct _args args_zero = { 0 };
149 struct options opts[1];
150 const char *opt, *item;
151
152 *args = args_zero;
153 options_init(opts, argc, argv);
154 while ((opt = options_opt(opts))) {
155 if (strcmp(opt, "b") == 0) {
156 args->mrbfile = TRUE;
157 }
158 else if (strcmp(opt, "c") == 0) {
159 args->check_syntax = TRUE;
160 }
161 else if (strcmp(opt, "d") == 0) {
162 args->debug = TRUE;
163 }
164 else if (strcmp(opt, "e") == 0) {
165 if ((item = options_arg(opts))) {
166 if (!args->cmdline) {
167 args->cmdline = dup_arg_item(mrb, item);
168 }
169 else {
170 size_t cmdlinelen;
171 size_t itemlen;
172
173 cmdlinelen = strlen(args->cmdline);
174 itemlen = strlen(item);
175 args->cmdline = (char*)mrb_realloc(mrb, args->cmdline, cmdlinelen + itemlen + 2);
176 args->cmdline[cmdlinelen] = '\n';
177 memcpy(args->cmdline + cmdlinelen + 1, item, itemlen + 1);
178 }
179 }
180 else {
181 fprintf(stderr, "%s: No code specified for -e\n", opts->program);
182 return EXIT_FAILURE;
183 }
184 }
185 else if (strcmp(opt, "h") == 0) {
186 usage(opts->program);
187 exit(EXIT_SUCCESS);
188 }
189 else if (strcmp(opt, "r") == 0) {
190 if ((item = options_arg(opts))) {
191 if (args->libc == 0) {
192 args->libv = (char**)mrb_malloc(mrb, sizeof(char*));
193 }
194 else {
195 args->libv = (char**)mrb_realloc(mrb, args->libv, sizeof(char*) * (args->libc + 1));
196 }
197 args->libv[args->libc++] = dup_arg_item(mrb, item);
198 }
199 else {
200 fprintf(stderr, "%s: No library specified for -r\n", opts->program);
201 return EXIT_FAILURE;
202 }
203 }
204 else if (strcmp(opt, "v") == 0) {
205 if (!args->verbose) {
206 mrb_show_version(mrb);
207 args->version = TRUE;
208 }
209 args->verbose = TRUE;
210 }
211 else if (strcmp(opt, "version") == 0) {
212 mrb_show_version(mrb);
213 exit(EXIT_SUCCESS);
214 }
215 else if (strcmp(opt, "verbose") == 0) {
216 args->verbose = TRUE;
217 }
218 else if (strcmp(opt, "copyright") == 0) {
219 mrb_show_copyright(mrb);
220 exit(EXIT_SUCCESS);
221 }
222 else {
223 fprintf(stderr, "%s: invalid option %s%s (-h will show valid options)\n",
224 opts->program, opt[1] ? "--" : "-", opt);
225 return EXIT_FAILURE;
226 }
227 }
228
229 argc = opts->argc; argv = opts->argv;
230 if (args->cmdline == NULL) {
231 if (*argv == NULL) {
232 if (args->version) exit(EXIT_SUCCESS);
233 args->rfp = stdin;
234 }
235 else {
236 args->rfp = strcmp(argv[0], "-") == 0 ?
237 stdin : fopen(argv[0], "rb");
238 if (args->rfp == NULL) {
239 fprintf(stderr, "%s: Cannot open program file: %s\n", opts->program, argv[0]);
240 return EXIT_FAILURE;
241 }
242 args->fname = TRUE;
243 args->cmdline = argv[0];
244 argc--; argv++;
245 }
246 }
247 #if defined(_WIN32) || defined(_WIN64)
248 if (args->rfp == stdin) {
249 _setmode(_fileno(stdin), O_BINARY);
250 }
251 #endif
252 args->argv = (char **)mrb_realloc(mrb, args->argv, sizeof(char*) * (argc + 1));
253 memcpy(args->argv, argv, (argc+1) * sizeof(char*));
254 args->argc = argc;
255
256 return EXIT_SUCCESS;
257 }
258
259 static void
260 cleanup(mrb_state *mrb, struct _args *args)
261 {
262 if (args->rfp && args->rfp != stdin)
263 fclose(args->rfp);
264 if (!args->fname)
265 mrb_free(mrb, args->cmdline);
266 mrb_free(mrb, args->argv);
267 if (args->libc) {
268 while (args->libc--) {
269 mrb_free(mrb, args->libv[args->libc]);
270 }
271 mrb_free(mrb, args->libv);
272 }
273 mrb_close(mrb);
274 }
275
276 int
277 main(int argc, char **argv)
278 {
279 mrb_state *mrb = mrb_open();
280 int n = -1;
281 int i;
282 struct _args args;
283 mrb_value ARGV;
284 mrbc_context *c;
285 mrb_value v;
286
287 if (mrb == NULL) {
288 fprintf(stderr, "%s: Invalid mrb_state, exiting mruby\n", *argv);
289 return EXIT_FAILURE;
290 }
291
292 n = parse_args(mrb, argc, argv, &args);
293 if (n == EXIT_FAILURE || (args.cmdline == NULL && args.rfp == NULL)) {
294 cleanup(mrb, &args);
295 return n;
296 }
297 else {
298 int ai = mrb_gc_arena_save(mrb);
299 ARGV = mrb_ary_new_capa(mrb, args.argc);
300 for (i = 0; i < args.argc; i++) {
301 char* utf8 = mrb_utf8_from_locale(args.argv[i], -1);
302 if (utf8) {
303 mrb_ary_push(mrb, ARGV, mrb_str_new_cstr(mrb, utf8));
304 mrb_utf8_free(utf8);
305 }
306 }
307 mrb_define_global_const(mrb, "ARGV", ARGV);
308 mrb_gv_set(mrb, mrb_intern_lit(mrb, "$DEBUG"), mrb_bool_value(args.debug));
309
310 c = mrbc_context_new(mrb);
311 if (args.verbose)
312 c->dump_result = TRUE;
313 if (args.check_syntax)
314 c->no_exec = TRUE;
315
316 /* Set $0 */
317 const char *cmdline;
318 if (args.rfp) {
319 cmdline = args.cmdline ? args.cmdline : "-";
320 }
321 else {
322 cmdline = "-e";
323 }
324 mrb_gv_set(mrb, mrb_intern_lit(mrb, "$0"), mrb_str_new_cstr(mrb, cmdline));
325
326 /* Load libraries */
327 for (i = 0; i < args.libc; i++) {
328 FILE *lfp = fopen(args.libv[i], "rb");
329 if (lfp == NULL) {
330 fprintf(stderr, "%s: Cannot open library file: %s\n", *argv, args.libv[i]);
331 mrbc_context_free(mrb, c);
332 cleanup(mrb, &args);
333 return EXIT_FAILURE;
334 }
335 mrbc_filename(mrb, c, args.libv[i]);
336 if (mrb_extension_p(args.libv[i])) {
337 v = mrb_load_irep_file_cxt(mrb, lfp, c);
338 }
339 else {
340 v = mrb_load_detect_file_cxt(mrb, lfp, c);
341 }
342 fclose(lfp);
343 mrb_vm_ci_env_clear(mrb, mrb->c->cibase);
344 mrbc_cleanup_local_variables(mrb, c);
345 }
346
347 /* set program file name */
348 mrbc_filename(mrb, c, cmdline);
349
350 /* Load program */
351 if (args.mrbfile || mrb_extension_p(cmdline)) {
352 v = mrb_load_irep_file_cxt(mrb, args.rfp, c);
353 }
354 else if (args.rfp) {
355 v = mrb_load_detect_file_cxt(mrb, args.rfp, c);
356 }
357 else {
358 char* utf8 = mrb_utf8_from_locale(args.cmdline, -1);
359 if (!utf8) abort();
360 v = mrb_load_string_cxt(mrb, utf8, c);
361 mrb_utf8_free(utf8);
362 }
363
364 mrb_gc_arena_restore(mrb, ai);
365 mrbc_context_free(mrb, c);
366 if (mrb->exc) {
367 MRB_EXC_CHECK_EXIT(mrb, mrb->exc);
368 if (!mrb_undef_p(v)) {
369 mrb_print_error(mrb);
370 }
371 n = EXIT_FAILURE;
372 }
373 else if (args.check_syntax) {
374 puts("Syntax OK");
375 }
376 }
377 cleanup(mrb, &args);
378
379 return n;
380 }