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(const char *);
126 static int varequal(const char *, const char *);
127 static int localevar(const char *);
130 * Initialize the variable symbol tables and import the environment.
135 MKINIT 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(const char *name, const char *val, int flags)
195 struct jmploc jmploc;
196 struct jmploc *const savehandler = handler;
199 if (setjmp(jmploc.loc))
203 setvar(name, val, flags);
205 handler = savehandler;
210 * Set the value of a variable. The flags argument is stored 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)
229 if (! is_in_name(*p)) {
230 if (*p == '\0' || *p == '=')
238 error("%.*s: bad variable name", namelen, name);
239 len = namelen + 2; /* 2 is space for '=' and '\0' */
245 nameeq = ckmalloc(len);
246 memcpy(nameeq, name, namelen);
247 nameeq[namelen] = '=';
249 scopy(val, nameeq + namelen + 1);
251 nameeq[namelen + 1] = '\0';
252 setvareq(nameeq, flags);
256 localevar(const char *s)
258 static const char *lnames[7] = {
259 "ALL", "COLLATE", "CTYPE", "MONETARY",
260 "NUMERIC", "TIME", NULL
266 if (varequal(s + 1, "ANG"))
268 if (strncmp(s + 1, "C_", 2) != 0)
270 for (ss = lnames; *ss ; ss++)
271 if (varequal(s + 3, *ss))
278 * Sets/unsets an environment variable from a pointer that may actually be a
279 * pointer into environ where the string should not be manipulated.
282 change_env(const char *s, int set)
288 if ((eqp = strchr(ss, '=')) != NULL)
290 if (set && eqp != NULL)
291 (void) setenv(ss, eqp + 1, 1);
301 * Same as setvar except that the variable and value are passed in
302 * the first argument as name=value. Since the first argument will
303 * be actually stored in the table, it should not be a string that
308 setvareq(char *s, int flags)
310 struct var *vp, **vpp;
316 for (vp = *vpp ; vp ; vp = vp->next) {
317 if (varequal(s, vp->text)) {
318 if (vp->flags & VREADONLY) {
319 len = strchr(s, '=') - s;
320 error("%.*s: is read only", len, s);
324 if (vp->func && (flags & VNOFUNC) == 0)
325 (*vp->func)(strchr(s, '=') + 1);
327 if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0)
330 vp->flags &= ~(VTEXTFIXED|VSTACK|VUNSET);
335 * We could roll this to a function, to handle it as
336 * a regular variable function callback, but why bother?
338 * Note: this assumes iflag is not set to 1 initially.
339 * As part of init(), this is called before arguments
342 if ((vp == &vmpath || (vp == &vmail && ! mpathset())) &&
345 if ((vp->flags & VEXPORT) && localevar(s)) {
347 (void) setlocale(LC_ALL, "");
354 vp = ckmalloc(sizeof (*vp));
361 if ((vp->flags & VEXPORT) && localevar(s)) {
363 (void) setlocale(LC_ALL, "");
371 * Process a linked list of variable assignments.
375 listsetvar(struct strlist *list)
380 for (lp = list ; lp ; lp = lp->next) {
381 setvareq(savestr(lp->text), 0);
389 * Find the value of a variable. Returns NULL if not set.
393 lookupvar(const char *name)
397 for (v = *hashvar(name) ; v ; v = v->next) {
398 if (varequal(v->text, name)) {
399 if (v->flags & VUNSET)
401 return strchr(v->text, '=') + 1;
410 * Search the environment of a builtin command. If the second argument
411 * is nonzero, return the value of a variable even if it hasn't been
416 bltinlookup(const char *name, int doall)
421 for (sp = cmdenviron ; sp ; sp = sp->next) {
422 if (varequal(sp->text, name))
423 return strchr(sp->text, '=') + 1;
425 for (v = *hashvar(name) ; v ; v = v->next) {
426 if (varequal(v->text, name)) {
427 if ((v->flags & VUNSET)
428 || (!doall && (v->flags & VEXPORT) == 0))
430 return strchr(v->text, '=') + 1;
439 * Generate a list of exported variables. This routine is used to construct
440 * the third argument to execve when executing a program.
452 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
453 for (vp = *vpp ; vp ; vp = vp->next)
454 if (vp->flags & VEXPORT)
457 ep = env = stalloc((nenv + 1) * sizeof *env);
458 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
459 for (vp = *vpp ; vp ; vp = vp->next)
460 if (vp->flags & VEXPORT)
469 * Called when a shell procedure is invoked to clear out nonexported
470 * variables. It is also necessary to reallocate variables of with
471 * VSTACK set since these are currently allocated on the stack.
474 MKINIT void shprocvar(void);
486 struct var *vp, **prev;
488 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
489 for (prev = vpp ; (vp = *prev) != NULL ; ) {
490 if ((vp->flags & VEXPORT) == 0) {
492 if ((vp->flags & VTEXTFIXED) == 0)
494 if ((vp->flags & VSTRFIXED) == 0)
497 if (vp->flags & VSTACK) {
498 vp->text = savestr(vp->text);
499 vp->flags &=~ VSTACK;
510 var_compare(const void *a, const void *b)
512 const char *const *sa, *const *sb;
517 * This compares two var=value strings which creates a different
518 * order from what you would probably expect. POSIX is somewhat
519 * ambiguous on what should be sorted exactly.
521 return strcoll(*sa, *sb);
526 * Command to list all variables which are set. This is invoked from the
527 * set command when it is called without any options or operands.
531 showvarscmd(int argc __unused, char **argv __unused)
540 * POSIX requires us to sort the variables.
543 for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) {
544 for (vp = *vpp; vp; vp = vp->next) {
545 if (!(vp->flags & VUNSET))
551 vars = ckmalloc(n * sizeof(*vars));
553 for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) {
554 for (vp = *vpp; vp; vp = vp->next) {
555 if (!(vp->flags & VUNSET))
556 vars[i++] = vp->text;
560 qsort(vars, n, sizeof(*vars), var_compare);
561 for (i = 0; i < n; i++) {
562 for (s = vars[i]; *s != '='; s++)
577 * The export and readonly commands.
581 exportcmd(int argc, char **argv)
589 int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT;
592 optreset = optind = 1;
595 while ((ch = getopt(argc, argv, "p")) != -1) {
602 error("unknown option: -%c", optopt);
608 if (values && argc != 0)
609 error("-p requires no arguments");
611 while ((name = *argv++) != NULL) {
612 if ((p = strchr(name, '=')) != NULL) {
616 for (vp = *vpp ; vp ; vp = vp->next) {
617 if (varequal(vp->text, name)) {
620 if ((vp->flags & VEXPORT) && localevar(vp->text)) {
621 change_env(vp->text, 1);
622 (void) setlocale(LC_ALL, "");
628 setvar(name, p, flag);
632 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
633 for (vp = *vpp ; vp ; vp = vp->next) {
634 if (vp->flags & flag) {
639 for (p = vp->text ; *p != '=' ; p++)
641 if (values && !(vp->flags & VUNSET)) {
655 * The "local" command.
659 localcmd(int argc __unused, char **argv __unused)
664 error("Not in a function");
665 while ((name = *argptr++) != NULL) {
673 * Make a variable a local variable. When a variable is made local, it's
674 * value and flags are saved in a localvar structure. The saved values
675 * will be restored when the shell function returns. We handle the name
676 * "-" as a special case.
682 struct localvar *lvp;
687 lvp = ckmalloc(sizeof (struct localvar));
688 if (name[0] == '-' && name[1] == '\0') {
689 lvp->text = ckmalloc(sizeof optlist);
690 memcpy(lvp->text, optlist, sizeof optlist);
694 for (vp = *vpp ; vp && ! varequal(vp->text, name) ; vp = vp->next);
696 if (strchr(name, '='))
697 setvareq(savestr(name), VSTRFIXED);
699 setvar(name, NULL, VSTRFIXED);
700 vp = *vpp; /* the new variable */
704 lvp->text = vp->text;
705 lvp->flags = vp->flags;
706 vp->flags |= VSTRFIXED|VTEXTFIXED;
707 if (strchr(name, '='))
708 setvareq(savestr(name), 0);
712 lvp->next = localvars;
719 * Called after a function returns.
725 struct localvar *lvp;
728 while ((lvp = localvars) != NULL) {
729 localvars = lvp->next;
731 if (vp == NULL) { /* $- saved */
732 memcpy(optlist, lvp->text, sizeof optlist);
734 } else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) {
735 (void)unsetvar(vp->text);
737 if ((vp->flags & VTEXTFIXED) == 0)
739 vp->flags = lvp->flags;
740 vp->text = lvp->text;
748 setvarcmd(int argc, char **argv)
751 return unsetcmd(argc, argv);
753 setvar(argv[1], argv[2], 0);
755 error("List assignment not implemented");
761 * The unset builtin command.
765 unsetcmd(int argc __unused, char **argv __unused)
773 while ((i = nextopt("vf")) != '\0') {
779 if (flg_func == 0 && flg_var == 0)
782 for (ap = argptr; *ap ; ap++) {
784 ret |= unsetfunc(*ap);
786 ret |= unsetvar(*ap);
793 * Unset the specified variable.
797 unsetvar(const char *s)
803 for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) {
804 if (varequal(vp->text, s)) {
805 if (vp->flags & VREADONLY)
808 if (*(strchr(vp->text, '=') + 1) != '\0')
809 setvar(s, nullstr, 0);
810 if ((vp->flags & VEXPORT) && localevar(vp->text)) {
812 setlocale(LC_ALL, "");
814 vp->flags &= ~VEXPORT;
816 if ((vp->flags & VSTRFIXED) == 0) {
817 if ((vp->flags & VTEXTFIXED) == 0)
833 * Find the appropriate entry in the hash table from the name.
837 hashvar(const char *p)
839 unsigned int hashval;
841 hashval = ((unsigned char) *p) << 4;
842 while (*p && *p != '=')
843 hashval += (unsigned char) *p++;
844 return &vartab[hashval % VTABSIZE];
850 * Returns true if the two strings specify the same varable. The first
851 * variable name is terminated by '='; the second may be terminated by
852 * either '=' or '\0'.
856 varequal(const char *p, const char *q)
862 if (*p == '=' && *(q - 1) == '\0')