diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2019-05-19 15:26:05 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2019-05-19 15:39:32 +0200 |
commit | 08fb82c80cf06b776822b8388c3863e7c5565a74 (patch) | |
tree | 89d311bb3eea705b8c35afe16bb94a59902fe682 /shell/hush.c | |
parent | ee9e5f92b659081a9d889ef16f91a070b8c36024 (diff) | |
download | busybox-w32-08fb82c80cf06b776822b8388c3863e7c5565a74.tar.gz busybox-w32-08fb82c80cf06b776822b8388c3863e7c5565a74.tar.bz2 busybox-w32-08fb82c80cf06b776822b8388c3863e7c5565a74.zip |
hush: handle LINENO the same way as RANDOM: variable is "ephemeral"
"env - hush" invocation (that is, with empty environment)
should not show LINENO in "set" output.
function old new delta
get_local_var_value 263 294 +31
hush_main 1105 1070 -35
handle_changed_special_names 79 38 -41
run_pipe 1834 1765 -69
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/3 up/down: 31/-145) Total: -114 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to '')
-rw-r--r-- | shell/hush.c | 50 |
1 files changed, 18 insertions, 32 deletions
diff --git a/shell/hush.c b/shell/hush.c index e2927afc4..629b7ff92 100644 --- a/shell/hush.c +++ b/shell/hush.c | |||
@@ -967,8 +967,8 @@ struct globals { | |||
967 | smallint we_have_children; | 967 | smallint we_have_children; |
968 | #endif | 968 | #endif |
969 | #if ENABLE_HUSH_LINENO_VAR | 969 | #if ENABLE_HUSH_LINENO_VAR |
970 | unsigned lineno; | 970 | unsigned parse_lineno; |
971 | char *lineno_var; | 971 | unsigned execute_lineno; |
972 | #endif | 972 | #endif |
973 | HFILE *HFILE_list; | 973 | HFILE *HFILE_list; |
974 | /* Which signals have non-DFL handler (even with no traps set)? | 974 | /* Which signals have non-DFL handler (even with no traps set)? |
@@ -2221,6 +2221,10 @@ static const char* FAST_FUNC get_local_var_value(const char *name) | |||
2221 | if (strcmp(name, "RANDOM") == 0) | 2221 | if (strcmp(name, "RANDOM") == 0) |
2222 | return utoa(next_random(&G.random_gen)); | 2222 | return utoa(next_random(&G.random_gen)); |
2223 | #endif | 2223 | #endif |
2224 | #if ENABLE_HUSH_LINENO_VAR | ||
2225 | if (strcmp(name, "LINENO") == 0) | ||
2226 | return utoa(G.execute_lineno); | ||
2227 | #endif | ||
2224 | #if BASH_EPOCH_VARS | 2228 | #if BASH_EPOCH_VARS |
2225 | { | 2229 | { |
2226 | const char *fmt = NULL; | 2230 | const char *fmt = NULL; |
@@ -2240,22 +2244,14 @@ static const char* FAST_FUNC get_local_var_value(const char *name) | |||
2240 | return NULL; | 2244 | return NULL; |
2241 | } | 2245 | } |
2242 | 2246 | ||
2243 | #if ENABLE_HUSH_LINENO_VAR || ENABLE_HUSH_GETOPTS | 2247 | #if ENABLE_HUSH_GETOPTS |
2244 | static void handle_changed_special_names(const char *name, unsigned name_len) | 2248 | static void handle_changed_special_names(const char *name, unsigned name_len) |
2245 | { | 2249 | { |
2246 | if (name_len == 6) { | 2250 | if (name_len == 6) { |
2247 | # if ENABLE_HUSH_LINENO_VAR | ||
2248 | if (strncmp(name, "LINENO", 6) == 0) { | ||
2249 | G.lineno_var = NULL; | ||
2250 | return; | ||
2251 | } | ||
2252 | # endif | ||
2253 | # if ENABLE_HUSH_GETOPTS | ||
2254 | if (strncmp(name, "OPTIND", 6) == 0) { | 2251 | if (strncmp(name, "OPTIND", 6) == 0) { |
2255 | G.getopt_count = 0; | 2252 | G.getopt_count = 0; |
2256 | return; | 2253 | return; |
2257 | } | 2254 | } |
2258 | # endif | ||
2259 | } | 2255 | } |
2260 | } | 2256 | } |
2261 | #else | 2257 | #else |
@@ -2727,8 +2723,8 @@ static int i_getch(struct in_str *i) | |||
2727 | i->last_char = ch; | 2723 | i->last_char = ch; |
2728 | #if ENABLE_HUSH_LINENO_VAR | 2724 | #if ENABLE_HUSH_LINENO_VAR |
2729 | if (ch == '\n') { | 2725 | if (ch == '\n') { |
2730 | G.lineno++; | 2726 | G.parse_lineno++; |
2731 | debug_printf_parse("G.lineno++ = %u\n", G.lineno); | 2727 | debug_printf_parse("G.parse_lineno++ = %u\n", G.parse_lineno); |
2732 | } | 2728 | } |
2733 | #endif | 2729 | #endif |
2734 | return ch; | 2730 | return ch; |
@@ -3730,8 +3726,8 @@ static int done_command(struct parse_context *ctx) | |||
3730 | clear_and_ret: | 3726 | clear_and_ret: |
3731 | memset(command, 0, sizeof(*command)); | 3727 | memset(command, 0, sizeof(*command)); |
3732 | #if ENABLE_HUSH_LINENO_VAR | 3728 | #if ENABLE_HUSH_LINENO_VAR |
3733 | command->lineno = G.lineno; | 3729 | command->lineno = G.parse_lineno; |
3734 | debug_printf_parse("command->lineno = G.lineno (%u)\n", G.lineno); | 3730 | debug_printf_parse("command->lineno = G.parse_lineno (%u)\n", G.parse_lineno); |
3735 | #endif | 3731 | #endif |
3736 | return pi->num_cmds; /* used only for 0/nonzero check */ | 3732 | return pi->num_cmds; /* used only for 0/nonzero check */ |
3737 | } | 3733 | } |
@@ -7261,22 +7257,22 @@ static void parse_and_run_stream(struct in_str *inp, int end_trigger) | |||
7261 | static void parse_and_run_string(const char *s) | 7257 | static void parse_and_run_string(const char *s) |
7262 | { | 7258 | { |
7263 | struct in_str input; | 7259 | struct in_str input; |
7264 | //IF_HUSH_LINENO_VAR(unsigned sv = G.lineno;) | 7260 | //IF_HUSH_LINENO_VAR(unsigned sv = G.parse_lineno;) |
7265 | 7261 | ||
7266 | setup_string_in_str(&input, s); | 7262 | setup_string_in_str(&input, s); |
7267 | parse_and_run_stream(&input, '\0'); | 7263 | parse_and_run_stream(&input, '\0'); |
7268 | //IF_HUSH_LINENO_VAR(G.lineno = sv;) | 7264 | //IF_HUSH_LINENO_VAR(G.parse_lineno = sv;) |
7269 | } | 7265 | } |
7270 | 7266 | ||
7271 | static void parse_and_run_file(HFILE *fp) | 7267 | static void parse_and_run_file(HFILE *fp) |
7272 | { | 7268 | { |
7273 | struct in_str input; | 7269 | struct in_str input; |
7274 | IF_HUSH_LINENO_VAR(unsigned sv = G.lineno;) | 7270 | IF_HUSH_LINENO_VAR(unsigned sv = G.parse_lineno;) |
7275 | 7271 | ||
7276 | IF_HUSH_LINENO_VAR(G.lineno = 1;) | 7272 | IF_HUSH_LINENO_VAR(G.parse_lineno = 1;) |
7277 | setup_file_in_str(&input, fp); | 7273 | setup_file_in_str(&input, fp); |
7278 | parse_and_run_stream(&input, ';'); | 7274 | parse_and_run_stream(&input, ';'); |
7279 | IF_HUSH_LINENO_VAR(G.lineno = sv;) | 7275 | IF_HUSH_LINENO_VAR(G.parse_lineno = sv;) |
7280 | } | 7276 | } |
7281 | 7277 | ||
7282 | #if ENABLE_HUSH_TICK | 7278 | #if ENABLE_HUSH_TICK |
@@ -8975,8 +8971,7 @@ static NOINLINE int run_pipe(struct pipe *pi) | |||
8975 | struct variable *old_vars; | 8971 | struct variable *old_vars; |
8976 | 8972 | ||
8977 | #if ENABLE_HUSH_LINENO_VAR | 8973 | #if ENABLE_HUSH_LINENO_VAR |
8978 | if (G.lineno_var) | 8974 | G.execute_lineno = command->lineno; |
8979 | strcpy(G.lineno_var + sizeof("LINENO=")-1, utoa(command->lineno)); | ||
8980 | #endif | 8975 | #endif |
8981 | 8976 | ||
8982 | if (argv[command->assignment_cnt] == NULL) { | 8977 | if (argv[command->assignment_cnt] == NULL) { |
@@ -9209,8 +9204,7 @@ static NOINLINE int run_pipe(struct pipe *pi) | |||
9209 | xpiped_pair(pipefds); | 9204 | xpiped_pair(pipefds); |
9210 | 9205 | ||
9211 | #if ENABLE_HUSH_LINENO_VAR | 9206 | #if ENABLE_HUSH_LINENO_VAR |
9212 | if (G.lineno_var) | 9207 | G.execute_lineno = command->lineno; |
9213 | strcpy(G.lineno_var + sizeof("LINENO=")-1, utoa(command->lineno)); | ||
9214 | #endif | 9208 | #endif |
9215 | 9209 | ||
9216 | command->pid = BB_MMU ? fork() : vfork(); | 9210 | command->pid = BB_MMU ? fork() : vfork(); |
@@ -9946,14 +9940,6 @@ int hush_main(int argc, char **argv) | |||
9946 | * PS4='+ ' | 9940 | * PS4='+ ' |
9947 | */ | 9941 | */ |
9948 | 9942 | ||
9949 | #if ENABLE_HUSH_LINENO_VAR | ||
9950 | if (ENABLE_HUSH_LINENO_VAR) { | ||
9951 | char *p = xasprintf("LINENO=%*s", (int)(sizeof(int)*3), ""); | ||
9952 | set_local_var(p, /*flags*/ 0); | ||
9953 | G.lineno_var = p; /* can't assign before set_local_var("LINENO=...") */ | ||
9954 | } | ||
9955 | #endif | ||
9956 | |||
9957 | #if ENABLE_FEATURE_EDITING | 9943 | #if ENABLE_FEATURE_EDITING |
9958 | G.line_input_state = new_line_input_t(FOR_SHELL); | 9944 | G.line_input_state = new_line_input_t(FOR_SHELL); |
9959 | #endif | 9945 | #endif |