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$");
55 #include "nodes.h" /* for other headers */
56 #include "eval.h" /* defines cmdenviron */
68 #include "myhistedit.h"
79 void (*func)(const char *);
96 static struct var voptind;
101 static const struct varinit varinit[] = {
103 { &vhistsize, VUNSET, "HISTSIZE=",
106 { &vifs, 0, "IFS= \t\n",
108 { &vmail, VUNSET, "MAIL=",
110 { &vmpath, VUNSET, "MAILPATH=",
112 { &vpath, 0, "PATH=" _PATH_DEFPATH,
114 { &vppid, VUNSET, "PPID=",
117 * vps1 depends on uid
119 { &vps2, 0, "PS2=> ",
121 { &vps4, 0, "PS4=+ ",
124 { &vterm, VUNSET, "TERM=",
127 { &voptind, 0, "OPTIND=1",
129 { &vdisvfork, VUNSET, "SH_DISABLE_VFORK=",
135 static struct var *vartab[VTABSIZE];
137 static const char *const locale_names[7] = {
138 "LC_COLLATE", "LC_CTYPE", "LC_MONETARY",
139 "LC_NUMERIC", "LC_TIME", "LC_MESSAGES", NULL
141 static const int locale_categories[7] = {
142 LC_COLLATE, LC_CTYPE, LC_MONETARY, LC_NUMERIC, LC_TIME, LC_MESSAGES, 0
145 static int varequal(const char *, const char *);
146 static struct var *find_var(const char *, struct var ***, int *);
147 static int localevar(const char *);
149 extern char **environ;
152 * This routine initializes the builtin variables and imports the environment.
153 * It is called when the shell is initialized.
160 const struct varinit *ip;
165 for (ip = varinit ; (vp = ip->var) != NULL ; ip++) {
166 if (find_var(ip->text, &vpp, &vp->name_len) != NULL)
170 vp->text = __DECONST(char *, ip->text);
171 vp->flags = ip->flags | VSTRFIXED | VTEXTFIXED;
177 if (find_var("PS1", &vpp, &vps1.name_len) == NULL) {
180 vps1.text = __DECONST(char *, geteuid() ? "PS1=$ " : "PS1=# ");
181 vps1.flags = VSTRFIXED|VTEXTFIXED;
183 if ((vppid.flags & VEXPORT) == 0) {
184 fmtstr(ppid, sizeof(ppid), "%d", (int)getppid());
185 setvarsafe("PPID", ppid, 0);
187 for (envp = environ ; *envp ; envp++) {
188 if (strchr(*envp, '=')) {
189 setvareq(*envp, VEXPORT|VTEXTFIXED);
195 * Safe version of setvar, returns 1 on success 0 on failure.
199 setvarsafe(const char *name, const char *val, int flags)
201 struct jmploc jmploc;
202 struct jmploc *const savehandler = handler;
207 if (setjmp(jmploc.loc))
211 setvar(name, val, flags);
213 handler = savehandler;
219 * Set the value of a variable. The flags argument is stored with the
220 * flags of the variable. If val is NULL, the variable is unset.
224 setvar(const char *name, const char *val, int flags)
238 if (! is_in_name(*p)) {
239 if (*p == '\0' || *p == '=')
247 error("%.*s: bad variable name", namelen, name);
248 len = namelen + 2; /* 2 is space for '=' and '\0' */
254 nameeq = ckmalloc(len);
255 memcpy(nameeq, name, namelen);
256 nameeq[namelen] = '=';
258 scopy(val, nameeq + namelen + 1);
260 nameeq[namelen + 1] = '\0';
261 setvareq(nameeq, flags);
265 localevar(const char *s)
267 const char *const *ss;
271 if (varequal(s + 1, "ANG"))
273 if (strncmp(s + 1, "C_", 2) != 0)
275 if (varequal(s + 3, "ALL"))
277 for (ss = locale_names; *ss ; ss++)
278 if (varequal(s + 3, *ss + 3))
285 * Sets/unsets an environment variable from a pointer that may actually be a
286 * pointer into environ where the string should not be manipulated.
289 change_env(const char *s, int set)
295 if ((eqp = strchr(ss, '=')) != NULL)
297 if (set && eqp != NULL)
298 (void) setenv(ss, eqp + 1, 1);
308 * Same as setvar except that the variable and value are passed in
309 * the first argument as name=value. Since the first argument will
310 * be actually stored in the table, it should not be a string that
315 setvareq(char *s, int flags)
317 struct var *vp, **vpp;
322 if (forcelocal && !(flags & (VNOSET | VNOLOCAL)))
324 vp = find_var(s, &vpp, &nlen);
326 if (vp->flags & VREADONLY)
327 error("%.*s: is read only", vp->name_len, s);
332 if (vp->func && (flags & VNOFUNC) == 0)
333 (*vp->func)(s + vp->name_len + 1);
335 if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0)
338 vp->flags &= ~(VTEXTFIXED|VSTACK|VUNSET);
343 * We could roll this to a function, to handle it as
344 * a regular variable function callback, but why bother?
346 * Note: this assumes iflag is not set to 1 initially.
347 * As part of initvar(), this is called before arguments
350 if ((vp == &vmpath || (vp == &vmail && ! mpathset())) &&
353 if ((vp->flags & VEXPORT) && localevar(s)) {
355 (void) setlocale(LC_ALL, "");
364 vp = ckmalloc(sizeof (*vp));
372 if ((vp->flags & VEXPORT) && localevar(s)) {
374 (void) setlocale(LC_ALL, "");
383 * Process a linked list of variable assignments.
387 listsetvar(struct strlist *list, int flags)
392 for (lp = list ; lp ; lp = lp->next) {
393 setvareq(savestr(lp->text), flags);
401 * Find the value of a variable. Returns NULL if not set.
405 lookupvar(const char *name)
409 v = find_var(name, NULL, NULL);
410 if (v == NULL || v->flags & VUNSET)
412 return v->text + v->name_len + 1;
418 * Search the environment of a builtin command. If the second argument
419 * is nonzero, return the value of a variable even if it hasn't been
424 bltinlookup(const char *name, int doall)
431 for (sp = cmdenviron ; sp ; sp = sp->next) {
432 if (varequal(sp->text, name))
433 result = strchr(sp->text, '=') + 1;
438 v = find_var(name, NULL, NULL);
439 if (v == NULL || v->flags & VUNSET ||
440 (!doall && (v->flags & VEXPORT) == 0))
442 return v->text + v->name_len + 1;
447 * Set up locale for a builtin (LANG/LC_* assignments).
457 for (lp = cmdenviron ; lp ; lp = lp->next) {
458 if (localevar(lp->text)) {
465 loc = bltinlookup("LC_ALL", 0);
468 setlocale(LC_ALL, loc);
473 locdef = bltinlookup("LANG", 0);
474 for (i = 0; locale_names[i] != NULL; i++) {
475 loc = bltinlookup(locale_names[i], 0);
479 setlocale(locale_categories[i], loc);
486 * Undo the effect of bltinlocaleset().
489 bltinunsetlocale(void)
494 for (lp = cmdenviron ; lp ; lp = lp->next) {
495 if (localevar(lp->text)) {
496 setlocale(LC_ALL, "");
505 * Update the localeisutf8 flag.
512 charset = nl_langinfo(CODESET);
513 localeisutf8 = !strcmp(charset, "UTF-8");
520 initial_localeisutf8 = localeisutf8;
524 * Generate a list of exported variables. This routine is used to construct
525 * the third argument to execve when executing a program.
537 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
538 for (vp = *vpp ; vp ; vp = vp->next)
539 if (vp->flags & VEXPORT)
542 ep = env = stalloc((nenv + 1) * sizeof *env);
543 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
544 for (vp = *vpp ; vp ; vp = vp->next)
545 if (vp->flags & VEXPORT)
554 var_compare(const void *a, const void *b)
556 const char *const *sa, *const *sb;
561 * This compares two var=value strings which creates a different
562 * order from what you would probably expect. POSIX is somewhat
563 * ambiguous on what should be sorted exactly.
565 return strcoll(*sa, *sb);
570 * Command to list all variables which are set. This is invoked from the
571 * set command when it is called without any options or operands.
575 showvarscmd(int argc __unused, char **argv __unused)
584 * POSIX requires us to sort the variables.
587 for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) {
588 for (vp = *vpp; vp; vp = vp->next) {
589 if (!(vp->flags & VUNSET))
595 vars = ckmalloc(n * sizeof(*vars));
597 for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) {
598 for (vp = *vpp; vp; vp = vp->next) {
599 if (!(vp->flags & VUNSET))
600 vars[i++] = vp->text;
604 qsort(vars, n, sizeof(*vars), var_compare);
605 for (i = 0; i < n; i++) {
607 * Skip improper variable names so the output remains usable as
610 if (!isassignment(vars[i]))
612 s = strchr(vars[i], '=');
614 outbin(vars[i], s - vars[i], out1);
627 * The export and readonly commands.
631 exportcmd(int argc __unused, char **argv)
640 int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT;
644 while ((ch = nextopt("p")) != '\0') {
652 if (values && *argptr != NULL)
653 error("-p requires no arguments");
654 if (*argptr != NULL) {
655 for (ap = argptr; (name = *ap) != NULL; ap++) {
656 if ((p = strchr(name, '=')) != NULL) {
659 vp = find_var(name, NULL, NULL);
662 if ((vp->flags & VEXPORT) && localevar(vp->text)) {
663 change_env(vp->text, 1);
664 (void) setlocale(LC_ALL, "");
670 setvar(name, p, flag);
673 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
674 for (vp = *vpp ; vp ; vp = vp->next) {
675 if (vp->flags & flag) {
678 * Skip improper variable names
679 * so the output remains usable
682 if (!isassignment(vp->text))
687 if (values && !(vp->flags & VUNSET)) {
689 vp->name_len + 1, out1);
693 outbin(vp->text, vp->name_len,
705 * The "local" command.
709 localcmd(int argc __unused, char **argv __unused)
715 error("Not in a function");
716 while ((name = *argptr++) != NULL) {
724 * Make a variable a local variable. When a variable is made local, it's
725 * value and flags are saved in a localvar structure. The saved values
726 * will be restored when the shell function returns. We handle the name
727 * "-" as a special case.
733 struct localvar *lvp;
738 lvp = ckmalloc(sizeof (struct localvar));
739 if (name[0] == '-' && name[1] == '\0') {
740 lvp->text = ckmalloc(sizeof optlist);
741 memcpy(lvp->text, optlist, sizeof optlist);
744 vp = find_var(name, &vpp, NULL);
746 if (strchr(name, '='))
747 setvareq(savestr(name), VSTRFIXED | VNOLOCAL);
749 setvar(name, NULL, VSTRFIXED | VNOLOCAL);
750 vp = *vpp; /* the new variable */
754 lvp->text = vp->text;
755 lvp->flags = vp->flags;
756 vp->flags |= VSTRFIXED|VTEXTFIXED;
757 if (name[vp->name_len] == '=')
758 setvareq(savestr(name), VNOLOCAL);
762 lvp->next = localvars;
769 * Called after a function returns.
775 struct localvar *lvp;
778 while ((lvp = localvars) != NULL) {
779 localvars = lvp->next;
781 if (vp == NULL) { /* $- saved */
782 memcpy(optlist, lvp->text, sizeof optlist);
785 } else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) {
786 (void)unsetvar(vp->text);
788 if ((vp->flags & VTEXTFIXED) == 0)
790 vp->flags = lvp->flags;
791 vp->text = lvp->text;
799 setvarcmd(int argc, char **argv)
802 return unsetcmd(argc, argv);
804 setvar(argv[1], argv[2], 0);
806 error("too many arguments");
812 * The unset builtin command.
816 unsetcmd(int argc __unused, char **argv __unused)
824 while ((i = nextopt("vf")) != '\0') {
830 if (flg_func == 0 && flg_var == 0)
833 for (ap = argptr; *ap ; ap++) {
835 ret |= unsetfunc(*ap);
837 ret |= unsetvar(*ap);
844 * Unset the specified variable.
848 unsetvar(const char *s)
853 vp = find_var(s, &vpp, NULL);
856 if (vp->flags & VREADONLY)
859 if (vp->text[vp->name_len + 1] != '\0')
860 setvar(s, nullstr, 0);
861 if ((vp->flags & VEXPORT) && localevar(vp->text)) {
863 setlocale(LC_ALL, "");
866 vp->flags &= ~VEXPORT;
868 if ((vp->flags & VSTRFIXED) == 0) {
869 if ((vp->flags & VTEXTFIXED) == 0)
881 * Returns true if the two strings specify the same variable. The first
882 * variable name is terminated by '='; the second may be terminated by
883 * either '=' or '\0'.
887 varequal(const char *p, const char *q)
893 if (*p == '=' && *(q - 1) == '\0')
899 * Search for a variable.
900 * 'name' may be terminated by '=' or a NUL.
901 * vppp is set to the pointer to vp, or the list head if vp isn't found
902 * lenp is set to the number of characters in 'name'
906 find_var(const char *name, struct var ***vppp, int *lenp)
908 unsigned int hashval;
910 struct var *vp, **vpp;
911 const char *p = name;
914 while (*p && *p != '=')
915 hashval = 2 * hashval + (unsigned char)*p++;
920 vpp = &vartab[hashval % VTABSIZE];
924 for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) {
925 if (vp->name_len != len)
927 if (memcmp(vp->text, name, len) != 0)