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);
253 nameeq = ckmalloc(len);
254 memcpy(nameeq, name, namelen);
255 nameeq[namelen] = '=';
257 memcpy(nameeq + namelen + 1, val, vallen + 1);
259 nameeq[namelen + 1] = '\0';
260 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)
296 if ((eqp = strchr(ss, '=')) != NULL)
298 if (set && eqp != NULL)
299 (void) setenv(ss, eqp + 1, 1);
310 * Same as setvar except that the variable and value are passed in
311 * the first argument as name=value. Since the first argument will
312 * be actually stored in the table, it should not be a string that
317 setvareq(char *s, int flags)
319 struct var *vp, **vpp;
324 if (forcelocal && !(flags & (VNOSET | VNOLOCAL)))
326 vp = find_var(s, &vpp, &nlen);
328 if (vp->flags & VREADONLY) {
329 if ((flags & (VTEXTFIXED|VSTACK)) == 0)
331 error("%.*s: is read only", vp->name_len, s);
337 if (vp->func && (flags & VNOFUNC) == 0)
338 (*vp->func)(s + vp->name_len + 1);
340 if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0)
343 vp->flags &= ~(VTEXTFIXED|VSTACK|VUNSET);
348 * We could roll this to a function, to handle it as
349 * a regular variable function callback, but why bother?
351 * Note: this assumes iflag is not set to 1 initially.
352 * As part of initvar(), this is called before arguments
355 if ((vp == &vmpath || (vp == &vmail && ! mpathset())) &&
358 if ((vp->flags & VEXPORT) && localevar(s)) {
360 (void) setlocale(LC_ALL, "");
370 vp = ckmalloc(sizeof (*vp));
377 if ((vp->flags & VEXPORT) && localevar(s)) {
379 (void) setlocale(LC_ALL, "");
388 * Process a linked list of variable assignments.
392 listsetvar(struct strlist *list, int flags)
397 for (lp = list ; lp ; lp = lp->next) {
398 setvareq(savestr(lp->text), flags);
406 * Find the value of a variable. Returns NULL if not set.
410 lookupvar(const char *name)
414 v = find_var(name, NULL, NULL);
415 if (v == NULL || v->flags & VUNSET)
417 return v->text + v->name_len + 1;
423 * Search the environment of a builtin command. If the second argument
424 * is nonzero, return the value of a variable even if it hasn't been
429 bltinlookup(const char *name, int doall)
436 for (sp = cmdenviron ; sp ; sp = sp->next) {
437 if (varequal(sp->text, name))
438 result = strchr(sp->text, '=') + 1;
443 v = find_var(name, NULL, NULL);
444 if (v == NULL || v->flags & VUNSET ||
445 (!doall && (v->flags & VEXPORT) == 0))
447 return v->text + v->name_len + 1;
452 * Set up locale for a builtin (LANG/LC_* assignments).
462 for (lp = cmdenviron ; lp ; lp = lp->next) {
463 if (localevar(lp->text)) {
470 loc = bltinlookup("LC_ALL", 0);
473 setlocale(LC_ALL, loc);
478 locdef = bltinlookup("LANG", 0);
479 for (i = 0; locale_names[i] != NULL; i++) {
480 loc = bltinlookup(locale_names[i], 0);
484 setlocale(locale_categories[i], loc);
491 * Undo the effect of bltinlocaleset().
494 bltinunsetlocale(void)
499 for (lp = cmdenviron ; lp ; lp = lp->next) {
500 if (localevar(lp->text)) {
501 setlocale(LC_ALL, "");
510 * Update the localeisutf8 flag.
517 charset = nl_langinfo(CODESET);
518 localeisutf8 = !strcmp(charset, "UTF-8");
525 initial_localeisutf8 = localeisutf8;
529 * Generate a list of exported variables. This routine is used to construct
530 * the third argument to execve when executing a program.
542 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
543 for (vp = *vpp ; vp ; vp = vp->next)
544 if (vp->flags & VEXPORT)
547 ep = env = stalloc((nenv + 1) * sizeof *env);
548 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
549 for (vp = *vpp ; vp ; vp = vp->next)
550 if (vp->flags & VEXPORT)
559 var_compare(const void *a, const void *b)
561 const char *const *sa, *const *sb;
566 * This compares two var=value strings which creates a different
567 * order from what you would probably expect. POSIX is somewhat
568 * ambiguous on what should be sorted exactly.
570 return strcoll(*sa, *sb);
575 * Command to list all variables which are set. This is invoked from the
576 * set command when it is called without any options or operands.
580 showvarscmd(int argc __unused, char **argv __unused)
589 * POSIX requires us to sort the variables.
592 for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) {
593 for (vp = *vpp; vp; vp = vp->next) {
594 if (!(vp->flags & VUNSET))
600 vars = ckmalloc(n * sizeof(*vars));
602 for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) {
603 for (vp = *vpp; vp; vp = vp->next) {
604 if (!(vp->flags & VUNSET))
605 vars[i++] = vp->text;
609 qsort(vars, n, sizeof(*vars), var_compare);
610 for (i = 0; i < n; i++) {
612 * Skip improper variable names so the output remains usable as
615 if (!isassignment(vars[i]))
617 s = strchr(vars[i], '=');
619 outbin(vars[i], s - vars[i], out1);
632 * The export and readonly commands.
636 exportcmd(int argc __unused, char **argv)
645 int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT;
649 while ((ch = nextopt("p")) != '\0') {
657 if (values && *argptr != NULL)
658 error("-p requires no arguments");
659 if (*argptr != NULL) {
660 for (ap = argptr; (name = *ap) != NULL; ap++) {
661 if ((p = strchr(name, '=')) != NULL) {
664 vp = find_var(name, NULL, NULL);
667 if ((vp->flags & VEXPORT) && localevar(vp->text)) {
668 change_env(vp->text, 1);
669 (void) setlocale(LC_ALL, "");
675 setvar(name, p, flag);
678 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
679 for (vp = *vpp ; vp ; vp = vp->next) {
680 if (vp->flags & flag) {
683 * Skip improper variable names
684 * so the output remains usable
687 if (!isassignment(vp->text))
692 if (values && !(vp->flags & VUNSET)) {
694 vp->name_len + 1, out1);
698 outbin(vp->text, vp->name_len,
710 * The "local" command.
714 localcmd(int argc __unused, char **argv __unused)
720 error("Not in a function");
721 while ((name = *argptr++) != NULL) {
729 * Make a variable a local variable. When a variable is made local, it's
730 * value and flags are saved in a localvar structure. The saved values
731 * will be restored when the shell function returns. We handle the name
732 * "-" as a special case.
738 struct localvar *lvp;
743 lvp = ckmalloc(sizeof (struct localvar));
744 if (name[0] == '-' && name[1] == '\0') {
745 lvp->text = ckmalloc(sizeof optlist);
746 memcpy(lvp->text, optlist, sizeof optlist);
749 vp = find_var(name, &vpp, NULL);
751 if (strchr(name, '='))
752 setvareq(savestr(name), VSTRFIXED | VNOLOCAL);
754 setvar(name, NULL, VSTRFIXED | VNOLOCAL);
755 vp = *vpp; /* the new variable */
759 lvp->text = vp->text;
760 lvp->flags = vp->flags;
761 vp->flags |= VSTRFIXED|VTEXTFIXED;
762 if (name[vp->name_len] == '=')
763 setvareq(savestr(name), VNOLOCAL);
767 lvp->next = localvars;
774 * Called after a function returns.
780 struct localvar *lvp;
784 while ((lvp = localvars) != NULL) {
785 localvars = lvp->next;
787 if (vp == NULL) { /* $- saved */
788 memcpy(optlist, lvp->text, sizeof optlist);
791 } else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) {
792 (void)unsetvar(vp->text);
794 if ((vp->flags & VTEXTFIXED) == 0)
796 vp->flags = lvp->flags;
797 vp->text = lvp->text;
806 setvarcmd(int argc, char **argv)
809 return unsetcmd(argc, argv);
811 setvar(argv[1], argv[2], 0);
813 error("too many arguments");
819 * The unset builtin command.
823 unsetcmd(int argc __unused, char **argv __unused)
831 while ((i = nextopt("vf")) != '\0') {
837 if (flg_func == 0 && flg_var == 0)
841 for (ap = argptr; *ap ; ap++) {
843 ret |= unsetfunc(*ap);
845 ret |= unsetvar(*ap);
853 * Unset the specified variable.
854 * Called with interrupts off.
858 unsetvar(const char *s)
863 vp = find_var(s, &vpp, NULL);
866 if (vp->flags & VREADONLY)
868 if (vp->text[vp->name_len + 1] != '\0')
869 setvar(s, nullstr, 0);
870 if ((vp->flags & VEXPORT) && localevar(vp->text)) {
872 setlocale(LC_ALL, "");
875 vp->flags &= ~VEXPORT;
877 if ((vp->flags & VSTRFIXED) == 0) {
878 if ((vp->flags & VTEXTFIXED) == 0)
889 * Returns true if the two strings specify the same variable. The first
890 * variable name is terminated by '='; the second may be terminated by
891 * either '=' or '\0'.
895 varequal(const char *p, const char *q)
901 if (*p == '=' && *(q - 1) == '\0')
907 * Search for a variable.
908 * 'name' may be terminated by '=' or a NUL.
909 * vppp is set to the pointer to vp, or the list head if vp isn't found
910 * lenp is set to the number of characters in 'name'
914 find_var(const char *name, struct var ***vppp, int *lenp)
916 unsigned int hashval;
918 struct var *vp, **vpp;
919 const char *p = name;
922 while (*p && *p != '=')
923 hashval = 2 * hashval + (unsigned char)*p++;
928 vpp = &vartab[hashval % VTABSIZE];
932 for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) {
933 if (vp->name_len != len)
935 if (memcmp(vp->text, name, len) != 0)