Skip to content

Commit 09bf3c9

Browse files
committed
[PRISM] Trigger moreswitches off shebang
1 parent 002e785 commit 09bf3c9

File tree

1 file changed

+37
-9
lines changed

1 file changed

+37
-9
lines changed

ruby.c

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2099,6 +2099,37 @@ process_script(ruby_cmdline_options_t *opt)
20992099
return ast_value;
21002100
}
21012101

2102+
static uint8_t
2103+
prism_script_command_line(ruby_cmdline_options_t *opt)
2104+
{
2105+
uint8_t command_line = 0;
2106+
if (opt->do_split) command_line |= PM_OPTIONS_COMMAND_LINE_A;
2107+
if (opt->do_line) command_line |= PM_OPTIONS_COMMAND_LINE_L;
2108+
if (opt->do_loop) command_line |= PM_OPTIONS_COMMAND_LINE_N;
2109+
if (opt->do_print) command_line |= PM_OPTIONS_COMMAND_LINE_P;
2110+
if (opt->xflag) command_line |= PM_OPTIONS_COMMAND_LINE_X;
2111+
return command_line;
2112+
}
2113+
2114+
static void
2115+
prism_script_shebang_callback(pm_options_t *options, const uint8_t *source, size_t length, void *data)
2116+
{
2117+
ruby_cmdline_options_t *opt = (ruby_cmdline_options_t *) data;
2118+
opt->warning = 0;
2119+
2120+
char *switches = malloc(length + 1);
2121+
memcpy(switches, source, length);
2122+
switches[length] = '\0';
2123+
2124+
moreswitches(switches, opt, 0);
2125+
free(switches);
2126+
2127+
pm_options_command_line_set(options, prism_script_command_line(opt));
2128+
if (opt->ext.enc.name != 0) {
2129+
pm_options_encoding_set(options, StringValueCStr(opt->ext.enc.name));
2130+
}
2131+
}
2132+
21022133
/**
21032134
* Process the command line options and parse the script into the given result.
21042135
* Raise an error if the script cannot be parsed.
@@ -2115,17 +2146,13 @@ prism_script(ruby_cmdline_options_t *opt, pm_parse_result_t *result)
21152146
pm_options_encoding_set(options, StringValueCStr(opt->ext.enc.name));
21162147
}
21172148

2118-
uint8_t command_line = 0;
2119-
if (opt->do_split) command_line |= PM_OPTIONS_COMMAND_LINE_A;
2120-
if (opt->do_line) command_line |= PM_OPTIONS_COMMAND_LINE_L;
2121-
if (opt->do_loop) command_line |= PM_OPTIONS_COMMAND_LINE_N;
2122-
if (opt->do_print) command_line |= PM_OPTIONS_COMMAND_LINE_P;
2123-
2149+
uint8_t command_line = prism_script_command_line(opt);
21242150
VALUE error;
2151+
21252152
if (strcmp(opt->script, "-") == 0) {
2126-
if (opt->xflag) command_line |= PM_OPTIONS_COMMAND_LINE_X;
21272153
pm_options_command_line_set(options, command_line);
21282154
pm_options_filepath_set(options, "-");
2155+
pm_options_shebang_callback_set(options, prism_script_shebang_callback, (void *) opt);
21292156

21302157
ruby_opt_init(opt);
21312158
error = pm_parse_stdin(result);
@@ -2138,16 +2165,17 @@ prism_script(ruby_cmdline_options_t *opt, pm_parse_result_t *result)
21382165
}
21392166
}
21402167
else if (opt->e_script) {
2141-
command_line |= PM_OPTIONS_COMMAND_LINE_E;
2168+
command_line = (uint8_t) ((command_line | PM_OPTIONS_COMMAND_LINE_E) & ~PM_OPTIONS_COMMAND_LINE_X);
21422169
pm_options_command_line_set(options, command_line);
21432170

21442171
ruby_opt_init(opt);
21452172
result->node.coverage_enabled = 0;
21462173
error = pm_parse_string(result, opt->e_script, rb_str_new2("-e"));
21472174
}
21482175
else {
2149-
if (opt->xflag) command_line |= PM_OPTIONS_COMMAND_LINE_X;
21502176
pm_options_command_line_set(options, command_line);
2177+
pm_options_shebang_callback_set(options, prism_script_shebang_callback, (void *) opt);
2178+
21512179
error = pm_load_file(result, opt->script_name, true);
21522180

21532181
// If reading the file did not error, at that point we load the command

0 commit comments

Comments
 (0)