aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2018-04-06 14:50:12 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2018-04-06 14:50:12 +0200
commitcf079ffe1c26c2d42a902a60696b20a262e92d87 (patch)
tree13ab2e6f6c2b4514559e0475415366aaaa8e024c
parentde0262598504f55251e3763e73f259074795de5d (diff)
downloadbusybox-w32-cf079ffe1c26c2d42a902a60696b20a262e92d87.tar.gz
busybox-w32-cf079ffe1c26c2d42a902a60696b20a262e92d87.tar.bz2
busybox-w32-cf079ffe1c26c2d42a902a60696b20a262e92d87.zip
hush: consolidate handling of setting/unsetting of PSn, LINENO, OPTIND
function old new delta handle_changed_special_names - 99 +99 unset_local_var 256 155 -101 set_local_var 557 437 -120 ------------------------------------------------------------------------------ (add/remove: 1/0 grow/shrink: 0/2 up/down: 99/-221) Total: -122 bytes Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--shell/hush.c59
1 files changed, 30 insertions, 29 deletions
diff --git a/shell/hush.c b/shell/hush.c
index 9ea3e3fcb..7b59b0ff9 100644
--- a/shell/hush.c
+++ b/shell/hush.c
@@ -2123,6 +2123,27 @@ static const char* FAST_FUNC get_local_var_value(const char *name)
2123 return NULL; 2123 return NULL;
2124} 2124}
2125 2125
2126static void handle_changed_special_names(const char *name, unsigned name_len)
2127{
2128 if (name_len == 3 && name[0] == 'P' && name[1] == 'S') {
2129 cmdedit_update_prompt();
2130 return;
2131 }
2132
2133 if ((ENABLE_HUSH_LINENO_VAR || ENABLE_HUSH_GETOPTS)
2134 && name_len == 6
2135 ) {
2136#if ENABLE_HUSH_LINENO_VAR
2137 if (strncmp(name, "LINENO", 6) == 0)
2138 G.lineno_var = NULL;
2139#endif
2140#if ENABLE_HUSH_GETOPTS
2141 if (strncmp(name, "OPTIND", 6) == 0)
2142 G.getopt_count = 0;
2143#endif
2144 }
2145}
2146
2126/* str holds "NAME=VAL" and is expected to be malloced. 2147/* str holds "NAME=VAL" and is expected to be malloced.
2127 * We take ownership of it. 2148 * We take ownership of it.
2128 */ 2149 */
@@ -2264,21 +2285,7 @@ static int set_local_var(char *str, unsigned flags)
2264 } 2285 }
2265 free(free_me); 2286 free(free_me);
2266 2287
2267 /* Handle special names */ 2288 handle_changed_special_names(cur->varstr, name_len - 1);
2268 if (name_len == 4 && cur->varstr[0] == 'P' && cur->varstr[1] == 'S')
2269 cmdedit_update_prompt();
2270 else
2271 if ((ENABLE_HUSH_LINENO_VAR || ENABLE_HUSH_GETOPTS) && name_len == 7) {
2272#if ENABLE_HUSH_LINENO_VAR
2273 if (G.lineno_var && strncmp(cur->varstr, "LINENO", 6) == 0)
2274 G.lineno_var = NULL;
2275#endif
2276#if ENABLE_HUSH_GETOPTS
2277 /* defoptindvar is a "OPTIND=..." constant string */
2278 if (strncmp(cur->varstr, defoptindvar, 7) == 0)
2279 G.getopt_count = 0;
2280#endif
2281 }
2282 2289
2283 return 0; 2290 return 0;
2284} 2291}
@@ -2297,32 +2304,26 @@ static int unset_local_var_len(const char *name, int name_len)
2297 if (!name) 2304 if (!name)
2298 return EXIT_SUCCESS; 2305 return EXIT_SUCCESS;
2299 2306
2300 if ((ENABLE_HUSH_LINENO_VAR || ENABLE_HUSH_GETOPTS) && name_len == 6) {
2301#if ENABLE_HUSH_GETOPTS
2302 if (strncmp(name, "OPTIND", 6) == 0)
2303 G.getopt_count = 0;
2304#endif
2305#if ENABLE_HUSH_LINENO_VAR
2306 if (G.lineno_var && strncmp(name, "LINENO", 6) == 0)
2307 G.lineno_var = NULL;
2308#endif
2309 }
2310
2311 cur_pp = &G.top_var; 2307 cur_pp = &G.top_var;
2312 while ((cur = *cur_pp) != NULL) { 2308 while ((cur = *cur_pp) != NULL) {
2313 if (strncmp(cur->varstr, name, name_len) == 0 && cur->varstr[name_len] == '=') { 2309 if (strncmp(cur->varstr, name, name_len) == 0
2310 && cur->varstr[name_len] == '='
2311 ) {
2314 if (cur->flg_read_only) { 2312 if (cur->flg_read_only) {
2315 bb_error_msg("%s: readonly variable", name); 2313 bb_error_msg("%s: readonly variable", name);
2316 return EXIT_FAILURE; 2314 return EXIT_FAILURE;
2317 } 2315 }
2316
2318 *cur_pp = cur->next; 2317 *cur_pp = cur->next;
2319 debug_printf_env("%s: unsetenv '%s'\n", __func__, cur->varstr); 2318 debug_printf_env("%s: unsetenv '%s'\n", __func__, cur->varstr);
2320 bb_unsetenv(cur->varstr); 2319 bb_unsetenv(cur->varstr);
2321 if (name_len == 3 && cur->varstr[0] == 'P' && cur->varstr[1] == 'S') 2320
2322 cmdedit_update_prompt(); 2321 handle_changed_special_names(name, name_len);
2322
2323 if (!cur->max_len) 2323 if (!cur->max_len)
2324 free(cur->varstr); 2324 free(cur->varstr);
2325 free(cur); 2325 free(cur);
2326
2326 return EXIT_SUCCESS; 2327 return EXIT_SUCCESS;
2327 } 2328 }
2328 cur_pp = &cur->next; 2329 cur_pp = &cur->next;