[BusyBox] msh scripting

Robert Schwebel robert at schwebel.de
Tue Mar 11 05:08:04 UTC 2003


Jonas, 

On Tue, Mar 11, 2003 at 10:21:41AM +0100, Jonas Holmberg wrote:
> <ftp://ftp.axis.com/pub/users/jonashg/minix-shell.tgz>

I had a look at the differences between the original minix shell and
your version; most changes seem to have been included in busybox
already, with exception of the grave() function. 

I tried to replace it with your version but it still does not work, see
attachment. When I run "a=`echo foo`" I get back a "not found", and I've
no idea where it comes from. strace lets me think that this is a vfork
problem: 

----------8<----------
ioctl(0, TCSETS, {B38400 opost isig icanon echo ...}) = 0
ioctl(0, SNDCTL_TMR_TIMEBASE, {B38400 opost isig icanon echo ...}) = 0
rt_sigaction(SIGWINCH, {SIG_DFL}, {0x80795b0, [WINCH],
SA_RESTORER|SA_RESTART, 0x4003f9d8}, 8) = 0
ioctl(0, SNDCTL_TMR_TIMEBASE, {B38400 opost isig icanon echo ...}) = 0
pipe([3, 4])                            = 0
vfork(not found)                                 = 7807
--- SIGCHLD (Child exited) @ 0 (0) ---
wait4(7807, NULL, 0, NULL)              = 7807
close(4)                                = 0
----------8<----------

My current patch is attached if somebody wants to try it out. Any help
would be, well, helpful :-)  

Robert
-- 
 Dipl.-Ing. Robert Schwebel | http://www.pengutronix.de
 Pengutronix - Linux Solutions for Science and Industry
   Braunschweiger Str. 79,  31134 Hildesheim, Germany
   Handelsregister:  Amtsgericht Hildesheim, HRA 2686
    Phone: +49-5121-28619-0 |  Fax: +49-5121-28619-4
-------------- next part --------------
--- msh-new.c	Tue Mar 11 13:02:57 2003
+++ msh-orig.c	Tue Mar 11 12:29:45 2003
@@ -43,7 +43,6 @@
 #include "cmdedit.h"
 #include "busybox.h"
 
-#define grave(foo) axis_grave(foo)
 
 /* -------- sh.h -------- */
 /*
@@ -251,10 +250,10 @@
 /*
  * error handling
  */
-static void leave (void); 	/* abort shell (or fail in subshell) */
-static void fail (void);	/* fail but return to process next command */
+static void leave (void); /* abort shell (or fail in subshell) */
+static void fail (void);	 /* fail but return to process next command */
 static void warn (char *s );
-static void sig (int i );	/* default signal handler */
+static void sig (int i );	 /* default signal handler */
 
 
 
@@ -537,7 +536,6 @@
 static void glob1 (char *base, char *lim );
 static void glob2 (char *i, char *j );
 static void glob3 (char *i, char *j, char *k );
-static char * memcopy (char *ato, char *from, int nb );
 static void readhere (char **name, char *s, int ec );
 static void pushio (struct ioarg *argp, int (*f)(struct ioarg *));
 static int xxchar(struct ioarg *ap );
@@ -689,7 +687,6 @@
 static char * current_prompt;
 #endif
 
-static char *subshellname;
 
 /* -------- sh.c -------- */
 /*
@@ -715,36 +712,6 @@
 	closeall();
 	areanum = 1;
 
-//#if AXIS
-	if ((argv[0][0] == '-') && (argv[0][1] != '\0')) {
-		if (!(subshellname = space(strlen(argv[0]))))
-			err("out of memory\n");
-		else {
-			memcopy(subshellname, argv[0] + 1, strlen(argv[0]));
-		}
-	} else {
-		if ((argv[0][0] != '/') && strchr(argv[0], '/')) {
-			char *cwd = getcwd(NULL, 0);
-			if (!(subshellname = space(strlen(cwd) + 1 +
-					           strlen(argv[0]) + 1)))
-				err("out of memory\n");
-			else {
-				memcopy(subshellname, cwd, strlen(cwd));
-				subshellname[strlen(cwd)] = '/';
-				memcopy(subshellname + strlen(cwd) + 1,
-					argv[0], strlen(argv[0]) + 1);
-				free(cwd);
-			}
-			
-		} else {
-		if (!(subshellname = space(strlen(argv[0]) + 1)))
-			err("out of memory\n");
-		else
-			memcopy(subshellname, argv[0], strlen(argv[0]) + 1);
-		}
-	}
-//#endif
-	
 	shell = lookup("SHELL");
 	if (shell->value == null)
 		setval(shell, shellname);
@@ -755,15 +722,11 @@
 		setval(homedir, "/");
 	export(homedir);
 
-	setval(lookup("$"), putn(getpid()));
+	setval(lookup("$"), itoa(getpid(), 5));
 
 	path = lookup("PATH");
-	if (path->value == null) {
-		if (geteuid() == 0)
-			setval(path, "/sbin:/bin:/usr/sbin:/usr/bin");
-		else
-			setval(path, "/bin:/usr/bin");
-	}
+	if (path->value == null)
+		setval(path, search);
 	export(path);
 
 	ifs = lookup("IFS");
@@ -895,7 +858,7 @@
 
 	cp = m;
 	for (c='a'; c<='z'; c++)
-		if (flag[(int)c])
+		if (flag[c])
 			*cp++ = c;
 	*cp = 0;
 	setval(lookup("-"), m);
@@ -1264,7 +1227,6 @@
 	if (vp->status & RONLY) {
 		for (xp = vp->name; *xp && *xp != '=';)
 			putc(*xp++, stderr);
-// AXIS: 		myputc(...)
 		err(" is read-only");
 		return;
 	}
@@ -2364,9 +2326,6 @@
 
 	switch(t->type) {
 	case TPAREN:
-		rv = execute(t->left, pin, pout, 0);
-		break;
-			
 	case TCOM:
 		{
 			int child;
@@ -3716,7 +3675,7 @@
 	int otask;
 	struct io *oiop;
 	char *dolp;
-	register char *s, c, *cp = NULL;
+	register char *s, c, *cp=NULL;
 	struct var *vp;
 
 	c = readc();
@@ -3808,12 +3767,11 @@
 	return(0);
 }
 
-// old version from busybox
 /*
  * Run the command in `...` and read its output.
  */
 static int
-old_grave(quoted)
+grave(quoted)
 int quoted;
 {
 	register int i;
@@ -3865,157 +3823,6 @@
 	exit(1);
 }
 
-/*
- * Run the command in `...` and read its output.
- */
-
-static int
-axis_grave(quoted)
-int quoted;
-{
-	char *cp;
-	register int i;
-	int j;
-	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__
-	/* Avoid longjmp clobbering */
-	(void) &cp;
-#endif
-	
-	for (cp = e.iop->argp->aword; *cp != '`'; cp++)
-		if (*cp == 0) {
-			err("no closing `");
-			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)
-		return(0);
-	while ((i = vfork()) == EAGAIN)
-		;
-	if (i < 0) {
-		closepipe(pf);
-		err("Out of memory");
-		return(0);
-	}
-	if (i != 0) {
-		waitpid(i, NULL, 0);
-		e.iop->argp->aword = ++cp;
-		close(pf[1]);
-		PUSHIO(afile, remap(pf[0]), (int(*)(struct ioarg *))((quoted)? qgravechar: gravechar));
-		return(1);
-	}
-	/* allow trapped signals */
-	/* XXX - Maybe this signal stuff should go as well? */
-	for (j=0; j<=_NSIG; j++)
-		if (ourtrap[j] && signal(j, SIG_IGN) != SIG_IGN)
-			signal(j, SIG_DFL);
-	
-	dup2(pf[1], 1);
-	closepipe(pf);
-	
-	argument_list[0] = subshellname;
-	argument_list[1] = "-c";
-	argument_list[2] = child_cmd;
-	argument_list[3] = 0;
-
-	prs(rexecve(argument_list[0], argument_list, makenv()));
-	_exit(1);
-}
-
-
 static char *
 unquote(as)
 register char *as;
@@ -4369,20 +4176,6 @@
 	} while(--m);
 }
 
-char *
-memcopy(ato, from, nb)
-register char *ato, *from;
-register int nb;
-{
-	register char *to;
-
-	to = ato;
-	while (--nb >= 0)
-		*to++ = *from++;
-	return(ato);
-}
-
-
 /* -------- io.c -------- */
 
 /*
@@ -4401,10 +4194,7 @@
 		return(c);
 	}
 	c = readc();
-// BB version: 	
-//	if (ec != '\'' && e.iop->task != XGRAVE) {
-// AXIS version:
-	if ((ec != '\'') && (ec != '`') && (e.iop->task != XGRAVE)) {
+ 	if (ec != '\'' && e.iop->task != XGRAVE) {
 		if(c == '\\') {
 			c = readc();
 			if (c == '\n' && ec != '\"')


More information about the busybox mailing list