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 */
67 #include "myhistedit.h"
78 void (*func)(const char *);
95 static struct var voptind;
97 static const struct varinit varinit[] = {
99 { &vhistsize, VUNSET, "HISTSIZE=",
102 { &vifs, 0, "IFS= \t\n",
104 { &vmail, VUNSET, "MAIL=",
106 { &vmpath, VUNSET, "MAILPATH=",
108 { &vpath, 0, "PATH=" _PATH_DEFPATH,
110 { &vppid, VUNSET, "PPID=",
113 * vps1 depends on uid
115 { &vps2, 0, "PS2=> ",
117 { &vps4, 0, "PS4=+ ",
120 { &vterm, VUNSET, "TERM=",
123 { &voptind, 0, "OPTIND=1",
129 static struct var *vartab[VTABSIZE];
131 static const char *const locale_names[7] = {
132 "LC_COLLATE", "LC_CTYPE", "LC_MONETARY",
133 "LC_NUMERIC", "LC_TIME", "LC_MESSAGES", NULL
135 static const int locale_categories[7] = {
136 LC_COLLATE, LC_CTYPE, LC_MONETARY, LC_NUMERIC, LC_TIME, LC_MESSAGES, 0
139 static int varequal(const char *, const char *);
140 static struct var *find_var(const char *, struct var ***, int *);
141 static int localevar(const char *);
144 * Initialize the variable symbol tables and import the environment.
149 MKINIT char **environ;
154 for (envp = environ ; *envp ; envp++) {
155 if (strchr(*envp, '=')) {
156 setvareq(*envp, VEXPORT|VTEXTFIXED);
164 * This routine initializes the builtin variables. It is called when the
165 * shell is initialized.
172 const struct varinit *ip;
176 for (ip = varinit ; (vp = ip->var) != NULL ; ip++) {
177 if (find_var(ip->text, &vpp, &vp->name_len) != NULL)
181 vp->text = __DECONST(char *, ip->text);
182 vp->flags = ip->flags | VSTRFIXED | VTEXTFIXED;
188 if (find_var("PS1", &vpp, &vps1.name_len) == NULL) {
191 vps1.text = __DECONST(char *, geteuid() ? "PS1=$ " : "PS1=# ");
192 vps1.flags = VSTRFIXED|VTEXTFIXED;
194 if ((vppid.flags & VEXPORT) == 0) {
195 fmtstr(ppid, sizeof(ppid), "%d", (int)getppid());
196 setvarsafe("PPID", ppid, 0);
201 * Safe version of setvar, returns 1 on success 0 on failure.
205 setvarsafe(const char *name, const char *val, int flags)
207 struct jmploc jmploc;
208 struct jmploc *const savehandler = handler;
213 if (setjmp(jmploc.loc))
217 setvar(name, val, flags);
219 handler = savehandler;
225 * Set the value of a variable. The flags argument is stored with the
226 * flags of the variable. If val is NULL, the variable is unset.
230 setvar(const char *name, const char *val, int flags)
244 if (! is_in_name(*p)) {
245 if (*p == '\0' || *p == '=')
253 error("%.*s: bad variable name", namelen, name);
254 len = namelen + 2; /* 2 is space for '=' and '\0' */
260 nameeq = ckmalloc(len);
261 memcpy(nameeq, name, namelen);
262 nameeq[namelen] = '=';
264 scopy(val, nameeq + namelen + 1);
266 nameeq[namelen + 1] = '\0';
267 setvareq(nameeq, flags);
271 localevar(const char *s)
273 const char *const *ss;
277 if (varequal(s + 1, "ANG"))
279 if (strncmp(s + 1, "C_", 2) != 0)
281 if (varequal(s + 3, "ALL"))
283 for (ss = locale_names; *ss ; ss++)
284 if (varequal(s + 3, *ss + 3))
291 * Sets/unsets an environment variable from a pointer that may actually be a
292 * pointer into environ where the string should not be manipulated.
295 change_env(const char *s, int set)
301 if ((eqp = strchr(ss, '=')) != NULL)
303 if (set && eqp != NULL)
304 (void) setenv(ss, eqp + 1, 1);
314 * Same as setvar except that the variable and value are passed in
315 * the first argument as name=value. Since the first argument will
316 * be actually stored in the table, it should not be a string that
321 setvareq(char *s, int flags)
323 struct var *vp, **vpp;
328 vp = find_var(s, &vpp, &nlen);
330 if (vp->flags & VREADONLY)
331 error("%.*s: is read only", vp->name_len, s);
336 if (vp->func && (flags & VNOFUNC) == 0)
337 (*vp->func)(s + vp->name_len + 1);
339 if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0)
342 vp->flags &= ~(VTEXTFIXED|VSTACK|VUNSET);
347 * We could roll this to a function, to handle it as
348 * a regular variable function callback, but why bother?
350 * Note: this assumes iflag is not set to 1 initially.
351 * As part of init(), this is called before arguments
354 if ((vp == &vmpath || (vp == &vmail && ! mpathset())) &&
357 if ((vp->flags & VEXPORT) && localevar(s)) {
359 (void) setlocale(LC_ALL, "");
368 vp = ckmalloc(sizeof (*vp));
376 if ((vp->flags & VEXPORT) && localevar(s)) {
378 (void) setlocale(LC_ALL, "");
387 * Process a linked list of variable assignments.
391 listsetvar(struct strlist *list, int flags)
396 for (lp = list ; lp ; lp = lp->next) {
397 setvareq(savestr(lp->text), flags);
405 * Find the value of a variable. Returns NULL if not set.
409 lookupvar(const char *name)
413 v = find_var(name, NULL, NULL);
414 if (v == NULL || v->flags & VUNSET)
416 return v->text + v->name_len + 1;
422 * Search the environment of a builtin command. If the second argument
423 * is nonzero, return the value of a variable even if it hasn't been
428 bltinlookup(const char *name, int doall)
435 for (sp = cmdenviron ; sp ; sp = sp->next) {
436 if (varequal(sp->text, name))
437 result = strchr(sp->text, '=') + 1;
442 v = find_var(name, NULL, NULL);
443 if (v == NULL || v->flags & VUNSET ||
444 (!doall && (v->flags & VEXPORT) == 0))
446 return v->text + v->name_len + 1;
451 * Set up locale for a builtin (LANG/LC_* assignments).
461 for (lp = cmdenviron ; lp ; lp = lp->next) {
462 if (localevar(lp->text)) {
469 loc = bltinlookup("LC_ALL", 0);
472 setlocale(LC_ALL, loc);
477 locdef = bltinlookup("LANG", 0);
478 for (i = 0; locale_names[i] != NULL; i++) {
479 loc = bltinlookup(locale_names[i], 0);
483 setlocale(locale_categories[i], loc);
490 * Undo the effect of bltinlocaleset().
493 bltinunsetlocale(void)
498 for (lp = cmdenviron ; lp ; lp = lp->next) {
499 if (localevar(lp->text)) {
500 setlocale(LC_ALL, "");
509 * Update the localeisutf8 flag.
516 charset = nl_langinfo(CODESET);
517 localeisutf8 = !strcmp(charset, "UTF-8");
524 initial_localeisutf8 = localeisutf8;
528 * Generate a list of exported variables. This routine is used to construct
529 * the third argument to execve when executing a program.
541 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
542 for (vp = *vpp ; vp ; vp = vp->next)
543 if (vp->flags & VEXPORT)
546 ep = env = stalloc((nenv + 1) * sizeof *env);
547 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
548 for (vp = *vpp ; vp ; vp = vp->next)
549 if (vp->flags & VEXPORT)
558 var_compare(const void *a, const void *b)
560 const char *const *sa, *const *sb;
565 * This compares two var=value strings which creates a different
566 * order from what you would probably expect. POSIX is somewhat
567 * ambiguous on what should be sorted exactly.
569 return strcoll(*sa, *sb);
574 * Command to list all variables which are set. This is invoked from the
575 * set command when it is called without any options or operands.
579 showvarscmd(int argc __unused, char **argv __unused)
588 * POSIX requires us to sort the variables.
591 for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) {
592 for (vp = *vpp; vp; vp = vp->next) {
593 if (!(vp->flags & VUNSET))
599 vars = ckmalloc(n * sizeof(*vars));
601 for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) {
602 for (vp = *vpp; vp; vp = vp->next) {
603 if (!(vp->flags & VUNSET))
604 vars[i++] = vp->text;
608 qsort(vars, n, sizeof(*vars), var_compare);
609 for (i = 0; i < n; i++) {
610 s = strchr(vars[i], '=');
612 outbin(vars[i], s - vars[i], out1);
625 * The export and readonly commands.
629 exportcmd(int argc, char **argv)
637 int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT;
640 optreset = optind = 1;
643 while ((ch = getopt(argc, argv, "p")) != -1) {
650 error("unknown option: -%c", optopt);
656 if (values && argc != 0)
657 error("-p requires no arguments");
659 while ((name = *argv++) != NULL) {
660 if ((p = strchr(name, '=')) != NULL) {
663 vp = find_var(name, NULL, NULL);
666 if ((vp->flags & VEXPORT) && localevar(vp->text)) {
667 change_env(vp->text, 1);
668 (void) setlocale(LC_ALL, "");
674 setvar(name, p, flag);
677 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
678 for (vp = *vpp ; vp ; vp = vp->next) {
679 if (vp->flags & flag) {
684 if (values && !(vp->flags & VUNSET)) {
686 vp->name_len + 1, out1);
690 outbin(vp->text, vp->name_len,
702 * The "local" command.
706 localcmd(int argc __unused, char **argv __unused)
711 error("Not in a function");
712 while ((name = *argptr++) != NULL) {
720 * Make a variable a local variable. When a variable is made local, it's
721 * value and flags are saved in a localvar structure. The saved values
722 * will be restored when the shell function returns. We handle the name
723 * "-" as a special case.
729 struct localvar *lvp;
734 lvp = ckmalloc(sizeof (struct localvar));
735 if (name[0] == '-' && name[1] == '\0') {
736 lvp->text = ckmalloc(sizeof optlist);
737 memcpy(lvp->text, optlist, sizeof optlist);
740 vp = find_var(name, &vpp, NULL);
742 if (strchr(name, '='))
743 setvareq(savestr(name), VSTRFIXED);
745 setvar(name, NULL, VSTRFIXED);
746 vp = *vpp; /* the new variable */
750 lvp->text = vp->text;
751 lvp->flags = vp->flags;
752 vp->flags |= VSTRFIXED|VTEXTFIXED;
753 if (name[vp->name_len] == '=')
754 setvareq(savestr(name), 0);
758 lvp->next = localvars;
765 * Called after a function returns.
771 struct localvar *lvp;
774 while ((lvp = localvars) != NULL) {
775 localvars = lvp->next;
777 if (vp == NULL) { /* $- saved */
778 memcpy(optlist, lvp->text, sizeof optlist);
781 } else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) {
782 (void)unsetvar(vp->text);
784 if ((vp->flags & VTEXTFIXED) == 0)
786 vp->flags = lvp->flags;
787 vp->text = lvp->text;
795 setvarcmd(int argc, char **argv)
798 return unsetcmd(argc, argv);
800 setvar(argv[1], argv[2], 0);
802 error("too many arguments");
808 * The unset builtin command.
812 unsetcmd(int argc __unused, char **argv __unused)
820 while ((i = nextopt("vf")) != '\0') {
826 if (flg_func == 0 && flg_var == 0)
829 for (ap = argptr; *ap ; ap++) {
831 ret |= unsetfunc(*ap);
833 ret |= unsetvar(*ap);
840 * Unset the specified variable.
844 unsetvar(const char *s)
849 vp = find_var(s, &vpp, NULL);
852 if (vp->flags & VREADONLY)
855 if (vp->text[vp->name_len + 1] != '\0')
856 setvar(s, nullstr, 0);
857 if ((vp->flags & VEXPORT) && localevar(vp->text)) {
859 setlocale(LC_ALL, "");
862 vp->flags &= ~VEXPORT;
864 if ((vp->flags & VSTRFIXED) == 0) {
865 if ((vp->flags & VTEXTFIXED) == 0)
877 * Returns true if the two strings specify the same varable. The first
878 * variable name is terminated by '='; the second may be terminated by
879 * either '=' or '\0'.
883 varequal(const char *p, const char *q)
889 if (*p == '=' && *(q - 1) == '\0')
895 * Search for a variable.
896 * 'name' may be terminated by '=' or a NUL.
897 * vppp is set to the pointer to vp, or the list head if vp isn't found
898 * lenp is set to the number of charactets in 'name'
902 find_var(const char *name, struct var ***vppp, int *lenp)
904 unsigned int hashval;
906 struct var *vp, **vpp;
907 const char *p = name;
910 while (*p && *p != '=')
911 hashval = 2 * hashval + (unsigned char)*p++;
916 vpp = &vartab[hashval % VTABSIZE];
920 for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) {
921 if (vp->name_len != len)
923 if (memcmp(vp->text, name, len) != 0)