[388] | 1 | /*
|
---|
| 2 | * This file comes from mruby. (renamed from tools/mruby/mruby.c)
|
---|
| 3 | * This file is distributed under ruby's license. (not under TOPPERS license)
|
---|
| 4 | * see https://github.com/mruby/mruby
|
---|
| 5 | * A little modification is done to to call initializer registering TECS components.
|
---|
| 6 | *
|
---|
| 7 | * このファイルは、mruby のものです。
|
---|
| 8 | * mruby のライセンスのもとで、配布されます。(TOPPERS ライセンスではありません)
|
---|
| 9 | * 参照: https://github.com/mruby/mruby
|
---|
| 10 | * TECS コンポーネントを登録する初期化子を呼ぶため、少しの修正がなされています。
|
---|
| 11 | */
|
---|
| 12 |
|
---|
| 13 | #include "nMruby_tMrubyProc_tecsgen.h"
|
---|
| 14 |
|
---|
| 15 | #include "mruby.h"
|
---|
| 16 | #include "mruby/proc.h"
|
---|
| 17 | #include "mruby/array.h"
|
---|
| 18 | #include "mruby/string.h"
|
---|
| 19 | #include "mruby/compile.h"
|
---|
| 20 | #include "mruby/dump.h"
|
---|
| 21 | #include "mruby/variable.h"
|
---|
| 22 | #include <stdio.h>
|
---|
| 23 | #include <string.h>
|
---|
| 24 |
|
---|
| 25 | #ifndef ENABLE_STDIO
|
---|
| 26 | static void
|
---|
| 27 | p(mrb_state *mrb, mrb_value obj)
|
---|
| 28 | {
|
---|
| 29 | obj = mrb_funcall(mrb, obj, "inspect", 0);
|
---|
| 30 | fwrite(RSTRING_PTR(obj), RSTRING_LEN(obj), 1, stdout);
|
---|
| 31 | putc('\n', stdout);
|
---|
| 32 | }
|
---|
| 33 | #else
|
---|
| 34 | #define p(mrb,obj) mrb_p(mrb,obj)
|
---|
| 35 | #endif
|
---|
| 36 |
|
---|
| 37 | void mrb_show_version(mrb_state *);
|
---|
| 38 | void mrb_show_copyright(mrb_state *);
|
---|
| 39 |
|
---|
| 40 | struct _args {
|
---|
| 41 | FILE *rfp;
|
---|
| 42 | char* cmdline;
|
---|
| 43 | int fname : 1;
|
---|
| 44 | int mrbfile : 1;
|
---|
| 45 | int check_syntax : 1;
|
---|
| 46 | int verbose : 1;
|
---|
| 47 | int argc;
|
---|
| 48 | char** argv;
|
---|
| 49 | };
|
---|
| 50 |
|
---|
| 51 | static void
|
---|
| 52 | usage(const char *name)
|
---|
| 53 | {
|
---|
| 54 | static const char *const usage_msg[] = {
|
---|
| 55 | "switches:",
|
---|
| 56 | "-b load and execute RiteBinary (mrb) file",
|
---|
| 57 | "-c check syntax only",
|
---|
| 58 | "-e 'command' one line of script",
|
---|
| 59 | "-v print version number, then run in verbose mode",
|
---|
| 60 | "--verbose run in verbose mode",
|
---|
| 61 | "--version print the version",
|
---|
| 62 | "--copyright print the copyright",
|
---|
| 63 | NULL
|
---|
| 64 | };
|
---|
| 65 | const char *const *p = usage_msg;
|
---|
| 66 |
|
---|
| 67 | printf("Usage: %s [switches] programfile\n", name);
|
---|
| 68 | while(*p)
|
---|
| 69 | printf(" %s\n", *p++);
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | static int
|
---|
| 73 | parse_args(mrb_state *mrb, int argc, char **argv, struct _args *args)
|
---|
| 74 | {
|
---|
| 75 | char **origargv = argv;
|
---|
| 76 | static const struct _args args_zero = { 0 };
|
---|
| 77 |
|
---|
| 78 | *args = args_zero;
|
---|
| 79 |
|
---|
| 80 | for (argc--,argv++; argc > 0; argc--,argv++) {
|
---|
| 81 | char *item;
|
---|
| 82 | if (argv[0][0] != '-') break;
|
---|
| 83 |
|
---|
| 84 | if (strlen(*argv) <= 1) {
|
---|
| 85 | argc--; argv++;
|
---|
| 86 | args->rfp = stdin;
|
---|
| 87 | break;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | item = argv[0] + 1;
|
---|
| 91 | switch (*item++) {
|
---|
| 92 | case 'b':
|
---|
| 93 | args->mrbfile = 1;
|
---|
| 94 | break;
|
---|
| 95 | case 'c':
|
---|
| 96 | args->check_syntax = 1;
|
---|
| 97 | break;
|
---|
| 98 | case 'e':
|
---|
| 99 | if (item[0]) {
|
---|
| 100 | goto append_cmdline;
|
---|
| 101 | }
|
---|
| 102 | else if (argc > 1) {
|
---|
| 103 | argc--; argv++;
|
---|
| 104 | item = argv[0];
|
---|
| 105 | append_cmdline:
|
---|
| 106 | if (!args->cmdline) {
|
---|
| 107 | char *buf;
|
---|
| 108 |
|
---|
| 109 | buf = (char *)mrb_malloc(mrb, strlen(item)+1);
|
---|
| 110 | strcpy(buf, item);
|
---|
| 111 | args->cmdline = buf;
|
---|
| 112 | }
|
---|
| 113 | else {
|
---|
| 114 | args->cmdline = (char *)mrb_realloc(mrb, args->cmdline, strlen(args->cmdline)+strlen(item)+2);
|
---|
| 115 | strcat(args->cmdline, "\n");
|
---|
| 116 | strcat(args->cmdline, item);
|
---|
| 117 | }
|
---|
| 118 | }
|
---|
| 119 | else {
|
---|
| 120 | printf("%s: No code specified for -e\n", *origargv);
|
---|
| 121 | return 0;
|
---|
| 122 | }
|
---|
| 123 | break;
|
---|
| 124 | case 'v':
|
---|
| 125 | mrb_show_version(mrb);
|
---|
| 126 | args->verbose = 1;
|
---|
| 127 | break;
|
---|
| 128 | case '-':
|
---|
| 129 | if (strcmp((*argv) + 2, "version") == 0) {
|
---|
| 130 | mrb_show_version(mrb);
|
---|
| 131 | exit(0);
|
---|
| 132 | }
|
---|
| 133 | else if (strcmp((*argv) + 2, "verbose") == 0) {
|
---|
| 134 | args->verbose = 1;
|
---|
| 135 | break;
|
---|
| 136 | }
|
---|
| 137 | else if (strcmp((*argv) + 2, "copyright") == 0) {
|
---|
| 138 | mrb_show_copyright(mrb);
|
---|
| 139 | exit(0);
|
---|
| 140 | }
|
---|
| 141 | else return -3;
|
---|
| 142 | return 0;
|
---|
| 143 | default:
|
---|
| 144 | return -4;
|
---|
| 145 | }
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | if (args->rfp == NULL && args->cmdline == NULL) {
|
---|
| 149 | if (*argv == NULL) args->rfp = stdin;
|
---|
| 150 | else {
|
---|
| 151 | args->rfp = fopen(argv[0], args->mrbfile ? "rb" : "r");
|
---|
| 152 | if (args->rfp == NULL) {
|
---|
| 153 | printf("%s: Cannot open program file. (%s)\n", *origargv, *argv);
|
---|
| 154 | return 0;
|
---|
| 155 | }
|
---|
| 156 | args->fname = 1;
|
---|
| 157 | args->cmdline = argv[0];
|
---|
| 158 | argc--; argv++;
|
---|
| 159 | }
|
---|
| 160 | }
|
---|
| 161 | args->argv = (char **)mrb_realloc(mrb, args->argv, sizeof(char*) * (argc + 1));
|
---|
| 162 | memcpy(args->argv, argv, (argc+1) * sizeof(char*));
|
---|
| 163 | args->argc = argc;
|
---|
| 164 |
|
---|
| 165 | return 0;
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | static void
|
---|
| 169 | cleanup(mrb_state *mrb, struct _args *args)
|
---|
| 170 | {
|
---|
| 171 | if (args->rfp && args->rfp != stdin)
|
---|
| 172 | fclose(args->rfp);
|
---|
| 173 | if (args->cmdline && !args->fname)
|
---|
| 174 | mrb_free(mrb, args->cmdline);
|
---|
| 175 | if (args->argv)
|
---|
| 176 | mrb_free(mrb, args->argv);
|
---|
| 177 | mrb_close(mrb);
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | static void
|
---|
| 181 | showcallinfo(mrb_state *mrb)
|
---|
| 182 | {
|
---|
| 183 | mrb_callinfo *ci;
|
---|
| 184 | mrb_int ciidx;
|
---|
| 185 | const char *filename, *method, *sep;
|
---|
| 186 | int i, line;
|
---|
| 187 |
|
---|
| 188 | printf("trace:\n");
|
---|
| 189 | ciidx = mrb_fixnum(mrb_obj_iv_get(mrb, mrb->exc, mrb_intern(mrb, "ciidx")));
|
---|
| 190 | if (ciidx >= mrb->ciend - mrb->cibase)
|
---|
| 191 | ciidx = 10; /* ciidx is broken... */
|
---|
| 192 |
|
---|
| 193 | for (i = ciidx; i >= 0; i--) {
|
---|
| 194 | ci = &mrb->cibase[i];
|
---|
| 195 | filename = "(unknown)";
|
---|
| 196 | line = -1;
|
---|
| 197 |
|
---|
| 198 | if (MRB_PROC_CFUNC_P(ci->proc)) {
|
---|
| 199 | continue;
|
---|
| 200 | }
|
---|
| 201 | else {
|
---|
| 202 | mrb_irep *irep = ci->proc->body.irep;
|
---|
| 203 | if (irep->filename != NULL)
|
---|
| 204 | filename = irep->filename;
|
---|
| 205 | if (irep->lines != NULL && i+1 <= ciidx) {
|
---|
| 206 | mrb_code *pc = mrb->cibase[i+1].pc;
|
---|
| 207 | if (irep->iseq <= pc && pc < irep->iseq + irep->ilen) {
|
---|
| 208 | line = irep->lines[pc - irep->iseq - 1];
|
---|
| 209 | }
|
---|
| 210 | }
|
---|
| 211 | }
|
---|
| 212 | if (ci->target_class == ci->proc->target_class)
|
---|
| 213 | sep = ".";
|
---|
| 214 | else
|
---|
| 215 | sep = "#";
|
---|
| 216 |
|
---|
| 217 | method = mrb_sym2name(mrb, ci->mid);
|
---|
| 218 | printf("\t[%d] %s:%d%s%s%s%s\n",
|
---|
| 219 | i, filename, line,
|
---|
| 220 | method ? ":in " : "",
|
---|
| 221 | method ? mrb_class_name(mrb, ci->proc->target_class) : "",
|
---|
| 222 | method ? sep : "",
|
---|
| 223 | method ? method : "");
|
---|
| 224 | }
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 | int
|
---|
| 228 | eMain_main( CELLIDX idx, int argc, const char **argv)
|
---|
| 229 | {
|
---|
| 230 | CELLCB *p_cellcb = GET_CELLCB( idx );
|
---|
| 231 | mrb_state *mrb = mrb_open();
|
---|
| 232 | int n = -1;
|
---|
| 233 | int i;
|
---|
| 234 | struct _args args;
|
---|
| 235 | mrb_value ARGV;
|
---|
| 236 |
|
---|
| 237 | cInit_initializeBridge( mrb );
|
---|
| 238 |
|
---|
| 239 | if (mrb == NULL) {
|
---|
| 240 | fprintf(stderr, "Invalid mrb_state, exiting mruby\n");
|
---|
| 241 | return EXIT_FAILURE;
|
---|
| 242 | }
|
---|
| 243 |
|
---|
| 244 | n = parse_args(mrb, argc, (char **)argv, &args);
|
---|
| 245 | if (n < 0 || (args.cmdline == NULL && args.rfp == NULL)) {
|
---|
| 246 | cleanup(mrb, &args);
|
---|
| 247 | usage(argv[0]);
|
---|
| 248 | return n;
|
---|
| 249 | }
|
---|
| 250 |
|
---|
| 251 | ARGV = mrb_ary_new_capa(mrb, args.argc);
|
---|
| 252 | for (i = 0; i < args.argc; i++) {
|
---|
| 253 | mrb_ary_push(mrb, ARGV, mrb_str_new(mrb, args.argv[i], strlen(args.argv[i])));
|
---|
| 254 | }
|
---|
| 255 | mrb_define_global_const(mrb, "ARGV", ARGV);
|
---|
| 256 |
|
---|
| 257 | if (args.mrbfile) {
|
---|
| 258 | n = mrb_load_irep(mrb, args.rfp);
|
---|
| 259 | if (n < 0) {
|
---|
| 260 | fprintf(stderr, "failed to load mrb file: %s\n", args.cmdline);
|
---|
| 261 | }
|
---|
| 262 | else if (!args.check_syntax) {
|
---|
| 263 | mrb_run(mrb, mrb_proc_new(mrb, mrb->irep[n]), mrb_top_self(mrb));
|
---|
| 264 | n = 0;
|
---|
| 265 | if (mrb->exc) {
|
---|
| 266 | showcallinfo(mrb);
|
---|
| 267 | p(mrb, mrb_obj_value(mrb->exc));
|
---|
| 268 | n = -1;
|
---|
| 269 | }
|
---|
| 270 | }
|
---|
| 271 | }
|
---|
| 272 | else {
|
---|
| 273 | mrbc_context *c = mrbc_context_new(mrb);
|
---|
| 274 | mrb_value v;
|
---|
| 275 |
|
---|
| 276 | if (args.verbose)
|
---|
| 277 | c->dump_result = 1;
|
---|
| 278 | if (args.check_syntax)
|
---|
| 279 | c->no_exec = 1;
|
---|
| 280 |
|
---|
| 281 | if (args.rfp) {
|
---|
| 282 | mrbc_filename(mrb, c, args.cmdline ? args.cmdline : "-");
|
---|
| 283 | v = mrb_load_file_cxt(mrb, args.rfp, c);
|
---|
| 284 | }
|
---|
| 285 | else {
|
---|
| 286 | mrbc_filename(mrb, c, "-e");
|
---|
| 287 | v = mrb_load_string_cxt(mrb, args.cmdline, c);
|
---|
| 288 | }
|
---|
| 289 | mrbc_context_free(mrb, c);
|
---|
| 290 | if (mrb->exc) {
|
---|
| 291 | if (!mrb_undef_p(v)) {
|
---|
| 292 | showcallinfo(mrb);
|
---|
| 293 | p(mrb, mrb_obj_value(mrb->exc));
|
---|
| 294 | }
|
---|
| 295 | n = -1;
|
---|
| 296 | }
|
---|
| 297 | else if (args.check_syntax) {
|
---|
| 298 | printf("Syntax OK\n");
|
---|
| 299 | }
|
---|
| 300 | }
|
---|
| 301 | cleanup(mrb, &args);
|
---|
| 302 |
|
---|
| 303 | return n == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
|
---|
| 304 | }
|
---|