aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2009-04-02 13:46:27 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2009-04-02 13:46:27 +0000
commitb29eb6ed255ad87f49b70220d254810063c7ebf3 (patch)
tree8b4ece864a7f9e1161abe59bf887c4fd81d473bb
parent0dfe1d26a9000da4925f64c07a96b3e09ba175b1 (diff)
downloadbusybox-w32-b29eb6ed255ad87f49b70220d254810063c7ebf3.tar.gz
busybox-w32-b29eb6ed255ad87f49b70220d254810063c7ebf3.tar.bz2
busybox-w32-b29eb6ed255ad87f49b70220d254810063c7ebf3.zip
shells: do not need to have math state global
function old new delta ash_arith - 143 +143 expand_variables 2102 2124 +22 popstring 134 140 +6 parse_command 1460 1463 +3 trapcmd 236 238 +2 changepath 197 196 -1 raise_interrupt 86 83 -3 hush_main 1012 991 -21 ash_main 1388 1364 -24 arith_set_local_var 73 34 -39 dash_arith 117 - -117 ------------------------------------------------------------------------------ (add/remove: 1/1 grow/shrink: 4/5 up/down: 176/-205) Total: -29 bytes
-rw-r--r--shell/ash.c85
-rw-r--r--shell/hush.c24
2 files changed, 50 insertions, 59 deletions
diff --git a/shell/ash.c b/shell/ash.c
index 4aaea2803..1cd205068 100644
--- a/shell/ash.c
+++ b/shell/ash.c
@@ -185,10 +185,6 @@ struct globals_misc {
185#define debug optlist[15] 185#define debug optlist[15]
186#endif 186#endif
187 187
188#if ENABLE_SH_MATH_SUPPORT
189 arith_eval_hooks_t math_hooks;
190#endif
191
192 /* trap handler commands */ 188 /* trap handler commands */
193 /* 189 /*
194 * Sigmode records the current value of the signal handlers for the various 190 * Sigmode records the current value of the signal handlers for the various
@@ -238,7 +234,6 @@ extern struct globals_misc *const ash_ptr_to_globals_misc;
238#define random_LCG (G_misc.random_LCG ) 234#define random_LCG (G_misc.random_LCG )
239#define backgndpid (G_misc.backgndpid ) 235#define backgndpid (G_misc.backgndpid )
240#define job_warning (G_misc.job_warning) 236#define job_warning (G_misc.job_warning)
241#define math_hooks (G_misc.math_hooks )
242#define INIT_G_misc() do { \ 237#define INIT_G_misc() do { \
243 (*(struct globals_misc**)&ash_ptr_to_globals_misc) = xzalloc(sizeof(G_misc)); \ 238 (*(struct globals_misc**)&ash_ptr_to_globals_misc) = xzalloc(sizeof(G_misc)); \
244 barrier(); \ 239 barrier(); \
@@ -1103,6 +1098,14 @@ ash_msg_and_raise_error(const char *msg, ...)
1103 va_end(ap); 1098 va_end(ap);
1104} 1099}
1105 1100
1101static void raise_error_syntax(const char *) NORETURN;
1102static void
1103raise_error_syntax(const char *msg)
1104{
1105 ash_msg_and_raise_error("syntax error: %s", msg);
1106 /* NOTREACHED */
1107}
1108
1106static void ash_msg_and_raise(int, const char *, ...) NORETURN; 1109static void ash_msg_and_raise(int, const char *, ...) NORETURN;
1107static void 1110static void
1108ash_msg_and_raise(int cond, const char *msg, ...) 1111ash_msg_and_raise(int cond, const char *msg, ...)
@@ -5241,7 +5244,32 @@ redirectsafe(union node *redir, int flags)
5241 */ 5244 */
5242 5245
5243#if ENABLE_SH_MATH_SUPPORT 5246#if ENABLE_SH_MATH_SUPPORT
5244static arith_t dash_arith(const char *); 5247static arith_t
5248ash_arith(const char *s)
5249{
5250 arith_eval_hooks_t math_hooks;
5251 arith_t result;
5252 int errcode = 0;
5253
5254 math_hooks.lookupvar = lookupvar;
5255 math_hooks.setvar = setvar;
5256 math_hooks.endofname = endofname;
5257
5258 INT_OFF;
5259 result = arith(s, &errcode, &math_hooks);
5260 if (errcode < 0) {
5261 if (errcode == -3)
5262 ash_msg_and_raise_error("exponent less than 0");
5263 if (errcode == -2)
5264 ash_msg_and_raise_error("divide by zero");
5265 if (errcode == -5)
5266 ash_msg_and_raise_error("expression recursion loop detected");
5267 raise_error_syntax(s);
5268 }
5269 INT_ON;
5270
5271 return result;
5272}
5245#endif 5273#endif
5246 5274
5247/* 5275/*
@@ -5720,7 +5748,7 @@ expari(int quotes)
5720 if (quotes) 5748 if (quotes)
5721 rmescapes(p + 2); 5749 rmescapes(p + 2);
5722 5750
5723 len = cvtnum(dash_arith(p + 2)); 5751 len = cvtnum(ash_arith(p + 2));
5724 5752
5725 if (flag != '"') 5753 if (flag != '"')
5726 recordregion(begoff, begoff + len, 0); 5754 recordregion(begoff, begoff + len, 0);
@@ -10127,14 +10155,6 @@ static struct heredoc *heredoc;
10127 */ 10155 */
10128#define NEOF ((union node *)&tokpushback) 10156#define NEOF ((union node *)&tokpushback)
10129 10157
10130static void raise_error_syntax(const char *) NORETURN;
10131static void
10132raise_error_syntax(const char *msg)
10133{
10134 ash_msg_and_raise_error("syntax error: %s", msg);
10135 /* NOTREACHED */
10136}
10137
10138/* 10158/*
10139 * Called when an unexpected token is read during the parse. The argument 10159 * Called when an unexpected token is read during the parse. The argument
10140 * is the token that is expected, or -1 if more than one type of token can 10160 * is the token that is expected, or -1 if more than one type of token can
@@ -12353,33 +12373,11 @@ timescmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
12353} 12373}
12354 12374
12355#if ENABLE_SH_MATH_SUPPORT 12375#if ENABLE_SH_MATH_SUPPORT
12356static arith_t
12357dash_arith(const char *s)
12358{
12359 arith_t result;
12360 int errcode = 0;
12361
12362 INT_OFF;
12363 result = arith(s, &errcode, &math_hooks);
12364 if (errcode < 0) {
12365 if (errcode == -3)
12366 ash_msg_and_raise_error("exponent less than 0");
12367 if (errcode == -2)
12368 ash_msg_and_raise_error("divide by zero");
12369 if (errcode == -5)
12370 ash_msg_and_raise_error("expression recursion loop detected");
12371 raise_error_syntax(s);
12372 }
12373 INT_ON;
12374
12375 return result;
12376}
12377
12378/* 12376/*
12379 * The let builtin. partial stolen from GNU Bash, the Bourne Again SHell. 12377 * The let builtin. partial stolen from GNU Bash, the Bourne Again SHell.
12380 * Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc. 12378 * Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc.
12381 * 12379 *
12382 * Copyright (C) 2003 Vladimir Oleynik <dzo@simtreas.ru> 12380 * Copyright (C) 2003 Vladimir Oleynik <dzo@simtreas.ru>
12383 */ 12381 */
12384static int 12382static int
12385letcmd(int argc UNUSED_PARAM, char **argv) 12383letcmd(int argc UNUSED_PARAM, char **argv)
@@ -12390,7 +12388,7 @@ letcmd(int argc UNUSED_PARAM, char **argv)
12390 if (!*argv) 12388 if (!*argv)
12391 ash_msg_and_raise_error("expression expected"); 12389 ash_msg_and_raise_error("expression expected");
12392 do { 12390 do {
12393 i = dash_arith(*argv); 12391 i = ash_arith(*argv);
12394 } while (*++argv); 12392 } while (*++argv);
12395 12393
12396 return !i; 12394 return !i;
@@ -13119,11 +13117,6 @@ int ash_main(int argc UNUSED_PARAM, char **argv)
13119 INIT_G_alias(); 13117 INIT_G_alias();
13120#endif 13118#endif
13121 INIT_G_cmdtable(); 13119 INIT_G_cmdtable();
13122#if ENABLE_SH_MATH_SUPPORT
13123 math_hooks.lookupvar = lookupvar;
13124 math_hooks.setvar = setvar;
13125 math_hooks.endofname = endofname;
13126#endif
13127 13120
13128#if PROFILE 13121#if PROFILE
13129 monitor(4, etext, profile_buf, sizeof(profile_buf), 50); 13122 monitor(4, etext, profile_buf, sizeof(profile_buf), 50);
diff --git a/shell/hush.c b/shell/hush.c
index e93e5a9a0..3725191d8 100644
--- a/shell/hush.c
+++ b/shell/hush.c
@@ -454,9 +454,6 @@ struct globals {
454#if ENABLE_FEATURE_EDITING 454#if ENABLE_FEATURE_EDITING
455 line_input_t *line_input_state; 455 line_input_t *line_input_state;
456#endif 456#endif
457#if ENABLE_SH_MATH_SUPPORT
458 arith_eval_hooks_t hooks;
459#endif
460 pid_t root_pid; 457 pid_t root_pid;
461 pid_t last_bg_pid; 458 pid_t last_bg_pid;
462#if ENABLE_HUSH_JOB 459#if ENABLE_HUSH_JOB
@@ -1189,12 +1186,12 @@ static char *endofname(const char *name)
1189static void arith_set_local_var(const char *name, const char *val, int flags) 1186static void arith_set_local_var(const char *name, const char *val, int flags)
1190{ 1187{
1191 /* arith code doesnt malloc space, so do it for it */ 1188 /* arith code doesnt malloc space, so do it for it */
1192 char *var = xmalloc(strlen(name) + 1 + strlen(val) + 1); 1189 char *var = xasprintf("%s=%s", name, val);
1193 sprintf(var, "%s=%s", name, val);
1194 set_local_var(var, flags); 1190 set_local_var(var, flags);
1195} 1191}
1196#endif 1192#endif
1197 1193
1194
1198/* 1195/*
1199 * in_str support 1196 * in_str support
1200 */ 1197 */
@@ -1807,20 +1804,26 @@ static int expand_vars_to_list(o_string *output, int n, char *arg, char or_mask)
1807#endif 1804#endif
1808#if ENABLE_SH_MATH_SUPPORT 1805#if ENABLE_SH_MATH_SUPPORT
1809 case '+': { /* <SPECIAL_VAR_SYMBOL>(cmd<SPECIAL_VAR_SYMBOL> */ 1806 case '+': { /* <SPECIAL_VAR_SYMBOL>(cmd<SPECIAL_VAR_SYMBOL> */
1807 arith_eval_hooks_t hooks;
1810 arith_t res; 1808 arith_t res;
1811 char buf[30]; 1809 char buf[30];
1812 int errcode; 1810 int errcode;
1811
1813 *p = '\0'; 1812 *p = '\0';
1814 ++arg; 1813 ++arg;
1815 debug_printf_subst("ARITH '%s' first_ch %x\n", arg, first_ch); 1814 debug_printf_subst("ARITH '%s' first_ch %x\n", arg, first_ch);
1816 res = arith(arg, &errcode, &G.hooks); 1815 hooks.lookupvar = lookup_param;
1817 if (errcode < 0) 1816 hooks.setvar = arith_set_local_var;
1817 hooks.endofname = endofname;
1818 res = arith(arg, &errcode, &hooks);
1819 if (errcode < 0) {
1818 switch (errcode) { 1820 switch (errcode) {
1819 case -3: maybe_die("arith", "exponent less than 0"); break; 1821 case -3: maybe_die("arith", "exponent less than 0"); break;
1820 case -2: maybe_die("arith", "divide by zero"); break; 1822 case -2: maybe_die("arith", "divide by zero"); break;
1821 case -5: maybe_die("arith", "expression recursion loop detected"); break; 1823 case -5: maybe_die("arith", "expression recursion loop detected"); break;
1822 default: maybe_die("arith", "syntax error"); 1824 default: maybe_die("arith", "syntax error"); break;
1823 } 1825 }
1826 }
1824 sprintf(buf, arith_t_fmt, res); 1827 sprintf(buf, arith_t_fmt, res);
1825 o_addstrauto(output, buf); 1828 o_addstrauto(output, buf);
1826 debug_printf_subst("ARITH RES '"arith_t_fmt"'\n", res); 1829 debug_printf_subst("ARITH RES '"arith_t_fmt"'\n", res);
@@ -4601,11 +4604,6 @@ int hush_main(int argc, char **argv)
4601#if ENABLE_FEATURE_EDITING 4604#if ENABLE_FEATURE_EDITING
4602 G.line_input_state = new_line_input_t(FOR_SHELL); 4605 G.line_input_state = new_line_input_t(FOR_SHELL);
4603#endif 4606#endif
4604#if ENABLE_SH_MATH_SUPPORT
4605 G.hooks.lookupvar = lookup_param;
4606 G.hooks.setvar = arith_set_local_var;
4607 G.hooks.endofname = endofname;
4608#endif
4609 /* XXX what should these be while sourcing /etc/profile? */ 4607 /* XXX what should these be while sourcing /etc/profile? */
4610 G.global_argc = argc; 4608 G.global_argc = argc;
4611 G.global_argv = argv; 4609 G.global_argv = argv;