- Timestamp:
- Jul 9, 2020, 8:51:43 AM (4 years ago)
- Location:
- EcnlProtoTool/trunk/mruby-2.1.1
- Files:
-
- 1 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
EcnlProtoTool/trunk/mruby-2.1.1/mrbgems/mruby-bin-mirb/tools/mirb/mirb.c
r331 r439 7 7 */ 8 8 9 #include <mruby.h> 10 11 #ifdef MRB_DISABLE_STDIO 12 # error mruby-bin-mirb conflicts 'MRB_DISABLE_STDIO' configuration in your 'build_config.rb' 13 #endif 14 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> 22 9 23 #include <stdlib.h> 10 24 #include <string.h> 11 #include <stdio.h>12 25 #include <ctype.h> 13 26 … … 20 33 #define MIRB_ADD_HISTORY(line) add_history(line) 21 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) 38 #else 39 #define MIRB_LINE_FREE(line) rl_free(line) 40 #endif 22 41 #define MIRB_WRITE_HISTORY(path) write_history(path) 23 42 #define MIRB_READ_HISTORY(path) read_history(path) … … 28 47 #define MIRB_ADD_HISTORY(line) linenoiseHistoryAdd(line) 29 48 #define MIRB_READLINE(ch) linenoise(ch) 49 #define MIRB_LINE_FREE(line) linenoiseFree(line) 30 50 #define MIRB_WRITE_HISTORY(path) linenoiseHistorySave(path) 31 51 #define MIRB_READ_HISTORY(path) linenoiseHistoryLoad(history_path) … … 42 62 #define SIGJMP_BUF jmp_buf 43 63 #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 64 51 65 #ifdef ENABLE_READLINE … … 89 103 { 90 104 mrb_value val; 105 char* msg; 91 106 92 107 val = mrb_funcall(mrb, obj, "inspect", 0); … … 102 117 val = mrb_obj_as_string(mrb, obj); 103 118 } 104 fwrite(RSTRING_PTR(val), RSTRING_LEN(val), 1, stdout); 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); 105 122 putc('\n', stdout); 106 123 } … … 115 132 /* check for heredoc */ 116 133 if (parser->parsing_heredoc != NULL) return TRUE; 117 if (parser->heredoc_end_now) {118 parser->heredoc_end_now = FALSE;119 return FALSE;120 }121 134 122 135 /* check for unterminated string */ … … 210 223 FILE *rfp; 211 224 mrb_bool verbose : 1; 225 mrb_bool debug : 1; 212 226 int argc; 213 227 char** argv; 228 int libc; 229 char **libv; 214 230 }; 215 231 … … 219 235 static const char *const usage_msg[] = { 220 236 "switches:", 237 "-d set $DEBUG to true (same as `mruby -d`)", 238 "-r library same as `mruby -r`", 221 239 "-v print version number, then run in verbose mode", 222 240 "--verbose run in verbose mode", … … 227 245 const char *const *p = usage_msg; 228 246 229 printf("Usage: %s [switches] \n", name);247 printf("Usage: %s [switches] [programfile] [arguments]\n", name); 230 248 while (*p) 231 249 printf(" %s\n", *p++); 232 250 } 233 251 252 static char * 253 dup_arg_item(mrb_state *mrb, const char *item) 254 { 255 size_t buflen = strlen(item) + 1; 256 char *buf = (char*)mrb_malloc(mrb, buflen); 257 memcpy(buf, item, buflen); 258 return buf; 259 } 260 234 261 static int 235 262 parse_args(mrb_state *mrb, int argc, char **argv, struct _args *args) 236 263 { 264 char **origargv = argv; 237 265 static const struct _args args_zero = { 0 }; 238 266 … … 245 273 item = argv[0] + 1; 246 274 switch (*item++) { 275 case 'd': 276 args->debug = TRUE; 277 break; 278 case 'r': 279 if (!item[0]) { 280 if (argc <= 1) { 281 printf("%s: No library specified for -r\n", *origargv); 282 return EXIT_FAILURE; 283 } 284 argc--; argv++; 285 item = argv[0]; 286 } 287 if (args->libc == 0) { 288 args->libv = (char**)mrb_malloc(mrb, sizeof(char*)); 289 } 290 else { 291 args->libv = (char**)mrb_realloc(mrb, args->libv, sizeof(char*) * (args->libc + 1)); 292 } 293 args->libv[args->libc++] = dup_arg_item(mrb, item); 294 break; 247 295 case 'v': 248 296 if (!args->verbose) mrb_show_version(mrb); … … 290 338 fclose(args->rfp); 291 339 mrb_free(mrb, args->argv); 340 if (args->libc) { 341 while (args->libc--) { 342 mrb_free(mrb, args->libv[args->libc]); 343 } 344 mrb_free(mrb, args->libv); 345 } 292 346 mrb_close(mrb); 293 347 } … … 324 378 325 379 /* skip preceding spaces */ 326 while (*p && isspace((unsigned char)*p)) {380 while (*p && ISSPACE(*p)) { 327 381 p++; 328 382 } … … 334 388 /* skip trailing spaces */ 335 389 while (*p) { 336 if (! isspace((unsigned char)*p)) return 0;390 if (!ISSPACE(*p)) return 0; 337 391 p++; 338 392 } … … 354 408 { 355 409 MIRB_SIGLONGJMP(ctrl_c_buf, 1); 410 } 411 #endif 412 413 #ifndef DISABLE_MIRB_UNDERSCORE 414 void decl_lv_underscore(mrb_state *mrb, mrbc_context *cxt) 415 { 416 struct RProc *proc; 417 struct mrb_parser_state *parser; 418 419 parser = mrb_parse_string(mrb, "_=nil", cxt); 420 if (parser == NULL) { 421 fputs("create parser state error\n", stderr); 422 mrb_close(mrb); 423 exit(EXIT_FAILURE); 424 } 425 426 proc = mrb_generate_code(mrb, parser); 427 mrb_vm_run(mrb, proc, mrb_top_self(mrb), 0); 428 429 mrb_parser_free(parser); 356 430 } 357 431 #endif … … 404 478 } 405 479 mrb_define_global_const(mrb, "ARGV", ARGV); 480 mrb_gv_set(mrb, mrb_intern_lit(mrb, "$DEBUG"), mrb_bool_value(args.debug)); 406 481 407 482 #ifdef ENABLE_READLINE … … 420 495 421 496 cxt = mrbc_context_new(mrb); 497 498 #ifndef DISABLE_MIRB_UNDERSCORE 499 decl_lv_underscore(mrb, cxt); 500 #endif 501 502 /* Load libraries */ 503 for (i = 0; i < args.libc; i++) { 504 FILE *lfp = fopen(args.libv[i], "r"); 505 if (lfp == NULL) { 506 printf("Cannot open library file. (%s)\n", args.libv[i]); 507 cleanup(mrb, &args); 508 return EXIT_FAILURE; 509 } 510 mrb_load_file_cxt(mrb, lfp, cxt); 511 fclose(lfp); 512 } 513 422 514 cxt->capture_errors = TRUE; 423 515 cxt->lineno = 1; … … 429 521 while (TRUE) { 430 522 char *utf8; 431 523 struct mrb_jmpbuf c_jmp; 524 525 MRB_TRY(&c_jmp); 526 mrb->jmp = &c_jmp; 432 527 if (args.rfp) { 433 528 if (fgets(last_code_line, sizeof(last_code_line)-1, args.rfp) != NULL) … … 490 585 strcat(last_code_line, "\n"); 491 586 MIRB_ADD_HISTORY(line); 492 free(line); 493 #endif 494 495 done: 496 587 MIRB_LINE_FREE(line); 588 #endif 589 590 done: 497 591 if (code_block_open) { 498 592 if (strlen(ruby_code)+strlen(last_code_line) > sizeof(ruby_code)-1) { … … 529 623 } 530 624 else { 625 if (0 < parser->nwarn) { 626 /* warning */ 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); 630 } 531 631 if (0 < parser->nerr) { 532 632 /* syntax error */ 533 printf("line %d: %s\n", parser->error_buffer[0].lineno, parser->error_buffer[0].message); 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); 534 636 } 535 637 else { … … 544 646 if (args.verbose) { 545 647 mrb_codedump_all(mrb, proc); 648 } 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_STACK_LEN(e) < proc->body.irep->nlocals) { 653 MRB_ENV_SET_STACK_LEN(e, proc->body.irep->nlocals); 654 } 546 655 } 547 656 /* pass a proc for evaluation */ … … 563 672 } 564 673 p(mrb, result, 1); 674 #ifndef DISABLE_MIRB_UNDERSCORE 675 *(mrb->c->stack + 1) = result; 676 #endif 565 677 } 566 678 } … … 571 683 mrb_parser_free(parser); 572 684 cxt->lineno++; 685 MRB_CATCH(&c_jmp) { 686 p(mrb, mrb_obj_value(mrb->exc), 0); 687 mrb->exc = 0; 688 } 689 MRB_END_EXC(&c_jmp); 573 690 } 574 691 … … 578 695 #endif 579 696 697 if (args.rfp) fclose(args.rfp); 698 mrb_free(mrb, args.argv); 699 if (args.libv) { 700 for (i = 0; i < args.libc; ++i) { 701 mrb_free(mrb, args.libv[i]); 702 } 703 mrb_free(mrb, args.libv); 704 } 580 705 mrbc_context_free(mrb, cxt); 581 706 mrb_close(mrb);
Note:
See TracChangeset
for help on using the changeset viewer.