diff options
author | Denys Vlasenko <dvlasenk@redhat.com> | 2010-09-12 15:04:27 +0200 |
---|---|---|
committer | Denys Vlasenko <dvlasenk@redhat.com> | 2010-09-12 15:04:27 +0200 |
commit | 958581a8d9583da8b9df0b69123e0c3990f7b3ff (patch) | |
tree | 607ae2e4129235063fd0a6e270770f4533d20e99 /shell/ash.c | |
parent | 6040fe88f4f6286da781dc9196ede4c6be310a04 (diff) | |
download | busybox-w32-958581a8d9583da8b9df0b69123e0c3990f7b3ff.tar.gz busybox-w32-958581a8d9583da8b9df0b69123e0c3990f7b3ff.tar.bz2 busybox-w32-958581a8d9583da8b9df0b69123e0c3990f7b3ff.zip |
ash: shrink by folding an if check into setprompt; unindent big block
function old new delta
setprompt_if - 66 +66
parseheredoc 126 124 -2
parsecmd 68 66 -2
redirect 1254 1249 -5
xxreadtoken 351 343 -8
readtoken1 3219 3204 -15
setprompt 62 - -62
------------------------------------------------------------------------------
(add/remove: 1/1 grow/shrink: 0/5 up/down: 66/-94) Total: -28 bytes
Signed-off-by: Denys Vlasenko <dvlasenk@redhat.com>
Diffstat (limited to 'shell/ash.c')
-rw-r--r-- | shell/ash.c | 315 |
1 files changed, 149 insertions, 166 deletions
diff --git a/shell/ash.c b/shell/ash.c index b618a47f9..f631e3d25 100644 --- a/shell/ash.c +++ b/shell/ash.c | |||
@@ -2433,12 +2433,13 @@ static const char *expandstr(const char *ps); | |||
2433 | #endif | 2433 | #endif |
2434 | 2434 | ||
2435 | static void | 2435 | static void |
2436 | setprompt(int whichprompt) | 2436 | setprompt_if(smallint do_set, int whichprompt) |
2437 | { | 2437 | { |
2438 | const char *prompt; | 2438 | const char *prompt; |
2439 | #if ENABLE_ASH_EXPAND_PRMT | 2439 | IF_ASH_EXPAND_PRMT(struct stackmark smark;) |
2440 | struct stackmark smark; | 2440 | |
2441 | #endif | 2441 | if (!do_set) |
2442 | return; | ||
2442 | 2443 | ||
2443 | needprompt = 0; | 2444 | needprompt = 0; |
2444 | 2445 | ||
@@ -6022,9 +6023,7 @@ argstr(char *p, int flags, struct strlist *var_str_list) | |||
6022 | c = p[length]; | 6023 | c = p[length]; |
6023 | if (c) { | 6024 | if (c) { |
6024 | if (!(c & 0x80) | 6025 | if (!(c & 0x80) |
6025 | #if ENABLE_SH_MATH_SUPPORT | 6026 | IF_SH_MATH_SUPPORT(|| c == CTLENDARI) |
6026 | || c == CTLENDARI | ||
6027 | #endif | ||
6028 | ) { | 6027 | ) { |
6029 | /* c == '=' || c == ':' || c == CTLENDARI */ | 6028 | /* c == '=' || c == ':' || c == CTLENDARI */ |
6030 | length++; | 6029 | length++; |
@@ -6107,8 +6106,7 @@ argstr(char *p, int flags, struct strlist *var_str_list) | |||
6107 | #endif | 6106 | #endif |
6108 | } | 6107 | } |
6109 | } | 6108 | } |
6110 | breakloop: | 6109 | breakloop: ; |
6111 | ; | ||
6112 | } | 6110 | } |
6113 | 6111 | ||
6114 | static char * | 6112 | static char * |
@@ -11054,159 +11052,156 @@ readtoken1(int c, int syntax, char *eofmark, int striptabs) | |||
11054 | STARTSTACKSTR(out); | 11052 | STARTSTACKSTR(out); |
11055 | loop: | 11053 | loop: |
11056 | /* For each line, until end of word */ | 11054 | /* For each line, until end of word */ |
11057 | { | 11055 | CHECKEND(); /* set c to PEOF if at end of here document */ |
11058 | CHECKEND(); /* set c to PEOF if at end of here document */ | 11056 | for (;;) { /* until end of line or end of word */ |
11059 | for (;;) { /* until end of line or end of word */ | 11057 | CHECKSTRSPACE(4, out); /* permit 4 calls to USTPUTC */ |
11060 | CHECKSTRSPACE(4, out); /* permit 4 calls to USTPUTC */ | 11058 | switch (SIT(c, syntax)) { |
11061 | switch (SIT(c, syntax)) { | 11059 | case CNL: /* '\n' */ |
11062 | case CNL: /* '\n' */ | 11060 | if (syntax == BASESYNTAX) |
11063 | if (syntax == BASESYNTAX) | 11061 | goto endword; /* exit outer loop */ |
11064 | goto endword; /* exit outer loop */ | 11062 | USTPUTC(c, out); |
11065 | USTPUTC(c, out); | 11063 | g_parsefile->linno++; |
11066 | g_parsefile->linno++; | 11064 | setprompt_if(doprompt, 2); |
11067 | if (doprompt) | 11065 | c = pgetc(); |
11068 | setprompt(2); | 11066 | goto loop; /* continue outer loop */ |
11069 | c = pgetc(); | 11067 | case CWORD: |
11070 | goto loop; /* continue outer loop */ | 11068 | USTPUTC(c, out); |
11071 | case CWORD: | 11069 | break; |
11072 | USTPUTC(c, out); | 11070 | case CCTL: |
11073 | break; | 11071 | if (eofmark == NULL || dblquote) |
11074 | case CCTL: | 11072 | USTPUTC(CTLESC, out); |
11075 | if (eofmark == NULL || dblquote) | ||
11076 | USTPUTC(CTLESC, out); | ||
11077 | #if ENABLE_ASH_BASH_COMPAT | 11073 | #if ENABLE_ASH_BASH_COMPAT |
11078 | if (c == '\\' && bash_dollar_squote) { | 11074 | if (c == '\\' && bash_dollar_squote) { |
11079 | c = decode_dollar_squote(); | 11075 | c = decode_dollar_squote(); |
11080 | if (c & 0x100) { | 11076 | if (c & 0x100) { |
11081 | USTPUTC('\\', out); | 11077 | USTPUTC('\\', out); |
11082 | c = (unsigned char)c; | 11078 | c = (unsigned char)c; |
11083 | } | ||
11084 | } | 11079 | } |
11080 | } | ||
11085 | #endif | 11081 | #endif |
11086 | USTPUTC(c, out); | 11082 | USTPUTC(c, out); |
11087 | break; | 11083 | break; |
11088 | case CBACK: /* backslash */ | 11084 | case CBACK: /* backslash */ |
11089 | c = pgetc_without_PEOA(); | 11085 | c = pgetc_without_PEOA(); |
11090 | if (c == PEOF) { | 11086 | if (c == PEOF) { |
11087 | USTPUTC(CTLESC, out); | ||
11088 | USTPUTC('\\', out); | ||
11089 | pungetc(); | ||
11090 | } else if (c == '\n') { | ||
11091 | setprompt_if(doprompt, 2); | ||
11092 | } else { | ||
11093 | #if ENABLE_ASH_EXPAND_PRMT | ||
11094 | if (c == '$' && pssyntax) { | ||
11091 | USTPUTC(CTLESC, out); | 11095 | USTPUTC(CTLESC, out); |
11092 | USTPUTC('\\', out); | 11096 | USTPUTC('\\', out); |
11093 | pungetc(); | 11097 | } |
11094 | } else if (c == '\n') { | ||
11095 | if (doprompt) | ||
11096 | setprompt(2); | ||
11097 | } else { | ||
11098 | #if ENABLE_ASH_EXPAND_PRMT | ||
11099 | if (c == '$' && pssyntax) { | ||
11100 | USTPUTC(CTLESC, out); | ||
11101 | USTPUTC('\\', out); | ||
11102 | } | ||
11103 | #endif | 11098 | #endif |
11104 | /* Backslash is retained if we are in "str" and next char isn't special */ | 11099 | /* Backslash is retained if we are in "str" and next char isn't special */ |
11105 | if (dblquote | 11100 | if (dblquote |
11106 | && c != '\\' | 11101 | && c != '\\' |
11107 | && c != '`' | 11102 | && c != '`' |
11108 | && c != '$' | 11103 | && c != '$' |
11109 | && (c != '"' || eofmark != NULL) | 11104 | && (c != '"' || eofmark != NULL) |
11110 | ) { | 11105 | ) { |
11111 | USTPUTC(CTLESC, out); | 11106 | USTPUTC(CTLESC, out); |
11112 | USTPUTC('\\', out); | 11107 | USTPUTC('\\', out); |
11113 | } | ||
11114 | if (SIT(c, SQSYNTAX) == CCTL) | ||
11115 | USTPUTC(CTLESC, out); | ||
11116 | USTPUTC(c, out); | ||
11117 | quotef = 1; | ||
11118 | } | 11108 | } |
11119 | break; | 11109 | if (SIT(c, SQSYNTAX) == CCTL) |
11120 | case CSQUOTE: | 11110 | USTPUTC(CTLESC, out); |
11121 | syntax = SQSYNTAX; | 11111 | USTPUTC(c, out); |
11112 | quotef = 1; | ||
11113 | } | ||
11114 | break; | ||
11115 | case CSQUOTE: | ||
11116 | syntax = SQSYNTAX; | ||
11122 | quotemark: | 11117 | quotemark: |
11123 | if (eofmark == NULL) { | 11118 | if (eofmark == NULL) { |
11124 | USTPUTC(CTLQUOTEMARK, out); | 11119 | USTPUTC(CTLQUOTEMARK, out); |
11120 | } | ||
11121 | break; | ||
11122 | case CDQUOTE: | ||
11123 | syntax = DQSYNTAX; | ||
11124 | dblquote = 1; | ||
11125 | goto quotemark; | ||
11126 | case CENDQUOTE: | ||
11127 | IF_ASH_BASH_COMPAT(bash_dollar_squote = 0;) | ||
11128 | if (eofmark != NULL && arinest == 0 | ||
11129 | && varnest == 0 | ||
11130 | ) { | ||
11131 | USTPUTC(c, out); | ||
11132 | } else { | ||
11133 | if (dqvarnest == 0) { | ||
11134 | syntax = BASESYNTAX; | ||
11135 | dblquote = 0; | ||
11125 | } | 11136 | } |
11126 | break; | 11137 | quotef = 1; |
11127 | case CDQUOTE: | ||
11128 | syntax = DQSYNTAX; | ||
11129 | dblquote = 1; | ||
11130 | goto quotemark; | 11138 | goto quotemark; |
11131 | case CENDQUOTE: | 11139 | } |
11132 | IF_ASH_BASH_COMPAT(bash_dollar_squote = 0;) | 11140 | break; |
11133 | if (eofmark != NULL && arinest == 0 | 11141 | case CVAR: /* '$' */ |
11134 | && varnest == 0 | 11142 | PARSESUB(); /* parse substitution */ |
11135 | ) { | 11143 | break; |
11136 | USTPUTC(c, out); | 11144 | case CENDVAR: /* '}' */ |
11137 | } else { | 11145 | if (varnest > 0) { |
11138 | if (dqvarnest == 0) { | 11146 | varnest--; |
11139 | syntax = BASESYNTAX; | 11147 | if (dqvarnest > 0) { |
11140 | dblquote = 0; | 11148 | dqvarnest--; |
11141 | } | ||
11142 | quotef = 1; | ||
11143 | goto quotemark; | ||
11144 | } | ||
11145 | break; | ||
11146 | case CVAR: /* '$' */ | ||
11147 | PARSESUB(); /* parse substitution */ | ||
11148 | break; | ||
11149 | case CENDVAR: /* '}' */ | ||
11150 | if (varnest > 0) { | ||
11151 | varnest--; | ||
11152 | if (dqvarnest > 0) { | ||
11153 | dqvarnest--; | ||
11154 | } | ||
11155 | c = CTLENDVAR; | ||
11156 | } | 11149 | } |
11157 | USTPUTC(c, out); | 11150 | c = CTLENDVAR; |
11158 | break; | 11151 | } |
11152 | USTPUTC(c, out); | ||
11153 | break; | ||
11159 | #if ENABLE_SH_MATH_SUPPORT | 11154 | #if ENABLE_SH_MATH_SUPPORT |
11160 | case CLP: /* '(' in arithmetic */ | 11155 | case CLP: /* '(' in arithmetic */ |
11161 | parenlevel++; | 11156 | parenlevel++; |
11162 | USTPUTC(c, out); | 11157 | USTPUTC(c, out); |
11163 | break; | 11158 | break; |
11164 | case CRP: /* ')' in arithmetic */ | 11159 | case CRP: /* ')' in arithmetic */ |
11165 | if (parenlevel > 0) { | 11160 | if (parenlevel > 0) { |
11166 | parenlevel--; | 11161 | parenlevel--; |
11167 | } else { | 11162 | } else { |
11168 | if (pgetc() == ')') { | 11163 | if (pgetc() == ')') { |
11169 | if (--arinest == 0) { | 11164 | if (--arinest == 0) { |
11170 | syntax = prevsyntax; | 11165 | syntax = prevsyntax; |
11171 | dblquote = (syntax == DQSYNTAX); | 11166 | dblquote = (syntax == DQSYNTAX); |
11172 | c = CTLENDARI; | 11167 | c = CTLENDARI; |
11173 | } | ||
11174 | } else { | ||
11175 | /* | ||
11176 | * unbalanced parens | ||
11177 | * (don't 2nd guess - no error) | ||
11178 | */ | ||
11179 | pungetc(); | ||
11180 | } | 11168 | } |
11169 | } else { | ||
11170 | /* | ||
11171 | * unbalanced parens | ||
11172 | * (don't 2nd guess - no error) | ||
11173 | */ | ||
11174 | pungetc(); | ||
11181 | } | 11175 | } |
11182 | USTPUTC(c, out); | 11176 | } |
11183 | break; | 11177 | USTPUTC(c, out); |
11178 | break; | ||
11184 | #endif | 11179 | #endif |
11185 | case CBQUOTE: /* '`' */ | 11180 | case CBQUOTE: /* '`' */ |
11186 | PARSEBACKQOLD(); | 11181 | PARSEBACKQOLD(); |
11187 | break; | 11182 | break; |
11188 | case CENDFILE: | 11183 | case CENDFILE: |
11189 | goto endword; /* exit outer loop */ | 11184 | goto endword; /* exit outer loop */ |
11190 | case CIGN: | 11185 | case CIGN: |
11191 | break; | 11186 | break; |
11192 | default: | 11187 | default: |
11193 | if (varnest == 0) { | 11188 | if (varnest == 0) { |
11194 | #if ENABLE_ASH_BASH_COMPAT | 11189 | #if ENABLE_ASH_BASH_COMPAT |
11195 | if (c == '&') { | 11190 | if (c == '&') { |
11196 | if (pgetc() == '>') | 11191 | if (pgetc() == '>') |
11197 | c = 0x100 + '>'; /* flag &> */ | 11192 | c = 0x100 + '>'; /* flag &> */ |
11198 | pungetc(); | 11193 | pungetc(); |
11199 | } | ||
11200 | #endif | ||
11201 | goto endword; /* exit outer loop */ | ||
11202 | } | 11194 | } |
11203 | IF_ASH_ALIAS(if (c != PEOA)) | 11195 | #endif |
11204 | USTPUTC(c, out); | 11196 | goto endword; /* exit outer loop */ |
11205 | } | 11197 | } |
11206 | c = pgetc_fast(); | 11198 | IF_ASH_ALIAS(if (c != PEOA)) |
11207 | } /* for (;;) */ | 11199 | USTPUTC(c, out); |
11208 | } | 11200 | } |
11201 | c = pgetc_fast(); | ||
11202 | } /* for (;;) */ | ||
11209 | endword: | 11203 | endword: |
11204 | |||
11210 | #if ENABLE_SH_MATH_SUPPORT | 11205 | #if ENABLE_SH_MATH_SUPPORT |
11211 | if (syntax == ARISYNTAX) | 11206 | if (syntax == ARISYNTAX) |
11212 | raise_error_syntax("missing '))'"); | 11207 | raise_error_syntax("missing '))'"); |
@@ -11542,16 +11537,14 @@ parsebackq: { | |||
11542 | treatment to some slashes, and then push the string and | 11537 | treatment to some slashes, and then push the string and |
11543 | reread it as input, interpreting it normally. */ | 11538 | reread it as input, interpreting it normally. */ |
11544 | char *pout; | 11539 | char *pout; |
11545 | int pc; | ||
11546 | size_t psavelen; | 11540 | size_t psavelen; |
11547 | char *pstr; | 11541 | char *pstr; |
11548 | 11542 | ||
11549 | |||
11550 | STARTSTACKSTR(pout); | 11543 | STARTSTACKSTR(pout); |
11551 | for (;;) { | 11544 | for (;;) { |
11552 | if (needprompt) { | 11545 | int pc; |
11553 | setprompt(2); | 11546 | |
11554 | } | 11547 | setprompt_if(needprompt, 2); |
11555 | pc = pgetc(); | 11548 | pc = pgetc(); |
11556 | switch (pc) { | 11549 | switch (pc) { |
11557 | case '`': | 11550 | case '`': |
@@ -11561,8 +11554,7 @@ parsebackq: { | |||
11561 | pc = pgetc(); | 11554 | pc = pgetc(); |
11562 | if (pc == '\n') { | 11555 | if (pc == '\n') { |
11563 | g_parsefile->linno++; | 11556 | g_parsefile->linno++; |
11564 | if (doprompt) | 11557 | setprompt_if(doprompt, 2); |
11565 | setprompt(2); | ||
11566 | /* | 11558 | /* |
11567 | * If eating a newline, avoid putting | 11559 | * If eating a newline, avoid putting |
11568 | * the newline into the new character | 11560 | * the newline into the new character |
@@ -11725,9 +11717,7 @@ xxreadtoken(void) | |||
11725 | tokpushback = 0; | 11717 | tokpushback = 0; |
11726 | return lasttoken; | 11718 | return lasttoken; |
11727 | } | 11719 | } |
11728 | if (needprompt) { | 11720 | setprompt_if(needprompt, 2); |
11729 | setprompt(2); | ||
11730 | } | ||
11731 | startlinno = g_parsefile->linno; | 11721 | startlinno = g_parsefile->linno; |
11732 | for (;;) { /* until token or start of word found */ | 11722 | for (;;) { /* until token or start of word found */ |
11733 | c = pgetc_fast(); | 11723 | c = pgetc_fast(); |
@@ -11744,8 +11734,7 @@ xxreadtoken(void) | |||
11744 | break; /* return readtoken1(...) */ | 11734 | break; /* return readtoken1(...) */ |
11745 | } | 11735 | } |
11746 | startlinno = ++g_parsefile->linno; | 11736 | startlinno = ++g_parsefile->linno; |
11747 | if (doprompt) | 11737 | setprompt_if(doprompt, 2); |
11748 | setprompt(2); | ||
11749 | } else { | 11738 | } else { |
11750 | const char *p; | 11739 | const char *p; |
11751 | 11740 | ||
@@ -11791,9 +11780,7 @@ xxreadtoken(void) | |||
11791 | tokpushback = 0; | 11780 | tokpushback = 0; |
11792 | return lasttoken; | 11781 | return lasttoken; |
11793 | } | 11782 | } |
11794 | if (needprompt) { | 11783 | setprompt_if(needprompt, 2); |
11795 | setprompt(2); | ||
11796 | } | ||
11797 | startlinno = g_parsefile->linno; | 11784 | startlinno = g_parsefile->linno; |
11798 | for (;;) { /* until token or start of word found */ | 11785 | for (;;) { /* until token or start of word found */ |
11799 | c = pgetc_fast(); | 11786 | c = pgetc_fast(); |
@@ -11809,8 +11796,7 @@ xxreadtoken(void) | |||
11809 | case '\\': | 11796 | case '\\': |
11810 | if (pgetc() == '\n') { | 11797 | if (pgetc() == '\n') { |
11811 | startlinno = ++g_parsefile->linno; | 11798 | startlinno = ++g_parsefile->linno; |
11812 | if (doprompt) | 11799 | setprompt_if(doprompt, 2); |
11813 | setprompt(2); | ||
11814 | continue; | 11800 | continue; |
11815 | } | 11801 | } |
11816 | pungetc(); | 11802 | pungetc(); |
@@ -11936,8 +11922,7 @@ parsecmd(int interact) | |||
11936 | 11922 | ||
11937 | tokpushback = 0; | 11923 | tokpushback = 0; |
11938 | doprompt = interact; | 11924 | doprompt = interact; |
11939 | if (doprompt) | 11925 | setprompt_if(doprompt, doprompt); |
11940 | setprompt(doprompt); | ||
11941 | needprompt = 0; | 11926 | needprompt = 0; |
11942 | t = readtoken(); | 11927 | t = readtoken(); |
11943 | if (t == TEOF) | 11928 | if (t == TEOF) |
@@ -11961,10 +11946,8 @@ parseheredoc(void) | |||
11961 | heredoclist = NULL; | 11946 | heredoclist = NULL; |
11962 | 11947 | ||
11963 | while (here) { | 11948 | while (here) { |
11964 | if (needprompt) { | 11949 | setprompt_if(needprompt, 2); |
11965 | setprompt(2); | 11950 | readtoken1(pgetc(), here->here->type == NHERE ? SQSYNTAX : DQSYNTAX, |
11966 | } | ||
11967 | readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX, | ||
11968 | here->eofmark, here->striptabs); | 11951 | here->eofmark, here->striptabs); |
11969 | n = stzalloc(sizeof(struct narg)); | 11952 | n = stzalloc(sizeof(struct narg)); |
11970 | n->narg.type = NARG; | 11953 | n->narg.type = NARG; |