]>
git.saurik.com Git - apple/shell_cmds.git/blob - sh/var.c
2 * SPDX-License-Identifier: BSD-3-Clause
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";
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD: head/bin/sh/var.c 329221 2018-02-13 16:48:57Z bdrewery $");
57 #include "nodes.h" /* for other headers */
58 #include "eval.h" /* defines cmdenviron */
70 #include "myhistedit.h"
83 void (*func
)(const char *);
98 static struct var voptind
;
101 struct localvar
*localvars
;
104 static const struct varinit varinit
[] = {
106 { &vhistsize
, VUNSET
, "HISTSIZE=",
109 { &vifs
, 0, "IFS= \t\n",
111 { &vmail
, VUNSET
, "MAIL=",
113 { &vmpath
, VUNSET
, "MAILPATH=",
115 { &vpath
, 0, "PATH=" _PATH_DEFPATH
,
118 * vps1 depends on uid
120 { &vps2
, 0, "PS2=> ",
122 { &vps4
, 0, "PS4=+ ",
125 { &vterm
, VUNSET
, "TERM=",
128 { &voptind
, 0, "OPTIND=1",
130 { &vdisvfork
, VUNSET
, "SH_DISABLE_VFORK=",
136 static struct var
*vartab
[VTABSIZE
];
138 static const char *const locale_names
[7] = {
139 "LC_COLLATE", "LC_CTYPE", "LC_MONETARY",
140 "LC_NUMERIC", "LC_TIME", "LC_MESSAGES", NULL
142 static const int locale_categories
[7] = {
143 LC_COLLATE
, LC_CTYPE
, LC_MONETARY
, LC_NUMERIC
, LC_TIME
, LC_MESSAGES
, 0
146 static int varequal(const char *, const char *);
147 static struct var
*find_var(const char *, struct var
***, int *);
148 static int localevar(const char *);
149 static void setvareq_const(const char *s
, int flags
);
151 extern char **environ
;
154 * This routine initializes the builtin variables and imports the environment.
155 * It is called when the shell is initialized.
162 const struct varinit
*ip
;
167 for (ip
= varinit
; (vp
= ip
->var
) != NULL
; ip
++) {
168 if (find_var(ip
->text
, &vpp
, &vp
->name_len
) != NULL
)
172 vp
->text
= __DECONST(char *, ip
->text
);
173 vp
->flags
= ip
->flags
| VSTRFIXED
| VTEXTFIXED
;
179 if (find_var("PS1", &vpp
, &vps1
.name_len
) == NULL
) {
182 vps1
.text
= __DECONST(char *, geteuid() ? "PS1=$ " : "PS1=# ");
183 vps1
.flags
= VSTRFIXED
|VTEXTFIXED
;
185 fmtstr(ppid
, sizeof(ppid
), "%d", (int)getppid());
186 setvarsafe("PPID", ppid
, 0);
187 for (envp
= environ
; *envp
; envp
++) {
188 if (strchr(*envp
, '=')) {
189 setvareq(*envp
, VEXPORT
|VTEXTFIXED
);
192 setvareq_const("OPTIND=1", 0);
193 setvareq_const("IFS= \t\n", 0);
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
*const savehandler
= handler
;
209 if (setjmp(jmploc
.loc
))
213 setvar(name
, val
, flags
);
215 handler
= savehandler
;
221 * Set the value of a variable. The flags argument is stored with the
222 * flags of the variable. If val is NULL, the variable is unset.
226 setvar(const char *name
, const char *val
, int flags
)
241 if (! is_in_name(*p
)) {
242 if (*p
== '\0' || *p
== '=')
250 error("%.*s: bad variable name", (int)namelen
, name
);
251 len
= namelen
+ 2; /* 2 is space for '=' and '\0' */
256 vallen
= strlen(val
);
260 nameeq
= ckmalloc(len
);
261 memcpy(nameeq
, name
, namelen
);
262 nameeq
[namelen
] = '=';
264 memcpy(nameeq
+ namelen
+ 1, val
, vallen
+ 1);
266 nameeq
[namelen
+ 1] = '\0';
267 setvareq(nameeq
, flags
);
272 localevar(const char *s
)
274 const char *const *ss
;
278 if (varequal(s
+ 1, "ANG"))
280 if (strncmp(s
+ 1, "C_", 2) != 0)
282 if (varequal(s
+ 3, "ALL"))
284 for (ss
= locale_names
; *ss
; ss
++)
285 if (varequal(s
+ 3, *ss
+ 3))
292 * Sets/unsets an environment variable from a pointer that may actually be a
293 * pointer into environ where the string should not be manipulated.
296 change_env(const char *s
, int set
)
303 if ((eqp
= strchr(ss
, '=')) != NULL
)
305 if (set
&& eqp
!= NULL
)
306 (void) setenv(ss
, eqp
+ 1, 1);
317 * Same as setvar except that the variable and value are passed in
318 * the first argument as name=value. Since the first argument will
319 * be actually stored in the table, it should not be a string that
324 setvareq(char *s
, int flags
)
326 struct var
*vp
, **vpp
;
331 if (forcelocal
&& !(flags
& (VNOSET
| VNOLOCAL
)))
333 vp
= find_var(s
, &vpp
, &nlen
);
335 if (vp
->flags
& VREADONLY
) {
336 if ((flags
& (VTEXTFIXED
|VSTACK
)) == 0)
338 error("%.*s: is read only", vp
->name_len
, vp
->text
);
340 if (flags
& VNOSET
) {
341 if ((flags
& (VTEXTFIXED
|VSTACK
)) == 0)
347 if (vp
->func
&& (flags
& VNOFUNC
) == 0)
348 (*vp
->func
)(s
+ vp
->name_len
+ 1);
350 if ((vp
->flags
& (VTEXTFIXED
|VSTACK
)) == 0)
353 vp
->flags
&= ~(VTEXTFIXED
|VSTACK
|VUNSET
);
358 * We could roll this to a function, to handle it as
359 * a regular variable function callback, but why bother?
361 * Note: this assumes iflag is not set to 1 initially.
362 * As part of initvar(), this is called before arguments
365 if ((vp
== &vmpath
|| (vp
== &vmail
&& ! mpathset())) &&
368 if ((vp
->flags
& VEXPORT
) && localevar(s
)) {
370 (void) setlocale(LC_ALL
, "");
377 if (flags
& VNOSET
) {
378 if ((flags
& (VTEXTFIXED
|VSTACK
)) == 0)
383 vp
= ckmalloc(sizeof (*vp
));
390 if ((vp
->flags
& VEXPORT
) && localevar(s
)) {
392 (void) setlocale(LC_ALL
, "");
400 setvareq_const(const char *s
, int flags
)
402 setvareq(__DECONST(char *, s
), flags
| VTEXTFIXED
);
407 * Process a linked list of variable assignments.
411 listsetvar(struct arglist
*list
, int flags
)
416 for (i
= 0; i
< list
->count
; i
++)
417 setvareq(savestr(list
->args
[i
]), flags
);
424 * Find the value of a variable. Returns NULL if not set.
428 lookupvar(const char *name
)
432 v
= find_var(name
, NULL
, NULL
);
433 if (v
== NULL
|| v
->flags
& VUNSET
)
435 return v
->text
+ v
->name_len
+ 1;
441 * Search the environment of a builtin command. If the second argument
442 * is nonzero, return the value of a variable even if it hasn't been
447 bltinlookup(const char *name
, int doall
)
454 if (cmdenviron
) for (i
= 0; i
< cmdenviron
->count
; i
++) {
455 if (varequal(cmdenviron
->args
[i
], name
))
456 result
= strchr(cmdenviron
->args
[i
], '=') + 1;
461 v
= find_var(name
, NULL
, NULL
);
462 if (v
== NULL
|| v
->flags
& VUNSET
||
463 (!doall
&& (v
->flags
& VEXPORT
) == 0))
465 return v
->text
+ v
->name_len
+ 1;
470 * Set up locale for a builtin (LANG/LC_* assignments).
479 if (cmdenviron
) for (i
= 0; i
< cmdenviron
->count
; i
++) {
480 if (localevar(cmdenviron
->args
[i
])) {
487 loc
= bltinlookup("LC_ALL", 0);
490 setlocale(LC_ALL
, loc
);
495 locdef
= bltinlookup("LANG", 0);
496 for (i
= 0; locale_names
[i
] != NULL
; i
++) {
497 loc
= bltinlookup(locale_names
[i
], 0);
501 setlocale(locale_categories
[i
], loc
);
508 * Undo the effect of bltinlocaleset().
511 bltinunsetlocale(void)
516 if (cmdenviron
) for (i
= 0; i
< cmdenviron
->count
; i
++) {
517 if (localevar(cmdenviron
->args
[i
])) {
518 setlocale(LC_ALL
, "");
527 * Update the localeisutf8 flag.
534 charset
= nl_langinfo(CODESET
);
535 localeisutf8
= !strcmp(charset
, "UTF-8");
542 initial_localeisutf8
= localeisutf8
;
546 * Generate a list of exported variables. This routine is used to construct
547 * the third argument to execve when executing a program.
559 for (vpp
= vartab
; vpp
< vartab
+ VTABSIZE
; vpp
++) {
560 for (vp
= *vpp
; vp
; vp
= vp
->next
)
561 if (vp
->flags
& VEXPORT
)
564 ep
= env
= stalloc((nenv
+ 1) * sizeof *env
);
565 for (vpp
= vartab
; vpp
< vartab
+ VTABSIZE
; vpp
++) {
566 for (vp
= *vpp
; vp
; vp
= vp
->next
)
567 if (vp
->flags
& VEXPORT
)
576 var_compare(const void *a
, const void *b
)
578 const char *const *sa
, *const *sb
;
583 * This compares two var=value strings which creates a different
584 * order from what you would probably expect. POSIX is somewhat
585 * ambiguous on what should be sorted exactly.
587 return strcoll(*sa
, *sb
);
592 * Command to list all variables which are set. This is invoked from the
593 * set command when it is called without any options or operands.
597 showvarscmd(int argc __unused
, char **argv __unused
)
606 * POSIX requires us to sort the variables.
609 for (vpp
= vartab
; vpp
< vartab
+ VTABSIZE
; vpp
++) {
610 for (vp
= *vpp
; vp
; vp
= vp
->next
) {
611 if (!(vp
->flags
& VUNSET
))
617 vars
= ckmalloc(n
* sizeof(*vars
));
619 for (vpp
= vartab
; vpp
< vartab
+ VTABSIZE
; vpp
++) {
620 for (vp
= *vpp
; vp
; vp
= vp
->next
) {
621 if (!(vp
->flags
& VUNSET
))
622 vars
[i
++] = vp
->text
;
626 qsort(vars
, n
, sizeof(*vars
), var_compare
);
627 for (i
= 0; i
< n
; i
++) {
629 * Skip improper variable names so the output remains usable as
632 if (!isassignment(vars
[i
]))
634 s
= strchr(vars
[i
], '=');
636 outbin(vars
[i
], s
- vars
[i
], out1
);
649 * The export and readonly commands.
653 exportcmd(int argc __unused
, char **argv
)
662 int flag
= argv
[0][0] == 'r'? VREADONLY
: VEXPORT
;
666 while ((ch
= nextopt("p")) != '\0') {
674 if (values
&& *argptr
!= NULL
)
675 error("-p requires no arguments");
676 if (*argptr
!= NULL
) {
677 for (ap
= argptr
; (name
= *ap
) != NULL
; ap
++) {
678 if ((p
= strchr(name
, '=')) != NULL
) {
681 vp
= find_var(name
, NULL
, NULL
);
684 if ((vp
->flags
& VEXPORT
) && localevar(vp
->text
)) {
685 change_env(vp
->text
, 1);
686 (void) setlocale(LC_ALL
, "");
692 setvar(name
, p
, flag
);
695 for (vpp
= vartab
; vpp
< vartab
+ VTABSIZE
; vpp
++) {
696 for (vp
= *vpp
; vp
; vp
= vp
->next
) {
697 if (vp
->flags
& flag
) {
700 * Skip improper variable names
701 * so the output remains usable
704 if (!isassignment(vp
->text
))
709 if (values
&& !(vp
->flags
& VUNSET
)) {
711 vp
->name_len
+ 1, out1
);
715 outbin(vp
->text
, vp
->name_len
,
727 * The "local" command.
731 localcmd(int argc __unused
, char **argv __unused
)
737 error("Not in a function");
738 while ((name
= *argptr
++) != NULL
) {
746 * Make a variable a local variable. When a variable is made local, it's
747 * value and flags are saved in a localvar structure. The saved values
748 * will be restored when the shell function returns. We handle the name
749 * "-" as a special case.
755 struct localvar
*lvp
;
760 lvp
= ckmalloc(sizeof (struct localvar
));
761 if (name
[0] == '-' && name
[1] == '\0') {
762 lvp
->text
= ckmalloc(sizeof optval
);
763 memcpy(lvp
->text
, optval
, sizeof optval
);
766 vp
= find_var(name
, &vpp
, NULL
);
768 if (strchr(name
, '='))
769 setvareq(savestr(name
), VSTRFIXED
| VNOLOCAL
);
771 setvar(name
, NULL
, VSTRFIXED
| VNOLOCAL
);
772 vp
= *vpp
; /* the new variable */
776 lvp
->text
= vp
->text
;
777 lvp
->flags
= vp
->flags
;
778 vp
->flags
|= VSTRFIXED
|VTEXTFIXED
;
779 if (name
[vp
->name_len
] == '=')
780 setvareq(savestr(name
), VNOLOCAL
);
784 lvp
->next
= localvars
;
791 * Called after a function returns.
797 struct localvar
*lvp
;
802 while ((lvp
= localvars
) != NULL
) {
803 localvars
= lvp
->next
;
805 if (vp
== NULL
) { /* $- saved */
806 memcpy(optval
, lvp
->text
, sizeof optval
);
809 } else if ((lvp
->flags
& (VUNSET
|VSTRFIXED
)) == VUNSET
) {
810 vp
->flags
&= ~VREADONLY
;
811 (void)unsetvar(vp
->text
);
813 islocalevar
= (vp
->flags
| lvp
->flags
) & VEXPORT
&&
814 localevar(lvp
->text
);
815 if ((vp
->flags
& VTEXTFIXED
) == 0)
817 vp
->flags
= lvp
->flags
;
818 vp
->text
= lvp
->text
;
820 (*vp
->func
)(vp
->text
+ vp
->name_len
+ 1);
822 change_env(vp
->text
, vp
->flags
& VEXPORT
&&
823 (vp
->flags
& VUNSET
) == 0);
824 setlocale(LC_ALL
, "");
835 setvarcmd(int argc
, char **argv
)
838 return unsetcmd(argc
, argv
);
840 setvar(argv
[1], argv
[2], 0);
842 error("too many arguments");
848 * The unset builtin command.
852 unsetcmd(int argc __unused
, char **argv __unused
)
860 while ((i
= nextopt("vf")) != '\0') {
866 if (flg_func
== 0 && flg_var
== 0)
870 for (ap
= argptr
; *ap
; ap
++) {
872 ret
|= unsetfunc(*ap
);
874 ret
|= unsetvar(*ap
);
882 * Unset the specified variable.
883 * Called with interrupts off.
887 unsetvar(const char *s
)
892 vp
= find_var(s
, &vpp
, NULL
);
895 if (vp
->flags
& VREADONLY
)
897 if (vp
->text
[vp
->name_len
+ 1] != '\0')
899 if ((vp
->flags
& VEXPORT
) && localevar(vp
->text
)) {
901 setlocale(LC_ALL
, "");
904 vp
->flags
&= ~VEXPORT
;
906 if ((vp
->flags
& VSTRFIXED
) == 0) {
907 if ((vp
->flags
& VTEXTFIXED
) == 0)
918 * Returns true if the two strings specify the same variable. The first
919 * variable name is terminated by '='; the second may be terminated by
920 * either '=' or '\0'.
924 varequal(const char *p
, const char *q
)
930 if (*p
== '=' && *(q
- 1) == '\0')
936 * Search for a variable.
937 * 'name' may be terminated by '=' or a NUL.
938 * vppp is set to the pointer to vp, or the list head if vp isn't found
939 * lenp is set to the number of characters in 'name'
943 find_var(const char *name
, struct var
***vppp
, int *lenp
)
945 unsigned int hashval
;
947 struct var
*vp
, **vpp
;
948 const char *p
= name
;
951 while (*p
&& *p
!= '=')
952 hashval
= 2 * hashval
+ (unsigned char)*p
++;
957 vpp
= &vartab
[hashval
% VTABSIZE
];
961 for (vp
= *vpp
; vp
; vpp
= &vp
->next
, vp
= *vpp
) {
962 if (vp
->name_len
!= len
)
964 if (memcmp(vp
->text
, name
, len
) != 0)