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 *);
94 static struct var voptind;
99 static const struct varinit varinit[] = {
101 { &vhistsize, VUNSET, "HISTSIZE=",
104 { &vifs, 0, "IFS= \t\n",
106 { &vmail, VUNSET, "MAIL=",
108 { &vmpath, VUNSET, "MAILPATH=",
110 { &vpath, 0, "PATH=" _PATH_DEFPATH,
113 * vps1 depends on uid
115 { &vps2, 0, "PS2=> ",
117 { &vps4, 0, "PS4=+ ",
120 { &vterm, VUNSET, "TERM=",
123 { &voptind, 0, "OPTIND=1",
125 { &vdisvfork, VUNSET, "SH_DISABLE_VFORK=",
131 static struct var *vartab[VTABSIZE];
133 static const char *const locale_names[7] = {
134 "LC_COLLATE", "LC_CTYPE", "LC_MONETARY",
135 "LC_NUMERIC", "LC_TIME", "LC_MESSAGES", NULL
137 static const int locale_categories[7] = {
138 LC_COLLATE, LC_CTYPE, LC_MONETARY, LC_NUMERIC, LC_TIME, LC_MESSAGES, 0
141 static int varequal(const char *, const char *);
142 static struct var *find_var(const char *, struct var ***, int *);
143 static int localevar(const char *);
145 extern char **environ;
148 * This routine initializes the builtin variables and imports the environment.
149 * It is called when the shell is initialized.
156 const struct varinit *ip;
161 for (ip = varinit ; (vp = ip->var) != NULL ; ip++) {
162 if (find_var(ip->text, &vpp, &vp->name_len) != NULL)
166 vp->text = __DECONST(char *, ip->text);
167 vp->flags = ip->flags | VSTRFIXED | VTEXTFIXED;
173 if (find_var("PS1", &vpp, &vps1.name_len) == NULL) {
176 vps1.text = __DECONST(char *, geteuid() ? "PS1=$ " : "PS1=# ");
177 vps1.flags = VSTRFIXED|VTEXTFIXED;
179 fmtstr(ppid, sizeof(ppid), "%d", (int)getppid());
180 setvarsafe("PPID", ppid, 0);
181 for (envp = environ ; *envp ; envp++) {
182 if (strchr(*envp, '=')) {
183 setvareq(*envp, VEXPORT|VTEXTFIXED);
186 setvareq("OPTIND=1", VTEXTFIXED);
190 * Safe version of setvar, returns 1 on success 0 on failure.
194 setvarsafe(const char *name, const char *val, int flags)
196 struct jmploc jmploc;
197 struct jmploc *const savehandler = handler;
202 if (setjmp(jmploc.loc))
206 setvar(name, val, flags);
208 handler = savehandler;
214 * Set the value of a variable. The flags argument is stored with the
215 * flags of the variable. If val is NULL, the variable is unset.
219 setvar(const char *name, const char *val, int flags)
234 if (! is_in_name(*p)) {
235 if (*p == '\0' || *p == '=')
243 error("%.*s: bad variable name", (int)namelen, name);
244 len = namelen + 2; /* 2 is space for '=' and '\0' */
249 vallen = strlen(val);
252 nameeq = ckmalloc(len);
253 memcpy(nameeq, name, namelen);
254 nameeq[namelen] = '=';
256 memcpy(nameeq + namelen + 1, val, vallen + 1);
258 nameeq[namelen + 1] = '\0';
259 setvareq(nameeq, flags);
263 localevar(const char *s)
265 const char *const *ss;
269 if (varequal(s + 1, "ANG"))
271 if (strncmp(s + 1, "C_", 2) != 0)
273 if (varequal(s + 3, "ALL"))
275 for (ss = locale_names; *ss ; ss++)
276 if (varequal(s + 3, *ss + 3))
283 * Sets/unsets an environment variable from a pointer that may actually be a
284 * pointer into environ where the string should not be manipulated.
287 change_env(const char *s, int set)
293 if ((eqp = strchr(ss, '=')) != NULL)
295 if (set && eqp != NULL)
296 (void) setenv(ss, eqp + 1, 1);
306 * Same as setvar except that the variable and value are passed in
307 * the first argument as name=value. Since the first argument will
308 * be actually stored in the table, it should not be a string that
313 setvareq(char *s, int flags)
315 struct var *vp, **vpp;
320 if (forcelocal && !(flags & (VNOSET | VNOLOCAL)))
322 vp = find_var(s, &vpp, &nlen);
324 if (vp->flags & VREADONLY)
325 error("%.*s: is read only", vp->name_len, s);
330 if (vp->func && (flags & VNOFUNC) == 0)
331 (*vp->func)(s + vp->name_len + 1);
333 if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0)
336 vp->flags &= ~(VTEXTFIXED|VSTACK|VUNSET);
341 * We could roll this to a function, to handle it as
342 * a regular variable function callback, but why bother?
344 * Note: this assumes iflag is not set to 1 initially.
345 * As part of initvar(), this is called before arguments
348 if ((vp == &vmpath || (vp == &vmail && ! mpathset())) &&
351 if ((vp->flags & VEXPORT) && localevar(s)) {
353 (void) setlocale(LC_ALL, "");
362 vp = ckmalloc(sizeof (*vp));
370 if ((vp->flags & VEXPORT) && localevar(s)) {
372 (void) setlocale(LC_ALL, "");
381 * Process a linked list of variable assignments.
385 listsetvar(struct strlist *list, int flags)
390 for (lp = list ; lp ; lp = lp->next) {
391 setvareq(savestr(lp->text), flags);
399 * Find the value of a variable. Returns NULL if not set.
403 lookupvar(const char *name)
407 v = find_var(name, NULL, NULL);
408 if (v == NULL || v->flags & VUNSET)
410 return v->text + v->name_len + 1;
416 * Search the environment of a builtin command. If the second argument
417 * is nonzero, return the value of a variable even if it hasn't been
422 bltinlookup(const char *name, int doall)
429 for (sp = cmdenviron ; sp ; sp = sp->next) {
430 if (varequal(sp->text, name))
431 result = strchr(sp->text, '=') + 1;
436 v = find_var(name, NULL, NULL);
437 if (v == NULL || v->flags & VUNSET ||
438 (!doall && (v->flags & VEXPORT) == 0))
440 return v->text + v->name_len + 1;
445 * Set up locale for a builtin (LANG/LC_* assignments).
455 for (lp = cmdenviron ; lp ; lp = lp->next) {
456 if (localevar(lp->text)) {
463 loc = bltinlookup("LC_ALL", 0);
466 setlocale(LC_ALL, loc);
471 locdef = bltinlookup("LANG", 0);
472 for (i = 0; locale_names[i] != NULL; i++) {
473 loc = bltinlookup(locale_names[i], 0);
477 setlocale(locale_categories[i], loc);
484 * Undo the effect of bltinlocaleset().
487 bltinunsetlocale(void)
492 for (lp = cmdenviron ; lp ; lp = lp->next) {
493 if (localevar(lp->text)) {
494 setlocale(LC_ALL, "");
503 * Update the localeisutf8 flag.
510 charset = nl_langinfo(CODESET);
511 localeisutf8 = !strcmp(charset, "UTF-8");
518 initial_localeisutf8 = localeisutf8;
522 * Generate a list of exported variables. This routine is used to construct
523 * the third argument to execve when executing a program.
535 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
536 for (vp = *vpp ; vp ; vp = vp->next)
537 if (vp->flags & VEXPORT)
540 ep = env = stalloc((nenv + 1) * sizeof *env);
541 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
542 for (vp = *vpp ; vp ; vp = vp->next)
543 if (vp->flags & VEXPORT)
552 var_compare(const void *a, const void *b)
554 const char *const *sa, *const *sb;
559 * This compares two var=value strings which creates a different
560 * order from what you would probably expect. POSIX is somewhat
561 * ambiguous on what should be sorted exactly.
563 return strcoll(*sa, *sb);
568 * Command to list all variables which are set. This is invoked from the
569 * set command when it is called without any options or operands.
573 showvarscmd(int argc __unused, char **argv __unused)
582 * POSIX requires us to sort the variables.
585 for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) {
586 for (vp = *vpp; vp; vp = vp->next) {
587 if (!(vp->flags & VUNSET))
593 vars = ckmalloc(n * sizeof(*vars));
595 for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) {
596 for (vp = *vpp; vp; vp = vp->next) {
597 if (!(vp->flags & VUNSET))
598 vars[i++] = vp->text;
602 qsort(vars, n, sizeof(*vars), var_compare);
603 for (i = 0; i < n; i++) {
605 * Skip improper variable names so the output remains usable as
608 if (!isassignment(vars[i]))
610 s = strchr(vars[i], '=');
612 outbin(vars[i], s - vars[i], out1);
625 * The export and readonly commands.
629 exportcmd(int argc __unused, char **argv)
638 int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT;
642 while ((ch = nextopt("p")) != '\0') {
650 if (values && *argptr != NULL)
651 error("-p requires no arguments");
652 if (*argptr != NULL) {
653 for (ap = argptr; (name = *ap) != NULL; ap++) {
654 if ((p = strchr(name, '=')) != NULL) {
657 vp = find_var(name, NULL, NULL);
660 if ((vp->flags & VEXPORT) && localevar(vp->text)) {
661 change_env(vp->text, 1);
662 (void) setlocale(LC_ALL, "");
668 setvar(name, p, flag);
671 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
672 for (vp = *vpp ; vp ; vp = vp->next) {
673 if (vp->flags & flag) {
676 * Skip improper variable names
677 * so the output remains usable
680 if (!isassignment(vp->text))
685 if (values && !(vp->flags & VUNSET)) {
687 vp->name_len + 1, out1);
691 outbin(vp->text, vp->name_len,
703 * The "local" command.
707 localcmd(int argc __unused, char **argv __unused)
713 error("Not in a function");
714 while ((name = *argptr++) != NULL) {
722 * Make a variable a local variable. When a variable is made local, it's
723 * value and flags are saved in a localvar structure. The saved values
724 * will be restored when the shell function returns. We handle the name
725 * "-" as a special case.
731 struct localvar *lvp;
736 lvp = ckmalloc(sizeof (struct localvar));
737 if (name[0] == '-' && name[1] == '\0') {
738 lvp->text = ckmalloc(sizeof optlist);
739 memcpy(lvp->text, optlist, sizeof optlist);
742 vp = find_var(name, &vpp, NULL);
744 if (strchr(name, '='))
745 setvareq(savestr(name), VSTRFIXED | VNOLOCAL);
747 setvar(name, NULL, VSTRFIXED | VNOLOCAL);
748 vp = *vpp; /* the new variable */
752 lvp->text = vp->text;
753 lvp->flags = vp->flags;
754 vp->flags |= VSTRFIXED|VTEXTFIXED;
755 if (name[vp->name_len] == '=')
756 setvareq(savestr(name), VNOLOCAL);
760 lvp->next = localvars;
767 * Called after a function returns.
773 struct localvar *lvp;
776 while ((lvp = localvars) != NULL) {
777 localvars = lvp->next;
779 if (vp == NULL) { /* $- saved */
780 memcpy(optlist, lvp->text, sizeof optlist);
783 } else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) {
784 (void)unsetvar(vp->text);
786 if ((vp->flags & VTEXTFIXED) == 0)
788 vp->flags = lvp->flags;
789 vp->text = lvp->text;
797 setvarcmd(int argc, char **argv)
800 return unsetcmd(argc, argv);
802 setvar(argv[1], argv[2], 0);
804 error("too many arguments");
810 * The unset builtin command.
814 unsetcmd(int argc __unused, char **argv __unused)
822 while ((i = nextopt("vf")) != '\0') {
828 if (flg_func == 0 && flg_var == 0)
831 for (ap = argptr; *ap ; ap++) {
833 ret |= unsetfunc(*ap);
835 ret |= unsetvar(*ap);
842 * Unset the specified variable.
846 unsetvar(const char *s)
851 vp = find_var(s, &vpp, NULL);
854 if (vp->flags & VREADONLY)
857 if (vp->text[vp->name_len + 1] != '\0')
858 setvar(s, nullstr, 0);
859 if ((vp->flags & VEXPORT) && localevar(vp->text)) {
861 setlocale(LC_ALL, "");
864 vp->flags &= ~VEXPORT;
866 if ((vp->flags & VSTRFIXED) == 0) {
867 if ((vp->flags & VTEXTFIXED) == 0)
879 * Returns true if the two strings specify the same variable. The first
880 * variable name is terminated by '='; the second may be terminated by
881 * either '=' or '\0'.
885 varequal(const char *p, const char *q)
891 if (*p == '=' && *(q - 1) == '\0')
897 * Search for a variable.
898 * 'name' may be terminated by '=' or a NUL.
899 * vppp is set to the pointer to vp, or the list head if vp isn't found
900 * lenp is set to the number of characters in 'name'
904 find_var(const char *name, struct var ***vppp, int *lenp)
906 unsigned int hashval;
908 struct var *vp, **vpp;
909 const char *p = name;
912 while (*p && *p != '=')
913 hashval = 2 * hashval + (unsigned char)*p++;
918 vpp = &vartab[hashval % VTABSIZE];
922 for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) {
923 if (vp->name_len != len)
925 if (memcmp(vp->text, name, len) != 0)