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;
97 struct localvar *localvars;
100 static const struct varinit varinit[] = {
102 { &vhistsize, VUNSET, "HISTSIZE=",
105 { &vifs, 0, "IFS= \t\n",
107 { &vmail, VUNSET, "MAIL=",
109 { &vmpath, VUNSET, "MAILPATH=",
111 { &vpath, 0, "PATH=" _PATH_DEFPATH,
114 * vps1 depends on uid
116 { &vps2, 0, "PS2=> ",
118 { &vps4, 0, "PS4=+ ",
121 { &vterm, VUNSET, "TERM=",
124 { &voptind, 0, "OPTIND=1",
126 { &vdisvfork, VUNSET, "SH_DISABLE_VFORK=",
132 static struct var *vartab[VTABSIZE];
134 static const char *const locale_names[7] = {
135 "LC_COLLATE", "LC_CTYPE", "LC_MONETARY",
136 "LC_NUMERIC", "LC_TIME", "LC_MESSAGES", NULL
138 static const int locale_categories[7] = {
139 LC_COLLATE, LC_CTYPE, LC_MONETARY, LC_NUMERIC, LC_TIME, LC_MESSAGES, 0
142 static int varequal(const char *, const char *);
143 static struct var *find_var(const char *, struct var ***, int *);
144 static int localevar(const char *);
145 static void setvareq_const(const char *s, int flags);
147 extern char **environ;
150 * This routine initializes the builtin variables and imports the environment.
151 * It is called when the shell is initialized.
158 const struct varinit *ip;
163 for (ip = varinit ; (vp = ip->var) != NULL ; ip++) {
164 if (find_var(ip->text, &vpp, &vp->name_len) != NULL)
168 vp->text = __DECONST(char *, ip->text);
169 vp->flags = ip->flags | VSTRFIXED | VTEXTFIXED;
175 if (find_var("PS1", &vpp, &vps1.name_len) == NULL) {
178 vps1.text = __DECONST(char *, geteuid() ? "PS1=$ " : "PS1=# ");
179 vps1.flags = VSTRFIXED|VTEXTFIXED;
181 fmtstr(ppid, sizeof(ppid), "%d", (int)getppid());
182 setvarsafe("PPID", ppid, 0);
183 for (envp = environ ; *envp ; envp++) {
184 if (strchr(*envp, '=')) {
185 setvareq(*envp, VEXPORT|VTEXTFIXED);
188 setvareq_const("OPTIND=1", 0);
189 setvareq_const("IFS= \t\n", 0);
193 * Safe version of setvar, returns 1 on success 0 on failure.
197 setvarsafe(const char *name, const char *val, int flags)
199 struct jmploc jmploc;
200 struct jmploc *const savehandler = handler;
205 if (setjmp(jmploc.loc))
209 setvar(name, val, flags);
211 handler = savehandler;
217 * Set the value of a variable. The flags argument is stored with the
218 * flags of the variable. If val is NULL, the variable is unset.
222 setvar(const char *name, const char *val, int flags)
237 if (! is_in_name(*p)) {
238 if (*p == '\0' || *p == '=')
246 error("%.*s: bad variable name", (int)namelen, name);
247 len = namelen + 2; /* 2 is space for '=' and '\0' */
252 vallen = strlen(val);
256 nameeq = ckmalloc(len);
257 memcpy(nameeq, name, namelen);
258 nameeq[namelen] = '=';
260 memcpy(nameeq + namelen + 1, val, vallen + 1);
262 nameeq[namelen + 1] = '\0';
263 setvareq(nameeq, flags);
268 localevar(const char *s)
270 const char *const *ss;
274 if (varequal(s + 1, "ANG"))
276 if (strncmp(s + 1, "C_", 2) != 0)
278 if (varequal(s + 3, "ALL"))
280 for (ss = locale_names; *ss ; ss++)
281 if (varequal(s + 3, *ss + 3))
288 * Sets/unsets an environment variable from a pointer that may actually be a
289 * pointer into environ where the string should not be manipulated.
292 change_env(const char *s, int set)
299 if ((eqp = strchr(ss, '=')) != NULL)
301 if (set && eqp != NULL)
302 (void) setenv(ss, eqp + 1, 1);
313 * Same as setvar except that the variable and value are passed in
314 * the first argument as name=value. Since the first argument will
315 * be actually stored in the table, it should not be a string that
320 setvareq(char *s, int flags)
322 struct var *vp, **vpp;
327 if (forcelocal && !(flags & (VNOSET | VNOLOCAL)))
329 vp = find_var(s, &vpp, &nlen);
331 if (vp->flags & VREADONLY) {
332 if ((flags & (VTEXTFIXED|VSTACK)) == 0)
334 error("%.*s: is read only", vp->name_len, vp->text);
336 if (flags & VNOSET) {
337 if ((flags & (VTEXTFIXED|VSTACK)) == 0)
343 if (vp->func && (flags & VNOFUNC) == 0)
344 (*vp->func)(s + vp->name_len + 1);
346 if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0)
349 vp->flags &= ~(VTEXTFIXED|VSTACK|VUNSET);
354 * We could roll this to a function, to handle it as
355 * a regular variable function callback, but why bother?
357 * Note: this assumes iflag is not set to 1 initially.
358 * As part of initvar(), this is called before arguments
361 if ((vp == &vmpath || (vp == &vmail && ! mpathset())) &&
364 if ((vp->flags & VEXPORT) && localevar(s)) {
366 (void) setlocale(LC_ALL, "");
373 if (flags & VNOSET) {
374 if ((flags & (VTEXTFIXED|VSTACK)) == 0)
379 vp = ckmalloc(sizeof (*vp));
386 if ((vp->flags & VEXPORT) && localevar(s)) {
388 (void) setlocale(LC_ALL, "");
396 setvareq_const(const char *s, int flags)
398 setvareq(__DECONST(char *, s), flags | VTEXTFIXED);
403 * Process a linked list of variable assignments.
407 listsetvar(struct arglist *list, int flags)
412 for (i = 0; i < list->count; i++)
413 setvareq(savestr(list->args[i]), flags);
420 * Find the value of a variable. Returns NULL if not set.
424 lookupvar(const char *name)
428 v = find_var(name, NULL, NULL);
429 if (v == NULL || v->flags & VUNSET)
431 return v->text + v->name_len + 1;
437 * Search the environment of a builtin command. If the second argument
438 * is nonzero, return the value of a variable even if it hasn't been
443 bltinlookup(const char *name, int doall)
450 if (cmdenviron) for (i = 0; i < cmdenviron->count; i++) {
451 if (varequal(cmdenviron->args[i], name))
452 result = strchr(cmdenviron->args[i], '=') + 1;
457 v = find_var(name, NULL, NULL);
458 if (v == NULL || v->flags & VUNSET ||
459 (!doall && (v->flags & VEXPORT) == 0))
461 return v->text + v->name_len + 1;
466 * Set up locale for a builtin (LANG/LC_* assignments).
475 if (cmdenviron) for (i = 0; i < cmdenviron->count; i++) {
476 if (localevar(cmdenviron->args[i])) {
483 loc = bltinlookup("LC_ALL", 0);
486 setlocale(LC_ALL, loc);
491 locdef = bltinlookup("LANG", 0);
492 for (i = 0; locale_names[i] != NULL; i++) {
493 loc = bltinlookup(locale_names[i], 0);
497 setlocale(locale_categories[i], loc);
504 * Undo the effect of bltinlocaleset().
507 bltinunsetlocale(void)
512 if (cmdenviron) for (i = 0; i < cmdenviron->count; i++) {
513 if (localevar(cmdenviron->args[i])) {
514 setlocale(LC_ALL, "");
523 * Update the localeisutf8 flag.
530 charset = nl_langinfo(CODESET);
531 localeisutf8 = !strcmp(charset, "UTF-8");
538 initial_localeisutf8 = localeisutf8;
542 * Generate a list of exported variables. This routine is used to construct
543 * the third argument to execve when executing a program.
555 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
556 for (vp = *vpp ; vp ; vp = vp->next)
557 if (vp->flags & VEXPORT)
560 ep = env = stalloc((nenv + 1) * sizeof *env);
561 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
562 for (vp = *vpp ; vp ; vp = vp->next)
563 if (vp->flags & VEXPORT)
572 var_compare(const void *a, const void *b)
574 const char *const *sa, *const *sb;
579 * This compares two var=value strings which creates a different
580 * order from what you would probably expect. POSIX is somewhat
581 * ambiguous on what should be sorted exactly.
583 return strcoll(*sa, *sb);
588 * Command to list all variables which are set. This is invoked from the
589 * set command when it is called without any options or operands.
593 showvarscmd(int argc __unused, char **argv __unused)
602 * POSIX requires us to sort the variables.
605 for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) {
606 for (vp = *vpp; vp; vp = vp->next) {
607 if (!(vp->flags & VUNSET))
613 vars = ckmalloc(n * sizeof(*vars));
615 for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) {
616 for (vp = *vpp; vp; vp = vp->next) {
617 if (!(vp->flags & VUNSET))
618 vars[i++] = vp->text;
622 qsort(vars, n, sizeof(*vars), var_compare);
623 for (i = 0; i < n; i++) {
625 * Skip improper variable names so the output remains usable as
628 if (!isassignment(vars[i]))
630 s = strchr(vars[i], '=');
632 outbin(vars[i], s - vars[i], out1);
645 * The export and readonly commands.
649 exportcmd(int argc __unused, char **argv)
658 int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT;
662 while ((ch = nextopt("p")) != '\0') {
670 if (values && *argptr != NULL)
671 error("-p requires no arguments");
672 if (*argptr != NULL) {
673 for (ap = argptr; (name = *ap) != NULL; ap++) {
674 if ((p = strchr(name, '=')) != NULL) {
677 vp = find_var(name, NULL, NULL);
680 if ((vp->flags & VEXPORT) && localevar(vp->text)) {
681 change_env(vp->text, 1);
682 (void) setlocale(LC_ALL, "");
688 setvar(name, p, flag);
691 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
692 for (vp = *vpp ; vp ; vp = vp->next) {
693 if (vp->flags & flag) {
696 * Skip improper variable names
697 * so the output remains usable
700 if (!isassignment(vp->text))
705 if (values && !(vp->flags & VUNSET)) {
707 vp->name_len + 1, out1);
711 outbin(vp->text, vp->name_len,
723 * The "local" command.
727 localcmd(int argc __unused, char **argv __unused)
733 error("Not in a function");
734 while ((name = *argptr++) != NULL) {
742 * Make a variable a local variable. When a variable is made local, it's
743 * value and flags are saved in a localvar structure. The saved values
744 * will be restored when the shell function returns. We handle the name
745 * "-" as a special case.
751 struct localvar *lvp;
756 lvp = ckmalloc(sizeof (struct localvar));
757 if (name[0] == '-' && name[1] == '\0') {
758 lvp->text = ckmalloc(sizeof optval);
759 memcpy(lvp->text, optval, sizeof optval);
762 vp = find_var(name, &vpp, NULL);
764 if (strchr(name, '='))
765 setvareq(savestr(name), VSTRFIXED | VNOLOCAL);
767 setvar(name, NULL, VSTRFIXED | VNOLOCAL);
768 vp = *vpp; /* the new variable */
772 lvp->text = vp->text;
773 lvp->flags = vp->flags;
774 vp->flags |= VSTRFIXED|VTEXTFIXED;
775 if (name[vp->name_len] == '=')
776 setvareq(savestr(name), VNOLOCAL);
780 lvp->next = localvars;
787 * Called after a function returns.
793 struct localvar *lvp;
798 while ((lvp = localvars) != NULL) {
799 localvars = lvp->next;
801 if (vp == NULL) { /* $- saved */
802 memcpy(optval, lvp->text, sizeof optval);
805 } else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) {
806 vp->flags &= ~VREADONLY;
807 (void)unsetvar(vp->text);
809 islocalevar = (vp->flags | lvp->flags) & VEXPORT &&
810 localevar(lvp->text);
811 if ((vp->flags & VTEXTFIXED) == 0)
813 vp->flags = lvp->flags;
814 vp->text = lvp->text;
816 (*vp->func)(vp->text + vp->name_len + 1);
818 change_env(vp->text, vp->flags & VEXPORT &&
819 (vp->flags & VUNSET) == 0);
820 setlocale(LC_ALL, "");
831 setvarcmd(int argc, char **argv)
834 return unsetcmd(argc, argv);
836 setvar(argv[1], argv[2], 0);
838 error("too many arguments");
844 * The unset builtin command.
848 unsetcmd(int argc __unused, char **argv __unused)
856 while ((i = nextopt("vf")) != '\0') {
862 if (flg_func == 0 && flg_var == 0)
866 for (ap = argptr; *ap ; ap++) {
868 ret |= unsetfunc(*ap);
870 ret |= unsetvar(*ap);
878 * Unset the specified variable.
879 * Called with interrupts off.
883 unsetvar(const char *s)
888 vp = find_var(s, &vpp, NULL);
891 if (vp->flags & VREADONLY)
893 if (vp->text[vp->name_len + 1] != '\0')
895 if ((vp->flags & VEXPORT) && localevar(vp->text)) {
897 setlocale(LC_ALL, "");
900 vp->flags &= ~VEXPORT;
902 if ((vp->flags & VSTRFIXED) == 0) {
903 if ((vp->flags & VTEXTFIXED) == 0)
914 * Returns true if the two strings specify the same variable. The first
915 * variable name is terminated by '='; the second may be terminated by
916 * either '=' or '\0'.
920 varequal(const char *p, const char *q)
926 if (*p == '=' && *(q - 1) == '\0')
932 * Search for a variable.
933 * 'name' may be terminated by '=' or a NUL.
934 * vppp is set to the pointer to vp, or the list head if vp isn't found
935 * lenp is set to the number of characters in 'name'
939 find_var(const char *name, struct var ***vppp, int *lenp)
941 unsigned int hashval;
943 struct var *vp, **vpp;
944 const char *p = name;
947 while (*p && *p != '=')
948 hashval = 2 * hashval + (unsigned char)*p++;
953 vpp = &vartab[hashval % VTABSIZE];
957 for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) {
958 if (vp->name_len != len)
960 if (memcmp(vp->text, name, len) != 0)