Index: /trunk/src/kash/Makefile.kmk
===================================================================
--- /trunk/src/kash/Makefile.kmk	(revision 1208)
+++ /trunk/src/kash/Makefile.kmk	(revision 1209)
@@ -51,9 +51,9 @@
 	bltin/echo.c \
 	bltin/test.c \
+	trap.c \
+	var.c \
 	\
-	trap.c \
 	parser.c \
 	redir.c \
-	var.c \
 	bltin/kill.c \
 	$(PATH_TARGET)/arith.c \
Index: /trunk/src/kash/histedit.c
===================================================================
--- /trunk/src/kash/histedit.c	(revision 1208)
+++ /trunk/src/kash/histedit.c	(revision 1209)
@@ -178,5 +178,5 @@
 
 void
-sethistsize(const char *hs)
+sethistsize(shinstance *psh, const char *hs)
 {
 	int histsize;
@@ -192,5 +192,5 @@
 
 void
-setterm(const char *term)
+setterm(shinstance *psh, const char *term)
 {
 	if (el != NULL && term != NULL)
Index: /trunk/src/kash/shinstance.h
===================================================================
--- /trunk/src/kash/shinstance.h	(revision 1208)
+++ /trunk/src/kash/shinstance.h	(revision 1209)
@@ -174,7 +174,16 @@
 #ifndef SMALL
     struct var          vterm;
-    struct var          vtermcap;
     struct var          vhistsize;
 #endif
+    struct var          voptind;
+#ifdef PC_OS2_LIBPATHS
+    struct var          libpath_vars[4];
+#endif
+#ifdef SMALL
+# define VTABSIZE 39
+#else
+# define VTABSIZE 517
+#endif
+    struct var         *vartab[VTABSIZE];
 
     /* myhistedit.h */
Index: /trunk/src/kash/var.c
===================================================================
--- /trunk/src/kash/var.c	(revision 1208)
+++ /trunk/src/kash/var.c	(revision 1209)
@@ -45,4 +45,5 @@
 
 #include <unistd.h>
+#include <stddef.h>
 #include <stdlib.h>
 #include <strings.h>
@@ -101,64 +102,65 @@
 #include "myhistedit.h"
 #endif
-
-#ifdef SMALL
-#define VTABSIZE 39
-#else
-#define VTABSIZE 517
-#endif
+#include "shinstance.h"
+
+//#ifdef SMALL
+//#define VTABSIZE 39
+//#else
+//#define VTABSIZE 517
+//#endif
 
 
 struct varinit {
-	struct var *var;
+	unsigned var_off;
 	int flags;
 	const char *text;
-	void (*func)(const char *);
+	void (*func)(shinstance *, const char *);
 };
 
 
-#if ATTY
-struct var vatty;
-#endif
-#ifndef SMALL
-struct var vhistsize;
-struct var vterm;
-#endif
-struct var vifs;
-struct var vmail;
-struct var vmpath;
-struct var vpath;
-#ifdef _MSC_VER
-struct var vpath2;
-#endif
-struct var vps1;
-struct var vps2;
-struct var vps4;
-struct var vvers;
-struct var voptind;
+//#if ATTY
+//struct var vatty;
+//#endif
+//#ifndef SMALL
+//struct var vhistsize;
+//struct var vterm;
+//#endif
+//struct var vifs;
+//struct var vmail;
+//struct var vmpath;
+//struct var vpath;
+//#ifdef _MSC_VER
+//struct var vpath2;
+//#endif
+//struct var vps1;
+//struct var vps2;
+//struct var vps4;
+//struct var vvers; - unused
+//struct var voptind;
 
 #ifdef PC_OS2_LIBPATHS
-static struct var libpath_vars[4];
-static const char *libpath_envs[4] = {"LIBPATH=", "BEGINLIBPATH=", "ENDLIBPATH=", "LIBPATHSTRICT="};
+//static struct var libpath_vars[4];
+static const char * const libpath_envs[4] = {"LIBPATH=", "BEGINLIBPATH=", "ENDLIBPATH=", "LIBPATHSTRICT="};
 #endif
 
 const struct varinit varinit[] = {
 #if ATTY
-	{ &vatty,	VSTRFIXED|VTEXTFIXED|VUNSET,	"ATTY=",
+	{ offsetof(shinstance, vatty),	VSTRFIXED|VTEXTFIXED|VUNSET,	"ATTY=",
 	  NULL },
 #endif
 #ifndef SMALL
-	{ &vhistsize,	VSTRFIXED|VTEXTFIXED|VUNSET,	"HISTSIZE=",
+	{ offsetof(shinstance, vhistsize),	VSTRFIXED|VTEXTFIXED|VUNSET,	"HISTSIZE=",
 	  sethistsize },
 #endif
-	{ &vifs,	VSTRFIXED|VTEXTFIXED,		"IFS= \t\n",
+	{ offsetof(shinstance, vifs),	VSTRFIXED|VTEXTFIXED,		"IFS= \t\n",
 	  NULL },
-	{ &vmail,	VSTRFIXED|VTEXTFIXED|VUNSET,	"MAIL=",
+	{ offsetof(shinstance, vmail),	VSTRFIXED|VTEXTFIXED|VUNSET,	"MAIL=",
 	  NULL },
-	{ &vmpath,	VSTRFIXED|VTEXTFIXED|VUNSET,	"MAILPATH=",
+	{ offsetof(shinstance, vmpath),	VSTRFIXED|VTEXTFIXED|VUNSET,	"MAILPATH=",
 	  NULL },
-	{ &vpath,	VSTRFIXED|VTEXTFIXED,		"PATH=" _PATH_DEFPATH,
+	{ offsetof(shinstance, vpath),	VSTRFIXED|VTEXTFIXED,		"PATH=" _PATH_DEFPATH,
 	  changepath },
 #ifdef _MSC_VER
-	{ &vpath2,	VSTRFIXED|VTEXTFIXED,		"Path=",
+	{ offsetof(shinstance, vpath2),	VSTRFIXED|VTEXTFIXED,		"Path=",
 	  changepath },
 #endif
@@ -166,22 +168,22 @@
 	 * vps1 depends on uid
 	 */
-	{ &vps2,	VSTRFIXED|VTEXTFIXED,		"PS2=> ",
+	{ offsetof(shinstance, vps2),	VSTRFIXED|VTEXTFIXED,		"PS2=> ",
 	  NULL },
-	{ &vps4,	VSTRFIXED|VTEXTFIXED,		"PS4=+ ",
+	{ offsetof(shinstance, vps4),	VSTRFIXED|VTEXTFIXED,		"PS4=+ ",
 	  NULL },
 #ifndef SMALL
-	{ &vterm,	VSTRFIXED|VTEXTFIXED|VUNSET,	"TERM=",
+	{ offsetof(shinstance, vterm),	VSTRFIXED|VTEXTFIXED|VUNSET,	"TERM=",
 	  setterm },
 #endif
-	{ &voptind,	VSTRFIXED|VTEXTFIXED|VNOFUNC,	"OPTIND=1",
+	{ offsetof(shinstance, voptind),	VSTRFIXED|VTEXTFIXED|VNOFUNC,	"OPTIND=1",
 	  getoptsreset },
-	{ NULL,	0,				NULL,
+	{ 0,	0,				NULL,
 	  NULL }
 };
 
-struct var *vartab[VTABSIZE];
+//struct var *vartab[VTABSIZE];
 
 STATIC int strequal(const char *, const char *);
-STATIC struct var *find_var(const char *, struct var ***, int *);
+STATIC struct var *find_var(shinstance *, const char *, struct var ***, int *);
 
 /*
@@ -211,52 +213,53 @@
 
 void
-initvar(void)
+initvar(shinstance *psh)
 {
 	const struct varinit *ip;
 	struct var *vp;
 	struct var **vpp;
-
 #ifdef PC_OS2_LIBPATHS
-        char *psz = ckmalloc(2048);
-        int rc;
-        int i;
-        for (i = 0; i < 4; i++)
-        {
-            libpath_vars[i].flags = VSTRFIXED | VOS2LIBPATH;
-            libpath_vars[i].func = NULL;
-
-            if (i > 0)
-            {
-                psz[0] = psz[1] = psz[2] = psz[3] = '\0';
-                rc = DosQueryExtLIBPATH(psz, i);
-            }
-            else
-            {
-                rc = DosQueryHeaderInfo(NULLHANDLE, 0, psz, 2048, QHINF_LIBPATH);
-                libpath_vars[i].flags |= VREADONLY;
-            }
-            if (!rc && *psz)
-            {
-                int cch1 = strlen(libpath_envs[i]);
-                int cch2 = strlen(psz) + 1;
-                libpath_vars[i].text = ckmalloc(cch1 + cch2);
-                memcpy(libpath_vars[i].text, libpath_envs[i], cch1);
-                memcpy(libpath_vars[i].text + cch1, psz, cch2);
-            }
-            else
-            {
-                libpath_vars[i].flags |= VUNSET | VTEXTFIXED;
-                libpath_vars[i].text = (char*)libpath_envs[i];
-            }
-            if (find_var(libpath_vars[i].text, &vpp, &libpath_vars[i].name_len) != NULL)
-                    continue;
-            libpath_vars[i].next = *vpp;
-            *vpp = &libpath_vars[i];
-        }
-        free(psz);
-#endif
-
-	for (ip = varinit ; (vp = ip->var) != NULL ; ip++) {
-		if (find_var(ip->text, &vpp, &vp->name_len) != NULL)
+	char *psz = ckmalloc(2048);
+	int rc;
+	int i;
+
+	for (i = 0; i < 4; i++)
+	{
+		psh->libpath_vars[i].flags = VSTRFIXED | VOS2LIBPATH;
+		psh->libpath_vars[i].func = NULL;
+
+		if (i > 0)
+		{
+			psz[0] = psz[1] = psz[2] = psz[3] = '\0';
+			rc = DosQueryExtLIBPATH(psz, i);
+		}
+		else
+		{
+			rc = DosQueryHeaderInfo(NULLHANDLE, 0, psz, 2048, QHINF_LIBPATH);
+			psh->libpath_vars[i].flags |= VREADONLY;
+		}
+		if (!rc && *psz)
+		{
+			int cch1 = strlen(libpath_envs[i]);
+			int cch2 = strlen(psz) + 1;
+			psh->libpath_vars[i].text = ckmalloc(cch1 + cch2);
+			memcpy(psh->libpath_vars[i].text, libpath_envs[i], cch1);
+			memcpy(psh->libpath_vars[i].text + cch1, psz, cch2);
+		}
+		else
+		{
+			psh->libpath_vars[i].flags |= VUNSET | VTEXTFIXED;
+			psh->libpath_vars[i].text = (char*)libpath_envs[i];
+		}
+		if (find_var(psh, psh->libpath_vars[i].text, &vpp, &psh->libpath_vars[i].name_len) != NULL)
+			continue;
+		psh->libpath_vars[i].next = *vpp;
+		*vpp = &psh->libpath_vars[i];
+	}
+	free(psz);
+#endif
+
+	for (ip = varinit; ip->text; ip++) {
+		vp = (struct var *)((char *)psh + ip->var_off);
+		if (find_var(psh, ip->text, &vpp, &vp->name_len) != NULL)
 			continue;
 		vp->next = *vpp;
@@ -269,9 +272,9 @@
 	 * PS1 depends on uid
 	 */
-	if (find_var("PS1", &vpp, &vps1.name_len) == NULL) {
-		vps1.next = *vpp;
-		*vpp = &vps1;
-		vps1.text = strdup(sh_geteuid(psh) ? "PS1=$ " : "PS1=# ");
-		vps1.flags = VSTRFIXED|VTEXTFIXED;
+	if (find_var(psh, "PS1", &vpp, &psh->vps1.name_len) == NULL) {
+		psh->vps1.next = *vpp;
+		*vpp = &psh->vps1;
+		psh->vps1.text = strdup(sh_geteuid(psh) ? "PS1=$ " : "PS1=# ");
+		psh->vps1.flags = VSTRFIXED|VTEXTFIXED;
 	}
 }
@@ -282,5 +285,5 @@
 
 int
-setvarsafe(const char *name, const char *val, int flags)
+setvarsafe(shinstance *psh, const char *name, const char *val, int flags)
 {
 	struct jmploc jmploc;
@@ -307,10 +310,10 @@
 
 void
-setvar(const char *name, const char *val, int flags)
+setvar(shinstance *psh, const char *name, const char *val, int flags)
 {
 	const char *p;
 	const char *q;
 	char *d;
-	int len;
+	size_t len;
 	int namelen;
 	char *nameeq;
@@ -330,5 +333,5 @@
 		p++;
 	}
-	namelen = p - name;
+	namelen = (int)(p - name);
 	if (isbad)
 		error(psh, "%.*s: bad variable name", namelen, name);
@@ -360,5 +363,5 @@
 
 void
-setvareq(char *s, int flags)
+setvareq(shinstance *psh, char *s, int flags)
 {
 	struct var *vp, **vpp;
@@ -367,5 +370,5 @@
 	if (aflag(psh))
 		flags |= VEXPORT;
-	vp = find_var(s, &vpp, &nlen);
+	vp = find_var(psh, s, &vpp, &nlen);
 	if (vp != NULL) {
 		if (vp->flags & VREADONLY)
@@ -376,5 +379,5 @@
 
 		if (vp->func && (flags & VNOFUNC) == 0)
-			(*vp->func)(s + vp->name_len + 1);
+			(*vp->func)(psh, s + vp->name_len + 1);
 
 		if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0)
@@ -385,6 +388,6 @@
 		vp->text = s;
 #ifdef PC_OS2_LIBPATHS
-                if ((vp->flags & VOS2LIBPATH) && (vp->flags & VEXPORT))
-                    vp->flags &= ~VEXPORT;
+		if ((vp->flags & VOS2LIBPATH) && (vp->flags & VEXPORT))
+			vp->flags &= ~VEXPORT;
 #endif
 
@@ -393,5 +396,5 @@
 		 * a regular variable function callback, but why bother?
 		 */
-		if (vp == &vmpath || (vp == &vmail && ! mpathset()))
+		if (vp == &psh->vmpath || (vp == &psh->vmail && ! mpathset(psh)))
 			chkmail(psh, 1);
 		INTON;
@@ -417,5 +420,5 @@
 
 void
-listsetvar(struct strlist *list, int flags)
+listsetvar(shinstance *psh, struct strlist *list, int flags)
 {
 	struct strlist *lp;
@@ -429,5 +432,5 @@
 
 void
-listmklocal(struct strlist *list, int flags)
+listmklocal(shinstance *psh, struct strlist *list, int flags)
 {
 	struct strlist *lp;
@@ -443,9 +446,9 @@
 
 char *
-lookupvar(const char *name)
+lookupvar(shinstance *psh, const char *name)
 {
 	struct var *v;
 
-	v = find_var(name, NULL, NULL);
+	v = find_var(psh, name, NULL, NULL);
 	if (v == NULL || v->flags & VUNSET)
 		return NULL;
@@ -462,15 +465,15 @@
 
 char *
-bltinlookup(const char *name, int doall)
+bltinlookup(shinstance *psh, const char *name, int doall)
 {
 	struct strlist *sp;
 	struct var *v;
 
-	for (sp = cmdenviron ; sp ; sp = sp->next) {
+	for (sp = psh->cmdenviron ; sp ; sp = sp->next) {
 		if (strequal(sp->text, name))
 			return strchr(sp->text, '=') + 1;
 	}
 
-	v = find_var(name, NULL, NULL);
+	v = find_var(psh, name, NULL, NULL);
 
 	if (v == NULL || v->flags & VUNSET || (!doall && !(v->flags & VEXPORT)))
@@ -487,5 +490,5 @@
 
 char **
-environment(void)
+environment(shinstance *psh)
 {
 	int nenv;
@@ -496,5 +499,5 @@
 
 	nenv = 0;
-	for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
+	for (vpp = psh->vartab ; vpp < psh->vartab + VTABSIZE ; vpp++) {
 		for (vp = *vpp ; vp ; vp = vp->next)
 			if (vp->flags & VEXPORT)
@@ -502,5 +505,5 @@
 	}
 	ep = env = stalloc(psh, (nenv + 1) * sizeof *env);
-	for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
+	for (vpp = psh->vartab ; vpp < psh->vartab + VTABSIZE ; vpp++) {
 		for (vp = *vpp ; vp ; vp = vp->next)
 			if (vp->flags & VEXPORT)
@@ -510,9 +513,9 @@
 
 #ifdef PC_OS2_LIBPATHS
-        /*
-         * Set the libpaths now as this is exec() time.
-         */
-        for (nenv = 0; nenv < 3; nenv++)
-            DosSetExtLIBPATH(strchr(libpath_vars[nenv].text, '=') + 1, nenv);
+	/*
+	 * Set the libpaths now as this is exec() time.
+	 */
+	for (nenv = 0; nenv < 3; nenv++)
+		DosSetExtLIBPATH(strchr(psh->libpath_vars[nenv].text, '=') + 1, nenv);
 #endif
 
@@ -570,5 +573,5 @@
 
 void
-print_quoted(const char *p)
+print_quoted(shinstance *psh, const char *p)
 {
 	const char *q;
@@ -613,5 +616,5 @@
 
 int
-showvars(const char *name, int flag, int show_value)
+showvars(shinstance *psh, const char *name, int flag, int show_value)
 {
 	struct var **vpp;
@@ -625,8 +628,8 @@
 	if (!list) {
 		list_len = 32;
-		list = ckmalloc(list_len * sizeof *list);
-	}
-
-	for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
+		list = ckmalloc(list_len * sizeof(*list));
+	}
+
+	for (vpp = psh->vartab ; vpp < psh->vartab + VTABSIZE ; vpp++) {
 		for (vp = *vpp ; vp ; vp = vp->next) {
 			if (flag && !(vp->flags & flag))
@@ -636,5 +639,5 @@
 			if (count >= list_len) {
 				list = ckrealloc(list,
-					(list_len << 1) * sizeof *list);
+					(list_len << 1) * sizeof(*list));
 				list_len <<= 1;
 			}
@@ -643,5 +646,5 @@
 	}
 
-	qsort(list, count, sizeof *list, sort_var);
+	qsort(list, count, sizeof(*list), sort_var);
 
 	for (vpp = list; count--; vpp++) {
@@ -667,5 +670,5 @@
 
 int
-exportcmd(int argc, char **argv)
+exportcmd(shinstance *psh, int argc, char **argv)
 {
 	struct var *vp;
@@ -685,5 +688,5 @@
 			p++;
 		} else {
-			vp = find_var(name, NULL, NULL);
+			vp = find_var(psh, name, NULL, NULL);
 			if (vp != NULL) {
 				vp->flags |= flag;
@@ -702,5 +705,5 @@
 
 int
-localcmd(int argc, char **argv)
+localcmd(shinstance *psh, int argc, char **argv)
 {
 	char *name;
@@ -723,5 +726,5 @@
 
 void
-mklocal(const char *name, int flags)
+mklocal(shinstance *psh, const char *name, int flags)
 {
 	struct localvar *lvp;
@@ -734,8 +737,8 @@
 		char *p;
 		p = ckmalloc(sizeof_optlist);
-		lvp->text = memcpy(p, optlist, sizeof_optlist);
+		lvp->text = memcpy(p, psh->optlist, sizeof_optlist);
 		vp = NULL;
 	} else {
-		vp = find_var(name, &vpp, NULL);
+		vp = find_var(psh, name, &vpp, NULL);
 		if (vp == NULL) {
 			if (strchr(name, '='))
@@ -755,6 +758,6 @@
 	}
 	lvp->vp = vp;
-	lvp->next = localvars;
-	localvars = lvp;
+	lvp->next = psh->localvars;
+	psh->localvars = lvp;
 	INTON;
 }
@@ -766,15 +769,15 @@
 
 void
-poplocalvars(void)
+poplocalvars(shinstance *psh)
 {
 	struct localvar *lvp;
 	struct var *vp;
 
-	while ((lvp = localvars) != NULL) {
-		localvars = lvp->next;
+	while ((lvp = psh->localvars) != NULL) {
+		psh->localvars = lvp->next;
 		vp = lvp->vp;
 		TRACE((psh, "poplocalvar %s", vp ? vp->text : "-"));
 		if (vp == NULL) {	/* $- saved */
-			memcpy(optlist, lvp->text, sizeof_optlist);
+			memcpy(psh->optlist, lvp->text, sizeof_optlist);
 			ckfree(lvp->text);
 		} else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) {
@@ -782,5 +785,5 @@
 		} else {
 			if (vp->func && (vp->flags & VNOFUNC) == 0)
-				(*vp->func)(lvp->text + vp->name_len + 1);
+				(*vp->func)(psh, lvp->text + vp->name_len + 1);
 			if ((vp->flags & VTEXTFIXED) == 0)
 				ckfree(vp->text);
@@ -794,5 +797,5 @@
 
 int
-setvarcmd(int argc, char **argv)
+setvarcmd(shinstance *psh, int argc, char **argv)
 {
 	if (argc <= 2)
@@ -813,5 +816,5 @@
 
 int
-unsetcmd(int argc, char **argv)
+unsetcmd(shinstance *psh, int argc, char **argv)
 {
 	char **ap;
@@ -845,10 +848,10 @@
 
 int
-unsetvar(const char *s, int unexport)
+unsetvar(shinstance *psh, const char *s, int unexport)
 {
 	struct var **vpp;
 	struct var *vp;
 
-	vp = find_var(s, &vpp, NULL);
+	vp = find_var(psh, s, &vpp, NULL);
 	if (vp == NULL)
 		return 1;
@@ -903,5 +906,5 @@
 
 STATIC struct var *
-find_var(const char *name, struct var ***vppp, int *lenp)
+find_var(shinstance *psh, const char *name, struct var ***vppp, int *lenp)
 {
 	unsigned int hashval;
@@ -913,9 +916,9 @@
 	while (*p && *p != '=')
 		hashval = 2 * hashval + (unsigned char)*p++;
-	len = p - name;
+	len = (int)(p - name);
 
 	if (lenp)
 		*lenp = len;
-	vpp = &vartab[hashval % VTABSIZE];
+	vpp = &psh->vartab[hashval % VTABSIZE];
 	if (vppp)
 		*vppp = vpp;
Index: /trunk/src/kash/var.h
===================================================================
--- /trunk/src/kash/var.h	(revision 1208)
+++ /trunk/src/kash/var.h	(revision 1209)
@@ -61,5 +61,5 @@
 	char *text;			/* name=value */
 	int name_len;			/* length of name */
-	void (*func)(const char *);
+	void (*func)(struct shinstance *, const char *);
 					/* function to be called when  */
 					/* the variable gets set/unset */
