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.7 2004/11/07 20:54:52 eirikn Exp $
53 #include "nodes.h" /* for other headers */
54 #include "eval.h" /* defines cmdenviron */
65 #include "myhistedit.h"
76 void (*func)(const char *);
91 STATIC struct var voptind;
93 STATIC const struct varinit varinit[] = {
95 { &vhistsize, VSTRFIXED|VTEXTFIXED|VUNSET, "HISTSIZE=",
98 { &vifs, VSTRFIXED|VTEXTFIXED, "IFS= \t\n",
100 { &vmail, VSTRFIXED|VTEXTFIXED|VUNSET, "MAIL=",
102 { &vmpath, VSTRFIXED|VTEXTFIXED|VUNSET, "MAILPATH=",
104 { &vpath, VSTRFIXED|VTEXTFIXED, "PATH=/bin:/usr/bin",
106 { &vppid, VSTRFIXED|VTEXTFIXED|VUNSET, "PPID=",
109 * vps1 depends on uid
111 { &vps2, VSTRFIXED|VTEXTFIXED, "PS2=> ",
113 { &voptind, VSTRFIXED|VTEXTFIXED, "OPTIND=1",
119 STATIC struct var *vartab[VTABSIZE];
121 STATIC struct var **hashvar(const char *);
122 STATIC int varequal(const char *, const char *);
123 STATIC int localevar(const char *);
126 * Initialize the variable symbol tables and import the environment.
133 extern char **environ;
136 for (envp = environ ; *envp ; envp++) {
137 if (strchr(*envp, '=')) {
138 setvareq(*envp, VEXPORT|VTEXTFIXED);
146 * This routine initializes the builtin variables. It is called when the
147 * shell is initialized and again when a shell procedure is spawned.
154 const struct varinit *ip;
158 for (ip = varinit ; (vp = ip->var) != NULL ; ip++) {
159 if ((vp->flags & VEXPORT) == 0) {
160 vpp = hashvar(ip->text);
164 vp->flags = ip->flags;
171 if ((vps1.flags & VEXPORT) == 0) {
172 vpp = hashvar("PS1=");
175 vps1.text = geteuid() ? "PS1=$ " : "PS1=# ";
176 vps1.flags = VSTRFIXED|VTEXTFIXED;
178 if ((vppid.flags & VEXPORT) == 0) {
179 fmtstr(ppid, sizeof(ppid), "%d", (int)getppid());
180 setvarsafe("PPID", ppid, 0);
185 * Safe version of setvar, returns 1 on success 0 on failure.
189 setvarsafe(const char *name, const char *val, int flags)
191 struct jmploc jmploc;
192 struct jmploc *volatile savehandler = handler;
195 /* Avoid longjmp clobbering */
199 if (setjmp(jmploc.loc))
203 setvar(name, val, flags);
205 handler = savehandler;
210 * Set the value of a variable. The flags argument is tored with the
211 * flags of the variable. If val is NULL, the variable is unset.
215 setvar(const char *name, const char *val, int flags)
230 if (!is_in_name(*cp)) {
231 if (*cp == '\0' || *cp == '=')
239 error("%.*s: bad variable name", namelen, name);
240 len = namelen + 2; /* 2 is space for '=' and '\0' */
246 p = nameeq = ckmalloc(len);
248 while (--namelen >= 0)
254 setvareq(nameeq, flags);
258 localevar(const char *s)
260 static const char * const lnames[7] = {
261 "ALL", "COLLATE", "CTYPE", "MONETARY",
262 "NUMERIC", "TIME", NULL
264 const char * const *ss;
268 if (varequal(s + 1, "ANG"))
270 if (strncmp(s + 1, "C_", 2) != 0)
272 for (ss = lnames; *ss ; ss++)
273 if (varequal(s + 3, *ss))
279 * Same as setvar except that the variable and value are passed in
280 * the first argument as name=value. Since the first argument will
281 * be actually stored in the table, it should not be a string that
286 setvareq(char *s, int flags)
288 struct var *vp, **vpp;
294 for (vp = *vpp ; vp ; vp = vp->next) {
295 if (varequal(s, vp->text)) {
296 if (vp->flags & VREADONLY) {
297 len = strchr(s, '=') - s;
298 error("%.*s: is read only", len, s);
302 if (vp->func && (flags & VNOFUNC) == 0)
303 (*vp->func)(strchr(s, '=') + 1);
305 if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0)
308 vp->flags &= ~(VTEXTFIXED|VSTACK|VUNSET);
313 * We could roll this to a function, to handle it as
314 * a regular variable function callback, but why bother?
316 if (vp == &vmpath || (vp == &vmail && ! mpathset()))
318 if ((vp->flags & VEXPORT) && localevar(s)) {
320 setlocale(LC_ALL, "");
327 vp = ckmalloc(sizeof (*vp));
334 if ((vp->flags & VEXPORT) && localevar(s)) {
336 setlocale(LC_ALL, "");
344 * Process a linked list of variable assignments.
348 listsetvar(struct strlist *list)
353 for (lp = list ; lp ; lp = lp->next) {
354 setvareq(savestr(lp->text), 0);
362 * Find the value of a variable. Returns NULL if not set.
366 lookupvar(const char *name)
370 for (v = *hashvar(name) ; v ; v = v->next) {
371 if (varequal(v->text, name)) {
372 if (v->flags & VUNSET)
374 return strchr(v->text, '=') + 1;
383 * Search the environment of a builtin command. If the second argument
384 * is nonzero, return the value of a variable even if it hasn't been
389 bltinlookup(const char *name, int doall)
394 for (sp = cmdenviron ; sp ; sp = sp->next) {
395 if (varequal(sp->text, name))
396 return strchr(sp->text, '=') + 1;
398 for (v = *hashvar(name) ; v ; v = v->next) {
399 if (varequal(v->text, name)) {
400 if ((v->flags & VUNSET)
401 || (!doall && (v->flags & VEXPORT) == 0))
403 return strchr(v->text, '=') + 1;
412 * Generate a list of exported variables. This routine is used to construct
413 * the third argument to execve when executing a program.
425 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
426 for (vp = *vpp ; vp ; vp = vp->next)
427 if (vp->flags & VEXPORT)
430 ep = env = stalloc((nenv + 1) * sizeof *env);
431 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
432 for (vp = *vpp ; vp ; vp = vp->next)
433 if (vp->flags & VEXPORT)
442 * Called when a shell procedure is invoked to clear out nonexported
443 * variables. It is also necessary to reallocate variables of with
444 * VSTACK set since these are currently allocated on the stack.
448 MKINIT void shprocvar(void);
459 struct var *vp, **prev;
461 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
462 for (prev = vpp ; (vp = *prev) != NULL ; ) {
463 if ((vp->flags & VEXPORT) == 0) {
465 if ((vp->flags & VTEXTFIXED) == 0)
467 if ((vp->flags & VSTRFIXED) == 0)
470 if (vp->flags & VSTACK) {
471 vp->text = savestr(vp->text);
472 vp->flags &=~ VSTACK;
484 * Command to list all variables which are set. Currently this command
485 * is invoked from the set command when the set command is called without
490 showvarscmd(int argc __unused, char **argv __unused)
496 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
497 for (vp = *vpp ; vp ; vp = vp->next) {
498 if (vp->flags & VUNSET)
500 for (s = vp->text; *s != '='; s++)
513 * The export and readonly commands.
517 exportcmd(int argc, char **argv)
525 int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT;
528 optreset = optind = 1;
531 while ((ch = getopt(argc, argv, "p")) != -1) {
538 error("unknown option: -%c", optopt);
544 listsetvar(cmdenviron);
546 while ((name = *argptr++) != NULL) {
547 if ((p = strchr(name, '=')) != NULL) {
551 for (vp = *vpp ; vp ; vp = vp->next) {
552 if (varequal(vp->text, name)) {
555 if ((vp->flags & VEXPORT) && localevar(vp->text)) {
557 setlocale(LC_ALL, "");
563 setvar(name, p, flag);
567 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
568 for (vp = *vpp ; vp ; vp = vp->next) {
569 if (vp->flags & flag) {
574 for (p = vp->text ; *p != '=' ; p++)
576 if (values && !(vp->flags & VUNSET)) {
590 * The "local" command.
594 localcmd(int argc __unused, char **argv __unused)
599 error("Not in a function");
600 while ((name = *argptr++) != NULL) {
608 * Make a variable a local variable. When a variable is made local, it's
609 * value and flags are saved in a localvar structure. The saved values
610 * will be restored when the shell function returns. We handle the name
611 * "-" as a special case.
617 struct localvar *lvp;
622 lvp = ckmalloc(sizeof (struct localvar));
623 if (name[0] == '-' && name[1] == '\0') {
624 lvp->text = ckmalloc(sizeof optlist);
625 memcpy(lvp->text, optlist, sizeof optlist);
629 for (vp = *vpp ; vp && ! varequal(vp->text, name) ; vp = vp->next);
631 if (strchr(name, '='))
632 setvareq(savestr(name), VSTRFIXED);
634 setvar(name, NULL, VSTRFIXED);
635 vp = *vpp; /* the new variable */
639 lvp->text = vp->text;
640 lvp->flags = vp->flags;
641 vp->flags |= VSTRFIXED|VTEXTFIXED;
642 if (strchr(name, '='))
643 setvareq(savestr(name), 0);
647 lvp->next = localvars;
654 * Called after a function returns.
660 struct localvar *lvp;
663 while ((lvp = localvars) != NULL) {
664 localvars = lvp->next;
666 if (vp == NULL) { /* $- saved */
667 memcpy(optlist, lvp->text, sizeof optlist);
669 } else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) {
672 if ((vp->flags & VTEXTFIXED) == 0)
674 vp->flags = lvp->flags;
675 vp->text = lvp->text;
683 setvarcmd(int argc, char **argv)
686 return unsetcmd(argc, argv);
688 setvar(argv[1], argv[2], 0);
690 error("List assignment not implemented");
696 * The unset builtin command. We unset the function before we unset the
697 * variable to allow a function to be unset when there is a readonly variable
698 * with the same name.
702 unsetcmd(int argc __unused, char **argv __unused)
710 while ((i = nextopt("vf")) != '\0') {
716 if (flg_func == 0 && flg_var == 0)
719 for (ap = argptr; *ap ; ap++) {
721 ret |= unsetfunc(*ap);
723 ret |= unsetvar(*ap);
730 * Unset the specified variable.
734 unsetvar(const char *s)
740 for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) {
741 if (varequal(vp->text, s)) {
742 if (vp->flags & VREADONLY)
745 if (*(strchr(vp->text, '=') + 1) != '\0')
746 setvar(s, nullstr, 0);
747 if ((vp->flags & VEXPORT) && localevar(vp->text)) {
749 setlocale(LC_ALL, "");
751 vp->flags &= ~VEXPORT;
753 if ((vp->flags & VSTRFIXED) == 0) {
754 if ((vp->flags & VTEXTFIXED) == 0)
769 * Find the appropriate entry in the hash table from the name.
773 hashvar(const char *p)
775 unsigned int hashval;
777 hashval = ((unsigned char) *p) << 4;
778 while (*p && *p != '=')
779 hashval += (unsigned char) *p++;
780 return &vartab[hashval % VTABSIZE];
786 * Returns true if the two strings specify the same varable. The first
787 * variable name is terminated by '='; the second may be terminated by
788 * either '=' or '\0'.
792 varequal(const char *p, const char *q)
798 if (*p == '=' && *(q - 1) == '\0')