1 /* $NetBSD: var.c,v 1.38 2006/12/18 00:37:33 christos Exp $ */
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
38 static char sccsid
[] = "@(#)var.c 8.3 (Berkeley) 5/4/95";
40 __RCSID("$NetBSD: var.c,v 1.38 2006/12/18 00:37:33 christos Exp $");
56 #include "nodes.h" /* for other headers */
57 #include "eval.h" /* defines cmdenviron */
69 #include "myhistedit.h"
83 void (*func
)(const char *);
86 struct localvar
*localvars
;
105 const struct varinit varinit
[] = {
107 { &vatty
, VSTRFIXED
|VTEXTFIXED
|VUNSET
, "ATTY=",
111 { &vhistsize
, VSTRFIXED
|VTEXTFIXED
|VUNSET
, "HISTSIZE=",
114 { &vifs
, VSTRFIXED
|VTEXTFIXED
, "IFS= \t\n",
116 { &vmail
, VSTRFIXED
|VTEXTFIXED
|VUNSET
, "MAIL=",
118 { &vmpath
, VSTRFIXED
|VTEXTFIXED
|VUNSET
, "MAILPATH=",
120 { &vpath
, VSTRFIXED
|VTEXTFIXED
, "PATH=" _PATH_DEFPATH
,
123 * vps1 depends on uid
125 { &vps2
, VSTRFIXED
|VTEXTFIXED
, "PS2=> ",
127 { &vps4
, VSTRFIXED
|VTEXTFIXED
, "PS4=+ ",
130 { &vterm
, VSTRFIXED
|VTEXTFIXED
|VUNSET
, "TERM=",
133 { &voptind
, VSTRFIXED
|VTEXTFIXED
|VNOFUNC
, "OPTIND=1",
139 struct var
*vartab
[VTABSIZE
];
141 STATIC
int strequal(const char *, const char *);
142 STATIC
struct var
*find_var(const char *, struct var
***, int *);
145 * Initialize the varable symbol tables and import the environment
150 MKINIT
char **environ
;
155 for (envp
= environ
; *envp
; envp
++) {
156 if (strchr(*envp
, '=')) {
157 setvareq(*envp
, VEXPORT
|VTEXTFIXED
);
165 * This routine initializes the builtin variables. It is called when the
166 * shell is initialized and again when a shell procedure is spawned.
172 const struct varinit
*ip
;
176 for (ip
= varinit
; (vp
= ip
->var
) != NULL
; ip
++) {
177 if (find_var(ip
->text
, &vpp
, &vp
->name_len
) != NULL
)
181 vp
->text
= strdup(ip
->text
);
182 vp
->flags
= ip
->flags
;
188 if (find_var("PS1", &vpp
, &vps1
.name_len
) == NULL
) {
191 vps1
.text
= strdup(geteuid() ? "PS1=$ " : "PS1=# ");
192 vps1
.flags
= VSTRFIXED
|VTEXTFIXED
;
197 * Safe version of setvar, returns 1 on success 0 on failure.
201 setvarsafe(const char *name
, const char *val
, int flags
)
203 struct jmploc jmploc
;
204 struct jmploc
*volatile savehandler
= handler
;
205 int volatile err
= 0;
207 if (setjmp(jmploc
.loc
))
211 setvar(name
, val
, flags
);
213 handler
= savehandler
;
218 * Set the value of a variable. The flags argument is ored with the
219 * flags of the variable. If val is NULL, the variable is unset.
223 setvar(const char *name
, const char *val
, int flags
)
239 if (! is_in_name(*p
)) {
240 if (*p
== '\0' || *p
== '=')
248 error("%.*s: bad variable name", namelen
, name
);
249 len
= namelen
+ 2; /* 2 is space for '=' and '\0' */
255 d
= nameeq
= ckmalloc(len
);
257 while (--namelen
>= 0)
263 setvareq(nameeq
, flags
);
269 * Same as setvar except that the variable and value are passed in
270 * the first argument as name=value. Since the first argument will
271 * be actually stored in the table, it should not be a string that
276 setvareq(char *s
, int flags
)
278 struct var
*vp
, **vpp
;
283 vp
= find_var(s
, &vpp
, &nlen
);
285 if (vp
->flags
& VREADONLY
)
286 error("%.*s: is read only", vp
->name_len
, s
);
291 if (vp
->func
&& (flags
& VNOFUNC
) == 0)
292 (*vp
->func
)(s
+ vp
->name_len
+ 1);
294 if ((vp
->flags
& (VTEXTFIXED
|VSTACK
)) == 0)
297 vp
->flags
&= ~(VTEXTFIXED
|VSTACK
|VUNSET
);
298 vp
->flags
|= flags
& ~VNOFUNC
;
302 * We could roll this to a function, to handle it as
303 * a regular variable function callback, but why bother?
305 if (vp
== &vmpath
|| (vp
== &vmail
&& ! mpathset()))
313 vp
= ckmalloc(sizeof (*vp
));
314 vp
->flags
= flags
& ~VNOFUNC
;
325 * Process a linked list of variable assignments.
329 listsetvar(struct strlist
*list
, int flags
)
334 for (lp
= list
; lp
; lp
= lp
->next
) {
335 setvareq(savestr(lp
->text
), flags
);
341 listmklocal(struct strlist
*list
, int flags
)
345 for (lp
= list
; lp
; lp
= lp
->next
)
346 mklocal(lp
->text
, flags
);
351 * Find the value of a variable. Returns NULL if not set.
355 lookupvar(const char *name
)
359 v
= find_var(name
, NULL
, NULL
);
360 if (v
== NULL
|| v
->flags
& VUNSET
)
362 return v
->text
+ v
->name_len
+ 1;
368 * Search the environment of a builtin command. If the second argument
369 * is nonzero, return the value of a variable even if it hasn't been
374 bltinlookup(const char *name
, int doall
)
379 for (sp
= cmdenviron
; sp
; sp
= sp
->next
) {
380 if (strequal(sp
->text
, name
))
381 return strchr(sp
->text
, '=') + 1;
384 v
= find_var(name
, NULL
, NULL
);
386 if (v
== NULL
|| v
->flags
& VUNSET
|| (!doall
&& !(v
->flags
& VEXPORT
)))
388 return v
->text
+ v
->name_len
+ 1;
394 * Generate a list of exported variables. This routine is used to construct
395 * the third argument to execve when executing a program.
408 for (vpp
= vartab
; vpp
< vartab
+ VTABSIZE
; vpp
++) {
409 for (vp
= *vpp
; vp
; vp
= vp
->next
)
410 if (vp
->flags
& VEXPORT
)
413 ep
= env
= stalloc((nenv
+ 1) * sizeof *env
);
414 for (vpp
= vartab
; vpp
< vartab
+ VTABSIZE
; vpp
++) {
415 for (vp
= *vpp
; vp
; vp
= vp
->next
)
416 if (vp
->flags
& VEXPORT
)
425 * Called when a shell procedure is invoked to clear out nonexported
426 * variables. It is also necessary to reallocate variables of with
427 * VSTACK set since these are currently allocated on the stack.
431 void shprocvar(void);
442 struct var
*vp
, **prev
;
444 for (vpp
= vartab
; vpp
< vartab
+ VTABSIZE
; vpp
++) {
445 for (prev
= vpp
; (vp
= *prev
) != NULL
; ) {
446 if ((vp
->flags
& VEXPORT
) == 0) {
448 if ((vp
->flags
& VTEXTFIXED
) == 0)
450 if ((vp
->flags
& VSTRFIXED
) == 0)
453 if (vp
->flags
& VSTACK
) {
454 vp
->text
= savestr(vp
->text
);
455 vp
->flags
&=~ VSTACK
;
467 * Command to list all variables which are set. Currently this command
468 * is invoked from the set command when the set command is called without
473 print_quoted(const char *p
)
477 if (strcspn(p
, "|&;<>()$`\\\"' \t\n*?[]#~=%") == strlen(p
)) {
492 out1fmt("'%.*s'", (int)(q
- p
), p
);
498 sort_var(const void *v_v1
, const void *v_v2
)
500 const struct var
* const *v1
= v_v1
;
501 const struct var
* const *v2
= v_v2
;
503 /* XXX Will anyone notice we include the '=' of the shorter name? */
504 return strcoll((*v1
)->text
, (*v2
)->text
);
508 * POSIX requires that 'set' (but not export or readonly) output the
509 * variables in lexicographic order - by the locale's collating order (sigh).
510 * Maybe we could keep them in an ordered balanced binary tree
511 * instead of hashed lists.
512 * For now just roll 'em through qsort for printing...
516 showvars(const char *name
, int flag
, int show_value
)
522 static struct var
**list
; /* static in case we are interrupted */
528 list
= ckmalloc(list_len
* sizeof *list
);
531 for (vpp
= vartab
; vpp
< vartab
+ VTABSIZE
; vpp
++) {
532 for (vp
= *vpp
; vp
; vp
= vp
->next
) {
533 if (flag
&& !(vp
->flags
& flag
))
535 if (vp
->flags
& VUNSET
&& !(show_value
& 2))
537 if (count
>= list_len
) {
538 list
= ckrealloc(list
,
539 (list_len
<< 1) * sizeof *list
);
546 qsort(list
, count
, sizeof *list
, sort_var
);
548 for (vpp
= list
; count
--; vpp
++) {
551 out1fmt("%s ", name
);
552 for (p
= vp
->text
; *p
!= '=' ; p
++)
554 if (!(vp
->flags
& VUNSET
) && show_value
) {
566 * The export and readonly commands.
570 exportcmd(int argc
, char **argv
)
575 int flag
= argv
[0][0] == 'r'? VREADONLY
: VEXPORT
;
578 pflag
= nextopt("p") == 'p' ? 3 : 0;
579 if (argc
<= 1 || pflag
) {
580 showvars( pflag
? argv
[0] : 0, flag
, pflag
);
584 while ((name
= *argptr
++) != NULL
) {
585 if ((p
= strchr(name
, '=')) != NULL
) {
588 vp
= find_var(name
, NULL
, NULL
);
594 setvar(name
, p
, flag
);
601 * The "local" command.
605 localcmd(int argc
, char **argv
)
610 error("Not in a function");
611 while ((name
= *argptr
++) != NULL
) {
619 * Make a variable a local variable. When a variable is made local, its
620 * value and flags are saved in a localvar structure. The saved values
621 * will be restored when the shell function returns. We handle the name
622 * "-" as a special case.
626 mklocal(const char *name
, int flags
)
628 struct localvar
*lvp
;
633 lvp
= ckmalloc(sizeof (struct localvar
));
634 if (name
[0] == '-' && name
[1] == '\0') {
636 p
= ckmalloc(sizeof_optlist
);
637 lvp
->text
= memcpy(p
, optlist
, sizeof_optlist
);
640 vp
= find_var(name
, &vpp
, NULL
);
642 if (strchr(name
, '='))
643 setvareq(savestr(name
), VSTRFIXED
|flags
);
645 setvar(name
, NULL
, VSTRFIXED
|flags
);
646 vp
= *vpp
; /* the new variable */
650 lvp
->text
= vp
->text
;
651 lvp
->flags
= vp
->flags
;
652 vp
->flags
|= VSTRFIXED
|VTEXTFIXED
;
653 if (name
[vp
->name_len
] == '=')
654 setvareq(savestr(name
), flags
);
658 lvp
->next
= localvars
;
665 * Called after a function returns.
671 struct localvar
*lvp
;
674 while ((lvp
= localvars
) != NULL
) {
675 localvars
= lvp
->next
;
677 TRACE(("poplocalvar %s", vp
? vp
->text
: "-"));
678 if (vp
== NULL
) { /* $- saved */
679 memcpy(optlist
, lvp
->text
, sizeof_optlist
);
681 } else if ((lvp
->flags
& (VUNSET
|VSTRFIXED
)) == VUNSET
) {
682 (void)unsetvar(vp
->text
, 0);
684 if (vp
->func
&& (vp
->flags
& VNOFUNC
) == 0)
685 (*vp
->func
)(lvp
->text
+ vp
->name_len
+ 1);
686 if ((vp
->flags
& VTEXTFIXED
) == 0)
688 vp
->flags
= lvp
->flags
;
689 vp
->text
= lvp
->text
;
697 setvarcmd(int argc
, char **argv
)
700 return unsetcmd(argc
, argv
);
702 setvar(argv
[1], argv
[2], 0);
704 error("List assignment not implemented");
710 * The unset builtin command. We unset the function before we unset the
711 * variable to allow a function to be unset when there is a readonly variable
712 * with the same name.
716 unsetcmd(int argc
, char **argv
)
724 while ((i
= nextopt("evf")) != '\0') {
730 if (flg_func
== 0 && flg_var
== 0)
733 for (ap
= argptr
; *ap
; ap
++) {
735 ret
|= unsetfunc(*ap
);
737 ret
|= unsetvar(*ap
, flg_var
== 'e');
744 * Unset the specified variable.
748 unsetvar(const char *s
, int unexport
)
753 vp
= find_var(s
, &vpp
, NULL
);
757 if (vp
->flags
& VREADONLY
)
762 vp
->flags
&= ~VEXPORT
;
764 if (vp
->text
[vp
->name_len
+ 1] != '\0')
765 setvar(s
, nullstr
, 0);
766 vp
->flags
&= ~VEXPORT
;
768 if ((vp
->flags
& VSTRFIXED
) == 0) {
769 if ((vp
->flags
& VTEXTFIXED
) == 0)
781 * Returns true if the two strings specify the same varable. The first
782 * variable name is terminated by '='; the second may be terminated by
783 * either '=' or '\0'.
787 strequal(const char *p
, const char *q
)
793 if (*p
== '=' && *(q
- 1) == '\0')
799 * Search for a variable.
800 * 'name' may be terminated by '=' or a NUL.
801 * vppp is set to the pointer to vp, or the list head if vp isn't found
802 * lenp is set to the number of charactets in 'name'
806 find_var(const char *name
, struct var
***vppp
, int *lenp
)
808 unsigned int hashval
;
810 struct var
*vp
, **vpp
;
811 const char *p
= name
;
814 while (*p
&& *p
!= '=')
815 hashval
= 2 * hashval
+ (unsigned char)*p
++;
820 vpp
= &vartab
[hashval
% VTABSIZE
];
824 for (vp
= *vpp
; vp
; vpp
= &vp
->next
, vp
= *vpp
) {
825 if (vp
->name_len
!= len
)
827 if (memcmp(vp
->text
, name
, len
) != 0)