source: EcnlProtoTool/trunk/mruby-1.3.0/mrbgems/mruby-bin-mirb/tools/mirb/mirb.c@ 331

Last change on this file since 331 was 331, checked in by coas-nagasima, 6 years ago

prototoolに関連するプロジェクトをnewlibからmuslを使うよう変更・更新
ntshellをnewlibの下位の実装から、muslのsyscallの実装に変更・更新
以下のOSSをアップデート
・mruby-1.3.0
・musl-1.1.18
・onigmo-6.1.3
・tcc-0.9.27
以下のOSSを追加
・openssl-1.1.0e
・curl-7.57.0
・zlib-1.2.11
以下のmrbgemsを追加
・iij/mruby-digest
・iij/mruby-env
・iij/mruby-errno
・iij/mruby-iijson
・iij/mruby-ipaddr
・iij/mruby-mock
・iij/mruby-require
・iij/mruby-tls-openssl

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