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 error("%.*s: is read only", vp->name_len, s);
334 if (vp->func && (flags & VNOFUNC) == 0)
335 (*vp->func)(s + vp->name_len + 1);
337 if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0)
340 vp->flags &= ~(VTEXTFIXED|VSTACK|VUNSET);
345 * We could roll this to a function, to handle it as
346 * a regular variable function callback, but why bother?
348 * Note: this assumes iflag is not set to 1 initially.
349 * As part of initvar(), this is called before arguments
352 if ((vp == &vmpath || (vp == &vmail && ! mpathset())) &&
355 if ((vp->flags & VEXPORT) && localevar(s)) {
357 (void) setlocale(LC_ALL, "");
367 vp = ckmalloc(sizeof (*vp));
374 if ((vp->flags & VEXPORT) && localevar(s)) {
376 (void) setlocale(LC_ALL, "");
385 * Process a linked list of variable assignments.
389 listsetvar(struct strlist *list, int flags)
394 for (lp = list ; lp ; lp = lp->next) {
395 setvareq(savestr(lp->text), flags);
403 * Find the value of a variable. Returns NULL if not set.
407 lookupvar(const char *name)
411 v = find_var(name, NULL, NULL);
412 if (v == NULL || v->flags & VUNSET)
414 return v->text + v->name_len + 1;
420 * Search the environment of a builtin command. If the second argument
421 * is nonzero, return the value of a variable even if it hasn't been
426 bltinlookup(const char *name, int doall)
433 for (sp = cmdenviron ; sp ; sp = sp->next) {
434 if (varequal(sp->text, name))
435 result = strchr(sp->text, '=') + 1;
440 v = find_var(name, NULL, NULL);
441 if (v == NULL || v->flags & VUNSET ||
442 (!doall && (v->flags & VEXPORT) == 0))
444 return v->text + v->name_len + 1;
449 * Set up locale for a builtin (LANG/LC_* assignments).
459 for (lp = cmdenviron ; lp ; lp = lp->next) {
460 if (localevar(lp->text)) {
467 loc = bltinlookup("LC_ALL", 0);
470 setlocale(LC_ALL, loc);
475 locdef = bltinlookup("LANG", 0);
476 for (i = 0; locale_names[i] != NULL; i++) {
477 loc = bltinlookup(locale_names[i], 0);
481 setlocale(locale_categories[i], loc);
488 * Undo the effect of bltinlocaleset().
491 bltinunsetlocale(void)
496 for (lp = cmdenviron ; lp ; lp = lp->next) {
497 if (localevar(lp->text)) {
498 setlocale(LC_ALL, "");
507 * Update the localeisutf8 flag.
514 charset = nl_langinfo(CODESET);
515 localeisutf8 = !strcmp(charset, "UTF-8");
522 initial_localeisutf8 = localeisutf8;
526 * Generate a list of exported variables. This routine is used to construct
527 * the third argument to execve when executing a program.
539 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
540 for (vp = *vpp ; vp ; vp = vp->next)
541 if (vp->flags & VEXPORT)
544 ep = env = stalloc((nenv + 1) * sizeof *env);
545 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
546 for (vp = *vpp ; vp ; vp = vp->next)
547 if (vp->flags & VEXPORT)
556 var_compare(const void *a, const void *b)
558 const char *const *sa, *const *sb;
563 * This compares two var=value strings which creates a different
564 * order from what you would probably expect. POSIX is somewhat
565 * ambiguous on what should be sorted exactly.
567 return strcoll(*sa, *sb);
572 * Command to list all variables which are set. This is invoked from the
573 * set command when it is called without any options or operands.
577 showvarscmd(int argc __unused, char **argv __unused)
586 * POSIX requires us to sort the variables.
589 for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) {
590 for (vp = *vpp; vp; vp = vp->next) {
591 if (!(vp->flags & VUNSET))
597 vars = ckmalloc(n * sizeof(*vars));
599 for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) {
600 for (vp = *vpp; vp; vp = vp->next) {
601 if (!(vp->flags & VUNSET))
602 vars[i++] = vp->text;
606 qsort(vars, n, sizeof(*vars), var_compare);
607 for (i = 0; i < n; i++) {
609 * Skip improper variable names so the output remains usable as
612 if (!isassignment(vars[i]))
614 s = strchr(vars[i], '=');
616 outbin(vars[i], s - vars[i], out1);
629 * The export and readonly commands.
633 exportcmd(int argc __unused, char **argv)
642 int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT;
646 while ((ch = nextopt("p")) != '\0') {
654 if (values && *argptr != NULL)
655 error("-p requires no arguments");
656 if (*argptr != NULL) {
657 for (ap = argptr; (name = *ap) != NULL; ap++) {
658 if ((p = strchr(name, '=')) != NULL) {
661 vp = find_var(name, NULL, NULL);
664 if ((vp->flags & VEXPORT) && localevar(vp->text)) {
665 change_env(vp->text, 1);
666 (void) setlocale(LC_ALL, "");
672 setvar(name, p, flag);
675 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
676 for (vp = *vpp ; vp ; vp = vp->next) {
677 if (vp->flags & flag) {
680 * Skip improper variable names
681 * so the output remains usable
684 if (!isassignment(vp->text))
689 if (values && !(vp->flags & VUNSET)) {
691 vp->name_len + 1, out1);
695 outbin(vp->text, vp->name_len,
707 * The "local" command.
711 localcmd(int argc __unused, char **argv __unused)
717 error("Not in a function");
718 while ((name = *argptr++) != NULL) {
726 * Make a variable a local variable. When a variable is made local, it's
727 * value and flags are saved in a localvar structure. The saved values
728 * will be restored when the shell function returns. We handle the name
729 * "-" as a special case.
735 struct localvar *lvp;
740 lvp = ckmalloc(sizeof (struct localvar));
741 if (name[0] == '-' && name[1] == '\0') {
742 lvp->text = ckmalloc(sizeof optlist);
743 memcpy(lvp->text, optlist, sizeof optlist);
746 vp = find_var(name, &vpp, NULL);
748 if (strchr(name, '='))
749 setvareq(savestr(name), VSTRFIXED | VNOLOCAL);
751 setvar(name, NULL, VSTRFIXED | VNOLOCAL);
752 vp = *vpp; /* the new variable */
756 lvp->text = vp->text;
757 lvp->flags = vp->flags;
758 vp->flags |= VSTRFIXED|VTEXTFIXED;
759 if (name[vp->name_len] == '=')
760 setvareq(savestr(name), VNOLOCAL);
764 lvp->next = localvars;
771 * Called after a function returns.
777 struct localvar *lvp;
781 while ((lvp = localvars) != NULL) {
782 localvars = lvp->next;
784 if (vp == NULL) { /* $- saved */
785 memcpy(optlist, lvp->text, sizeof optlist);
788 } else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) {
789 (void)unsetvar(vp->text);
791 if ((vp->flags & VTEXTFIXED) == 0)
793 vp->flags = lvp->flags;
794 vp->text = lvp->text;
803 setvarcmd(int argc, char **argv)
806 return unsetcmd(argc, argv);
808 setvar(argv[1], argv[2], 0);
810 error("too many arguments");
816 * The unset builtin command.
820 unsetcmd(int argc __unused, char **argv __unused)
828 while ((i = nextopt("vf")) != '\0') {
834 if (flg_func == 0 && flg_var == 0)
838 for (ap = argptr; *ap ; ap++) {
840 ret |= unsetfunc(*ap);
842 ret |= unsetvar(*ap);
850 * Unset the specified variable.
851 * Called with interrupts off.
855 unsetvar(const char *s)
860 vp = find_var(s, &vpp, NULL);
863 if (vp->flags & VREADONLY)
865 if (vp->text[vp->name_len + 1] != '\0')
866 setvar(s, nullstr, 0);
867 if ((vp->flags & VEXPORT) && localevar(vp->text)) {
869 setlocale(LC_ALL, "");
872 vp->flags &= ~VEXPORT;
874 if ((vp->flags & VSTRFIXED) == 0) {
875 if ((vp->flags & VTEXTFIXED) == 0)
886 * Returns true if the two strings specify the same variable. The first
887 * variable name is terminated by '='; the second may be terminated by
888 * either '=' or '\0'.
892 varequal(const char *p, const char *q)
898 if (*p == '=' && *(q - 1) == '\0')
904 * Search for a variable.
905 * 'name' may be terminated by '=' or a NUL.
906 * vppp is set to the pointer to vp, or the list head if vp isn't found
907 * lenp is set to the number of characters in 'name'
911 find_var(const char *name, struct var ***vppp, int *lenp)
913 unsigned int hashval;
915 struct var *vp, **vpp;
916 const char *p = name;
919 while (*p && *p != '=')
920 hashval = 2 * hashval + (unsigned char)*p++;
925 vpp = &vartab[hashval % VTABSIZE];
929 for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) {
930 if (vp->name_len != len)
932 if (memcmp(vp->text, name, len) != 0)