kash: Some variable handling fixes that showed up when using 'local' on stuff in...
[kbuild-mirror.git] / src / kash / var.c
blob22acb28d3c10ee12e555f93487aa233f6d774a04
1 /* $NetBSD: var.c,v 1.36 2004/10/06 10:23:43 enami Exp $ */
3 /*-
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Kenneth Almquist.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
35 #if 0
36 #ifndef lint
37 static char sccsid[] = "@(#)var.c 8.3 (Berkeley) 5/4/95";
38 #else
39 __RCSID("$NetBSD: var.c,v 1.36 2004/10/06 10:23:43 enami Exp $");
40 #endif /* not lint */
41 #endif
43 #include <stddef.h>
44 #include <stdlib.h>
45 #include <string.h>
47 #ifdef PC_OS2_LIBPATHS
48 #define INCL_BASE
49 #include <os2.h>
51 #ifndef LIBPATHSTRICT
52 #define LIBPATHSTRICT 3
53 #endif
55 extern APIRET
56 #ifdef APIENTRY
57 APIENTRY
58 #endif
59 DosQueryHeaderInfo(HMODULE hmod, ULONG ulIndex, PVOID pvBuffer, ULONG cbBuffer, ULONG ulSubFunction);
60 #define QHINF_EXEINFO 1 /* NE exeinfo. */
61 #define QHINF_READRSRCTBL 2 /* Reads from the resource table. */
62 #define QHINF_READFILE 3 /* Reads from the executable file. */
63 #define QHINF_LIBPATHLENGTH 4 /* Gets the libpath length. */
64 #define QHINF_LIBPATH 5 /* Gets the entire libpath. */
65 #define QHINF_FIXENTRY 6 /* NE only */
66 #define QHINF_STE 7 /* NE only */
67 #define QHINF_MAPSEL 8 /* NE only */
69 #endif
74 * Shell variables.
77 #include "shell.h"
78 #include "output.h"
79 #include "expand.h"
80 #include "nodes.h" /* for other headers */
81 #include "eval.h" /* defines cmdenviron */
82 #include "exec.h"
83 #include "syntax.h"
84 #include "options.h"
85 #include "mail.h"
86 #include "var.h"
87 #include "memalloc.h"
88 #include "error.h"
89 #include "mystring.h"
90 #include "parser.h"
91 #include "show.h"
92 #ifndef SMALL
93 # include "myhistedit.h"
94 #endif
95 #include "shinstance.h"
97 //#ifdef SMALL
98 //#define VTABSIZE 39
99 //#else
100 //#define VTABSIZE 517
101 //#endif
104 struct varinit {
105 unsigned var_off;
106 int flags;
107 const char *text;
108 void (*func)(shinstance *, const char *);
112 //#if ATTY
113 //struct var vatty;
114 //#endif
115 //#ifndef SMALL
116 //struct var vhistsize;
117 //struct var vterm;
118 //#endif
119 //struct var vifs;
120 //struct var vmail;
121 //struct var vmpath;
122 //struct var vpath;
123 //#ifdef _MSC_VER
124 //struct var vpath2;
125 //#endif
126 //struct var vps1;
127 //struct var vps2;
128 //struct var vps4;
129 //struct var vvers; - unused
130 //struct var voptind;
132 #ifdef PC_OS2_LIBPATHS
133 //static struct var libpath_vars[4];
134 static const char * const libpath_envs[4] = {"LIBPATH=", "BEGINLIBPATH=", "ENDLIBPATH=", "LIBPATHSTRICT="};
135 #endif
137 const struct varinit varinit[] = {
138 #if ATTY
139 { offsetof(shinstance, vatty), VSTRFIXED|VSTRFIXED2|VTEXTFIXED|VUNSET, "ATTY=",
140 NULL },
141 #endif
142 #ifndef SMALL
143 { offsetof(shinstance, vhistsize), VSTRFIXED|VSTRFIXED2|VTEXTFIXED|VUNSET, "HISTSIZE=",
144 sethistsize },
145 #endif
146 { offsetof(shinstance, vifs), VSTRFIXED|VSTRFIXED2|VTEXTFIXED, "IFS= \t\n",
147 NULL },
148 { offsetof(shinstance, vmail), VSTRFIXED|VSTRFIXED2|VTEXTFIXED|VUNSET, "MAIL=",
149 NULL },
150 { offsetof(shinstance, vmpath), VSTRFIXED|VSTRFIXED2|VTEXTFIXED|VUNSET, "MAILPATH=",
151 NULL },
152 { offsetof(shinstance, vpath), VSTRFIXED|VSTRFIXED2|VTEXTFIXED, "PATH=" _PATH_DEFPATH,
153 changepath },
155 * vps1 depends on uid
157 { offsetof(shinstance, vps2), VSTRFIXED|VSTRFIXED2|VTEXTFIXED, "PS2=> ",
158 NULL },
159 { offsetof(shinstance, vps4), VSTRFIXED|VSTRFIXED2|VTEXTFIXED, "PS4=+ ",
160 NULL },
161 #ifndef SMALL
162 { offsetof(shinstance, vterm), VSTRFIXED|VSTRFIXED2|VTEXTFIXED|VUNSET, "TERM=",
163 setterm },
164 #endif
165 { offsetof(shinstance, voptind), VSTRFIXED|VSTRFIXED2|VTEXTFIXED|VNOFUNC, "OPTIND=1",
166 getoptsreset },
167 { 0, 0, NULL,
168 NULL }
171 //struct var *vartab[VTABSIZE];
173 STATIC int strequal(const char *, const char *);
174 STATIC struct var *find_var(shinstance *, const char *, struct var ***, int *);
177 * Initialize the varable symbol tables and import the environment
180 #ifdef mkinit
181 INCLUDE "var.h"
183 INIT {
184 char **envp;
186 initvar(psh);
187 for (envp = sh_environ(psh) ; *envp ; envp++) {
188 if (strchr(*envp, '=')) {
189 setvareq(psh, *envp, VEXPORT|VTEXTFIXED);
193 #endif
197 * This routine initializes the builtin variables. It is called when the
198 * shell is initialized and again when a shell procedure is spawned.
201 void
202 initvar(shinstance *psh)
204 const struct varinit *ip;
205 struct var *vp;
206 struct var **vpp;
207 #ifdef PC_OS2_LIBPATHS
208 char *psz = ckmalloc(psh, 2048);
209 int rc;
210 int i;
212 for (i = 0; i < 4; i++)
214 psh->libpath_vars[i].flags = VSTRFIXED | VSTRFIXED2 | VOS2LIBPATH;
215 psh->libpath_vars[i].func = NULL;
217 if (i > 0)
219 psz[0] = psz[1] = psz[2] = psz[3] = '\0';
220 rc = DosQueryExtLIBPATH(psz, i);
222 else
224 rc = DosQueryHeaderInfo(NULLHANDLE, 0, psz, 2048, QHINF_LIBPATH);
225 psh->libpath_vars[i].flags |= VREADONLY;
227 if (!rc && *psz)
229 int cch1 = strlen(libpath_envs[i]);
230 int cch2 = strlen(psz) + 1;
231 psh->libpath_vars[i].text = ckmalloc(psh, cch1 + cch2);
232 memcpy(psh->libpath_vars[i].text, libpath_envs[i], cch1);
233 memcpy(psh->libpath_vars[i].text + cch1, psz, cch2);
235 else
237 psh->libpath_vars[i].flags |= VUNSET | VTEXTFIXED;
238 psh->libpath_vars[i].text = (char *)libpath_envs[i];
240 if (find_var(psh, psh->libpath_vars[i].text, &vpp, &psh->libpath_vars[i].name_len) != NULL)
241 continue;
242 psh->libpath_vars[i].next = *vpp;
243 *vpp = &psh->libpath_vars[i];
245 ckfree(psh, psz);
246 #endif
248 for (ip = varinit; ip->text; ip++) {
249 vp = (struct var *)((char *)psh + ip->var_off);
250 if (find_var(psh, ip->text, &vpp, &vp->name_len) != NULL)
251 continue;
252 vp->next = *vpp;
253 *vpp = vp;
254 vp->text = sh_strdup(psh, ip->text);
255 vp->flags = ip->flags;
256 vp->func = ip->func;
259 * PS1 depends on uid
261 if (find_var(psh, "PS1", &vpp, &psh->vps1.name_len) == NULL) {
262 psh->vps1.next = *vpp;
263 *vpp = &psh->vps1;
264 #ifdef KBUILD_VERSION_MAJOR
265 psh->vps1.text = sh_strdup(psh, sh_geteuid(psh) ? "PS1=kash$ " : "PS1=kash# ");
266 #else
267 psh->vps1.text = sh_strdup(psh, sh_geteuid(psh) ? "PS1=$ " : "PS1=# ");
268 #endif
269 psh->vps1.flags = VSTRFIXED|VSTRFIXED2|VTEXTFIXED; /** @todo text isn't fixed here... */
274 #ifndef SH_FORKED_MODE
276 * This routine is called to copy variable state from parent to child shell.
278 void
279 subshellinitvar(shinstance *psh, shinstance *inherit)
281 unsigned i;
282 for (i = 0; i < K_ELEMENTS(inherit->vartab); i++) {
283 struct var const *vsrc = inherit->vartab[i];
284 if (vsrc) {
285 struct var **ppdst = &psh->vartab[i];
288 struct var *dst;
289 if (!(vsrc->flags & VSTRFIXED2)) {
290 dst = (struct var *)ckmalloc(psh, sizeof(*dst));
291 *dst = *vsrc;
292 dst->flags &= ~VSTRFIXED;
293 } else {
294 /* VSTRFIXED2 is used when the structure is a fixed allocation in
295 the shinstance structure, so scan those to find which it is: */
296 size_t left = ((struct var *)&inherit->vartab[0] - &inherit->vatty);
297 struct var const *fixedsrc = &inherit->vatty;
298 dst = &psh->vatty;
299 while (left-- > 0)
300 if (vsrc != fixedsrc) {
301 fixedsrc++;
302 dst++;
303 } else
304 break;
305 kHlpAssert(left < 256 /*whatever, just no rollover*/);
306 *dst = *vsrc;
309 if (!(vsrc->flags & VTEXTFIXED)) {
310 dst->text = savestr(psh, vsrc->text);
311 dst->flags &= ~VSTACK;
314 *ppdst = dst;
315 ppdst = &dst->next;
317 vsrc = vsrc->next;
318 } while (vsrc);
319 *ppdst = NULL;
323 /** @todo We don't always need to copy local variables. */
324 if (inherit->localvars) {
325 struct localvar const *vsrc = inherit->localvars;
326 struct localvar **ppdst = &psh->localvars;
329 struct localvar *dst = ckmalloc(psh, sizeof(*dst));
331 dst->flags = vsrc->flags & ~(VSTACK | VTEXTFIXED | (vsrc->flags & VSTRFIXED2 ? 0 : VSTRFIXED));
332 if (vsrc->text)
333 dst->text = savestr(psh, vsrc->text);
334 else
336 dst->text = NULL;
337 dst->flags |= vsrc->flags & VTEXTFIXED;
339 dst->vp = find_var(psh, vsrc->vp->text, NULL, NULL);
340 kHlpAssert(dst->vp);
342 *ppdst = dst;
343 ppdst = &dst->next;
345 vsrc = vsrc->next;
346 } while (vsrc);
347 *ppdst = NULL;
350 #endif /* !SH_FORKED_MODE */
353 * Safe version of setvar, returns 1 on success 0 on failure.
357 setvarsafe(shinstance *psh, const char *name, const char *val, int flags)
359 struct jmploc jmploc;
360 struct jmploc *volatile savehandler = psh->handler;
361 int err = 0;
362 #ifdef __GNUC__
363 (void) &err;
364 #endif
366 if (setjmp(jmploc.loc))
367 err = 1;
368 else {
369 psh->handler = &jmploc;
370 setvar(psh, name, val, flags);
372 psh->handler = savehandler;
373 return err;
377 * Set the value of a variable. The flags argument is ored with the
378 * flags of the variable. If val is NULL, the variable is unset.
381 void
382 setvar(shinstance *psh, const char *name, const char *val, int flags)
384 const char *p;
385 const char *q;
386 char *d;
387 size_t len;
388 int namelen;
389 char *nameeq;
390 int isbad;
392 isbad = 0;
393 p = name;
394 if (! is_name(*p))
395 isbad = 1;
396 p++;
397 for (;;) {
398 if (! is_in_name(*p)) {
399 if (*p == '\0' || *p == '=')
400 break;
401 isbad = 1;
403 p++;
405 namelen = (int)(p - name);
406 if (isbad)
407 error(psh, "%.*s: bad variable name", namelen, name);
408 len = namelen + 2; /* 2 is space for '=' and '\0' */
409 if (val == NULL) {
410 flags |= VUNSET;
411 } else {
412 len += strlen(val);
414 d = nameeq = ckmalloc(psh, len);
415 q = name;
416 while (--namelen >= 0)
417 *d++ = *q++;
418 *d++ = '=';
419 *d = '\0';
420 if (val)
421 scopy(val, d);
422 setvareq(psh, nameeq, flags);
428 * Same as setvar except that the variable and value are passed in
429 * the first argument as name=value. Since the first argument will
430 * be actually stored in the table, it should not be a string that
431 * will go away.
434 void
435 setvareq(shinstance *psh, char *s, int flags)
437 struct var *vp, **vpp;
438 int nlen;
440 #if defined(_MSC_VER) || defined(_WIN32)
441 /* On Windows PATH is often spelled 'Path', correct this here. */
442 if ( s[0] == 'P'
443 && s[1] == 'a'
444 && s[2] == 't'
445 && s[3] == 'h'
446 && (s[4] == '\0' || s[4] == '=') ) {
447 s[1] = 'A';
448 s[2] = 'T';
449 s[3] = 'H';
451 #endif
453 if (aflag(psh))
454 flags |= VEXPORT;
455 vp = find_var(psh, s, &vpp, &nlen);
456 if (vp != NULL) {
457 if (vp->flags & VREADONLY)
458 error(psh, "%.*s: is read only", vp->name_len, s);
459 if (flags & VNOSET)
460 return;
461 INTOFF;
463 if (vp->func && (flags & VNOFUNC) == 0)
464 (*vp->func)(psh, s + vp->name_len + 1);
466 if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0)
467 ckfree(psh, vp->text);
469 vp->flags &= ~(VTEXTFIXED|VSTACK|VUNSET);
470 vp->flags |= flags & ~VNOFUNC;
471 vp->text = s;
472 #ifdef PC_OS2_LIBPATHS
473 if ((vp->flags & VOS2LIBPATH) && (vp->flags & VEXPORT))
474 vp->flags &= ~VEXPORT;
475 #endif
478 * We could roll this to a function, to handle it as
479 * a regular variable function callback, but why bother?
481 if (vp == &psh->vmpath || (vp == &psh->vmail && ! mpathset(psh)))
482 chkmail(psh, 1);
483 INTON;
484 return;
486 /* not found */
487 if (flags & VNOSET)
488 return;
490 vp = ckmalloc(psh, sizeof (*vp));
491 vp->flags = flags & ~VNOFUNC;
492 vp->text = s;
493 vp->name_len = nlen;
494 vp->next = *vpp;
495 vp->func = NULL;
496 *vpp = vp;
502 * Process a linked list of variable assignments.
505 void
506 listsetvar(shinstance *psh, struct strlist *list, int flags)
508 struct strlist *lp;
510 INTOFF;
511 for (lp = list ; lp ; lp = lp->next) {
512 setvareq(psh, savestr(psh, lp->text), flags);
514 INTON;
517 void
518 listmklocal(shinstance *psh, struct strlist *list, int flags)
520 struct strlist *lp;
522 for (lp = list ; lp ; lp = lp->next)
523 mklocal(psh, lp->text, flags);
528 * Find the value of a variable. Returns NULL if not set.
531 char *
532 lookupvar(shinstance *psh, const char *name)
534 struct var *v;
536 v = find_var(psh, name, NULL, NULL);
537 if (v == NULL || v->flags & VUNSET)
538 return NULL;
539 return v->text + v->name_len + 1;
545 * Search the environment of a builtin command. If the second argument
546 * is nonzero, return the value of a variable even if it hasn't been
547 * exported.
550 char *
551 bltinlookup(shinstance *psh, const char *name, int doall)
553 struct strlist *sp;
554 struct var *v;
556 for (sp = psh->cmdenviron ; sp ; sp = sp->next) {
557 if (strequal(sp->text, name))
558 return strchr(sp->text, '=') + 1;
561 v = find_var(psh, name, NULL, NULL);
563 if (v == NULL || v->flags & VUNSET || (!doall && !(v->flags & VEXPORT)))
564 return NULL;
565 return v->text + v->name_len + 1;
571 * Generate a list of exported variables. This routine is used to construct
572 * the third argument to execve when executing a program.
575 char **
576 environment(shinstance *psh)
578 int nenv;
579 struct var **vpp;
580 struct var *vp;
581 char **env;
582 char **ep;
584 nenv = 0;
585 for (vpp = psh->vartab ; vpp < psh->vartab + VTABSIZE ; vpp++) {
586 for (vp = *vpp ; vp ; vp = vp->next)
587 if (vp->flags & VEXPORT)
588 nenv++;
590 ep = env = stalloc(psh, (nenv + 1) * sizeof *env);
591 for (vpp = psh->vartab ; vpp < psh->vartab + VTABSIZE ; vpp++) {
592 for (vp = *vpp ; vp ; vp = vp->next)
593 if (vp->flags & VEXPORT)
594 *ep++ = vp->text;
596 *ep = NULL;
598 #ifdef PC_OS2_LIBPATHS
600 * Set the libpaths now as this is exec() time.
602 for (nenv = 0; nenv < 3; nenv++)
603 DosSetExtLIBPATH(strchr(psh->libpath_vars[nenv].text, '=') + 1, nenv);
604 #endif
606 return env;
611 * Called when a shell procedure is invoked to clear out nonexported
612 * variables. It is also necessary to reallocate variables of with
613 * VSTACK set since these are currently allocated on the stack.
616 #ifdef mkinit
617 void shprocvar(shinstance *psh);
619 SHELLPROC {
620 shprocvar(psh);
622 #endif
624 void
625 shprocvar(shinstance *psh)
627 struct var **vpp;
628 struct var *vp, **prev;
630 for (vpp = psh->vartab ; vpp < psh->vartab + VTABSIZE ; vpp++) {
631 for (prev = vpp ; (vp = *prev) != NULL ; ) {
632 if ((vp->flags & VEXPORT) == 0) {
633 *prev = vp->next;
634 if ((vp->flags & VTEXTFIXED) == 0)
635 ckfree(psh, vp->text);
636 if ((vp->flags & (VSTRFIXED | VSTRFIXED2)) == 0)
637 ckfree(psh, vp);
638 } else {
639 if (vp->flags & VSTACK) {
640 vp->text = savestr(psh, vp->text);
641 vp->flags &=~ VSTACK;
643 prev = &vp->next;
647 initvar(psh);
653 * Command to list all variables which are set. Currently this command
654 * is invoked from the set command when the set command is called without
655 * any variables.
658 void
659 print_quoted(shinstance *psh, const char *p)
661 const char *q;
663 if (strcspn(p, "|&;<>()$`\\\"' \t\n*?[]#~=%") == strlen(p)) {
664 out1fmt(psh, "%s", p);
665 return;
667 while (*p) {
668 if (*p == '\'') {
669 out1fmt(psh, "\\'");
670 p++;
671 continue;
673 q = strchr(p, '\'');
674 if (!q) {
675 out1fmt(psh, "'%s'", p );
676 return;
678 out1fmt(psh, "'%.*s'", (int)(q - p), p );
679 p = q;
683 static int
684 sort_var(const void *v_v1, const void *v_v2)
686 const struct var * const *v1 = v_v1;
687 const struct var * const *v2 = v_v2;
689 /* XXX Will anyone notice we include the '=' of the shorter name? */
690 return strcoll((*v1)->text, (*v2)->text);
694 * POSIX requires that 'set' (but not export or readonly) output the
695 * variables in lexicographic order - by the locale's collating order (sigh).
696 * Maybe we could keep them in an ordered balanced binary tree
697 * instead of hashed lists.
698 * For now just roll 'em through qsort for printing...
702 showvars(shinstance *psh, const char *name, int flag, int show_value)
704 struct var **vpp;
705 struct var *vp;
706 const char *p;
708 static struct var **list; /* static in case we are interrupted */
709 static int list_len;
710 int count = 0;
712 if (!list) {
713 list_len = 32;
714 list = ckmalloc(psh, list_len * sizeof(*list));
717 for (vpp = psh->vartab ; vpp < psh->vartab + VTABSIZE ; vpp++) {
718 for (vp = *vpp ; vp ; vp = vp->next) {
719 if (flag && !(vp->flags & flag))
720 continue;
721 if (vp->flags & VUNSET && !(show_value & 2))
722 continue;
723 if (count >= list_len) {
724 list = ckrealloc(psh, list,
725 (list_len << 1) * sizeof(*list));
726 list_len <<= 1;
728 list[count++] = vp;
732 qsort(list, count, sizeof(*list), sort_var);
734 for (vpp = list; count--; vpp++) {
735 vp = *vpp;
736 if (name)
737 out1fmt(psh, "%s ", name);
738 for (p = vp->text ; *p != '=' ; p++)
739 out1c(psh, *p);
740 if (!(vp->flags & VUNSET) && show_value) {
741 out1fmt(psh, "=");
742 print_quoted(psh, ++p);
744 out1c(psh, '\n');
746 return 0;
752 * The export and readonly commands.
756 exportcmd(shinstance *psh, int argc, char **argv)
758 struct var *vp;
759 char *name;
760 const char *p;
761 int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT;
762 int pflag;
764 pflag = nextopt(psh, "p") == 'p' ? 3 : 0;
765 if (argc <= 1 || pflag) {
766 showvars(psh, pflag ? argv[0] : 0, flag, pflag );
767 return 0;
770 while ((name = *psh->argptr++) != NULL) {
771 if ((p = strchr(name, '=')) != NULL) {
772 p++;
773 } else {
774 vp = find_var(psh, name, NULL, NULL);
775 if (vp != NULL) {
776 vp->flags |= flag;
777 continue;
780 setvar(psh, name, p, flag);
782 return 0;
787 * The "local" command.
791 localcmd(shinstance *psh, int argc, char **argv)
793 char *name;
795 if (! in_function(psh))
796 error(psh, "Not in a function");
797 while ((name = *psh->argptr++) != NULL) {
798 mklocal(psh, name, 0);
800 return 0;
805 * Make a variable a local variable. When a variable is made local, it's
806 * value and flags are saved in a localvar structure. The saved values
807 * will be restored when the shell function returns. We handle the name
808 * "-" as a special case.
811 void
812 mklocal(shinstance *psh, const char *name, int flags)
814 struct localvar *lvp;
815 struct var **vpp;
816 struct var *vp;
818 INTOFF;
819 lvp = ckmalloc(psh, sizeof (struct localvar));
820 if (name[0] == '-' && name[1] == '\0') {
821 char *p;
822 p = ckmalloc(psh, sizeof_optlist);
823 lvp->text = memcpy(p, psh->optlist, sizeof_optlist);
824 vp = NULL;
825 } else {
826 vp = find_var(psh, name, &vpp, NULL);
827 if (vp == NULL) {
828 if (strchr(name, '='))
829 setvareq(psh, savestr(psh, name), VSTRFIXED|flags);
830 else
831 setvar(psh, name, NULL, VSTRFIXED|flags);
832 vp = *vpp; /* the new variable */
833 lvp->text = NULL;
834 lvp->flags = VUNSET;
835 } else {
836 lvp->text = vp->text;
837 lvp->flags = vp->flags;
838 vp->flags |= VSTRFIXED|VTEXTFIXED;
839 if (name[vp->name_len] == '=')
840 setvareq(psh, savestr(psh, name), flags);
843 lvp->vp = vp;
844 lvp->next = psh->localvars;
845 psh->localvars = lvp;
846 INTON;
851 * Called after a function returns.
854 void
855 poplocalvars(shinstance *psh)
857 struct localvar *lvp;
858 struct var *vp;
860 while ((lvp = psh->localvars) != NULL) {
861 psh->localvars = lvp->next;
862 vp = lvp->vp;
863 TRACE((psh, "poplocalvar %s", vp ? vp->text : "-"));
864 if (vp == NULL) { /* $- saved */
865 memcpy(psh->optlist, lvp->text, sizeof_optlist);
866 ckfree(psh, lvp->text);
867 } else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) {
868 (void)unsetvar(psh, vp->text, 0);
869 } else {
870 if (vp->func && (vp->flags & VNOFUNC) == 0)
871 (*vp->func)(psh, lvp->text + vp->name_len + 1);
872 if ((vp->flags & VTEXTFIXED) == 0)
873 ckfree(psh, vp->text);
874 vp->flags = lvp->flags;
875 vp->text = lvp->text;
877 ckfree(psh, lvp);
883 setvarcmd(shinstance *psh, int argc, char **argv)
885 if (argc <= 2)
886 return unsetcmd(psh, argc, argv);
887 else if (argc == 3)
888 setvar(psh, argv[1], argv[2], 0);
889 else
890 error(psh, "List assignment not implemented");
891 return 0;
896 * The unset builtin command. We unset the function before we unset the
897 * variable to allow a function to be unset when there is a readonly variable
898 * with the same name.
902 unsetcmd(shinstance *psh, int argc, char **argv)
904 char **ap;
905 int i;
906 int flg_func = 0;
907 int flg_var = 0;
908 int ret = 0;
910 while ((i = nextopt(psh, "evf")) != '\0') {
911 if (i == 'f')
912 flg_func = 1;
913 else
914 flg_var = i;
916 if (flg_func == 0 && flg_var == 0)
917 flg_var = 1;
919 for (ap = psh->argptr; *ap ; ap++) {
920 if (flg_func)
921 ret |= unsetfunc(psh, *ap);
922 if (flg_var)
923 ret |= unsetvar(psh, *ap, flg_var == 'e');
925 return ret;
930 * Unset the specified variable.
934 unsetvar(shinstance *psh, const char *s, int unexport)
936 struct var **vpp;
937 struct var *vp;
939 vp = find_var(psh, s, &vpp, NULL);
940 if (vp == NULL)
941 return 1;
943 if (vp->flags & VREADONLY)
944 return (1);
946 INTOFF;
947 if (unexport) {
948 vp->flags &= ~VEXPORT;
949 } else {
950 if (vp->text[vp->name_len + 1] != '\0')
951 setvar(psh, s, nullstr, 0);
952 vp->flags &= ~VEXPORT;
953 vp->flags |= VUNSET;
954 if ((vp->flags & (VSTRFIXED | VSTRFIXED2)) == 0) {
955 if ((vp->flags & VTEXTFIXED) == 0)
956 ckfree(psh, vp->text);
957 *vpp = vp->next;
958 ckfree(psh, vp);
961 INTON;
962 return 0;
967 * Returns true if the two strings specify the same varable. The first
968 * variable name is terminated by '='; the second may be terminated by
969 * either '=' or '\0'.
972 STATIC int
973 strequal(const char *p, const char *q)
975 while (*p == *q++) {
976 if (*p++ == '=')
977 return 1;
979 if (*p == '=' && *(q - 1) == '\0')
980 return 1;
981 return 0;
985 * Search for a variable.
986 * 'name' may be terminated by '=' or a NUL.
987 * vppp is set to the pointer to vp, or the list head if vp isn't found
988 * lenp is set to the number of charactets in 'name'
991 STATIC struct var *
992 find_var(shinstance *psh, const char *name, struct var ***vppp, int *lenp)
994 unsigned int hashval;
995 int len;
996 struct var *vp, **vpp;
997 const char *p = name;
999 hashval = 0;
1000 while (*p && *p != '=')
1001 hashval = 2 * hashval + (unsigned char)*p++;
1002 len = (int)(p - name);
1004 if (lenp)
1005 *lenp = len;
1006 vpp = &psh->vartab[hashval % VTABSIZE];
1007 if (vppp)
1008 *vppp = vpp;
1010 for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) {
1011 if (vp->name_len != len)
1012 continue;
1013 if (memcmp(vp->text, name, len) != 0)
1014 continue;
1015 if (vppp)
1016 *vppp = vpp;
1017 return vp;
1019 return NULL;