Index: trunk/src/ash-messup/bltin/bltin.h
===================================================================
--- trunk/src/ash-messup/bltin/bltin.h	(revision 879)
+++ trunk/src/ash-messup/bltin/bltin.h	(revision 880)
@@ -84,11 +84,11 @@
 #endif
 
-pointer stalloc(int);
-void error(const char *, ...);
-void sh_warnx(const char *, ...);
-void sh_exit(int) __attribute__((__noreturn__));
+pointer stalloc(struct shinstance *, int);
+void error(struct shinstance *, const char *, ...);
+void sh_warnx(struct shinstance *, const char *, ...);
+void sh_exit(struct shinstance *, int) __attribute__((__noreturn__));
 
-int echocmd(int, char **);
+int echocmd(struct shinstance *, int, char **);
 
 
-extern const char *commandname;
+/*extern const char *commandname;*/
Index: trunk/src/ash-messup/main.c
===================================================================
--- trunk/src/ash-messup/main.c	(revision 879)
+++ trunk/src/ash-messup/main.c	(revision 880)
@@ -81,6 +81,6 @@
 #define PROFILE 0
 
-int rootpid;
-int rootshell;
+/*int rootpid;
+int rootshell;*/
 STATIC union node *curcmd;
 STATIC union node *prevcmd;
@@ -89,4 +89,8 @@
 STATIC char *find_dot_file(char *);
 int main(int, char **);
+int shell_main(shinstance *, int, char **);
+#ifdef _MSC_VER
+extern void init_syntax(void);
+#endif
 
 /*
@@ -103,6 +107,11 @@
 	shinstance *psh;
 
+	/*
+	 * Global initializations. 
+         */
 	setlocale(LC_ALL, "");
-
+#ifdef _MSC_VER
+	init_syntax();
+#endif
 	/*
 	 * Create the root shell instance.
@@ -111,4 +120,5 @@
 	if (!psh)
 		return 2;
+	shthread_set_shell(psh);
 	return shell_main(psh, argc);
 }	
@@ -129,18 +139,18 @@
 		 * the shell procedure.
 		 */
-		switch (exception) {
+		switch (psh->exception) {
 		case EXSHELLPROC:
-			rootpid = getpid();
-			rootshell = 1;
-			minusc = NULL;
-			state = 3;
+			psh->rootpid = /*getpid()*/ psh->pid;
+			psh->rootshell = 1;
+			psh->minusc = NULL;
+			psh->state = 3;
 			break;
 
 		case EXEXEC:
-			exitstatus = exerrno;
+			psh->exitstatus = psh->exerrno;
 			break;
 
 		case EXERROR:
-			exitstatus = 2;
+			psh->exitstatus = 2;
 			break;
 
@@ -149,18 +159,18 @@
 		}
 
-		if (exception != EXSHELLPROC) {
-			if (state == 0 || iflag == 0 || ! rootshell)
-				exitshell(exitstatus);
-		}
-		reset();
-		if (exception == EXINT
+		if (psh->exception != EXSHELLPROC) {
+			if (state == 0 || iflag == 0 || ! psh->rootshell)
+				exitshell(psh, exitstatus);
+		}
+		reset(psh);
+		if (psh->exception == EXINT
 #if ATTY
-		 && (! attyset() || equal(termval(), "emacs"))
+		 && (! attyset(psh) || equal(termval(psh), "emacs"))
 #endif
 		 ) {
-			out2c('\n');
+			out2c(psh, '\n');
 			flushout(&errout);
 		}
-		popstackmark(&smark);
+		popstackmark(psh, &smark);
 		FORCEINTON;				/* enable interrupts */
 		if (state == 1)
@@ -174,22 +184,17 @@
 	}
 	psh->handler = &jmploc;
+	psh->rootpid = /*getpid()*/ psh->pid;
+	psh->rootshell = 1;
 #ifdef DEBUG
 #if DEBUG == 2
 	debug = 1;
 #endif
-	opentrace();
+	opentrace(psh);
 	trputs("Shell args:  ");  trargs(argv);
 #endif
-	psh->rootpid = getpid();
-	psh->rootshell = 1;
-#ifdef _MSC_VER
-    {
-        extern void init_syntax(void);
-        init_syntax();
-    }
-#endif
-	init();
-	setstackmark(&smark);
-	procargs(argc, argv);
+
+	init(psh);
+	setstackmark(psh, &smark);
+	procargs(psh, argc, argv);
 	if (argv[0] && argv[0][0] == '-') {
 		state = 1;
@@ -209,5 +214,5 @@
 state3:
 	state = 4;
-	if (sflag == 0 || minusc) {
+	if (psh->sflag == 0 || psh->minusc) {
 		static int sigs[] =  {
 		    SIGINT, SIGQUIT, SIGHUP,
@@ -242,5 +247,5 @@
 
 void
-cmdloop(int top)
+cmdloop(struct shinstance *psh, int top)
 {
 	union node *n;
@@ -250,39 +255,39 @@
 
 	TRACE(("cmdloop(%d) called\n", top));
-	setstackmark(&smark);
+	setstackmark(psh, &smark);
 	for (;;) {
-		if (pendingsigs)
-			dotrap();
+		if (psh->pendingsigs)
+			dotrap(psh);
 		inter = 0;
-		if (iflag && top) {
+		if (psh->iflag && top) {
 			inter = 1;
-			showjobs(out2, SHOW_CHANGED);
-			chkmail(0);
-			flushout(&errout);
-		}
-		n = parsecmd(inter);
+			showjobs(psh, psh->out2, SHOW_CHANGED);
+			chkmail(psh, 0);
+			flushout(&psh->errout);
+		}
+		n = parsecmd(psh, inter);
 		/* showtree(n); DEBUG */
 		if (n == NEOF) {
 			if (!top || numeof >= 50)
 				break;
-			if (!stoppedjobs()) {
-				if (!Iflag)
+			if (!stoppedjobs(psh)) {
+				if (!psh->Iflag)
 					break;
-				out2str("\nUse \"exit\" to leave shell.\n");
+				out2str(psh, "\nUse \"exit\" to leave shell.\n");
 			}
 			numeof++;
-		} else if (n != NULL && nflag == 0) {
-			job_warning = (job_warning == 2) ? 1 : 0;
+		} else if (n != NULL && psh->nflag == 0) {
+			psh->job_warning = (psh->job_warning == 2) ? 1 : 0;
 			numeof = 0;
-			evaltree(n, 0);
-		}
-		popstackmark(&smark);
-		setstackmark(&smark);
-		if (evalskip == SKIPFILE) {
-			evalskip = 0;
+			evaltree(psh, n, 0);
+		}
+		popstackmark(psh, &smark);
+		setstackmark(psh, &smark);
+		if (psh->evalskip == SKIPFILE) {
+			psh->evalskip = 0;
 			break;
 		}
 	}
-	popstackmark(&smark);
+	popstackmark(psh, &smark);
 }
 
@@ -294,5 +299,5 @@
 
 STATIC void
-read_profile(const char *name)
+read_profile(struct shinstance *psh, const char *name)
 {
 	int fd;
@@ -301,24 +306,24 @@
 
 	INTOFF;
-	if ((fd = open(name, O_RDONLY)) >= 0)
-		setinputfd(fd, 1);
+	if ((fd = shfile_open(psh, name, O_RDONLY)) >= 0)
+		setinputfd(psh, fd, 1);
 	INTON;
 	if (fd < 0)
 		return;
 	/* -q turns off -x and -v just when executing init files */
-	if (qflag)  {
-	    if (xflag)
-		    xflag = 0, xflag_set = 1;
-	    if (vflag)
-		    vflag = 0, vflag_set = 1;
-	}
-	cmdloop(0);
-	if (qflag)  {
+	if (psh->qflag)  {
+	    if (psh->xflag)
+		    psh->xflag = 0, xflag_set = 1;
+	    if (psh->vflag)
+		    psh->vflag = 0, vflag_set = 1;
+	}
+	cmdloop(psh, 0);
+	if (psh->qflag)  {
 	    if (xflag_set)
-		    xflag = 1;
+		    psh->xflag = 1;
 	    if (vflag_set)
-		    vflag = 1;
-	}
-	popfile();
+		    psh->vflag = 1;
+	}
+	popfile(psh);
 }
 
@@ -330,5 +335,5 @@
 
 void
-readcmdfile(char *name)
+readcmdfile(struct shinstance *psh, char *name)
 {
 	int fd;
@@ -336,10 +341,10 @@
 	INTOFF;
 	if ((fd = open(name, O_RDONLY)) >= 0)
-		setinputfd(fd, 1);
+		setinputfd(psh, fd, 1);
 	else
 		error("Can't open %s", name);
 	INTON;
-	cmdloop(0);
-	popfile();
+	cmdloop(psh, 0);
+	popfile(psh);
 }
 
@@ -353,8 +358,8 @@
 
 STATIC char *
-find_dot_file(char *basename)
+find_dot_file(struct shinstance *psh, char *basename)
 {
 	char *fullname;
-	const char *path = pathval();
+	const char *path = pathval(psh);
 	struct stat statb;
 
@@ -363,5 +368,5 @@
 		return basename;
 
-	while ((fullname = padvance(&path, basename)) != NULL) {
+	while ((fullname = padvance(psh, &path, basename)) != NULL) {
 		if ((stat(fullname, &statb) == 0) && S_ISREG(statb.st_mode)) {
 			/*
@@ -371,5 +376,5 @@
 			return fullname;
 		}
-		stunalloc(fullname);
+		stunalloc(psh, fullname);
 	}
 
@@ -380,7 +385,7 @@
 
 int
-dotcmd(int argc, char **argv)
-{
-	exitstatus = 0;
+dotcmd(struct shinstance *psh, int argc, char **argv)
+{
+	psh->exitstatus = 0;
 
 	if (argc >= 2) {		/* That's what SVR2 does */
@@ -388,24 +393,30 @@
 		struct stackmark smark;
 
-		setstackmark(&smark);
-		fullname = find_dot_file(argv[1]);
-		setinputfile(fullname, 1);
-		commandname = fullname;
-		cmdloop(0);
-		popfile();
-		popstackmark(&smark);
-	}
-	return exitstatus;
+		setstackmark(psh, &smark);
+		fullname = find_dot_file(psh, argv[1]);
+		setinputfile(psh, fullname, 1);
+		psh->commandname = fullname;
+		cmdloop(psh, 0);
+		popfile(psh);
+		popstackmark(psh, &smark);
+	}
+	return psh->exitstatus;
 }
 
 
 int
-exitcmd(int argc, char **argv)
-{
-	if (stoppedjobs())
+exitcmd(struct shinstance *psh, int argc, char **argv)
+{
+	if (stoppedjobs(psh))
 		return 0;
 	if (argc > 1)
-		exitstatus = number(argv[1]);
-	exitshell(exitstatus);
+		psh->exitstatus = number(argv[1]);
+	exitshell(psh, exitstatus);
 	/* NOTREACHED */
 }
+
+/*
+ * Local Variables:
+ *   c-file-style: bsd
+ * End:
+ */
Index: trunk/src/ash-messup/options.h
===================================================================
--- trunk/src/ash-messup/options.h	(revision 879)
+++ trunk/src/ash-messup/options.h	(revision 880)
@@ -69,44 +69,44 @@
 
 DEF_OPT( "errexit",	'e' )	/* exit on error */
-#define eflag psh->optlist[0].val
+#define eflag optlist[0].val
 DEF_OPT( "noglob",	'f' )	/* no pathname expansion */
-#define fflag psh->optlist[1].val
+#define fflag optlist[1].val
 DEF_OPT( "ignoreeof",	'I' )	/* do not exit on EOF */
-#define Iflag psh->optlist[2].val
+#define Iflag optlist[2].val
 DEF_OPT( "interactive",'i' )	/* interactive shell */
-#define iflag psh->optlist[3].val
+#define iflag optlist[3].val
 DEF_OPT( "monitor",	'm' )	/* job control */
-#define mflag psh->optlist[4].val
+#define mflag optlist[4].val
 DEF_OPT( "noexec",	'n' )	/* [U] do not exec commands */
-#define nflag psh->optlist[5].val
+#define nflag optlist[5].val
 DEF_OPT( "stdin",	's' )	/* read from stdin */
-#define sflag psh->optlist[6].val
+#define sflag optlist[6].val
 DEF_OPT( "xtrace",	'x' )	/* trace after expansion */
-#define xflag psh->optlist[7].val
+#define xflag optlist[7].val
 DEF_OPT( "verbose",	'v' )	/* trace read input */
-#define vflag psh->optlist[8].val
+#define vflag optlist[8].val
 DEF_OPTS( "vi",		'V', 'V' )	/* vi style editing */
-#define Vflag psh->optlist[9].val
+#define Vflag optlist[9].val
 DEF_OPTS( "emacs",	'E', 'V' )	/* emacs style editing */
-#define	Eflag psh->optlist[10].val
+#define	Eflag optlist[10].val
 DEF_OPT( "noclobber",	'C' )	/* do not overwrite files with > */
-#define	Cflag psh->optlist[11].val
+#define	Cflag optlist[11].val
 DEF_OPT( "allexport",	'a' )	/* export all variables */
-#define	aflag psh->optlist[12].val
+#define	aflag optlist[12].val
 DEF_OPT( "notify",	'b' )	/* [U] report completion of background jobs */
-#define	bflag psh->optlist[13].val
+#define	bflag optlist[13].val
 DEF_OPT( "nounset",	'u' )	/* error expansion of unset variables */
-#define	uflag psh->optlist[14].val
+#define	uflag optlist[14].val
 DEF_OPT( "quietprofile", 'q' )
-#define	qflag psh->optlist[15].val
+#define	qflag optlist[15].val
 DEF_OPT( "nolog",	0 )	/* [U] no functon defs in command history */
-#define	nolog psh->optlist[16].val
+#define	nolog optlist[16].val
 DEF_OPT( "cdprint",	0 )	/* always print result of cd */
-#define	cdprint psh->optlist[17].val
+#define	cdprint optlist[17].val
 DEF_OPT( "tabcomplete",	0 )	/* <tab> causes filename expansion */
-#define	tabcomplete psh->optlist[18].val
+#define	tabcomplete optlist[18].val
 #ifdef DEBUG
 DEF_OPT( "debug",	0 )	/* enable debug prints */
-#define	debug psh->optlist[19].val
+#define	debug optlist[19].val
 #endif
 
Index: trunk/src/ash-messup/output.h
===================================================================
--- trunk/src/ash-messup/output.h	(revision 879)
+++ trunk/src/ash-messup/output.h	(revision 880)
@@ -75,7 +75,7 @@
 int xioctl(int, unsigned long, char *);
 
-#define outc(c, file)	(--(file)->nleft < 0? (emptyoutbuf(psh, file), *(file)->nextc++ = (c)) : (*(file)->nextc++ = (c)))
-#define out1c(c)	outc(c, psh->out1);
-#define out2c(c)	outc(c, psh->out2);
+#define outc(c, file)	(--(file)->nleft < 0? (emptyoutbuf(file), *(file)->nextc++ = (c)) : (*(file)->nextc++ = (c)))
+#define out1c(psh, c)	outc(c, (psh)->out1);
+#define out2c(psh, c)	outc(c, (psh)->out2);
 
 #define OUTPUT_INCL
Index: trunk/src/ash-messup/shinstance.h
===================================================================
--- trunk/src/ash-messup/shinstance.h	(revision 879)
+++ trunk/src/ash-messup/shinstance.h	(revision 880)
@@ -153,4 +153,8 @@
     int                 funcnest;
     int                 evalskip;
+
+    /* builtins.h */
+    const char         *commandname;
+
 } shinstance;
 
Index: trunk/src/ash-messup/show.h
===================================================================
--- trunk/src/ash-messup/show.h	(revision 879)
+++ trunk/src/ash-messup/show.h	(revision 880)
@@ -42,4 +42,4 @@
 void trputc(int);
 void trputs(const char *);
-void opentrace(void);
+void opentrace(struct shinstance *);
 #endif
Index: trunk/src/ash-messup/var.h
===================================================================
--- trunk/src/ash-messup/var.h	(revision 879)
+++ trunk/src/ash-messup/var.h	(revision 880)
@@ -100,26 +100,26 @@
  */
 
-#define ifsval()	(psh->vifs.text + 4)
-#define ifsset()	((psh->vifs.flags & VUNSET) == 0)
-#define mailval()	(psh->vmail.text + 5)
-#define mpathval()	(psh->vmpath.text + 9)
+#define ifsval(psh)	((psh)->vifs.text + 4)
+#define ifsset(psh)	(((psh)->vifs.flags & VUNSET) == 0)
+#define mailval(psh)	((psh)->vmail.text + 5)
+#define mpathval(psh)	((psh)->vmpath.text + 9)
 #ifdef _MSC_VER
-#define pathval()	(psh->vpath.text[5] ? &psh->vpath.text[5] : &psh->vpath2.text[5])
+#define pathval(psh)	((psh)->vpath.text[5] ? &(psh)->vpath.text[5] : &(psh)->vpath2.text[5])
 #else
-#define pathval()	(psh->vpath.text + 5)
+#define pathval(psh)	((psh)->vpath.text + 5)
 #endif
-#define ps1val()	(psh->vps1.text + 4)
-#define ps2val()	(psh->vps2.text + 4)
-#define ps4val()	(psh->vps4.text + 4)
-#define optindval()	(psh->voptind.text + 7)
+#define ps1val(psh)	((psh)->vps1.text + 4)
+#define ps2val(psh)	((psh)->vps2.text + 4)
+#define ps4val(psh)	((psh)->vps4.text + 4)
+#define optindval(psh)	((psh)->voptind.text + 7)
 #ifndef SMALL
-#define histsizeval()	(psh->vhistsize.text + 9)
-#define termval()	(psh->vterm.text + 5)
+#define histsizeval(psh) ((psh)->vhistsize.text + 9)
+#define termval(psh)	((psh)->vterm.text + 5)
 #endif
 
 #if ATTY
-#define attyset()	((psh->vatty.flags & VUNSET) == 0)
+#define attyset(psh)	(((psh)->vatty.flags & VUNSET) == 0)
 #endif
-#define mpathset()	((psh->vmpath.flags & VUNSET) == 0)
+#define mpathset(psh)	(((psh)->vmpath.flags & VUNSET) == 0)
 
 void initvar(struct shinstance *);
