2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 static char sccsid[] = "@(#)var.c 8.3 (Berkeley) 5/4/95";
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD: src/bin/sh/var.c,v 1.15.2.2 2002/08/27 01:36:28 tjr Exp $");
57 #include "nodes.h" /* for other headers */
58 #include "eval.h" /* defines cmdenviron */
69 #include "myhistedit.h"
80 void (*func)(const char *);
103 const struct varinit varinit[] = {
105 { &vatty, VSTRFIXED|VTEXTFIXED|VUNSET, "ATTY=",
109 { &vhistsize, VSTRFIXED|VTEXTFIXED|VUNSET, "HISTSIZE=",
112 { &vifs, VSTRFIXED|VTEXTFIXED, "IFS= \t\n",
114 { &vmail, VSTRFIXED|VTEXTFIXED|VUNSET, "MAIL=",
116 { &vmpath, VSTRFIXED|VTEXTFIXED|VUNSET, "MAILPATH=",
118 { &vpath, VSTRFIXED|VTEXTFIXED, "PATH=/bin:/usr/bin",
120 { &vppid, VSTRFIXED|VTEXTFIXED|VUNSET, "PPID=",
123 * vps1 depends on uid
125 { &vps2, VSTRFIXED|VTEXTFIXED, "PS2=> ",
128 { &vterm, VSTRFIXED|VTEXTFIXED|VUNSET, "TERM=",
131 { &voptind, VSTRFIXED|VTEXTFIXED, "OPTIND=1",
137 struct var *vartab[VTABSIZE];
139 STATIC struct var **hashvar(char *);
140 STATIC int varequal(char *, char *);
141 STATIC int localevar(char *);
144 * Initialize the varable symbol tables and import the environment
151 extern char **environ;
154 for (envp = environ ; *envp ; envp++) {
155 if (strchr(*envp, '=')) {
156 setvareq(*envp, VEXPORT|VTEXTFIXED);
164 * This routine initializes the builtin variables. It is called when the
165 * shell is initialized and again when a shell procedure is spawned.
172 const struct varinit *ip;
176 for (ip = varinit ; (vp = ip->var) != NULL ; ip++) {
177 if ((vp->flags & VEXPORT) == 0) {
178 vpp = hashvar(ip->text);
182 vp->flags = ip->flags;
189 if ((vps1.flags & VEXPORT) == 0) {
190 vpp = hashvar("PS1=");
193 vps1.text = geteuid() ? "PS1=$ " : "PS1=# ";
194 vps1.flags = VSTRFIXED|VTEXTFIXED;
196 if ((vppid.flags & VEXPORT) == 0) {
197 fmtstr(ppid, sizeof(ppid), "%d", (int)getppid());
198 setvarsafe("PPID", ppid, 0);
203 * Safe version of setvar, returns 1 on success 0 on failure.
207 setvarsafe(char *name, char *val, int flags)
209 struct jmploc jmploc;
210 struct jmploc *volatile savehandler = handler;
213 /* Avoid longjmp clobbering */
217 if (setjmp(jmploc.loc))
221 setvar(name, val, flags);
223 handler = savehandler;
228 * Set the value of a variable. The flags argument is tored with the
229 * flags of the variable. If val is NULL, the variable is unset.
233 setvar(char *name, char *val, int flags)
247 if (! is_in_name(*p)) {
248 if (*p == '\0' || *p == '=')
256 error("%.*s: bad variable name", namelen, name);
257 len = namelen + 2; /* 2 is space for '=' and '\0' */
263 p = nameeq = ckmalloc(len);
265 while (--namelen >= 0)
271 setvareq(nameeq, flags);
277 static char *lnames[7] = {
278 "ALL", "COLLATE", "CTYPE", "MONETARY",
279 "NUMERIC", "TIME", NULL
285 if (varequal(s + 1, "ANG"))
287 if (strncmp(s + 1, "C_", 2) != 0)
289 for (ss = lnames; *ss ; ss++)
290 if (varequal(s + 3, *ss))
296 * Same as setvar except that the variable and value are passed in
297 * the first argument as name=value. Since the first argument will
298 * be actually stored in the table, it should not be a string that
303 setvareq(char *s, int flags)
305 struct var *vp, **vpp;
311 for (vp = *vpp ; vp ; vp = vp->next) {
312 if (varequal(s, vp->text)) {
313 if (vp->flags & VREADONLY) {
314 len = strchr(s, '=') - s;
315 error("%.*s: is read only", len, s);
319 if (vp->func && (flags & VNOFUNC) == 0)
320 (*vp->func)(strchr(s, '=') + 1);
322 if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0)
325 vp->flags &= ~(VTEXTFIXED|VSTACK|VUNSET);
330 * We could roll this to a function, to handle it as
331 * a regular variable function callback, but why bother?
333 if (vp == &vmpath || (vp == &vmail && ! mpathset()))
335 if ((vp->flags & VEXPORT) && localevar(s)) {
337 (void) setlocale(LC_ALL, "");
344 vp = ckmalloc(sizeof (*vp));
351 if ((vp->flags & VEXPORT) && localevar(s)) {
353 (void) setlocale(LC_ALL, "");
361 * Process a linked list of variable assignments.
365 listsetvar(struct strlist *list)
370 for (lp = list ; lp ; lp = lp->next) {
371 setvareq(savestr(lp->text), 0);
379 * Find the value of a variable. Returns NULL if not set.
383 lookupvar(char *name)
387 for (v = *hashvar(name) ; v ; v = v->next) {
388 if (varequal(v->text, name)) {
389 if (v->flags & VUNSET)
391 return strchr(v->text, '=') + 1;
400 * Search the environment of a builtin command. If the second argument
401 * is nonzero, return the value of a variable even if it hasn't been
406 bltinlookup(char *name, int doall)
411 for (sp = cmdenviron ; sp ; sp = sp->next) {
412 if (varequal(sp->text, name))
413 return strchr(sp->text, '=') + 1;
415 for (v = *hashvar(name) ; v ; v = v->next) {
416 if (varequal(v->text, name)) {
417 if ((v->flags & VUNSET)
418 || (!doall && (v->flags & VEXPORT) == 0))
420 return strchr(v->text, '=') + 1;
429 * Generate a list of exported variables. This routine is used to construct
430 * the third argument to execve when executing a program.
442 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
443 for (vp = *vpp ; vp ; vp = vp->next)
444 if (vp->flags & VEXPORT)
447 ep = env = stalloc((nenv + 1) * sizeof *env);
448 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
449 for (vp = *vpp ; vp ; vp = vp->next)
450 if (vp->flags & VEXPORT)
459 * Called when a shell procedure is invoked to clear out nonexported
460 * variables. It is also necessary to reallocate variables of with
461 * VSTACK set since these are currently allocated on the stack.
465 MKINIT void shprocvar();
476 struct var *vp, **prev;
478 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
479 for (prev = vpp ; (vp = *prev) != NULL ; ) {
480 if ((vp->flags & VEXPORT) == 0) {
482 if ((vp->flags & VTEXTFIXED) == 0)
484 if ((vp->flags & VSTRFIXED) == 0)
487 if (vp->flags & VSTACK) {
488 vp->text = savestr(vp->text);
489 vp->flags &=~ VSTACK;
501 * Command to list all variables which are set. Currently this command
502 * is invoked from the set command when the set command is called without
507 showvarscmd(int argc __unused, char **argv __unused)
513 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
514 for (vp = *vpp ; vp ; vp = vp->next) {
515 if (vp->flags & VUNSET)
517 for (s = vp->text; *s != '='; s++)
530 * The export and readonly commands.
534 exportcmd(int argc, char **argv)
542 int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT;
545 optreset = optind = 1;
548 while ((ch = getopt(argc, argv, "p")) != -1) {
555 error("unknown option: -%c", optopt);
561 listsetvar(cmdenviron);
563 while ((name = *argptr++) != NULL) {
564 if ((p = strchr(name, '=')) != NULL) {
568 for (vp = *vpp ; vp ; vp = vp->next) {
569 if (varequal(vp->text, name)) {
572 if ((vp->flags & VEXPORT) && localevar(vp->text)) {
574 (void) setlocale(LC_ALL, "");
580 setvar(name, p, flag);
584 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
585 for (vp = *vpp ; vp ; vp = vp->next) {
586 if (vp->flags & flag) {
591 for (p = vp->text ; *p != '=' ; p++)
593 if (values && !(vp->flags & VUNSET)) {
607 * The "local" command.
611 localcmd(int argc __unused, char **argv __unused)
616 error("Not in a function");
617 while ((name = *argptr++) != NULL) {
625 * Make a variable a local variable. When a variable is made local, it's
626 * value and flags are saved in a localvar structure. The saved values
627 * will be restored when the shell function returns. We handle the name
628 * "-" as a special case.
634 struct localvar *lvp;
639 lvp = ckmalloc(sizeof (struct localvar));
640 if (name[0] == '-' && name[1] == '\0') {
641 lvp->text = ckmalloc(sizeof optlist);
642 memcpy(lvp->text, optlist, sizeof optlist);
646 for (vp = *vpp ; vp && ! varequal(vp->text, name) ; vp = vp->next);
648 if (strchr(name, '='))
649 setvareq(savestr(name), VSTRFIXED);
651 setvar(name, NULL, VSTRFIXED);
652 vp = *vpp; /* the new variable */
656 lvp->text = vp->text;
657 lvp->flags = vp->flags;
658 vp->flags |= VSTRFIXED|VTEXTFIXED;
659 if (strchr(name, '='))
660 setvareq(savestr(name), 0);
664 lvp->next = localvars;
671 * Called after a function returns.
677 struct localvar *lvp;
680 while ((lvp = localvars) != NULL) {
681 localvars = lvp->next;
683 if (vp == NULL) { /* $- saved */
684 memcpy(optlist, lvp->text, sizeof optlist);
686 } else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) {
687 (void)unsetvar(vp->text);
689 if ((vp->flags & VTEXTFIXED) == 0)
691 vp->flags = lvp->flags;
692 vp->text = lvp->text;
700 setvarcmd(int argc, char **argv)
703 return unsetcmd(argc, argv);
705 setvar(argv[1], argv[2], 0);
707 error("List assignment not implemented");
713 * The unset builtin command. We unset the function before we unset the
714 * variable to allow a function to be unset when there is a readonly variable
715 * with the same name.
719 unsetcmd(int argc __unused, char **argv __unused)
727 while ((i = nextopt("vf")) != '\0') {
733 if (flg_func == 0 && flg_var == 0)
736 for (ap = argptr; *ap ; ap++) {
738 ret |= unsetfunc(*ap);
740 ret |= unsetvar(*ap);
747 * Unset the specified variable.
757 for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) {
758 if (varequal(vp->text, s)) {
759 if (vp->flags & VREADONLY)
762 if (*(strchr(vp->text, '=') + 1) != '\0')
763 setvar(s, nullstr, 0);
764 if ((vp->flags & VEXPORT) && localevar(vp->text)) {
766 setlocale(LC_ALL, "");
768 vp->flags &= ~VEXPORT;
770 if ((vp->flags & VSTRFIXED) == 0) {
771 if ((vp->flags & VTEXTFIXED) == 0)
787 * Find the appropriate entry in the hash table from the name.
793 unsigned int hashval;
795 hashval = ((unsigned char) *p) << 4;
796 while (*p && *p != '=')
797 hashval += (unsigned char) *p++;
798 return &vartab[hashval % VTABSIZE];
804 * Returns true if the two strings specify the same varable. The first
805 * variable name is terminated by '='; the second may be terminated by
806 * either '=' or '\0'.
810 varequal(char *p, char *q)
816 if (*p == '=' && *(q - 1) == '\0')