2 ** mirb - Embeddable Interactive Ruby Shell
4 ** This program takes code from the user in
5 ** an interactive way and executes it
6 ** immediately. It's a REPL...
11 #ifdef MRB_DISABLE_STDIO
12 # error mruby-bin-mirb conflicts 'MRB_DISABLE_STDIO' configuration in your 'build_config.rb'
15 #include <mruby/array.h>
16 #include <mruby/proc.h>
17 #include <mruby/compile.h>
18 #include <mruby/dump.h>
19 #include <mruby/string.h>
20 #include <mruby/variable.h>
21 #include <mruby/throw.h>
30 #ifdef ENABLE_READLINE
31 #include <readline/readline.h>
32 #include <readline/history.h>
33 #define MIRB_ADD_HISTORY(line) add_history(line)
34 #define MIRB_READLINE(ch) readline(ch)
35 #if !defined(RL_READLINE_VERSION) || RL_READLINE_VERSION < 0x600
36 /* libedit & older readline do not have rl_free() */
37 #define MIRB_LINE_FREE(line) free(line)
39 #define MIRB_LINE_FREE(line) rl_free(line)
41 #define MIRB_WRITE_HISTORY(path) write_history(path)
42 #define MIRB_READ_HISTORY(path) read_history(path)
43 #define MIRB_USING_HISTORY() using_history()
44 #elif defined(ENABLE_LINENOISE)
45 #define ENABLE_READLINE
46 #include <linenoise.h>
47 #define MIRB_ADD_HISTORY(line) linenoiseHistoryAdd(line)
48 #define MIRB_READLINE(ch) linenoise(ch)
49 #define MIRB_LINE_FREE(line) linenoiseFree(line)
50 #define MIRB_WRITE_HISTORY(path) linenoiseHistorySave(path)
51 #define MIRB_READ_HISTORY(path) linenoiseHistoryLoad(history_path)
52 #define MIRB_USING_HISTORY()
56 #define MIRB_SIGSETJMP(env) sigsetjmp(env, 1)
57 #define MIRB_SIGLONGJMP(env, val) siglongjmp(env, val)
58 #define SIGJMP_BUF sigjmp_buf
60 #define MIRB_SIGSETJMP(env) setjmp(env)
61 #define MIRB_SIGLONGJMP(env, val) longjmp(env, val)
62 #define SIGJMP_BUF jmp_buf
65 #ifdef ENABLE_READLINE
67 static const char history_file_name[] = ".mirb_history";
70 get_history_path(mrb_state *mrb)
73 const char *home = getenv("HOME");
77 home = getenv("USERPROFILE");
82 int len = snprintf(NULL, 0, "%s/%s", home, history_file_name);
84 size_t size = len + 1;
85 path = (char *)mrb_malloc_simple(mrb, size);
87 int n = snprintf(path, size, "%s/%s", home, history_file_name);
102 p(mrb_state *mrb, mrb_value obj, int prompt)
107 val = mrb_funcall(mrb, obj, "inspect", 0);
110 fputs(" => ", stdout);
113 val = mrb_funcall(mrb, mrb_obj_value(mrb->exc), "inspect", 0);
116 if (!mrb_string_p(val)) {
117 val = mrb_obj_as_string(mrb, obj);
119 msg = mrb_locale_from_utf8(RSTRING_PTR(val), (int)RSTRING_LEN(val));
120 fwrite(msg, strlen(msg), 1, stdout);
121 mrb_locale_free(msg);
125 /* Guess if the user might want to enter more
126 * or if he wants an evaluation of his code now */
128 is_code_block_open(struct mrb_parser_state *parser)
130 mrb_bool code_block_open = FALSE;
132 /* check for heredoc */
133 if (parser->parsing_heredoc != NULL) return TRUE;
135 /* check for unterminated string */
136 if (parser->lex_strterm) return TRUE;
138 /* check if parser error are available */
139 if (0 < parser->nerr) {
140 const char unexpected_end[] = "syntax error, unexpected $end";
141 const char *message = parser->error_buffer[0].message;
143 /* a parser error occur, we have to check if */
144 /* we need to read one more line or if there is */
145 /* a different issue which we have to show to */
148 if (strncmp(message, unexpected_end, sizeof(unexpected_end) - 1) == 0) {
149 code_block_open = TRUE;
151 else if (strcmp(message, "syntax error, unexpected keyword_end") == 0) {
152 code_block_open = FALSE;
154 else if (strcmp(message, "syntax error, unexpected tREGEXP_BEG") == 0) {
155 code_block_open = FALSE;
157 return code_block_open;
160 switch (parser->lstate) {
162 /* all states which need more code */
165 /* beginning of a statement, */
166 /* that means previous line ended */
167 code_block_open = FALSE;
170 /* a message dot was the last token, */
171 /* there has to come more */
172 code_block_open = TRUE;
175 /* a class keyword is not enough! */
176 /* we need also a name of the class */
177 code_block_open = TRUE;
180 /* a method name is necessary */
181 code_block_open = TRUE;
184 /* if, elsif, etc. without condition */
185 code_block_open = TRUE;
188 /* now all the states which are closed */
191 /* an argument is the last token */
192 code_block_open = FALSE;
195 /* all states which are unsure */
200 /* an expression was ended */
203 /* closing parenthese */
209 /* jump keyword like break, return, ... */
212 /* don't know what to do with this token */
215 /* this state is unexpected! */
219 return code_block_open;
224 mrb_bool verbose : 1;
233 usage(const char *name)
235 static const char *const usage_msg[] = {
237 "-d set $DEBUG to true (same as `mruby -d`)",
238 "-r library same as `mruby -r`",
239 "-v print version number, then run in verbose mode",
240 "--verbose run in verbose mode",
241 "--version print the version",
242 "--copyright print the copyright",
245 const char *const *p = usage_msg;
247 printf("Usage: %s [switches] [programfile] [arguments]\n", name);
249 printf(" %s\n", *p++);
253 dup_arg_item(mrb_state *mrb, const char *item)
255 size_t buflen = strlen(item) + 1;
256 char *buf = (char*)mrb_malloc(mrb, buflen);
257 memcpy(buf, item, buflen);
262 parse_args(mrb_state *mrb, int argc, char **argv, struct _args *args)
264 char **origargv = argv;
265 static const struct _args args_zero = { 0 };
269 for (argc--,argv++; argc > 0; argc--,argv++) {
271 if (argv[0][0] != '-') break;
281 printf("%s: No library specified for -r\n", *origargv);
287 if (args->libc == 0) {
288 args->libv = (char**)mrb_malloc(mrb, sizeof(char*));
291 args->libv = (char**)mrb_realloc(mrb, args->libv, sizeof(char*) * (args->libc + 1));
293 args->libv[args->libc++] = dup_arg_item(mrb, item);
296 if (!args->verbose) mrb_show_version(mrb);
297 args->verbose = TRUE;
300 if (strcmp((*argv) + 2, "version") == 0) {
301 mrb_show_version(mrb);
304 else if (strcmp((*argv) + 2, "verbose") == 0) {
305 args->verbose = TRUE;
308 else if (strcmp((*argv) + 2, "copyright") == 0) {
309 mrb_show_copyright(mrb);
317 if (args->rfp == NULL) {
319 args->rfp = fopen(argv[0], "r");
320 if (args->rfp == NULL) {
321 printf("Cannot open program file. (%s)\n", *argv);
327 args->argv = (char **)mrb_realloc(mrb, args->argv, sizeof(char*) * (argc + 1));
328 memcpy(args->argv, argv, (argc+1) * sizeof(char*));
335 cleanup(mrb_state *mrb, struct _args *args)
339 mrb_free(mrb, args->argv);
341 while (args->libc--) {
342 mrb_free(mrb, args->libv[args->libc]);
344 mrb_free(mrb, args->libv);
349 /* Print a short remark for the user */
353 printf("mirb - Embeddable Interactive Ruby Shell\n\n");
356 #ifndef ENABLE_READLINE
357 /* Print the command line prompt of the REPL */
359 print_cmdline(int code_block_open)
361 if (code_block_open) {
371 void mrb_codedump_all(mrb_state*, struct RProc*);
374 check_keyword(const char *buf, const char *word)
377 size_t len = strlen(word);
379 /* skip preceding spaces */
380 while (*p && ISSPACE(*p)) {
384 if (strncmp(p, word, len) != 0) {
388 /* skip trailing spaces */
390 if (!ISSPACE(*p)) return 0;
397 #ifndef ENABLE_READLINE
398 volatile sig_atomic_t input_canceled = 0;
400 ctrl_c_handler(int signo)
405 SIGJMP_BUF ctrl_c_buf;
407 ctrl_c_handler(int signo)
409 MIRB_SIGLONGJMP(ctrl_c_buf, 1);
413 #ifndef DISABLE_MIRB_UNDERSCORE
414 void decl_lv_underscore(mrb_state *mrb, mrbc_context *cxt)
417 struct mrb_parser_state *parser;
419 parser = mrb_parse_string(mrb, "_=nil", cxt);
420 if (parser == NULL) {
421 fputs("create parser state error\n", stderr);
426 proc = mrb_generate_code(mrb, parser);
427 mrb_vm_run(mrb, proc, mrb_top_self(mrb), 0);
429 mrb_parser_free(parser);
434 main(int argc, char **argv)
436 char ruby_code[4096] = { 0 };
437 char last_code_line[1024] = { 0 };
438 #ifndef ENABLE_READLINE
446 struct mrb_parser_state *parser;
453 mrb_bool code_block_open = FALSE;
455 unsigned int stack_keep = 0;
457 /* new interpreter instance */
460 fputs("Invalid mrb interpreter, exiting mirb\n", stderr);
464 n = parse_args(mrb, argc, argv, &args);
465 if (n == EXIT_FAILURE) {
471 ARGV = mrb_ary_new_capa(mrb, args.argc);
472 for (i = 0; i < args.argc; i++) {
473 char* utf8 = mrb_utf8_from_locale(args.argv[i], -1);
475 mrb_ary_push(mrb, ARGV, mrb_str_new_cstr(mrb, utf8));
479 mrb_define_global_const(mrb, "ARGV", ARGV);
480 mrb_gv_set(mrb, mrb_intern_lit(mrb, "$DEBUG"), mrb_bool_value(args.debug));
482 #ifdef ENABLE_READLINE
483 history_path = get_history_path(mrb);
484 if (history_path == NULL) {
485 fputs("failed to get history path\n", stderr);
490 MIRB_USING_HISTORY();
491 MIRB_READ_HISTORY(history_path);
496 cxt = mrbc_context_new(mrb);
498 #ifndef DISABLE_MIRB_UNDERSCORE
499 decl_lv_underscore(mrb, cxt);
503 for (i = 0; i < args.libc; i++) {
504 FILE *lfp = fopen(args.libv[i], "r");
506 printf("Cannot open library file. (%s)\n", args.libv[i]);
510 mrb_load_file_cxt(mrb, lfp, cxt);
514 cxt->capture_errors = TRUE;
516 mrbc_filename(mrb, cxt, "(mirb)");
517 if (args.verbose) cxt->dump_result = TRUE;
519 ai = mrb_gc_arena_save(mrb);
523 struct mrb_jmpbuf c_jmp;
528 if (fgets(last_code_line, sizeof(last_code_line)-1, args.rfp) != NULL)
533 #ifndef ENABLE_READLINE
534 print_cmdline(code_block_open);
536 signal(SIGINT, ctrl_c_handler);
538 while ((last_char = getchar()) != '\n') {
539 if (last_char == EOF) break;
540 if (char_index >= sizeof(last_code_line)-2) {
541 fputs("input string too long\n", stderr);
544 last_code_line[char_index++] = last_char;
546 signal(SIGINT, SIG_DFL);
547 if (input_canceled) {
549 last_code_line[0] = '\0';
550 code_block_open = FALSE;
555 if (last_char == EOF) {
560 last_code_line[char_index++] = '\n';
561 last_code_line[char_index] = '\0';
563 if (MIRB_SIGSETJMP(ctrl_c_buf) == 0) {
568 last_code_line[0] = '\0';
569 code_block_open = FALSE;
572 signal(SIGINT, ctrl_c_handler);
573 line = MIRB_READLINE(code_block_open ? "* " : "> ");
574 signal(SIGINT, SIG_DFL);
580 if (strlen(line) > sizeof(last_code_line)-2) {
581 fputs("input string too long\n", stderr);
584 strcpy(last_code_line, line);
585 strcat(last_code_line, "\n");
586 MIRB_ADD_HISTORY(line);
587 MIRB_LINE_FREE(line);
591 if (code_block_open) {
592 if (strlen(ruby_code)+strlen(last_code_line) > sizeof(ruby_code)-1) {
593 fputs("concatenated input string too long\n", stderr);
596 strcat(ruby_code, last_code_line);
599 if (check_keyword(last_code_line, "quit") || check_keyword(last_code_line, "exit")) {
602 strcpy(ruby_code, last_code_line);
605 utf8 = mrb_utf8_from_locale(ruby_code, -1);
609 parser = mrb_parser_new(mrb);
610 if (parser == NULL) {
611 fputs("create parser state error\n", stderr);
615 parser->send = utf8 + strlen(utf8);
616 parser->lineno = cxt->lineno;
617 mrb_parser_parse(parser, cxt);
618 code_block_open = is_code_block_open(parser);
621 if (code_block_open) {
622 /* no evaluation of code */
625 if (0 < parser->nwarn) {
627 char* msg = mrb_locale_from_utf8(parser->warn_buffer[0].message, -1);
628 printf("line %d: %s\n", parser->warn_buffer[0].lineno, msg);
629 mrb_locale_free(msg);
631 if (0 < parser->nerr) {
633 char* msg = mrb_locale_from_utf8(parser->error_buffer[0].message, -1);
634 printf("line %d: %s\n", parser->error_buffer[0].lineno, msg);
635 mrb_locale_free(msg);
638 /* generate bytecode */
639 struct RProc *proc = mrb_generate_code(mrb, parser);
641 fputs("codegen error\n", stderr);
642 mrb_parser_free(parser);
647 mrb_codedump_all(mrb, proc);
649 /* adjust stack length of toplevel environment */
650 if (mrb->c->cibase->env) {
651 struct REnv *e = mrb->c->cibase->env;
652 if (e && MRB_ENV_LEN(e) < proc->body.irep->nlocals) {
653 MRB_ENV_SET_LEN(e, proc->body.irep->nlocals);
656 /* pass a proc for evaluation */
657 /* evaluate the bytecode */
658 result = mrb_vm_run(mrb,
662 stack_keep = proc->body.irep->nlocals;
663 /* did an exception occur? */
665 p(mrb, mrb_obj_value(mrb->exc), 0);
670 if (!mrb_respond_to(mrb, result, mrb_intern_lit(mrb, "inspect"))){
671 result = mrb_any_to_s(mrb, result);
674 #ifndef DISABLE_MIRB_UNDERSCORE
675 *(mrb->c->stack + 1) = result;
680 last_code_line[0] = '\0';
681 mrb_gc_arena_restore(mrb, ai);
683 mrb_parser_free(parser);
686 p(mrb, mrb_obj_value(mrb->exc), 0);
692 #ifdef ENABLE_READLINE
693 MIRB_WRITE_HISTORY(history_path);
694 mrb_free(mrb, history_path);
697 if (args.rfp) fclose(args.rfp);
698 mrb_free(mrb, args.argv);
700 for (i = 0; i < args.libc; ++i) {
701 mrb_free(mrb, args.libv[i]);
703 mrb_free(mrb, args.libv);
705 mrbc_context_free(mrb, cxt);