14b88c807SRodney W. Grimes /*-
28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni *
44b88c807SRodney W. Grimes * Copyright (c) 1991, 1993
54b88c807SRodney W. Grimes * The Regents of the University of California. All rights reserved.
64b88c807SRodney W. Grimes *
74b88c807SRodney W. Grimes * This code is derived from software contributed to Berkeley by
84b88c807SRodney W. Grimes * Kenneth Almquist.
94b88c807SRodney W. Grimes *
104b88c807SRodney W. Grimes * Redistribution and use in source and binary forms, with or without
114b88c807SRodney W. Grimes * modification, are permitted provided that the following conditions
124b88c807SRodney W. Grimes * are met:
134b88c807SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright
144b88c807SRodney W. Grimes * notice, this list of conditions and the following disclaimer.
154b88c807SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright
164b88c807SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the
174b88c807SRodney W. Grimes * documentation and/or other materials provided with the distribution.
18fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors
194b88c807SRodney W. Grimes * may be used to endorse or promote products derived from this software
204b88c807SRodney W. Grimes * without specific prior written permission.
214b88c807SRodney W. Grimes *
224b88c807SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
234b88c807SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
244b88c807SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
254b88c807SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
264b88c807SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
274b88c807SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
284b88c807SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
294b88c807SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
304b88c807SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
314b88c807SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
324b88c807SRodney W. Grimes * SUCH DAMAGE.
334b88c807SRodney W. Grimes */
344b88c807SRodney W. Grimes
35aa9caaf6SPeter Wemm #include <unistd.h>
36aa9caaf6SPeter Wemm #include <stdlib.h>
378d5c19ffSDavid E. O'Brien #include <paths.h>
38aa9caaf6SPeter Wemm
394b88c807SRodney W. Grimes /*
404b88c807SRodney W. Grimes * Shell variables.
414b88c807SRodney W. Grimes */
424b88c807SRodney W. Grimes
43ba726b8aSAndrey A. Chernov #include <locale.h>
446ed74a0aSJilles Tjoelker #include <langinfo.h>
45ba726b8aSAndrey A. Chernov
464b88c807SRodney W. Grimes #include "shell.h"
474b88c807SRodney W. Grimes #include "output.h"
484b88c807SRodney W. Grimes #include "expand.h"
494b88c807SRodney W. Grimes #include "nodes.h" /* for other headers */
504b88c807SRodney W. Grimes #include "eval.h" /* defines cmdenviron */
514b88c807SRodney W. Grimes #include "exec.h"
524b88c807SRodney W. Grimes #include "syntax.h"
534b88c807SRodney W. Grimes #include "options.h"
544b88c807SRodney W. Grimes #include "mail.h"
554b88c807SRodney W. Grimes #include "var.h"
564b88c807SRodney W. Grimes #include "memalloc.h"
574b88c807SRodney W. Grimes #include "error.h"
584b88c807SRodney W. Grimes #include "mystring.h"
59ab0a2172SSteve Price #include "parser.h"
60454a02b3SJilles Tjoelker #include "builtins.h"
61aa9caaf6SPeter Wemm #ifndef NO_HISTORY
62aa9caaf6SPeter Wemm #include "myhistedit.h"
63aa9caaf6SPeter Wemm #endif
644b88c807SRodney W. Grimes
654b88c807SRodney W. Grimes
663cdd74bbSBryan Drewery #ifndef VTABSIZE
674b88c807SRodney W. Grimes #define VTABSIZE 39
683cdd74bbSBryan Drewery #endif
694b88c807SRodney W. Grimes
704b88c807SRodney W. Grimes
714b88c807SRodney W. Grimes struct varinit {
724b88c807SRodney W. Grimes struct var *var;
734b88c807SRodney W. Grimes int flags;
74c6c5dd37SJilles Tjoelker const char *text;
755134c3f7SWarner Losh void (*func)(const char *);
764b88c807SRodney W. Grimes };
774b88c807SRodney W. Grimes
784b88c807SRodney W. Grimes
79aa9caaf6SPeter Wemm #ifndef NO_HISTORY
804b88c807SRodney W. Grimes struct var vhistsize;
81580eefdfSJilles Tjoelker struct var vterm;
82aa9caaf6SPeter Wemm #endif
834b88c807SRodney W. Grimes struct var vifs;
844b88c807SRodney W. Grimes struct var vmail;
854b88c807SRodney W. Grimes struct var vmpath;
864b88c807SRodney W. Grimes struct var vpath;
874b88c807SRodney W. Grimes struct var vps1;
884b88c807SRodney W. Grimes struct var vps2;
89120c8e6cSStefan Farfeleder struct var vps4;
90aa7b6f82SDavid E. O'Brien static struct var voptind;
91caf29fabSJilles Tjoelker struct var vdisvfork;
924b88c807SRodney W. Grimes
9358bdb076SJilles Tjoelker struct localvar *localvars;
94c543e1aeSJilles Tjoelker int forcelocal;
95c543e1aeSJilles Tjoelker
96aa7b6f82SDavid E. O'Brien static const struct varinit varinit[] = {
97aa9caaf6SPeter Wemm #ifndef NO_HISTORY
98c6c5dd37SJilles Tjoelker { &vhistsize, VUNSET, "HISTSIZE=",
99ab0a2172SSteve Price sethistsize },
100aa9caaf6SPeter Wemm #endif
101c6c5dd37SJilles Tjoelker { &vifs, 0, "IFS= \t\n",
102ab0a2172SSteve Price NULL },
103c6c5dd37SJilles Tjoelker { &vmail, VUNSET, "MAIL=",
104ab0a2172SSteve Price NULL },
105c6c5dd37SJilles Tjoelker { &vmpath, VUNSET, "MAILPATH=",
106ab0a2172SSteve Price NULL },
107c6c5dd37SJilles Tjoelker { &vpath, 0, "PATH=" _PATH_DEFPATH,
108ab0a2172SSteve Price changepath },
1094b88c807SRodney W. Grimes /*
1104b88c807SRodney W. Grimes * vps1 depends on uid
1114b88c807SRodney W. Grimes */
112c6c5dd37SJilles Tjoelker { &vps2, 0, "PS2=> ",
113ab0a2172SSteve Price NULL },
114c6c5dd37SJilles Tjoelker { &vps4, 0, "PS4=+ ",
115120c8e6cSStefan Farfeleder NULL },
116580eefdfSJilles Tjoelker #ifndef NO_HISTORY
117580eefdfSJilles Tjoelker { &vterm, VUNSET, "TERM=",
118580eefdfSJilles Tjoelker setterm },
119580eefdfSJilles Tjoelker #endif
120c6c5dd37SJilles Tjoelker { &voptind, 0, "OPTIND=1",
121ab0a2172SSteve Price getoptsreset },
122caf29fabSJilles Tjoelker { &vdisvfork, VUNSET, "SH_DISABLE_VFORK=",
123caf29fabSJilles Tjoelker NULL },
124ab0a2172SSteve Price { NULL, 0, NULL,
125ab0a2172SSteve Price NULL }
1264b88c807SRodney W. Grimes };
1274b88c807SRodney W. Grimes
128aa7b6f82SDavid E. O'Brien static struct var *vartab[VTABSIZE];
1294b88c807SRodney W. Grimes
130aa7b6f82SDavid E. O'Brien static const char *const locale_names[7] = {
131e4b50334SJilles Tjoelker "LC_COLLATE", "LC_CTYPE", "LC_MONETARY",
132e4b50334SJilles Tjoelker "LC_NUMERIC", "LC_TIME", "LC_MESSAGES", NULL
133e4b50334SJilles Tjoelker };
134aa7b6f82SDavid E. O'Brien static const int locale_categories[7] = {
135e4b50334SJilles Tjoelker LC_COLLATE, LC_CTYPE, LC_MONETARY, LC_NUMERIC, LC_TIME, LC_MESSAGES, 0
136e4b50334SJilles Tjoelker };
137e4b50334SJilles Tjoelker
13888328642SDavid E. O'Brien static int varequal(const char *, const char *);
1393a99ed46SJilles Tjoelker static struct var *find_var(const char *, struct var ***, int *);
14088328642SDavid E. O'Brien static int localevar(const char *);
14122afca9bSJilles Tjoelker static void setvareq_const(const char *s, int flags);
1424b88c807SRodney W. Grimes
14359e0cc8eSJilles Tjoelker extern char **environ;
1444b88c807SRodney W. Grimes
1454b88c807SRodney W. Grimes /*
14659e0cc8eSJilles Tjoelker * This routine initializes the builtin variables and imports the environment.
14759e0cc8eSJilles Tjoelker * It is called when the shell is initialized.
1484b88c807SRodney W. Grimes */
1494b88c807SRodney W. Grimes
1504b88c807SRodney W. Grimes void
initvar(void)1515134c3f7SWarner Losh initvar(void)
1525134c3f7SWarner Losh {
15339dccc6fSTim J. Robbins char ppid[20];
1544b88c807SRodney W. Grimes const struct varinit *ip;
1554b88c807SRodney W. Grimes struct var *vp;
1564b88c807SRodney W. Grimes struct var **vpp;
15759e0cc8eSJilles Tjoelker char **envp;
1584b88c807SRodney W. Grimes
1594b88c807SRodney W. Grimes for (ip = varinit ; (vp = ip->var) != NULL ; ip++) {
1603a99ed46SJilles Tjoelker if (find_var(ip->text, &vpp, &vp->name_len) != NULL)
1613a99ed46SJilles Tjoelker continue;
1624b88c807SRodney W. Grimes vp->next = *vpp;
1634b88c807SRodney W. Grimes *vpp = vp;
164c6c5dd37SJilles Tjoelker vp->text = __DECONST(char *, ip->text);
165c6c5dd37SJilles Tjoelker vp->flags = ip->flags | VSTRFIXED | VTEXTFIXED;
166ab0a2172SSteve Price vp->func = ip->func;
1674b88c807SRodney W. Grimes }
1684b88c807SRodney W. Grimes /*
1694b88c807SRodney W. Grimes * PS1 depends on uid
1704b88c807SRodney W. Grimes */
1713a99ed46SJilles Tjoelker if (find_var("PS1", &vpp, &vps1.name_len) == NULL) {
1724b88c807SRodney W. Grimes vps1.next = *vpp;
1734b88c807SRodney W. Grimes *vpp = &vps1;
174c6c5dd37SJilles Tjoelker vps1.text = __DECONST(char *, geteuid() ? "PS1=$ " : "PS1=# ");
1754b88c807SRodney W. Grimes vps1.flags = VSTRFIXED|VTEXTFIXED;
1764b88c807SRodney W. Grimes }
17739dccc6fSTim J. Robbins fmtstr(ppid, sizeof(ppid), "%d", (int)getppid());
17839dccc6fSTim J. Robbins setvarsafe("PPID", ppid, 0);
17959e0cc8eSJilles Tjoelker for (envp = environ ; *envp ; envp++) {
18059e0cc8eSJilles Tjoelker if (strchr(*envp, '=')) {
18159e0cc8eSJilles Tjoelker setvareq(*envp, VEXPORT|VTEXTFIXED);
18259e0cc8eSJilles Tjoelker }
18359e0cc8eSJilles Tjoelker }
18422afca9bSJilles Tjoelker setvareq_const("OPTIND=1", 0);
1857cca93e6SJilles Tjoelker setvareq_const("IFS= \t\n", 0);
1864b88c807SRodney W. Grimes }
1874b88c807SRodney W. Grimes
1884b88c807SRodney W. Grimes /*
189ab0a2172SSteve Price * Safe version of setvar, returns 1 on success 0 on failure.
190ab0a2172SSteve Price */
191ab0a2172SSteve Price
192ab0a2172SSteve Price int
setvarsafe(const char * name,const char * val,int flags)1932cac6e36SJilles Tjoelker setvarsafe(const char *name, const char *val, int flags)
194ab0a2172SSteve Price {
195ab0a2172SSteve Price struct jmploc jmploc;
196224fbf9fSJilles Tjoelker struct jmploc *const savehandler = handler;
197ab0a2172SSteve Price int err = 0;
1989922c6d2SJilles Tjoelker int inton;
199ab0a2172SSteve Price
2009922c6d2SJilles Tjoelker inton = is_int_on();
201ab0a2172SSteve Price if (setjmp(jmploc.loc))
202ab0a2172SSteve Price err = 1;
203ab0a2172SSteve Price else {
204ab0a2172SSteve Price handler = &jmploc;
205ab0a2172SSteve Price setvar(name, val, flags);
206ab0a2172SSteve Price }
207ab0a2172SSteve Price handler = savehandler;
2089922c6d2SJilles Tjoelker SETINTON(inton);
209ab0a2172SSteve Price return err;
210ab0a2172SSteve Price }
211ab0a2172SSteve Price
212ab0a2172SSteve Price /*
213b3decf89SJens Schweikhardt * Set the value of a variable. The flags argument is stored with the
2144b88c807SRodney W. Grimes * flags of the variable. If val is NULL, the variable is unset.
2154b88c807SRodney W. Grimes */
2164b88c807SRodney W. Grimes
2174b88c807SRodney W. Grimes void
setvar(const char * name,const char * val,int flags)2182cac6e36SJilles Tjoelker setvar(const char *name, const char *val, int flags)
2194b88c807SRodney W. Grimes {
2202cac6e36SJilles Tjoelker const char *p;
221670dd3f0SJilles Tjoelker size_t len;
222670dd3f0SJilles Tjoelker size_t namelen;
223670dd3f0SJilles Tjoelker size_t vallen;
2244b88c807SRodney W. Grimes char *nameeq;
2254b88c807SRodney W. Grimes int isbad;
2264b88c807SRodney W. Grimes
2274b88c807SRodney W. Grimes isbad = 0;
2284b88c807SRodney W. Grimes p = name;
229ba726b8aSAndrey A. Chernov if (! is_name(*p))
2304b88c807SRodney W. Grimes isbad = 1;
231ba726b8aSAndrey A. Chernov p++;
2324b88c807SRodney W. Grimes for (;;) {
2334b88c807SRodney W. Grimes if (! is_in_name(*p)) {
2344b88c807SRodney W. Grimes if (*p == '\0' || *p == '=')
2354b88c807SRodney W. Grimes break;
2364b88c807SRodney W. Grimes isbad = 1;
2374b88c807SRodney W. Grimes }
2384b88c807SRodney W. Grimes p++;
2394b88c807SRodney W. Grimes }
2404b88c807SRodney W. Grimes namelen = p - name;
2414b88c807SRodney W. Grimes if (isbad)
242670dd3f0SJilles Tjoelker error("%.*s: bad variable name", (int)namelen, name);
2434b88c807SRodney W. Grimes len = namelen + 2; /* 2 is space for '=' and '\0' */
2444b88c807SRodney W. Grimes if (val == NULL) {
2454b88c807SRodney W. Grimes flags |= VUNSET;
246670dd3f0SJilles Tjoelker vallen = 0;
2474b88c807SRodney W. Grimes } else {
248670dd3f0SJilles Tjoelker vallen = strlen(val);
249670dd3f0SJilles Tjoelker len += vallen;
2504b88c807SRodney W. Grimes }
2511632bf1aSJilles Tjoelker INTOFF;
2522cac6e36SJilles Tjoelker nameeq = ckmalloc(len);
2532cac6e36SJilles Tjoelker memcpy(nameeq, name, namelen);
2542cac6e36SJilles Tjoelker nameeq[namelen] = '=';
2554b88c807SRodney W. Grimes if (val)
256670dd3f0SJilles Tjoelker memcpy(nameeq + namelen + 1, val, vallen + 1);
2572cac6e36SJilles Tjoelker else
2582cac6e36SJilles Tjoelker nameeq[namelen + 1] = '\0';
2594b88c807SRodney W. Grimes setvareq(nameeq, flags);
2601632bf1aSJilles Tjoelker INTON;
2614b88c807SRodney W. Grimes }
2624b88c807SRodney W. Grimes
26388328642SDavid E. O'Brien static int
localevar(const char * s)2642cac6e36SJilles Tjoelker localevar(const char *s)
265ba726b8aSAndrey A. Chernov {
266e4b50334SJilles Tjoelker const char *const *ss;
2674b88c807SRodney W. Grimes
268ba726b8aSAndrey A. Chernov if (*s != 'L')
269ba726b8aSAndrey A. Chernov return 0;
270ba726b8aSAndrey A. Chernov if (varequal(s + 1, "ANG"))
271ba726b8aSAndrey A. Chernov return 1;
272ba726b8aSAndrey A. Chernov if (strncmp(s + 1, "C_", 2) != 0)
273ba726b8aSAndrey A. Chernov return 0;
274e4b50334SJilles Tjoelker if (varequal(s + 3, "ALL"))
275e4b50334SJilles Tjoelker return 1;
276e4b50334SJilles Tjoelker for (ss = locale_names; *ss ; ss++)
277e4b50334SJilles Tjoelker if (varequal(s + 3, *ss + 3))
278ba726b8aSAndrey A. Chernov return 1;
279ba726b8aSAndrey A. Chernov return 0;
280ba726b8aSAndrey A. Chernov }
2814b88c807SRodney W. Grimes
2825b78c6c2SSean Farley
2835b78c6c2SSean Farley /*
2845b78c6c2SSean Farley * Sets/unsets an environment variable from a pointer that may actually be a
2855b78c6c2SSean Farley * pointer into environ where the string should not be manipulated.
2865b78c6c2SSean Farley */
28788328642SDavid E. O'Brien static void
change_env(const char * s,int set)2882cac6e36SJilles Tjoelker change_env(const char *s, int set)
2895b78c6c2SSean Farley {
2905b78c6c2SSean Farley char *eqp;
2915b78c6c2SSean Farley char *ss;
2925b78c6c2SSean Farley
2931632bf1aSJilles Tjoelker INTOFF;
2945b78c6c2SSean Farley ss = savestr(s);
2955b78c6c2SSean Farley if ((eqp = strchr(ss, '=')) != NULL)
2965b78c6c2SSean Farley *eqp = '\0';
2975b78c6c2SSean Farley if (set && eqp != NULL)
2985b78c6c2SSean Farley (void) setenv(ss, eqp + 1, 1);
2995b78c6c2SSean Farley else
3005b78c6c2SSean Farley (void) unsetenv(ss);
3015b78c6c2SSean Farley ckfree(ss);
3021632bf1aSJilles Tjoelker INTON;
3035b78c6c2SSean Farley
3045b78c6c2SSean Farley return;
3055b78c6c2SSean Farley }
3065b78c6c2SSean Farley
3075b78c6c2SSean Farley
3084b88c807SRodney W. Grimes /*
3094b88c807SRodney W. Grimes * Same as setvar except that the variable and value are passed in
3104b88c807SRodney W. Grimes * the first argument as name=value. Since the first argument will
3114b88c807SRodney W. Grimes * be actually stored in the table, it should not be a string that
3124b88c807SRodney W. Grimes * will go away.
3134b88c807SRodney W. Grimes */
3144b88c807SRodney W. Grimes
3154b88c807SRodney W. Grimes void
setvareq(char * s,int flags)3165134c3f7SWarner Losh setvareq(char *s, int flags)
3174b88c807SRodney W. Grimes {
3184b88c807SRodney W. Grimes struct var *vp, **vpp;
3193a99ed46SJilles Tjoelker int nlen;
3204b88c807SRodney W. Grimes
321b8ec435eSMartin Cracauer if (aflag)
322b8ec435eSMartin Cracauer flags |= VEXPORT;
323c543e1aeSJilles Tjoelker if (forcelocal && !(flags & (VNOSET | VNOLOCAL)))
324c543e1aeSJilles Tjoelker mklocal(s);
3253a99ed46SJilles Tjoelker vp = find_var(s, &vpp, &nlen);
3263a99ed46SJilles Tjoelker if (vp != NULL) {
32789d4f883SJilles Tjoelker if (vp->flags & VREADONLY) {
32889d4f883SJilles Tjoelker if ((flags & (VTEXTFIXED|VSTACK)) == 0)
32989d4f883SJilles Tjoelker ckfree(s);
330d41b2be1SJilles Tjoelker error("%.*s: is read only", vp->name_len, vp->text);
33189d4f883SJilles Tjoelker }
332d73dba75SJilles Tjoelker if (flags & VNOSET) {
333d73dba75SJilles Tjoelker if ((flags & (VTEXTFIXED|VSTACK)) == 0)
334d73dba75SJilles Tjoelker ckfree(s);
335850460c0SJilles Tjoelker return;
336d73dba75SJilles Tjoelker }
3374b88c807SRodney W. Grimes INTOFF;
338ab0a2172SSteve Price
339ab0a2172SSteve Price if (vp->func && (flags & VNOFUNC) == 0)
3403a99ed46SJilles Tjoelker (*vp->func)(s + vp->name_len + 1);
341ab0a2172SSteve Price
3424b88c807SRodney W. Grimes if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0)
3434b88c807SRodney W. Grimes ckfree(vp->text);
344ab0a2172SSteve Price
3454b88c807SRodney W. Grimes vp->flags &= ~(VTEXTFIXED|VSTACK|VUNSET);
3464b88c807SRodney W. Grimes vp->flags |= flags;
3474b88c807SRodney W. Grimes vp->text = s;
348ab0a2172SSteve Price
349ab0a2172SSteve Price /*
350ab0a2172SSteve Price * We could roll this to a function, to handle it as
351ab0a2172SSteve Price * a regular variable function callback, but why bother?
35257063576SJilles Tjoelker *
35357063576SJilles Tjoelker * Note: this assumes iflag is not set to 1 initially.
35459e0cc8eSJilles Tjoelker * As part of initvar(), this is called before arguments
35557063576SJilles Tjoelker * are looked at.
356ab0a2172SSteve Price */
35757063576SJilles Tjoelker if ((vp == &vmpath || (vp == &vmail && ! mpathset())) &&
35857063576SJilles Tjoelker iflag == 1)
3594b88c807SRodney W. Grimes chkmail(1);
360ba726b8aSAndrey A. Chernov if ((vp->flags & VEXPORT) && localevar(s)) {
3615b78c6c2SSean Farley change_env(s, 1);
362ba726b8aSAndrey A. Chernov (void) setlocale(LC_ALL, "");
3636ed74a0aSJilles Tjoelker updatecharset();
364ba726b8aSAndrey A. Chernov }
3654b88c807SRodney W. Grimes INTON;
3664b88c807SRodney W. Grimes return;
3674b88c807SRodney W. Grimes }
3684b88c807SRodney W. Grimes /* not found */
369d73dba75SJilles Tjoelker if (flags & VNOSET) {
370d73dba75SJilles Tjoelker if ((flags & (VTEXTFIXED|VSTACK)) == 0)
371d73dba75SJilles Tjoelker ckfree(s);
372850460c0SJilles Tjoelker return;
373d73dba75SJilles Tjoelker }
3741632bf1aSJilles Tjoelker INTOFF;
3754b88c807SRodney W. Grimes vp = ckmalloc(sizeof (*vp));
3764b88c807SRodney W. Grimes vp->flags = flags;
3774b88c807SRodney W. Grimes vp->text = s;
3783a99ed46SJilles Tjoelker vp->name_len = nlen;
3794b88c807SRodney W. Grimes vp->next = *vpp;
380ab0a2172SSteve Price vp->func = NULL;
3814b88c807SRodney W. Grimes *vpp = vp;
382ba726b8aSAndrey A. Chernov if ((vp->flags & VEXPORT) && localevar(s)) {
3835b78c6c2SSean Farley change_env(s, 1);
384ba726b8aSAndrey A. Chernov (void) setlocale(LC_ALL, "");
3856ed74a0aSJilles Tjoelker updatecharset();
386ba726b8aSAndrey A. Chernov }
387ba726b8aSAndrey A. Chernov INTON;
3884b88c807SRodney W. Grimes }
3894b88c807SRodney W. Grimes
3904b88c807SRodney W. Grimes
39122afca9bSJilles Tjoelker static void
setvareq_const(const char * s,int flags)39222afca9bSJilles Tjoelker setvareq_const(const char *s, int flags)
39322afca9bSJilles Tjoelker {
39422afca9bSJilles Tjoelker setvareq(__DECONST(char *, s), flags | VTEXTFIXED);
39522afca9bSJilles Tjoelker }
39622afca9bSJilles Tjoelker
3974b88c807SRodney W. Grimes
3984b88c807SRodney W. Grimes /*
3994b88c807SRodney W. Grimes * Process a linked list of variable assignments.
4004b88c807SRodney W. Grimes */
4014b88c807SRodney W. Grimes
4024b88c807SRodney W. Grimes void
listsetvar(struct arglist * list,int flags)4038ef0ae8aSJilles Tjoelker listsetvar(struct arglist *list, int flags)
4044b88c807SRodney W. Grimes {
4058ef0ae8aSJilles Tjoelker int i;
4064b88c807SRodney W. Grimes
4074b88c807SRodney W. Grimes INTOFF;
4088ef0ae8aSJilles Tjoelker for (i = 0; i < list->count; i++)
4098ef0ae8aSJilles Tjoelker setvareq(savestr(list->args[i]), flags);
4104b88c807SRodney W. Grimes INTON;
4114b88c807SRodney W. Grimes }
4124b88c807SRodney W. Grimes
4134b88c807SRodney W. Grimes
4144b88c807SRodney W. Grimes
4154b88c807SRodney W. Grimes /*
4164b88c807SRodney W. Grimes * Find the value of a variable. Returns NULL if not set.
4174b88c807SRodney W. Grimes */
4184b88c807SRodney W. Grimes
4194b88c807SRodney W. Grimes char *
lookupvar(const char * name)4202cac6e36SJilles Tjoelker lookupvar(const char *name)
4214b88c807SRodney W. Grimes {
4224b88c807SRodney W. Grimes struct var *v;
4234b88c807SRodney W. Grimes
4243a99ed46SJilles Tjoelker v = find_var(name, NULL, NULL);
4253a99ed46SJilles Tjoelker if (v == NULL || v->flags & VUNSET)
4264b88c807SRodney W. Grimes return NULL;
4273a99ed46SJilles Tjoelker return v->text + v->name_len + 1;
4284b88c807SRodney W. Grimes }
4294b88c807SRodney W. Grimes
4304b88c807SRodney W. Grimes
4314b88c807SRodney W. Grimes
4324b88c807SRodney W. Grimes /*
4334b88c807SRodney W. Grimes * Search the environment of a builtin command. If the second argument
4344b88c807SRodney W. Grimes * is nonzero, return the value of a variable even if it hasn't been
4354b88c807SRodney W. Grimes * exported.
4364b88c807SRodney W. Grimes */
4374b88c807SRodney W. Grimes
4384b88c807SRodney W. Grimes char *
bltinlookup(const char * name,int doall)4392cac6e36SJilles Tjoelker bltinlookup(const char *name, int doall)
4404b88c807SRodney W. Grimes {
4414b88c807SRodney W. Grimes struct var *v;
442011d162dSJilles Tjoelker char *result;
4438ef0ae8aSJilles Tjoelker int i;
4444b88c807SRodney W. Grimes
445011d162dSJilles Tjoelker result = NULL;
4468ef0ae8aSJilles Tjoelker if (cmdenviron) for (i = 0; i < cmdenviron->count; i++) {
4478ef0ae8aSJilles Tjoelker if (varequal(cmdenviron->args[i], name))
4488ef0ae8aSJilles Tjoelker result = strchr(cmdenviron->args[i], '=') + 1;
4494b88c807SRodney W. Grimes }
450011d162dSJilles Tjoelker if (result != NULL)
451011d162dSJilles Tjoelker return result;
4523a99ed46SJilles Tjoelker
4533a99ed46SJilles Tjoelker v = find_var(name, NULL, NULL);
4543a99ed46SJilles Tjoelker if (v == NULL || v->flags & VUNSET ||
4553a99ed46SJilles Tjoelker (!doall && (v->flags & VEXPORT) == 0))
4564b88c807SRodney W. Grimes return NULL;
4573a99ed46SJilles Tjoelker return v->text + v->name_len + 1;
4584b88c807SRodney W. Grimes }
4594b88c807SRodney W. Grimes
4604b88c807SRodney W. Grimes
461e4b50334SJilles Tjoelker /*
462e4b50334SJilles Tjoelker * Set up locale for a builtin (LANG/LC_* assignments).
463e4b50334SJilles Tjoelker */
464e4b50334SJilles Tjoelker void
bltinsetlocale(void)465e4b50334SJilles Tjoelker bltinsetlocale(void)
466e4b50334SJilles Tjoelker {
467e4b50334SJilles Tjoelker int act = 0;
468e4b50334SJilles Tjoelker char *loc, *locdef;
469e4b50334SJilles Tjoelker int i;
470e4b50334SJilles Tjoelker
4718ef0ae8aSJilles Tjoelker if (cmdenviron) for (i = 0; i < cmdenviron->count; i++) {
4728ef0ae8aSJilles Tjoelker if (localevar(cmdenviron->args[i])) {
473e4b50334SJilles Tjoelker act = 1;
474e4b50334SJilles Tjoelker break;
475e4b50334SJilles Tjoelker }
476e4b50334SJilles Tjoelker }
477e4b50334SJilles Tjoelker if (!act)
478e4b50334SJilles Tjoelker return;
479e4b50334SJilles Tjoelker loc = bltinlookup("LC_ALL", 0);
480e4b50334SJilles Tjoelker INTOFF;
481e4b50334SJilles Tjoelker if (loc != NULL) {
482e4b50334SJilles Tjoelker setlocale(LC_ALL, loc);
483e4b50334SJilles Tjoelker INTON;
4846ed74a0aSJilles Tjoelker updatecharset();
485e4b50334SJilles Tjoelker return;
486e4b50334SJilles Tjoelker }
487e4b50334SJilles Tjoelker locdef = bltinlookup("LANG", 0);
488e4b50334SJilles Tjoelker for (i = 0; locale_names[i] != NULL; i++) {
489e4b50334SJilles Tjoelker loc = bltinlookup(locale_names[i], 0);
490e4b50334SJilles Tjoelker if (loc == NULL)
491e4b50334SJilles Tjoelker loc = locdef;
492e4b50334SJilles Tjoelker if (loc != NULL)
493e4b50334SJilles Tjoelker setlocale(locale_categories[i], loc);
494e4b50334SJilles Tjoelker }
495e4b50334SJilles Tjoelker INTON;
4966ed74a0aSJilles Tjoelker updatecharset();
497e4b50334SJilles Tjoelker }
498e4b50334SJilles Tjoelker
499e4b50334SJilles Tjoelker /*
500e4b50334SJilles Tjoelker * Undo the effect of bltinlocaleset().
501e4b50334SJilles Tjoelker */
502e4b50334SJilles Tjoelker void
bltinunsetlocale(void)503e4b50334SJilles Tjoelker bltinunsetlocale(void)
504e4b50334SJilles Tjoelker {
5058ef0ae8aSJilles Tjoelker int i;
506e4b50334SJilles Tjoelker
507e4b50334SJilles Tjoelker INTOFF;
5088ef0ae8aSJilles Tjoelker if (cmdenviron) for (i = 0; i < cmdenviron->count; i++) {
5098ef0ae8aSJilles Tjoelker if (localevar(cmdenviron->args[i])) {
510e4b50334SJilles Tjoelker setlocale(LC_ALL, "");
5116ed74a0aSJilles Tjoelker updatecharset();
5123f2da875SJilles Tjoelker break;
513e4b50334SJilles Tjoelker }
514e4b50334SJilles Tjoelker }
515e4b50334SJilles Tjoelker INTON;
516e4b50334SJilles Tjoelker }
517e4b50334SJilles Tjoelker
5186ed74a0aSJilles Tjoelker /*
5196ed74a0aSJilles Tjoelker * Update the localeisutf8 flag.
5206ed74a0aSJilles Tjoelker */
5216ed74a0aSJilles Tjoelker void
updatecharset(void)5226ed74a0aSJilles Tjoelker updatecharset(void)
5236ed74a0aSJilles Tjoelker {
5246ed74a0aSJilles Tjoelker char *charset;
5256ed74a0aSJilles Tjoelker
5266ed74a0aSJilles Tjoelker charset = nl_langinfo(CODESET);
5276ed74a0aSJilles Tjoelker localeisutf8 = !strcmp(charset, "UTF-8");
5286ed74a0aSJilles Tjoelker }
5294b88c807SRodney W. Grimes
53007eb7033SJilles Tjoelker void
initcharset(void)53107eb7033SJilles Tjoelker initcharset(void)
53207eb7033SJilles Tjoelker {
53307eb7033SJilles Tjoelker updatecharset();
53407eb7033SJilles Tjoelker initial_localeisutf8 = localeisutf8;
53507eb7033SJilles Tjoelker }
53607eb7033SJilles Tjoelker
5374b88c807SRodney W. Grimes /*
5384b88c807SRodney W. Grimes * Generate a list of exported variables. This routine is used to construct
5394b88c807SRodney W. Grimes * the third argument to execve when executing a program.
5404b88c807SRodney W. Grimes */
5414b88c807SRodney W. Grimes
5424b88c807SRodney W. Grimes char **
environment(void)5435134c3f7SWarner Losh environment(void)
5445134c3f7SWarner Losh {
5454b88c807SRodney W. Grimes int nenv;
5464b88c807SRodney W. Grimes struct var **vpp;
5474b88c807SRodney W. Grimes struct var *vp;
5484b88c807SRodney W. Grimes char **env, **ep;
5494b88c807SRodney W. Grimes
5504b88c807SRodney W. Grimes nenv = 0;
5514b88c807SRodney W. Grimes for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
5524b88c807SRodney W. Grimes for (vp = *vpp ; vp ; vp = vp->next)
553*56f33d07SJilles Tjoelker if ((vp->flags & (VEXPORT|VUNSET)) == VEXPORT)
5544b88c807SRodney W. Grimes nenv++;
5554b88c807SRodney W. Grimes }
5564b88c807SRodney W. Grimes ep = env = stalloc((nenv + 1) * sizeof *env);
5574b88c807SRodney W. Grimes for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
5584b88c807SRodney W. Grimes for (vp = *vpp ; vp ; vp = vp->next)
559*56f33d07SJilles Tjoelker if ((vp->flags & (VEXPORT|VUNSET)) == VEXPORT)
5604b88c807SRodney W. Grimes *ep++ = vp->text;
5614b88c807SRodney W. Grimes }
5624b88c807SRodney W. Grimes *ep = NULL;
5634b88c807SRodney W. Grimes return env;
5644b88c807SRodney W. Grimes }
5654b88c807SRodney W. Grimes
5664b88c807SRodney W. Grimes
56788328642SDavid E. O'Brien static int
var_compare(const void * a,const void * b)568692f35fdSStefan Farfeleder var_compare(const void *a, const void *b)
569692f35fdSStefan Farfeleder {
570692f35fdSStefan Farfeleder const char *const *sa, *const *sb;
571692f35fdSStefan Farfeleder
572692f35fdSStefan Farfeleder sa = a;
573692f35fdSStefan Farfeleder sb = b;
574692f35fdSStefan Farfeleder /*
575692f35fdSStefan Farfeleder * This compares two var=value strings which creates a different
576692f35fdSStefan Farfeleder * order from what you would probably expect. POSIX is somewhat
577692f35fdSStefan Farfeleder * ambiguous on what should be sorted exactly.
578692f35fdSStefan Farfeleder */
579692f35fdSStefan Farfeleder return strcoll(*sa, *sb);
580692f35fdSStefan Farfeleder }
581692f35fdSStefan Farfeleder
5824b88c807SRodney W. Grimes
5834b88c807SRodney W. Grimes /*
584cff1d849SJilles Tjoelker * Command to list all variables which are set. This is invoked from the
585cff1d849SJilles Tjoelker * set command when it is called without any options or operands.
5864b88c807SRodney W. Grimes */
5874b88c807SRodney W. Grimes
5884b88c807SRodney W. Grimes int
showvarscmd(int argc __unused,char ** argv __unused)5895134c3f7SWarner Losh showvarscmd(int argc __unused, char **argv __unused)
590aa9caaf6SPeter Wemm {
5914b88c807SRodney W. Grimes struct var **vpp;
5924b88c807SRodney W. Grimes struct var *vp;
59359258844STim J. Robbins const char *s;
594692f35fdSStefan Farfeleder const char **vars;
595692f35fdSStefan Farfeleder int i, n;
5964b88c807SRodney W. Grimes
597692f35fdSStefan Farfeleder /*
598692f35fdSStefan Farfeleder * POSIX requires us to sort the variables.
599692f35fdSStefan Farfeleder */
600692f35fdSStefan Farfeleder n = 0;
6014b88c807SRodney W. Grimes for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) {
6024b88c807SRodney W. Grimes for (vp = *vpp; vp; vp = vp->next) {
603692f35fdSStefan Farfeleder if (!(vp->flags & VUNSET))
604692f35fdSStefan Farfeleder n++;
605692f35fdSStefan Farfeleder }
606692f35fdSStefan Farfeleder }
607692f35fdSStefan Farfeleder
60833233ec7SJilles Tjoelker INTOFF;
609692f35fdSStefan Farfeleder vars = ckmalloc(n * sizeof(*vars));
610692f35fdSStefan Farfeleder i = 0;
611692f35fdSStefan Farfeleder for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) {
612692f35fdSStefan Farfeleder for (vp = *vpp; vp; vp = vp->next) {
613692f35fdSStefan Farfeleder if (!(vp->flags & VUNSET))
614692f35fdSStefan Farfeleder vars[i++] = vp->text;
615692f35fdSStefan Farfeleder }
616692f35fdSStefan Farfeleder }
617692f35fdSStefan Farfeleder
618692f35fdSStefan Farfeleder qsort(vars, n, sizeof(*vars), var_compare);
619692f35fdSStefan Farfeleder for (i = 0; i < n; i++) {
620f5f215e2SJilles Tjoelker /*
621f5f215e2SJilles Tjoelker * Skip improper variable names so the output remains usable as
622f5f215e2SJilles Tjoelker * shell input.
623f5f215e2SJilles Tjoelker */
624f5f215e2SJilles Tjoelker if (!isassignment(vars[i]))
625f5f215e2SJilles Tjoelker continue;
626aeb5d065SJilles Tjoelker s = strchr(vars[i], '=');
627aeb5d065SJilles Tjoelker s++;
628aeb5d065SJilles Tjoelker outbin(vars[i], s - vars[i], out1);
629aeb5d065SJilles Tjoelker out1qstr(s);
63059258844STim J. Robbins out1c('\n');
6314b88c807SRodney W. Grimes }
632692f35fdSStefan Farfeleder ckfree(vars);
63333233ec7SJilles Tjoelker INTON;
634692f35fdSStefan Farfeleder
6354b88c807SRodney W. Grimes return 0;
6364b88c807SRodney W. Grimes }
6374b88c807SRodney W. Grimes
6384b88c807SRodney W. Grimes
6394b88c807SRodney W. Grimes
6404b88c807SRodney W. Grimes /*
6414b88c807SRodney W. Grimes * The export and readonly commands.
6424b88c807SRodney W. Grimes */
6434b88c807SRodney W. Grimes
6444b88c807SRodney W. Grimes int
exportcmd(int argc __unused,char ** argv)6457cbda738SJilles Tjoelker exportcmd(int argc __unused, char **argv)
646aa9caaf6SPeter Wemm {
6474b88c807SRodney W. Grimes struct var **vpp;
6484b88c807SRodney W. Grimes struct var *vp;
6497cbda738SJilles Tjoelker char **ap;
6504b88c807SRodney W. Grimes char *name;
6514b88c807SRodney W. Grimes char *p;
65245086f8cSTim J. Robbins char *cmdname;
65345086f8cSTim J. Robbins int ch, values;
6544b88c807SRodney W. Grimes int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT;
6554b88c807SRodney W. Grimes
65645086f8cSTim J. Robbins cmdname = argv[0];
65745086f8cSTim J. Robbins values = 0;
6587cbda738SJilles Tjoelker while ((ch = nextopt("p")) != '\0') {
65945086f8cSTim J. Robbins switch (ch) {
66045086f8cSTim J. Robbins case 'p':
66145086f8cSTim J. Robbins values = 1;
66245086f8cSTim J. Robbins break;
66345086f8cSTim J. Robbins }
66445086f8cSTim J. Robbins }
66545086f8cSTim J. Robbins
6667cbda738SJilles Tjoelker if (values && *argptr != NULL)
6673fda45ecSStefan Farfeleder error("-p requires no arguments");
6687cbda738SJilles Tjoelker if (*argptr != NULL) {
6697cbda738SJilles Tjoelker for (ap = argptr; (name = *ap) != NULL; ap++) {
6704b88c807SRodney W. Grimes if ((p = strchr(name, '=')) != NULL) {
6714b88c807SRodney W. Grimes p++;
6724b88c807SRodney W. Grimes } else {
6733a99ed46SJilles Tjoelker vp = find_var(name, NULL, NULL);
6743a99ed46SJilles Tjoelker if (vp != NULL) {
6754b88c807SRodney W. Grimes vp->flags |= flag;
676ba726b8aSAndrey A. Chernov if ((vp->flags & VEXPORT) && localevar(vp->text)) {
6775b78c6c2SSean Farley change_env(vp->text, 1);
678ba726b8aSAndrey A. Chernov (void) setlocale(LC_ALL, "");
6796ed74a0aSJilles Tjoelker updatecharset();
680ba726b8aSAndrey A. Chernov }
6813a99ed46SJilles Tjoelker continue;
6824b88c807SRodney W. Grimes }
6834b88c807SRodney W. Grimes }
6844b88c807SRodney W. Grimes setvar(name, p, flag);
6854b88c807SRodney W. Grimes }
6864b88c807SRodney W. Grimes } else {
6874b88c807SRodney W. Grimes for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
6884b88c807SRodney W. Grimes for (vp = *vpp ; vp ; vp = vp->next) {
6894b88c807SRodney W. Grimes if (vp->flags & flag) {
69045086f8cSTim J. Robbins if (values) {
691f5f215e2SJilles Tjoelker /*
692f5f215e2SJilles Tjoelker * Skip improper variable names
693f5f215e2SJilles Tjoelker * so the output remains usable
694f5f215e2SJilles Tjoelker * as shell input.
695f5f215e2SJilles Tjoelker */
696f5f215e2SJilles Tjoelker if (!isassignment(vp->text))
697f5f215e2SJilles Tjoelker continue;
69845086f8cSTim J. Robbins out1str(cmdname);
69945086f8cSTim J. Robbins out1c(' ');
70045086f8cSTim J. Robbins }
70145086f8cSTim J. Robbins if (values && !(vp->flags & VUNSET)) {
702258ef734SJilles Tjoelker outbin(vp->text,
703258ef734SJilles Tjoelker vp->name_len + 1, out1);
704258ef734SJilles Tjoelker out1qstr(vp->text +
705258ef734SJilles Tjoelker vp->name_len + 1);
706aeb5d065SJilles Tjoelker } else
707258ef734SJilles Tjoelker outbin(vp->text, vp->name_len,
708aeb5d065SJilles Tjoelker out1);
7094b88c807SRodney W. Grimes out1c('\n');
7104b88c807SRodney W. Grimes }
7114b88c807SRodney W. Grimes }
7124b88c807SRodney W. Grimes }
7134b88c807SRodney W. Grimes }
7144b88c807SRodney W. Grimes return 0;
7154b88c807SRodney W. Grimes }
7164b88c807SRodney W. Grimes
7174b88c807SRodney W. Grimes
7184b88c807SRodney W. Grimes /*
7194b88c807SRodney W. Grimes * The "local" command.
7204b88c807SRodney W. Grimes */
7214b88c807SRodney W. Grimes
722aa9caaf6SPeter Wemm int
localcmd(int argc __unused,char ** argv __unused)7235134c3f7SWarner Losh localcmd(int argc __unused, char **argv __unused)
724aa9caaf6SPeter Wemm {
7254b88c807SRodney W. Grimes char *name;
7264b88c807SRodney W. Grimes
727056fd329SJilles Tjoelker nextopt("");
7284b88c807SRodney W. Grimes if (! in_function())
7294b88c807SRodney W. Grimes error("Not in a function");
7304b88c807SRodney W. Grimes while ((name = *argptr++) != NULL) {
7314b88c807SRodney W. Grimes mklocal(name);
7324b88c807SRodney W. Grimes }
7334b88c807SRodney W. Grimes return 0;
7344b88c807SRodney W. Grimes }
7354b88c807SRodney W. Grimes
7364b88c807SRodney W. Grimes
7374b88c807SRodney W. Grimes /*
7384b88c807SRodney W. Grimes * Make a variable a local variable. When a variable is made local, it's
7394b88c807SRodney W. Grimes * value and flags are saved in a localvar structure. The saved values
7404b88c807SRodney W. Grimes * will be restored when the shell function returns. We handle the name
7414b88c807SRodney W. Grimes * "-" as a special case.
7424b88c807SRodney W. Grimes */
7434b88c807SRodney W. Grimes
7444b88c807SRodney W. Grimes void
mklocal(char * name)7455134c3f7SWarner Losh mklocal(char *name)
7464b88c807SRodney W. Grimes {
7474b88c807SRodney W. Grimes struct localvar *lvp;
7484b88c807SRodney W. Grimes struct var **vpp;
7494b88c807SRodney W. Grimes struct var *vp;
7504b88c807SRodney W. Grimes
7514b88c807SRodney W. Grimes INTOFF;
7524b88c807SRodney W. Grimes lvp = ckmalloc(sizeof (struct localvar));
7534b88c807SRodney W. Grimes if (name[0] == '-' && name[1] == '\0') {
7543da40d4aSJilles Tjoelker lvp->text = ckmalloc(sizeof optval);
7553da40d4aSJilles Tjoelker memcpy(lvp->text, optval, sizeof optval);
7564b88c807SRodney W. Grimes vp = NULL;
7574b88c807SRodney W. Grimes } else {
7583a99ed46SJilles Tjoelker vp = find_var(name, &vpp, NULL);
7594b88c807SRodney W. Grimes if (vp == NULL) {
7604b88c807SRodney W. Grimes if (strchr(name, '='))
761c543e1aeSJilles Tjoelker setvareq(savestr(name), VSTRFIXED | VNOLOCAL);
7624b88c807SRodney W. Grimes else
763c543e1aeSJilles Tjoelker setvar(name, NULL, VSTRFIXED | VNOLOCAL);
7644b88c807SRodney W. Grimes vp = *vpp; /* the new variable */
7654b88c807SRodney W. Grimes lvp->text = NULL;
7664b88c807SRodney W. Grimes lvp->flags = VUNSET;
7674b88c807SRodney W. Grimes } else {
7684b88c807SRodney W. Grimes lvp->text = vp->text;
7694b88c807SRodney W. Grimes lvp->flags = vp->flags;
7704b88c807SRodney W. Grimes vp->flags |= VSTRFIXED|VTEXTFIXED;
7713a99ed46SJilles Tjoelker if (name[vp->name_len] == '=')
772c543e1aeSJilles Tjoelker setvareq(savestr(name), VNOLOCAL);
7734b88c807SRodney W. Grimes }
7744b88c807SRodney W. Grimes }
7754b88c807SRodney W. Grimes lvp->vp = vp;
7764b88c807SRodney W. Grimes lvp->next = localvars;
7774b88c807SRodney W. Grimes localvars = lvp;
7784b88c807SRodney W. Grimes INTON;
7794b88c807SRodney W. Grimes }
7804b88c807SRodney W. Grimes
7814b88c807SRodney W. Grimes
7824b88c807SRodney W. Grimes /*
7834b88c807SRodney W. Grimes * Called after a function returns.
7844b88c807SRodney W. Grimes */
7854b88c807SRodney W. Grimes
7864b88c807SRodney W. Grimes void
poplocalvars(void)7875134c3f7SWarner Losh poplocalvars(void)
7885134c3f7SWarner Losh {
7894b88c807SRodney W. Grimes struct localvar *lvp;
7904b88c807SRodney W. Grimes struct var *vp;
791cf45f124SJilles Tjoelker int islocalevar;
7924b88c807SRodney W. Grimes
7931632bf1aSJilles Tjoelker INTOFF;
7944b88c807SRodney W. Grimes while ((lvp = localvars) != NULL) {
7954b88c807SRodney W. Grimes localvars = lvp->next;
7964b88c807SRodney W. Grimes vp = lvp->vp;
7974b88c807SRodney W. Grimes if (vp == NULL) { /* $- saved */
7983da40d4aSJilles Tjoelker memcpy(optval, lvp->text, sizeof optval);
7994b88c807SRodney W. Grimes ckfree(lvp->text);
8004a7b1013SJilles Tjoelker optschanged();
8014b88c807SRodney W. Grimes } else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) {
802a9bdd616SJilles Tjoelker vp->flags &= ~VREADONLY;
8034b88c807SRodney W. Grimes (void)unsetvar(vp->text);
8044b88c807SRodney W. Grimes } else {
805cf45f124SJilles Tjoelker islocalevar = (vp->flags | lvp->flags) & VEXPORT &&
806cf45f124SJilles Tjoelker localevar(lvp->text);
8074b88c807SRodney W. Grimes if ((vp->flags & VTEXTFIXED) == 0)
8084b88c807SRodney W. Grimes ckfree(vp->text);
8094b88c807SRodney W. Grimes vp->flags = lvp->flags;
8104b88c807SRodney W. Grimes vp->text = lvp->text;
811cf45f124SJilles Tjoelker if (vp->func)
812cf45f124SJilles Tjoelker (*vp->func)(vp->text + vp->name_len + 1);
813cf45f124SJilles Tjoelker if (islocalevar) {
814cf45f124SJilles Tjoelker change_env(vp->text, vp->flags & VEXPORT &&
815cf45f124SJilles Tjoelker (vp->flags & VUNSET) == 0);
816cf45f124SJilles Tjoelker setlocale(LC_ALL, "");
817cf45f124SJilles Tjoelker updatecharset();
818cf45f124SJilles Tjoelker }
8194b88c807SRodney W. Grimes }
8204b88c807SRodney W. Grimes ckfree(lvp);
8214b88c807SRodney W. Grimes }
8221632bf1aSJilles Tjoelker INTON;
8234b88c807SRodney W. Grimes }
8244b88c807SRodney W. Grimes
8254b88c807SRodney W. Grimes
826aa9caaf6SPeter Wemm int
setvarcmd(int argc,char ** argv)8275134c3f7SWarner Losh setvarcmd(int argc, char **argv)
828aa9caaf6SPeter Wemm {
8294b88c807SRodney W. Grimes if (argc <= 2)
8304b88c807SRodney W. Grimes return unsetcmd(argc, argv);
8314b88c807SRodney W. Grimes else if (argc == 3)
8324b88c807SRodney W. Grimes setvar(argv[1], argv[2], 0);
8334b88c807SRodney W. Grimes else
834274110dfSJilles Tjoelker error("too many arguments");
8354b88c807SRodney W. Grimes return 0;
8364b88c807SRodney W. Grimes }
8374b88c807SRodney W. Grimes
8384b88c807SRodney W. Grimes
8394b88c807SRodney W. Grimes /*
840cff1d849SJilles Tjoelker * The unset builtin command.
8414b88c807SRodney W. Grimes */
8424b88c807SRodney W. Grimes
843aa9caaf6SPeter Wemm int
unsetcmd(int argc __unused,char ** argv __unused)8445134c3f7SWarner Losh unsetcmd(int argc __unused, char **argv __unused)
845aa9caaf6SPeter Wemm {
8464b88c807SRodney W. Grimes char **ap;
8474b88c807SRodney W. Grimes int i;
8484b88c807SRodney W. Grimes int flg_func = 0;
8494b88c807SRodney W. Grimes int flg_var = 0;
8504b88c807SRodney W. Grimes int ret = 0;
8514b88c807SRodney W. Grimes
8524b88c807SRodney W. Grimes while ((i = nextopt("vf")) != '\0') {
8534b88c807SRodney W. Grimes if (i == 'f')
8544b88c807SRodney W. Grimes flg_func = 1;
8554b88c807SRodney W. Grimes else
8564b88c807SRodney W. Grimes flg_var = 1;
8574b88c807SRodney W. Grimes }
8584b88c807SRodney W. Grimes if (flg_func == 0 && flg_var == 0)
8594b88c807SRodney W. Grimes flg_var = 1;
8604b88c807SRodney W. Grimes
8611632bf1aSJilles Tjoelker INTOFF;
8624b88c807SRodney W. Grimes for (ap = argptr; *ap ; ap++) {
8634b88c807SRodney W. Grimes if (flg_func)
8644b88c807SRodney W. Grimes ret |= unsetfunc(*ap);
8654b88c807SRodney W. Grimes if (flg_var)
8664b88c807SRodney W. Grimes ret |= unsetvar(*ap);
8674b88c807SRodney W. Grimes }
8681632bf1aSJilles Tjoelker INTON;
8694b88c807SRodney W. Grimes return ret;
8704b88c807SRodney W. Grimes }
8714b88c807SRodney W. Grimes
8724b88c807SRodney W. Grimes
8734b88c807SRodney W. Grimes /*
8744b88c807SRodney W. Grimes * Unset the specified variable.
8751632bf1aSJilles Tjoelker * Called with interrupts off.
8764b88c807SRodney W. Grimes */
8774b88c807SRodney W. Grimes
878ab0a2172SSteve Price int
unsetvar(const char * s)8792cac6e36SJilles Tjoelker unsetvar(const char *s)
8804b88c807SRodney W. Grimes {
8814b88c807SRodney W. Grimes struct var **vpp;
8824b88c807SRodney W. Grimes struct var *vp;
8834b88c807SRodney W. Grimes
8843a99ed46SJilles Tjoelker vp = find_var(s, &vpp, NULL);
8853a99ed46SJilles Tjoelker if (vp == NULL)
8863a99ed46SJilles Tjoelker return (0);
8874b88c807SRodney W. Grimes if (vp->flags & VREADONLY)
8884b88c807SRodney W. Grimes return (1);
8893a99ed46SJilles Tjoelker if (vp->text[vp->name_len + 1] != '\0')
890781bfb5aSJilles Tjoelker setvar(s, "", 0);
891ba726b8aSAndrey A. Chernov if ((vp->flags & VEXPORT) && localevar(vp->text)) {
8925b78c6c2SSean Farley change_env(s, 0);
893ba726b8aSAndrey A. Chernov setlocale(LC_ALL, "");
8946ed74a0aSJilles Tjoelker updatecharset();
895ba726b8aSAndrey A. Chernov }
8964b88c807SRodney W. Grimes vp->flags &= ~VEXPORT;
8974b88c807SRodney W. Grimes vp->flags |= VUNSET;
8984b88c807SRodney W. Grimes if ((vp->flags & VSTRFIXED) == 0) {
8994b88c807SRodney W. Grimes if ((vp->flags & VTEXTFIXED) == 0)
9004b88c807SRodney W. Grimes ckfree(vp->text);
9014b88c807SRodney W. Grimes *vpp = vp->next;
9024b88c807SRodney W. Grimes ckfree(vp);
9034b88c807SRodney W. Grimes }
9044b88c807SRodney W. Grimes return (0);
9054b88c807SRodney W. Grimes }
9064b88c807SRodney W. Grimes
9074b88c807SRodney W. Grimes
9084b88c807SRodney W. Grimes
9094b88c807SRodney W. Grimes /*
9106c7d8328SEitan Adler * Returns true if the two strings specify the same variable. The first
9114b88c807SRodney W. Grimes * variable name is terminated by '='; the second may be terminated by
9124b88c807SRodney W. Grimes * either '=' or '\0'.
9134b88c807SRodney W. Grimes */
9144b88c807SRodney W. Grimes
91588328642SDavid E. O'Brien static int
varequal(const char * p,const char * q)9162cac6e36SJilles Tjoelker varequal(const char *p, const char *q)
9174b88c807SRodney W. Grimes {
9184b88c807SRodney W. Grimes while (*p == *q++) {
9194b88c807SRodney W. Grimes if (*p++ == '=')
9204b88c807SRodney W. Grimes return 1;
9214b88c807SRodney W. Grimes }
9224b88c807SRodney W. Grimes if (*p == '=' && *(q - 1) == '\0')
9234b88c807SRodney W. Grimes return 1;
9244b88c807SRodney W. Grimes return 0;
9254b88c807SRodney W. Grimes }
9263a99ed46SJilles Tjoelker
9273a99ed46SJilles Tjoelker /*
9283a99ed46SJilles Tjoelker * Search for a variable.
9293a99ed46SJilles Tjoelker * 'name' may be terminated by '=' or a NUL.
9303a99ed46SJilles Tjoelker * vppp is set to the pointer to vp, or the list head if vp isn't found
9316c7d8328SEitan Adler * lenp is set to the number of characters in 'name'
9323a99ed46SJilles Tjoelker */
9333a99ed46SJilles Tjoelker
9343a99ed46SJilles Tjoelker static struct var *
find_var(const char * name,struct var *** vppp,int * lenp)9353a99ed46SJilles Tjoelker find_var(const char *name, struct var ***vppp, int *lenp)
9363a99ed46SJilles Tjoelker {
9373a99ed46SJilles Tjoelker unsigned int hashval;
9383a99ed46SJilles Tjoelker int len;
9393a99ed46SJilles Tjoelker struct var *vp, **vpp;
9403a99ed46SJilles Tjoelker const char *p = name;
9413a99ed46SJilles Tjoelker
9423a99ed46SJilles Tjoelker hashval = 0;
9433a99ed46SJilles Tjoelker while (*p && *p != '=')
9443a99ed46SJilles Tjoelker hashval = 2 * hashval + (unsigned char)*p++;
9453a99ed46SJilles Tjoelker len = p - name;
9463a99ed46SJilles Tjoelker
9473a99ed46SJilles Tjoelker if (lenp)
9483a99ed46SJilles Tjoelker *lenp = len;
9493a99ed46SJilles Tjoelker vpp = &vartab[hashval % VTABSIZE];
9503a99ed46SJilles Tjoelker if (vppp)
9513a99ed46SJilles Tjoelker *vppp = vpp;
9523a99ed46SJilles Tjoelker
9533a99ed46SJilles Tjoelker for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) {
9543a99ed46SJilles Tjoelker if (vp->name_len != len)
9553a99ed46SJilles Tjoelker continue;
9563a99ed46SJilles Tjoelker if (memcmp(vp->text, name, len) != 0)
9573a99ed46SJilles Tjoelker continue;
9583a99ed46SJilles Tjoelker if (vppp)
9593a99ed46SJilles Tjoelker *vppp = vpp;
9603a99ed46SJilles Tjoelker return vp;
9613a99ed46SJilles Tjoelker }
9623a99ed46SJilles Tjoelker return NULL;
9633a99ed46SJilles Tjoelker }
964