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 and again when a shell procedure is spawned.
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 * Called when a shell procedure is invoked to clear out nonexported
547 * variables. It is also necessary to reallocate variables of with
548 * VSTACK set since these are currently allocated on the stack.
551 MKINIT void shprocvar(void);
563 struct var *vp, **prev;
565 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
566 for (prev = vpp ; (vp = *prev) != NULL ; ) {
567 if ((vp->flags & VEXPORT) == 0) {
569 if ((vp->flags & VTEXTFIXED) == 0)
571 if ((vp->flags & VSTRFIXED) == 0)
574 if (vp->flags & VSTACK) {
575 vp->text = savestr(vp->text);
576 vp->flags &=~ VSTACK;
587 var_compare(const void *a, const void *b)
589 const char *const *sa, *const *sb;
594 * This compares two var=value strings which creates a different
595 * order from what you would probably expect. POSIX is somewhat
596 * ambiguous on what should be sorted exactly.
598 return strcoll(*sa, *sb);
603 * Command to list all variables which are set. Currently this command
604 * is invoked from the set command when the set command is called without
609 showvarscmd(int argc __unused, char **argv __unused)
618 * POSIX requires us to sort the variables.
621 for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) {
622 for (vp = *vpp; vp; vp = vp->next) {
623 if (!(vp->flags & VUNSET))
629 vars = ckmalloc(n * sizeof(*vars));
631 for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) {
632 for (vp = *vpp; vp; vp = vp->next) {
633 if (!(vp->flags & VUNSET))
634 vars[i++] = vp->text;
638 qsort(vars, n, sizeof(*vars), var_compare);
639 for (i = 0; i < n; i++) {
640 s = strchr(vars[i], '=');
642 outbin(vars[i], s - vars[i], out1);
655 * The export and readonly commands.
659 exportcmd(int argc, char **argv)
667 int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT;
670 optreset = optind = 1;
673 while ((ch = getopt(argc, argv, "p")) != -1) {
680 error("unknown option: -%c", optopt);
686 if (values && argc != 0)
687 error("-p requires no arguments");
689 while ((name = *argv++) != NULL) {
690 if ((p = strchr(name, '=')) != NULL) {
694 for (vp = *vpp ; vp ; vp = vp->next) {
695 if (varequal(vp->text, name)) {
698 if ((vp->flags & VEXPORT) && localevar(vp->text)) {
699 change_env(vp->text, 1);
700 (void) setlocale(LC_ALL, "");
706 setvar(name, p, flag);
710 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
711 for (vp = *vpp ; vp ; vp = vp->next) {
712 if (vp->flags & flag) {
717 p = strchr(vp->text, '=');
718 if (values && !(vp->flags & VUNSET)) {
720 outbin(vp->text, p - vp->text,
724 outbin(vp->text, p - vp->text,
736 * The "local" command.
740 localcmd(int argc __unused, char **argv __unused)
745 error("Not in a function");
746 while ((name = *argptr++) != NULL) {
754 * Make a variable a local variable. When a variable is made local, it's
755 * value and flags are saved in a localvar structure. The saved values
756 * will be restored when the shell function returns. We handle the name
757 * "-" as a special case.
763 struct localvar *lvp;
768 lvp = ckmalloc(sizeof (struct localvar));
769 if (name[0] == '-' && name[1] == '\0') {
770 lvp->text = ckmalloc(sizeof optlist);
771 memcpy(lvp->text, optlist, sizeof optlist);
775 for (vp = *vpp ; vp && ! varequal(vp->text, name) ; vp = vp->next);
777 if (strchr(name, '='))
778 setvareq(savestr(name), VSTRFIXED);
780 setvar(name, NULL, VSTRFIXED);
781 vp = *vpp; /* the new variable */
785 lvp->text = vp->text;
786 lvp->flags = vp->flags;
787 vp->flags |= VSTRFIXED|VTEXTFIXED;
788 if (strchr(name, '='))
789 setvareq(savestr(name), 0);
793 lvp->next = localvars;
800 * Called after a function returns.
806 struct localvar *lvp;
809 while ((lvp = localvars) != NULL) {
810 localvars = lvp->next;
812 if (vp == NULL) { /* $- saved */
813 memcpy(optlist, lvp->text, sizeof optlist);
816 } else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) {
817 (void)unsetvar(vp->text);
819 if ((vp->flags & VTEXTFIXED) == 0)
821 vp->flags = lvp->flags;
822 vp->text = lvp->text;
830 setvarcmd(int argc, char **argv)
833 return unsetcmd(argc, argv);
835 setvar(argv[1], argv[2], 0);
837 error("too many arguments");
843 * The unset builtin command. We unset the function before we unset the
844 * variable to allow a function to be unset when there is a readonly variable
845 * with the same name.
849 unsetcmd(int argc __unused, char **argv __unused)
857 while ((i = nextopt("vf")) != '\0') {
863 if (flg_func == 0 && flg_var == 0)
866 for (ap = argptr; *ap ; ap++) {
868 ret |= unsetfunc(*ap);
870 ret |= unsetvar(*ap);
877 * Unset the specified variable.
881 unsetvar(const char *s)
887 for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) {
888 if (varequal(vp->text, s)) {
889 if (vp->flags & VREADONLY)
892 if (*(strchr(vp->text, '=') + 1) != '\0')
893 setvar(s, nullstr, 0);
894 if ((vp->flags & VEXPORT) && localevar(vp->text)) {
896 setlocale(LC_ALL, "");
898 vp->flags &= ~VEXPORT;
900 if ((vp->flags & VSTRFIXED) == 0) {
901 if ((vp->flags & VTEXTFIXED) == 0)
917 * Find the appropriate entry in the hash table from the name.
921 hashvar(const char *p)
923 unsigned int hashval;
925 hashval = ((unsigned char) *p) << 4;
926 while (*p && *p != '=')
927 hashval += (unsigned char) *p++;
928 return &vartab[hashval % VTABSIZE];
934 * Returns true if the two strings specify the same varable. The first
935 * variable name is terminated by '='; the second may be terminated by
936 * either '=' or '\0'.
940 varequal(const char *p, const char *q)
946 if (*p == '=' && *(q - 1) == '\0')