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 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)var.c 8.3 (Berkeley) 5/4/95
37 * $FreeBSD: src/bin/sh/var.c,v 1.56 2011/02/04 22:47:55 jilles Exp $
53 #include "nodes.h" /* for other headers */
54 #include "eval.h" /* defines cmdenviron */
65 #include "myhistedit.h"
76 void (*func)(const char *);
93 static struct var voptind;
95 static const struct varinit varinit[] = {
97 { &vhistsize, VUNSET, "HISTSIZE=",
100 { &vifs, 0, "IFS= \t\n",
102 { &vmail, VUNSET, "MAIL=",
104 { &vmpath, VUNSET, "MAILPATH=",
106 { &vpath, 0, "PATH=" _PATH_DEFPATH,
108 { &vppid, VUNSET, "PPID=",
111 * vps1 depends on uid
113 { &vps2, 0, "PS2=> ",
115 { &vps4, 0, "PS4=+ ",
118 { &vterm, VUNSET, "TERM=",
121 { &voptind, 0, "OPTIND=1",
127 static struct var *vartab[VTABSIZE];
129 static const char *const locale_names[7] = {
130 "LC_COLLATE", "LC_CTYPE", "LC_MONETARY",
131 "LC_NUMERIC", "LC_TIME", "LC_MESSAGES", NULL
133 static const int locale_categories[7] = {
134 LC_COLLATE, LC_CTYPE, LC_MONETARY, LC_NUMERIC, LC_TIME, LC_MESSAGES, 0
137 static struct var **hashvar(const char *);
138 static int varequal(const char *, const char *);
139 static int localevar(const char *);
142 * Initialize the variable symbol tables and import the environment.
147 MKINIT char **environ;
152 for (envp = environ ; *envp ; envp++) {
153 if (strchr(*envp, '=')) {
154 setvareq(*envp, VEXPORT|VTEXTFIXED);
162 * This routine initializes the builtin variables. It is called when the
163 * shell is initialized.
170 const struct varinit *ip;
174 for (ip = varinit ; (vp = ip->var) != NULL ; ip++) {
175 if ((vp->flags & VEXPORT) == 0) {
176 vpp = hashvar(ip->text);
179 vp->text = __DECONST(char *, ip->text);
180 vp->flags = ip->flags | VSTRFIXED | VTEXTFIXED;
187 if ((vps1.flags & VEXPORT) == 0) {
188 vpp = hashvar("PS1=");
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))
290 * Sets/unsets an environment variable from a pointer that may actually be a
291 * pointer into environ where the string should not be manipulated.
294 change_env(const char *s, int set)
300 if ((eqp = strchr(ss, '=')) != NULL)
302 if (set && eqp != NULL) {
303 if (setenv(ss, eqp + 1, 1) != 0)
304 error("setenv: cannot set %s=%s", ss, eqp + 1);
312 * Same as setvar except that the variable and value are passed in
313 * the first argument as name=value. Since the first argument will
314 * be actually stored in the table, it should not be a string that
319 setvareq(char *s, int flags)
321 struct var *vp, **vpp;
327 for (vp = *vpp ; vp ; vp = vp->next) {
328 if (varequal(s, vp->text)) {
329 if (vp->flags & VREADONLY) {
330 len = strchr(s, '=') - s;
331 error("%.*s: is read only", len, s);
337 if (vp->func && (flags & VNOFUNC) == 0)
338 (*vp->func)(strchr(s, '=') + 1);
340 if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0)
343 vp->flags &= ~(VTEXTFIXED|VSTACK|VUNSET);
348 * We could roll this to a function, to handle it as
349 * a regular variable function callback, but why bother?
351 * Note: this assumes iflag is not set to 1 initially.
352 * As part of init(), this is called before arguments
355 if ((vp == &vmpath || (vp == &vmail && ! mpathset())) &&
358 if ((vp->flags & VEXPORT) && localevar(s)) {
360 setlocale(LC_ALL, "");
369 vp = ckmalloc(sizeof (*vp));
376 if ((vp->flags & VEXPORT) && localevar(s)) {
378 setlocale(LC_ALL, "");
386 * Process a linked list of variable assignments.
390 listsetvar(struct strlist *list, int flags)
395 for (lp = list ; lp ; lp = lp->next) {
396 setvareq(savestr(lp->text), flags);
404 * Find the value of a variable. Returns NULL if not set.
408 lookupvar(const char *name)
412 for (v = *hashvar(name) ; v ; v = v->next) {
413 if (varequal(v->text, name)) {
414 if (v->flags & VUNSET)
416 return strchr(v->text, '=') + 1;
425 * Search the environment of a builtin command. If the second argument
426 * is nonzero, return the value of a variable even if it hasn't been
431 bltinlookup(const char *name, int doall)
438 for (sp = cmdenviron ; sp ; sp = sp->next) {
439 if (varequal(sp->text, name))
440 result = strchr(sp->text, '=') + 1;
444 for (v = *hashvar(name) ; v ; v = v->next) {
445 if (varequal(v->text, name)) {
446 if ((v->flags & VUNSET)
447 || (!doall && (v->flags & VEXPORT) == 0))
449 return strchr(v->text, '=') + 1;
457 * Set up locale for a builtin (LANG/LC_* assignments).
467 for (lp = cmdenviron ; lp ; lp = lp->next) {
468 if (localevar(lp->text)) {
475 loc = bltinlookup("LC_ALL", 0);
478 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);
494 * Undo the effect of bltinlocaleset().
497 bltinunsetlocale(void)
502 for (lp = cmdenviron ; lp ; lp = lp->next) {
503 if (localevar(lp->text)) {
504 setlocale(LC_ALL, "");
513 * Generate a list of exported variables. This routine is used to construct
514 * the third argument to execve when executing a program.
526 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
527 for (vp = *vpp ; vp ; vp = vp->next)
528 if (vp->flags & VEXPORT)
531 ep = env = stalloc((nenv + 1) * sizeof *env);
532 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
533 for (vp = *vpp ; vp ; vp = vp->next)
534 if (vp->flags & VEXPORT)
543 var_compare(const void *a, const void *b)
545 const char *const *sa, *const *sb;
550 * This compares two var=value strings which creates a different
551 * order from what you would probably expect. POSIX is somewhat
552 * ambiguous on what should be sorted exactly.
554 return strcoll(*sa, *sb);
559 * Command to list all variables which are set. This is invoked from the
560 * set command when it is called without any options or operands.
564 showvarscmd(int argc __unused, char **argv __unused)
573 * POSIX requires us to sort the variables.
576 for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) {
577 for (vp = *vpp; vp; vp = vp->next) {
578 if (!(vp->flags & VUNSET))
584 vars = ckmalloc(n * sizeof(*vars));
586 for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) {
587 for (vp = *vpp; vp; vp = vp->next) {
588 if (!(vp->flags & VUNSET))
589 vars[i++] = vp->text;
593 qsort(vars, n, sizeof(*vars), var_compare);
594 for (i = 0; i < n; i++) {
595 s = strchr(vars[i], '=');
597 outbin(vars[i], s - vars[i], out1);
610 * The export and readonly commands.
614 exportcmd(int argc, char **argv)
622 int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT;
625 optreset = optind = 1;
628 while ((ch = getopt(argc, argv, "p")) != -1) {
635 error("unknown option: -%c", optopt);
641 if (values && argc != 0)
642 error("-p requires no arguments");
644 while ((name = *argv++) != NULL) {
645 if ((p = strchr(name, '=')) != NULL) {
649 for (vp = *vpp ; vp ; vp = vp->next) {
650 if (varequal(vp->text, name)) {
653 if ((vp->flags & VEXPORT) && localevar(vp->text)) {
654 change_env(vp->text, 1);
655 setlocale(LC_ALL, "");
661 setvar(name, p, flag);
665 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
666 for (vp = *vpp ; vp ; vp = vp->next) {
667 if (vp->flags & flag) {
672 p = strchr(vp->text, '=');
673 if (values && !(vp->flags & VUNSET)) {
675 outbin(vp->text, p - vp->text,
679 outbin(vp->text, p - vp->text,
691 * The "local" command.
695 localcmd(int argc __unused, char **argv __unused)
700 error("Not in a function");
701 while ((name = *argptr++) != NULL) {
709 * Make a variable a local variable. When a variable is made local, it's
710 * value and flags are saved in a localvar structure. The saved values
711 * will be restored when the shell function returns. We handle the name
712 * "-" as a special case.
718 struct localvar *lvp;
723 lvp = ckmalloc(sizeof (struct localvar));
724 if (name[0] == '-' && name[1] == '\0') {
725 lvp->text = ckmalloc(sizeof optlist);
726 memcpy(lvp->text, optlist, sizeof optlist);
730 for (vp = *vpp ; vp && ! varequal(vp->text, name) ; vp = vp->next);
732 if (strchr(name, '='))
733 setvareq(savestr(name), VSTRFIXED);
735 setvar(name, NULL, VSTRFIXED);
736 vp = *vpp; /* the new variable */
740 lvp->text = vp->text;
741 lvp->flags = vp->flags;
742 vp->flags |= VSTRFIXED|VTEXTFIXED;
743 if (strchr(name, '='))
744 setvareq(savestr(name), 0);
748 lvp->next = localvars;
755 * Called after a function returns.
761 struct localvar *lvp;
764 while ((lvp = localvars) != NULL) {
765 localvars = lvp->next;
767 if (vp == NULL) { /* $- saved */
768 memcpy(optlist, lvp->text, sizeof optlist);
771 } else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) {
774 if ((vp->flags & VTEXTFIXED) == 0)
776 vp->flags = lvp->flags;
777 vp->text = lvp->text;
785 setvarcmd(int argc, char **argv)
788 return unsetcmd(argc, argv);
790 setvar(argv[1], argv[2], 0);
792 error("too many arguments");
798 * The unset builtin command.
802 unsetcmd(int argc __unused, char **argv __unused)
810 while ((i = nextopt("vf")) != '\0') {
816 if (flg_func == 0 && flg_var == 0)
819 for (ap = argptr; *ap ; ap++) {
821 ret |= unsetfunc(*ap);
823 ret |= unsetvar(*ap);
830 * Unset the specified variable.
834 unsetvar(const char *s)
840 for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) {
841 if (varequal(vp->text, s)) {
842 if (vp->flags & VREADONLY)
845 if (*(strchr(vp->text, '=') + 1) != '\0')
846 setvar(s, nullstr, 0);
847 if ((vp->flags & VEXPORT) && localevar(vp->text)) {
848 change_env(__DECONST(char *, s), 0);
849 setlocale(LC_ALL, "");
851 vp->flags &= ~VEXPORT;
853 if ((vp->flags & VSTRFIXED) == 0) {
854 if ((vp->flags & VTEXTFIXED) == 0)
869 * Find the appropriate entry in the hash table from the name.
873 hashvar(const char *p)
875 unsigned int hashval;
877 hashval = ((unsigned char) *p) << 4;
878 while (*p && *p != '=')
879 hashval += (unsigned char) *p++;
880 return &vartab[hashval % VTABSIZE];
886 * Returns true if the two strings specify the same varable. The first
887 * variable name is terminated by '='; the second may be terminated by
888 * either '=' or '\0'.
892 varequal(const char *p, const char *q)
898 if (*p == '=' && *(q - 1) == '\0')