mrb_value grn_mrb_load(grn_ctx *ctx, const char *path) { grn_mrb_data *data = &(ctx->impl->mrb); mrb_state *mrb = data->state; char expanded_path[PATH_MAX]; FILE *file; mrb_value result; struct mrb_parser_state *parser; if (!mrb) { return mrb_nil_value(); } if (!grn_mrb_expand_script_path(ctx, path, expanded_path, PATH_MAX)) { return mrb_nil_value(); } file = grn_fopen(expanded_path, "r"); if (!file) { mrb_value exception; SERR("fopen: failed to open mruby script file: <%s>", expanded_path); exception = mrb_exc_new(mrb, E_LOAD_ERROR, ctx->errbuf, strlen(ctx->errbuf)); mrb->exc = mrb_obj_ptr(exception); return mrb_nil_value(); } { char current_base_directory[PATH_MAX]; char *last_directory; grn_strcpy(current_base_directory, PATH_MAX, data->base_directory); grn_strcpy(data->base_directory, PATH_MAX, expanded_path); last_directory = strrchr(data->base_directory, '/'); if (last_directory) { last_directory[0] = '\0'; } parser = mrb_parser_new(mrb); mrb_parser_set_filename(parser, expanded_path); parser->s = parser->send = NULL; parser->f = file; mrb_parser_parse(parser, NULL); fclose(file); { struct RProc *proc; proc = mrb_generate_code(mrb, parser); result = mrb_toplevel_run(mrb, proc); } mrb_parser_free(parser); grn_strcpy(data->base_directory, PATH_MAX, current_base_directory); } return result; }
static int partial_hook(struct mrb_parser_state *p) { mrbc_context *c = p->cxt; struct mrbc_args *args = (struct mrbc_args *)c->partial_data; const char *fn; if (p->f) fclose(p->f); if (args->idx >= args->argc) { p->f = NULL; return -1; } fn = args->argv[args->idx++]; p->f = fopen(fn, "r"); if (p->f == NULL) { fprintf(stderr, "%s: cannot open program file. (%s)\n", args->prog, fn); return -1; } mrb_parser_set_filename(p, fn); return 0; }