sh: Replace some macros and repeated code in expand.c with functions.

No functional change is intended, but the binary is about 1K smaller on
i386.
This commit is contained in:
Jilles Tjoelker 2010-12-11 22:13:29 +00:00
parent 41f15bbbd9
commit f7dea8517f
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=216384

View File

@ -137,6 +137,18 @@ expandhere(union node *arg, int fd)
xwrite(fd, stackblock(), expdest - stackblock());
}
static char *
stputs_quotes(const char *data, const char *syntax, char *p)
{
while (*data) {
CHECKSTRSPACE(2, p);
if (syntax[(int)*data] == CCTL)
USTPUTC(CTLESC, p);
USTPUTC(*data++, p);
}
return (p);
}
#define STPUTS_QUOTES(data, syntax, p) p = stputs_quotes((data), syntax, p)
/*
* Perform expansions on an argument, placing the resulting list of arguments
@ -334,11 +346,10 @@ exptilde(char *p, int flag)
if (*home == '\0')
goto lose;
*p = c;
while ((c = *home++) != '\0') {
if (quotes && SQSYNTAX[(int)c] == CCTL)
STPUTC(CTLESC, expdest);
STPUTC(c, expdest);
}
if (quotes)
STPUTS_QUOTES(home, SQSYNTAX, expdest);
else
STPUTS(home, expdest);
return (p);
lose:
*p = c;
@ -723,12 +734,10 @@ evalvar(char *p, int flag)
varlen++;
}
else {
while (*val) {
if (quotes &&
syntax[(int)*val] == CCTL)
STPUTC(CTLESC, expdest);
STPUTC(*val++, expdest);
}
if (quotes)
STPUTS_QUOTES(val, syntax, expdest);
else
STPUTS(val, expdest);
}
}
@ -877,7 +886,14 @@ varisset(char *name, int nulok)
return 1;
}
static void
strtodest(const char *p, int flag, int subtype, int quoted)
{
if (flag & (EXP_FULL | EXP_CASE) && subtype != VSLENGTH)
STPUTS_QUOTES(p, quoted ? DQSYNTAX : BASESYNTAX, expdest);
else
STPUTS(p, expdest);
}
/*
* Add the value of a specialized variable to the stack string.
@ -891,21 +907,6 @@ varvalue(char *name, int quoted, int subtype, int flag)
int i;
char sep;
char **ap;
char const *syntax;
#define STRTODEST(p) \
do {\
if (flag & (EXP_FULL | EXP_CASE) && subtype != VSLENGTH) { \
syntax = quoted? DQSYNTAX : BASESYNTAX; \
while (*p) { \
if (syntax[(int)*p] == CCTL) \
STPUTC(CTLESC, expdest); \
STPUTC(*p++, expdest); \
} \
} else \
STPUTS(p, expdest); \
} while (0)
switch (*name) {
case '$':
@ -931,7 +932,7 @@ varvalue(char *name, int quoted, int subtype, int flag)
case '@':
if (flag & EXP_FULL && quoted) {
for (ap = shellparam.p ; (p = *ap++) != NULL ; ) {
STRTODEST(p);
strtodest(p, flag, subtype, quoted);
if (*ap)
STPUTC('\0', expdest);
}
@ -944,21 +945,21 @@ varvalue(char *name, int quoted, int subtype, int flag)
else
sep = ' ';
for (ap = shellparam.p ; (p = *ap++) != NULL ; ) {
STRTODEST(p);
strtodest(p, flag, subtype, quoted);
if (*ap && sep)
STPUTC(sep, expdest);
}
break;
case '0':
p = arg0;
STRTODEST(p);
strtodest(p, flag, subtype, quoted);
break;
default:
if (is_digit(*name)) {
num = atoi(name);
if (num > 0 && num <= shellparam.nparam) {
p = shellparam.p[num - 1];
STRTODEST(p);
strtodest(p, flag, subtype, quoted);
}
}
break;