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.14 2007/01/14 15:14:52 pavalos Exp $
53 #include "nodes.h" /* for other headers */
54 #include "eval.h" /* defines cmdenviron */
65 #include "myhistedit.h"
76 void (*func)(const char *);
92 STATIC struct var voptind;
94 STATIC const struct varinit varinit[] = {
96 { &vhistsize, VSTRFIXED|VTEXTFIXED|VUNSET, "HISTSIZE=",
99 { &vifs, VSTRFIXED|VTEXTFIXED, "IFS= \t\n",
101 { &vmail, VSTRFIXED|VTEXTFIXED|VUNSET, "MAIL=",
103 { &vmpath, VSTRFIXED|VTEXTFIXED|VUNSET, "MAILPATH=",
105 { &vpath, VSTRFIXED|VTEXTFIXED, "PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/pkg/bin:/usr/pkg/sbin",
107 { &vppid, VSTRFIXED|VTEXTFIXED|VUNSET, "PPID=",
110 * vps1 depends on uid
112 { &vps2, VSTRFIXED|VTEXTFIXED, "PS2=> ",
114 { &vps4, VSTRFIXED|VTEXTFIXED, "PS4=+ ",
116 { &voptind, VSTRFIXED|VTEXTFIXED, "OPTIND=1",
122 STATIC struct var *vartab[VTABSIZE];
124 STATIC struct var **hashvar(const char *);
125 STATIC int varequal(const char *, const char *);
126 STATIC int localevar(const char *);
129 * Initialize the variable symbol tables and import the environment.
134 MKINIT char **environ;
139 for (envp = environ ; *envp ; envp++) {
140 if (strchr(*envp, '=')) {
141 setvareq(*envp, VEXPORT|VTEXTFIXED);
149 * This routine initializes the builtin variables. It is called when the
150 * shell is initialized and again when a shell procedure is spawned.
157 const struct varinit *ip;
161 for (ip = varinit ; (vp = ip->var) != NULL ; ip++) {
162 if ((vp->flags & VEXPORT) == 0) {
163 vpp = hashvar(ip->text);
166 vp->text = strdup(ip->text);
167 vp->flags = ip->flags;
174 if ((vps1.flags & VEXPORT) == 0) {
175 vpp = hashvar("PS1=");
178 vps1.text = strdup(geteuid() ? "PS1=$ " : "PS1=# ");
179 vps1.flags = VSTRFIXED|VTEXTFIXED;
181 if ((vppid.flags & VEXPORT) == 0) {
182 fmtstr(ppid, sizeof(ppid), "%d", (int)getppid());
183 setvarsafe("PPID", ppid, 0);
188 * Safe version of setvar, returns 1 on success 0 on failure.
192 setvarsafe(const char *name, const char *val, int flags)
194 struct jmploc jmploc;
195 struct jmploc *volatile savehandler = handler;
198 /* Avoid longjmp clobbering */
202 if (setjmp(jmploc.loc))
206 setvar(name, val, flags);
208 handler = savehandler;
213 * Set the value of a variable. The flags argument is stored with the
214 * flags of the variable. If val is NULL, the variable is unset.
218 setvar(const char *name, const char *val, int flags)
233 if (!is_in_name(*cp)) {
234 if (*cp == '\0' || *cp == '=')
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);
261 localevar(const char *s)
263 static const char * const lnames[7] = {
264 "ALL", "COLLATE", "CTYPE", "MONETARY",
265 "NUMERIC", "TIME", NULL
267 const char * const *ss;
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 error("putenv: cannot set %s", s);
324 setlocale(LC_ALL, "");
331 vp = ckmalloc(sizeof (*vp));
338 if ((vp->flags & VEXPORT) && localevar(s)) {
340 error("putenv: cannot set %s", s);
341 setlocale(LC_ALL, "");
349 * Process a linked list of variable assignments.
353 listsetvar(struct strlist *list)
358 for (lp = list ; lp ; lp = lp->next) {
359 setvareq(savestr(lp->text), 0);
367 * Find the value of a variable. Returns NULL if not set.
371 lookupvar(const char *name)
375 for (v = *hashvar(name) ; v ; v = v->next) {
376 if (varequal(v->text, name)) {
377 if (v->flags & VUNSET)
379 return strchr(v->text, '=') + 1;
388 * Search the environment of a builtin command. If the second argument
389 * is nonzero, return the value of a variable even if it hasn't been
394 bltinlookup(const char *name, int doall)
399 for (sp = cmdenviron ; sp ; sp = sp->next) {
400 if (varequal(sp->text, name))
401 return strchr(sp->text, '=') + 1;
403 for (v = *hashvar(name) ; v ; v = v->next) {
404 if (varequal(v->text, name)) {
405 if ((v->flags & VUNSET)
406 || (!doall && (v->flags & VEXPORT) == 0))
408 return strchr(v->text, '=') + 1;
417 * Generate a list of exported variables. This routine is used to construct
418 * the third argument to execve when executing a program.
430 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
431 for (vp = *vpp ; vp ; vp = vp->next)
432 if (vp->flags & VEXPORT)
435 ep = env = stalloc((nenv + 1) * sizeof *env);
436 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
437 for (vp = *vpp ; vp ; vp = vp->next)
438 if (vp->flags & VEXPORT)
447 * Called when a shell procedure is invoked to clear out nonexported
448 * variables. It is also necessary to reallocate variables of with
449 * VSTACK set since these are currently allocated on the stack.
453 void shprocvar(void);
464 struct var *vp, **prev;
466 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
467 for (prev = vpp ; (vp = *prev) != NULL ; ) {
468 if ((vp->flags & VEXPORT) == 0) {
470 if ((vp->flags & VTEXTFIXED) == 0)
472 if ((vp->flags & VSTRFIXED) == 0)
475 if (vp->flags & VSTACK) {
476 vp->text = savestr(vp->text);
477 vp->flags &=~ VSTACK;
488 var_compare(const void *a, const void *b)
490 const char *const *sa, *const *sb;
495 * This compares two var=value strings which creates a different
496 * order from what you would probably expect. POSIX is somewhat
497 * ambiguous on what should be sorted exactly.
499 return strcoll(*sa, *sb);
504 * Command to list all variables which are set. Currently this command
505 * is invoked from the set command when the set command is called without
510 showvarscmd(int argc __unused, char **argv __unused)
519 * POSIX requires us to sort the variables.
522 for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) {
523 for (vp = *vpp; vp; vp = vp->next) {
524 if (!(vp->flags & VUNSET))
530 vars = ckmalloc(n * sizeof(*vars));
532 for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) {
533 for (vp = *vpp; vp; vp = vp->next) {
534 if (!(vp->flags & VUNSET))
535 vars[i++] = vp->text;
539 qsort(vars, n, sizeof(*vars), var_compare);
540 for (i = 0; i < n; i++) {
541 for (s = vars[i]; *s != '='; s++)
556 * The export and readonly commands.
560 exportcmd(int argc, char **argv)
568 int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT;
571 optreset = optind = 1;
574 while ((ch = getopt(argc, argv, "p")) != -1) {
581 error("unknown option: -%c", optopt);
587 if (values && argc != 0)
588 error("-p requires no arguments");
589 listsetvar(cmdenviron);
591 while ((name = *argv++) != NULL) {
592 if ((p = strchr(name, '=')) != NULL) {
596 for (vp = *vpp ; vp ; vp = vp->next) {
597 if (varequal(vp->text, name)) {
600 if ((vp->flags & VEXPORT) && localevar(vp->text)) {
601 if (putenv(vp->text) != 0)
602 error("putenv: cannot set %s", vp->text);
603 setlocale(LC_ALL, "");
609 setvar(name, p, flag);
613 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
614 for (vp = *vpp ; vp ; vp = vp->next) {
615 if (vp->flags & flag) {
620 for (p = vp->text ; *p != '=' ; p++)
622 if (values && !(vp->flags & VUNSET)) {
636 * The "local" command.
640 localcmd(int argc __unused, char **argv __unused)
645 error("Not in a function");
646 while ((name = *argptr++) != NULL) {
654 * Make a variable a local variable. When a variable is made local, it's
655 * value and flags are saved in a localvar structure. The saved values
656 * will be restored when the shell function returns. We handle the name
657 * "-" as a special case.
663 struct localvar *lvp;
668 lvp = ckmalloc(sizeof (struct localvar));
669 if (name[0] == '-' && name[1] == '\0') {
670 lvp->text = ckmalloc(sizeof optlist);
671 memcpy(lvp->text, optlist, sizeof optlist);
675 for (vp = *vpp ; vp && ! varequal(vp->text, name) ; vp = vp->next);
677 if (strchr(name, '='))
678 setvareq(savestr(name), VSTRFIXED);
680 setvar(name, NULL, VSTRFIXED);
681 vp = *vpp; /* the new variable */
685 lvp->text = vp->text;
686 lvp->flags = vp->flags;
687 vp->flags |= VSTRFIXED|VTEXTFIXED;
688 if (strchr(name, '='))
689 setvareq(savestr(name), 0);
693 lvp->next = localvars;
700 * Called after a function returns.
706 struct localvar *lvp;
709 while ((lvp = localvars) != NULL) {
710 localvars = lvp->next;
712 if (vp == NULL) { /* $- saved */
713 memcpy(optlist, lvp->text, sizeof optlist);
715 } else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) {
718 if ((vp->flags & VTEXTFIXED) == 0)
720 vp->flags = lvp->flags;
721 vp->text = lvp->text;
729 setvarcmd(int argc, char **argv)
732 return unsetcmd(argc, argv);
734 setvar(argv[1], argv[2], 0);
736 error("List assignment not implemented");
742 * The unset builtin command. We unset the function before we unset the
743 * variable to allow a function to be unset when there is a readonly variable
744 * with the same name.
748 unsetcmd(int argc __unused, char **argv __unused)
756 while ((i = nextopt("vf")) != '\0') {
762 if (flg_func == 0 && flg_var == 0)
765 for (ap = argptr; *ap ; ap++) {
767 ret |= unsetfunc(*ap);
769 ret |= unsetvar(*ap);
776 * Unset the specified variable.
780 unsetvar(const char *s)
786 for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) {
787 if (varequal(vp->text, s)) {
788 if (vp->flags & VREADONLY)
791 if (*(strchr(vp->text, '=') + 1) != '\0')
792 setvar(s, nullstr, 0);
793 if ((vp->flags & VEXPORT) && localevar(vp->text)) {
795 setlocale(LC_ALL, "");
797 vp->flags &= ~VEXPORT;
799 if ((vp->flags & VSTRFIXED) == 0) {
800 if ((vp->flags & VTEXTFIXED) == 0)
815 * Find the appropriate entry in the hash table from the name.
819 hashvar(const char *p)
821 unsigned int hashval;
823 hashval = ((unsigned char) *p) << 4;
824 while (*p && *p != '=')
825 hashval += (unsigned char) *p++;
826 return &vartab[hashval % VTABSIZE];
832 * Returns true if the two strings specify the same varable. The first
833 * variable name is terminated by '='; the second may be terminated by
834 * either '=' or '\0'.
838 varequal(const char *p, const char *q)
844 if (*p == '=' && *(q - 1) == '\0')