Exemple #1
0
void
grn_ctx_impl_mrb_init(grn_ctx *ctx)
{
  if (!grn_ctx_impl_mrb_mruby_enabled) {
    ctx->impl->mrb.state = NULL;
    ctx->impl->mrb.base_directory[0] = '\0';
    ctx->impl->mrb.module = NULL;
    ctx->impl->mrb.object_class = NULL;
    ctx->impl->mrb.checked_procs = NULL;
    ctx->impl->mrb.registered_plugins = NULL;
    ctx->impl->mrb.builtin.time_class = NULL;
    ctx->impl->mrb.groonga.operator_class = NULL;
  } else {
    mrb_state *mrb;
    mrb = mrb_open();
    ctx->impl->mrb.state = mrb;
    ctx->impl->mrb.base_directory[0] = '\0';
    grn_ctx_impl_mrb_init_bindings(ctx);
    /* TODO: Implement better error handling on init. */
    if (ctx->impl->mrb.state->exc) {
      mrb_print_error(mrb);
    }
    ctx->impl->mrb.checked_procs =
      grn_hash_create(ctx, NULL, sizeof(grn_id), 0, GRN_HASH_TINY);
    ctx->impl->mrb.registered_plugins =
      grn_hash_create(ctx, NULL, sizeof(grn_id), 0, GRN_HASH_TINY);
    GRN_VOID_INIT(&(ctx->impl->mrb.buffer.from));
    GRN_VOID_INIT(&(ctx->impl->mrb.buffer.to));
    ctx->impl->mrb.builtin.time_class = mrb_class_get(mrb, "Time");
  }
}
Exemple #2
0
grn_obj *
grn_mrb_expr_rewrite(grn_ctx *ctx, grn_obj *expr)
{
  grn_mrb_data *data = &(ctx->impl->mrb);
  mrb_state *mrb = data->state;
  mrb_value mrb_expression;
  mrb_value mrb_rewritten_expression;
  grn_obj *rewritten_expression = NULL;
  int arena_index;

  arena_index = mrb_gc_arena_save(mrb);

  mrb_expression = grn_mrb_value_from_grn_obj(mrb, expr);
  mrb_rewritten_expression = mrb_funcall(mrb, mrb_expression, "rewrite", 0);
  if (mrb_nil_p(mrb_rewritten_expression)) {
    goto exit;
  }

  if (mrb_type(mrb_rewritten_expression) == MRB_TT_EXCEPTION) {
    mrb->exc = mrb_obj_ptr(mrb_rewritten_expression);
    mrb_print_error(mrb);
    goto exit;
  }

  rewritten_expression = DATA_PTR(mrb_rewritten_expression);

exit:
  mrb_gc_arena_restore(mrb, arena_index);

  return rewritten_expression;
}
extern "C" int main(int argc, char* argv[]) {
	mrb_state* const M = Player::create_vm();
	Player::parse_args(argc, argv, M);

#ifdef GEKKO
	// Init libfat (Mount SD/USB)
	if (!fatInitDefault()) {
		Output::Error("Couldn't mount any storage medium!");
	}
	// Wii doesn't provide a correct working directory before mounting
	char gekko_dir[256];
	getcwd(gekko_dir, 255);
	Main_Data::project_path = gekko_dir;
#endif

#if (defined(_WIN32) && defined(NDEBUG) && defined(WINVER) && WINVER >= 0x0600)
	InitMiniDumpWriter();
#endif

	if(! DisplayUi) {
		DisplayUi.reset(new SdlUi(
			SCREEN_TARGET_WIDTH, SCREEN_TARGET_HEIGHT,
			"EasyRPG Player", !Player::window_flag(M)));
	}

	Player::run(M);

	if(M->exc) {
		mrb_print_error(M);
		return EXIT_FAILURE;
	}

	return EXIT_SUCCESS;
}
Exemple #4
0
SEXP dotRb(SEXP args)
{
  SEXP ans;
  char *cmd;
  mrb_value val; //non utilisé pour l'instant!!!

  if(!isValidString(CADR(args)))
    error("invalid argument");
    cmd = (char*)CHAR(STRING_ELT(CADR(args), 0));
//printf("instruction à executer %s\n",cmd);
  val=mrb_load_string_cxt(mrb,cmd,mrb_cxt);
  //printf("state\n");
  if(mrb->exc) {
    if (!mrb_undef_p(val)) {
      mrb_print_error(mrb);
    }
    //rb_p(state);
    printf("MRuby error !!!\n");
    printf("in executing : %s\n",cmd);
    return R_NilValue;
  } else {
    //ans=(SEXP)newRbObj(val);
    ans=mrbArray2RVector(val);
    return ans;
  }
}
Exemple #5
0
int main(int argc, char *argv[])
{
  mrb_state *mrb = mrb_open();
  mrb_value ARGV = mrb_ary_new_capa(mrb, argc);
  int i;
  int return_value;

  struct RClass *c = mrb_define_class(mrb, "Timeout", mrb->object_class);
  mrb_define_method(mrb, c, "alarm", mrb_alarm, MRB_ARGS_REQ(1));

  for (i = 0; i < argc; i++) {
    mrb_ary_push(mrb, ARGV, mrb_str_new_cstr(mrb, argv[i]));
  }
  mrb_define_global_const(mrb, "ARGV", ARGV);

  // call __main__(ARGV)
  mrb_funcall(mrb, mrb_top_self(mrb), "__main__", 1, ARGV);

  return_value = EXIT_SUCCESS;

  if (mrb->exc) {
    mrb_print_error(mrb);
    return_value = EXIT_FAILURE;
  }
  mrb_close(mrb);

  return return_value;
}
Exemple #6
0
void GENERATED_TMP_mrb_mruby_numeric_ext_gem_init(mrb_state *mrb) {
  int ai = mrb_gc_arena_save(mrb);
  mrb_mruby_numeric_ext_gem_init(mrb);
  mrb_load_irep(mrb, gem_mrblib_irep_mruby_numeric_ext);
  if (mrb->exc) {
    mrb_print_error(mrb);
    exit(EXIT_FAILURE);
  }
  mrb_gc_arena_restore(mrb, ai);
}
Exemple #7
0
int main(int argc, char *argv[])
{
  mrb_state *mrb = mrb_open();
  mrb_value ARGV = mrb_ary_new_capa(mrb, argc);
  int i;
  int return_value;

#ifdef HACONIWA_SECURE_RUN
  if(check_match_owner(argc, argv) < 0) {
    mrb_raise(mrb, E_RUNTIME_ERROR,
              "haconiwa runner and hacofile's owner would not be matched: This run prohibited on secure-run build");
    exit(2);
  }
#endif
  if(setuid(geteuid()) < 0) {
    mrb_sys_fail(mrb, "setuid");
    exit(2);
  }
  if(setgid(getegid()) < 0) {
    mrb_sys_fail(mrb, "setgid");
    exit(2);
  }

  for (i = 0; i < argc; i++) {
    mrb_ary_push(mrb, ARGV, mrb_str_new_cstr(mrb, argv[i]));
  }
  mrb_define_global_const(mrb, "ARGV", ARGV);

  // call __main__(ARGV)
  mrb_funcall(mrb, mrb_top_self(mrb), "__main__", 1, ARGV);

  return_value = EXIT_SUCCESS;

  if (mrb->exc) {
    mrb_print_error(mrb);
    return_value = EXIT_FAILURE;
  }
  mrb_close(mrb);

  return return_value;
}
Exemple #8
0
int main(int argc,const char *argv[])
{
	mrb_state *mrb;
	mrb_value ret;

	// initialize mruby
	if (!(mrb = mrb_open()))
	{
		fprintf(stderr,"%s: could not initialize mruby\n",argv[0]);
		return -1;
	}

	mrb_value args = mrb_ary_new(mrb);
	int i;

	// convert argv into mruby strings
	for (i=1; i<argc; i++)
	{
		 mrb_ary_push(mrb, args, mrb_str_new_cstr(mrb,argv[i]));
	}

	mrb_define_global_const(mrb, "ARGV", args);

	// load the compiled library
	ret = lib_init(mrb);

	// check for exception
	if (mrb->exc)
	{
		// print exception
		mrb_print_error(mrb);
	}

	// cleanup
	mrb_close(mrb);
	return mrb->exc != NULL;
}
Exemple #9
0
static int
mrb_actor_pipe_reader(zloop_t* reactor, zsock_t* pipe, void* args)
{
    errno = 0;
    self_t* self = (self_t*)args;
    int rc = 0;
    zmsg_t* msg = zmsg_recv(pipe);
    if (!msg)
        return -1;

    char* command = zmsg_popstr(msg);
    zsys_debug("command: %s", command);
    if (streq(command, "$TERM")) {
        rc = -1;
    }
    else if (streq(command, "BIND ROUTER")) {
        char* endpoint = zmsg_popstr(msg);
        if (zsock_bind(self->router, "%s", endpoint) >= 0) {
            const char* boundendpoint = zsock_endpoint(self->router);
            zyre_set_header(self->discovery, "mrb-actor-v1-router", "%s", boundendpoint);
            zsock_signal(pipe, 0);
            zsock_send(pipe, "s", boundendpoint);
        } else {
            zsock_signal(pipe, 1);
            zsock_send(pipe, "i", errno);
        }
        zstr_free(&endpoint);
    }
    else if (streq(command, "BIND PULL")) {
        char* endpoint = zmsg_popstr(msg);
        if (zsock_bind(self->pull, "%s", endpoint) >= 0) {
            const char* boundendpoint = zsock_endpoint(self->pull);
            zyre_set_header(self->discovery, "mrb-actor-v1-pull", "%s", boundendpoint);
            zsock_signal(pipe, 0);
            zsock_send(pipe, "s", boundendpoint);
        } else {
            zsock_signal(pipe, 1);
            zsock_send(pipe, "i", errno);
        }
        zstr_free(&endpoint);
    }
    else if (streq(command, "ZYRE SET ENDPOINT")) {
        char* endpoint = zmsg_popstr(msg);
        if (zyre_set_endpoint(self->discovery, "%s", endpoint) == -1) {
            zsock_signal(pipe, 1);
        } else {
            zsock_signal(pipe, 0);
        }
        zstr_free(&endpoint);
    }
    else if (streq(command, "ZYRE GOSSIP BIND")) {
        char* endpoint = zmsg_popstr(msg);
        zyre_gossip_bind(self->discovery, "%s", endpoint);
        zstr_free(&endpoint);
    }
    else if (streq(command, "ZYRE GOSSIP CONNECT")) {
        char* endpoint = zmsg_popstr(msg);
        zyre_gossip_connect(self->discovery, "%s", endpoint);
        zstr_free(&endpoint);
    }
    else if (streq(command, "ZYRE START")) {
        if (zyre_start(self->discovery) == -1) {
            zsock_signal(pipe, 1);
        } else {
            zyre_join(self->discovery, "mrb-actor-v1");
            zsock_signal(pipe, 0);
        }
    }
    else if (streq(command, "LOAD IREP FILE")) {
        char* mrb_file = zmsg_popstr(msg);
        mrb_state* mrb = self->mrb;
        int ai = mrb_gc_arena_save(mrb);
        struct mrb_jmpbuf* prev_jmp = mrb->jmp;
        struct mrb_jmpbuf c_jmp;

        MRB_TRY(&c_jmp)
        {
            mrb->jmp = &c_jmp;
            FILE* fp = fopen(mrb_file, "rb");
            if (!fp) {
                mrb_sys_fail(mrb, "fopen");
            }
            mrb_load_irep_file(mrb, fp);
            fclose(fp);
            if (mrb->exc) {
                mrb_exc_raise(mrb, mrb_obj_value(mrb->exc));
            }
            zsock_signal(pipe, 0);
            mrb->jmp = prev_jmp;
        }
        MRB_CATCH(&c_jmp)
        {
            mrb->jmp = prev_jmp;
            mrb_print_error(mrb);
            zsock_signal(pipe, 1);
            mrb->exc = NULL;
        }
        MRB_END_EXC(&c_jmp);
        mrb_gc_arena_restore(mrb, ai);

        zstr_free(&mrb_file);
    }
Exemple #10
0
int
main(int argc, char **argv)
{
  mrb_state *mrb = mrb_open();
  int n = -1;
  struct _args args;
  mrb_value v;
  mrdb_state *mrdb;
  mrdb_state *mrdb_backup;
  mrb_debug_context* dbg_backup;
  debug_command *cmd;

 l_restart:

  if (mrb == NULL) {
    fputs("Invalid mrb_state, exiting mruby\n", stderr);
    return EXIT_FAILURE;
  }

  /* parse command parameters */
  n = parse_args(mrb, argc, argv, &args);
  if (n == EXIT_FAILURE || args.rfp == NULL) {
    cleanup(mrb, &args);
    usage(argv[0]);
    return n;
  }

  /* initialize debugger information */
  mrdb = mrdb_state_get(mrb);
  mrb_assert(mrdb && mrdb->dbg);
  mrdb->srcpath = args.srcpath;

  if(mrdb->dbg->xm == DBG_QUIT) {
    mrdb->dbg->xphase = DBG_PHASE_RESTART;
  }
  else {
    mrdb->dbg->xphase = DBG_PHASE_BEFORE_RUN;
  }
  mrdb->dbg->xm = DBG_INIT;
  mrdb->dbg->ccnt = 1;
  
  /* setup hook functions */
  mrb->code_fetch_hook = mrb_code_fetch_hook;
  mrdb->dbg->break_hook = mrb_debug_break_hook;

  if (args.mrbfile) { /* .mrb */
    v = mrb_load_irep_file(mrb, args.rfp);
  }
  else {              /* .rb */
    mrbc_context *cc = mrbc_context_new(mrb);
    mrbc_filename(mrb, cc, args.fname);
    v = mrb_load_file_cxt(mrb, args.rfp, cc);
    mrbc_context_free(mrb, cc);
  }
  if (mrdb->dbg->xm == DBG_QUIT && !mrb_undef_p(v) && mrb->exc) {
    const char *classname = mrb_obj_classname(mrb, mrb_obj_value(mrb->exc));
    if (!strcmp(classname, "DebuggerExit")) {
      cleanup(mrb, &args);
      return 0;
    }
    if (!strcmp(classname, "DebuggerRestart")) {
      mrdb_backup = mrdb_state_get(mrb);
      dbg_backup = mrb_debug_context_get(mrb);

      mrdb_state_set(NULL);
      mrb_debug_context_set(NULL);

      cleanup(mrb, &args);
      mrb = mrb_open();

      mrdb_state_set(mrdb_backup);
      mrb_debug_context_set(dbg_backup);

      goto l_restart;
    }
  }
  puts("mruby application exited.");
  mrdb->dbg->xphase = DBG_PHASE_AFTER_RUN;
  if (!mrb_undef_p(v)) {
    if (mrb->exc) {
      mrb_print_error(mrb);
    }
    else {
      printf(" => ");
      mrb_p(mrb, v);
    }
  }
  
  mrdb->dbg->prvfile = "-";
  mrdb->dbg->prvline = 0;
  
  while (1) {
    cmd = get_and_parse_command(mrb, mrdb);
    mrb_assert(cmd);
    
    if (cmd->id == DBGCMD_QUIT) {
      break;
    }
    
    if( cmd->func(mrb, mrdb) == DBGST_RESTART ) goto l_restart;
  }
  
  cleanup(mrb, &args);

  return 0;
}
Exemple #11
0
int main(int argc, char const *argv[])
{
#ifdef _MEM_PROFILER
  uint8_t checkpoint_set = 0;
#endif
  fd_set rfds;
  char buffer[PIPE_BUFFER_SIZE];
  int i, n;
  Plugin plugins[MAX_PLUGINS];
  int plugins_count = 0;
  mrb_state *mrb;
  mrb_value r_output, r_plugins_list;
  mrb_sym output_gv_sym, plugins_to_load_gv_sym;
  
  printf("Version: %s\n", PROBE_VERSION);
  
  if( argc != 2 ){
    printf("Usage: %s <config_path>\n", argv[0]);
    exit(1);
  }
  
#ifdef _MEM_PROFILER
  init_profiler();
#endif
  
  config_path = argv[1];
  
  printf("Initializing core...\n");
  mrb = mrb_open_allocf(profiler_allocf, "main");
  output_gv_sym = mrb_intern_cstr(mrb, "$output");
  plugins_to_load_gv_sym = mrb_intern_cstr(mrb, "$plugins_to_load");
  setup_api(mrb);
  execute_file(mrb, "plugins/main.rb");
  execute_file(mrb, config_path);
  
  printf("Loading plugins...\n");
  r_plugins_list = mrb_gv_get(mrb, plugins_to_load_gv_sym);
  for(i = 0; i< mrb_ary_len(mrb, r_plugins_list); i++){
    char *path, tmp[100];
    int ssize;
    
    mrb_value r_plugin_name = mrb_ary_ref(mrb, r_plugins_list, i);
    const char *plugin_name = mrb_string_value_cstr(mrb, &r_plugin_name);
    
    snprintf(tmp, sizeof(tmp) - 1, "plugins/%s.rb", plugin_name);
    ssize = strlen(tmp);
    
    path = malloc(ssize + 1);
    strncpy(path, tmp, ssize);
    path[ssize] = '\0';
    
    if( access(path, F_OK) == -1 ){
      printf("cannot open plugin file \"%s\": %s\n", path, strerror(errno));
      exit(1);
    }
    
    init_plugin_from_file(&plugins[plugins_count], path, plugin_name); plugins_count++;
  }
  
  printf("Instanciating output class...\n");
  r_output = mrb_gv_get(mrb, output_gv_sym);
  interval = mrb_fixnum(mrb_funcall(mrb, r_output, "interval", 0));
  
  printf("Interval set to %dms\n", (int)interval);
  
  printf("Sending initial report...\n");
  mrb_funcall(mrb, r_output, "send_report", 0);
  
  if (mrb->exc) {
    mrb_print_error(mrb);
    
    exit(1);
  }

  
  // start all the threads
  for(i= 0; i< plugins_count; i++){
    // printf("== plugin %d\n", i);
    n = pthread_create(&plugins[i].thread, NULL, plugin_thread, (void *)&plugins[i]);
    if( n < 0 ){
      fprintf(stderr, "create failed\n");
    }
  }
  
  if( signal(SIGINT, clean_exit) == SIG_ERR){
    perror("signal");
    exit(1);
  }
  
  while(running){
    int fds[MAX_PLUGINS];
    int maxfd = 0, ai;
    struct timeval tv;
    mrb_value r_buffer;
    struct timeval cycle_started_at, cycle_completed_at;
    
    gettimeofday(&cycle_started_at, NULL);
    
    bzero(fds, sizeof(int) * MAX_PLUGINS);
    
    // ask every plugin to send their data
    for(i= 0; i< plugins_count; i++){
      strcpy(buffer, "request");
      if( send(plugins[i].host_pipe, buffer, strlen(buffer), 0) == -1 ){
        printf("send error when writing in pipe connected to plugin '%s'\n", plugins[i].name);
      }
      fds[i] = plugins[i].host_pipe;
      // printf("sent request to %d\n", i);
    }
    
    // printf("waiting answers...\n");
    // and now wait for each answer
    while(1){
      int left = 0;
      
      FD_ZERO(&rfds);
      
      for(i = 0; i< MAX_PLUGINS; i++){
        if( fds[i] != NOPLUGIN_VALUE ){
          FD_SET(fds[i], &rfds);
          left++;
          if( fds[i] > maxfd )
            maxfd = fds[i];
        }
      }
      
      // printf("left: %d %d\n", left, left <= 0);
      
      if( !running || (0 == left) )
        break;
      
      // substract 20ms to stay below the loop delay
      fill_timeout(&tv, cycle_started_at, interval - 20);
      // printf("before select\n");
      n = select(maxfd + 1, &rfds, NULL, NULL, &tv);
      // printf("after select: %d\n", n);
      if( n > 0 ){
        // find out which pipes have data
        for(i = 0; i< MAX_PLUGINS; i++){
          if( (fds[i] != NOPLUGIN_VALUE) && FD_ISSET(fds[i], &rfds) ){
            while (1){
              struct timeval answered_at;
              n = read(fds[i], buffer, sizeof(buffer));
              if( n == -1 ){
                if( errno != EAGAIN )
                  perror("read");
                break;
              }
              
              if( n == PIPE_BUFFER_SIZE ){
                printf("PIPE_BUFFER_SIZE is too small, increase it ! (value: %d)\n", PIPE_BUFFER_SIZE);
                continue;
              }
              
              gettimeofday(&answered_at, NULL);
              // printf("received answer from %s in %u ms\n", (const char *) plugins[i].mrb->ud,
              //     (uint32_t)((answered_at.tv_sec - cycle_started_at.tv_sec) * 1000 +
              //     (answered_at.tv_usec - cycle_started_at.tv_usec) / 1000)
              //   );
              
              buffer[n] = 0x00;
              
              ai = mrb_gc_arena_save(mrb);
              r_buffer = mrb_str_buf_new(mrb, n);
              mrb_str_buf_cat(mrb, r_buffer, buffer, n);
              
              // mrb_funcall(mrb, r_output, "tick", 0);
              mrb_funcall(mrb, r_output, "add", 1, r_buffer);
              check_exception("add", mrb);
              
              // pp(mrb, r_output, 0);
              
              mrb_gc_arena_restore(mrb, ai);
            }
            
            fds[i] = 0;
          }
        }
      }
      else if( n == 0 )  {
        printf("no responses received from %d plugins.\n", left);
        break;
        // timeout
      }
      else {
        perror("select");
      }
    }
    
    int idx = mrb_gc_arena_save(mrb);
    mrb_funcall(mrb, r_output, "flush", 0);
    check_exception("flush", mrb);
    mrb_gc_arena_restore(mrb, idx);
    
    // and now sleep until the next cycle
    gettimeofday(&cycle_completed_at, NULL);
    
  #ifdef _MEM_PROFILER
    if( checkpoint_set ){
      print_allocations();
    }
  #endif
    
    // force a gc run at the end of each cycle
    mrb_full_gc(mrb);
    // printf("[main] capa: %d / %d\n", mrb->arena_idx, mrb->arena_capa);
    // for(i= 0; i< plugins_count; i++){
    //   printf("[%s] capa: %d / %d\n", plugins[i].name, plugins[i].mrb->arena_idx, plugins[i].mrb->arena_capa);
    // }
  
  #ifdef _MEM_PROFILER
    checkpoint_set = 1;
    // and set starting point
    profiler_set_checkpoint();
  #endif

    
  #ifdef _MEM_PROFILER_RUBY
    // dump VMS state
    dump_state(mrb);
    for(i= 0; i< plugins_count; i++){
      dump_state(plugins[i].mrb);
    }
  #endif
    
    fflush(stdout);
    sleep_delay(&cycle_started_at, &cycle_completed_at, interval);
  }
  
  printf("Sending exit signal to all plugins...\n");
  strcpy(buffer, "exit");
  for(i= 0; i< plugins_count; i++){
    C_CHECK("send", send(plugins[i].host_pipe, buffer, strlen(buffer), 0) );
  }
  
  printf("Giving some time for threads to exit...\n\n");
  really_sleep(2000);
  
  
  for(i= 0; i< plugins_count; i++){
    int ret = pthread_kill(plugins[i].thread, 0);
    
    // if a success is returned then the thread is still alive
    // which means the thread did not acknoledged the exit message
    // kill it.
    if( ret == 0 ){
      printf("    - plugin \"%s\" failed to exit properly, killing it...\n", (const char *) plugins[i].mrb->ud);
      pthread_cancel(plugins[i].thread);
    }
    else {
      printf("    - plugin \"%s\" exited properly.\n", (const char *) plugins[i].mrb->allocf_ud);
    }
    
    if( pthread_join(plugins[i].thread, NULL) < 0){
      fprintf(stderr, "join failed\n");
    }
    
    mrb_close(plugins[i].mrb);
  }
  
  mrb_close(mrb);
  
  printf("Exited !\n");
  return 0;
}
Exemple #12
0
int
main(int argc, char **argv)
{
  mrb_state *mrb = mrb_open();
  int n = -1;
  int i;
  struct _args args;
  mrb_value ARGV;
  mrbc_context *c;
  mrb_value v;

  if (mrb == NULL) {
    fputs("Invalid mrb_state, exiting mruby\n", stderr);
    return EXIT_FAILURE;
  }

  n = parse_args(mrb, argc, argv, &args);
  if (n == EXIT_FAILURE || (args.cmdline == NULL && args.rfp == NULL)) {
    cleanup(mrb, &args);
    usage(argv[0]);
    return n;
  }

  ARGV = mrb_ary_new_capa(mrb, args.argc);
  for (i = 0; i < args.argc; i++) {
    mrb_ary_push(mrb, ARGV, mrb_str_new(mrb, args.argv[i], strlen(args.argv[i])));
  }
  mrb_define_global_const(mrb, "ARGV", ARGV);

  c = mrbc_context_new(mrb);
  if (args.verbose)
    c->dump_result = TRUE;
  if (args.check_syntax)
    c->no_exec = FALSE;
  if (args.mrbfile) {
    v = mrb_load_irep_file_cxt(mrb, args.rfp, c);
  }
  else {
    mrb_sym zero_sym = mrb_intern_lit(mrb, "$0");

    if (args.rfp) {
      char *cmdline;
      cmdline = args.cmdline ? args.cmdline : "-";
      mrbc_filename(mrb, c, cmdline);
      mrb_gv_set(mrb, zero_sym, mrb_str_new_cstr(mrb, cmdline));
      v = mrb_load_file_cxt(mrb, args.rfp, c);
    }
    else {
      mrbc_filename(mrb, c, "-e");
      mrb_gv_set(mrb, zero_sym, mrb_str_new_lit(mrb, "-e"));
      v = mrb_load_string_cxt(mrb, args.cmdline, c);
    }
  }
  mrbc_context_free(mrb, c);
  if (mrb->exc) {
    if (!mrb_undef_p(v)) {
      mrb_print_error(mrb);
    }
    n = -1;
  }
  else if (args.check_syntax) {
    printf("Syntax OK\n");
  }
  cleanup(mrb, &args);

  return n == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}
Exemple #13
0
int
main(int argc, char **argv)
{
  mrb_state *mrb = mrb_open();
  int n = -1;
  int i;
  struct _args args;
  mrb_value ARGV;
  mrbc_context *c;
  mrb_value v;
  mrb_sym zero_sym;
  struct mrb_jmpbuf c_jmp;
  int ai;

  if (mrb == NULL) {
    fputs("Invalid mrb_state, exiting mruby\n", stderr);
    return EXIT_FAILURE;
  }

  n = parse_args(mrb, argc, argv, &args);
  if (n == EXIT_FAILURE || (args.cmdline == NULL && args.rfp == NULL)) {
    cleanup(mrb, &args);
    usage(argv[0]);
    return n;
  }

  ai = mrb_gc_arena_save(mrb);
  MRB_TRY(&c_jmp) {
    mrb->jmp = &c_jmp;
    ARGV = mrb_ary_new_capa(mrb, args.argc);
    for (i = 0; i < args.argc; i++) {
      char* utf8 = mrb_utf8_from_locale(args.argv[i], -1);
      if (utf8) {
        mrb_ary_push(mrb, ARGV, mrb_str_new_cstr(mrb, utf8));
        mrb_utf8_free(utf8);
      }
    }
    mrb_define_global_const(mrb, "ARGV", ARGV);

    c = mrbc_context_new(mrb);
    if (args.verbose)
      c->dump_result = TRUE;
    if (args.check_syntax)
      c->no_exec = TRUE;

    /* Set $0 */
    zero_sym = mrb_intern_lit(mrb, "$0");
    if (args.rfp) {
      const char *cmdline;
      cmdline = args.cmdline ? args.cmdline : "-";
      mrbc_filename(mrb, c, cmdline);
      mrb_gv_set(mrb, zero_sym, mrb_str_new_cstr(mrb, cmdline));
    }
    else {
      mrbc_filename(mrb, c, "-e");
      mrb_gv_set(mrb, zero_sym, mrb_str_new_lit(mrb, "-e"));
    }

    /* Load program */
    if (args.mrbfile) {
      v = mrb_load_irep_file_cxt(mrb, args.rfp, c);
    }
    else if (args.rfp) {
      v = mrb_load_file_cxt(mrb, args.rfp, c);
    }
    else {
      char* utf8 = mrb_utf8_from_locale(args.cmdline, -1);
      if (!utf8) abort();
      v = mrb_load_string_cxt(mrb, utf8, c);
      mrb_utf8_free(utf8);
    }

    mrb_gc_arena_restore(mrb, ai);
    mrbc_context_free(mrb, c);
    if (mrb->exc) {
      if (!mrb_undef_p(v)) {
        mrb_print_error(mrb);
      }
      n = -1;
    }
    else if (args.check_syntax) {
      printf("Syntax OK\n");
    }
  }
  MRB_CATCH(&c_jmp) {           /* error */
  }
  MRB_END_EXC(&c_jmp);
  cleanup(mrb, &args);

  return n == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}