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.2 2003/06/17 04:22:50 dillon Exp $
53 #include "nodes.h" /* for other headers */
54 #include "eval.h" /* defines cmdenviron */
65 #include "myhistedit.h"
76 void (*func)(const char *);
99 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 struct var *vartab[VTABSIZE];
135 STATIC struct var **hashvar(char *);
136 STATIC int varequal(char *, char *);
137 STATIC int localevar(char *);
140 * Initialize the varable 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(char *name, 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(char *name, char *val, int flags)
243 if (! is_in_name(*p)) {
244 if (*p == '\0' || *p == '=')
252 error("%.*s: bad variable name", namelen, name);
253 len = namelen + 2; /* 2 is space for '=' and '\0' */
259 p = nameeq = ckmalloc(len);
261 while (--namelen >= 0)
267 setvareq(nameeq, flags);
273 static char *lnames[7] = {
274 "ALL", "COLLATE", "CTYPE", "MONETARY",
275 "NUMERIC", "TIME", NULL
281 if (varequal(s + 1, "ANG"))
283 if (strncmp(s + 1, "C_", 2) != 0)
285 for (ss = lnames; *ss ; ss++)
286 if (varequal(s + 3, *ss))
292 * Same as setvar except that the variable and value are passed in
293 * the first argument as name=value. Since the first argument will
294 * be actually stored in the table, it should not be a string that
299 setvareq(char *s, int flags)
301 struct var *vp, **vpp;
307 for (vp = *vpp ; vp ; vp = vp->next) {
308 if (varequal(s, vp->text)) {
309 if (vp->flags & VREADONLY) {
310 len = strchr(s, '=') - s;
311 error("%.*s: is read only", len, s);
315 if (vp->func && (flags & VNOFUNC) == 0)
316 (*vp->func)(strchr(s, '=') + 1);
318 if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0)
321 vp->flags &= ~(VTEXTFIXED|VSTACK|VUNSET);
326 * We could roll this to a function, to handle it as
327 * a regular variable function callback, but why bother?
329 if (vp == &vmpath || (vp == &vmail && ! mpathset()))
331 if ((vp->flags & VEXPORT) && localevar(s)) {
333 (void) setlocale(LC_ALL, "");
340 vp = ckmalloc(sizeof (*vp));
347 if ((vp->flags & VEXPORT) && localevar(s)) {
349 (void) setlocale(LC_ALL, "");
357 * Process a linked list of variable assignments.
361 listsetvar(struct strlist *list)
366 for (lp = list ; lp ; lp = lp->next) {
367 setvareq(savestr(lp->text), 0);
375 * Find the value of a variable. Returns NULL if not set.
379 lookupvar(char *name)
383 for (v = *hashvar(name) ; v ; v = v->next) {
384 if (varequal(v->text, name)) {
385 if (v->flags & VUNSET)
387 return strchr(v->text, '=') + 1;
396 * Search the environment of a builtin command. If the second argument
397 * is nonzero, return the value of a variable even if it hasn't been
402 bltinlookup(char *name, int doall)
407 for (sp = cmdenviron ; sp ; sp = sp->next) {
408 if (varequal(sp->text, name))
409 return strchr(sp->text, '=') + 1;
411 for (v = *hashvar(name) ; v ; v = v->next) {
412 if (varequal(v->text, name)) {
413 if ((v->flags & VUNSET)
414 || (!doall && (v->flags & VEXPORT) == 0))
416 return strchr(v->text, '=') + 1;
425 * Generate a list of exported variables. This routine is used to construct
426 * the third argument to execve when executing a program.
438 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
439 for (vp = *vpp ; vp ; vp = vp->next)
440 if (vp->flags & VEXPORT)
443 ep = env = stalloc((nenv + 1) * sizeof *env);
444 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
445 for (vp = *vpp ; vp ; vp = vp->next)
446 if (vp->flags & VEXPORT)
455 * Called when a shell procedure is invoked to clear out nonexported
456 * variables. It is also necessary to reallocate variables of with
457 * VSTACK set since these are currently allocated on the stack.
461 MKINIT void shprocvar();
472 struct var *vp, **prev;
474 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
475 for (prev = vpp ; (vp = *prev) != NULL ; ) {
476 if ((vp->flags & VEXPORT) == 0) {
478 if ((vp->flags & VTEXTFIXED) == 0)
480 if ((vp->flags & VSTRFIXED) == 0)
483 if (vp->flags & VSTACK) {
484 vp->text = savestr(vp->text);
485 vp->flags &=~ VSTACK;
497 * Command to list all variables which are set. Currently this command
498 * is invoked from the set command when the set command is called without
503 showvarscmd(int argc __unused, char **argv __unused)
509 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
510 for (vp = *vpp ; vp ; vp = vp->next) {
511 if (vp->flags & VUNSET)
513 for (s = vp->text; *s != '='; s++)
526 * The export and readonly commands.
530 exportcmd(int argc, char **argv)
538 int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT;
541 optreset = optind = 1;
544 while ((ch = getopt(argc, argv, "p")) != -1) {
551 error("unknown option: -%c", optopt);
557 listsetvar(cmdenviron);
559 while ((name = *argptr++) != NULL) {
560 if ((p = strchr(name, '=')) != NULL) {
564 for (vp = *vpp ; vp ; vp = vp->next) {
565 if (varequal(vp->text, name)) {
568 if ((vp->flags & VEXPORT) && localevar(vp->text)) {
570 (void) setlocale(LC_ALL, "");
576 setvar(name, p, flag);
580 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
581 for (vp = *vpp ; vp ; vp = vp->next) {
582 if (vp->flags & flag) {
587 for (p = vp->text ; *p != '=' ; p++)
589 if (values && !(vp->flags & VUNSET)) {
603 * The "local" command.
607 localcmd(int argc __unused, char **argv __unused)
612 error("Not in a function");
613 while ((name = *argptr++) != NULL) {
621 * Make a variable a local variable. When a variable is made local, it's
622 * value and flags are saved in a localvar structure. The saved values
623 * will be restored when the shell function returns. We handle the name
624 * "-" as a special case.
630 struct localvar *lvp;
635 lvp = ckmalloc(sizeof (struct localvar));
636 if (name[0] == '-' && name[1] == '\0') {
637 lvp->text = ckmalloc(sizeof optlist);
638 memcpy(lvp->text, optlist, sizeof optlist);
642 for (vp = *vpp ; vp && ! varequal(vp->text, name) ; vp = vp->next);
644 if (strchr(name, '='))
645 setvareq(savestr(name), VSTRFIXED);
647 setvar(name, NULL, VSTRFIXED);
648 vp = *vpp; /* the new variable */
652 lvp->text = vp->text;
653 lvp->flags = vp->flags;
654 vp->flags |= VSTRFIXED|VTEXTFIXED;
655 if (strchr(name, '='))
656 setvareq(savestr(name), 0);
660 lvp->next = localvars;
667 * Called after a function returns.
673 struct localvar *lvp;
676 while ((lvp = localvars) != NULL) {
677 localvars = lvp->next;
679 if (vp == NULL) { /* $- saved */
680 memcpy(optlist, lvp->text, sizeof optlist);
682 } else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) {
683 (void)unsetvar(vp->text);
685 if ((vp->flags & VTEXTFIXED) == 0)
687 vp->flags = lvp->flags;
688 vp->text = lvp->text;
696 setvarcmd(int argc, char **argv)
699 return unsetcmd(argc, argv);
701 setvar(argv[1], argv[2], 0);
703 error("List assignment not implemented");
709 * The unset builtin command. We unset the function before we unset the
710 * variable to allow a function to be unset when there is a readonly variable
711 * with the same name.
715 unsetcmd(int argc __unused, char **argv __unused)
723 while ((i = nextopt("vf")) != '\0') {
729 if (flg_func == 0 && flg_var == 0)
732 for (ap = argptr; *ap ; ap++) {
734 ret |= unsetfunc(*ap);
736 ret |= unsetvar(*ap);
743 * Unset the specified variable.
753 for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) {
754 if (varequal(vp->text, s)) {
755 if (vp->flags & VREADONLY)
758 if (*(strchr(vp->text, '=') + 1) != '\0')
759 setvar(s, nullstr, 0);
760 if ((vp->flags & VEXPORT) && localevar(vp->text)) {
762 setlocale(LC_ALL, "");
764 vp->flags &= ~VEXPORT;
766 if ((vp->flags & VSTRFIXED) == 0) {
767 if ((vp->flags & VTEXTFIXED) == 0)
783 * Find the appropriate entry in the hash table from the name.
789 unsigned int hashval;
791 hashval = ((unsigned char) *p) << 4;
792 while (*p && *p != '=')
793 hashval += (unsigned char) *p++;
794 return &vartab[hashval % VTABSIZE];
800 * Returns true if the two strings specify the same varable. The first
801 * variable name is terminated by '='; the second may be terminated by
802 * either '=' or '\0'.
806 varequal(char *p, char *q)
812 if (*p == '=' && *(q - 1) == '\0')