E23E minix/commands/ash/expand.c at R3.1.7 · dtytomi/minix · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History
1548 lines (1403 loc) · 29.9 KB

expand.c

File metadata and controls

1548 lines (1403 loc) · 29.9 KB
special = 1;
p = strchr(p, '=') + 1;
again: /* jump here after setting a variable with ${var=text} */
if (special) {
set = varisset(var, varflags & VSNUL);
val = NULL;
} else {
val = bltinlookup(var, 1);
if (val == NULL || ((varflags & VSNUL) && val[0] == '\0')) {
val = NULL;
set = 0;
} else
set = 1;
}
varlen = 0;
startloc = expdest - stackblock();
if (!set && uflag) {
switch (subtype) {
case VSNORMAL:
case VSTRIMLEFT:
case VSTRIMLEFTMAX:
case VSTRIMRIGHT:
case VSTRIMRIGHTMAX:
case VSLENGTH:
error("%.*s: parameter not set", (int)(p - var - 1),
var);
}
}
if (set && subtype != VSPLUS) {
/* insert the value of the variable */
if (special) {
varvalue(var, varflags & VSQUOTE, flag & EXP_FULL);
if (subtype == VSLENGTH) {
varlen = expdest - stackblock() - startloc;
STADJUST(-varlen, expdest);
}
} else {
char const *syntax = (varflags & VSQUOTE) ? DQSYNTAX
: BASESYNTAX;
if (subtype == VSLENGTH) {
for (;*val; val++)
varlen++;
}
else {
while (*val) {
if (quotes &&
syntax[(int)*val] == CCTL)
STPUTC(CTLESC, expdest);
STPUTC(*val++, expdest);
}
}
}
}
if (subtype == VSPLUS)
set = ! set;
easy = ((varflags & VSQUOTE) == 0 ||
(*var == '@' && shellparam.nparam != 1));
switch (subtype) {
case VSLENGTH:
expdest = cvtnum(varlen, expdest);
goto record;
case VSNORMAL:
if (!easy)
break;
record:
recordregion(startloc, expdest - stackblock(),
varflags & VSQUOTE);
break;
case VSPLUS:
case VSMINUS:
if (!set) {
argstr(p, flag);
break;
}
if (easy)
goto record;
break;
case VSTRIMLEFT:
case VSTRIMLEFTMAX:
case VSTRIMRIGHT:
case VSTRIMRIGHTMAX:
if (!set)
break;
/*
* Terminate the string and start recording the pattern
* right after it
*/
STPUTC('\0', expdest);
patloc = expdest - stackblock();
if (subevalvar(p, NULL, patloc, subtype,
startloc, varflags) == 0) {
int amount = (expdest - stackblock() - patloc) + 1;
STADJUST(-amount, expdest);
}
/* Remove any recorded regions beyond start of variable */
removerecordregions(startloc);
goto record;
case VSASSIGN:
case VSQUESTION:
if (!set) {
if (subevalvar(p, var, 0, subtype, startloc, varflags)) {
varflags &= ~VSNUL;
/*
* Remove any recorded regions beyond
* start of variable
*/
removerecordregions(startloc);
goto again;
}
break;
}
if (easy)
goto record;
break;
default:
abort();
}
if (subtype != VSNORMAL) { /* skip to end of alternative */
int nesting = 1;
for (;;) {
if ((c = *p++) == CTLESC)
p++;
else if (c == CTLBACKQ || c == (CTLBACKQ|CTLQUOTE)) {
if (set)
argbackq = argbackq->next;
} else if (c == CTLVAR) {
if ((*p++ & VSTYPE) != VSNORMAL)
nesting++;
} else if (c == CTLENDVAR) {
if (--nesting == 0)
break;
}
}
}
return p;
}
/*
* Test whether a specialized variable is set.
*/
STATIC int
varisset(char *name, int nulok)
{
if (*name == '!')
return backgndpid != -1;
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;
}
/*
* Add the value of a specialized variable to the stack string.
*/
STATIC void
varvalue(char *name, int quoted, int allow_split)
{
int num;
char *p;
int i;
extern int oexitstatus;
char sep;
char **ap;
char const *syntax;
#define STRTODEST(p) \
do {\
if (allow_split) { \
syntax = quoted? DQSYNTAX : BASESYNTAX; \
while (*p) { \
if (syntax[(int)*p] == CCTL) \
STPUTC(CTLESC, expdest); \
STPUTC(*p++, expdest); \
} \
} else \
while (*p) \
STPUTC(*p++, expdest); \
} while (0)
switch (*name) {
case '$':
num = rootpid;
goto numvar;
case '?':
num = oexitstatus;
goto numvar;
case '#':
num = shellparam.nparam;
goto numvar;
case '!':
num = backgndpid;
numvar:
expdest = cvtnum(num, expdest);
break;
case '-':
for (i = 0 ; i < NOPTS ; i++) {
if (optlist[i].val)
STPUTC(optlist[i].letter, expdest);
}
break;
case '@':
if (allow_split && quoted) {
for (ap = shellparam.p ; (p = *ap++) != NULL ; ) {
STRTODEST(p);
if (*ap)
STPUTC('\0', expdest);
}
break;
}
/* FALLTHROUGH */
case '*':
if (ifsset() != 0)
sep = ifsval()[0];
else
sep = ' ';
for (ap = shellparam.p ; (p = *ap++) != NULL ; ) {
STRTODEST(p);
if (*ap && sep)
STPUTC(sep, expdest);
}
break;
case '0':
p = arg0;
STRTODEST(p);
break;
default:
if (is_digit(*name)) {
num = atoi(name);
if (num > 0 && num <= shellparam.nparam) {
p = shellparam.p[num - 1];
STRTODEST(p);
}
}
break;
}
}
/*
* Record the the fact that we have to scan this region of the
* string for IFS characters.
*/
STATIC void
recordregion(int start, int end, int nulonly)
{
struct ifsregion *ifsp;
if (ifslastp == NULL) {
ifsp = &ifsfirst;
} else {
ifsp = (struct ifsregion *)ckmalloc(sizeof (struct ifsregion));
ifslastp->next = ifsp;
}
ifslastp = ifsp;
ifslastp->next = NULL;
ifslastp->begoff = start;
ifslastp->endoff = end;
ifslastp->nulonly = nulonly;
}
/*
* Break the argument string into pieces based upon IFS and add the
* strings to the argument list. The regions of the string to be
* searched for IFS characters have been stored by recordregion.
*/
STATIC void
ifsbreakup(char *string, struct arglist *arglist)
{
struct ifsregion *ifsp;
struct strlist *sp;
char *start;
char *p;
char *q;
char *ifs;
int ifsspc;
int nulonly;
start = string;
ifsspc = 0;
nulonly = 0;
if (ifslastp != NULL) {
ifsp = &ifsfirst;
do {
p = string + ifsp->begoff;
nulonly = ifsp->nulonly;
ifs = nulonly ? nullstr :
( ifsset() ? ifsval() : " \t\n" );
ifsspc = 0;
while (p < string + ifsp->endoff) {
q = p;
if (*p == CTLESC)
p++;
if (strchr(ifs, *p)) {
if (!nulonly)
ifsspc = (strchr(" \t\n", *p) != NULL);
/* Ignore IFS whitespace at start */
if (q == start && ifsspc) {
p++;
start = p;
continue;
}
*q = '\0';
sp = (struct strlist *)stalloc(sizeof *sp);
sp->text = start;
*arglist->lastp = sp;
arglist->lastp = &sp->next;
p++;
if (!nulonly) {
for (;;) {
0