1 /*
2 ** mirb - Embeddable Interactive Ruby Shell
3 **
4 ** This program takes code from the user in
5 ** an interactive way and executes it
6 ** immediately. It's a REPL...
7 */
8 
9 #include <mruby.h>
10 #include <mruby/array.h>
11 #include <mruby/proc.h>
12 #include <mruby/compile.h>
13 #include <mruby/dump.h>
14 #include <mruby/string.h>
15 #include <mruby/variable.h>
16 #include <mruby/throw.h>
17 
18 #include <stdlib.h>
19 #include <string.h>
20 #include <stdio.h>
21 #include <ctype.h>
22 
23 #include <signal.h>
24 #include <setjmp.h>
25 
26 #ifdef ENABLE_READLINE
27 #include <readline/readline.h>
28 #include <readline/history.h>
29 #define MIRB_ADD_HISTORY(line) add_history(line)
30 #define MIRB_READLINE(ch) readline(ch)
31 #if !defined(RL_READLINE_VERSION) || RL_READLINE_VERSION < 0x600
32 /* libedit & older readline do not have rl_free() */
33 #define MIRB_LINE_FREE(line) free(line)
34 #else
35 #define MIRB_LINE_FREE(line) rl_free(line)
36 #endif
37 #define MIRB_WRITE_HISTORY(path) write_history(path)
38 #define MIRB_READ_HISTORY(path) read_history(path)
39 #define MIRB_USING_HISTORY() using_history()
40 #elif defined(ENABLE_LINENOISE)
41 #define ENABLE_READLINE
42 #include <linenoise.h>
43 #define MIRB_ADD_HISTORY(line) linenoiseHistoryAdd(line)
44 #define MIRB_READLINE(ch) linenoise(ch)
45 #define MIRB_LINE_FREE(line) linenoiseFree(line)
46 #define MIRB_WRITE_HISTORY(path) linenoiseHistorySave(path)
47 #define MIRB_READ_HISTORY(path) linenoiseHistoryLoad(history_path)
48 #define MIRB_USING_HISTORY()
49 #endif
50 
51 #ifndef _WIN32
52 #define MIRB_SIGSETJMP(env) sigsetjmp(env, 1)
53 #define MIRB_SIGLONGJMP(env, val) siglongjmp(env, val)
54 #define SIGJMP_BUF sigjmp_buf
55 #else
56 #define MIRB_SIGSETJMP(env) setjmp(env)
57 #define MIRB_SIGLONGJMP(env, val) longjmp(env, val)
58 #define SIGJMP_BUF jmp_buf
59 #endif
60 
61 #ifdef ENABLE_READLINE
62 
63 static const char history_file_name[] = ".mirb_history";
64 
65 static char *
get_history_path(mrb_state * mrb)66 get_history_path(mrb_state *mrb)
67 {
68   char *path = NULL;
69   const char *home = getenv("HOME");
70 
71 #ifdef _WIN32
72   if (home != NULL) {
73     home = getenv("USERPROFILE");
74   }
75 #endif
76 
77   if (home != NULL) {
78     int len = snprintf(NULL, 0, "%s/%s", home, history_file_name);
79     if (len >= 0) {
80       size_t size = len + 1;
81       path = (char *)mrb_malloc_simple(mrb, size);
82       if (path != NULL) {
83         int n = snprintf(path, size, "%s/%s", home, history_file_name);
84         if (n != len) {
85           mrb_free(mrb, path);
86           path = NULL;
87         }
88       }
89     }
90   }
91 
92   return path;
93 }
94 
95 #endif
96 
97 static void
p(mrb_state * mrb,mrb_value obj,int prompt)98 p(mrb_state *mrb, mrb_value obj, int prompt)
99 {
100   mrb_value val;
101   char* msg;
102 
103   val = mrb_funcall(mrb, obj, "inspect", 0);
104   if (prompt) {
105     if (!mrb->exc) {
106       fputs(" => ", stdout);
107     }
108     else {
109       val = mrb_funcall(mrb, mrb_obj_value(mrb->exc), "inspect", 0);
110     }
111   }
112   if (!mrb_string_p(val)) {
113     val = mrb_obj_as_string(mrb, obj);
114   }
115   msg = mrb_locale_from_utf8(RSTRING_PTR(val), (int)RSTRING_LEN(val));
116   fwrite(msg, strlen(msg), 1, stdout);
117   mrb_locale_free(msg);
118   putc('\n', stdout);
119 }
120 
121 /* Guess if the user might want to enter more
122  * or if he wants an evaluation of his code now */
123 static mrb_bool
is_code_block_open(struct mrb_parser_state * parser)124 is_code_block_open(struct mrb_parser_state *parser)
125 {
126   mrb_bool code_block_open = FALSE;
127 
128   /* check for heredoc */
129   if (parser->parsing_heredoc != NULL) return TRUE;
130 
131   /* check for unterminated string */
132   if (parser->lex_strterm) return TRUE;
133 
134   /* check if parser error are available */
135   if (0 < parser->nerr) {
136     const char unexpected_end[] = "syntax error, unexpected $end";
137     const char *message = parser->error_buffer[0].message;
138 
139     /* a parser error occur, we have to check if */
140     /* we need to read one more line or if there is */
141     /* a different issue which we have to show to */
142     /* the user */
143 
144     if (strncmp(message, unexpected_end, sizeof(unexpected_end) - 1) == 0) {
145       code_block_open = TRUE;
146     }
147     else if (strcmp(message, "syntax error, unexpected keyword_end") == 0) {
148       code_block_open = FALSE;
149     }
150     else if (strcmp(message, "syntax error, unexpected tREGEXP_BEG") == 0) {
151       code_block_open = FALSE;
152     }
153     return code_block_open;
154   }
155 
156   switch (parser->lstate) {
157 
158   /* all states which need more code */
159 
160   case EXPR_BEG:
161     /* beginning of a statement, */
162     /* that means previous line ended */
163     code_block_open = FALSE;
164     break;
165   case EXPR_DOT:
166     /* a message dot was the last token, */
167     /* there has to come more */
168     code_block_open = TRUE;
169     break;
170   case EXPR_CLASS:
171     /* a class keyword is not enough! */
172     /* we need also a name of the class */
173     code_block_open = TRUE;
174     break;
175   case EXPR_FNAME:
176     /* a method name is necessary */
177     code_block_open = TRUE;
178     break;
179   case EXPR_VALUE:
180     /* if, elsif, etc. without condition */
181     code_block_open = TRUE;
182     break;
183 
184   /* now all the states which are closed */
185 
186   case EXPR_ARG:
187     /* an argument is the last token */
188     code_block_open = FALSE;
189     break;
190 
191   /* all states which are unsure */
192 
193   case EXPR_CMDARG:
194     break;
195   case EXPR_END:
196     /* an expression was ended */
197     break;
198   case EXPR_ENDARG:
199     /* closing parenthese */
200     break;
201   case EXPR_ENDFN:
202     /* definition end */
203     break;
204   case EXPR_MID:
205     /* jump keyword like break, return, ... */
206     break;
207   case EXPR_MAX_STATE:
208     /* don't know what to do with this token */
209     break;
210   default:
211     /* this state is unexpected! */
212     break;
213   }
214 
215   return code_block_open;
216 }
217 
218 struct _args {
219   FILE *rfp;
220   mrb_bool verbose      : 1;
221   mrb_bool debug        : 1;
222   int argc;
223   char** argv;
224   int libc;
225   char **libv;
226 };
227 
228 static void
usage(const char * name)229 usage(const char *name)
230 {
231   static const char *const usage_msg[] = {
232   "switches:",
233   "-d           set $DEBUG to true (same as `mruby -d`)",
234   "-r library   same as `mruby -r`",
235   "-v           print version number, then run in verbose mode",
236   "--verbose    run in verbose mode",
237   "--version    print the version",
238   "--copyright  print the copyright",
239   NULL
240   };
241   const char *const *p = usage_msg;
242 
243   printf("Usage: %s [switches]\n", name);
244   while (*p)
245     printf("  %s\n", *p++);
246 }
247 
248 static char *
dup_arg_item(mrb_state * mrb,const char * item)249 dup_arg_item(mrb_state *mrb, const char *item)
250 {
251   size_t buflen = strlen(item) + 1;
252   char *buf = (char*)mrb_malloc(mrb, buflen);
253   memcpy(buf, item, buflen);
254   return buf;
255 }
256 
257 static int
parse_args(mrb_state * mrb,int argc,char ** argv,struct _args * args)258 parse_args(mrb_state *mrb, int argc, char **argv, struct _args *args)
259 {
260   char **origargv = argv;
261   static const struct _args args_zero = { 0 };
262 
263   *args = args_zero;
264 
265   for (argc--,argv++; argc > 0; argc--,argv++) {
266     char *item;
267     if (argv[0][0] != '-') break;
268 
269     item = argv[0] + 1;
270     switch (*item++) {
271     case 'd':
272       args->debug = TRUE;
273       break;
274     case 'r':
275       if (!item[0]) {
276         if (argc <= 1) {
277           printf("%s: No library specified for -r\n", *origargv);
278           return EXIT_FAILURE;
279         }
280         argc--; argv++;
281         item = argv[0];
282       }
283       if (args->libc == 0) {
284         args->libv = (char**)mrb_malloc(mrb, sizeof(char*));
285       }
286       else {
287         args->libv = (char**)mrb_realloc(mrb, args->libv, sizeof(char*) * (args->libc + 1));
288       }
289       args->libv[args->libc++] = dup_arg_item(mrb, item);
290       break;
291     case 'v':
292       if (!args->verbose) mrb_show_version(mrb);
293       args->verbose = TRUE;
294       break;
295     case '-':
296       if (strcmp((*argv) + 2, "version") == 0) {
297         mrb_show_version(mrb);
298         exit(EXIT_SUCCESS);
299       }
300       else if (strcmp((*argv) + 2, "verbose") == 0) {
301         args->verbose = TRUE;
302         break;
303       }
304       else if (strcmp((*argv) + 2, "copyright") == 0) {
305         mrb_show_copyright(mrb);
306         exit(EXIT_SUCCESS);
307       }
308     default:
309       return EXIT_FAILURE;
310     }
311   }
312 
313   if (args->rfp == NULL) {
314     if (*argv != NULL) {
315       args->rfp = fopen(argv[0], "r");
316       if (args->rfp == NULL) {
317         printf("Cannot open program file. (%s)\n", *argv);
318         return EXIT_FAILURE;
319       }
320       argc--; argv++;
321     }
322   }
323   args->argv = (char **)mrb_realloc(mrb, args->argv, sizeof(char*) * (argc + 1));
324   memcpy(args->argv, argv, (argc+1) * sizeof(char*));
325   args->argc = argc;
326 
327   return EXIT_SUCCESS;
328 }
329 
330 static void
cleanup(mrb_state * mrb,struct _args * args)331 cleanup(mrb_state *mrb, struct _args *args)
332 {
333   if (args->rfp)
334     fclose(args->rfp);
335   mrb_free(mrb, args->argv);
336   if (args->libc) {
337     while (args->libc--) {
338       mrb_free(mrb, args->libv[args->libc]);
339     }
340     mrb_free(mrb, args->libv);
341   }
342   mrb_close(mrb);
343 }
344 
345 /* Print a short remark for the user */
346 static void
print_hint(void)347 print_hint(void)
348 {
349   printf("mirb - Embeddable Interactive Ruby Shell\n\n");
350 }
351 
352 #ifndef ENABLE_READLINE
353 /* Print the command line prompt of the REPL */
354 static void
print_cmdline(int code_block_open)355 print_cmdline(int code_block_open)
356 {
357   if (code_block_open) {
358     printf("* ");
359   }
360   else {
361     printf("> ");
362   }
363   fflush(stdout);
364 }
365 #endif
366 
367 void mrb_codedump_all(mrb_state*, struct RProc*);
368 
369 static int
check_keyword(const char * buf,const char * word)370 check_keyword(const char *buf, const char *word)
371 {
372   const char *p = buf;
373   size_t len = strlen(word);
374 
375   /* skip preceding spaces */
376   while (*p && ISSPACE(*p)) {
377     p++;
378   }
379   /* check keyword */
380   if (strncmp(p, word, len) != 0) {
381     return 0;
382   }
383   p += len;
384   /* skip trailing spaces */
385   while (*p) {
386     if (!ISSPACE(*p)) return 0;
387     p++;
388   }
389   return 1;
390 }
391 
392 
393 #ifndef ENABLE_READLINE
394 volatile sig_atomic_t input_canceled = 0;
395 void
ctrl_c_handler(int signo)396 ctrl_c_handler(int signo)
397 {
398   input_canceled = 1;
399 }
400 #else
401 SIGJMP_BUF ctrl_c_buf;
402 void
ctrl_c_handler(int signo)403 ctrl_c_handler(int signo)
404 {
405   MIRB_SIGLONGJMP(ctrl_c_buf, 1);
406 }
407 #endif
408 
409 int
main(int argc,char ** argv)410 main(int argc, char **argv)
411 {
412   char ruby_code[4096] = { 0 };
413   char last_code_line[1024] = { 0 };
414 #ifndef ENABLE_READLINE
415   int last_char;
416   size_t char_index;
417 #else
418   char *history_path;
419   char* line;
420 #endif
421   mrbc_context *cxt;
422   struct mrb_parser_state *parser;
423   mrb_state *mrb;
424   mrb_value result;
425   struct _args args;
426   mrb_value ARGV;
427   int n;
428   int i;
429   mrb_bool code_block_open = FALSE;
430   int ai;
431   unsigned int stack_keep = 0;
432 
433   /* new interpreter instance */
434   mrb = mrb_open();
435   if (mrb == NULL) {
436     fputs("Invalid mrb interpreter, exiting mirb\n", stderr);
437     return EXIT_FAILURE;
438   }
439 
440   n = parse_args(mrb, argc, argv, &args);
441   if (n == EXIT_FAILURE) {
442     cleanup(mrb, &args);
443     usage(argv[0]);
444     return n;
445   }
446 
447   ARGV = mrb_ary_new_capa(mrb, args.argc);
448   for (i = 0; i < args.argc; i++) {
449     char* utf8 = mrb_utf8_from_locale(args.argv[i], -1);
450     if (utf8) {
451       mrb_ary_push(mrb, ARGV, mrb_str_new_cstr(mrb, utf8));
452       mrb_utf8_free(utf8);
453     }
454   }
455   mrb_define_global_const(mrb, "ARGV", ARGV);
456   mrb_gv_set(mrb, mrb_intern_lit(mrb, "$DEBUG"), mrb_bool_value(args.debug));
457 
458 #ifdef ENABLE_READLINE
459   history_path = get_history_path(mrb);
460   if (history_path == NULL) {
461     fputs("failed to get history path\n", stderr);
462     mrb_close(mrb);
463     return EXIT_FAILURE;
464   }
465 
466   MIRB_USING_HISTORY();
467   MIRB_READ_HISTORY(history_path);
468 #endif
469 
470   print_hint();
471 
472   cxt = mrbc_context_new(mrb);
473 
474   /* Load libraries */
475   for (i = 0; i < args.libc; i++) {
476     FILE *lfp = fopen(args.libv[i], "r");
477     if (lfp == NULL) {
478       printf("Cannot open library file. (%s)\n", args.libv[i]);
479       cleanup(mrb, &args);
480       return EXIT_FAILURE;
481     }
482     mrb_load_file_cxt(mrb, lfp, cxt);
483     fclose(lfp);
484   }
485 
486   cxt->capture_errors = TRUE;
487   cxt->lineno = 1;
488   mrbc_filename(mrb, cxt, "(mirb)");
489   if (args.verbose) cxt->dump_result = TRUE;
490 
491   ai = mrb_gc_arena_save(mrb);
492 
493   while (TRUE) {
494     char *utf8;
495     struct mrb_jmpbuf c_jmp;
496 
497     MRB_TRY(&c_jmp);
498     mrb->jmp = &c_jmp;
499     if (args.rfp) {
500       if (fgets(last_code_line, sizeof(last_code_line)-1, args.rfp) != NULL)
501         goto done;
502       break;
503     }
504 
505 #ifndef ENABLE_READLINE
506     print_cmdline(code_block_open);
507 
508     signal(SIGINT, ctrl_c_handler);
509     char_index = 0;
510     while ((last_char = getchar()) != '\n') {
511       if (last_char == EOF) break;
512       if (char_index >= sizeof(last_code_line)-2) {
513         fputs("input string too long\n", stderr);
514         continue;
515       }
516       last_code_line[char_index++] = last_char;
517     }
518     signal(SIGINT, SIG_DFL);
519     if (input_canceled) {
520       ruby_code[0] = '\0';
521       last_code_line[0] = '\0';
522       code_block_open = FALSE;
523       puts("^C");
524       input_canceled = 0;
525       continue;
526     }
527     if (last_char == EOF) {
528       fputs("\n", stdout);
529       break;
530     }
531 
532     last_code_line[char_index++] = '\n';
533     last_code_line[char_index] = '\0';
534 #else
535     if (MIRB_SIGSETJMP(ctrl_c_buf) == 0) {
536       ;
537     }
538     else {
539       ruby_code[0] = '\0';
540       last_code_line[0] = '\0';
541       code_block_open = FALSE;
542       puts("^C");
543     }
544     signal(SIGINT, ctrl_c_handler);
545     line = MIRB_READLINE(code_block_open ? "* " : "> ");
546     signal(SIGINT, SIG_DFL);
547 
548     if (line == NULL) {
549       printf("\n");
550       break;
551     }
552     if (strlen(line) > sizeof(last_code_line)-2) {
553       fputs("input string too long\n", stderr);
554       continue;
555     }
556     strcpy(last_code_line, line);
557     strcat(last_code_line, "\n");
558     MIRB_ADD_HISTORY(line);
559     MIRB_LINE_FREE(line);
560 #endif
561 
562   done:
563     if (code_block_open) {
564       if (strlen(ruby_code)+strlen(last_code_line) > sizeof(ruby_code)-1) {
565         fputs("concatenated input string too long\n", stderr);
566         continue;
567       }
568       strcat(ruby_code, last_code_line);
569     }
570     else {
571       if (check_keyword(last_code_line, "quit") || check_keyword(last_code_line, "exit")) {
572         break;
573       }
574       strcpy(ruby_code, last_code_line);
575     }
576 
577     utf8 = mrb_utf8_from_locale(ruby_code, -1);
578     if (!utf8) abort();
579 
580     /* parse code */
581     parser = mrb_parser_new(mrb);
582     if (parser == NULL) {
583       fputs("create parser state error\n", stderr);
584       break;
585     }
586     parser->s = utf8;
587     parser->send = utf8 + strlen(utf8);
588     parser->lineno = cxt->lineno;
589     mrb_parser_parse(parser, cxt);
590     code_block_open = is_code_block_open(parser);
591     mrb_utf8_free(utf8);
592 
593     if (code_block_open) {
594       /* no evaluation of code */
595     }
596     else {
597       if (0 < parser->nwarn) {
598         /* warning */
599         char* msg = mrb_locale_from_utf8(parser->warn_buffer[0].message, -1);
600         printf("line %d: %s\n", parser->warn_buffer[0].lineno, msg);
601         mrb_locale_free(msg);
602       }
603       if (0 < parser->nerr) {
604         /* syntax error */
605         char* msg = mrb_locale_from_utf8(parser->error_buffer[0].message, -1);
606         printf("line %d: %s\n", parser->error_buffer[0].lineno, msg);
607         mrb_locale_free(msg);
608       }
609       else {
610         /* generate bytecode */
611         struct RProc *proc = mrb_generate_code(mrb, parser);
612         if (proc == NULL) {
613           fputs("codegen error\n", stderr);
614           mrb_parser_free(parser);
615           break;
616         }
617 
618         if (args.verbose) {
619           mrb_codedump_all(mrb, proc);
620         }
621         /* adjust stack length of toplevel environment */
622         if (mrb->c->cibase->env) {
623           struct REnv *e = mrb->c->cibase->env;
624           if (e && MRB_ENV_STACK_LEN(e) < proc->body.irep->nlocals) {
625             MRB_ENV_SET_STACK_LEN(e, proc->body.irep->nlocals);
626           }
627         }
628         /* pass a proc for evaluation */
629         /* evaluate the bytecode */
630         result = mrb_vm_run(mrb,
631             proc,
632             mrb_top_self(mrb),
633             stack_keep);
634         stack_keep = proc->body.irep->nlocals;
635         /* did an exception occur? */
636         if (mrb->exc) {
637           p(mrb, mrb_obj_value(mrb->exc), 0);
638           mrb->exc = 0;
639         }
640         else {
641           /* no */
642           if (!mrb_respond_to(mrb, result, mrb_intern_lit(mrb, "inspect"))){
643             result = mrb_any_to_s(mrb, result);
644           }
645           p(mrb, result, 1);
646         }
647       }
648       ruby_code[0] = '\0';
649       last_code_line[0] = '\0';
650       mrb_gc_arena_restore(mrb, ai);
651     }
652     mrb_parser_free(parser);
653     cxt->lineno++;
654     MRB_CATCH(&c_jmp) {
655       p(mrb, mrb_obj_value(mrb->exc), 0);
656       mrb->exc = 0;
657     }
658     MRB_END_EXC(&c_jmp);
659   }
660 
661 #ifdef ENABLE_READLINE
662   MIRB_WRITE_HISTORY(history_path);
663   mrb_free(mrb, history_path);
664 #endif
665 
666   if (args.rfp) fclose(args.rfp);
667   mrb_free(mrb, args.argv);
668   if (args.libv) {
669     for (i = 0; i < args.libc; ++i) {
670       mrb_free(mrb, args.libv[i]);
671     }
672     mrb_free(mrb, args.libv);
673   }
674   mrbc_context_free(mrb, cxt);
675   mrb_close(mrb);
676 
677   return 0;
678 }
679