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);
338 if (vp->func && (flags & VNOFUNC) == 0)
339 (*vp->func)(strchr(s, '=') + 1);
341 if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0)
344 vp->flags &= ~(VTEXTFIXED|VSTACK|VUNSET);
349 * We could roll this to a function, to handle it as
350 * a regular variable function callback, but why bother?
352 * Note: this assumes iflag is not set to 1 initially.
353 * As part of init(), this is called before arguments
356 if ((vp == &vmpath || (vp == &vmail && ! mpathset())) &&
359 if ((vp->flags & VEXPORT) && localevar(s)) {
361 (void) setlocale(LC_ALL, "");
368 vp = ckmalloc(sizeof (*vp));
375 if ((vp->flags & VEXPORT) && localevar(s)) {
377 (void) setlocale(LC_ALL, "");
385 * Process a linked list of variable assignments.
389 listsetvar(struct strlist *list)
394 for (lp = list ; lp ; lp = lp->next) {
395 setvareq(savestr(lp->text), 0);
403 * Find the value of a variable. Returns NULL if not set.
407 lookupvar(const char *name)
411 for (v = *hashvar(name) ; v ; v = v->next) {
412 if (varequal(v->text, name)) {
413 if (v->flags & VUNSET)
415 return strchr(v->text, '=') + 1;
424 * Search the environment of a builtin command. If the second argument
425 * is nonzero, return the value of a variable even if it hasn't been
430 bltinlookup(const char *name, int doall)
437 for (sp = cmdenviron ; sp ; sp = sp->next) {
438 if (varequal(sp->text, name))
439 result = strchr(sp->text, '=') + 1;
443 for (v = *hashvar(name) ; v ; v = v->next) {
444 if (varequal(v->text, name)) {
445 if ((v->flags & VUNSET)
446 || (!doall && (v->flags & VEXPORT) == 0))
448 return strchr(v->text, '=') + 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);
481 locdef = bltinlookup("LANG", 0);
482 for (i = 0; locale_names[i] != NULL; i++) {
483 loc = bltinlookup(locale_names[i], 0);
487 setlocale(locale_categories[i], loc);
493 * Undo the effect of bltinlocaleset().
496 bltinunsetlocale(void)
501 for (lp = cmdenviron ; lp ; lp = lp->next) {
502 if (localevar(lp->text)) {
503 setlocale(LC_ALL, "");
512 * Generate a list of exported variables. This routine is used to construct
513 * the third argument to execve when executing a program.
525 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
526 for (vp = *vpp ; vp ; vp = vp->next)
527 if (vp->flags & VEXPORT)
530 ep = env = stalloc((nenv + 1) * sizeof *env);
531 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
532 for (vp = *vpp ; vp ; vp = vp->next)
533 if (vp->flags & VEXPORT)
542 * Called when a shell procedure is invoked to clear out nonexported
543 * variables. It is also necessary to reallocate variables of with
544 * VSTACK set since these are currently allocated on the stack.
547 MKINIT void shprocvar(void);
559 struct var *vp, **prev;
561 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
562 for (prev = vpp ; (vp = *prev) != NULL ; ) {
563 if ((vp->flags & VEXPORT) == 0) {
565 if ((vp->flags & VTEXTFIXED) == 0)
567 if ((vp->flags & VSTRFIXED) == 0)
570 if (vp->flags & VSTACK) {
571 vp->text = savestr(vp->text);
572 vp->flags &=~ VSTACK;
583 var_compare(const void *a, const void *b)
585 const char *const *sa, *const *sb;
590 * This compares two var=value strings which creates a different
591 * order from what you would probably expect. POSIX is somewhat
592 * ambiguous on what should be sorted exactly.
594 return strcoll(*sa, *sb);
599 * Command to list all variables which are set. Currently this command
600 * is invoked from the set command when the set command is called without
605 showvarscmd(int argc __unused, char **argv __unused)
614 * POSIX requires us to sort the variables.
617 for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) {
618 for (vp = *vpp; vp; vp = vp->next) {
619 if (!(vp->flags & VUNSET))
625 vars = ckmalloc(n * sizeof(*vars));
627 for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) {
628 for (vp = *vpp; vp; vp = vp->next) {
629 if (!(vp->flags & VUNSET))
630 vars[i++] = vp->text;
634 qsort(vars, n, sizeof(*vars), var_compare);
635 for (i = 0; i < n; i++) {
636 for (s = vars[i]; *s != '='; s++)
651 * The export and readonly commands.
655 exportcmd(int argc, char **argv)
663 int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT;
666 optreset = optind = 1;
669 while ((ch = getopt(argc, argv, "p")) != -1) {
676 error("unknown option: -%c", optopt);
682 if (values && argc != 0)
683 error("-p requires no arguments");
685 while ((name = *argv++) != NULL) {
686 if ((p = strchr(name, '=')) != NULL) {
690 for (vp = *vpp ; vp ; vp = vp->next) {
691 if (varequal(vp->text, name)) {
694 if ((vp->flags & VEXPORT) && localevar(vp->text)) {
695 change_env(vp->text, 1);
696 (void) setlocale(LC_ALL, "");
702 setvar(name, p, flag);
706 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
707 for (vp = *vpp ; vp ; vp = vp->next) {
708 if (vp->flags & flag) {
713 for (p = vp->text ; *p != '=' ; p++)
715 if (values && !(vp->flags & VUNSET)) {
729 * The "local" command.
733 localcmd(int argc __unused, char **argv __unused)
738 error("Not in a function");
739 while ((name = *argptr++) != NULL) {
747 * Make a variable a local variable. When a variable is made local, it's
748 * value and flags are saved in a localvar structure. The saved values
749 * will be restored when the shell function returns. We handle the name
750 * "-" as a special case.
756 struct localvar *lvp;
761 lvp = ckmalloc(sizeof (struct localvar));
762 if (name[0] == '-' && name[1] == '\0') {
763 lvp->text = ckmalloc(sizeof optlist);
764 memcpy(lvp->text, optlist, sizeof optlist);
768 for (vp = *vpp ; vp && ! varequal(vp->text, name) ; vp = vp->next);
770 if (strchr(name, '='))
771 setvareq(savestr(name), VSTRFIXED);
773 setvar(name, NULL, VSTRFIXED);
774 vp = *vpp; /* the new variable */
778 lvp->text = vp->text;
779 lvp->flags = vp->flags;
780 vp->flags |= VSTRFIXED|VTEXTFIXED;
781 if (strchr(name, '='))
782 setvareq(savestr(name), 0);
786 lvp->next = localvars;
793 * Called after a function returns.
799 struct localvar *lvp;
802 while ((lvp = localvars) != NULL) {
803 localvars = lvp->next;
805 if (vp == NULL) { /* $- saved */
806 memcpy(optlist, lvp->text, sizeof optlist);
808 } else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) {
809 (void)unsetvar(vp->text);
811 if ((vp->flags & VTEXTFIXED) == 0)
813 vp->flags = lvp->flags;
814 vp->text = lvp->text;
822 setvarcmd(int argc, char **argv)
825 return unsetcmd(argc, argv);
827 setvar(argv[1], argv[2], 0);
829 error("List assignment not implemented");
835 * The unset builtin command. We unset the function before we unset the
836 * variable to allow a function to be unset when there is a readonly variable
837 * with the same name.
841 unsetcmd(int argc __unused, char **argv __unused)
849 while ((i = nextopt("vf")) != '\0') {
855 if (flg_func == 0 && flg_var == 0)
858 for (ap = argptr; *ap ; ap++) {
860 ret |= unsetfunc(*ap);
862 ret |= unsetvar(*ap);
869 * Unset the specified variable.
873 unsetvar(const char *s)
879 for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) {
880 if (varequal(vp->text, s)) {
881 if (vp->flags & VREADONLY)
884 if (*(strchr(vp->text, '=') + 1) != '\0')
885 setvar(s, nullstr, 0);
886 if ((vp->flags & VEXPORT) && localevar(vp->text)) {
888 setlocale(LC_ALL, "");
890 vp->flags &= ~VEXPORT;
892 if ((vp->flags & VSTRFIXED) == 0) {
893 if ((vp->flags & VTEXTFIXED) == 0)
909 * Find the appropriate entry in the hash table from the name.
913 hashvar(const char *p)
915 unsigned int hashval;
917 hashval = ((unsigned char) *p) << 4;
918 while (*p && *p != '=')
919 hashval += (unsigned char) *p++;
920 return &vartab[hashval % VTABSIZE];
926 * Returns true if the two strings specify the same varable. The first
927 * variable name is terminated by '='; the second may be terminated by
928 * either '=' or '\0'.
932 varequal(const char *p, const char *q)
938 if (*p == '=' && *(q - 1) == '\0')