From a045b6b8d93f70d7bf57a94ed5c7e0432190d584 Mon Sep 17 00:00:00 2001 From: dearblue Date: Sat, 21 Nov 2020 10:17:27 +0900 Subject: Allow to mixed and specify `*.rb` and `*.mrb` in `bin/mruby` It is not decides by the extension. In order to be recognized as a `.mrb` file, the following three points must be satisfied: - File starts with "RITE" - At least `sizeof(struct rite_binary_header)` bytes can be read - `NUL` is included in the first 64 bytes of the file If these are not met, it is judged as a text file and it is processed as a Ruby script. The `bin/mruby -b` switch is still available which treats the given file as a `.mrb` file. New `MRB_API` function: - `include/mruby/compile.h` and `mrbgems/mruby-compiler/core/parse.y` - `mrb_load_detect_file_cxt()` (remove with `MRB_DISABLE_STDIO`) NOTE: - Even script files now always open in binary mode for `bin/mruby`. The `\r\n` is handled by the `nextc()` function already, so there is no problem even on Windows. - The `nextc0()` function in `mrbgems/mruby-compiler/core/parse.y` can now specify a string buffer and a file pointer at the same time. In this case, get it from the string buffer first. This patch includes modifies by comment of https://github.com/mruby/mruby/pull/5157. --- mrbgems/mruby-bin-mruby/tools/mruby/mruby.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) (limited to 'mrbgems/mruby-bin-mruby/tools') diff --git a/mrbgems/mruby-bin-mruby/tools/mruby/mruby.c b/mrbgems/mruby-bin-mruby/tools/mruby/mruby.c index e9bd4c326..c60a0945b 100644 --- a/mrbgems/mruby-bin-mruby/tools/mruby/mruby.c +++ b/mrbgems/mruby-bin-mruby/tools/mruby/mruby.c @@ -12,6 +12,11 @@ #include #include +#if defined(_WIN32) || defined(_WIN64) +# include /* for setmode */ +# include +#endif + struct _args { FILE *rfp; char *cmdline; @@ -218,7 +223,7 @@ parse_args(mrb_state *mrb, int argc, char **argv, struct _args *args) } else { args->rfp = strcmp(argv[0], "-") == 0 ? - stdin : fopen(argv[0], args->mrbfile ? "rb" : "r"); + stdin : fopen(argv[0], "rb"); if (args->rfp == NULL) { fprintf(stderr, "%s: Cannot open program file: %s\n", opts->program, argv[0]); return EXIT_FAILURE; @@ -228,6 +233,11 @@ parse_args(mrb_state *mrb, int argc, char **argv, struct _args *args) argc--; argv++; } } +#if defined(_WIN32) || defined(_WIN64) + if (args->rfp == stdin) { + setmode(_fileno(stdin), O_BINARY); + } +#endif args->argv = (char **)mrb_realloc(mrb, args->argv, sizeof(char*) * (argc + 1)); memcpy(args->argv, argv, (argc+1) * sizeof(char*)); args->argc = argc; @@ -309,7 +319,7 @@ main(int argc, char **argv) /* Load libraries */ for (i = 0; i < args.libc; i++) { struct REnv *e; - FILE *lfp = fopen(args.libv[i], args.mrbfile ? "rb" : "r"); + FILE *lfp = fopen(args.libv[i], "rb"); if (lfp == NULL) { fprintf(stderr, "%s: Cannot open library file: %s\n", *argv, args.libv[i]); mrbc_context_free(mrb, c); @@ -320,7 +330,7 @@ main(int argc, char **argv) v = mrb_load_irep_file_cxt(mrb, lfp, c); } else { - v = mrb_load_file_cxt(mrb, lfp, c); + v = mrb_load_detect_file_cxt(mrb, lfp, c); } fclose(lfp); e = mrb->c->cibase->env; @@ -334,7 +344,7 @@ main(int argc, char **argv) v = mrb_load_irep_file_cxt(mrb, args.rfp, c); } else if (args.rfp) { - v = mrb_load_file_cxt(mrb, args.rfp, c); + v = mrb_load_detect_file_cxt(mrb, args.rfp, c); } else { char* utf8 = mrb_utf8_from_locale(args.cmdline, -1); -- cgit v1.2.3