summaryrefslogtreecommitdiffhomepage
path: root/mrbgems/mruby-bin-mruby/tools
diff options
context:
space:
mode:
authordearblue <[email protected]>2020-11-21 10:17:27 +0900
committerdearblue <[email protected]>2020-11-21 19:05:46 +0900
commita045b6b8d93f70d7bf57a94ed5c7e0432190d584 (patch)
tree54bdd026a2fbf6d6c6b607086f505ac75fdae1df /mrbgems/mruby-bin-mruby/tools
parent55e127577863e0f913a4b7b8653fcba577c29b86 (diff)
downloadmruby-a045b6b8d93f70d7bf57a94ed5c7e0432190d584.tar.gz
mruby-a045b6b8d93f70d7bf57a94ed5c7e0432190d584.zip
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.
Diffstat (limited to 'mrbgems/mruby-bin-mruby/tools')
-rw-r--r--mrbgems/mruby-bin-mruby/tools/mruby/mruby.c18
1 files changed, 14 insertions, 4 deletions
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 <mruby/variable.h>
#include <mruby/proc.h>
+#if defined(_WIN32) || defined(_WIN64)
+# include <io.h> /* for setmode */
+# include <fcntl.h>
+#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);