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
This commit is contained in:
Denis Vlasenko 2009-04-02 13:46:27 +00:00
parent 0dfe1d26a9
commit b29eb6ed25
2 changed files with 50 additions and 59 deletions

View File

@ -185,10 +185,6 @@ struct globals_misc {
#define debug optlist[15] #define debug optlist[15]
#endif #endif
#if ENABLE_SH_MATH_SUPPORT
arith_eval_hooks_t math_hooks;
#endif
/* trap handler commands */ /* trap handler commands */
/* /*
* Sigmode records the current value of the signal handlers for the various * 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;
#define random_LCG (G_misc.random_LCG ) #define random_LCG (G_misc.random_LCG )
#define backgndpid (G_misc.backgndpid ) #define backgndpid (G_misc.backgndpid )
#define job_warning (G_misc.job_warning) #define job_warning (G_misc.job_warning)
#define math_hooks (G_misc.math_hooks )
#define INIT_G_misc() do { \ #define INIT_G_misc() do { \
(*(struct globals_misc**)&ash_ptr_to_globals_misc) = xzalloc(sizeof(G_misc)); \ (*(struct globals_misc**)&ash_ptr_to_globals_misc) = xzalloc(sizeof(G_misc)); \
barrier(); \ barrier(); \
@ -1103,6 +1098,14 @@ ash_msg_and_raise_error(const char *msg, ...)
va_end(ap); va_end(ap);
} }
static void raise_error_syntax(const char *) NORETURN;
static void
raise_error_syntax(const char *msg)
{
ash_msg_and_raise_error("syntax error: %s", msg);
/* NOTREACHED */
}
static void ash_msg_and_raise(int, const char *, ...) NORETURN; static void ash_msg_and_raise(int, const char *, ...) NORETURN;
static void static void
ash_msg_and_raise(int cond, const char *msg, ...) ash_msg_and_raise(int cond, const char *msg, ...)
@ -5241,7 +5244,32 @@ redirectsafe(union node *redir, int flags)
*/ */
#if ENABLE_SH_MATH_SUPPORT #if ENABLE_SH_MATH_SUPPORT
static arith_t dash_arith(const char *); static arith_t
ash_arith(const char *s)
{
arith_eval_hooks_t math_hooks;
arith_t result;
int errcode = 0;
math_hooks.lookupvar = lookupvar;
math_hooks.setvar = setvar;
math_hooks.endofname = endofname;
INT_OFF;
result = arith(s, &errcode, &math_hooks);
if (errcode < 0) {
if (errcode == -3)
ash_msg_and_raise_error("exponent less than 0");
if (errcode == -2)
ash_msg_and_raise_error("divide by zero");
if (errcode == -5)
ash_msg_and_raise_error("expression recursion loop detected");
raise_error_syntax(s);
}
INT_ON;
return result;
}
#endif #endif
/* /*
@ -5720,7 +5748,7 @@ expari(int quotes)
if (quotes) if (quotes)
rmescapes(p + 2); rmescapes(p + 2);
len = cvtnum(dash_arith(p + 2)); len = cvtnum(ash_arith(p + 2));
if (flag != '"') if (flag != '"')
recordregion(begoff, begoff + len, 0); recordregion(begoff, begoff + len, 0);
@ -10127,14 +10155,6 @@ static struct heredoc *heredoc;
*/ */
#define NEOF ((union node *)&tokpushback) #define NEOF ((union node *)&tokpushback)
static void raise_error_syntax(const char *) NORETURN;
static void
raise_error_syntax(const char *msg)
{
ash_msg_and_raise_error("syntax error: %s", msg);
/* NOTREACHED */
}
/* /*
* Called when an unexpected token is read during the parse. The argument * Called when an unexpected token is read during the parse. The argument
* is the token that is expected, or -1 if more than one type of token can * 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)
} }
#if ENABLE_SH_MATH_SUPPORT #if ENABLE_SH_MATH_SUPPORT
static arith_t
dash_arith(const char *s)
{
arith_t result;
int errcode = 0;
INT_OFF;
result = arith(s, &errcode, &math_hooks);
if (errcode < 0) {
if (errcode == -3)
ash_msg_and_raise_error("exponent less than 0");
if (errcode == -2)
ash_msg_and_raise_error("divide by zero");
if (errcode == -5)
ash_msg_and_raise_error("expression recursion loop detected");
raise_error_syntax(s);
}
INT_ON;
return result;
}
/* /*
* The let builtin. partial stolen from GNU Bash, the Bourne Again SHell. * The let builtin. partial stolen from GNU Bash, the Bourne Again SHell.
* Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc. * Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc.
* *
* Copyright (C) 2003 Vladimir Oleynik <dzo@simtreas.ru> * Copyright (C) 2003 Vladimir Oleynik <dzo@simtreas.ru>
*/ */
static int static int
letcmd(int argc UNUSED_PARAM, char **argv) letcmd(int argc UNUSED_PARAM, char **argv)
@ -12390,7 +12388,7 @@ letcmd(int argc UNUSED_PARAM, char **argv)
if (!*argv) if (!*argv)
ash_msg_and_raise_error("expression expected"); ash_msg_and_raise_error("expression expected");
do { do {
i = dash_arith(*argv); i = ash_arith(*argv);
} while (*++argv); } while (*++argv);
return !i; return !i;
@ -13119,11 +13117,6 @@ int ash_main(int argc UNUSED_PARAM, char **argv)
INIT_G_alias(); INIT_G_alias();
#endif #endif
INIT_G_cmdtable(); INIT_G_cmdtable();
#if ENABLE_SH_MATH_SUPPORT
math_hooks.lookupvar = lookupvar;
math_hooks.setvar = setvar;
math_hooks.endofname = endofname;
#endif
#if PROFILE #if PROFILE
monitor(4, etext, profile_buf, sizeof(profile_buf), 50); monitor(4, etext, profile_buf, sizeof(profile_buf), 50);

View File

@ -453,9 +453,6 @@ struct globals {
#endif #endif
#if ENABLE_FEATURE_EDITING #if ENABLE_FEATURE_EDITING
line_input_t *line_input_state; line_input_t *line_input_state;
#endif
#if ENABLE_SH_MATH_SUPPORT
arith_eval_hooks_t hooks;
#endif #endif
pid_t root_pid; pid_t root_pid;
pid_t last_bg_pid; pid_t last_bg_pid;
@ -1189,12 +1186,12 @@ static char *endofname(const char *name)
static void arith_set_local_var(const char *name, const char *val, int flags) static void arith_set_local_var(const char *name, const char *val, int flags)
{ {
/* arith code doesnt malloc space, so do it for it */ /* arith code doesnt malloc space, so do it for it */
char *var = xmalloc(strlen(name) + 1 + strlen(val) + 1); char *var = xasprintf("%s=%s", name, val);
sprintf(var, "%s=%s", name, val);
set_local_var(var, flags); set_local_var(var, flags);
} }
#endif #endif
/* /*
* in_str support * in_str support
*/ */
@ -1807,20 +1804,26 @@ static int expand_vars_to_list(o_string *output, int n, char *arg, char or_mask)
#endif #endif
#if ENABLE_SH_MATH_SUPPORT #if ENABLE_SH_MATH_SUPPORT
case '+': { /* <SPECIAL_VAR_SYMBOL>(cmd<SPECIAL_VAR_SYMBOL> */ case '+': { /* <SPECIAL_VAR_SYMBOL>(cmd<SPECIAL_VAR_SYMBOL> */
arith_eval_hooks_t hooks;
arith_t res; arith_t res;
char buf[30]; char buf[30];
int errcode; int errcode;
*p = '\0'; *p = '\0';
++arg; ++arg;
debug_printf_subst("ARITH '%s' first_ch %x\n", arg, first_ch); debug_printf_subst("ARITH '%s' first_ch %x\n", arg, first_ch);
res = arith(arg, &errcode, &G.hooks); hooks.lookupvar = lookup_param;
if (errcode < 0) hooks.setvar = arith_set_local_var;
hooks.endofname = endofname;
res = arith(arg, &errcode, &hooks);
if (errcode < 0) {
switch (errcode) { switch (errcode) {
case -3: maybe_die("arith", "exponent less than 0"); break; case -3: maybe_die("arith", "exponent less than 0"); break;
case -2: maybe_die("arith", "divide by zero"); break; case -2: maybe_die("arith", "divide by zero"); break;
case -5: maybe_die("arith", "expression recursion loop detected"); break; case -5: maybe_die("arith", "expression recursion loop detected"); break;
default: maybe_die("arith", "syntax error"); default: maybe_die("arith", "syntax error"); break;
} }
}
sprintf(buf, arith_t_fmt, res); sprintf(buf, arith_t_fmt, res);
o_addstrauto(output, buf); o_addstrauto(output, buf);
debug_printf_subst("ARITH RES '"arith_t_fmt"'\n", res); debug_printf_subst("ARITH RES '"arith_t_fmt"'\n", res);
@ -4600,11 +4603,6 @@ int hush_main(int argc, char **argv)
#if ENABLE_FEATURE_EDITING #if ENABLE_FEATURE_EDITING
G.line_input_state = new_line_input_t(FOR_SHELL); G.line_input_state = new_line_input_t(FOR_SHELL);
#endif
#if ENABLE_SH_MATH_SUPPORT
G.hooks.lookupvar = lookup_param;
G.hooks.setvar = arith_set_local_var;
G.hooks.endofname = endofname;
#endif #endif
/* XXX what should these be while sourcing /etc/profile? */ /* XXX what should these be while sourcing /etc/profile? */
G.global_argc = argc; G.global_argc = argc;