diff options
author | Denys Vlasenko <dvlasenk@redhat.com> | 2010-09-07 12:19:33 +0200 |
---|---|---|
committer | Denys Vlasenko <dvlasenk@redhat.com> | 2010-09-07 12:19:33 +0200 |
commit | 8b2f13d84def87b4ebd0901bf0c3157bc1f6dee3 (patch) | |
tree | 0bf072d3612c77dd8b0720973e46e8dbc37a040a | |
parent | 27c56f12670295286a881bbb87d506f0a5bfd40e (diff) | |
download | busybox-w32-8b2f13d84def87b4ebd0901bf0c3157bc1f6dee3.tar.gz busybox-w32-8b2f13d84def87b4ebd0901bf0c3157bc1f6dee3.tar.bz2 busybox-w32-8b2f13d84def87b4ebd0901bf0c3157bc1f6dee3.zip |
shell: unify endofname() in hush and ash
function old new delta
builtin_umask 132 133 +1
changepath 195 194 -1
expand_and_evaluate_arith 77 69 -8
ash_arith 143 135 -8
expand_one_var 1551 1515 -36
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/4 up/down: 1/-53) Total: -52 bytes
Signed-off-by: Denys Vlasenko <dvlasenk@redhat.com>
-rw-r--r-- | shell/ash.c | 42 | ||||
-rw-r--r-- | shell/hush.c | 20 | ||||
-rw-r--r-- | shell/math.c | 14 | ||||
-rw-r--r-- | shell/math.h | 9 |
4 files changed, 42 insertions, 43 deletions
diff --git a/shell/ash.c b/shell/ash.c index 273ecabdb..70425b324 100644 --- a/shell/ash.c +++ b/shell/ash.c | |||
@@ -1982,10 +1982,6 @@ extern struct globals_var *const ash_ptr_to_globals_var; | |||
1982 | # define optindval() (voptind.var_text + 7) | 1982 | # define optindval() (voptind.var_text + 7) |
1983 | #endif | 1983 | #endif |
1984 | 1984 | ||
1985 | |||
1986 | #define is_name(c) ((c) == '_' || isalpha((unsigned char)(c))) | ||
1987 | #define is_in_name(c) ((c) == '_' || isalnum((unsigned char)(c))) | ||
1988 | |||
1989 | #if ENABLE_ASH_GETOPTS | 1985 | #if ENABLE_ASH_GETOPTS |
1990 | static void FAST_FUNC | 1986 | static void FAST_FUNC |
1991 | getoptsreset(const char *value) | 1987 | getoptsreset(const char *value) |
@@ -1995,24 +1991,26 @@ getoptsreset(const char *value) | |||
1995 | } | 1991 | } |
1996 | #endif | 1992 | #endif |
1997 | 1993 | ||
1994 | /* math.h has these, otherwise define our private copies */ | ||
1995 | #if !ENABLE_SH_MATH_SUPPORT | ||
1996 | #define is_name(c) ((c) == '_' || isalpha((unsigned char)(c))) | ||
1997 | #define is_in_name(c) ((c) == '_' || isalnum((unsigned char)(c))) | ||
1998 | /* | 1998 | /* |
1999 | * Return of a legal variable name (a letter or underscore followed by zero or | 1999 | * Return the pointer to the first char which is not part of a legal variable name |
2000 | * more letters, underscores, and digits). | 2000 | * (a letter or underscore followed by letters, underscores, and digits). |
2001 | */ | 2001 | */ |
2002 | static char* FAST_FUNC | 2002 | static const char* |
2003 | endofname(const char *name) | 2003 | endofname(const char *name) |
2004 | { | 2004 | { |
2005 | char *p; | 2005 | if (!is_name(*name)) |
2006 | 2006 | return name; | |
2007 | p = (char *) name; | 2007 | while (*++name) { |
2008 | if (!is_name(*p)) | 2008 | if (!is_in_name(*name)) |
2009 | return p; | ||
2010 | while (*++p) { | ||
2011 | if (!is_in_name(*p)) | ||
2012 | break; | 2009 | break; |
2013 | } | 2010 | } |
2014 | return p; | 2011 | return name; |
2015 | } | 2012 | } |
2013 | #endif | ||
2016 | 2014 | ||
2017 | /* | 2015 | /* |
2018 | * Compares two strings up to the first = or '\0'. The first | 2016 | * Compares two strings up to the first = or '\0'. The first |
@@ -2195,9 +2193,10 @@ setvareq(char *s, int flags) | |||
2195 | static void | 2193 | static void |
2196 | setvar(const char *name, const char *val, int flags) | 2194 | setvar(const char *name, const char *val, int flags) |
2197 | { | 2195 | { |
2198 | char *p, *q; | 2196 | const char *q; |
2199 | size_t namelen; | 2197 | char *p; |
2200 | char *nameeq; | 2198 | char *nameeq; |
2199 | size_t namelen; | ||
2201 | size_t vallen; | 2200 | size_t vallen; |
2202 | 2201 | ||
2203 | q = endofname(name); | 2202 | q = endofname(name); |
@@ -2211,12 +2210,13 @@ setvar(const char *name, const char *val, int flags) | |||
2211 | } else { | 2210 | } else { |
2212 | vallen = strlen(val); | 2211 | vallen = strlen(val); |
2213 | } | 2212 | } |
2213 | |||
2214 | INT_OFF; | 2214 | INT_OFF; |
2215 | nameeq = ckmalloc(namelen + vallen + 2); | 2215 | nameeq = ckmalloc(namelen + vallen + 2); |
2216 | p = (char *)memcpy(nameeq, name, namelen) + namelen; | 2216 | p = memcpy(nameeq, name, namelen) + namelen; |
2217 | if (val) { | 2217 | if (val) { |
2218 | *p++ = '='; | 2218 | *p++ = '='; |
2219 | p = (char *)memcpy(p, val, vallen) + vallen; | 2219 | p = memcpy(p, val, vallen) + vallen; |
2220 | } | 2220 | } |
2221 | *p = '\0'; | 2221 | *p = '\0'; |
2222 | setvareq(nameeq, flags | VNOSAVE); | 2222 | setvareq(nameeq, flags | VNOSAVE); |
@@ -5444,7 +5444,7 @@ ash_arith(const char *s) | |||
5444 | 5444 | ||
5445 | math_hooks.lookupvar = lookupvar; | 5445 | math_hooks.lookupvar = lookupvar; |
5446 | math_hooks.setvar = setvar2; | 5446 | math_hooks.setvar = setvar2; |
5447 | math_hooks.endofname = endofname; | 5447 | //math_hooks.endofname = endofname; |
5448 | 5448 | ||
5449 | INT_OFF; | 5449 | INT_OFF; |
5450 | result = arith(s, &errcode, &math_hooks); | 5450 | result = arith(s, &errcode, &math_hooks); |
@@ -9405,7 +9405,7 @@ evalbltin(const struct builtincmd *cmd, int argc, char **argv) | |||
9405 | static int | 9405 | static int |
9406 | goodname(const char *p) | 9406 | goodname(const char *p) |
9407 | { | 9407 | { |
9408 | return !*endofname(p); | 9408 | return endofname(p)[0] == '\0'; |
9409 | } | 9409 | } |
9410 | 9410 | ||
9411 | 9411 | ||
diff --git a/shell/hush.c b/shell/hush.c index ae2876ac7..3e8c387e7 100644 --- a/shell/hush.c +++ b/shell/hush.c | |||
@@ -1671,24 +1671,6 @@ static void unset_vars(char **strings) | |||
1671 | free(strings); | 1671 | free(strings); |
1672 | } | 1672 | } |
1673 | 1673 | ||
1674 | #if ENABLE_SH_MATH_SUPPORT | ||
1675 | # define is_name(c) ((c) == '_' || isalpha((unsigned char)(c))) | ||
1676 | # define is_in_name(c) ((c) == '_' || isalnum((unsigned char)(c))) | ||
1677 | static char* FAST_FUNC endofname(const char *name) | ||
1678 | { | ||
1679 | char *p; | ||
1680 | |||
1681 | p = (char *) name; | ||
1682 | if (!is_name(*p)) | ||
1683 | return p; | ||
1684 | while (*++p) { | ||
1685 | if (!is_in_name(*p)) | ||
1686 | break; | ||
1687 | } | ||
1688 | return p; | ||
1689 | } | ||
1690 | #endif | ||
1691 | |||
1692 | static void FAST_FUNC set_local_var_from_halves(const char *name, const char *val) | 1674 | static void FAST_FUNC set_local_var_from_halves(const char *name, const char *val) |
1693 | { | 1675 | { |
1694 | char *var = xasprintf("%s=%s", name, val); | 1676 | char *var = xasprintf("%s=%s", name, val); |
@@ -4446,7 +4428,7 @@ static arith_t expand_and_evaluate_arith(const char *arg, int *errcode_p) | |||
4446 | 4428 | ||
4447 | hooks.lookupvar = get_local_var_value; | 4429 | hooks.lookupvar = get_local_var_value; |
4448 | hooks.setvar = set_local_var_from_halves; | 4430 | hooks.setvar = set_local_var_from_halves; |
4449 | hooks.endofname = endofname; | 4431 | //hooks.endofname = endofname; |
4450 | exp_str = expand_pseudo_dquoted(arg); | 4432 | exp_str = expand_pseudo_dquoted(arg); |
4451 | res = arith(exp_str ? exp_str : arg, errcode_p, &hooks); | 4433 | res = arith(exp_str ? exp_str : arg, errcode_p, &hooks); |
4452 | free(exp_str); | 4434 | free(exp_str); |
diff --git a/shell/math.c b/shell/math.c index f0cc2e35d..a4c55a4d0 100644 --- a/shell/math.c +++ b/shell/math.c | |||
@@ -122,7 +122,7 @@ | |||
122 | #define a_e_h_t arith_eval_hooks_t | 122 | #define a_e_h_t arith_eval_hooks_t |
123 | #define lookupvar (math_hooks->lookupvar) | 123 | #define lookupvar (math_hooks->lookupvar) |
124 | #define setvar (math_hooks->setvar ) | 124 | #define setvar (math_hooks->setvar ) |
125 | #define endofname (math_hooks->endofname) | 125 | //#define endofname (math_hooks->endofname) |
126 | 126 | ||
127 | #define arith_isspace(arithval) \ | 127 | #define arith_isspace(arithval) \ |
128 | (arithval == ' ' || arithval == '\n' || arithval == '\t') | 128 | (arithval == ' ' || arithval == '\n' || arithval == '\t') |
@@ -479,6 +479,18 @@ static const char op_tokens[] ALIGN1 = { | |||
479 | /* ptr to ")" */ | 479 | /* ptr to ")" */ |
480 | #define endexpression (&op_tokens[sizeof(op_tokens)-7]) | 480 | #define endexpression (&op_tokens[sizeof(op_tokens)-7]) |
481 | 481 | ||
482 | const char* FAST_FUNC | ||
483 | endofname(const char *name) | ||
484 | { | ||
485 | if (!is_name(*name)) | ||
486 | return name; | ||
487 | while (*++name) { | ||
488 | if (!is_in_name(*name)) | ||
489 | break; | ||
490 | } | ||
491 | return name; | ||
492 | } | ||
493 | |||
482 | arith_t | 494 | arith_t |
483 | arith(const char *expr, int *perrcode, a_e_h_t *math_hooks) | 495 | arith(const char *expr, int *perrcode, a_e_h_t *math_hooks) |
484 | { | 496 | { |
diff --git a/shell/math.h b/shell/math.h index 2b0b2b89d..96088b4d2 100644 --- a/shell/math.h +++ b/shell/math.h | |||
@@ -87,14 +87,19 @@ typedef long arith_t; | |||
87 | #define strto_arith_t strtoul | 87 | #define strto_arith_t strtoul |
88 | #endif | 88 | #endif |
89 | 89 | ||
90 | /* ash's and hush's endofname is the same, so... */ | ||
91 | # define is_name(c) ((c) == '_' || isalpha((unsigned char)(c))) | ||
92 | # define is_in_name(c) ((c) == '_' || isalnum((unsigned char)(c))) | ||
93 | const char* FAST_FUNC endofname(const char *name); | ||
94 | |||
90 | typedef const char* FAST_FUNC (*arith_var_lookup_t)(const char *name); | 95 | typedef const char* FAST_FUNC (*arith_var_lookup_t)(const char *name); |
91 | typedef void FAST_FUNC (*arith_var_set_t)(const char *name, const char *val); | 96 | typedef void FAST_FUNC (*arith_var_set_t)(const char *name, const char *val); |
92 | typedef char* FAST_FUNC (*arith_var_endofname_t)(const char *name); | 97 | //typedef const char* FAST_FUNC (*arith_var_endofname_t)(const char *name); |
93 | 98 | ||
94 | typedef struct arith_eval_hooks { | 99 | typedef struct arith_eval_hooks { |
95 | arith_var_lookup_t lookupvar; | 100 | arith_var_lookup_t lookupvar; |
96 | arith_var_set_t setvar; | 101 | arith_var_set_t setvar; |
97 | arith_var_endofname_t endofname; | 102 | // arith_var_endofname_t endofname; |
98 | } arith_eval_hooks_t; | 103 | } arith_eval_hooks_t; |
99 | 104 | ||
100 | arith_t arith(const char *expr, int *perrcode, arith_eval_hooks_t*); | 105 | arith_t arith(const char *expr, int *perrcode, arith_eval_hooks_t*); |