1 /* $NetBSD: var.c,v 1.36 2004/10/06 10:23:43 enami 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
37 static char sccsid
[] = "@(#)var.c 8.3 (Berkeley) 5/4/95";
39 __RCSID("$NetBSD: var.c,v 1.36 2004/10/06 10:23:43 enami Exp $");
47 #ifdef PC_OS2_LIBPATHS
52 #define LIBPATHSTRICT 3
59 DosQueryHeaderInfo(HMODULE hmod
, ULONG ulIndex
, PVOID pvBuffer
, ULONG cbBuffer
, ULONG ulSubFunction
);
60 #define QHINF_EXEINFO 1 /* NE exeinfo. */
61 #define QHINF_READRSRCTBL 2 /* Reads from the resource table. */
62 #define QHINF_READFILE 3 /* Reads from the executable file. */
63 #define QHINF_LIBPATHLENGTH 4 /* Gets the libpath length. */
64 #define QHINF_LIBPATH 5 /* Gets the entire libpath. */
65 #define QHINF_FIXENTRY 6 /* NE only */
66 #define QHINF_STE 7 /* NE only */
67 #define QHINF_MAPSEL 8 /* NE only */
80 #include "nodes.h" /* for other headers */
81 #include "eval.h" /* defines cmdenviron */
93 # include "myhistedit.h"
95 #include "shinstance.h"
100 //#define VTABSIZE 517
108 void (*func
)(shinstance
*, const char *);
116 //struct var vhistsize;
129 //struct var vvers; - unused
130 //struct var voptind;
132 #ifdef PC_OS2_LIBPATHS
133 //static struct var libpath_vars[4];
134 static const char * const libpath_envs
[4] = {"LIBPATH=", "BEGINLIBPATH=", "ENDLIBPATH=", "LIBPATHSTRICT="};
137 const struct varinit varinit
[] = {
139 { offsetof(shinstance
, vatty
), VSTRFIXED
|VSTRFIXED2
|VTEXTFIXED
|VUNSET
, "ATTY=",
143 { offsetof(shinstance
, vhistsize
), VSTRFIXED
|VSTRFIXED2
|VTEXTFIXED
|VUNSET
, "HISTSIZE=",
146 { offsetof(shinstance
, vifs
), VSTRFIXED
|VSTRFIXED2
|VTEXTFIXED
, "IFS= \t\n",
148 { offsetof(shinstance
, vmail
), VSTRFIXED
|VSTRFIXED2
|VTEXTFIXED
|VUNSET
, "MAIL=",
150 { offsetof(shinstance
, vmpath
), VSTRFIXED
|VSTRFIXED2
|VTEXTFIXED
|VUNSET
, "MAILPATH=",
152 { offsetof(shinstance
, vpath
), VSTRFIXED
|VSTRFIXED2
|VTEXTFIXED
, "PATH=" _PATH_DEFPATH
,
155 * vps1 depends on uid
157 { offsetof(shinstance
, vps2
), VSTRFIXED
|VSTRFIXED2
|VTEXTFIXED
, "PS2=> ",
159 { offsetof(shinstance
, vps4
), VSTRFIXED
|VSTRFIXED2
|VTEXTFIXED
, "PS4=+ ",
162 { offsetof(shinstance
, vterm
), VSTRFIXED
|VSTRFIXED2
|VTEXTFIXED
|VUNSET
, "TERM=",
165 { offsetof(shinstance
, voptind
), VSTRFIXED
|VSTRFIXED2
|VTEXTFIXED
|VNOFUNC
, "OPTIND=1",
171 //struct var *vartab[VTABSIZE];
173 STATIC
int strequal(const char *, const char *);
174 STATIC
struct var
*find_var(shinstance
*, const char *, struct var
***, int *);
177 * Initialize the varable symbol tables and import the environment
187 for (envp
= sh_environ(psh
) ; *envp
; envp
++) {
188 if (strchr(*envp
, '=')) {
189 setvareq(psh
, *envp
, VEXPORT
|VTEXTFIXED
);
197 * This routine initializes the builtin variables. It is called when the
198 * shell is initialized and again when a shell procedure is spawned.
202 initvar(shinstance
*psh
)
204 const struct varinit
*ip
;
207 #ifdef PC_OS2_LIBPATHS
208 char *psz
= ckmalloc(psh
, 2048);
212 for (i
= 0; i
< 4; i
++)
214 psh
->libpath_vars
[i
].flags
= VSTRFIXED
| VSTRFIXED2
| VOS2LIBPATH
;
215 psh
->libpath_vars
[i
].func
= NULL
;
219 psz
[0] = psz
[1] = psz
[2] = psz
[3] = '\0';
220 rc
= DosQueryExtLIBPATH(psz
, i
);
224 rc
= DosQueryHeaderInfo(NULLHANDLE
, 0, psz
, 2048, QHINF_LIBPATH
);
225 psh
->libpath_vars
[i
].flags
|= VREADONLY
;
229 int cch1
= strlen(libpath_envs
[i
]);
230 int cch2
= strlen(psz
) + 1;
231 psh
->libpath_vars
[i
].text
= ckmalloc(psh
, cch1
+ cch2
);
232 memcpy(psh
->libpath_vars
[i
].text
, libpath_envs
[i
], cch1
);
233 memcpy(psh
->libpath_vars
[i
].text
+ cch1
, psz
, cch2
);
237 psh
->libpath_vars
[i
].flags
|= VUNSET
| VTEXTFIXED
;
238 psh
->libpath_vars
[i
].text
= (char *)libpath_envs
[i
];
240 if (find_var(psh
, psh
->libpath_vars
[i
].text
, &vpp
, &psh
->libpath_vars
[i
].name_len
) != NULL
)
242 psh
->libpath_vars
[i
].next
= *vpp
;
243 *vpp
= &psh
->libpath_vars
[i
];
248 for (ip
= varinit
; ip
->text
; ip
++) {
249 vp
= (struct var
*)((char *)psh
+ ip
->var_off
);
250 if (find_var(psh
, ip
->text
, &vpp
, &vp
->name_len
) != NULL
)
254 vp
->text
= sh_strdup(psh
, ip
->text
);
255 vp
->flags
= ip
->flags
;
261 if (find_var(psh
, "PS1", &vpp
, &psh
->vps1
.name_len
) == NULL
) {
262 psh
->vps1
.next
= *vpp
;
264 #ifdef KBUILD_VERSION_MAJOR
265 psh
->vps1
.text
= sh_strdup(psh
, sh_geteuid(psh
) ? "PS1=kash$ " : "PS1=kash# ");
267 psh
->vps1
.text
= sh_strdup(psh
, sh_geteuid(psh
) ? "PS1=$ " : "PS1=# ");
269 psh
->vps1
.flags
= VSTRFIXED
|VSTRFIXED2
|VTEXTFIXED
; /** @todo text isn't fixed here... */
274 #ifndef SH_FORKED_MODE
276 * This routine is called to copy variable state from parent to child shell.
279 subshellinitvar(shinstance
*psh
, shinstance
*inherit
)
282 for (i
= 0; i
< K_ELEMENTS(inherit
->vartab
); i
++) {
283 struct var
const *vsrc
= inherit
->vartab
[i
];
285 struct var
**ppdst
= &psh
->vartab
[i
];
289 if (!(vsrc
->flags
& VSTRFIXED2
)) {
290 dst
= (struct var
*)ckmalloc(psh
, sizeof(*dst
));
292 dst
->flags
&= ~VSTRFIXED
;
294 /* VSTRFIXED2 is used when the structure is a fixed allocation in
295 the shinstance structure, so scan those to find which it is: */
296 size_t left
= ((struct var
*)&inherit
->vartab
[0] - &inherit
->vatty
);
297 struct var
const *fixedsrc
= &inherit
->vatty
;
300 if (vsrc
!= fixedsrc
) {
305 kHlpAssert(left
< 256 /*whatever, just no rollover*/);
309 if (!(vsrc
->flags
& VTEXTFIXED
)) {
310 dst
->text
= savestr(psh
, vsrc
->text
);
311 dst
->flags
&= ~VSTACK
;
323 /** @todo We don't always need to copy local variables. */
324 if (inherit
->localvars
) {
325 struct localvar
const *vsrc
= inherit
->localvars
;
326 struct localvar
**ppdst
= &psh
->localvars
;
329 struct localvar
*dst
= ckmalloc(psh
, sizeof(*dst
));
331 dst
->flags
= vsrc
->flags
& ~(VSTACK
| VTEXTFIXED
| (vsrc
->flags
& VSTRFIXED2
? 0 : VSTRFIXED
));
333 dst
->text
= savestr(psh
, vsrc
->text
);
337 dst
->flags
|= vsrc
->flags
& VTEXTFIXED
;
339 dst
->vp
= find_var(psh
, vsrc
->vp
->text
, NULL
, NULL
);
350 #endif /* !SH_FORKED_MODE */
353 * Safe version of setvar, returns 1 on success 0 on failure.
357 setvarsafe(shinstance
*psh
, const char *name
, const char *val
, int flags
)
359 struct jmploc jmploc
;
360 struct jmploc
*volatile savehandler
= psh
->handler
;
366 if (setjmp(jmploc
.loc
))
369 psh
->handler
= &jmploc
;
370 setvar(psh
, name
, val
, flags
);
372 psh
->handler
= savehandler
;
377 * Set the value of a variable. The flags argument is ored with the
378 * flags of the variable. If val is NULL, the variable is unset.
382 setvar(shinstance
*psh
, const char *name
, const char *val
, int flags
)
398 if (! is_in_name(*p
)) {
399 if (*p
== '\0' || *p
== '=')
405 namelen
= (int)(p
- name
);
407 error(psh
, "%.*s: bad variable name", namelen
, name
);
408 len
= namelen
+ 2; /* 2 is space for '=' and '\0' */
414 d
= nameeq
= ckmalloc(psh
, len
);
416 while (--namelen
>= 0)
422 setvareq(psh
, nameeq
, flags
);
428 * Same as setvar except that the variable and value are passed in
429 * the first argument as name=value. Since the first argument will
430 * be actually stored in the table, it should not be a string that
435 setvareq(shinstance
*psh
, char *s
, int flags
)
437 struct var
*vp
, **vpp
;
440 #if defined(_MSC_VER) || defined(_WIN32)
441 /* On Windows PATH is often spelled 'Path', correct this here. */
446 && (s
[4] == '\0' || s
[4] == '=') ) {
455 vp
= find_var(psh
, s
, &vpp
, &nlen
);
457 if (vp
->flags
& VREADONLY
)
458 error(psh
, "%.*s: is read only", vp
->name_len
, s
);
463 if (vp
->func
&& (flags
& VNOFUNC
) == 0)
464 (*vp
->func
)(psh
, s
+ vp
->name_len
+ 1);
466 if ((vp
->flags
& (VTEXTFIXED
|VSTACK
)) == 0)
467 ckfree(psh
, vp
->text
);
469 vp
->flags
&= ~(VTEXTFIXED
|VSTACK
|VUNSET
);
470 vp
->flags
|= flags
& ~VNOFUNC
;
472 #ifdef PC_OS2_LIBPATHS
473 if ((vp
->flags
& VOS2LIBPATH
) && (vp
->flags
& VEXPORT
))
474 vp
->flags
&= ~VEXPORT
;
478 * We could roll this to a function, to handle it as
479 * a regular variable function callback, but why bother?
481 if (vp
== &psh
->vmpath
|| (vp
== &psh
->vmail
&& ! mpathset(psh
)))
490 vp
= ckmalloc(psh
, sizeof (*vp
));
491 vp
->flags
= flags
& ~VNOFUNC
;
502 * Process a linked list of variable assignments.
506 listsetvar(shinstance
*psh
, struct strlist
*list
, int flags
)
511 for (lp
= list
; lp
; lp
= lp
->next
) {
512 setvareq(psh
, savestr(psh
, lp
->text
), flags
);
518 listmklocal(shinstance
*psh
, struct strlist
*list
, int flags
)
522 for (lp
= list
; lp
; lp
= lp
->next
)
523 mklocal(psh
, lp
->text
, flags
);
528 * Find the value of a variable. Returns NULL if not set.
532 lookupvar(shinstance
*psh
, const char *name
)
536 v
= find_var(psh
, name
, NULL
, NULL
);
537 if (v
== NULL
|| v
->flags
& VUNSET
)
539 return v
->text
+ v
->name_len
+ 1;
545 * Search the environment of a builtin command. If the second argument
546 * is nonzero, return the value of a variable even if it hasn't been
551 bltinlookup(shinstance
*psh
, const char *name
, int doall
)
556 for (sp
= psh
->cmdenviron
; sp
; sp
= sp
->next
) {
557 if (strequal(sp
->text
, name
))
558 return strchr(sp
->text
, '=') + 1;
561 v
= find_var(psh
, name
, NULL
, NULL
);
563 if (v
== NULL
|| v
->flags
& VUNSET
|| (!doall
&& !(v
->flags
& VEXPORT
)))
565 return v
->text
+ v
->name_len
+ 1;
571 * Generate a list of exported variables. This routine is used to construct
572 * the third argument to execve when executing a program.
576 environment(shinstance
*psh
)
585 for (vpp
= psh
->vartab
; vpp
< psh
->vartab
+ VTABSIZE
; vpp
++) {
586 for (vp
= *vpp
; vp
; vp
= vp
->next
)
587 if (vp
->flags
& VEXPORT
)
590 ep
= env
= stalloc(psh
, (nenv
+ 1) * sizeof *env
);
591 for (vpp
= psh
->vartab
; vpp
< psh
->vartab
+ VTABSIZE
; vpp
++) {
592 for (vp
= *vpp
; vp
; vp
= vp
->next
)
593 if (vp
->flags
& VEXPORT
)
598 #ifdef PC_OS2_LIBPATHS
600 * Set the libpaths now as this is exec() time.
602 for (nenv
= 0; nenv
< 3; nenv
++)
603 DosSetExtLIBPATH(strchr(psh
->libpath_vars
[nenv
].text
, '=') + 1, nenv
);
611 * Called when a shell procedure is invoked to clear out nonexported
612 * variables. It is also necessary to reallocate variables of with
613 * VSTACK set since these are currently allocated on the stack.
617 void shprocvar(shinstance
*psh
);
625 shprocvar(shinstance
*psh
)
628 struct var
*vp
, **prev
;
630 for (vpp
= psh
->vartab
; vpp
< psh
->vartab
+ VTABSIZE
; vpp
++) {
631 for (prev
= vpp
; (vp
= *prev
) != NULL
; ) {
632 if ((vp
->flags
& VEXPORT
) == 0) {
634 if ((vp
->flags
& VTEXTFIXED
) == 0)
635 ckfree(psh
, vp
->text
);
636 if ((vp
->flags
& (VSTRFIXED
| VSTRFIXED2
)) == 0)
639 if (vp
->flags
& VSTACK
) {
640 vp
->text
= savestr(psh
, vp
->text
);
641 vp
->flags
&=~ VSTACK
;
653 * Command to list all variables which are set. Currently this command
654 * is invoked from the set command when the set command is called without
659 print_quoted(shinstance
*psh
, const char *p
)
663 if (strcspn(p
, "|&;<>()$`\\\"' \t\n*?[]#~=%") == strlen(p
)) {
664 out1fmt(psh
, "%s", p
);
675 out1fmt(psh
, "'%s'", p
);
678 out1fmt(psh
, "'%.*s'", (int)(q
- p
), p
);
684 sort_var(const void *v_v1
, const void *v_v2
)
686 const struct var
* const *v1
= v_v1
;
687 const struct var
* const *v2
= v_v2
;
689 /* XXX Will anyone notice we include the '=' of the shorter name? */
690 return strcoll((*v1
)->text
, (*v2
)->text
);
694 * POSIX requires that 'set' (but not export or readonly) output the
695 * variables in lexicographic order - by the locale's collating order (sigh).
696 * Maybe we could keep them in an ordered balanced binary tree
697 * instead of hashed lists.
698 * For now just roll 'em through qsort for printing...
702 showvars(shinstance
*psh
, const char *name
, int flag
, int show_value
)
708 static struct var
**list
; /* static in case we are interrupted */
714 list
= ckmalloc(psh
, list_len
* sizeof(*list
));
717 for (vpp
= psh
->vartab
; vpp
< psh
->vartab
+ VTABSIZE
; vpp
++) {
718 for (vp
= *vpp
; vp
; vp
= vp
->next
) {
719 if (flag
&& !(vp
->flags
& flag
))
721 if (vp
->flags
& VUNSET
&& !(show_value
& 2))
723 if (count
>= list_len
) {
724 list
= ckrealloc(psh
, list
,
725 (list_len
<< 1) * sizeof(*list
));
732 qsort(list
, count
, sizeof(*list
), sort_var
);
734 for (vpp
= list
; count
--; vpp
++) {
737 out1fmt(psh
, "%s ", name
);
738 for (p
= vp
->text
; *p
!= '=' ; p
++)
740 if (!(vp
->flags
& VUNSET
) && show_value
) {
742 print_quoted(psh
, ++p
);
752 * The export and readonly commands.
756 exportcmd(shinstance
*psh
, int argc
, char **argv
)
761 int flag
= argv
[0][0] == 'r'? VREADONLY
: VEXPORT
;
764 pflag
= nextopt(psh
, "p") == 'p' ? 3 : 0;
765 if (argc
<= 1 || pflag
) {
766 showvars(psh
, pflag
? argv
[0] : 0, flag
, pflag
);
770 while ((name
= *psh
->argptr
++) != NULL
) {
771 if ((p
= strchr(name
, '=')) != NULL
) {
774 vp
= find_var(psh
, name
, NULL
, NULL
);
780 setvar(psh
, name
, p
, flag
);
787 * The "local" command.
791 localcmd(shinstance
*psh
, int argc
, char **argv
)
795 if (! in_function(psh
))
796 error(psh
, "Not in a function");
797 while ((name
= *psh
->argptr
++) != NULL
) {
798 mklocal(psh
, name
, 0);
805 * Make a variable a local variable. When a variable is made local, it's
806 * value and flags are saved in a localvar structure. The saved values
807 * will be restored when the shell function returns. We handle the name
808 * "-" as a special case.
812 mklocal(shinstance
*psh
, const char *name
, int flags
)
814 struct localvar
*lvp
;
819 lvp
= ckmalloc(psh
, sizeof (struct localvar
));
820 if (name
[0] == '-' && name
[1] == '\0') {
822 p
= ckmalloc(psh
, sizeof_optlist
);
823 lvp
->text
= memcpy(p
, psh
->optlist
, sizeof_optlist
);
826 vp
= find_var(psh
, name
, &vpp
, NULL
);
828 if (strchr(name
, '='))
829 setvareq(psh
, savestr(psh
, name
), VSTRFIXED
|flags
);
831 setvar(psh
, name
, NULL
, VSTRFIXED
|flags
);
832 vp
= *vpp
; /* the new variable */
836 lvp
->text
= vp
->text
;
837 lvp
->flags
= vp
->flags
;
838 vp
->flags
|= VSTRFIXED
|VTEXTFIXED
;
839 if (name
[vp
->name_len
] == '=')
840 setvareq(psh
, savestr(psh
, name
), flags
);
844 lvp
->next
= psh
->localvars
;
845 psh
->localvars
= lvp
;
851 * Called after a function returns.
855 poplocalvars(shinstance
*psh
)
857 struct localvar
*lvp
;
860 while ((lvp
= psh
->localvars
) != NULL
) {
861 psh
->localvars
= lvp
->next
;
863 TRACE((psh
, "poplocalvar %s", vp
? vp
->text
: "-"));
864 if (vp
== NULL
) { /* $- saved */
865 memcpy(psh
->optlist
, lvp
->text
, sizeof_optlist
);
866 ckfree(psh
, lvp
->text
);
867 } else if ((lvp
->flags
& (VUNSET
|VSTRFIXED
)) == VUNSET
) {
868 (void)unsetvar(psh
, vp
->text
, 0);
870 if (vp
->func
&& (vp
->flags
& VNOFUNC
) == 0)
871 (*vp
->func
)(psh
, lvp
->text
+ vp
->name_len
+ 1);
872 if ((vp
->flags
& VTEXTFIXED
) == 0)
873 ckfree(psh
, vp
->text
);
874 vp
->flags
= lvp
->flags
;
875 vp
->text
= lvp
->text
;
883 setvarcmd(shinstance
*psh
, int argc
, char **argv
)
886 return unsetcmd(psh
, argc
, argv
);
888 setvar(psh
, argv
[1], argv
[2], 0);
890 error(psh
, "List assignment not implemented");
896 * The unset builtin command. We unset the function before we unset the
897 * variable to allow a function to be unset when there is a readonly variable
898 * with the same name.
902 unsetcmd(shinstance
*psh
, int argc
, char **argv
)
910 while ((i
= nextopt(psh
, "evf")) != '\0') {
916 if (flg_func
== 0 && flg_var
== 0)
919 for (ap
= psh
->argptr
; *ap
; ap
++) {
921 ret
|= unsetfunc(psh
, *ap
);
923 ret
|= unsetvar(psh
, *ap
, flg_var
== 'e');
930 * Unset the specified variable.
934 unsetvar(shinstance
*psh
, const char *s
, int unexport
)
939 vp
= find_var(psh
, s
, &vpp
, NULL
);
943 if (vp
->flags
& VREADONLY
)
948 vp
->flags
&= ~VEXPORT
;
950 if (vp
->text
[vp
->name_len
+ 1] != '\0')
951 setvar(psh
, s
, nullstr
, 0);
952 vp
->flags
&= ~VEXPORT
;
954 if ((vp
->flags
& (VSTRFIXED
| VSTRFIXED2
)) == 0) {
955 if ((vp
->flags
& VTEXTFIXED
) == 0)
956 ckfree(psh
, vp
->text
);
967 * Returns true if the two strings specify the same varable. The first
968 * variable name is terminated by '='; the second may be terminated by
969 * either '=' or '\0'.
973 strequal(const char *p
, const char *q
)
979 if (*p
== '=' && *(q
- 1) == '\0')
985 * Search for a variable.
986 * 'name' may be terminated by '=' or a NUL.
987 * vppp is set to the pointer to vp, or the list head if vp isn't found
988 * lenp is set to the number of charactets in 'name'
992 find_var(shinstance
*psh
, const char *name
, struct var
***vppp
, int *lenp
)
994 unsigned int hashval
;
996 struct var
*vp
, **vpp
;
997 const char *p
= name
;
1000 while (*p
&& *p
!= '=')
1001 hashval
= 2 * hashval
+ (unsigned char)*p
++;
1002 len
= (int)(p
- name
);
1006 vpp
= &psh
->vartab
[hashval
% VTABSIZE
];
1010 for (vp
= *vpp
; vp
; vpp
= &vp
->next
, vp
= *vpp
) {
1011 if (vp
->name_len
!= len
)
1013 if (memcmp(vp
->text
, name
, len
) != 0)