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;
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,
113 { &vppid, VUNSET, "PPID=",
116 * vps1 depends on uid
118 { &vps2, 0, "PS2=> ",
120 { &vps4, 0, "PS4=+ ",
123 { &vterm, VUNSET, "TERM=",
126 { &voptind, 0, "OPTIND=1",
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 *);
147 * Initialize the variable symbol tables and import the environment.
152 MKINIT char **environ;
157 for (envp = environ ; *envp ; envp++) {
158 if (strchr(*envp, '=')) {
159 setvareq(*envp, VEXPORT|VTEXTFIXED);
167 * This routine initializes the builtin variables. It is called when the
168 * shell is initialized.
175 const struct varinit *ip;
179 for (ip = varinit ; (vp = ip->var) != NULL ; ip++) {
180 if (find_var(ip->text, &vpp, &vp->name_len) != NULL)
184 vp->text = __DECONST(char *, ip->text);
185 vp->flags = ip->flags | VSTRFIXED | VTEXTFIXED;
191 if (find_var("PS1", &vpp, &vps1.name_len) == NULL) {
194 vps1.text = __DECONST(char *, geteuid() ? "PS1=$ " : "PS1=# ");
195 vps1.flags = VSTRFIXED|VTEXTFIXED;
197 if ((vppid.flags & VEXPORT) == 0) {
198 fmtstr(ppid, sizeof(ppid), "%d", (int)getppid());
199 setvarsafe("PPID", ppid, 0);
204 * Safe version of setvar, returns 1 on success 0 on failure.
208 setvarsafe(const char *name, const char *val, int flags)
210 struct jmploc jmploc;
211 struct jmploc *const savehandler = handler;
216 if (setjmp(jmploc.loc))
220 setvar(name, val, flags);
222 handler = savehandler;
228 * Set the value of a variable. The flags argument is stored with the
229 * flags of the variable. If val is NULL, the variable is unset.
233 setvar(const char *name, const char *val, int flags)
247 if (! is_in_name(*p)) {
248 if (*p == '\0' || *p == '=')
256 error("%.*s: bad variable name", namelen, name);
257 len = namelen + 2; /* 2 is space for '=' and '\0' */
263 nameeq = ckmalloc(len);
264 memcpy(nameeq, name, namelen);
265 nameeq[namelen] = '=';
267 scopy(val, nameeq + namelen + 1);
269 nameeq[namelen + 1] = '\0';
270 setvareq(nameeq, flags);
274 localevar(const char *s)
276 const char *const *ss;
280 if (varequal(s + 1, "ANG"))
282 if (strncmp(s + 1, "C_", 2) != 0)
284 if (varequal(s + 3, "ALL"))
286 for (ss = locale_names; *ss ; ss++)
287 if (varequal(s + 3, *ss + 3))
294 * Sets/unsets an environment variable from a pointer that may actually be a
295 * pointer into environ where the string should not be manipulated.
298 change_env(const char *s, int set)
304 if ((eqp = strchr(ss, '=')) != NULL)
306 if (set && eqp != NULL)
307 (void) setenv(ss, eqp + 1, 1);
317 * Same as setvar except that the variable and value are passed in
318 * the first argument as name=value. Since the first argument will
319 * be actually stored in the table, it should not be a string that
324 setvareq(char *s, int flags)
326 struct var *vp, **vpp;
331 if (forcelocal && !(flags & (VNOSET | VNOLOCAL)))
333 vp = find_var(s, &vpp, &nlen);
335 if (vp->flags & VREADONLY)
336 error("%.*s: is read only", vp->name_len, s);
341 if (vp->func && (flags & VNOFUNC) == 0)
342 (*vp->func)(s + vp->name_len + 1);
344 if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0)
347 vp->flags &= ~(VTEXTFIXED|VSTACK|VUNSET);
352 * We could roll this to a function, to handle it as
353 * a regular variable function callback, but why bother?
355 * Note: this assumes iflag is not set to 1 initially.
356 * As part of init(), this is called before arguments
359 if ((vp == &vmpath || (vp == &vmail && ! mpathset())) &&
362 if ((vp->flags & VEXPORT) && localevar(s)) {
364 (void) setlocale(LC_ALL, "");
373 vp = ckmalloc(sizeof (*vp));
381 if ((vp->flags & VEXPORT) && localevar(s)) {
383 (void) setlocale(LC_ALL, "");
392 * Process a linked list of variable assignments.
396 listsetvar(struct strlist *list, int flags)
401 for (lp = list ; lp ; lp = lp->next) {
402 setvareq(savestr(lp->text), flags);
410 * Find the value of a variable. Returns NULL if not set.
414 lookupvar(const char *name)
418 v = find_var(name, NULL, NULL);
419 if (v == NULL || v->flags & VUNSET)
421 return v->text + v->name_len + 1;
427 * Search the environment of a builtin command. If the second argument
428 * is nonzero, return the value of a variable even if it hasn't been
433 bltinlookup(const char *name, int doall)
440 for (sp = cmdenviron ; sp ; sp = sp->next) {
441 if (varequal(sp->text, name))
442 result = strchr(sp->text, '=') + 1;
447 v = find_var(name, NULL, NULL);
448 if (v == NULL || v->flags & VUNSET ||
449 (!doall && (v->flags & VEXPORT) == 0))
451 return v->text + v->name_len + 1;
456 * Set up locale for a builtin (LANG/LC_* assignments).
466 for (lp = cmdenviron ; lp ; lp = lp->next) {
467 if (localevar(lp->text)) {
474 loc = bltinlookup("LC_ALL", 0);
477 setlocale(LC_ALL, loc);
482 locdef = bltinlookup("LANG", 0);
483 for (i = 0; locale_names[i] != NULL; i++) {
484 loc = bltinlookup(locale_names[i], 0);
488 setlocale(locale_categories[i], loc);
495 * Undo the effect of bltinlocaleset().
498 bltinunsetlocale(void)
503 for (lp = cmdenviron ; lp ; lp = lp->next) {
504 if (localevar(lp->text)) {
505 setlocale(LC_ALL, "");
514 * Update the localeisutf8 flag.
521 charset = nl_langinfo(CODESET);
522 localeisutf8 = !strcmp(charset, "UTF-8");
529 initial_localeisutf8 = localeisutf8;
533 * Generate a list of exported variables. This routine is used to construct
534 * the third argument to execve when executing a program.
546 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
547 for (vp = *vpp ; vp ; vp = vp->next)
548 if (vp->flags & VEXPORT)
551 ep = env = stalloc((nenv + 1) * sizeof *env);
552 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
553 for (vp = *vpp ; vp ; vp = vp->next)
554 if (vp->flags & VEXPORT)
563 var_compare(const void *a, const void *b)
565 const char *const *sa, *const *sb;
570 * This compares two var=value strings which creates a different
571 * order from what you would probably expect. POSIX is somewhat
572 * ambiguous on what should be sorted exactly.
574 return strcoll(*sa, *sb);
579 * Command to list all variables which are set. This is invoked from the
580 * set command when it is called without any options or operands.
584 showvarscmd(int argc __unused, char **argv __unused)
593 * POSIX requires us to sort the variables.
596 for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) {
597 for (vp = *vpp; vp; vp = vp->next) {
598 if (!(vp->flags & VUNSET))
604 vars = ckmalloc(n * sizeof(*vars));
606 for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) {
607 for (vp = *vpp; vp; vp = vp->next) {
608 if (!(vp->flags & VUNSET))
609 vars[i++] = vp->text;
613 qsort(vars, n, sizeof(*vars), var_compare);
614 for (i = 0; i < n; i++) {
615 s = strchr(vars[i], '=');
617 outbin(vars[i], s - vars[i], out1);
630 * The export and readonly commands.
634 exportcmd(int argc, char **argv)
642 int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT;
645 optreset = optind = 1;
648 while ((ch = getopt(argc, argv, "p")) != -1) {
655 error("unknown option: -%c", optopt);
661 if (values && argc != 0)
662 error("-p requires no arguments");
664 while ((name = *argv++) != NULL) {
665 if ((p = strchr(name, '=')) != NULL) {
668 vp = find_var(name, NULL, NULL);
671 if ((vp->flags & VEXPORT) && localevar(vp->text)) {
672 change_env(vp->text, 1);
673 (void) setlocale(LC_ALL, "");
679 setvar(name, p, flag);
682 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
683 for (vp = *vpp ; vp ; vp = vp->next) {
684 if (vp->flags & flag) {
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)
716 error("Not in a function");
717 while ((name = *argptr++) != NULL) {
725 * Make a variable a local variable. When a variable is made local, it's
726 * value and flags are saved in a localvar structure. The saved values
727 * will be restored when the shell function returns. We handle the name
728 * "-" as a special case.
734 struct localvar *lvp;
739 lvp = ckmalloc(sizeof (struct localvar));
740 if (name[0] == '-' && name[1] == '\0') {
741 lvp->text = ckmalloc(sizeof optlist);
742 memcpy(lvp->text, optlist, sizeof optlist);
745 vp = find_var(name, &vpp, NULL);
747 if (strchr(name, '='))
748 setvareq(savestr(name), VSTRFIXED | VNOLOCAL);
750 setvar(name, NULL, VSTRFIXED | VNOLOCAL);
751 vp = *vpp; /* the new variable */
755 lvp->text = vp->text;
756 lvp->flags = vp->flags;
757 vp->flags |= VSTRFIXED|VTEXTFIXED;
758 if (name[vp->name_len] == '=')
759 setvareq(savestr(name), VNOLOCAL);
763 lvp->next = localvars;
770 * Called after a function returns.
776 struct localvar *lvp;
779 while ((lvp = localvars) != NULL) {
780 localvars = lvp->next;
782 if (vp == NULL) { /* $- saved */
783 memcpy(optlist, lvp->text, sizeof optlist);
786 } else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) {
787 (void)unsetvar(vp->text);
789 if ((vp->flags & VTEXTFIXED) == 0)
791 vp->flags = lvp->flags;
792 vp->text = lvp->text;
800 setvarcmd(int argc, char **argv)
803 return unsetcmd(argc, argv);
805 setvar(argv[1], argv[2], 0);
807 error("too many arguments");
813 * The unset builtin command.
817 unsetcmd(int argc __unused, char **argv __unused)
825 while ((i = nextopt("vf")) != '\0') {
831 if (flg_func == 0 && flg_var == 0)
834 for (ap = argptr; *ap ; ap++) {
836 ret |= unsetfunc(*ap);
838 ret |= unsetvar(*ap);
845 * Unset the specified variable.
849 unsetvar(const char *s)
854 vp = find_var(s, &vpp, NULL);
857 if (vp->flags & VREADONLY)
860 if (vp->text[vp->name_len + 1] != '\0')
861 setvar(s, nullstr, 0);
862 if ((vp->flags & VEXPORT) && localevar(vp->text)) {
864 setlocale(LC_ALL, "");
867 vp->flags &= ~VEXPORT;
869 if ((vp->flags & VSTRFIXED) == 0) {
870 if ((vp->flags & VTEXTFIXED) == 0)
882 * Returns true if the two strings specify the same varable. The first
883 * variable name is terminated by '='; the second may be terminated by
884 * either '=' or '\0'.
888 varequal(const char *p, const char *q)
894 if (*p == '=' && *(q - 1) == '\0')
900 * Search for a variable.
901 * 'name' may be terminated by '=' or a NUL.
902 * vppp is set to the pointer to vp, or the list head if vp isn't found
903 * lenp is set to the number of charactets in 'name'
907 find_var(const char *name, struct var ***vppp, int *lenp)
909 unsigned int hashval;
911 struct var *vp, **vpp;
912 const char *p = name;
915 while (*p && *p != '=')
916 hashval = 2 * hashval + (unsigned char)*p++;
921 vpp = &vartab[hashval % VTABSIZE];
925 for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) {
926 if (vp->name_len != len)
928 if (memcmp(vp->text, name, len) != 0)