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
36 * @(#)var.c 8.3 (Berkeley) 5/4/95
37 * $FreeBSD: src/bin/sh/var.c,v 1.15.2.2 2002/08/27 01:36:28 tjr Exp $
38 * $DragonFly: src/bin/sh/var.c,v 1.4 2004/03/19 18:39:41 cpressey Exp $
53 #include "nodes.h" /* for other headers */
54 #include "eval.h" /* defines cmdenviron */
65 #include "myhistedit.h"
76 void (*func)(const char *);
95 STATIC struct var vterm;
97 STATIC struct var voptind;
99 STATIC const struct varinit varinit[] = {
101 { &vatty, VSTRFIXED|VTEXTFIXED|VUNSET, "ATTY=",
105 { &vhistsize, VSTRFIXED|VTEXTFIXED|VUNSET, "HISTSIZE=",
108 { &vifs, VSTRFIXED|VTEXTFIXED, "IFS= \t\n",
110 { &vmail, VSTRFIXED|VTEXTFIXED|VUNSET, "MAIL=",
112 { &vmpath, VSTRFIXED|VTEXTFIXED|VUNSET, "MAILPATH=",
114 { &vpath, VSTRFIXED|VTEXTFIXED, "PATH=/bin:/usr/bin",
116 { &vppid, VSTRFIXED|VTEXTFIXED|VUNSET, "PPID=",
119 * vps1 depends on uid
121 { &vps2, VSTRFIXED|VTEXTFIXED, "PS2=> ",
124 { &vterm, VSTRFIXED|VTEXTFIXED|VUNSET, "TERM=",
127 { &voptind, VSTRFIXED|VTEXTFIXED, "OPTIND=1",
133 STATIC struct var *vartab[VTABSIZE];
135 STATIC struct var **hashvar(const char *);
136 STATIC int varequal(const char *, const char *);
137 STATIC int localevar(const char *);
140 * Initialize the variable symbol tables and import the environment.
147 extern char **environ;
150 for (envp = environ ; *envp ; envp++) {
151 if (strchr(*envp, '=')) {
152 setvareq(*envp, VEXPORT|VTEXTFIXED);
160 * This routine initializes the builtin variables. It is called when the
161 * shell is initialized and again when a shell procedure is spawned.
168 const struct varinit *ip;
172 for (ip = varinit ; (vp = ip->var) != NULL ; ip++) {
173 if ((vp->flags & VEXPORT) == 0) {
174 vpp = hashvar(ip->text);
178 vp->flags = ip->flags;
185 if ((vps1.flags & VEXPORT) == 0) {
186 vpp = hashvar("PS1=");
189 vps1.text = geteuid() ? "PS1=$ " : "PS1=# ";
190 vps1.flags = VSTRFIXED|VTEXTFIXED;
192 if ((vppid.flags & VEXPORT) == 0) {
193 fmtstr(ppid, sizeof(ppid), "%d", (int)getppid());
194 setvarsafe("PPID", ppid, 0);
199 * Safe version of setvar, returns 1 on success 0 on failure.
203 setvarsafe(const char *name, const char *val, int flags)
205 struct jmploc jmploc;
206 struct jmploc *volatile savehandler = handler;
209 /* Avoid longjmp clobbering */
213 if (setjmp(jmploc.loc))
217 setvar(name, val, flags);
219 handler = savehandler;
224 * Set the value of a variable. The flags argument is tored with the
225 * flags of the variable. If val is NULL, the variable is unset.
229 setvar(const char *name, const char *val, int flags)
244 if (!is_in_name(*cp)) {
245 if (*cp == '\0' || *cp == '=')
253 error("%.*s: bad variable name", namelen, name);
254 len = namelen + 2; /* 2 is space for '=' and '\0' */
260 p = nameeq = ckmalloc(len);
262 while (--namelen >= 0)
268 setvareq(nameeq, flags);
272 localevar(const char *s)
274 static const char * const lnames[7] = {
275 "ALL", "COLLATE", "CTYPE", "MONETARY",
276 "NUMERIC", "TIME", NULL
278 const char * const *ss;
282 if (varequal(s + 1, "ANG"))
284 if (strncmp(s + 1, "C_", 2) != 0)
286 for (ss = lnames; *ss ; ss++)
287 if (varequal(s + 3, *ss))
293 * Same as setvar except that the variable and value are passed in
294 * the first argument as name=value. Since the first argument will
295 * be actually stored in the table, it should not be a string that
300 setvareq(char *s, int flags)
302 struct var *vp, **vpp;
308 for (vp = *vpp ; vp ; vp = vp->next) {
309 if (varequal(s, vp->text)) {
310 if (vp->flags & VREADONLY) {
311 len = strchr(s, '=') - s;
312 error("%.*s: is read only", len, s);
316 if (vp->func && (flags & VNOFUNC) == 0)
317 (*vp->func)(strchr(s, '=') + 1);
319 if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0)
322 vp->flags &= ~(VTEXTFIXED|VSTACK|VUNSET);
327 * We could roll this to a function, to handle it as
328 * a regular variable function callback, but why bother?
330 if (vp == &vmpath || (vp == &vmail && ! mpathset()))
332 if ((vp->flags & VEXPORT) && localevar(s)) {
334 (void) setlocale(LC_ALL, "");
341 vp = ckmalloc(sizeof (*vp));
348 if ((vp->flags & VEXPORT) && localevar(s)) {
350 (void) setlocale(LC_ALL, "");
358 * Process a linked list of variable assignments.
362 listsetvar(struct strlist *list)
367 for (lp = list ; lp ; lp = lp->next) {
368 setvareq(savestr(lp->text), 0);
376 * Find the value of a variable. Returns NULL if not set.
380 lookupvar(const char *name)
384 for (v = *hashvar(name) ; v ; v = v->next) {
385 if (varequal(v->text, name)) {
386 if (v->flags & VUNSET)
388 return strchr(v->text, '=') + 1;
397 * Search the environment of a builtin command. If the second argument
398 * is nonzero, return the value of a variable even if it hasn't been
403 bltinlookup(const char *name, int doall)
408 for (sp = cmdenviron ; sp ; sp = sp->next) {
409 if (varequal(sp->text, name))
410 return strchr(sp->text, '=') + 1;
412 for (v = *hashvar(name) ; v ; v = v->next) {
413 if (varequal(v->text, name)) {
414 if ((v->flags & VUNSET)
415 || (!doall && (v->flags & VEXPORT) == 0))
417 return strchr(v->text, '=') + 1;
426 * Generate a list of exported variables. This routine is used to construct
427 * the third argument to execve when executing a program.
439 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
440 for (vp = *vpp ; vp ; vp = vp->next)
441 if (vp->flags & VEXPORT)
444 ep = env = stalloc((nenv + 1) * sizeof *env);
445 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
446 for (vp = *vpp ; vp ; vp = vp->next)
447 if (vp->flags & VEXPORT)
456 * Called when a shell procedure is invoked to clear out nonexported
457 * variables. It is also necessary to reallocate variables of with
458 * VSTACK set since these are currently allocated on the stack.
462 MKINIT void shprocvar(void);
473 struct var *vp, **prev;
475 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
476 for (prev = vpp ; (vp = *prev) != NULL ; ) {
477 if ((vp->flags & VEXPORT) == 0) {
479 if ((vp->flags & VTEXTFIXED) == 0)
481 if ((vp->flags & VSTRFIXED) == 0)
484 if (vp->flags & VSTACK) {
485 vp->text = savestr(vp->text);
486 vp->flags &=~ VSTACK;
498 * Command to list all variables which are set. Currently this command
499 * is invoked from the set command when the set command is called without
504 showvarscmd(int argc __unused, char **argv __unused)
510 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
511 for (vp = *vpp ; vp ; vp = vp->next) {
512 if (vp->flags & VUNSET)
514 for (s = vp->text; *s != '='; s++)
527 * The export and readonly commands.
531 exportcmd(int argc, char **argv)
539 int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT;
542 optreset = optind = 1;
545 while ((ch = getopt(argc, argv, "p")) != -1) {
552 error("unknown option: -%c", optopt);
558 listsetvar(cmdenviron);
560 while ((name = *argptr++) != NULL) {
561 if ((p = strchr(name, '=')) != NULL) {
565 for (vp = *vpp ; vp ; vp = vp->next) {
566 if (varequal(vp->text, name)) {
569 if ((vp->flags & VEXPORT) && localevar(vp->text)) {
571 (void) setlocale(LC_ALL, "");
577 setvar(name, p, flag);
581 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
582 for (vp = *vpp ; vp ; vp = vp->next) {
583 if (vp->flags & flag) {
588 for (p = vp->text ; *p != '=' ; p++)
590 if (values && !(vp->flags & VUNSET)) {
604 * The "local" command.
608 localcmd(int argc __unused, char **argv __unused)
613 error("Not in a function");
614 while ((name = *argptr++) != NULL) {
622 * Make a variable a local variable. When a variable is made local, it's
623 * value and flags are saved in a localvar structure. The saved values
624 * will be restored when the shell function returns. We handle the name
625 * "-" as a special case.
631 struct localvar *lvp;
636 lvp = ckmalloc(sizeof (struct localvar));
637 if (name[0] == '-' && name[1] == '\0') {
638 lvp->text = ckmalloc(sizeof optlist);
639 memcpy(lvp->text, optlist, sizeof optlist);
643 for (vp = *vpp ; vp && ! varequal(vp->text, name) ; vp = vp->next);
645 if (strchr(name, '='))
646 setvareq(savestr(name), VSTRFIXED);
648 setvar(name, NULL, VSTRFIXED);
649 vp = *vpp; /* the new variable */
653 lvp->text = vp->text;
654 lvp->flags = vp->flags;
655 vp->flags |= VSTRFIXED|VTEXTFIXED;
656 if (strchr(name, '='))
657 setvareq(savestr(name), 0);
661 lvp->next = localvars;
668 * Called after a function returns.
674 struct localvar *lvp;
677 while ((lvp = localvars) != NULL) {
678 localvars = lvp->next;
680 if (vp == NULL) { /* $- saved */
681 memcpy(optlist, lvp->text, sizeof optlist);
683 } else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) {
684 (void)unsetvar(vp->text);
686 if ((vp->flags & VTEXTFIXED) == 0)
688 vp->flags = lvp->flags;
689 vp->text = lvp->text;
697 setvarcmd(int argc, char **argv)
700 return unsetcmd(argc, argv);
702 setvar(argv[1], argv[2], 0);
704 error("List assignment not implemented");
710 * The unset builtin command. We unset the function before we unset the
711 * variable to allow a function to be unset when there is a readonly variable
712 * with the same name.
716 unsetcmd(int argc __unused, char **argv __unused)
724 while ((i = nextopt("vf")) != '\0') {
730 if (flg_func == 0 && flg_var == 0)
733 for (ap = argptr; *ap ; ap++) {
735 ret |= unsetfunc(*ap);
737 ret |= unsetvar(*ap);
744 * Unset the specified variable.
748 unsetvar(const char *s)
754 for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) {
755 if (varequal(vp->text, s)) {
756 if (vp->flags & VREADONLY)
759 if (*(strchr(vp->text, '=') + 1) != '\0')
760 setvar(s, nullstr, 0);
761 if ((vp->flags & VEXPORT) && localevar(vp->text)) {
763 setlocale(LC_ALL, "");
765 vp->flags &= ~VEXPORT;
767 if ((vp->flags & VSTRFIXED) == 0) {
768 if ((vp->flags & VTEXTFIXED) == 0)
784 * Find the appropriate entry in the hash table from the name.
788 hashvar(const char *p)
790 unsigned int hashval;
792 hashval = ((unsigned char) *p) << 4;
793 while (*p && *p != '=')
794 hashval += (unsigned char) *p++;
795 return &vartab[hashval % VTABSIZE];
801 * Returns true if the two strings specify the same varable. The first
802 * variable name is terminated by '='; the second may be terminated by
803 * either '=' or '\0'.
807 varequal(const char *p, const char *q)
813 if (*p == '=' && *(q - 1) == '\0')