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.15.2.2 2002/08/27 01:36:28 tjr Exp $
38 * $DragonFly: src/bin/sh/var.c,v 1.12 2006/09/28 22:29:44 pavalos Exp $
53 #include "nodes.h" /* for other headers */
54 #include "eval.h" /* defines cmdenviron */
65 #include "myhistedit.h"
76 void (*func
)(const char *);
91 STATIC
struct var voptind
;
93 STATIC
const struct varinit varinit
[] = {
95 { &vhistsize
, VSTRFIXED
|VTEXTFIXED
|VUNSET
, "HISTSIZE=",
98 { &vifs
, VSTRFIXED
|VTEXTFIXED
, "IFS= \t\n",
100 { &vmail
, VSTRFIXED
|VTEXTFIXED
|VUNSET
, "MAIL=",
102 { &vmpath
, VSTRFIXED
|VTEXTFIXED
|VUNSET
, "MAILPATH=",
104 { &vpath
, VSTRFIXED
|VTEXTFIXED
, "PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/pkg/bin:/usr/pkg/sbin",
106 { &vppid
, VSTRFIXED
|VTEXTFIXED
|VUNSET
, "PPID=",
109 * vps1 depends on uid
111 { &vps2
, VSTRFIXED
|VTEXTFIXED
, "PS2=> ",
113 { &voptind
, VSTRFIXED
|VTEXTFIXED
, "OPTIND=1",
119 STATIC
struct var
*vartab
[VTABSIZE
];
121 STATIC
struct var
**hashvar(const char *);
122 STATIC
int varequal(const char *, const char *);
123 STATIC
int localevar(const char *);
126 * Initialize the variable symbol tables and import the environment.
131 MKINIT
char **environ
;
136 for (envp
= environ
; *envp
; envp
++) {
137 if (strchr(*envp
, '=')) {
138 setvareq(*envp
, VEXPORT
|VTEXTFIXED
);
146 * This routine initializes the builtin variables. It is called when the
147 * shell is initialized and again when a shell procedure is spawned.
154 const struct varinit
*ip
;
158 for (ip
= varinit
; (vp
= ip
->var
) != NULL
; ip
++) {
159 if ((vp
->flags
& VEXPORT
) == 0) {
160 vpp
= hashvar(ip
->text
);
163 vp
->text
= strdup(ip
->text
);
164 vp
->flags
= ip
->flags
;
171 if ((vps1
.flags
& VEXPORT
) == 0) {
172 vpp
= hashvar("PS1=");
175 vps1
.text
= strdup(geteuid() ? "PS1=$ " : "PS1=# ");
176 vps1
.flags
= VSTRFIXED
|VTEXTFIXED
;
178 if ((vppid
.flags
& VEXPORT
) == 0) {
179 fmtstr(ppid
, sizeof(ppid
), "%d", (int)getppid());
180 setvarsafe("PPID", ppid
, 0);
185 * Safe version of setvar, returns 1 on success 0 on failure.
189 setvarsafe(const char *name
, const char *val
, int flags
)
191 struct jmploc jmploc
;
192 struct jmploc
*volatile savehandler
= handler
;
195 /* Avoid longjmp clobbering */
199 if (setjmp(jmploc
.loc
))
203 setvar(name
, val
, flags
);
205 handler
= savehandler
;
210 * Set the value of a variable. The flags argument is tored with the
211 * flags of the variable. If val is NULL, the variable is unset.
215 setvar(const char *name
, const char *val
, int flags
)
230 if (!is_in_name(*cp
)) {
231 if (*cp
== '\0' || *cp
== '=')
239 error("%.*s: bad variable name", namelen
, name
);
240 len
= namelen
+ 2; /* 2 is space for '=' and '\0' */
246 p
= nameeq
= ckmalloc(len
);
248 while (--namelen
>= 0)
254 setvareq(nameeq
, flags
);
258 localevar(const char *s
)
260 static const char * const lnames
[7] = {
261 "ALL", "COLLATE", "CTYPE", "MONETARY",
262 "NUMERIC", "TIME", NULL
264 const char * const *ss
;
268 if (varequal(s
+ 1, "ANG"))
270 if (strncmp(s
+ 1, "C_", 2) != 0)
272 for (ss
= lnames
; *ss
; ss
++)
273 if (varequal(s
+ 3, *ss
))
279 * Same as setvar except that the variable and value are passed in
280 * the first argument as name=value. Since the first argument will
281 * be actually stored in the table, it should not be a string that
286 setvareq(char *s
, int flags
)
288 struct var
*vp
, **vpp
;
294 for (vp
= *vpp
; vp
; vp
= vp
->next
) {
295 if (varequal(s
, vp
->text
)) {
296 if (vp
->flags
& VREADONLY
) {
297 len
= strchr(s
, '=') - s
;
298 error("%.*s: is read only", len
, s
);
302 if (vp
->func
&& (flags
& VNOFUNC
) == 0)
303 (*vp
->func
)(strchr(s
, '=') + 1);
305 if ((vp
->flags
& (VTEXTFIXED
|VSTACK
)) == 0)
308 vp
->flags
&= ~(VTEXTFIXED
|VSTACK
|VUNSET
);
313 * We could roll this to a function, to handle it as
314 * a regular variable function callback, but why bother?
316 if (vp
== &vmpath
|| (vp
== &vmail
&& ! mpathset()))
318 if ((vp
->flags
& VEXPORT
) && localevar(s
)) {
320 error("putenv: cannot set %s", s
);
321 setlocale(LC_ALL
, "");
328 vp
= ckmalloc(sizeof (*vp
));
335 if ((vp
->flags
& VEXPORT
) && localevar(s
)) {
337 error("putenv: cannot set %s", s
);
338 setlocale(LC_ALL
, "");
346 * Process a linked list of variable assignments.
350 listsetvar(struct strlist
*list
)
355 for (lp
= list
; lp
; lp
= lp
->next
) {
356 setvareq(savestr(lp
->text
), 0);
364 * Find the value of a variable. Returns NULL if not set.
368 lookupvar(const char *name
)
372 for (v
= *hashvar(name
) ; v
; v
= v
->next
) {
373 if (varequal(v
->text
, name
)) {
374 if (v
->flags
& VUNSET
)
376 return strchr(v
->text
, '=') + 1;
385 * Search the environment of a builtin command. If the second argument
386 * is nonzero, return the value of a variable even if it hasn't been
391 bltinlookup(const char *name
, int doall
)
396 for (sp
= cmdenviron
; sp
; sp
= sp
->next
) {
397 if (varequal(sp
->text
, name
))
398 return strchr(sp
->text
, '=') + 1;
400 for (v
= *hashvar(name
) ; v
; v
= v
->next
) {
401 if (varequal(v
->text
, name
)) {
402 if ((v
->flags
& VUNSET
)
403 || (!doall
&& (v
->flags
& VEXPORT
) == 0))
405 return strchr(v
->text
, '=') + 1;
414 * Generate a list of exported variables. This routine is used to construct
415 * the third argument to execve when executing a program.
427 for (vpp
= vartab
; vpp
< vartab
+ VTABSIZE
; vpp
++) {
428 for (vp
= *vpp
; vp
; vp
= vp
->next
)
429 if (vp
->flags
& VEXPORT
)
432 ep
= env
= stalloc((nenv
+ 1) * sizeof *env
);
433 for (vpp
= vartab
; vpp
< vartab
+ VTABSIZE
; vpp
++) {
434 for (vp
= *vpp
; vp
; vp
= vp
->next
)
435 if (vp
->flags
& VEXPORT
)
444 * Called when a shell procedure is invoked to clear out nonexported
445 * variables. It is also necessary to reallocate variables of with
446 * VSTACK set since these are currently allocated on the stack.
450 void shprocvar(void);
461 struct var
*vp
, **prev
;
463 for (vpp
= vartab
; vpp
< vartab
+ VTABSIZE
; vpp
++) {
464 for (prev
= vpp
; (vp
= *prev
) != NULL
; ) {
465 if ((vp
->flags
& VEXPORT
) == 0) {
467 if ((vp
->flags
& VTEXTFIXED
) == 0)
469 if ((vp
->flags
& VSTRFIXED
) == 0)
472 if (vp
->flags
& VSTACK
) {
473 vp
->text
= savestr(vp
->text
);
474 vp
->flags
&=~ VSTACK
;
486 * Command to list all variables which are set. Currently this command
487 * is invoked from the set command when the set command is called without
492 showvarscmd(int argc __unused
, char **argv __unused
)
498 for (vpp
= vartab
; vpp
< vartab
+ VTABSIZE
; vpp
++) {
499 for (vp
= *vpp
; vp
; vp
= vp
->next
) {
500 if (vp
->flags
& VUNSET
)
502 for (s
= vp
->text
; *s
!= '='; s
++)
515 * The export and readonly commands.
519 exportcmd(int argc
, char **argv
)
527 int flag
= argv
[0][0] == 'r'? VREADONLY
: VEXPORT
;
530 optreset
= optind
= 1;
533 while ((ch
= getopt(argc
, argv
, "p")) != -1) {
540 error("unknown option: -%c", optopt
);
546 listsetvar(cmdenviron
);
548 while ((name
= *argptr
++) != NULL
) {
549 if ((p
= strchr(name
, '=')) != NULL
) {
553 for (vp
= *vpp
; vp
; vp
= vp
->next
) {
554 if (varequal(vp
->text
, name
)) {
557 if ((vp
->flags
& VEXPORT
) && localevar(vp
->text
)) {
558 if (putenv(vp
->text
) != 0)
559 error("putenv: cannot set %s", vp
->text
);
560 setlocale(LC_ALL
, "");
566 setvar(name
, p
, flag
);
570 for (vpp
= vartab
; vpp
< vartab
+ VTABSIZE
; vpp
++) {
571 for (vp
= *vpp
; vp
; vp
= vp
->next
) {
572 if (vp
->flags
& flag
) {
577 for (p
= vp
->text
; *p
!= '=' ; p
++)
579 if (values
&& !(vp
->flags
& VUNSET
)) {
593 * The "local" command.
597 localcmd(int argc __unused
, char **argv __unused
)
602 error("Not in a function");
603 while ((name
= *argptr
++) != NULL
) {
611 * Make a variable a local variable. When a variable is made local, it's
612 * value and flags are saved in a localvar structure. The saved values
613 * will be restored when the shell function returns. We handle the name
614 * "-" as a special case.
620 struct localvar
*lvp
;
625 lvp
= ckmalloc(sizeof (struct localvar
));
626 if (name
[0] == '-' && name
[1] == '\0') {
627 lvp
->text
= ckmalloc(sizeof optlist
);
628 memcpy(lvp
->text
, optlist
, sizeof optlist
);
632 for (vp
= *vpp
; vp
&& ! varequal(vp
->text
, name
) ; vp
= vp
->next
);
634 if (strchr(name
, '='))
635 setvareq(savestr(name
), VSTRFIXED
);
637 setvar(name
, NULL
, VSTRFIXED
);
638 vp
= *vpp
; /* the new variable */
642 lvp
->text
= vp
->text
;
643 lvp
->flags
= vp
->flags
;
644 vp
->flags
|= VSTRFIXED
|VTEXTFIXED
;
645 if (strchr(name
, '='))
646 setvareq(savestr(name
), 0);
650 lvp
->next
= localvars
;
657 * Called after a function returns.
663 struct localvar
*lvp
;
666 while ((lvp
= localvars
) != NULL
) {
667 localvars
= lvp
->next
;
669 if (vp
== NULL
) { /* $- saved */
670 memcpy(optlist
, lvp
->text
, sizeof optlist
);
672 } else if ((lvp
->flags
& (VUNSET
|VSTRFIXED
)) == VUNSET
) {
675 if ((vp
->flags
& VTEXTFIXED
) == 0)
677 vp
->flags
= lvp
->flags
;
678 vp
->text
= lvp
->text
;
686 setvarcmd(int argc
, char **argv
)
689 return unsetcmd(argc
, argv
);
691 setvar(argv
[1], argv
[2], 0);
693 error("List assignment not implemented");
699 * The unset builtin command. We unset the function before we unset the
700 * variable to allow a function to be unset when there is a readonly variable
701 * with the same name.
705 unsetcmd(int argc __unused
, char **argv __unused
)
713 while ((i
= nextopt("vf")) != '\0') {
719 if (flg_func
== 0 && flg_var
== 0)
722 for (ap
= argptr
; *ap
; ap
++) {
724 ret
|= unsetfunc(*ap
);
726 ret
|= unsetvar(*ap
);
733 * Unset the specified variable.
737 unsetvar(const char *s
)
743 for (vp
= *vpp
; vp
; vpp
= &vp
->next
, vp
= *vpp
) {
744 if (varequal(vp
->text
, s
)) {
745 if (vp
->flags
& VREADONLY
)
748 if (*(strchr(vp
->text
, '=') + 1) != '\0')
749 setvar(s
, nullstr
, 0);
750 if ((vp
->flags
& VEXPORT
) && localevar(vp
->text
)) {
752 setlocale(LC_ALL
, "");
754 vp
->flags
&= ~VEXPORT
;
756 if ((vp
->flags
& VSTRFIXED
) == 0) {
757 if ((vp
->flags
& VTEXTFIXED
) == 0)
772 * Find the appropriate entry in the hash table from the name.
776 hashvar(const char *p
)
778 unsigned int hashval
;
780 hashval
= ((unsigned char) *p
) << 4;
781 while (*p
&& *p
!= '=')
782 hashval
+= (unsigned char) *p
++;
783 return &vartab
[hashval
% VTABSIZE
];
789 * Returns true if the two strings specify the same varable. The first
790 * variable name is terminated by '='; the second may be terminated by
791 * either '=' or '\0'.
795 varequal(const char *p
, const char *q
)
801 if (*p
== '=' && *(q
- 1) == '\0')