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>
This commit is contained in:
Denys Vlasenko 2010-09-12 15:04:27 +02:00
parent 6040fe88f4
commit 958581a8d9

View File

@ -2433,12 +2433,13 @@ static const char *expandstr(const char *ps);
#endif #endif
static void static void
setprompt(int whichprompt) setprompt_if(smallint do_set, int whichprompt)
{ {
const char *prompt; const char *prompt;
#if ENABLE_ASH_EXPAND_PRMT IF_ASH_EXPAND_PRMT(struct stackmark smark;)
struct stackmark smark;
#endif if (!do_set)
return;
needprompt = 0; needprompt = 0;
@ -6022,9 +6023,7 @@ argstr(char *p, int flags, struct strlist *var_str_list)
c = p[length]; c = p[length];
if (c) { if (c) {
if (!(c & 0x80) if (!(c & 0x80)
#if ENABLE_SH_MATH_SUPPORT IF_SH_MATH_SUPPORT(|| c == CTLENDARI)
|| c == CTLENDARI
#endif
) { ) {
/* c == '=' || c == ':' || c == CTLENDARI */ /* c == '=' || c == ':' || c == CTLENDARI */
length++; length++;
@ -6107,8 +6106,7 @@ argstr(char *p, int flags, struct strlist *var_str_list)
#endif #endif
} }
} }
breakloop: breakloop: ;
;
} }
static char * static char *
@ -11054,159 +11052,156 @@ readtoken1(int c, int syntax, char *eofmark, int striptabs)
STARTSTACKSTR(out); STARTSTACKSTR(out);
loop: loop:
/* For each line, until end of word */ /* For each line, until end of word */
{ CHECKEND(); /* set c to PEOF if at end of here document */
CHECKEND(); /* set c to PEOF if at end of here document */ for (;;) { /* until end of line or end of word */
for (;;) { /* until end of line or end of word */ CHECKSTRSPACE(4, out); /* permit 4 calls to USTPUTC */
CHECKSTRSPACE(4, out); /* permit 4 calls to USTPUTC */ switch (SIT(c, syntax)) {
switch (SIT(c, syntax)) { case CNL: /* '\n' */
case CNL: /* '\n' */ if (syntax == BASESYNTAX)
if (syntax == BASESYNTAX) goto endword; /* exit outer loop */
goto endword; /* exit outer loop */ USTPUTC(c, out);
USTPUTC(c, out); g_parsefile->linno++;
g_parsefile->linno++; setprompt_if(doprompt, 2);
if (doprompt) c = pgetc();
setprompt(2); goto loop; /* continue outer loop */
c = pgetc(); case CWORD:
goto loop; /* continue outer loop */ USTPUTC(c, out);
case CWORD: break;
USTPUTC(c, out); case CCTL:
break; if (eofmark == NULL || dblquote)
case CCTL: USTPUTC(CTLESC, out);
if (eofmark == NULL || dblquote)
USTPUTC(CTLESC, out);
#if ENABLE_ASH_BASH_COMPAT #if ENABLE_ASH_BASH_COMPAT
if (c == '\\' && bash_dollar_squote) { if (c == '\\' && bash_dollar_squote) {
c = decode_dollar_squote(); c = decode_dollar_squote();
if (c & 0x100) { if (c & 0x100) {
USTPUTC('\\', out); USTPUTC('\\', out);
c = (unsigned char)c; c = (unsigned char)c;
}
} }
}
#endif #endif
USTPUTC(c, out); USTPUTC(c, out);
break; break;
case CBACK: /* backslash */ case CBACK: /* backslash */
c = pgetc_without_PEOA(); c = pgetc_without_PEOA();
if (c == PEOF) { if (c == PEOF) {
USTPUTC(CTLESC, out);
USTPUTC('\\', out);
pungetc();
} else if (c == '\n') {
setprompt_if(doprompt, 2);
} else {
#if ENABLE_ASH_EXPAND_PRMT
if (c == '$' && pssyntax) {
USTPUTC(CTLESC, out); USTPUTC(CTLESC, out);
USTPUTC('\\', out); USTPUTC('\\', out);
pungetc(); }
} else if (c == '\n') {
if (doprompt)
setprompt(2);
} else {
#if ENABLE_ASH_EXPAND_PRMT
if (c == '$' && pssyntax) {
USTPUTC(CTLESC, out);
USTPUTC('\\', out);
}
#endif #endif
/* Backslash is retained if we are in "str" and next char isn't special */ /* Backslash is retained if we are in "str" and next char isn't special */
if (dblquote if (dblquote
&& c != '\\' && c != '\\'
&& c != '`' && c != '`'
&& c != '$' && c != '$'
&& (c != '"' || eofmark != NULL) && (c != '"' || eofmark != NULL)
) {
USTPUTC(CTLESC, out);
USTPUTC('\\', out);
}
if (SIT(c, SQSYNTAX) == CCTL)
USTPUTC(CTLESC, out);
USTPUTC(c, out);
quotef = 1;
}
break;
case CSQUOTE:
syntax = SQSYNTAX;
quotemark:
if (eofmark == NULL) {
USTPUTC(CTLQUOTEMARK, out);
}
break;
case CDQUOTE:
syntax = DQSYNTAX;
dblquote = 1;
goto quotemark;
case CENDQUOTE:
IF_ASH_BASH_COMPAT(bash_dollar_squote = 0;)
if (eofmark != NULL && arinest == 0
&& varnest == 0
) { ) {
USTPUTC(c, out); USTPUTC(CTLESC, out);
} else { USTPUTC('\\', out);
if (dqvarnest == 0) {
syntax = BASESYNTAX;
dblquote = 0;
}
quotef = 1;
goto quotemark;
}
break;
case CVAR: /* '$' */
PARSESUB(); /* parse substitution */
break;
case CENDVAR: /* '}' */
if (varnest > 0) {
varnest--;
if (dqvarnest > 0) {
dqvarnest--;
}
c = CTLENDVAR;
} }
if (SIT(c, SQSYNTAX) == CCTL)
USTPUTC(CTLESC, out);
USTPUTC(c, out); USTPUTC(c, out);
break; quotef = 1;
#if ENABLE_SH_MATH_SUPPORT
case CLP: /* '(' in arithmetic */
parenlevel++;
USTPUTC(c, out);
break;
case CRP: /* ')' in arithmetic */
if (parenlevel > 0) {
parenlevel--;
} else {
if (pgetc() == ')') {
if (--arinest == 0) {
syntax = prevsyntax;
dblquote = (syntax == DQSYNTAX);
c = CTLENDARI;
}
} else {
/*
* unbalanced parens
* (don't 2nd guess - no error)
*/
pungetc();
}
}
USTPUTC(c, out);
break;
#endif
case CBQUOTE: /* '`' */
PARSEBACKQOLD();
break;
case CENDFILE:
goto endword; /* exit outer loop */
case CIGN:
break;
default:
if (varnest == 0) {
#if ENABLE_ASH_BASH_COMPAT
if (c == '&') {
if (pgetc() == '>')
c = 0x100 + '>'; /* flag &> */
pungetc();
}
#endif
goto endword; /* exit outer loop */
}
IF_ASH_ALIAS(if (c != PEOA))
USTPUTC(c, out);
} }
c = pgetc_fast(); break;
} /* for (;;) */ case CSQUOTE:
} syntax = SQSYNTAX;
quotemark:
if (eofmark == NULL) {
USTPUTC(CTLQUOTEMARK, out);
}
break;
case CDQUOTE:
syntax = DQSYNTAX;
dblquote = 1;
goto quotemark;
case CENDQUOTE:
IF_ASH_BASH_COMPAT(bash_dollar_squote = 0;)
if (eofmark != NULL && arinest == 0
&& varnest == 0
) {
USTPUTC(c, out);
} else {
if (dqvarnest == 0) {
syntax = BASESYNTAX;
dblquote = 0;
}
quotef = 1;
goto quotemark;
}
break;
case CVAR: /* '$' */
PARSESUB(); /* parse substitution */
break;
case CENDVAR: /* '}' */
if (varnest > 0) {
varnest--;
if (dqvarnest > 0) {
dqvarnest--;
}
c = CTLENDVAR;
}
USTPUTC(c, out);
break;
#if ENABLE_SH_MATH_SUPPORT
case CLP: /* '(' in arithmetic */
parenlevel++;
USTPUTC(c, out);
break;
case CRP: /* ')' in arithmetic */
if (parenlevel > 0) {
parenlevel--;
} else {
if (pgetc() == ')') {
if (--arinest == 0) {
syntax = prevsyntax;
dblquote = (syntax == DQSYNTAX);
c = CTLENDARI;
}
} else {
/*
* unbalanced parens
* (don't 2nd guess - no error)
*/
pungetc();
}
}
USTPUTC(c, out);
break;
#endif
case CBQUOTE: /* '`' */
PARSEBACKQOLD();
break;
case CENDFILE:
goto endword; /* exit outer loop */
case CIGN:
break;
default:
if (varnest == 0) {
#if ENABLE_ASH_BASH_COMPAT
if (c == '&') {
if (pgetc() == '>')
c = 0x100 + '>'; /* flag &> */
pungetc();
}
#endif
goto endword; /* exit outer loop */
}
IF_ASH_ALIAS(if (c != PEOA))
USTPUTC(c, out);
}
c = pgetc_fast();
} /* for (;;) */
endword: endword:
#if ENABLE_SH_MATH_SUPPORT #if ENABLE_SH_MATH_SUPPORT
if (syntax == ARISYNTAX) if (syntax == ARISYNTAX)
raise_error_syntax("missing '))'"); raise_error_syntax("missing '))'");
@ -11542,16 +11537,14 @@ parsebackq: {
treatment to some slashes, and then push the string and treatment to some slashes, and then push the string and
reread it as input, interpreting it normally. */ reread it as input, interpreting it normally. */
char *pout; char *pout;
int pc;
size_t psavelen; size_t psavelen;
char *pstr; char *pstr;
STARTSTACKSTR(pout); STARTSTACKSTR(pout);
for (;;) { for (;;) {
if (needprompt) { int pc;
setprompt(2);
} setprompt_if(needprompt, 2);
pc = pgetc(); pc = pgetc();
switch (pc) { switch (pc) {
case '`': case '`':
@ -11561,8 +11554,7 @@ parsebackq: {
pc = pgetc(); pc = pgetc();
if (pc == '\n') { if (pc == '\n') {
g_parsefile->linno++; g_parsefile->linno++;
if (doprompt) setprompt_if(doprompt, 2);
setprompt(2);
/* /*
* If eating a newline, avoid putting * If eating a newline, avoid putting
* the newline into the new character * the newline into the new character
@ -11725,9 +11717,7 @@ xxreadtoken(void)
tokpushback = 0; tokpushback = 0;
return lasttoken; return lasttoken;
} }
if (needprompt) { setprompt_if(needprompt, 2);
setprompt(2);
}
startlinno = g_parsefile->linno; startlinno = g_parsefile->linno;
for (;;) { /* until token or start of word found */ for (;;) { /* until token or start of word found */
c = pgetc_fast(); c = pgetc_fast();
@ -11744,8 +11734,7 @@ xxreadtoken(void)
break; /* return readtoken1(...) */ break; /* return readtoken1(...) */
} }
startlinno = ++g_parsefile->linno; startlinno = ++g_parsefile->linno;
if (doprompt) setprompt_if(doprompt, 2);
setprompt(2);
} else { } else {
const char *p; const char *p;
@ -11791,9 +11780,7 @@ xxreadtoken(void)
tokpushback = 0; tokpushback = 0;
return lasttoken; return lasttoken;
} }
if (needprompt) { setprompt_if(needprompt, 2);
setprompt(2);
}
startlinno = g_parsefile->linno; startlinno = g_parsefile->linno;
for (;;) { /* until token or start of word found */ for (;;) { /* until token or start of word found */
c = pgetc_fast(); c = pgetc_fast();
@ -11809,8 +11796,7 @@ xxreadtoken(void)
case '\\': case '\\':
if (pgetc() == '\n') { if (pgetc() == '\n') {
startlinno = ++g_parsefile->linno; startlinno = ++g_parsefile->linno;
if (doprompt) setprompt_if(doprompt, 2);
setprompt(2);
continue; continue;
} }
pungetc(); pungetc();
@ -11936,8 +11922,7 @@ parsecmd(int interact)
tokpushback = 0; tokpushback = 0;
doprompt = interact; doprompt = interact;
if (doprompt) setprompt_if(doprompt, doprompt);
setprompt(doprompt);
needprompt = 0; needprompt = 0;
t = readtoken(); t = readtoken();
if (t == TEOF) if (t == TEOF)
@ -11961,10 +11946,8 @@ parseheredoc(void)
heredoclist = NULL; heredoclist = NULL;
while (here) { while (here) {
if (needprompt) { setprompt_if(needprompt, 2);
setprompt(2); readtoken1(pgetc(), here->here->type == NHERE ? SQSYNTAX : DQSYNTAX,
}
readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX,
here->eofmark, here->striptabs); here->eofmark, here->striptabs);
n = stzalloc(sizeof(struct narg)); n = stzalloc(sizeof(struct narg));
n->narg.type = NARG; n->narg.type = NARG;