Ignore:
Timestamp:
Jul 9, 2020, 8:51:43 AM (4 years ago)
Author:
coas-nagasima
Message:

mrubyを2.1.1に更新

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  
    77*/
    88
     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
    923#include <stdlib.h>
    1024#include <string.h>
    11 #include <stdio.h>
    1225#include <ctype.h>
    1326
     
    2033#define MIRB_ADD_HISTORY(line) add_history(line)
    2134#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
    2241#define MIRB_WRITE_HISTORY(path) write_history(path)
    2342#define MIRB_READ_HISTORY(path) read_history(path)
     
    2847#define MIRB_ADD_HISTORY(line) linenoiseHistoryAdd(line)
    2948#define MIRB_READLINE(ch) linenoise(ch)
     49#define MIRB_LINE_FREE(line) linenoiseFree(line)
    3050#define MIRB_WRITE_HISTORY(path) linenoiseHistorySave(path)
    3151#define MIRB_READ_HISTORY(path) linenoiseHistoryLoad(history_path)
     
    4262#define SIGJMP_BUF jmp_buf
    4363#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>
    5064
    5165#ifdef ENABLE_READLINE
     
    89103{
    90104  mrb_value val;
     105  char* msg;
    91106
    92107  val = mrb_funcall(mrb, obj, "inspect", 0);
     
    102117    val = mrb_obj_as_string(mrb, obj);
    103118  }
    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);
    105122  putc('\n', stdout);
    106123}
     
    115132  /* check for heredoc */
    116133  if (parser->parsing_heredoc != NULL) return TRUE;
    117   if (parser->heredoc_end_now) {
    118     parser->heredoc_end_now = FALSE;
    119     return FALSE;
    120   }
    121134
    122135  /* check for unterminated string */
     
    210223  FILE *rfp;
    211224  mrb_bool verbose      : 1;
     225  mrb_bool debug        : 1;
    212226  int argc;
    213227  char** argv;
     228  int libc;
     229  char **libv;
    214230};
    215231
     
    219235  static const char *const usage_msg[] = {
    220236  "switches:",
     237  "-d           set $DEBUG to true (same as `mruby -d`)",
     238  "-r library   same as `mruby -r`",
    221239  "-v           print version number, then run in verbose mode",
    222240  "--verbose    run in verbose mode",
     
    227245  const char *const *p = usage_msg;
    228246
    229   printf("Usage: %s [switches]\n", name);
     247  printf("Usage: %s [switches] [programfile] [arguments]\n", name);
    230248  while (*p)
    231249    printf("  %s\n", *p++);
    232250}
    233251
     252static char *
     253dup_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
    234261static int
    235262parse_args(mrb_state *mrb, int argc, char **argv, struct _args *args)
    236263{
     264  char **origargv = argv;
    237265  static const struct _args args_zero = { 0 };
    238266
     
    245273    item = argv[0] + 1;
    246274    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;
    247295    case 'v':
    248296      if (!args->verbose) mrb_show_version(mrb);
     
    290338    fclose(args->rfp);
    291339  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  }
    292346  mrb_close(mrb);
    293347}
     
    324378
    325379  /* skip preceding spaces */
    326   while (*p && isspace((unsigned char)*p)) {
     380  while (*p && ISSPACE(*p)) {
    327381    p++;
    328382  }
     
    334388  /* skip trailing spaces */
    335389  while (*p) {
    336     if (!isspace((unsigned char)*p)) return 0;
     390    if (!ISSPACE(*p)) return 0;
    337391    p++;
    338392  }
     
    354408{
    355409  MIRB_SIGLONGJMP(ctrl_c_buf, 1);
     410}
     411#endif
     412
     413#ifndef DISABLE_MIRB_UNDERSCORE
     414void 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);
    356430}
    357431#endif
     
    404478  }
    405479  mrb_define_global_const(mrb, "ARGV", ARGV);
     480  mrb_gv_set(mrb, mrb_intern_lit(mrb, "$DEBUG"), mrb_bool_value(args.debug));
    406481
    407482#ifdef ENABLE_READLINE
     
    420495
    421496  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
    422514  cxt->capture_errors = TRUE;
    423515  cxt->lineno = 1;
     
    429521  while (TRUE) {
    430522    char *utf8;
    431 
     523    struct mrb_jmpbuf c_jmp;
     524
     525    MRB_TRY(&c_jmp);
     526    mrb->jmp = &c_jmp;
    432527    if (args.rfp) {
    433528      if (fgets(last_code_line, sizeof(last_code_line)-1, args.rfp) != NULL)
     
    490585    strcat(last_code_line, "\n");
    491586    MIRB_ADD_HISTORY(line);
    492     free(line);
    493 #endif
    494 
    495 done:
    496 
     587    MIRB_LINE_FREE(line);
     588#endif
     589
     590  done:
    497591    if (code_block_open) {
    498592      if (strlen(ruby_code)+strlen(last_code_line) > sizeof(ruby_code)-1) {
     
    529623    }
    530624    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      }
    531631      if (0 < parser->nerr) {
    532632        /* 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);
    534636      }
    535637      else {
     
    544646        if (args.verbose) {
    545647          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          }
    546655        }
    547656        /* pass a proc for evaluation */
     
    563672          }
    564673          p(mrb, result, 1);
     674#ifndef DISABLE_MIRB_UNDERSCORE
     675          *(mrb->c->stack + 1) = result;
     676#endif
    565677        }
    566678      }
     
    571683    mrb_parser_free(parser);
    572684    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);
    573690  }
    574691
     
    578695#endif
    579696
     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  }
    580705  mrbc_context_free(mrb, cxt);
    581706  mrb_close(mrb);
Note: See TracChangeset for help on using the changeset viewer.