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, vp->text);
333 if (flags & VNOSET) {
334 if ((flags & (VTEXTFIXED|VSTACK)) == 0)
340 if (vp->func && (flags & VNOFUNC) == 0)
341 (*vp->func)(s + vp->name_len + 1);
343 if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0)
346 vp->flags &= ~(VTEXTFIXED|VSTACK|VUNSET);
351 * We could roll this to a function, to handle it as
352 * a regular variable function callback, but why bother?
354 * Note: this assumes iflag is not set to 1 initially.
355 * As part of initvar(), this is called before arguments
358 if ((vp == &vmpath || (vp == &vmail && ! mpathset())) &&
361 if ((vp->flags & VEXPORT) && localevar(s)) {
363 (void) setlocale(LC_ALL, "");
370 if (flags & VNOSET) {
371 if ((flags & (VTEXTFIXED|VSTACK)) == 0)
376 vp = ckmalloc(sizeof (*vp));
383 if ((vp->flags & VEXPORT) && localevar(s)) {
385 (void) setlocale(LC_ALL, "");
394 * Process a linked list of variable assignments.
398 listsetvar(struct strlist *list, int flags)
403 for (lp = list ; lp ; lp = lp->next) {
404 setvareq(savestr(lp->text), flags);
412 * Find the value of a variable. Returns NULL if not set.
416 lookupvar(const char *name)
420 v = find_var(name, NULL, NULL);
421 if (v == NULL || v->flags & VUNSET)
423 return v->text + v->name_len + 1;
429 * Search the environment of a builtin command. If the second argument
430 * is nonzero, return the value of a variable even if it hasn't been
435 bltinlookup(const char *name, int doall)
442 for (sp = cmdenviron ; sp ; sp = sp->next) {
443 if (varequal(sp->text, name))
444 result = strchr(sp->text, '=') + 1;
449 v = find_var(name, NULL, NULL);
450 if (v == NULL || v->flags & VUNSET ||
451 (!doall && (v->flags & VEXPORT) == 0))
453 return v->text + v->name_len + 1;
458 * Set up locale for a builtin (LANG/LC_* assignments).
468 for (lp = cmdenviron ; lp ; lp = lp->next) {
469 if (localevar(lp->text)) {
476 loc = bltinlookup("LC_ALL", 0);
479 setlocale(LC_ALL, loc);
484 locdef = bltinlookup("LANG", 0);
485 for (i = 0; locale_names[i] != NULL; i++) {
486 loc = bltinlookup(locale_names[i], 0);
490 setlocale(locale_categories[i], loc);
497 * Undo the effect of bltinlocaleset().
500 bltinunsetlocale(void)
505 for (lp = cmdenviron ; lp ; lp = lp->next) {
506 if (localevar(lp->text)) {
507 setlocale(LC_ALL, "");
516 * Update the localeisutf8 flag.
523 charset = nl_langinfo(CODESET);
524 localeisutf8 = !strcmp(charset, "UTF-8");
531 initial_localeisutf8 = localeisutf8;
535 * Generate a list of exported variables. This routine is used to construct
536 * the third argument to execve when executing a program.
548 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
549 for (vp = *vpp ; vp ; vp = vp->next)
550 if (vp->flags & VEXPORT)
553 ep = env = stalloc((nenv + 1) * sizeof *env);
554 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
555 for (vp = *vpp ; vp ; vp = vp->next)
556 if (vp->flags & VEXPORT)
565 var_compare(const void *a, const void *b)
567 const char *const *sa, *const *sb;
572 * This compares two var=value strings which creates a different
573 * order from what you would probably expect. POSIX is somewhat
574 * ambiguous on what should be sorted exactly.
576 return strcoll(*sa, *sb);
581 * Command to list all variables which are set. This is invoked from the
582 * set command when it is called without any options or operands.
586 showvarscmd(int argc __unused, char **argv __unused)
595 * POSIX requires us to sort the variables.
598 for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) {
599 for (vp = *vpp; vp; vp = vp->next) {
600 if (!(vp->flags & VUNSET))
606 vars = ckmalloc(n * sizeof(*vars));
608 for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) {
609 for (vp = *vpp; vp; vp = vp->next) {
610 if (!(vp->flags & VUNSET))
611 vars[i++] = vp->text;
615 qsort(vars, n, sizeof(*vars), var_compare);
616 for (i = 0; i < n; i++) {
618 * Skip improper variable names so the output remains usable as
621 if (!isassignment(vars[i]))
623 s = strchr(vars[i], '=');
625 outbin(vars[i], s - vars[i], out1);
638 * The export and readonly commands.
642 exportcmd(int argc __unused, char **argv)
651 int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT;
655 while ((ch = nextopt("p")) != '\0') {
663 if (values && *argptr != NULL)
664 error("-p requires no arguments");
665 if (*argptr != NULL) {
666 for (ap = argptr; (name = *ap) != NULL; ap++) {
667 if ((p = strchr(name, '=')) != NULL) {
670 vp = find_var(name, NULL, NULL);
673 if ((vp->flags & VEXPORT) && localevar(vp->text)) {
674 change_env(vp->text, 1);
675 (void) setlocale(LC_ALL, "");
681 setvar(name, p, flag);
684 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
685 for (vp = *vpp ; vp ; vp = vp->next) {
686 if (vp->flags & flag) {
689 * Skip improper variable names
690 * so the output remains usable
693 if (!isassignment(vp->text))
698 if (values && !(vp->flags & VUNSET)) {
700 vp->name_len + 1, out1);
704 outbin(vp->text, vp->name_len,
716 * The "local" command.
720 localcmd(int argc __unused, char **argv __unused)
726 error("Not in a function");
727 while ((name = *argptr++) != NULL) {
735 * Make a variable a local variable. When a variable is made local, it's
736 * value and flags are saved in a localvar structure. The saved values
737 * will be restored when the shell function returns. We handle the name
738 * "-" as a special case.
744 struct localvar *lvp;
749 lvp = ckmalloc(sizeof (struct localvar));
750 if (name[0] == '-' && name[1] == '\0') {
751 lvp->text = ckmalloc(sizeof optlist);
752 memcpy(lvp->text, optlist, sizeof optlist);
755 vp = find_var(name, &vpp, NULL);
757 if (strchr(name, '='))
758 setvareq(savestr(name), VSTRFIXED | VNOLOCAL);
760 setvar(name, NULL, VSTRFIXED | VNOLOCAL);
761 vp = *vpp; /* the new variable */
765 lvp->text = vp->text;
766 lvp->flags = vp->flags;
767 vp->flags |= VSTRFIXED|VTEXTFIXED;
768 if (name[vp->name_len] == '=')
769 setvareq(savestr(name), VNOLOCAL);
773 lvp->next = localvars;
780 * Called after a function returns.
786 struct localvar *lvp;
790 while ((lvp = localvars) != NULL) {
791 localvars = lvp->next;
793 if (vp == NULL) { /* $- saved */
794 memcpy(optlist, lvp->text, sizeof optlist);
797 } else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) {
798 (void)unsetvar(vp->text);
800 if ((vp->flags & VTEXTFIXED) == 0)
802 vp->flags = lvp->flags;
803 vp->text = lvp->text;
812 setvarcmd(int argc, char **argv)
815 return unsetcmd(argc, argv);
817 setvar(argv[1], argv[2], 0);
819 error("too many arguments");
825 * The unset builtin command.
829 unsetcmd(int argc __unused, char **argv __unused)
837 while ((i = nextopt("vf")) != '\0') {
843 if (flg_func == 0 && flg_var == 0)
847 for (ap = argptr; *ap ; ap++) {
849 ret |= unsetfunc(*ap);
851 ret |= unsetvar(*ap);
859 * Unset the specified variable.
860 * Called with interrupts off.
864 unsetvar(const char *s)
869 vp = find_var(s, &vpp, NULL);
872 if (vp->flags & VREADONLY)
874 if (vp->text[vp->name_len + 1] != '\0')
875 setvar(s, nullstr, 0);
876 if ((vp->flags & VEXPORT) && localevar(vp->text)) {
878 setlocale(LC_ALL, "");
881 vp->flags &= ~VEXPORT;
883 if ((vp->flags & VSTRFIXED) == 0) {
884 if ((vp->flags & VTEXTFIXED) == 0)
895 * Returns true if the two strings specify the same variable. The first
896 * variable name is terminated by '='; the second may be terminated by
897 * either '=' or '\0'.
901 varequal(const char *p, const char *q)
907 if (*p == '=' && *(q - 1) == '\0')
913 * Search for a variable.
914 * 'name' may be terminated by '=' or a NUL.
915 * vppp is set to the pointer to vp, or the list head if vp isn't found
916 * lenp is set to the number of characters in 'name'
920 find_var(const char *name, struct var ***vppp, int *lenp)
922 unsigned int hashval;
924 struct var *vp, **vpp;
925 const char *p = name;
928 while (*p && *p != '=')
929 hashval = 2 * hashval + (unsigned char)*p++;
934 vpp = &vartab[hashval % VTABSIZE];
938 for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) {
939 if (vp->name_len != len)
941 if (memcmp(vp->text, name, len) != 0)