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 *);
150 * Initialize the variable symbol tables and import the environment.
155 MKINIT char **environ;
160 for (envp = environ ; *envp ; envp++) {
161 if (strchr(*envp, '=')) {
162 setvareq(*envp, VEXPORT|VTEXTFIXED);
170 * This routine initializes the builtin variables. It is called when the
171 * shell is initialized.
178 const struct varinit *ip;
182 for (ip = varinit ; (vp = ip->var) != NULL ; ip++) {
183 if (find_var(ip->text, &vpp, &vp->name_len) != NULL)
187 vp->text = __DECONST(char *, ip->text);
188 vp->flags = ip->flags | VSTRFIXED | VTEXTFIXED;
194 if (find_var("PS1", &vpp, &vps1.name_len) == NULL) {
197 vps1.text = __DECONST(char *, geteuid() ? "PS1=$ " : "PS1=# ");
198 vps1.flags = VSTRFIXED|VTEXTFIXED;
200 if ((vppid.flags & VEXPORT) == 0) {
201 fmtstr(ppid, sizeof(ppid), "%d", (int)getppid());
202 setvarsafe("PPID", ppid, 0);
207 * Safe version of setvar, returns 1 on success 0 on failure.
211 setvarsafe(const char *name, const char *val, int flags)
213 struct jmploc jmploc;
214 struct jmploc *const savehandler = handler;
219 if (setjmp(jmploc.loc))
223 setvar(name, val, flags);
225 handler = savehandler;
231 * Set the value of a variable. The flags argument is stored with the
232 * flags of the variable. If val is NULL, the variable is unset.
236 setvar(const char *name, const char *val, int flags)
250 if (! is_in_name(*p)) {
251 if (*p == '\0' || *p == '=')
259 error("%.*s: bad variable name", namelen, name);
260 len = namelen + 2; /* 2 is space for '=' and '\0' */
267 nameeq = ckmalloc(len);
268 memcpy(nameeq, name, namelen);
269 nameeq[namelen] = '=';
271 scopy(val, nameeq + namelen + 1);
273 nameeq[namelen + 1] = '\0';
274 setvareq(nameeq, flags);
279 localevar(const char *s)
281 const char *const *ss;
285 if (varequal(s + 1, "ANG"))
287 if (strncmp(s + 1, "C_", 2) != 0)
289 if (varequal(s + 3, "ALL"))
291 for (ss = locale_names; *ss ; ss++)
292 if (varequal(s + 3, *ss + 3))
299 * Sets/unsets an environment variable from a pointer that may actually be a
300 * pointer into environ where the string should not be manipulated.
303 change_env(const char *s, int set)
310 if ((eqp = strchr(ss, '=')) != NULL)
312 if (set && eqp != NULL)
313 (void) setenv(ss, eqp + 1, 1);
324 * Same as setvar except that the variable and value are passed in
325 * the first argument as name=value. Since the first argument will
326 * be actually stored in the table, it should not be a string that
331 setvareq(char *s, int flags)
333 struct var *vp, **vpp;
338 if (forcelocal && !(flags & (VNOSET | VNOLOCAL)))
340 vp = find_var(s, &vpp, &nlen);
342 if (vp->flags & VREADONLY) {
343 if ((flags & (VTEXTFIXED|VSTACK)) == 0)
345 error("%.*s: is read only", vp->name_len, s);
351 if (vp->func && (flags & VNOFUNC) == 0)
352 (*vp->func)(s + vp->name_len + 1);
354 if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0)
357 vp->flags &= ~(VTEXTFIXED|VSTACK|VUNSET);
362 * We could roll this to a function, to handle it as
363 * a regular variable function callback, but why bother?
365 * Note: this assumes iflag is not set to 1 initially.
366 * As part of init(), this is called before arguments
369 if ((vp == &vmpath || (vp == &vmail && ! mpathset())) &&
372 if ((vp->flags & VEXPORT) && localevar(s)) {
374 (void) setlocale(LC_ALL, "");
384 vp = ckmalloc(sizeof (*vp));
391 if ((vp->flags & VEXPORT) && localevar(s)) {
393 (void) setlocale(LC_ALL, "");
402 * Process a linked list of variable assignments.
406 listsetvar(struct strlist *list, int flags)
411 for (lp = list ; lp ; lp = lp->next) {
412 setvareq(savestr(lp->text), 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 for (sp = cmdenviron ; sp ; sp = sp->next) {
451 if (varequal(sp->text, name))
452 result = strchr(sp->text, '=') + 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).
476 for (lp = cmdenviron ; lp ; lp = lp->next) {
477 if (localevar(lp->text)) {
484 loc = bltinlookup("LC_ALL", 0);
487 setlocale(LC_ALL, loc);
492 locdef = bltinlookup("LANG", 0);
493 for (i = 0; locale_names[i] != NULL; i++) {
494 loc = bltinlookup(locale_names[i], 0);
498 setlocale(locale_categories[i], loc);
505 * Undo the effect of bltinlocaleset().
508 bltinunsetlocale(void)
513 for (lp = cmdenviron ; lp ; lp = lp->next) {
514 if (localevar(lp->text)) {
515 setlocale(LC_ALL, "");
524 * Update the localeisutf8 flag.
531 charset = nl_langinfo(CODESET);
532 localeisutf8 = !strcmp(charset, "UTF-8");
539 initial_localeisutf8 = localeisutf8;
543 * Generate a list of exported variables. This routine is used to construct
544 * the third argument to execve when executing a program.
556 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
557 for (vp = *vpp ; vp ; vp = vp->next)
558 if (vp->flags & VEXPORT)
561 ep = env = stalloc((nenv + 1) * sizeof *env);
562 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
563 for (vp = *vpp ; vp ; vp = vp->next)
564 if (vp->flags & VEXPORT)
573 var_compare(const void *a, const void *b)
575 const char *const *sa, *const *sb;
580 * This compares two var=value strings which creates a different
581 * order from what you would probably expect. POSIX is somewhat
582 * ambiguous on what should be sorted exactly.
584 return strcoll(*sa, *sb);
589 * Command to list all variables which are set. This is invoked from the
590 * set command when it is called without any options or operands.
594 showvarscmd(int argc __unused, char **argv __unused)
603 * POSIX requires us to sort the variables.
606 for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) {
607 for (vp = *vpp; vp; vp = vp->next) {
608 if (!(vp->flags & VUNSET))
614 vars = ckmalloc(n * sizeof(*vars));
616 for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) {
617 for (vp = *vpp; vp; vp = vp->next) {
618 if (!(vp->flags & VUNSET))
619 vars[i++] = vp->text;
623 qsort(vars, n, sizeof(*vars), var_compare);
624 for (i = 0; i < n; i++) {
626 * Skip improper variable names so the output remains usable as
629 if (!isassignment(vars[i]))
631 s = strchr(vars[i], '=');
633 outbin(vars[i], s - vars[i], out1);
646 * The export and readonly commands.
650 exportcmd(int argc, char **argv)
658 int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT;
661 optreset = optind = 1;
664 while ((ch = getopt(argc, argv, "p")) != -1) {
671 error("unknown option: -%c", optopt);
677 if (values && argc != 0)
678 error("-p requires no arguments");
680 while ((name = *argv++) != NULL) {
681 if ((p = strchr(name, '=')) != NULL) {
684 vp = find_var(name, NULL, NULL);
687 if ((vp->flags & VEXPORT) && localevar(vp->text)) {
688 change_env(vp->text, 1);
689 (void) setlocale(LC_ALL, "");
695 setvar(name, p, flag);
698 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
699 for (vp = *vpp ; vp ; vp = vp->next) {
700 if (vp->flags & flag) {
703 * Skip improper variable names
704 * so the output remains usable
707 if (!isassignment(vp->text))
712 if (values && !(vp->flags & VUNSET)) {
714 vp->name_len + 1, out1);
718 outbin(vp->text, vp->name_len,
730 * The "local" command.
734 localcmd(int argc __unused, char **argv __unused)
739 error("Not in a function");
740 while ((name = *argptr++) != NULL) {
748 * Make a variable a local variable. When a variable is made local, it's
749 * value and flags are saved in a localvar structure. The saved values
750 * will be restored when the shell function returns. We handle the name
751 * "-" as a special case.
757 struct localvar *lvp;
762 lvp = ckmalloc(sizeof (struct localvar));
763 if (name[0] == '-' && name[1] == '\0') {
764 lvp->text = ckmalloc(sizeof optlist);
765 memcpy(lvp->text, optlist, sizeof optlist);
768 vp = find_var(name, &vpp, NULL);
770 if (strchr(name, '='))
771 setvareq(savestr(name), VSTRFIXED | VNOLOCAL);
773 setvar(name, NULL, VSTRFIXED | VNOLOCAL);
774 vp = *vpp; /* the new variable */
778 lvp->text = vp->text;
779 lvp->flags = vp->flags;
780 vp->flags |= VSTRFIXED|VTEXTFIXED;
781 if (name[vp->name_len] == '=')
782 setvareq(savestr(name), VNOLOCAL);
786 lvp->next = localvars;
793 * Called after a function returns.
799 struct localvar *lvp;
803 while ((lvp = localvars) != NULL) {
804 localvars = lvp->next;
806 if (vp == NULL) { /* $- saved */
807 memcpy(optlist, lvp->text, sizeof optlist);
810 } else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) {
811 (void)unsetvar(vp->text);
813 if ((vp->flags & VTEXTFIXED) == 0)
815 vp->flags = lvp->flags;
816 vp->text = lvp->text;
825 setvarcmd(int argc, char **argv)
828 return unsetcmd(argc, argv);
830 setvar(argv[1], argv[2], 0);
832 error("too many arguments");
838 * The unset builtin command.
842 unsetcmd(int argc __unused, char **argv __unused)
850 while ((i = nextopt("vf")) != '\0') {
856 if (flg_func == 0 && flg_var == 0)
860 for (ap = argptr; *ap ; ap++) {
862 ret |= unsetfunc(*ap);
864 ret |= unsetvar(*ap);
872 * Unset the specified variable.
873 * Called with interrupts off.
877 unsetvar(const char *s)
882 vp = find_var(s, &vpp, NULL);
885 if (vp->flags & VREADONLY)
887 if (vp->text[vp->name_len + 1] != '\0')
888 setvar(s, nullstr, 0);
889 if ((vp->flags & VEXPORT) && localevar(vp->text)) {
891 setlocale(LC_ALL, "");
894 vp->flags &= ~VEXPORT;
896 if ((vp->flags & VSTRFIXED) == 0) {
897 if ((vp->flags & VTEXTFIXED) == 0)
908 * Returns true if the two strings specify the same varable. The first
909 * variable name is terminated by '='; the second may be terminated by
910 * either '=' or '\0'.
914 varequal(const char *p, const char *q)
920 if (*p == '=' && *(q - 1) == '\0')
926 * Search for a variable.
927 * 'name' may be terminated by '=' or a NUL.
928 * vppp is set to the pointer to vp, or the list head if vp isn't found
929 * lenp is set to the number of charactets in 'name'
933 find_var(const char *name, struct var ***vppp, int *lenp)
935 unsigned int hashval;
937 struct var *vp, **vpp;
938 const char *p = name;
941 while (*p && *p != '=')
942 hashval = 2 * hashval + (unsigned char)*p++;
947 vpp = &vartab[hashval % VTABSIZE];
951 for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) {
952 if (vp->name_len != len)
954 if (memcmp(vp->text, name, len) != 0)