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 *);
96 static struct var voptind;
101 static const struct varinit varinit[] = {
103 { &vhistsize, VUNSET, "HISTSIZE=",
106 { &vifs, 0, "IFS= \t\n",
108 { &vmail, VUNSET, "MAIL=",
110 { &vmpath, VUNSET, "MAILPATH=",
112 { &vpath, 0, "PATH=" _PATH_DEFPATH,
114 { &vppid, VUNSET, "PPID=",
117 * vps1 depends on uid
119 { &vps2, 0, "PS2=> ",
121 { &vps4, 0, "PS4=+ ",
124 { &vterm, VUNSET, "TERM=",
127 { &voptind, 0, "OPTIND=1",
129 { &vdisvfork, VUNSET, "SH_DISABLE_VFORK=",
135 static struct var *vartab[VTABSIZE];
137 static const char *const locale_names[7] = {
138 "LC_COLLATE", "LC_CTYPE", "LC_MONETARY",
139 "LC_NUMERIC", "LC_TIME", "LC_MESSAGES", NULL
141 static const int locale_categories[7] = {
142 LC_COLLATE, LC_CTYPE, LC_MONETARY, LC_NUMERIC, LC_TIME, LC_MESSAGES, 0
145 static int varequal(const char *, const char *);
146 static struct var *find_var(const char *, struct var ***, int *);
147 static int localevar(const char *);
149 extern char **environ;
152 * This routine initializes the builtin variables and imports the environment.
153 * It is called when the shell is initialized.
160 const struct varinit *ip;
165 for (ip = varinit ; (vp = ip->var) != NULL ; ip++) {
166 if (find_var(ip->text, &vpp, &vp->name_len) != NULL)
170 vp->text = __DECONST(char *, ip->text);
171 vp->flags = ip->flags | VSTRFIXED | VTEXTFIXED;
177 if (find_var("PS1", &vpp, &vps1.name_len) == NULL) {
180 vps1.text = __DECONST(char *, geteuid() ? "PS1=$ " : "PS1=# ");
181 vps1.flags = VSTRFIXED|VTEXTFIXED;
183 if ((vppid.flags & VEXPORT) == 0) {
184 fmtstr(ppid, sizeof(ppid), "%d", (int)getppid());
185 setvarsafe("PPID", ppid, 0);
187 for (envp = environ ; *envp ; envp++) {
188 if (strchr(*envp, '=')) {
189 setvareq(*envp, VEXPORT|VTEXTFIXED);
195 * Safe version of setvar, returns 1 on success 0 on failure.
199 setvarsafe(const char *name, const char *val, int flags)
201 struct jmploc jmploc;
202 struct jmploc *const savehandler = handler;
207 if (setjmp(jmploc.loc))
211 setvar(name, val, flags);
213 handler = savehandler;
219 * Set the value of a variable. The flags argument is stored with the
220 * flags of the variable. If val is NULL, the variable is unset.
224 setvar(const char *name, const char *val, int flags)
238 if (! is_in_name(*p)) {
239 if (*p == '\0' || *p == '=')
247 error("%.*s: bad variable name", namelen, name);
248 len = namelen + 2; /* 2 is space for '=' and '\0' */
254 nameeq = ckmalloc(len);
255 memcpy(nameeq, name, namelen);
256 nameeq[namelen] = '=';
258 scopy(val, nameeq + namelen + 1);
260 nameeq[namelen + 1] = '\0';
261 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)
295 if ((eqp = strchr(ss, '=')) != NULL)
297 if (set && eqp != NULL)
298 (void) setenv(ss, eqp + 1, 1);
308 * Same as setvar except that the variable and value are passed in
309 * the first argument as name=value. Since the first argument will
310 * be actually stored in the table, it should not be a string that
315 setvareq(char *s, int flags)
317 struct var *vp, **vpp;
322 if (forcelocal && !(flags & (VNOSET | VNOLOCAL)))
324 vp = find_var(s, &vpp, &nlen);
326 if (vp->flags & VREADONLY)
327 error("%.*s: is read only", vp->name_len, s);
332 if (vp->func && (flags & VNOFUNC) == 0)
333 (*vp->func)(s + vp->name_len + 1);
335 if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0)
338 vp->flags &= ~(VTEXTFIXED|VSTACK|VUNSET);
343 * We could roll this to a function, to handle it as
344 * a regular variable function callback, but why bother?
346 * Note: this assumes iflag is not set to 1 initially.
347 * As part of initvar(), this is called before arguments
350 if ((vp == &vmpath || (vp == &vmail && ! mpathset())) &&
353 if ((vp->flags & VEXPORT) && localevar(s)) {
355 (void) setlocale(LC_ALL, "");
364 vp = ckmalloc(sizeof (*vp));
372 if ((vp->flags & VEXPORT) && localevar(s)) {
374 (void) setlocale(LC_ALL, "");
383 * Process a linked list of variable assignments.
387 listsetvar(struct strlist *list, int flags)
392 for (lp = list ; lp ; lp = lp->next) {
393 setvareq(savestr(lp->text), flags);
401 * Find the value of a variable. Returns NULL if not set.
405 lookupvar(const char *name)
409 v = find_var(name, NULL, NULL);
410 if (v == NULL || v->flags & VUNSET)
412 return v->text + v->name_len + 1;
418 * Search the environment of a builtin command. If the second argument
419 * is nonzero, return the value of a variable even if it hasn't been
424 bltinlookup(const char *name, int doall)
431 for (sp = cmdenviron ; sp ; sp = sp->next) {
432 if (varequal(sp->text, name))
433 result = strchr(sp->text, '=') + 1;
438 v = find_var(name, NULL, NULL);
439 if (v == NULL || v->flags & VUNSET ||
440 (!doall && (v->flags & VEXPORT) == 0))
442 return v->text + v->name_len + 1;
447 * Set up locale for a builtin (LANG/LC_* assignments).
457 for (lp = cmdenviron ; lp ; lp = lp->next) {
458 if (localevar(lp->text)) {
465 loc = bltinlookup("LC_ALL", 0);
468 setlocale(LC_ALL, loc);
473 locdef = bltinlookup("LANG", 0);
474 for (i = 0; locale_names[i] != NULL; i++) {
475 loc = bltinlookup(locale_names[i], 0);
479 setlocale(locale_categories[i], loc);
486 * Undo the effect of bltinlocaleset().
489 bltinunsetlocale(void)
494 for (lp = cmdenviron ; lp ; lp = lp->next) {
495 if (localevar(lp->text)) {
496 setlocale(LC_ALL, "");
505 * Update the localeisutf8 flag.
512 charset = nl_langinfo(CODESET);
513 localeisutf8 = !strcmp(charset, "UTF-8");
520 initial_localeisutf8 = localeisutf8;
524 * Generate a list of exported variables. This routine is used to construct
525 * the third argument to execve when executing a program.
537 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
538 for (vp = *vpp ; vp ; vp = vp->next)
539 if (vp->flags & VEXPORT)
542 ep = env = stalloc((nenv + 1) * sizeof *env);
543 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
544 for (vp = *vpp ; vp ; vp = vp->next)
545 if (vp->flags & VEXPORT)
554 var_compare(const void *a, const void *b)
556 const char *const *sa, *const *sb;
561 * This compares two var=value strings which creates a different
562 * order from what you would probably expect. POSIX is somewhat
563 * ambiguous on what should be sorted exactly.
565 return strcoll(*sa, *sb);
570 * Command to list all variables which are set. This is invoked from the
571 * set command when it is called without any options or operands.
575 showvarscmd(int argc __unused, char **argv __unused)
584 * POSIX requires us to sort the variables.
587 for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) {
588 for (vp = *vpp; vp; vp = vp->next) {
589 if (!(vp->flags & VUNSET))
595 vars = ckmalloc(n * sizeof(*vars));
597 for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) {
598 for (vp = *vpp; vp; vp = vp->next) {
599 if (!(vp->flags & VUNSET))
600 vars[i++] = vp->text;
604 qsort(vars, n, sizeof(*vars), var_compare);
605 for (i = 0; i < n; i++) {
607 * Skip improper variable names so the output remains usable as
610 if (!isassignment(vars[i]))
612 s = strchr(vars[i], '=');
614 outbin(vars[i], s - vars[i], out1);
627 * The export and readonly commands.
631 exportcmd(int argc __unused, char **argv)
640 int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT;
644 while ((ch = nextopt("p")) != '\0') {
652 if (values && *argptr != NULL)
653 error("-p requires no arguments");
654 if (*argptr != NULL) {
655 for (ap = argptr; (name = *ap) != NULL; ap++) {
656 if ((p = strchr(name, '=')) != NULL) {
659 vp = find_var(name, NULL, NULL);
662 if ((vp->flags & VEXPORT) && localevar(vp->text)) {
663 change_env(vp->text, 1);
664 (void) setlocale(LC_ALL, "");
670 setvar(name, p, flag);
673 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
674 for (vp = *vpp ; vp ; vp = vp->next) {
675 if (vp->flags & flag) {
678 * Skip improper variable names
679 * so the output remains usable
682 if (!isassignment(vp->text))
687 if (values && !(vp->flags & VUNSET)) {
689 vp->name_len + 1, out1);
693 outbin(vp->text, vp->name_len,
705 * The "local" command.
709 localcmd(int argc __unused, char **argv __unused)
714 error("Not in a function");
715 while ((name = *argptr++) != NULL) {
723 * Make a variable a local variable. When a variable is made local, it's
724 * value and flags are saved in a localvar structure. The saved values
725 * will be restored when the shell function returns. We handle the name
726 * "-" as a special case.
732 struct localvar *lvp;
737 lvp = ckmalloc(sizeof (struct localvar));
738 if (name[0] == '-' && name[1] == '\0') {
739 lvp->text = ckmalloc(sizeof optlist);
740 memcpy(lvp->text, optlist, sizeof optlist);
743 vp = find_var(name, &vpp, NULL);
745 if (strchr(name, '='))
746 setvareq(savestr(name), VSTRFIXED | VNOLOCAL);
748 setvar(name, NULL, VSTRFIXED | VNOLOCAL);
749 vp = *vpp; /* the new variable */
753 lvp->text = vp->text;
754 lvp->flags = vp->flags;
755 vp->flags |= VSTRFIXED|VTEXTFIXED;
756 if (name[vp->name_len] == '=')
757 setvareq(savestr(name), VNOLOCAL);
761 lvp->next = localvars;
768 * Called after a function returns.
774 struct localvar *lvp;
777 while ((lvp = localvars) != NULL) {
778 localvars = lvp->next;
780 if (vp == NULL) { /* $- saved */
781 memcpy(optlist, lvp->text, sizeof optlist);
784 } else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) {
785 (void)unsetvar(vp->text);
787 if ((vp->flags & VTEXTFIXED) == 0)
789 vp->flags = lvp->flags;
790 vp->text = lvp->text;
798 setvarcmd(int argc, char **argv)
801 return unsetcmd(argc, argv);
803 setvar(argv[1], argv[2], 0);
805 error("too many arguments");
811 * The unset builtin command.
815 unsetcmd(int argc __unused, char **argv __unused)
823 while ((i = nextopt("vf")) != '\0') {
829 if (flg_func == 0 && flg_var == 0)
832 for (ap = argptr; *ap ; ap++) {
834 ret |= unsetfunc(*ap);
836 ret |= unsetvar(*ap);
843 * Unset the specified variable.
847 unsetvar(const char *s)
852 vp = find_var(s, &vpp, NULL);
855 if (vp->flags & VREADONLY)
858 if (vp->text[vp->name_len + 1] != '\0')
859 setvar(s, nullstr, 0);
860 if ((vp->flags & VEXPORT) && localevar(vp->text)) {
862 setlocale(LC_ALL, "");
865 vp->flags &= ~VEXPORT;
867 if ((vp->flags & VSTRFIXED) == 0) {
868 if ((vp->flags & VTEXTFIXED) == 0)
880 * Returns true if the two strings specify the same variable. The first
881 * variable name is terminated by '='; the second may be terminated by
882 * either '=' or '\0'.
886 varequal(const char *p, const char *q)
892 if (*p == '=' && *(q - 1) == '\0')
898 * Search for a variable.
899 * 'name' may be terminated by '=' or a NUL.
900 * vppp is set to the pointer to vp, or the list head if vp isn't found
901 * lenp is set to the number of characters in 'name'
905 find_var(const char *name, struct var ***vppp, int *lenp)
907 unsigned int hashval;
909 struct var *vp, **vpp;
910 const char *p = name;
913 while (*p && *p != '=')
914 hashval = 2 * hashval + (unsigned char)*p++;
919 vpp = &vartab[hashval % VTABSIZE];
923 for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) {
924 if (vp->name_len != len)
926 if (memcmp(vp->text, name, len) != 0)