Patch from Jonas Holmberg, adjusted by Robert Schwebel, and

later re-adjusted by me to fix msh backtick handling
This commit is contained in:
Eric Andersen 2003-03-14 16:05:59 +00:00
parent d2b8118650
commit 737f5fb890

View File

@ -5,6 +5,11 @@
* This version of the Minix shell was adapted for use in busybox * This version of the Minix shell was adapted for use in busybox
* by Erik Andersen <andersee@debian.org> * by Erik Andersen <andersee@debian.org>
* *
* - backtick expansion did not work properly
* Jonas Holmberg <jonas.holmberg@axis.com>
* Robert Schwebel <r.schwebel@pengutronix.de>
* Erik Andersen <andersee@debian.org>
*
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or * the Free Software Foundation; either version 2 of the License, or
@ -222,7 +227,7 @@ static char *space (int n );
static char *strsave (char *s, int a ); static char *strsave (char *s, int a );
static char *evalstr (char *cp, int f ); static char *evalstr (char *cp, int f );
static char *putn (int n ); static char *putn (int n );
static char *itoa (unsigned u, int n ); static char *itoa (int n );
static char *unquote (char *as ); static char *unquote (char *as );
static struct var *lookup (char *n ); static struct var *lookup (char *n );
static int rlookup (char *n ); static int rlookup (char *n );
@ -664,7 +669,6 @@ static int heedint =1;
static struct env e ={line, iostack, iostack-1, (xint *)NULL, FDBASE, (struct env *)NULL}; static struct env e ={line, iostack, iostack-1, (xint *)NULL, FDBASE, (struct env *)NULL};
static void (*qflag)(int) = SIG_IGN; static void (*qflag)(int) = SIG_IGN;
static char shellname[] = "/bin/sh"; static char shellname[] = "/bin/sh";
static char search[] = ":/bin:/usr/bin";
static int startl; static int startl;
static int peeksym; static int peeksym;
static int nlseen; static int nlseen;
@ -687,7 +691,6 @@ static void * brkaddr;
static char * current_prompt; static char * current_prompt;
#endif #endif
/* -------- sh.c -------- */ /* -------- sh.c -------- */
/* /*
* shell * shell
@ -722,11 +725,15 @@ extern int msh_main(int argc, char **argv)
setval(homedir, "/"); setval(homedir, "/");
export(homedir); export(homedir);
setval(lookup("$"), itoa(getpid(), 5)); setval(lookup("$"), putn(getpid()));
path = lookup("PATH"); path = lookup("PATH");
if (path->value == null) if (path->value == null) {
setval(path, search); if (geteuid() == 0)
setval(path, "/sbin:/bin:/usr/sbin:/usr/bin");
else
setval(path, "/bin:/usr/bin");
}
export(path); export(path);
ifs = lookup("IFS"); ifs = lookup("IFS");
@ -858,7 +865,7 @@ setdash()
cp = m; cp = m;
for (c='a'; c<='z'; c++) for (c='a'; c<='z'; c++)
if (flag[c]) if (flag[(int)c])
*cp++ = c; *cp++ = c;
*cp = 0; *cp = 0;
setval(lookup("-"), m); setval(lookup("-"), m);
@ -1047,32 +1054,16 @@ static char *
putn(n) putn(n)
register int n; register int n;
{ {
return(itoa(n, -1)); return(itoa(n));
} }
static char * static char *
itoa(u, n) itoa(n)
register unsigned u; register int n;
int n;
{ {
register char *cp;
static char s[20]; static char s[20];
int m; snprintf(s, sizeof(s), "%u", n);
return(s);
m = 0;
if (n < 0 && (int) u < 0) {
m++;
u = -u;
}
cp = s+sizeof(s);
*--cp = 0;
do {
*--cp = u%10 + '0';
u /= 10;
} while (--n > 0 || u);
if (m)
*--cp = '-';
return(cp);
} }
static void static void
@ -2326,6 +2317,9 @@ int act;
switch(t->type) { switch(t->type) {
case TPAREN: case TPAREN:
rv = execute(t->left, pin, pout, 0);
break;
case TCOM: case TCOM:
{ {
int child; int child;
@ -2877,12 +2871,12 @@ char *c, **v, **envp;
*v = e.linep; *v = e.linep;
tp = *--v; tp = *--v;
*v = e.linep; *v = e.linep;
execve("/bin/sh", v, envp); execve(shellname, v, envp);
*v = tp; *v = tp;
return("no Shell"); return("no Shell");
case ENOMEM: case ENOMEM:
return("program too big"); return((char*)memory_exhausted);
case E2BIG: case E2BIG:
return("argument list too long"); return("argument list too long");
@ -3643,7 +3637,7 @@ loop:
*/ */
static int static int
subgetc(ec, quoted) subgetc(ec, quoted)
register int ec; register char ec;
int quoted; int quoted;
{ {
register char c; register char c;
@ -3770,59 +3764,154 @@ int quoted;
/* /*
* Run the command in `...` and read its output. * Run the command in `...` and read its output.
*/ */
static int static int
grave(quoted) grave(quoted)
int quoted; int quoted;
{ {
register int i;
char *cp; char *cp;
register int i;
int j;
int pf[2]; int pf[2];
static char child_cmd[LINELIM];
char *src;
char *dest;
int count;
int ignore;
int ignore_once;
char *argument_list[4];
#if __GNUC__ #if __GNUC__
/* Avoid longjmp clobbering */ /* Avoid longjmp clobbering */
(void) &cp; (void) &cp;
#endif #endif
for (cp = e.iop->argp->aword; *cp != '`'; cp++) for (cp = e.iop->argp->aword; *cp != '`'; cp++)
if (*cp == 0) { if (*cp == 0) {
err("no closing `"); err("no closing `");
return(0); return(0);
} }
/* string copy with dollar expansion */
src = e.iop->argp->aword;
dest = child_cmd;
count = 0;
ignore = 0;
ignore_once = 0;
while ((*src != '`') && (count < LINELIM)) {
if (*src == '\'')
ignore = !ignore;
if (*src == '\\')
ignore_once = 1;
if (*src == '$' && !ignore && !ignore_once) {
struct var *vp;
char var_name[LINELIM];
char alt_value[LINELIM];
int var_index = 0;
int alt_index = 0;
char operator = 0;
int braces = 0;
char *value;
src++;
if (*src == '{') {
braces = 1;
src++;
}
var_name[var_index++] = *src++;
while (isalnum(*src))
var_name[var_index++] = *src++;
var_name[var_index] = 0;
if (braces) {
switch (*src) {
case '}':
break;
case '-':
case '=':
case '+':
case '?':
operator = *src;
break;
default:
err("unclosed ${\n");
return(0);
}
if (operator) {
src++;
while (*src && (*src != '}')) {
alt_value[alt_index++] = *src++;
}
alt_value[alt_index] = 0;
if (*src != '}') {
err("unclosed ${\n");
return(0);
}
}
src++;
}
vp = lookup(var_name);
if (vp->value != null)
value = (operator == '+')? alt_value : vp->value;
else if (operator == '?') {
err(alt_value);
return(0);
} else if (alt_index && (operator != '+')) {
value = alt_value;
if (operator == '=')
setval(vp, value);
} else
continue;
while (*value && (count < LINELIM)) {
*dest++ = *value++;
count++;
}
} else {
*dest++ = *src++;
count++;
ignore_once = 0;
}
}
*dest = '\0';
if (openpipe(pf) < 0) if (openpipe(pf) < 0)
return(0); return(0);
if ((i = vfork()) == -1) { while ((i = vfork()) == -1 && errno == EAGAIN)
;
if (i < 0) {
closepipe(pf); closepipe(pf);
err("try again"); err((char*)memory_exhausted);
return(0); return(0);
} }
if (i != 0) { if (i != 0) {
waitpid(i, NULL, 0);
e.iop->argp->aword = ++cp; e.iop->argp->aword = ++cp;
close(pf[1]); close(pf[1]);
PUSHIO(afile, remap(pf[0]), (int(*)(struct ioarg *))((quoted)? qgravechar: gravechar)); PUSHIO(afile, remap(pf[0]), (int(*)(struct ioarg *))((quoted)? qgravechar: gravechar));
return(1); return(1);
} }
*cp = 0;
/* allow trapped signals */ /* allow trapped signals */
for (i=0; i<=_NSIG; i++) /* XXX - Maybe this signal stuff should go as well? */
if (ourtrap[i] && signal(i, SIG_IGN) != SIG_IGN) for (j=0; j<=_NSIG; j++)
signal(i, SIG_DFL); if (ourtrap[j] && signal(j, SIG_IGN) != SIG_IGN)
signal(j, SIG_DFL);
dup2(pf[1], 1); dup2(pf[1], 1);
closepipe(pf); closepipe(pf);
flag['e'] = 0;
flag['v'] = 0; argument_list[0] = shellname;
flag['n'] = 0; argument_list[1] = "-c";
cp = strsave(e.iop->argp->aword, 0); argument_list[2] = child_cmd;
areanum = 1; argument_list[3] = 0;
freehere(areanum);
freearea(areanum); /* free old space */ prs(rexecve(argument_list[0], argument_list, makenv()));
e.oenv = NULL; _exit(1);
e.iop = (e.iobase = iostack) - 1;
unquote(cp);
interactive = 0;
PUSHIO(aword, cp, nlchar);
onecommand();
exit(1);
} }
static char * static char *
unquote(as) unquote(as)
register char *as; register char *as;
@ -4194,7 +4283,7 @@ static int my_getc( int ec)
return(c); return(c);
} }
c = readc(); c = readc();
if (ec != '\'' && e.iop->task != XGRAVE) { if ((ec != '\'') && (ec != '`') && (e.iop->task != XGRAVE)) {
if(c == '\\') { if(c == '\\') {
c = readc(); c = readc();
if (c == '\n' && ec != '\"') if (c == '\n' && ec != '\"')
@ -4457,7 +4546,7 @@ register struct ioarg *ap;
} }
#ifdef CONFIG_FEATURE_COMMAND_EDITING #ifdef CONFIG_FEATURE_COMMAND_EDITING
if (interactive) { if (interactive && isatty(ap->afile)) {
static char mycommand[BUFSIZ]; static char mycommand[BUFSIZ];
static int position = 0, size = 0; static int position = 0, size = 0;
@ -4568,7 +4657,7 @@ static void
prn(u) prn(u)
unsigned u; unsigned u;
{ {
prs(itoa(u, 0)); prs(itoa(u));
} }
static void static void