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 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 static char sccsid[] = "@(#)var.c 8.3 (Berkeley) 5/4/95";
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
54 #include "nodes.h" /* for other headers */
55 #include "eval.h" /* defines cmdenviron */
66 #include "myhistedit.h"
77 void (*func)(const char *);
93 STATIC struct var voptind;
95 STATIC const struct varinit varinit[] = {
97 { &vhistsize, VSTRFIXED|VTEXTFIXED|VUNSET, "HISTSIZE=",
100 { &vifs, VSTRFIXED|VTEXTFIXED, "IFS= \t\n",
102 { &vmail, VSTRFIXED|VTEXTFIXED|VUNSET, "MAIL=",
104 { &vmpath, VSTRFIXED|VTEXTFIXED|VUNSET, "MAILPATH=",
106 { &vpath, VSTRFIXED|VTEXTFIXED, "PATH=" _PATH_DEFPATH,
108 { &vppid, VSTRFIXED|VTEXTFIXED|VUNSET, "PPID=",
111 * vps1 depends on uid
113 { &vps2, VSTRFIXED|VTEXTFIXED, "PS2=> ",
115 { &vps4, VSTRFIXED|VTEXTFIXED, "PS4=+ ",
117 { &voptind, VSTRFIXED|VTEXTFIXED, "OPTIND=1",
123 STATIC struct var *vartab[VTABSIZE];
125 STATIC struct var **hashvar(char *);
126 STATIC int varequal(char *, char *);
127 STATIC int localevar(char *);
130 * Initialize the variable symbol tables and import the environment.
137 extern char **environ;
140 for (envp = environ ; *envp ; envp++) {
141 if (strchr(*envp, '=')) {
142 setvareq(*envp, VEXPORT|VTEXTFIXED);
150 * This routine initializes the builtin variables. It is called when the
151 * shell is initialized and again when a shell procedure is spawned.
158 const struct varinit *ip;
162 for (ip = varinit ; (vp = ip->var) != NULL ; ip++) {
163 if ((vp->flags & VEXPORT) == 0) {
164 vpp = hashvar(ip->text);
168 vp->flags = ip->flags;
175 if ((vps1.flags & VEXPORT) == 0) {
176 vpp = hashvar("PS1=");
179 vps1.text = geteuid() ? "PS1=$ " : "PS1=# ";
180 vps1.flags = VSTRFIXED|VTEXTFIXED;
182 if ((vppid.flags & VEXPORT) == 0) {
183 fmtstr(ppid, sizeof(ppid), "%d", (int)getppid());
184 setvarsafe("PPID", ppid, 0);
189 * Safe version of setvar, returns 1 on success 0 on failure.
193 setvarsafe(char *name, char *val, int flags)
195 struct jmploc jmploc;
196 struct jmploc *volatile savehandler = handler;
199 /* Avoid longjmp clobbering */
203 if (setjmp(jmploc.loc))
207 setvar(name, val, flags);
209 handler = savehandler;
214 * Set the value of a variable. The flags argument is stored with the
215 * flags of the variable. If val is NULL, the variable is unset.
219 setvar(char *name, char *val, int flags)
233 if (! is_in_name(*p)) {
234 if (*p == '\0' || *p == '=')
242 error("%.*s: bad variable name", namelen, name);
243 len = namelen + 2; /* 2 is space for '=' and '\0' */
249 p = nameeq = ckmalloc(len);
251 while (--namelen >= 0)
257 setvareq(nameeq, flags);
263 static char *lnames[7] = {
264 "ALL", "COLLATE", "CTYPE", "MONETARY",
265 "NUMERIC", "TIME", NULL
271 if (varequal(s + 1, "ANG"))
273 if (strncmp(s + 1, "C_", 2) != 0)
275 for (ss = lnames; *ss ; ss++)
276 if (varequal(s + 3, *ss))
282 * Same as setvar except that the variable and value are passed in
283 * the first argument as name=value. Since the first argument will
284 * be actually stored in the table, it should not be a string that
289 setvareq(char *s, int flags)
291 struct var *vp, **vpp;
297 for (vp = *vpp ; vp ; vp = vp->next) {
298 if (varequal(s, vp->text)) {
299 if (vp->flags & VREADONLY) {
300 len = strchr(s, '=') - s;
301 error("%.*s: is read only", len, s);
305 if (vp->func && (flags & VNOFUNC) == 0)
306 (*vp->func)(strchr(s, '=') + 1);
308 if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0)
311 vp->flags &= ~(VTEXTFIXED|VSTACK|VUNSET);
316 * We could roll this to a function, to handle it as
317 * a regular variable function callback, but why bother?
319 if (vp == &vmpath || (vp == &vmail && ! mpathset()))
321 if ((vp->flags & VEXPORT) && localevar(s)) {
323 (void) setlocale(LC_ALL, "");
330 vp = ckmalloc(sizeof (*vp));
337 if ((vp->flags & VEXPORT) && localevar(s)) {
339 (void) setlocale(LC_ALL, "");
347 * Process a linked list of variable assignments.
351 listsetvar(struct strlist *list)
356 for (lp = list ; lp ; lp = lp->next) {
357 setvareq(savestr(lp->text), 0);
365 * Find the value of a variable. Returns NULL if not set.
369 lookupvar(char *name)
373 for (v = *hashvar(name) ; v ; v = v->next) {
374 if (varequal(v->text, name)) {
375 if (v->flags & VUNSET)
377 return strchr(v->text, '=') + 1;
386 * Search the environment of a builtin command. If the second argument
387 * is nonzero, return the value of a variable even if it hasn't been
392 bltinlookup(char *name, int doall)
397 for (sp = cmdenviron ; sp ; sp = sp->next) {
398 if (varequal(sp->text, name))
399 return strchr(sp->text, '=') + 1;
401 for (v = *hashvar(name) ; v ; v = v->next) {
402 if (varequal(v->text, name)) {
403 if ((v->flags & VUNSET)
404 || (!doall && (v->flags & VEXPORT) == 0))
406 return strchr(v->text, '=') + 1;
415 * Generate a list of exported variables. This routine is used to construct
416 * the third argument to execve when executing a program.
428 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
429 for (vp = *vpp ; vp ; vp = vp->next)
430 if (vp->flags & VEXPORT)
433 ep = env = stalloc((nenv + 1) * sizeof *env);
434 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
435 for (vp = *vpp ; vp ; vp = vp->next)
436 if (vp->flags & VEXPORT)
445 * Called when a shell procedure is invoked to clear out nonexported
446 * variables. It is also necessary to reallocate variables of with
447 * VSTACK set since these are currently allocated on the stack.
451 MKINIT void shprocvar(void);
462 struct var *vp, **prev;
464 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
465 for (prev = vpp ; (vp = *prev) != NULL ; ) {
466 if ((vp->flags & VEXPORT) == 0) {
468 if ((vp->flags & VTEXTFIXED) == 0)
470 if ((vp->flags & VSTRFIXED) == 0)
473 if (vp->flags & VSTACK) {
474 vp->text = savestr(vp->text);
475 vp->flags &=~ VSTACK;
486 var_compare(const void *a, const void *b)
488 const char *const *sa, *const *sb;
493 * This compares two var=value strings which creates a different
494 * order from what you would probably expect. POSIX is somewhat
495 * ambiguous on what should be sorted exactly.
497 return strcoll(*sa, *sb);
502 * Command to list all variables which are set. Currently this command
503 * is invoked from the set command when the set command is called without
508 showvarscmd(int argc __unused, char **argv __unused)
517 * POSIX requires us to sort the variables.
520 for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) {
521 for (vp = *vpp; vp; vp = vp->next) {
522 if (!(vp->flags & VUNSET))
528 vars = ckmalloc(n * sizeof(*vars));
530 for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) {
531 for (vp = *vpp; vp; vp = vp->next) {
532 if (!(vp->flags & VUNSET))
533 vars[i++] = vp->text;
537 qsort(vars, n, sizeof(*vars), var_compare);
538 for (i = 0; i < n; i++) {
539 for (s = vars[i]; *s != '='; s++)
554 * The export and readonly commands.
558 exportcmd(int argc, char **argv)
566 int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT;
569 optreset = optind = 1;
572 while ((ch = getopt(argc, argv, "p")) != -1) {
579 error("unknown option: -%c", optopt);
585 if (values && argc != 0)
586 error("-p requires no arguments");
587 listsetvar(cmdenviron);
589 while ((name = *argv++) != NULL) {
590 if ((p = strchr(name, '=')) != NULL) {
594 for (vp = *vpp ; vp ; vp = vp->next) {
595 if (varequal(vp->text, name)) {
598 if ((vp->flags & VEXPORT) && localevar(vp->text)) {
600 (void) setlocale(LC_ALL, "");
606 setvar(name, p, flag);
610 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
611 for (vp = *vpp ; vp ; vp = vp->next) {
612 if (vp->flags & flag) {
617 for (p = vp->text ; *p != '=' ; p++)
619 if (values && !(vp->flags & VUNSET)) {
633 * The "local" command.
637 localcmd(int argc __unused, char **argv __unused)
642 error("Not in a function");
643 while ((name = *argptr++) != NULL) {
651 * Make a variable a local variable. When a variable is made local, it's
652 * value and flags are saved in a localvar structure. The saved values
653 * will be restored when the shell function returns. We handle the name
654 * "-" as a special case.
660 struct localvar *lvp;
665 lvp = ckmalloc(sizeof (struct localvar));
666 if (name[0] == '-' && name[1] == '\0') {
667 lvp->text = ckmalloc(sizeof optlist);
668 memcpy(lvp->text, optlist, sizeof optlist);
672 for (vp = *vpp ; vp && ! varequal(vp->text, name) ; vp = vp->next);
674 if (strchr(name, '='))
675 setvareq(savestr(name), VSTRFIXED);
677 setvar(name, NULL, VSTRFIXED);
678 vp = *vpp; /* the new variable */
682 lvp->text = vp->text;
683 lvp->flags = vp->flags;
684 vp->flags |= VSTRFIXED|VTEXTFIXED;
685 if (strchr(name, '='))
686 setvareq(savestr(name), 0);
690 lvp->next = localvars;
697 * Called after a function returns.
703 struct localvar *lvp;
706 while ((lvp = localvars) != NULL) {
707 localvars = lvp->next;
709 if (vp == NULL) { /* $- saved */
710 memcpy(optlist, lvp->text, sizeof optlist);
712 } else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) {
713 (void)unsetvar(vp->text);
715 if ((vp->flags & VTEXTFIXED) == 0)
717 vp->flags = lvp->flags;
718 vp->text = lvp->text;
726 setvarcmd(int argc, char **argv)
729 return unsetcmd(argc, argv);
731 setvar(argv[1], argv[2], 0);
733 error("List assignment not implemented");
739 * The unset builtin command. We unset the function before we unset the
740 * variable to allow a function to be unset when there is a readonly variable
741 * with the same name.
745 unsetcmd(int argc __unused, char **argv __unused)
753 while ((i = nextopt("vf")) != '\0') {
759 if (flg_func == 0 && flg_var == 0)
762 for (ap = argptr; *ap ; ap++) {
764 ret |= unsetfunc(*ap);
766 ret |= unsetvar(*ap);
773 * Unset the specified variable.
783 for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) {
784 if (varequal(vp->text, s)) {
785 if (vp->flags & VREADONLY)
788 if (*(strchr(vp->text, '=') + 1) != '\0')
789 setvar(s, nullstr, 0);
790 if ((vp->flags & VEXPORT) && localevar(vp->text)) {
792 setlocale(LC_ALL, "");
794 vp->flags &= ~VEXPORT;
796 if ((vp->flags & VSTRFIXED) == 0) {
797 if ((vp->flags & VTEXTFIXED) == 0)
813 * Find the appropriate entry in the hash table from the name.
819 unsigned int hashval;
821 hashval = ((unsigned char) *p) << 4;
822 while (*p && *p != '=')
823 hashval += (unsigned char) *p++;
824 return &vartab[hashval % VTABSIZE];
830 * Returns true if the two strings specify the same varable. The first
831 * variable name is terminated by '='; the second may be terminated by
832 * either '=' or '\0'.
836 varequal(char *p, char *q)
842 if (*p == '=' && *(q - 1) == '\0')