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>
40 __FBSDID("$FreeBSD: src/bin/sh/var.c,v 1.26.2.1 2004/09/30 04:41:55 des Exp $");
58 #include "nodes.h" /* for other headers */
59 #include "eval.h" /* defines cmdenviron */
69 #if !defined(NO_HISTORY)
70 #include "myhistedit.h"
76 #define _PATH_DEFPATH "/usr/bin:/bin"
86 void (*func
)(const char *);
102 STATIC
struct var voptind
;
104 STATIC
const struct varinit varinit
[] = {
105 #if !defined(NO_HISTORY)
106 { &vhistsize
, VSTRFIXED
|VTEXTFIXED
|VUNSET
, "HISTSIZE=",
109 { &vifs
, VSTRFIXED
|VTEXTFIXED
, "IFS= \t\n",
111 { &vmail
, VSTRFIXED
|VTEXTFIXED
|VUNSET
, "MAIL=",
113 { &vmpath
, VSTRFIXED
|VTEXTFIXED
|VUNSET
, "MAILPATH=",
115 { &vpath
, VSTRFIXED
|VTEXTFIXED
, "PATH=" _PATH_DEFPATH
,
117 { &vppid
, VSTRFIXED
|VTEXTFIXED
|VUNSET
, "PPID=",
120 * vps1 depends on uid
122 { &vps2
, VSTRFIXED
|VTEXTFIXED
, "PS2=> ",
124 { &vpse
, VSTRFIXED
|VTEXTFIXED
|VUNSET
, "PSE=",
126 { &voptind
, VSTRFIXED
|VTEXTFIXED
, "OPTIND=1",
132 STATIC
struct var
*vartab
[VTABSIZE
];
134 STATIC
struct var
**hashvar(char *);
135 STATIC
int varequal(char *, char *);
136 STATIC
int localevar(char *);
139 * Initialize the varable symbol tables and import the environment
146 extern char **environ
;
149 for (envp
= environ
; *envp
; envp
++) {
150 if (strchr(*envp
, '=')) {
151 setvareq(*envp
, VEXPORT
|VTEXTFIXED
);
159 * This routine initializes the builtin variables. It is called when the
160 * shell is initialized and again when a shell procedure is spawned.
167 const struct varinit
*ip
;
171 for (ip
= varinit
; (vp
= ip
->var
) != NULL
; ip
++) {
172 if ((vp
->flags
& VEXPORT
) == 0) {
173 vpp
= hashvar(ip
->text
);
177 vp
->flags
= ip
->flags
;
184 if ((vps1
.flags
& VEXPORT
) == 0) {
185 vpp
= hashvar("PS1=");
188 vps1
.text
= geteuid() ? "PS1=$ " : "PS1=# ";
189 vps1
.flags
= VSTRFIXED
|VTEXTFIXED
;
191 if ((vppid
.flags
& VEXPORT
) == 0) {
192 fmtstr(ppid
, sizeof(ppid
), "%d", (int)getppid());
193 setvarsafe("PPID", ppid
, 0);
198 * Safe version of setvar, returns 1 on success 0 on failure.
202 setvarsafe(char *name
, char *val
, int flags
)
204 struct jmploc jmploc
;
205 struct jmploc
*volatile savehandler
= handler
;
208 /* Avoid longjmp clobbering */
212 if (setjmp(jmploc
.loc
))
216 setvar(name
, val
, flags
);
218 handler
= savehandler
;
223 * Set the value of a variable. The flags argument is tored with the
224 * flags of the variable. If val is NULL, the variable is unset.
228 setvar(char *name
, char *val
, int flags
)
242 if (! is_in_name(*p
)) {
243 if (*p
== '\0' || *p
== '=')
251 error("%.*s: bad variable name", namelen
, name
);
252 len
= namelen
+ 2; /* 2 is space for '=' and '\0' */
258 p
= nameeq
= ckmalloc(len
);
260 while (--namelen
>= 0)
266 setvareq(nameeq
, flags
);
272 static char *lnames
[7] = {
273 "ALL", "COLLATE", "CTYPE", "MONETARY",
274 "NUMERIC", "TIME", NULL
280 if (varequal(s
+ 1, "ANG"))
282 if (strncmp(s
+ 1, "C_", 2) != 0)
284 for (ss
= lnames
; *ss
; ss
++)
285 if (varequal(s
+ 3, *ss
))
291 * Same as setvar except that the variable and value are passed in
292 * the first argument as name=value. Since the first argument will
293 * be actually stored in the table, it should not be a string that
298 setvareq(char *s
, int flags
)
300 struct var
*vp
, **vpp
;
306 for (vp
= *vpp
; vp
; vp
= vp
->next
) {
307 if (varequal(s
, vp
->text
)) {
308 if (vp
->flags
& VREADONLY
) {
309 len
= strchr(s
, '=') - s
;
310 error("%.*s: is read only", len
, s
);
314 if (vp
->func
&& (flags
& VNOFUNC
) == 0)
315 (*vp
->func
)(strchr(s
, '=') + 1);
317 if ((vp
->flags
& (VTEXTFIXED
|VSTACK
)) == 0)
320 vp
->flags
&= ~(VTEXTFIXED
|VSTACK
|VUNSET
);
325 * We could roll this to a function, to handle it as
326 * a regular variable function callback, but why bother?
328 if (vp
== &vmpath
|| (vp
== &vmail
&& ! mpathset()))
330 if ((vp
->flags
& VEXPORT
) && localevar(s
)) {
332 (void) setlocale(LC_ALL
, "");
339 vp
= ckmalloc(sizeof (*vp
));
346 if ((vp
->flags
& VEXPORT
) && localevar(s
)) {
348 (void) setlocale(LC_ALL
, "");
356 * Process a linked list of variable assignments.
360 listsetvar(struct strlist
*list
)
365 for (lp
= list
; lp
; lp
= lp
->next
) {
366 setvareq(savestr(lp
->text
), 0);
374 * Find the value of a variable. Returns NULL if not set.
378 lookupvar(char *name
)
382 for (v
= *hashvar(name
) ; v
; v
= v
->next
) {
383 if (varequal(v
->text
, name
)) {
384 if (v
->flags
& VUNSET
)
386 return strchr(v
->text
, '=') + 1;
395 * Search the environment of a builtin command. If the second argument
396 * is nonzero, return the value of a variable even if it hasn't been
401 bltinlookup(char *name
, int doall
)
406 for (sp
= cmdenviron
; sp
; sp
= sp
->next
) {
407 if (varequal(sp
->text
, name
))
408 return strchr(sp
->text
, '=') + 1;
410 for (v
= *hashvar(name
) ; v
; v
= v
->next
) {
411 if (varequal(v
->text
, name
)) {
412 if ((v
->flags
& VUNSET
)
413 || (!doall
&& (v
->flags
& VEXPORT
) == 0))
415 return strchr(v
->text
, '=') + 1;
424 * Generate a list of exported variables. This routine is used to construct
425 * the third argument to execve when executing a program.
437 for (vpp
= vartab
; vpp
< vartab
+ VTABSIZE
; vpp
++) {
438 for (vp
= *vpp
; vp
; vp
= vp
->next
)
439 if (vp
->flags
& VEXPORT
)
442 ep
= env
= stalloc((nenv
+ 1) * sizeof *env
);
443 for (vpp
= vartab
; vpp
< vartab
+ VTABSIZE
; vpp
++) {
444 for (vp
= *vpp
; vp
; vp
= vp
->next
)
445 if (vp
->flags
& VEXPORT
)
454 * Called when a shell procedure is invoked to clear out nonexported
455 * variables. It is also necessary to reallocate variables of with
456 * VSTACK set since these are currently allocated on the stack.
469 struct var
*vp
, **prev
;
471 for (vpp
= vartab
; vpp
< vartab
+ VTABSIZE
; vpp
++) {
472 for (prev
= vpp
; (vp
= *prev
) != NULL
; ) {
473 if ((vp
->flags
& VEXPORT
) == 0) {
475 if ((vp
->flags
& VTEXTFIXED
) == 0)
477 if ((vp
->flags
& VSTRFIXED
) == 0)
480 if (vp
->flags
& VSTACK
) {
481 vp
->text
= savestr(vp
->text
);
482 vp
->flags
&=~ VSTACK
;
494 * Command to list all variables which are set. Currently this command
495 * is invoked from the set command when the set command is called without
500 showvarscmd(int argc __unused
, char **argv __unused
)
506 for (vpp
= vartab
; vpp
< vartab
+ VTABSIZE
; vpp
++) {
507 for (vp
= *vpp
; vp
; vp
= vp
->next
) {
508 if (vp
->flags
& VUNSET
)
510 for (s
= vp
->text
; *s
!= '='; s
++)
523 * The export and readonly commands.
527 exportcmd(int argc
, char **argv
)
535 int flag
= argv
[0][0] == 'r'? VREADONLY
: VEXPORT
;
538 optreset
= optind
= 1;
541 while ((ch
= getopt(argc
, argv
, "p")) != -1) {
548 error("unknown option: -%c", optopt
);
554 listsetvar(cmdenviron
);
556 while ((name
= *argptr
++) != NULL
) {
557 if ((p
= strchr(name
, '=')) != NULL
) {
561 for (vp
= *vpp
; vp
; vp
= vp
->next
) {
562 if (varequal(vp
->text
, name
)) {
565 if ((vp
->flags
& VEXPORT
) && localevar(vp
->text
)) {
567 (void) setlocale(LC_ALL
, "");
573 setvar(name
, p
, flag
);
577 for (vpp
= vartab
; vpp
< vartab
+ VTABSIZE
; vpp
++) {
578 for (vp
= *vpp
; vp
; vp
= vp
->next
) {
579 if (vp
->flags
& flag
) {
584 for (p
= vp
->text
; *p
!= '=' ; p
++)
586 if (values
&& !(vp
->flags
& VUNSET
)) {
600 * The "local" command.
604 localcmd(int argc __unused
, char **argv __unused
)
609 error("Not in a function");
610 while ((name
= *argptr
++) != NULL
) {
618 * Make a variable a local variable. When a variable is made local, it's
619 * value and flags are saved in a localvar structure. The saved values
620 * will be restored when the shell function returns. We handle the name
621 * "-" as a special case.
627 struct localvar
*lvp
;
632 lvp
= ckmalloc(sizeof (struct localvar
));
633 if (name
[0] == '-' && name
[1] == '\0') {
634 lvp
->text
= ckmalloc(sizeof optlist
);
635 memcpy(lvp
->text
, optlist
, sizeof optlist
);
639 for (vp
= *vpp
; vp
&& ! varequal(vp
->text
, name
) ; vp
= vp
->next
);
641 if (strchr(name
, '='))
642 setvareq(savestr(name
), VSTRFIXED
);
644 setvar(name
, NULL
, VSTRFIXED
);
645 vp
= *vpp
; /* the new variable */
649 lvp
->text
= vp
->text
;
650 lvp
->flags
= vp
->flags
;
651 vp
->flags
|= VSTRFIXED
|VTEXTFIXED
;
652 if (strchr(name
, '='))
653 setvareq(savestr(name
), 0);
657 lvp
->next
= localvars
;
664 * Called after a function returns.
670 struct localvar
*lvp
;
673 while ((lvp
= localvars
) != NULL
) {
674 localvars
= lvp
->next
;
676 if (vp
== NULL
) { /* $- saved */
677 memcpy(optlist
, lvp
->text
, sizeof optlist
);
679 } else if ((lvp
->flags
& (VUNSET
|VSTRFIXED
)) == VUNSET
) {
680 (void)unsetvar(vp
->text
);
682 if ((vp
->flags
& VTEXTFIXED
) == 0)
684 vp
->flags
= lvp
->flags
;
685 vp
->text
= lvp
->text
;
693 setvarcmd(int argc
, char **argv
)
696 return unsetcmd(argc
, argv
);
698 setvar(argv
[1], argv
[2], 0);
700 error("List assignment not implemented");
706 * The unset builtin command. We unset the function before we unset the
707 * variable to allow a function to be unset when there is a readonly variable
708 * with the same name.
712 unsetcmd(int argc __unused
, char **argv __unused
)
720 while ((i
= nextopt("vf")) != '\0') {
726 if (flg_func
== 0 && flg_var
== 0)
729 for (ap
= argptr
; *ap
; ap
++) {
731 ret
|= unsetfunc(*ap
);
733 ret
|= unsetvar(*ap
);
740 * Unset the specified variable.
750 for (vp
= *vpp
; vp
; vpp
= &vp
->next
, vp
= *vpp
) {
751 if (varequal(vp
->text
, s
)) {
752 if (vp
->flags
& VREADONLY
)
755 if (*(strchr(vp
->text
, '=') + 1) != '\0')
756 setvar(s
, nullstr
, 0);
757 if ((vp
->flags
& VEXPORT
) && localevar(vp
->text
)) {
759 setlocale(LC_ALL
, "");
761 vp
->flags
&= ~VEXPORT
;
763 if ((vp
->flags
& VSTRFIXED
) == 0) {
764 if ((vp
->flags
& VTEXTFIXED
) == 0)
780 * Find the appropriate entry in the hash table from the name.
786 unsigned int hashval
;
788 hashval
= ((unsigned char) *p
) << 4;
789 while (*p
&& *p
!= '=')
790 hashval
+= (unsigned char) *p
++;
791 return &vartab
[hashval
% VTABSIZE
];
797 * Returns true if the two strings specify the same varable. The first
798 * variable name is terminated by '='; the second may be terminated by
799 * either '=' or '\0'.
803 varequal(char *p
, char *q
)
809 if (*p
== '=' && *(q
- 1) == '\0')
815 * $PchId: var.c,v 1.5 2006/05/22 12:28:49 philip Exp $