8000 minix/commands/ash/expand.c at v3.1.4 · lx00700/minix · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History
executable file
·
1548 lines (1403 loc) · 29.9 KB

expand.c

File metadata and controls

executable file
·
1548 lines (1403 loc) · 29.9 KB
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