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$");
54 #include "nodes.h" /* for other headers */
55 #include "eval.h" /* defines cmdenviron */
66 #include "myhistedit.h"
77 void (*func)(const char *);
94 static struct var voptind;
96 static const struct varinit varinit[] = {
98 { &vhistsize, VUNSET, "HISTSIZE=",
101 { &vifs, 0, "IFS= \t\n",
103 { &vmail, VUNSET, "MAIL=",
105 { &vmpath, VUNSET, "MAILPATH=",
107 { &vpath, 0, "PATH=" _PATH_DEFPATH,
109 { &vppid, VUNSET, "PPID=",
112 * vps1 depends on uid
114 { &vps2, 0, "PS2=> ",
116 { &vps4, 0, "PS4=+ ",
119 { &vterm, VUNSET, "TERM=",
122 { &voptind, 0, "OPTIND=1",
128 static struct var *vartab[VTABSIZE];
130 static const char *const locale_names[7] = {
131 "LC_COLLATE", "LC_CTYPE", "LC_MONETARY",
132 "LC_NUMERIC", "LC_TIME", "LC_MESSAGES", NULL
134 static const int locale_categories[7] = {
135 LC_COLLATE, LC_CTYPE, LC_MONETARY, LC_NUMERIC, LC_TIME, LC_MESSAGES, 0
138 static struct var **hashvar(const char *);
139 static int varequal(const char *, const char *);
140 static int localevar(const char *);
143 * Initialize the variable symbol tables and import the environment.
148 MKINIT char **environ;
153 for (envp = environ ; *envp ; envp++) {
154 if (strchr(*envp, '=')) {
155 setvareq(*envp, VEXPORT|VTEXTFIXED);
163 * This routine initializes the builtin variables. It is called when the
164 * shell is initialized.
171 const struct varinit *ip;
175 for (ip = varinit ; (vp = ip->var) != NULL ; ip++) {
176 if ((vp->flags & VEXPORT) == 0) {
177 vpp = hashvar(ip->text);
180 vp->text = __DECONST(char *, ip->text);
181 vp->flags = ip->flags | VSTRFIXED | VTEXTFIXED;
188 if ((vps1.flags & VEXPORT) == 0) {
189 vpp = hashvar("PS1=");
192 vps1.text = __DECONST(char *, geteuid() ? "PS1=$ " : "PS1=# ");
193 vps1.flags = VSTRFIXED|VTEXTFIXED;
195 if ((vppid.flags & VEXPORT) == 0) {
196 fmtstr(ppid, sizeof(ppid), "%d", (int)getppid());
197 setvarsafe("PPID", ppid, 0);
202 * Safe version of setvar, returns 1 on success 0 on failure.
206 setvarsafe(const char *name, const char *val, int flags)
208 struct jmploc jmploc;
209 struct jmploc *const savehandler = handler;
214 if (setjmp(jmploc.loc))
218 setvar(name, val, flags);
220 handler = savehandler;
226 * Set the value of a variable. The flags argument is stored with the
227 * flags of the variable. If val is NULL, the variable is unset.
231 setvar(const char *name, const char *val, int flags)
245 if (! is_in_name(*p)) {
246 if (*p == '\0' || *p == '=')
254 error("%.*s: bad variable name", namelen, name);
255 len = namelen + 2; /* 2 is space for '=' and '\0' */
261 nameeq = ckmalloc(len);
262 memcpy(nameeq, name, namelen);
263 nameeq[namelen] = '=';
265 scopy(val, nameeq + namelen + 1);
267 nameeq[namelen + 1] = '\0';
268 setvareq(nameeq, flags);
272 localevar(const char *s)
274 const char *const *ss;
278 if (varequal(s + 1, "ANG"))
280 if (strncmp(s + 1, "C_", 2) != 0)
282 if (varequal(s + 3, "ALL"))
284 for (ss = locale_names; *ss ; ss++)
285 if (varequal(s + 3, *ss + 3))
292 * Sets/unsets an environment variable from a pointer that may actually be a
293 * pointer into environ where the string should not be manipulated.
296 change_env(const char *s, int set)
302 if ((eqp = strchr(ss, '=')) != NULL)
304 if (set && eqp != NULL)
305 (void) setenv(ss, eqp + 1, 1);
315 * Same as setvar except that the variable and value are passed in
316 * the first argument as name=value. Since the first argument will
317 * be actually stored in the table, it should not be a string that
322 setvareq(char *s, int flags)
324 struct var *vp, **vpp;
330 for (vp = *vpp ; vp ; vp = vp->next) {
331 if (varequal(s, vp->text)) {
332 if (vp->flags & VREADONLY) {
333 len = strchr(s, '=') - s;
334 error("%.*s: is read only", len, s);
340 if (vp->func && (flags & VNOFUNC) == 0)
341 (*vp->func)(strchr(s, '=') + 1);
343 if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0)
346 vp->flags &= ~(VTEXTFIXED|VSTACK|VUNSET);
351 * We could roll this to a function, to handle it as
352 * a regular variable function callback, but why bother?
354 * Note: this assumes iflag is not set to 1 initially.
355 * As part of init(), this is called before arguments
358 if ((vp == &vmpath || (vp == &vmail && ! mpathset())) &&
361 if ((vp->flags & VEXPORT) && localevar(s)) {
363 (void) setlocale(LC_ALL, "");
372 vp = ckmalloc(sizeof (*vp));
379 if ((vp->flags & VEXPORT) && localevar(s)) {
381 (void) setlocale(LC_ALL, "");
389 * Process a linked list of variable assignments.
393 listsetvar(struct strlist *list, int flags)
398 for (lp = list ; lp ; lp = lp->next) {
399 setvareq(savestr(lp->text), flags);
407 * Find the value of a variable. Returns NULL if not set.
411 lookupvar(const char *name)
415 for (v = *hashvar(name) ; v ; v = v->next) {
416 if (varequal(v->text, name)) {
417 if (v->flags & VUNSET)
419 return strchr(v->text, '=') + 1;
428 * Search the environment of a builtin command. If the second argument
429 * is nonzero, return the value of a variable even if it hasn't been
434 bltinlookup(const char *name, int doall)
441 for (sp = cmdenviron ; sp ; sp = sp->next) {
442 if (varequal(sp->text, name))
443 result = strchr(sp->text, '=') + 1;
447 for (v = *hashvar(name) ; v ; v = v->next) {
448 if (varequal(v->text, name)) {
449 if ((v->flags & VUNSET)
450 || (!doall && (v->flags & VEXPORT) == 0))
452 return strchr(v->text, '=') + 1;
460 * Set up locale for a builtin (LANG/LC_* assignments).
470 for (lp = cmdenviron ; lp ; lp = lp->next) {
471 if (localevar(lp->text)) {
478 loc = bltinlookup("LC_ALL", 0);
481 setlocale(LC_ALL, loc);
485 locdef = bltinlookup("LANG", 0);
486 for (i = 0; locale_names[i] != NULL; i++) {
487 loc = bltinlookup(locale_names[i], 0);
491 setlocale(locale_categories[i], loc);
497 * Undo the effect of bltinlocaleset().
500 bltinunsetlocale(void)
505 for (lp = cmdenviron ; lp ; lp = lp->next) {
506 if (localevar(lp->text)) {
507 setlocale(LC_ALL, "");
516 * Generate a list of exported variables. This routine is used to construct
517 * the third argument to execve when executing a program.
529 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
530 for (vp = *vpp ; vp ; vp = vp->next)
531 if (vp->flags & VEXPORT)
534 ep = env = stalloc((nenv + 1) * sizeof *env);
535 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
536 for (vp = *vpp ; vp ; vp = vp->next)
537 if (vp->flags & VEXPORT)
546 var_compare(const void *a, const void *b)
548 const char *const *sa, *const *sb;
553 * This compares two var=value strings which creates a different
554 * order from what you would probably expect. POSIX is somewhat
555 * ambiguous on what should be sorted exactly.
557 return strcoll(*sa, *sb);
562 * Command to list all variables which are set. This is invoked from the
563 * set command when it is called without any options or operands.
567 showvarscmd(int argc __unused, char **argv __unused)
576 * POSIX requires us to sort the variables.
579 for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) {
580 for (vp = *vpp; vp; vp = vp->next) {
581 if (!(vp->flags & VUNSET))
587 vars = ckmalloc(n * sizeof(*vars));
589 for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) {
590 for (vp = *vpp; vp; vp = vp->next) {
591 if (!(vp->flags & VUNSET))
592 vars[i++] = vp->text;
596 qsort(vars, n, sizeof(*vars), var_compare);
597 for (i = 0; i < n; i++) {
598 s = strchr(vars[i], '=');
600 outbin(vars[i], s - vars[i], out1);
613 * The export and readonly commands.
617 exportcmd(int argc, char **argv)
625 int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT;
628 optreset = optind = 1;
631 while ((ch = getopt(argc, argv, "p")) != -1) {
638 error("unknown option: -%c", optopt);
644 if (values && argc != 0)
645 error("-p requires no arguments");
647 while ((name = *argv++) != NULL) {
648 if ((p = strchr(name, '=')) != NULL) {
652 for (vp = *vpp ; vp ; vp = vp->next) {
653 if (varequal(vp->text, name)) {
656 if ((vp->flags & VEXPORT) && localevar(vp->text)) {
657 change_env(vp->text, 1);
658 (void) setlocale(LC_ALL, "");
664 setvar(name, p, flag);
668 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
669 for (vp = *vpp ; vp ; vp = vp->next) {
670 if (vp->flags & flag) {
675 p = strchr(vp->text, '=');
676 if (values && !(vp->flags & VUNSET)) {
678 outbin(vp->text, p - vp->text,
682 outbin(vp->text, p - vp->text,
694 * The "local" command.
698 localcmd(int argc __unused, char **argv __unused)
703 error("Not in a function");
704 while ((name = *argptr++) != NULL) {
712 * Make a variable a local variable. When a variable is made local, it's
713 * value and flags are saved in a localvar structure. The saved values
714 * will be restored when the shell function returns. We handle the name
715 * "-" as a special case.
721 struct localvar *lvp;
726 lvp = ckmalloc(sizeof (struct localvar));
727 if (name[0] == '-' && name[1] == '\0') {
728 lvp->text = ckmalloc(sizeof optlist);
729 memcpy(lvp->text, optlist, sizeof optlist);
733 for (vp = *vpp ; vp && ! varequal(vp->text, name) ; vp = vp->next);
735 if (strchr(name, '='))
736 setvareq(savestr(name), VSTRFIXED);
738 setvar(name, NULL, VSTRFIXED);
739 vp = *vpp; /* the new variable */
743 lvp->text = vp->text;
744 lvp->flags = vp->flags;
745 vp->flags |= VSTRFIXED|VTEXTFIXED;
746 if (strchr(name, '='))
747 setvareq(savestr(name), 0);
751 lvp->next = localvars;
758 * Called after a function returns.
764 struct localvar *lvp;
767 while ((lvp = localvars) != NULL) {
768 localvars = lvp->next;
770 if (vp == NULL) { /* $- saved */
771 memcpy(optlist, lvp->text, sizeof optlist);
774 } else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) {
775 (void)unsetvar(vp->text);
777 if ((vp->flags & VTEXTFIXED) == 0)
779 vp->flags = lvp->flags;
780 vp->text = lvp->text;
788 setvarcmd(int argc, char **argv)
791 return unsetcmd(argc, argv);
793 setvar(argv[1], argv[2], 0);
795 error("too many arguments");
801 * The unset builtin command.
805 unsetcmd(int argc __unused, char **argv __unused)
813 while ((i = nextopt("vf")) != '\0') {
819 if (flg_func == 0 && flg_var == 0)
822 for (ap = argptr; *ap ; ap++) {
824 ret |= unsetfunc(*ap);
826 ret |= unsetvar(*ap);
833 * Unset the specified variable.
837 unsetvar(const char *s)
843 for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) {
844 if (varequal(vp->text, s)) {
845 if (vp->flags & VREADONLY)
848 if (*(strchr(vp->text, '=') + 1) != '\0')
849 setvar(s, nullstr, 0);
850 if ((vp->flags & VEXPORT) && localevar(vp->text)) {
852 setlocale(LC_ALL, "");
854 vp->flags &= ~VEXPORT;
856 if ((vp->flags & VSTRFIXED) == 0) {
857 if ((vp->flags & VTEXTFIXED) == 0)
873 * Find the appropriate entry in the hash table from the name.
877 hashvar(const char *p)
879 unsigned int hashval;
881 hashval = ((unsigned char) *p) << 4;
882 while (*p && *p != '=')
883 hashval += (unsigned char) *p++;
884 return &vartab[hashval % VTABSIZE];
890 * Returns true if the two strings specify the same varable. The first
891 * variable name is terminated by '='; the second may be terminated by
892 * either '=' or '\0'.
896 varequal(const char *p, const char *q)
902 if (*p == '=' && *(q - 1) == '\0')