1 /* $NetBSD: var.c,v 1.44 2015/05/26 21:35:15 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.44 2015/05/26 21:35:15 christos Exp $");
57 #include "nodes.h" /* for other headers */
58 #include "eval.h" /* defines cmdenviron */
71 #include "myhistedit.h"
85 void (*func
)(const char *);
88 struct localvar
*localvars
;
107 const struct varinit varinit
[] = {
109 { &vatty
, VSTRFIXED
|VTEXTFIXED
|VUNSET
, "ATTY=",
113 { &vhistsize
, VSTRFIXED
|VTEXTFIXED
|VUNSET
, "HISTSIZE=",
116 { &vifs
, VSTRFIXED
|VTEXTFIXED
, "IFS= \t\n",
118 { &vmail
, VSTRFIXED
|VTEXTFIXED
|VUNSET
, "MAIL=",
120 { &vmpath
, VSTRFIXED
|VTEXTFIXED
|VUNSET
, "MAILPATH=",
122 { &vpath
, VSTRFIXED
|VTEXTFIXED
, "PATH=" _PATH_DEFPATH
,
125 * vps1 depends on uid
127 { &vps2
, VSTRFIXED
|VTEXTFIXED
, "PS2=> ",
129 { &vps4
, VSTRFIXED
|VTEXTFIXED
, "PS4=+ ",
132 { &vterm
, VSTRFIXED
|VTEXTFIXED
|VUNSET
, "TERM=",
135 { &voptind
, VSTRFIXED
|VTEXTFIXED
|VNOFUNC
, "OPTIND=1",
141 struct var
*vartab
[VTABSIZE
];
143 STATIC
int strequal(const char *, const char *);
144 STATIC
struct var
*find_var(const char *, struct var
***, int *);
147 * Initialize the varable symbol tables and import the environment
152 MKINIT
char **environ
;
157 for (envp
= environ
; *envp
; envp
++) {
158 if (strchr(*envp
, '=')) {
159 setvareq(*envp
, VEXPORT
|VTEXTFIXED
);
167 * This routine initializes the builtin variables. It is called when the
168 * shell is initialized and again when a shell procedure is spawned.
174 const struct varinit
*ip
;
178 for (ip
= varinit
; (vp
= ip
->var
) != NULL
; ip
++) {
179 if (find_var(ip
->text
, &vpp
, &vp
->name_len
) != NULL
)
183 vp
->text
= strdup(ip
->text
);
184 vp
->flags
= ip
->flags
;
190 if (find_var("PS1", &vpp
, &vps1
.name_len
) == NULL
) {
193 vps1
.flags
= VSTRFIXED
|VTEXTFIXED
;
203 vps1
.text
= strdup(geteuid() ? "PS1=$ " : "PS1=# ");
207 * Safe version of setvar, returns 1 on success 0 on failure.
211 setvarsafe(const char *name
, const char *val
, int flags
)
213 struct jmploc jmploc
;
214 struct jmploc
*volatile savehandler
= handler
;
215 int volatile err
= 0;
217 if (setjmp(jmploc
.loc
))
221 setvar(name
, val
, flags
);
223 handler
= savehandler
;
228 * Set the value of a variable. The flags argument is ored with the
229 * flags of the variable. If val is NULL, the variable is unset.
233 setvar(const char *name
, const char *val
, int flags
)
249 if (! is_in_name(*p
)) {
250 if (*p
== '\0' || *p
== '=')
258 error("%.*s: bad variable name", namelen
, name
);
259 len
= namelen
+ 2; /* 2 is space for '=' and '\0' */
265 d
= nameeq
= ckmalloc(len
);
267 while (--namelen
>= 0)
273 setvareq(nameeq
, flags
);
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
;
293 vp
= find_var(s
, &vpp
, &nlen
);
295 if (vp
->flags
& VREADONLY
)
296 error("%.*s: is read only", vp
->name_len
, s
);
301 if (vp
->func
&& (flags
& VNOFUNC
) == 0)
302 (*vp
->func
)(s
+ vp
->name_len
+ 1);
304 if ((vp
->flags
& (VTEXTFIXED
|VSTACK
)) == 0)
307 vp
->flags
&= ~(VTEXTFIXED
|VSTACK
|VUNSET
);
308 vp
->flags
|= flags
& ~VNOFUNC
;
312 * We could roll this to a function, to handle it as
313 * a regular variable function callback, but why bother?
315 if (vp
== &vmpath
|| (vp
== &vmail
&& ! mpathset()))
323 vp
= ckmalloc(sizeof (*vp
));
324 vp
->flags
= flags
& ~VNOFUNC
;
335 * Process a linked list of variable assignments.
339 listsetvar(struct strlist
*list
, int flags
)
344 for (lp
= list
; lp
; lp
= lp
->next
) {
345 setvareq(savestr(lp
->text
), flags
);
351 listmklocal(struct strlist
*list
, int flags
)
355 for (lp
= list
; lp
; lp
= lp
->next
)
356 mklocal(lp
->text
, flags
);
361 * Find the value of a variable. Returns NULL if not set.
365 lookupvar(const char *name
)
369 v
= find_var(name
, NULL
, NULL
);
370 if (v
== NULL
|| v
->flags
& VUNSET
)
372 return v
->text
+ v
->name_len
+ 1;
378 * Search the environment of a builtin command. If the second argument
379 * is nonzero, return the value of a variable even if it hasn't been
384 bltinlookup(const char *name
, int doall
)
389 for (sp
= cmdenviron
; sp
; sp
= sp
->next
) {
390 if (strequal(sp
->text
, name
))
391 return strchr(sp
->text
, '=') + 1;
394 v
= find_var(name
, NULL
, NULL
);
396 if (v
== NULL
|| v
->flags
& VUNSET
|| (!doall
&& !(v
->flags
& VEXPORT
)))
398 return v
->text
+ v
->name_len
+ 1;
404 * Generate a list of exported variables. This routine is used to construct
405 * the third argument to execve when executing a program.
418 for (vpp
= vartab
; vpp
< vartab
+ VTABSIZE
; vpp
++) {
419 for (vp
= *vpp
; vp
; vp
= vp
->next
)
420 if (vp
->flags
& VEXPORT
)
423 ep
= env
= stalloc((nenv
+ 1) * sizeof *env
);
424 for (vpp
= vartab
; vpp
< vartab
+ VTABSIZE
; vpp
++) {
425 for (vp
= *vpp
; vp
; vp
= vp
->next
)
426 if (vp
->flags
& VEXPORT
)
435 * Called when a shell procedure is invoked to clear out nonexported
436 * variables. It is also necessary to reallocate variables of with
437 * VSTACK set since these are currently allocated on the stack.
441 void shprocvar(void);
452 struct var
*vp
, **prev
;
454 for (vpp
= vartab
; vpp
< vartab
+ VTABSIZE
; vpp
++) {
455 for (prev
= vpp
; (vp
= *prev
) != NULL
; ) {
456 if ((vp
->flags
& VEXPORT
) == 0) {
458 if ((vp
->flags
& VTEXTFIXED
) == 0)
460 if ((vp
->flags
& VSTRFIXED
) == 0)
463 if (vp
->flags
& VSTACK
) {
464 vp
->text
= savestr(vp
->text
);
465 vp
->flags
&=~ VSTACK
;
477 * Command to list all variables which are set. Currently this command
478 * is invoked from the set command when the set command is called without
483 print_quoted(const char *p
)
487 if (strcspn(p
, "|&;<>()$`\\\"' \t\n*?[]#~=%") == strlen(p
)) {
502 out1fmt("'%.*s'", (int)(q
- p
), p
);
508 sort_var(const void *v_v1
, const void *v_v2
)
510 const struct var
* const *v1
= v_v1
;
511 const struct var
* const *v2
= v_v2
;
513 /* XXX Will anyone notice we include the '=' of the shorter name? */
514 return strcoll((*v1
)->text
, (*v2
)->text
);
518 * POSIX requires that 'set' (but not export or readonly) output the
519 * variables in lexicographic order - by the locale's collating order (sigh).
520 * Maybe we could keep them in an ordered balanced binary tree
521 * instead of hashed lists.
522 * For now just roll 'em through qsort for printing...
526 showvars(const char *name
, int flag
, int show_value
)
532 static struct var
**list
; /* static in case we are interrupted */
538 list
= ckmalloc(list_len
* sizeof *list
);
541 for (vpp
= vartab
; vpp
< vartab
+ VTABSIZE
; vpp
++) {
542 for (vp
= *vpp
; vp
; vp
= vp
->next
) {
543 if (flag
&& !(vp
->flags
& flag
))
545 if (vp
->flags
& VUNSET
&& !(show_value
& 2))
547 if (count
>= list_len
) {
548 list
= ckrealloc(list
,
549 (list_len
<< 1) * sizeof *list
);
556 qsort(list
, count
, sizeof *list
, sort_var
);
558 for (vpp
= list
; count
--; vpp
++) {
561 out1fmt("%s ", name
);
562 for (p
= vp
->text
; *p
!= '=' ; p
++)
564 if (!(vp
->flags
& VUNSET
) && show_value
) {
576 * The export and readonly commands.
580 exportcmd(int argc
, char **argv
)
585 int flag
= argv
[0][0] == 'r'? VREADONLY
: VEXPORT
;
588 pflg
= nextopt("p") == 'p' ? 3 : 0;
589 if (argc
<= 1 || pflg
) {
590 showvars( pflg
? argv
[0] : 0, flag
, pflg
);
594 while ((name
= *argptr
++) != NULL
) {
595 if ((p
= strchr(name
, '=')) != NULL
) {
598 vp
= find_var(name
, NULL
, NULL
);
604 setvar(name
, p
, flag
);
611 * The "local" command.
615 localcmd(int argc
, char **argv
)
620 error("Not in a function");
621 while ((name
= *argptr
++) != NULL
) {
629 * Make a variable a local variable. When a variable is made local, its
630 * value and flags are saved in a localvar structure. The saved values
631 * will be restored when the shell function returns. We handle the name
632 * "-" as a special case.
636 mklocal(const char *name
, int flags
)
638 struct localvar
*lvp
;
643 lvp
= ckmalloc(sizeof (struct localvar
));
644 if (name
[0] == '-' && name
[1] == '\0') {
646 p
= ckmalloc(sizeof_optlist
);
647 lvp
->text
= memcpy(p
, optlist
, sizeof_optlist
);
650 vp
= find_var(name
, &vpp
, NULL
);
652 if (strchr(name
, '='))
653 setvareq(savestr(name
), VSTRFIXED
|flags
);
655 setvar(name
, NULL
, VSTRFIXED
|flags
);
656 vp
= *vpp
; /* the new variable */
660 lvp
->text
= vp
->text
;
661 lvp
->flags
= vp
->flags
;
662 vp
->flags
|= VSTRFIXED
|VTEXTFIXED
;
663 if (name
[vp
->name_len
] == '=')
664 setvareq(savestr(name
), flags
);
668 lvp
->next
= localvars
;
675 * Called after a function returns.
681 struct localvar
*lvp
;
684 while ((lvp
= localvars
) != NULL
) {
685 localvars
= lvp
->next
;
687 TRACE(("poplocalvar %s", vp
? vp
->text
: "-"));
688 if (vp
== NULL
) { /* $- saved */
689 memcpy(optlist
, lvp
->text
, sizeof_optlist
);
691 } else if ((lvp
->flags
& (VUNSET
|VSTRFIXED
)) == VUNSET
) {
692 (void)unsetvar(vp
->text
, 0);
694 if (vp
->func
&& (vp
->flags
& VNOFUNC
) == 0)
695 (*vp
->func
)(lvp
->text
+ vp
->name_len
+ 1);
696 if ((vp
->flags
& VTEXTFIXED
) == 0)
698 vp
->flags
= lvp
->flags
;
699 vp
->text
= lvp
->text
;
707 setvarcmd(int argc
, char **argv
)
710 return unsetcmd(argc
, argv
);
712 setvar(argv
[1], argv
[2], 0);
714 error("List assignment not implemented");
720 * The unset builtin command. We unset the function before we unset the
721 * variable to allow a function to be unset when there is a readonly variable
722 * with the same name.
726 unsetcmd(int argc
, char **argv
)
734 while ((i
= nextopt("evf")) != '\0') {
740 if (flg_func
== 0 && flg_var
== 0)
743 for (ap
= argptr
; *ap
; ap
++) {
745 ret
|= unsetfunc(*ap
);
747 ret
|= unsetvar(*ap
, flg_var
== 'e');
754 * Unset the specified variable.
758 unsetvar(const char *s
, int unexport
)
763 vp
= find_var(s
, &vpp
, NULL
);
767 if (vp
->flags
& VREADONLY
)
772 vp
->flags
&= ~VEXPORT
;
774 if (vp
->text
[vp
->name_len
+ 1] != '\0')
775 setvar(s
, nullstr
, 0);
776 vp
->flags
&= ~VEXPORT
;
778 if ((vp
->flags
& VSTRFIXED
) == 0) {
779 if ((vp
->flags
& VTEXTFIXED
) == 0)
791 * Returns true if the two strings specify the same varable. The first
792 * variable name is terminated by '='; the second may be terminated by
793 * either '=' or '\0'.
797 strequal(const char *p
, const char *q
)
803 if (*p
== '=' && *(q
- 1) == '\0')
809 * Search for a variable.
810 * 'name' may be terminated by '=' or a NUL.
811 * vppp is set to the pointer to vp, or the list head if vp isn't found
812 * lenp is set to the number of charactets in 'name'
816 find_var(const char *name
, struct var
***vppp
, int *lenp
)
818 unsigned int hashval
;
820 struct var
*vp
, **vpp
;
821 const char *p
= name
;
824 while (*p
&& *p
!= '=')
825 hashval
= 2 * hashval
+ (unsigned char)*p
++;
830 vpp
= &vartab
[hashval
% VTABSIZE
];
834 for (vp
= *vpp
; vp
; vpp
= &vp
->next
, vp
= *vpp
) {
835 if (vp
->name_len
!= len
)
837 if (memcmp(vp
->text
, name
, len
) != 0)