Vodz last_patch_121, syncing with dash_0.4.19, reduce code size.

This commit is contained in:
Glenn L McGrath
2004-01-13 10:19:37 +00:00
parent dfb6211df3
commit 76620620ec

View File

@ -1214,7 +1214,7 @@ static char *nodesavestr(char *);
static void evalstring(char *, int); static void evalstring(char *);
union node; /* BLETCH for ansi C */ union node; /* BLETCH for ansi C */
static void evaltree(union node *, int); static void evaltree(union node *, int);
static void evalbackcmd(union node *, struct backcmd *); static void evalbackcmd(union node *, struct backcmd *);
@ -2704,7 +2704,7 @@ evalcmd(int argc, char **argv)
STPUTC('\0', concat); STPUTC('\0', concat);
p = grabstackstr(concat); p = grabstackstr(concat);
} }
evalstring(p, EV_TESTED); evalstring(p);
} }
return exitstatus; return exitstatus;
} }
@ -2715,7 +2715,7 @@ evalcmd(int argc, char **argv)
*/ */
static void static void
evalstring(char *s, int flag) evalstring(char *s)
{ {
union node *n; union node *n;
struct stackmark smark; struct stackmark smark;
@ -2724,7 +2724,7 @@ evalstring(char *s, int flag)
setinputstring(s); setinputstring(s);
while ((n = parsecmd(0)) != NEOF) { while ((n = parsecmd(0)) != NEOF) {
evaltree(n, flag); evaltree(n, 0);
popstackmark(&smark); popstackmark(&smark);
if (evalskip) if (evalskip)
break; break;
@ -4496,10 +4496,9 @@ static char *exptilde(char *, char *, int);
static void expbackq(union node *, int, int); static void expbackq(union node *, int, int);
static const char *subevalvar(char *, char *, int, int, int, int, int); static const char *subevalvar(char *, char *, int, int, int, int, int);
static char *evalvar(char *, int); static char *evalvar(char *, int);
static int varisset(char *, int);
static void strtodest(const char *, int, int); static void strtodest(const char *, int, int);
static void memtodest(const char *p, size_t len, int syntax, int quotes); static void memtodest(const char *p, size_t len, int syntax, int quotes);
static void varvalue(char *, int, int); static ssize_t varvalue(char *, int, int);
static void recordregion(int, int, int); static void recordregion(int, int, int);
static void removerecordregions(int); static void removerecordregions(int);
static void ifsbreakup(char *, struct arglist *); static void ifsbreakup(char *, struct arglist *);
@ -5112,9 +5111,8 @@ evalvar(char *p, int flag)
char *var; char *var;
int patloc; int patloc;
int c; int c;
int set;
int startloc; int startloc;
size_t varlen; ssize_t varlen;
int easy; int easy;
int quotes; int quotes;
int quoted; int quoted;
@ -5125,48 +5123,22 @@ evalvar(char *p, int flag)
quoted = varflags & VSQUOTE; quoted = varflags & VSQUOTE;
var = p; var = p;
easy = (!quoted || (*var == '@' && shellparam.nparam)); easy = (!quoted || (*var == '@' && shellparam.nparam));
varlen = 0;
startloc = expdest - (char *)stackblock(); startloc = expdest - (char *)stackblock();
p = strchr(p, '=') + 1; p = strchr(p, '=') + 1;
if (!is_name(*var)) {
set = varisset(var, varflags & VSNUL);
set--;
if (subtype == VSPLUS)
goto vsplus;
if (++set) {
varvalue(var, quoted, flag);
if (subtype == VSLENGTH) {
varlen =
expdest - (char *)stackblock() -
startloc;
STADJUST(-varlen, expdest);
goto vslen;
}
}
} else {
const char *val;
again: again:
/* jump here after setting a variable with ${var=text} */ varlen = varvalue(var, varflags, flag);
val = lookupvar(var); if (varflags & VSNUL)
set = !val || ((varflags & VSNUL) && !*val); varlen--;
if (subtype == VSPLUS)
goto vsplus;
if (--set) {
varlen = strlen(val);
if (subtype == VSLENGTH)
goto vslen;
memtodest(
val, varlen, quoted ? DQSYNTAX : BASESYNTAX,
quotes
);
}
}
if (subtype == VSPLUS) {
varlen = -1 - varlen;
goto vsplus;
}
if (subtype == VSMINUS) { if (subtype == VSMINUS) {
vsplus: vsplus:
if (!set) { if (varlen < 0) {
argstr( argstr(
p, flag | EXP_TILDE | p, flag | EXP_TILDE |
(quoted ? EXP_QWORD : EXP_WORD) (quoted ? EXP_QWORD : EXP_WORD)
@ -5179,7 +5151,7 @@ vsplus:
} }
if (subtype == VSASSIGN || subtype == VSQUESTION) { if (subtype == VSASSIGN || subtype == VSQUESTION) {
if (!set) { if (varlen < 0) {
if (subevalvar(p, var, 0, subtype, startloc, if (subevalvar(p, var, 0, subtype, startloc,
varflags, 0)) { varflags, 0)) {
varflags &= ~VSNUL; varflags &= ~VSNUL;
@ -5197,12 +5169,11 @@ vsplus:
goto end; goto end;
} }
if (!set && uflag) if (varlen < 0 && uflag)
varunset(p, var, 0, 0); varunset(p, var, 0, 0);
if (subtype == VSLENGTH) { if (subtype == VSLENGTH) {
vslen: cvtnum(varlen > 0 ? varlen : 0);
cvtnum(varlen);
goto record; goto record;
} }
@ -5226,7 +5197,7 @@ record:
} }
#endif #endif
if (set) { if (varlen >= 0) {
/* /*
* Terminate the string and start recording the pattern * Terminate the string and start recording the pattern
* right after it * right after it
@ -5252,7 +5223,7 @@ end:
if ((c = *p++) == CTLESC) if ((c = *p++) == CTLESC)
p++; p++;
else if (c == CTLBACKQ || c == (CTLBACKQ|CTLQUOTE)) { else if (c == CTLBACKQ || c == (CTLBACKQ|CTLQUOTE)) {
if (set) if (varlen >= 0)
argbackq = argbackq->next; argbackq = argbackq->next;
} else if (c == CTLVAR) { } else if (c == CTLVAR) {
if ((*p++ & VSTYPE) != VSNORMAL) if ((*p++ & VSTYPE) != VSNORMAL)
@ -5267,47 +5238,6 @@ end:
} }
/*
* Test whether a specialized variable is set.
*/
static int
varisset(char *name, int nulok)
{
if (*name == '!')
return backgndpid != 0;
else if (*name == '@' || *name == '*') {
if (*shellparam.p == NULL)
return 0;
if (nulok) {
char **av;
for (av = shellparam.p; *av; av++)
if (**av != '\0')
return 1;
return 0;
}
} else if (is_digit(*name)) {
char *ap;
int num = atoi(name);
if (num > shellparam.nparam)
return 0;
if (num == 0)
ap = arg0;
else
ap = shellparam.p[num - 1];
if (nulok && (ap == NULL || *ap == '\0'))
return 0;
}
return 1;
}
/* /*
* Put a string on the stack. * Put a string on the stack.
*/ */
@ -5342,19 +5272,24 @@ strtodest(const char *p, int syntax, int quotes)
* Add the value of a specialized variable to the stack string. * Add the value of a specialized variable to the stack string.
*/ */
static void static ssize_t
varvalue(char *name, int quoted, int flags) varvalue(char *name, int varflags, int flags)
{ {
int num; int num;
char *p; char *p;
int i; int i;
int sep; int sep = 0;
int sepq = 0; int sepq = 0;
ssize_t len = 0;
char **ap; char **ap;
int syntax; int syntax;
int allow_split = flags & EXP_FULL; int quoted = varflags & VSQUOTE;
int subtype = varflags & VSTYPE;
int quotes = flags & (EXP_FULL | EXP_CASE); int quotes = flags & (EXP_FULL | EXP_CASE);
if (quoted && (flags & EXP_FULL))
sep = 1 << CHAR_BIT;
syntax = quoted ? DQSYNTAX : BASESYNTAX; syntax = quoted ? DQSYNTAX : BASESYNTAX;
switch (*name) { switch (*name) {
case '$': case '$':
@ -5368,48 +5303,86 @@ varvalue(char *name, int quoted, int flags)
goto numvar; goto numvar;
case '!': case '!':
num = backgndpid; num = backgndpid;
if (num == 0)
return -1;
numvar: numvar:
cvtnum(num); len = cvtnum(num);
break; break;
case '-': case '-':
for (i = 0 ; i < NOPTS ; i++) { p = makestrspace(NOPTS, expdest);
if (optlist[i]) for (i = NOPTS - 1; i >= 0; i--) {
STPUTC(optletters(i), expdest); if (optlist[i]) {
USTPUTC(optletters(i), p);
len++;
}
} }
expdest = p;
break; break;
case '@': case '@':
if (allow_split && quoted) { if (sep)
sep = 1 << CHAR_BIT;
goto param; goto param;
}
/* fall through */ /* fall through */
case '*': case '*':
sep = ifsset() ? ifsval()[0] : ' '; sep = ifsset() ? ifsval()[0] : ' ';
if (quotes) { if (quotes && (SIT(sep, syntax) == CCTL || SIT(sep, syntax) == CBACK))
sepq = (SIT(sep, syntax) == CCTL) || (SIT(sep, syntax) == CBACK); sepq = 1;
}
param: param:
for (ap = shellparam.p ; (p = *ap++) != NULL ; ) { if (!(ap = shellparam.p))
strtodest(p, syntax, quotes); return -1;
if (*ap && sep) { while ((p = *ap++)) {
p = expdest; size_t partlen;
partlen = strlen(p);
len += partlen;
if (len > partlen && sep) {
char *q;
len++;
if (subtype == VSPLUS || subtype == VSLENGTH) {
continue;
}
q = expdest;
if (sepq) if (sepq)
STPUTC(CTLESC, p); STPUTC(CTLESC, q);
STPUTC(sep, p); STPUTC(sep, q);
expdest = p; expdest = q;
} }
if (!(subtype == VSPLUS || subtype == VSLENGTH))
memtodest(p, partlen, syntax, quotes);
} }
break; return len;
case '0': case '0':
strtodest(arg0, syntax, quotes); case '1':
break; case '2':
default: case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
num = atoi(name); num = atoi(name);
if (num > 0 && num <= shellparam.nparam) { if (num < 0 || num > shellparam.nparam)
strtodest(shellparam.p[num - 1], syntax, quotes); return -1;
} p = num ? shellparam.p[num - 1] : arg0;
break; goto value;
default:
p = lookupvar(name);
value:
if (!p)
return -1;
len = strlen(p);
if (!(subtype == VSPLUS || subtype == VSLENGTH))
memtodest(p, len, syntax, quotes);
return len;
} }
if (subtype == VSPLUS || subtype == VSLENGTH)
STADJUST(-len, expdest);
return len;
} }
@ -7946,7 +7919,7 @@ state2:
state3: state3:
state = 4; state = 4;
if (minusc) if (minusc)
evalstring(minusc, 0); evalstring(minusc);
if (sflag || minusc == NULL) { if (sflag || minusc == NULL) {
#ifdef CONFIG_FEATURE_COMMAND_SAVEHISTORY #ifdef CONFIG_FEATURE_COMMAND_SAVEHISTORY
@ -7988,8 +7961,8 @@ cmdloop(int top)
int numeof = 0; int numeof = 0;
TRACE(("cmdloop(%d) called\n", top)); TRACE(("cmdloop(%d) called\n", top));
setstackmark(&smark);
for (;;) { for (;;) {
setstackmark(&smark);
if (pendingsigs) if (pendingsigs)
dotrap(); dotrap();
#if JOBS #if JOBS
@ -8020,13 +7993,11 @@ cmdloop(int top)
evaltree(n, 0); evaltree(n, 0);
} }
popstackmark(&smark); popstackmark(&smark);
setstackmark(&smark); if (evalskip) {
if (evalskip == SKIPFILE) {
evalskip = 0; evalskip = 0;
break; break;
} }
} }
popstackmark(&smark);
} }
@ -11778,7 +11749,7 @@ dotrap(void)
p = trap[p - q + 1]; p = trap[p - q + 1];
if (!p) if (!p)
continue; continue;
evalstring(p, 0); evalstring(p);
exitstatus = savestatus; exitstatus = savestatus;
} }
} }
@ -11870,7 +11841,7 @@ exitshell(void)
handler = &loc; handler = &loc;
if ((p = trap[0]) != NULL && *p != '\0') { if ((p = trap[0]) != NULL && *p != '\0') {
trap[0] = NULL; trap[0] = NULL;
evalstring(p, 0); evalstring(p);
} }
flushall(); flushall();
#ifdef CONFIG_FEATURE_COMMAND_SAVEHISTORY #ifdef CONFIG_FEATURE_COMMAND_SAVEHISTORY
@ -12497,7 +12468,7 @@ letcmd(int argc, char **argv)
#undef rflag #undef rflag
#ifdef __GLIBC__ #ifdef __GLIBC__
#if !defined(__GLIBC__) || __GLIBC__ == 2 && __GLIBC_MINOR__ < 1 #if __GLIBC__ == 2 && __GLIBC_MINOR__ < 1
typedef enum __rlimit_resource rlim_t; typedef enum __rlimit_resource rlim_t;
#endif #endif
#endif #endif
@ -12694,34 +12665,52 @@ static const struct limits limits[] = {
{ "locked memory(kbytes)", RLIMIT_MEMLOCK, 1024, 'l' }, { "locked memory(kbytes)", RLIMIT_MEMLOCK, 1024, 'l' },
#endif #endif
#ifdef RLIMIT_NPROC #ifdef RLIMIT_NPROC
{ "process(processes)", RLIMIT_NPROC, 1, 'p' }, { "process", RLIMIT_NPROC, 1, 'p' },
#endif #endif
#ifdef RLIMIT_NOFILE #ifdef RLIMIT_NOFILE
{ "nofiles(descriptors)", RLIMIT_NOFILE, 1, 'n' }, { "nofiles", RLIMIT_NOFILE, 1, 'n' },
#endif #endif
#ifdef RLIMIT_VMEM #ifdef RLIMIT_AS
{ "vmemory(kbytes)", RLIMIT_VMEM, 1024, 'v' }, { "vmemory(kbytes)", RLIMIT_AS, 1024, 'v' },
#endif #endif
#ifdef RLIMIT_SWAP #ifdef RLIMIT_LOCKS
{ "swap(kbytes)", RLIMIT_SWAP, 1024, 'w' }, { "locks", RLIMIT_LOCKS, 1, 'w' },
#endif #endif
{ (char *) 0, 0, 0, '\0' } { (char *) 0, 0, 0, '\0' }
}; };
enum limtype { SOFT = 0x1, HARD = 0x2 };
static void printlim(enum limtype how, const struct rlimit *limit,
const struct limits *l)
{
rlim_t val;
val = limit->rlim_max;
if (how & SOFT)
val = limit->rlim_cur;
if (val == RLIM_INFINITY)
out1fmt("unlimited\n");
else {
val /= l->factor;
out1fmt("%lld\n", (long long) val);
}
}
int int
ulimitcmd(int argc, char **argv) ulimitcmd(int argc, char **argv)
{ {
int c; int c;
rlim_t val = 0; rlim_t val = 0;
enum { SOFT = 0x1, HARD = 0x2 } enum limtype how = SOFT | HARD;
how = SOFT | HARD;
const struct limits *l; const struct limits *l;
int set, all = 0; int set, all = 0;
int optc, what; int optc, what;
struct rlimit limit; struct rlimit limit;
what = 'f'; what = 'f';
while ((optc = nextopt("HSatfdsmcnpl")) != '\0') while ((optc = nextopt("HSatfdsmcnplvw")) != '\0')
switch (optc) { switch (optc) {
case 'H': case 'H':
how = HARD; how = HARD;
@ -12736,10 +12725,8 @@ ulimitcmd(int argc, char **argv)
what = optc; what = optc;
} }
for (l = limits; l->name && l->option != what; l++) for (l = limits; l->option != what; l++)
; ;
if (!l->name)
error("internal error (%c)", what);
set = *argptr ? 1 : 0; set = *argptr ? 1 : 0;
if (set) { if (set) {
@ -12766,19 +12753,8 @@ ulimitcmd(int argc, char **argv)
if (all) { if (all) {
for (l = limits; l->name; l++) { for (l = limits; l->name; l++) {
getrlimit(l->cmd, &limit); getrlimit(l->cmd, &limit);
if (how & SOFT)
val = limit.rlim_cur;
else if (how & HARD)
val = limit.rlim_max;
out1fmt("%-20s ", l->name); out1fmt("%-20s ", l->name);
if (val == RLIM_INFINITY) printlim(how, &limit, l);
out1fmt("unlimited\n");
else
{
val /= l->factor;
out1fmt("%lld\n", (long long) val);
}
} }
return 0; return 0;
} }
@ -12792,18 +12768,7 @@ ulimitcmd(int argc, char **argv)
if (setrlimit(l->cmd, &limit) < 0) if (setrlimit(l->cmd, &limit) < 0)
error("error setting limit (%m)"); error("error setting limit (%m)");
} else { } else {
if (how & SOFT) printlim(how, &limit, l);
val = limit.rlim_cur;
else if (how & HARD)
val = limit.rlim_max;
if (val == RLIM_INFINITY)
out1fmt("unlimited\n");
else
{
val /= l->factor;
out1fmt("%lld\n", (long long) val);
}
} }
return 0; return 0;
} }