14b88c807SRodney W. Grimes /*- 24b88c807SRodney W. Grimes * Copyright (c) 1991, 1993 34b88c807SRodney W. Grimes * The Regents of the University of California. All rights reserved. 44b88c807SRodney W. Grimes * 54b88c807SRodney W. Grimes * This code is derived from software contributed to Berkeley by 64b88c807SRodney W. Grimes * Kenneth Almquist. 74b88c807SRodney W. Grimes * 84b88c807SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 94b88c807SRodney W. Grimes * modification, are permitted provided that the following conditions 104b88c807SRodney W. Grimes * are met: 114b88c807SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 124b88c807SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 134b88c807SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 144b88c807SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 154b88c807SRodney W. Grimes * documentation and/or other materials provided with the distribution. 164b88c807SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 174b88c807SRodney W. Grimes * may be used to endorse or promote products derived from this software 184b88c807SRodney W. Grimes * without specific prior written permission. 194b88c807SRodney W. Grimes * 204b88c807SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 214b88c807SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 224b88c807SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 234b88c807SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 244b88c807SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 254b88c807SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 264b88c807SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 274b88c807SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 284b88c807SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 294b88c807SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 304b88c807SRodney W. Grimes * SUCH DAMAGE. 314b88c807SRodney W. Grimes */ 324b88c807SRodney W. Grimes 334b88c807SRodney W. Grimes #ifndef lint 343d7b5b93SPhilippe Charnier #if 0 353d7b5b93SPhilippe Charnier static char sccsid[] = "@(#)var.c 8.3 (Berkeley) 5/4/95"; 363d7b5b93SPhilippe Charnier #endif 374b88c807SRodney W. Grimes #endif /* not lint */ 382749b141SDavid E. O'Brien #include <sys/cdefs.h> 392749b141SDavid E. O'Brien __FBSDID("$FreeBSD$"); 404b88c807SRodney W. Grimes 41aa9caaf6SPeter Wemm #include <unistd.h> 42aa9caaf6SPeter Wemm #include <stdlib.h> 438d5c19ffSDavid E. O'Brien #include <paths.h> 44aa9caaf6SPeter Wemm 454b88c807SRodney W. Grimes /* 464b88c807SRodney W. Grimes * Shell variables. 474b88c807SRodney W. Grimes */ 484b88c807SRodney W. Grimes 49ba726b8aSAndrey A. Chernov #include <locale.h> 506ed74a0aSJilles Tjoelker #include <langinfo.h> 51ba726b8aSAndrey A. Chernov 524b88c807SRodney W. Grimes #include "shell.h" 534b88c807SRodney W. Grimes #include "output.h" 544b88c807SRodney W. Grimes #include "expand.h" 554b88c807SRodney W. Grimes #include "nodes.h" /* for other headers */ 564b88c807SRodney W. Grimes #include "eval.h" /* defines cmdenviron */ 574b88c807SRodney W. Grimes #include "exec.h" 584b88c807SRodney W. Grimes #include "syntax.h" 594b88c807SRodney W. Grimes #include "options.h" 604b88c807SRodney W. Grimes #include "mail.h" 614b88c807SRodney W. Grimes #include "var.h" 624b88c807SRodney W. Grimes #include "memalloc.h" 634b88c807SRodney W. Grimes #include "error.h" 644b88c807SRodney W. Grimes #include "mystring.h" 65ab0a2172SSteve Price #include "parser.h" 66454a02b3SJilles Tjoelker #include "builtins.h" 67aa9caaf6SPeter Wemm #ifndef NO_HISTORY 68aa9caaf6SPeter Wemm #include "myhistedit.h" 69aa9caaf6SPeter Wemm #endif 704b88c807SRodney W. Grimes 714b88c807SRodney W. Grimes 724b88c807SRodney W. Grimes #define VTABSIZE 39 734b88c807SRodney W. Grimes 744b88c807SRodney W. Grimes 754b88c807SRodney W. Grimes struct varinit { 764b88c807SRodney W. Grimes struct var *var; 774b88c807SRodney W. Grimes int flags; 78c6c5dd37SJilles Tjoelker const char *text; 795134c3f7SWarner Losh void (*func)(const char *); 804b88c807SRodney W. Grimes }; 814b88c807SRodney W. Grimes 824b88c807SRodney W. Grimes 83aa9caaf6SPeter Wemm #ifndef NO_HISTORY 844b88c807SRodney W. Grimes struct var vhistsize; 85580eefdfSJilles Tjoelker struct var vterm; 86aa9caaf6SPeter Wemm #endif 874b88c807SRodney W. Grimes struct var vifs; 884b88c807SRodney W. Grimes struct var vmail; 894b88c807SRodney W. Grimes struct var vmpath; 904b88c807SRodney W. Grimes struct var vpath; 914b88c807SRodney W. Grimes struct var vps1; 924b88c807SRodney W. Grimes struct var vps2; 93120c8e6cSStefan Farfeleder struct var vps4; 94aa7b6f82SDavid E. O'Brien static struct var voptind; 95caf29fabSJilles Tjoelker struct var vdisvfork; 964b88c807SRodney W. Grimes 9758bdb076SJilles Tjoelker struct localvar *localvars; 98c543e1aeSJilles Tjoelker int forcelocal; 99c543e1aeSJilles Tjoelker 100aa7b6f82SDavid E. O'Brien static const struct varinit varinit[] = { 101aa9caaf6SPeter Wemm #ifndef NO_HISTORY 102c6c5dd37SJilles Tjoelker { &vhistsize, VUNSET, "HISTSIZE=", 103ab0a2172SSteve Price sethistsize }, 104aa9caaf6SPeter Wemm #endif 105c6c5dd37SJilles Tjoelker { &vifs, 0, "IFS= \t\n", 106ab0a2172SSteve Price NULL }, 107c6c5dd37SJilles Tjoelker { &vmail, VUNSET, "MAIL=", 108ab0a2172SSteve Price NULL }, 109c6c5dd37SJilles Tjoelker { &vmpath, VUNSET, "MAILPATH=", 110ab0a2172SSteve Price NULL }, 111c6c5dd37SJilles Tjoelker { &vpath, 0, "PATH=" _PATH_DEFPATH, 112ab0a2172SSteve Price changepath }, 1134b88c807SRodney W. Grimes /* 1144b88c807SRodney W. Grimes * vps1 depends on uid 1154b88c807SRodney W. Grimes */ 116c6c5dd37SJilles Tjoelker { &vps2, 0, "PS2=> ", 117ab0a2172SSteve Price NULL }, 118c6c5dd37SJilles Tjoelker { &vps4, 0, "PS4=+ ", 119120c8e6cSStefan Farfeleder NULL }, 120580eefdfSJilles Tjoelker #ifndef NO_HISTORY 121580eefdfSJilles Tjoelker { &vterm, VUNSET, "TERM=", 122580eefdfSJilles Tjoelker setterm }, 123580eefdfSJilles Tjoelker #endif 124c6c5dd37SJilles Tjoelker { &voptind, 0, "OPTIND=1", 125ab0a2172SSteve Price getoptsreset }, 126caf29fabSJilles Tjoelker { &vdisvfork, VUNSET, "SH_DISABLE_VFORK=", 127caf29fabSJilles Tjoelker NULL }, 128ab0a2172SSteve Price { NULL, 0, NULL, 129ab0a2172SSteve Price NULL } 1304b88c807SRodney W. Grimes }; 1314b88c807SRodney W. Grimes 132aa7b6f82SDavid E. O'Brien static struct var *vartab[VTABSIZE]; 1334b88c807SRodney W. Grimes 134aa7b6f82SDavid E. O'Brien static const char *const locale_names[7] = { 135e4b50334SJilles Tjoelker "LC_COLLATE", "LC_CTYPE", "LC_MONETARY", 136e4b50334SJilles Tjoelker "LC_NUMERIC", "LC_TIME", "LC_MESSAGES", NULL 137e4b50334SJilles Tjoelker }; 138aa7b6f82SDavid E. O'Brien static const int locale_categories[7] = { 139e4b50334SJilles Tjoelker LC_COLLATE, LC_CTYPE, LC_MONETARY, LC_NUMERIC, LC_TIME, LC_MESSAGES, 0 140e4b50334SJilles Tjoelker }; 141e4b50334SJilles Tjoelker 14288328642SDavid E. O'Brien static int varequal(const char *, const char *); 1433a99ed46SJilles Tjoelker static struct var *find_var(const char *, struct var ***, int *); 14488328642SDavid E. O'Brien static int localevar(const char *); 14522afca9bSJilles Tjoelker static void setvareq_const(const char *s, int flags); 1464b88c807SRodney W. Grimes 14759e0cc8eSJilles Tjoelker extern char **environ; 1484b88c807SRodney W. Grimes 1494b88c807SRodney W. Grimes /* 15059e0cc8eSJilles Tjoelker * This routine initializes the builtin variables and imports the environment. 15159e0cc8eSJilles Tjoelker * It is called when the shell is initialized. 1524b88c807SRodney W. Grimes */ 1534b88c807SRodney W. Grimes 1544b88c807SRodney W. Grimes void 1555134c3f7SWarner Losh initvar(void) 1565134c3f7SWarner Losh { 15739dccc6fSTim J. Robbins char ppid[20]; 1584b88c807SRodney W. Grimes const struct varinit *ip; 1594b88c807SRodney W. Grimes struct var *vp; 1604b88c807SRodney W. Grimes struct var **vpp; 16159e0cc8eSJilles Tjoelker char **envp; 1624b88c807SRodney W. Grimes 1634b88c807SRodney W. Grimes for (ip = varinit ; (vp = ip->var) != NULL ; ip++) { 1643a99ed46SJilles Tjoelker if (find_var(ip->text, &vpp, &vp->name_len) != NULL) 1653a99ed46SJilles Tjoelker continue; 1664b88c807SRodney W. Grimes vp->next = *vpp; 1674b88c807SRodney W. Grimes *vpp = vp; 168c6c5dd37SJilles Tjoelker vp->text = __DECONST(char *, ip->text); 169c6c5dd37SJilles Tjoelker vp->flags = ip->flags | VSTRFIXED | VTEXTFIXED; 170ab0a2172SSteve Price vp->func = ip->func; 1714b88c807SRodney W. Grimes } 1724b88c807SRodney W. Grimes /* 1734b88c807SRodney W. Grimes * PS1 depends on uid 1744b88c807SRodney W. Grimes */ 1753a99ed46SJilles Tjoelker if (find_var("PS1", &vpp, &vps1.name_len) == NULL) { 1764b88c807SRodney W. Grimes vps1.next = *vpp; 1774b88c807SRodney W. Grimes *vpp = &vps1; 178c6c5dd37SJilles Tjoelker vps1.text = __DECONST(char *, geteuid() ? "PS1=$ " : "PS1=# "); 1794b88c807SRodney W. Grimes vps1.flags = VSTRFIXED|VTEXTFIXED; 1804b88c807SRodney W. Grimes } 18139dccc6fSTim J. Robbins fmtstr(ppid, sizeof(ppid), "%d", (int)getppid()); 18239dccc6fSTim J. Robbins setvarsafe("PPID", ppid, 0); 18359e0cc8eSJilles Tjoelker for (envp = environ ; *envp ; envp++) { 18459e0cc8eSJilles Tjoelker if (strchr(*envp, '=')) { 18559e0cc8eSJilles Tjoelker setvareq(*envp, VEXPORT|VTEXTFIXED); 18659e0cc8eSJilles Tjoelker } 18759e0cc8eSJilles Tjoelker } 18822afca9bSJilles Tjoelker setvareq_const("OPTIND=1", 0); 1894b88c807SRodney W. Grimes } 1904b88c807SRodney W. Grimes 1914b88c807SRodney W. Grimes /* 192ab0a2172SSteve Price * Safe version of setvar, returns 1 on success 0 on failure. 193ab0a2172SSteve Price */ 194ab0a2172SSteve Price 195ab0a2172SSteve Price int 1962cac6e36SJilles Tjoelker setvarsafe(const char *name, const char *val, int flags) 197ab0a2172SSteve Price { 198ab0a2172SSteve Price struct jmploc jmploc; 199224fbf9fSJilles Tjoelker struct jmploc *const savehandler = handler; 200ab0a2172SSteve Price int err = 0; 2019922c6d2SJilles Tjoelker int inton; 202ab0a2172SSteve Price 2039922c6d2SJilles Tjoelker inton = is_int_on(); 204ab0a2172SSteve Price if (setjmp(jmploc.loc)) 205ab0a2172SSteve Price err = 1; 206ab0a2172SSteve Price else { 207ab0a2172SSteve Price handler = &jmploc; 208ab0a2172SSteve Price setvar(name, val, flags); 209ab0a2172SSteve Price } 210ab0a2172SSteve Price handler = savehandler; 2119922c6d2SJilles Tjoelker SETINTON(inton); 212ab0a2172SSteve Price return err; 213ab0a2172SSteve Price } 214ab0a2172SSteve Price 215ab0a2172SSteve Price /* 216b3decf89SJens Schweikhardt * Set the value of a variable. The flags argument is stored with the 2174b88c807SRodney W. Grimes * flags of the variable. If val is NULL, the variable is unset. 2184b88c807SRodney W. Grimes */ 2194b88c807SRodney W. Grimes 2204b88c807SRodney W. Grimes void 2212cac6e36SJilles Tjoelker setvar(const char *name, const char *val, int flags) 2224b88c807SRodney W. Grimes { 2232cac6e36SJilles Tjoelker const char *p; 224670dd3f0SJilles Tjoelker size_t len; 225670dd3f0SJilles Tjoelker size_t namelen; 226670dd3f0SJilles Tjoelker size_t vallen; 2274b88c807SRodney W. Grimes char *nameeq; 2284b88c807SRodney W. Grimes int isbad; 2294b88c807SRodney W. Grimes 2304b88c807SRodney W. Grimes isbad = 0; 2314b88c807SRodney W. Grimes p = name; 232ba726b8aSAndrey A. Chernov if (! is_name(*p)) 2334b88c807SRodney W. Grimes isbad = 1; 234ba726b8aSAndrey A. Chernov p++; 2354b88c807SRodney W. Grimes for (;;) { 2364b88c807SRodney W. Grimes if (! is_in_name(*p)) { 2374b88c807SRodney W. Grimes if (*p == '\0' || *p == '=') 2384b88c807SRodney W. Grimes break; 2394b88c807SRodney W. Grimes isbad = 1; 2404b88c807SRodney W. Grimes } 2414b88c807SRodney W. Grimes p++; 2424b88c807SRodney W. Grimes } 2434b88c807SRodney W. Grimes namelen = p - name; 2444b88c807SRodney W. Grimes if (isbad) 245670dd3f0SJilles Tjoelker error("%.*s: bad variable name", (int)namelen, name); 2464b88c807SRodney W. Grimes len = namelen + 2; /* 2 is space for '=' and '\0' */ 2474b88c807SRodney W. Grimes if (val == NULL) { 2484b88c807SRodney W. Grimes flags |= VUNSET; 249670dd3f0SJilles Tjoelker vallen = 0; 2504b88c807SRodney W. Grimes } else { 251670dd3f0SJilles Tjoelker vallen = strlen(val); 252670dd3f0SJilles Tjoelker len += vallen; 2534b88c807SRodney W. Grimes } 2541632bf1aSJilles Tjoelker INTOFF; 2552cac6e36SJilles Tjoelker nameeq = ckmalloc(len); 2562cac6e36SJilles Tjoelker memcpy(nameeq, name, namelen); 2572cac6e36SJilles Tjoelker nameeq[namelen] = '='; 2584b88c807SRodney W. Grimes if (val) 259670dd3f0SJilles Tjoelker memcpy(nameeq + namelen + 1, val, vallen + 1); 2602cac6e36SJilles Tjoelker else 2612cac6e36SJilles Tjoelker nameeq[namelen + 1] = '\0'; 2624b88c807SRodney W. Grimes setvareq(nameeq, flags); 2631632bf1aSJilles Tjoelker INTON; 2644b88c807SRodney W. Grimes } 2654b88c807SRodney W. Grimes 26688328642SDavid E. O'Brien static int 2672cac6e36SJilles Tjoelker localevar(const char *s) 268ba726b8aSAndrey A. Chernov { 269e4b50334SJilles Tjoelker const char *const *ss; 2704b88c807SRodney W. Grimes 271ba726b8aSAndrey A. Chernov if (*s != 'L') 272ba726b8aSAndrey A. Chernov return 0; 273ba726b8aSAndrey A. Chernov if (varequal(s + 1, "ANG")) 274ba726b8aSAndrey A. Chernov return 1; 275ba726b8aSAndrey A. Chernov if (strncmp(s + 1, "C_", 2) != 0) 276ba726b8aSAndrey A. Chernov return 0; 277e4b50334SJilles Tjoelker if (varequal(s + 3, "ALL")) 278e4b50334SJilles Tjoelker return 1; 279e4b50334SJilles Tjoelker for (ss = locale_names; *ss ; ss++) 280e4b50334SJilles Tjoelker if (varequal(s + 3, *ss + 3)) 281ba726b8aSAndrey A. Chernov return 1; 282ba726b8aSAndrey A. Chernov return 0; 283ba726b8aSAndrey A. Chernov } 2844b88c807SRodney W. Grimes 2855b78c6c2SSean Farley 2865b78c6c2SSean Farley /* 2875b78c6c2SSean Farley * Sets/unsets an environment variable from a pointer that may actually be a 2885b78c6c2SSean Farley * pointer into environ where the string should not be manipulated. 2895b78c6c2SSean Farley */ 29088328642SDavid E. O'Brien static void 2912cac6e36SJilles Tjoelker change_env(const char *s, int set) 2925b78c6c2SSean Farley { 2935b78c6c2SSean Farley char *eqp; 2945b78c6c2SSean Farley char *ss; 2955b78c6c2SSean Farley 2961632bf1aSJilles Tjoelker INTOFF; 2975b78c6c2SSean Farley ss = savestr(s); 2985b78c6c2SSean Farley if ((eqp = strchr(ss, '=')) != NULL) 2995b78c6c2SSean Farley *eqp = '\0'; 3005b78c6c2SSean Farley if (set && eqp != NULL) 3015b78c6c2SSean Farley (void) setenv(ss, eqp + 1, 1); 3025b78c6c2SSean Farley else 3035b78c6c2SSean Farley (void) unsetenv(ss); 3045b78c6c2SSean Farley ckfree(ss); 3051632bf1aSJilles Tjoelker INTON; 3065b78c6c2SSean Farley 3075b78c6c2SSean Farley return; 3085b78c6c2SSean Farley } 3095b78c6c2SSean Farley 3105b78c6c2SSean Farley 3114b88c807SRodney W. Grimes /* 3124b88c807SRodney W. Grimes * Same as setvar except that the variable and value are passed in 3134b88c807SRodney W. Grimes * the first argument as name=value. Since the first argument will 3144b88c807SRodney W. Grimes * be actually stored in the table, it should not be a string that 3154b88c807SRodney W. Grimes * will go away. 3164b88c807SRodney W. Grimes */ 3174b88c807SRodney W. Grimes 3184b88c807SRodney W. Grimes void 3195134c3f7SWarner Losh setvareq(char *s, int flags) 3204b88c807SRodney W. Grimes { 3214b88c807SRodney W. Grimes struct var *vp, **vpp; 3223a99ed46SJilles Tjoelker int nlen; 3234b88c807SRodney W. Grimes 324b8ec435eSMartin Cracauer if (aflag) 325b8ec435eSMartin Cracauer flags |= VEXPORT; 326c543e1aeSJilles Tjoelker if (forcelocal && !(flags & (VNOSET | VNOLOCAL))) 327c543e1aeSJilles Tjoelker mklocal(s); 3283a99ed46SJilles Tjoelker vp = find_var(s, &vpp, &nlen); 3293a99ed46SJilles Tjoelker if (vp != NULL) { 33089d4f883SJilles Tjoelker if (vp->flags & VREADONLY) { 33189d4f883SJilles Tjoelker if ((flags & (VTEXTFIXED|VSTACK)) == 0) 33289d4f883SJilles Tjoelker ckfree(s); 333d41b2be1SJilles Tjoelker error("%.*s: is read only", vp->name_len, vp->text); 33489d4f883SJilles Tjoelker } 335d73dba75SJilles Tjoelker if (flags & VNOSET) { 336d73dba75SJilles Tjoelker if ((flags & (VTEXTFIXED|VSTACK)) == 0) 337d73dba75SJilles Tjoelker ckfree(s); 338850460c0SJilles Tjoelker return; 339d73dba75SJilles Tjoelker } 3404b88c807SRodney W. Grimes INTOFF; 341ab0a2172SSteve Price 342ab0a2172SSteve Price if (vp->func && (flags & VNOFUNC) == 0) 3433a99ed46SJilles Tjoelker (*vp->func)(s + vp->name_len + 1); 344ab0a2172SSteve Price 3454b88c807SRodney W. Grimes if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0) 3464b88c807SRodney W. Grimes ckfree(vp->text); 347ab0a2172SSteve Price 3484b88c807SRodney W. Grimes vp->flags &= ~(VTEXTFIXED|VSTACK|VUNSET); 3494b88c807SRodney W. Grimes vp->flags |= flags; 3504b88c807SRodney W. Grimes vp->text = s; 351ab0a2172SSteve Price 352ab0a2172SSteve Price /* 353ab0a2172SSteve Price * We could roll this to a function, to handle it as 354ab0a2172SSteve Price * a regular variable function callback, but why bother? 35557063576SJilles Tjoelker * 35657063576SJilles Tjoelker * Note: this assumes iflag is not set to 1 initially. 35759e0cc8eSJilles Tjoelker * As part of initvar(), this is called before arguments 35857063576SJilles Tjoelker * are looked at. 359ab0a2172SSteve Price */ 36057063576SJilles Tjoelker if ((vp == &vmpath || (vp == &vmail && ! mpathset())) && 36157063576SJilles Tjoelker iflag == 1) 3624b88c807SRodney W. Grimes chkmail(1); 363ba726b8aSAndrey A. Chernov if ((vp->flags & VEXPORT) && localevar(s)) { 3645b78c6c2SSean Farley change_env(s, 1); 365ba726b8aSAndrey A. Chernov (void) setlocale(LC_ALL, ""); 3666ed74a0aSJilles Tjoelker updatecharset(); 367ba726b8aSAndrey A. Chernov } 3684b88c807SRodney W. Grimes INTON; 3694b88c807SRodney W. Grimes return; 3704b88c807SRodney W. Grimes } 3714b88c807SRodney W. Grimes /* not found */ 372d73dba75SJilles Tjoelker if (flags & VNOSET) { 373d73dba75SJilles Tjoelker if ((flags & (VTEXTFIXED|VSTACK)) == 0) 374d73dba75SJilles Tjoelker ckfree(s); 375850460c0SJilles Tjoelker return; 376d73dba75SJilles Tjoelker } 3771632bf1aSJilles Tjoelker INTOFF; 3784b88c807SRodney W. Grimes vp = ckmalloc(sizeof (*vp)); 3794b88c807SRodney W. Grimes vp->flags = flags; 3804b88c807SRodney W. Grimes vp->text = s; 3813a99ed46SJilles Tjoelker vp->name_len = nlen; 3824b88c807SRodney W. Grimes vp->next = *vpp; 383ab0a2172SSteve Price vp->func = NULL; 3844b88c807SRodney W. Grimes *vpp = vp; 385ba726b8aSAndrey A. Chernov if ((vp->flags & VEXPORT) && localevar(s)) { 3865b78c6c2SSean Farley change_env(s, 1); 387ba726b8aSAndrey A. Chernov (void) setlocale(LC_ALL, ""); 3886ed74a0aSJilles Tjoelker updatecharset(); 389ba726b8aSAndrey A. Chernov } 390ba726b8aSAndrey A. Chernov INTON; 3914b88c807SRodney W. Grimes } 3924b88c807SRodney W. Grimes 3934b88c807SRodney W. Grimes 39422afca9bSJilles Tjoelker static void 39522afca9bSJilles Tjoelker setvareq_const(const char *s, int flags) 39622afca9bSJilles Tjoelker { 39722afca9bSJilles Tjoelker setvareq(__DECONST(char *, s), flags | VTEXTFIXED); 39822afca9bSJilles Tjoelker } 39922afca9bSJilles Tjoelker 4004b88c807SRodney W. Grimes 4014b88c807SRodney W. Grimes /* 4024b88c807SRodney W. Grimes * Process a linked list of variable assignments. 4034b88c807SRodney W. Grimes */ 4044b88c807SRodney W. Grimes 4054b88c807SRodney W. Grimes void 4068ef0ae8aSJilles Tjoelker listsetvar(struct arglist *list, int flags) 4074b88c807SRodney W. Grimes { 4088ef0ae8aSJilles Tjoelker int i; 4094b88c807SRodney W. Grimes 4104b88c807SRodney W. Grimes INTOFF; 4118ef0ae8aSJilles Tjoelker for (i = 0; i < list->count; i++) 4128ef0ae8aSJilles Tjoelker setvareq(savestr(list->args[i]), flags); 4134b88c807SRodney W. Grimes INTON; 4144b88c807SRodney W. Grimes } 4154b88c807SRodney W. Grimes 4164b88c807SRodney W. Grimes 4174b88c807SRodney W. Grimes 4184b88c807SRodney W. Grimes /* 4194b88c807SRodney W. Grimes * Find the value of a variable. Returns NULL if not set. 4204b88c807SRodney W. Grimes */ 4214b88c807SRodney W. Grimes 4224b88c807SRodney W. Grimes char * 4232cac6e36SJilles Tjoelker lookupvar(const char *name) 4244b88c807SRodney W. Grimes { 4254b88c807SRodney W. Grimes struct var *v; 4264b88c807SRodney W. Grimes 4273a99ed46SJilles Tjoelker v = find_var(name, NULL, NULL); 4283a99ed46SJilles Tjoelker if (v == NULL || v->flags & VUNSET) 4294b88c807SRodney W. Grimes return NULL; 4303a99ed46SJilles Tjoelker return v->text + v->name_len + 1; 4314b88c807SRodney W. Grimes } 4324b88c807SRodney W. Grimes 4334b88c807SRodney W. Grimes 4344b88c807SRodney W. Grimes 4354b88c807SRodney W. Grimes /* 4364b88c807SRodney W. Grimes * Search the environment of a builtin command. If the second argument 4374b88c807SRodney W. Grimes * is nonzero, return the value of a variable even if it hasn't been 4384b88c807SRodney W. Grimes * exported. 4394b88c807SRodney W. Grimes */ 4404b88c807SRodney W. Grimes 4414b88c807SRodney W. Grimes char * 4422cac6e36SJilles Tjoelker bltinlookup(const char *name, int doall) 4434b88c807SRodney W. Grimes { 4444b88c807SRodney W. Grimes struct var *v; 445011d162dSJilles Tjoelker char *result; 4468ef0ae8aSJilles Tjoelker int i; 4474b88c807SRodney W. Grimes 448011d162dSJilles Tjoelker result = NULL; 4498ef0ae8aSJilles Tjoelker if (cmdenviron) for (i = 0; i < cmdenviron->count; i++) { 4508ef0ae8aSJilles Tjoelker if (varequal(cmdenviron->args[i], name)) 4518ef0ae8aSJilles Tjoelker result = strchr(cmdenviron->args[i], '=') + 1; 4524b88c807SRodney W. Grimes } 453011d162dSJilles Tjoelker if (result != NULL) 454011d162dSJilles Tjoelker return result; 4553a99ed46SJilles Tjoelker 4563a99ed46SJilles Tjoelker v = find_var(name, NULL, NULL); 4573a99ed46SJilles Tjoelker if (v == NULL || v->flags & VUNSET || 4583a99ed46SJilles Tjoelker (!doall && (v->flags & VEXPORT) == 0)) 4594b88c807SRodney W. Grimes return NULL; 4603a99ed46SJilles Tjoelker return v->text + v->name_len + 1; 4614b88c807SRodney W. Grimes } 4624b88c807SRodney W. Grimes 4634b88c807SRodney W. Grimes 464e4b50334SJilles Tjoelker /* 465e4b50334SJilles Tjoelker * Set up locale for a builtin (LANG/LC_* assignments). 466e4b50334SJilles Tjoelker */ 467e4b50334SJilles Tjoelker void 468e4b50334SJilles Tjoelker bltinsetlocale(void) 469e4b50334SJilles Tjoelker { 470e4b50334SJilles Tjoelker int act = 0; 471e4b50334SJilles Tjoelker char *loc, *locdef; 472e4b50334SJilles Tjoelker int i; 473e4b50334SJilles Tjoelker 4748ef0ae8aSJilles Tjoelker if (cmdenviron) for (i = 0; i < cmdenviron->count; i++) { 4758ef0ae8aSJilles Tjoelker if (localevar(cmdenviron->args[i])) { 476e4b50334SJilles Tjoelker act = 1; 477e4b50334SJilles Tjoelker break; 478e4b50334SJilles Tjoelker } 479e4b50334SJilles Tjoelker } 480e4b50334SJilles Tjoelker if (!act) 481e4b50334SJilles Tjoelker return; 482e4b50334SJilles Tjoelker loc = bltinlookup("LC_ALL", 0); 483e4b50334SJilles Tjoelker INTOFF; 484e4b50334SJilles Tjoelker if (loc != NULL) { 485e4b50334SJilles Tjoelker setlocale(LC_ALL, loc); 486e4b50334SJilles Tjoelker INTON; 4876ed74a0aSJilles Tjoelker updatecharset(); 488e4b50334SJilles Tjoelker return; 489e4b50334SJilles Tjoelker } 490e4b50334SJilles Tjoelker locdef = bltinlookup("LANG", 0); 491e4b50334SJilles Tjoelker for (i = 0; locale_names[i] != NULL; i++) { 492e4b50334SJilles Tjoelker loc = bltinlookup(locale_names[i], 0); 493e4b50334SJilles Tjoelker if (loc == NULL) 494e4b50334SJilles Tjoelker loc = locdef; 495e4b50334SJilles Tjoelker if (loc != NULL) 496e4b50334SJilles Tjoelker setlocale(locale_categories[i], loc); 497e4b50334SJilles Tjoelker } 498e4b50334SJilles Tjoelker INTON; 4996ed74a0aSJilles Tjoelker updatecharset(); 500e4b50334SJilles Tjoelker } 501e4b50334SJilles Tjoelker 502e4b50334SJilles Tjoelker /* 503e4b50334SJilles Tjoelker * Undo the effect of bltinlocaleset(). 504e4b50334SJilles Tjoelker */ 505e4b50334SJilles Tjoelker void 506e4b50334SJilles Tjoelker bltinunsetlocale(void) 507e4b50334SJilles Tjoelker { 5088ef0ae8aSJilles Tjoelker int i; 509e4b50334SJilles Tjoelker 510e4b50334SJilles Tjoelker INTOFF; 5118ef0ae8aSJilles Tjoelker if (cmdenviron) for (i = 0; i < cmdenviron->count; i++) { 5128ef0ae8aSJilles Tjoelker if (localevar(cmdenviron->args[i])) { 513e4b50334SJilles Tjoelker setlocale(LC_ALL, ""); 5146ed74a0aSJilles Tjoelker updatecharset(); 515e4b50334SJilles Tjoelker return; 516e4b50334SJilles Tjoelker } 517e4b50334SJilles Tjoelker } 518e4b50334SJilles Tjoelker INTON; 519e4b50334SJilles Tjoelker } 520e4b50334SJilles Tjoelker 5216ed74a0aSJilles Tjoelker /* 5226ed74a0aSJilles Tjoelker * Update the localeisutf8 flag. 5236ed74a0aSJilles Tjoelker */ 5246ed74a0aSJilles Tjoelker void 5256ed74a0aSJilles Tjoelker updatecharset(void) 5266ed74a0aSJilles Tjoelker { 5276ed74a0aSJilles Tjoelker char *charset; 5286ed74a0aSJilles Tjoelker 5296ed74a0aSJilles Tjoelker charset = nl_langinfo(CODESET); 5306ed74a0aSJilles Tjoelker localeisutf8 = !strcmp(charset, "UTF-8"); 5316ed74a0aSJilles Tjoelker } 5324b88c807SRodney W. Grimes 53307eb7033SJilles Tjoelker void 53407eb7033SJilles Tjoelker initcharset(void) 53507eb7033SJilles Tjoelker { 53607eb7033SJilles Tjoelker updatecharset(); 53707eb7033SJilles Tjoelker initial_localeisutf8 = localeisutf8; 53807eb7033SJilles Tjoelker } 53907eb7033SJilles Tjoelker 5404b88c807SRodney W. Grimes /* 5414b88c807SRodney W. Grimes * Generate a list of exported variables. This routine is used to construct 5424b88c807SRodney W. Grimes * the third argument to execve when executing a program. 5434b88c807SRodney W. Grimes */ 5444b88c807SRodney W. Grimes 5454b88c807SRodney W. Grimes char ** 5465134c3f7SWarner Losh environment(void) 5475134c3f7SWarner Losh { 5484b88c807SRodney W. Grimes int nenv; 5494b88c807SRodney W. Grimes struct var **vpp; 5504b88c807SRodney W. Grimes struct var *vp; 5514b88c807SRodney W. Grimes char **env, **ep; 5524b88c807SRodney W. Grimes 5534b88c807SRodney W. Grimes nenv = 0; 5544b88c807SRodney W. Grimes for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) { 5554b88c807SRodney W. Grimes for (vp = *vpp ; vp ; vp = vp->next) 5564b88c807SRodney W. Grimes if (vp->flags & VEXPORT) 5574b88c807SRodney W. Grimes nenv++; 5584b88c807SRodney W. Grimes } 5594b88c807SRodney W. Grimes ep = env = stalloc((nenv + 1) * sizeof *env); 5604b88c807SRodney W. Grimes for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) { 5614b88c807SRodney W. Grimes for (vp = *vpp ; vp ; vp = vp->next) 5624b88c807SRodney W. Grimes if (vp->flags & VEXPORT) 5634b88c807SRodney W. Grimes *ep++ = vp->text; 5644b88c807SRodney W. Grimes } 5654b88c807SRodney W. Grimes *ep = NULL; 5664b88c807SRodney W. Grimes return env; 5674b88c807SRodney W. Grimes } 5684b88c807SRodney W. Grimes 5694b88c807SRodney W. Grimes 57088328642SDavid E. O'Brien static int 571692f35fdSStefan Farfeleder var_compare(const void *a, const void *b) 572692f35fdSStefan Farfeleder { 573692f35fdSStefan Farfeleder const char *const *sa, *const *sb; 574692f35fdSStefan Farfeleder 575692f35fdSStefan Farfeleder sa = a; 576692f35fdSStefan Farfeleder sb = b; 577692f35fdSStefan Farfeleder /* 578692f35fdSStefan Farfeleder * This compares two var=value strings which creates a different 579692f35fdSStefan Farfeleder * order from what you would probably expect. POSIX is somewhat 580692f35fdSStefan Farfeleder * ambiguous on what should be sorted exactly. 581692f35fdSStefan Farfeleder */ 582692f35fdSStefan Farfeleder return strcoll(*sa, *sb); 583692f35fdSStefan Farfeleder } 584692f35fdSStefan Farfeleder 5854b88c807SRodney W. Grimes 5864b88c807SRodney W. Grimes /* 587cff1d849SJilles Tjoelker * Command to list all variables which are set. This is invoked from the 588cff1d849SJilles Tjoelker * set command when it is called without any options or operands. 5894b88c807SRodney W. Grimes */ 5904b88c807SRodney W. Grimes 5914b88c807SRodney W. Grimes int 5925134c3f7SWarner Losh showvarscmd(int argc __unused, char **argv __unused) 593aa9caaf6SPeter Wemm { 5944b88c807SRodney W. Grimes struct var **vpp; 5954b88c807SRodney W. Grimes struct var *vp; 59659258844STim J. Robbins const char *s; 597692f35fdSStefan Farfeleder const char **vars; 598692f35fdSStefan Farfeleder int i, n; 5994b88c807SRodney W. Grimes 600692f35fdSStefan Farfeleder /* 601692f35fdSStefan Farfeleder * POSIX requires us to sort the variables. 602692f35fdSStefan Farfeleder */ 603692f35fdSStefan Farfeleder n = 0; 6044b88c807SRodney W. Grimes for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) { 6054b88c807SRodney W. Grimes for (vp = *vpp; vp; vp = vp->next) { 606692f35fdSStefan Farfeleder if (!(vp->flags & VUNSET)) 607692f35fdSStefan Farfeleder n++; 608692f35fdSStefan Farfeleder } 609692f35fdSStefan Farfeleder } 610692f35fdSStefan Farfeleder 61133233ec7SJilles Tjoelker INTOFF; 612692f35fdSStefan Farfeleder vars = ckmalloc(n * sizeof(*vars)); 613692f35fdSStefan Farfeleder i = 0; 614692f35fdSStefan Farfeleder for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) { 615692f35fdSStefan Farfeleder for (vp = *vpp; vp; vp = vp->next) { 616692f35fdSStefan Farfeleder if (!(vp->flags & VUNSET)) 617692f35fdSStefan Farfeleder vars[i++] = vp->text; 618692f35fdSStefan Farfeleder } 619692f35fdSStefan Farfeleder } 620692f35fdSStefan Farfeleder 621692f35fdSStefan Farfeleder qsort(vars, n, sizeof(*vars), var_compare); 622692f35fdSStefan Farfeleder for (i = 0; i < n; i++) { 623f5f215e2SJilles Tjoelker /* 624f5f215e2SJilles Tjoelker * Skip improper variable names so the output remains usable as 625f5f215e2SJilles Tjoelker * shell input. 626f5f215e2SJilles Tjoelker */ 627f5f215e2SJilles Tjoelker if (!isassignment(vars[i])) 628f5f215e2SJilles Tjoelker continue; 629aeb5d065SJilles Tjoelker s = strchr(vars[i], '='); 630aeb5d065SJilles Tjoelker s++; 631aeb5d065SJilles Tjoelker outbin(vars[i], s - vars[i], out1); 632aeb5d065SJilles Tjoelker out1qstr(s); 63359258844STim J. Robbins out1c('\n'); 6344b88c807SRodney W. Grimes } 635692f35fdSStefan Farfeleder ckfree(vars); 63633233ec7SJilles Tjoelker INTON; 637692f35fdSStefan Farfeleder 6384b88c807SRodney W. Grimes return 0; 6394b88c807SRodney W. Grimes } 6404b88c807SRodney W. Grimes 6414b88c807SRodney W. Grimes 6424b88c807SRodney W. Grimes 6434b88c807SRodney W. Grimes /* 6444b88c807SRodney W. Grimes * The export and readonly commands. 6454b88c807SRodney W. Grimes */ 6464b88c807SRodney W. Grimes 6474b88c807SRodney W. Grimes int 6487cbda738SJilles Tjoelker exportcmd(int argc __unused, char **argv) 649aa9caaf6SPeter Wemm { 6504b88c807SRodney W. Grimes struct var **vpp; 6514b88c807SRodney W. Grimes struct var *vp; 6527cbda738SJilles Tjoelker char **ap; 6534b88c807SRodney W. Grimes char *name; 6544b88c807SRodney W. Grimes char *p; 65545086f8cSTim J. Robbins char *cmdname; 65645086f8cSTim J. Robbins int ch, values; 6574b88c807SRodney W. Grimes int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT; 6584b88c807SRodney W. Grimes 65945086f8cSTim J. Robbins cmdname = argv[0]; 66045086f8cSTim J. Robbins values = 0; 6617cbda738SJilles Tjoelker while ((ch = nextopt("p")) != '\0') { 66245086f8cSTim J. Robbins switch (ch) { 66345086f8cSTim J. Robbins case 'p': 66445086f8cSTim J. Robbins values = 1; 66545086f8cSTim J. Robbins break; 66645086f8cSTim J. Robbins } 66745086f8cSTim J. Robbins } 66845086f8cSTim J. Robbins 6697cbda738SJilles Tjoelker if (values && *argptr != NULL) 6703fda45ecSStefan Farfeleder error("-p requires no arguments"); 6717cbda738SJilles Tjoelker if (*argptr != NULL) { 6727cbda738SJilles Tjoelker for (ap = argptr; (name = *ap) != NULL; ap++) { 6734b88c807SRodney W. Grimes if ((p = strchr(name, '=')) != NULL) { 6744b88c807SRodney W. Grimes p++; 6754b88c807SRodney W. Grimes } else { 6763a99ed46SJilles Tjoelker vp = find_var(name, NULL, NULL); 6773a99ed46SJilles Tjoelker if (vp != NULL) { 6784b88c807SRodney W. Grimes vp->flags |= flag; 679ba726b8aSAndrey A. Chernov if ((vp->flags & VEXPORT) && localevar(vp->text)) { 6805b78c6c2SSean Farley change_env(vp->text, 1); 681ba726b8aSAndrey A. Chernov (void) setlocale(LC_ALL, ""); 6826ed74a0aSJilles Tjoelker updatecharset(); 683ba726b8aSAndrey A. Chernov } 6843a99ed46SJilles Tjoelker continue; 6854b88c807SRodney W. Grimes } 6864b88c807SRodney W. Grimes } 6874b88c807SRodney W. Grimes setvar(name, p, flag); 6884b88c807SRodney W. Grimes } 6894b88c807SRodney W. Grimes } else { 6904b88c807SRodney W. Grimes for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) { 6914b88c807SRodney W. Grimes for (vp = *vpp ; vp ; vp = vp->next) { 6924b88c807SRodney W. Grimes if (vp->flags & flag) { 69345086f8cSTim J. Robbins if (values) { 694f5f215e2SJilles Tjoelker /* 695f5f215e2SJilles Tjoelker * Skip improper variable names 696f5f215e2SJilles Tjoelker * so the output remains usable 697f5f215e2SJilles Tjoelker * as shell input. 698f5f215e2SJilles Tjoelker */ 699f5f215e2SJilles Tjoelker if (!isassignment(vp->text)) 700f5f215e2SJilles Tjoelker continue; 70145086f8cSTim J. Robbins out1str(cmdname); 70245086f8cSTim J. Robbins out1c(' '); 70345086f8cSTim J. Robbins } 70445086f8cSTim J. Robbins if (values && !(vp->flags & VUNSET)) { 705258ef734SJilles Tjoelker outbin(vp->text, 706258ef734SJilles Tjoelker vp->name_len + 1, out1); 707258ef734SJilles Tjoelker out1qstr(vp->text + 708258ef734SJilles Tjoelker vp->name_len + 1); 709aeb5d065SJilles Tjoelker } else 710258ef734SJilles Tjoelker outbin(vp->text, vp->name_len, 711aeb5d065SJilles Tjoelker out1); 7124b88c807SRodney W. Grimes out1c('\n'); 7134b88c807SRodney W. Grimes } 7144b88c807SRodney W. Grimes } 7154b88c807SRodney W. Grimes } 7164b88c807SRodney W. Grimes } 7174b88c807SRodney W. Grimes return 0; 7184b88c807SRodney W. Grimes } 7194b88c807SRodney W. Grimes 7204b88c807SRodney W. Grimes 7214b88c807SRodney W. Grimes /* 7224b88c807SRodney W. Grimes * The "local" command. 7234b88c807SRodney W. Grimes */ 7244b88c807SRodney W. Grimes 725aa9caaf6SPeter Wemm int 7265134c3f7SWarner Losh localcmd(int argc __unused, char **argv __unused) 727aa9caaf6SPeter Wemm { 7284b88c807SRodney W. Grimes char *name; 7294b88c807SRodney W. Grimes 730056fd329SJilles Tjoelker nextopt(""); 7314b88c807SRodney W. Grimes if (! in_function()) 7324b88c807SRodney W. Grimes error("Not in a function"); 7334b88c807SRodney W. Grimes while ((name = *argptr++) != NULL) { 7344b88c807SRodney W. Grimes mklocal(name); 7354b88c807SRodney W. Grimes } 7364b88c807SRodney W. Grimes return 0; 7374b88c807SRodney W. Grimes } 7384b88c807SRodney W. Grimes 7394b88c807SRodney W. Grimes 7404b88c807SRodney W. Grimes /* 7414b88c807SRodney W. Grimes * Make a variable a local variable. When a variable is made local, it's 7424b88c807SRodney W. Grimes * value and flags are saved in a localvar structure. The saved values 7434b88c807SRodney W. Grimes * will be restored when the shell function returns. We handle the name 7444b88c807SRodney W. Grimes * "-" as a special case. 7454b88c807SRodney W. Grimes */ 7464b88c807SRodney W. Grimes 7474b88c807SRodney W. Grimes void 7485134c3f7SWarner Losh mklocal(char *name) 7494b88c807SRodney W. Grimes { 7504b88c807SRodney W. Grimes struct localvar *lvp; 7514b88c807SRodney W. Grimes struct var **vpp; 7524b88c807SRodney W. Grimes struct var *vp; 7534b88c807SRodney W. Grimes 7544b88c807SRodney W. Grimes INTOFF; 7554b88c807SRodney W. Grimes lvp = ckmalloc(sizeof (struct localvar)); 7564b88c807SRodney W. Grimes if (name[0] == '-' && name[1] == '\0') { 7573da40d4aSJilles Tjoelker lvp->text = ckmalloc(sizeof optval); 7583da40d4aSJilles Tjoelker memcpy(lvp->text, optval, sizeof optval); 7594b88c807SRodney W. Grimes vp = NULL; 7604b88c807SRodney W. Grimes } else { 7613a99ed46SJilles Tjoelker vp = find_var(name, &vpp, NULL); 7624b88c807SRodney W. Grimes if (vp == NULL) { 7634b88c807SRodney W. Grimes if (strchr(name, '=')) 764c543e1aeSJilles Tjoelker setvareq(savestr(name), VSTRFIXED | VNOLOCAL); 7654b88c807SRodney W. Grimes else 766c543e1aeSJilles Tjoelker setvar(name, NULL, VSTRFIXED | VNOLOCAL); 7674b88c807SRodney W. Grimes vp = *vpp; /* the new variable */ 7684b88c807SRodney W. Grimes lvp->text = NULL; 7694b88c807SRodney W. Grimes lvp->flags = VUNSET; 7704b88c807SRodney W. Grimes } else { 7714b88c807SRodney W. Grimes lvp->text = vp->text; 7724b88c807SRodney W. Grimes lvp->flags = vp->flags; 7734b88c807SRodney W. Grimes vp->flags |= VSTRFIXED|VTEXTFIXED; 7743a99ed46SJilles Tjoelker if (name[vp->name_len] == '=') 775c543e1aeSJilles Tjoelker setvareq(savestr(name), VNOLOCAL); 7764b88c807SRodney W. Grimes } 7774b88c807SRodney W. Grimes } 7784b88c807SRodney W. Grimes lvp->vp = vp; 7794b88c807SRodney W. Grimes lvp->next = localvars; 7804b88c807SRodney W. Grimes localvars = lvp; 7814b88c807SRodney W. Grimes INTON; 7824b88c807SRodney W. Grimes } 7834b88c807SRodney W. Grimes 7844b88c807SRodney W. Grimes 7854b88c807SRodney W. Grimes /* 7864b88c807SRodney W. Grimes * Called after a function returns. 7874b88c807SRodney W. Grimes */ 7884b88c807SRodney W. Grimes 7894b88c807SRodney W. Grimes void 7905134c3f7SWarner Losh poplocalvars(void) 7915134c3f7SWarner Losh { 7924b88c807SRodney W. Grimes struct localvar *lvp; 7934b88c807SRodney W. Grimes struct var *vp; 794cf45f124SJilles Tjoelker int islocalevar; 7954b88c807SRodney W. Grimes 7961632bf1aSJilles Tjoelker INTOFF; 7974b88c807SRodney W. Grimes while ((lvp = localvars) != NULL) { 7984b88c807SRodney W. Grimes localvars = lvp->next; 7994b88c807SRodney W. Grimes vp = lvp->vp; 8004b88c807SRodney W. Grimes if (vp == NULL) { /* $- saved */ 8013da40d4aSJilles Tjoelker memcpy(optval, lvp->text, sizeof optval); 8024b88c807SRodney W. Grimes ckfree(lvp->text); 8034a7b1013SJilles Tjoelker optschanged(); 8044b88c807SRodney W. Grimes } else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) { 805*a9bdd616SJilles Tjoelker vp->flags &= ~VREADONLY; 8064b88c807SRodney W. Grimes (void)unsetvar(vp->text); 8074b88c807SRodney W. Grimes } else { 808cf45f124SJilles Tjoelker islocalevar = (vp->flags | lvp->flags) & VEXPORT && 809cf45f124SJilles Tjoelker localevar(lvp->text); 8104b88c807SRodney W. Grimes if ((vp->flags & VTEXTFIXED) == 0) 8114b88c807SRodney W. Grimes ckfree(vp->text); 8124b88c807SRodney W. Grimes vp->flags = lvp->flags; 8134b88c807SRodney W. Grimes vp->text = lvp->text; 814cf45f124SJilles Tjoelker if (vp->func) 815cf45f124SJilles Tjoelker (*vp->func)(vp->text + vp->name_len + 1); 816cf45f124SJilles Tjoelker if (islocalevar) { 817cf45f124SJilles Tjoelker change_env(vp->text, vp->flags & VEXPORT && 818cf45f124SJilles Tjoelker (vp->flags & VUNSET) == 0); 819cf45f124SJilles Tjoelker setlocale(LC_ALL, ""); 820cf45f124SJilles Tjoelker updatecharset(); 821cf45f124SJilles Tjoelker } 8224b88c807SRodney W. Grimes } 8234b88c807SRodney W. Grimes ckfree(lvp); 8244b88c807SRodney W. Grimes } 8251632bf1aSJilles Tjoelker INTON; 8264b88c807SRodney W. Grimes } 8274b88c807SRodney W. Grimes 8284b88c807SRodney W. Grimes 829aa9caaf6SPeter Wemm int 8305134c3f7SWarner Losh setvarcmd(int argc, char **argv) 831aa9caaf6SPeter Wemm { 8324b88c807SRodney W. Grimes if (argc <= 2) 8334b88c807SRodney W. Grimes return unsetcmd(argc, argv); 8344b88c807SRodney W. Grimes else if (argc == 3) 8354b88c807SRodney W. Grimes setvar(argv[1], argv[2], 0); 8364b88c807SRodney W. Grimes else 837274110dfSJilles Tjoelker error("too many arguments"); 8384b88c807SRodney W. Grimes return 0; 8394b88c807SRodney W. Grimes } 8404b88c807SRodney W. Grimes 8414b88c807SRodney W. Grimes 8424b88c807SRodney W. Grimes /* 843cff1d849SJilles Tjoelker * The unset builtin command. 8444b88c807SRodney W. Grimes */ 8454b88c807SRodney W. Grimes 846aa9caaf6SPeter Wemm int 8475134c3f7SWarner Losh unsetcmd(int argc __unused, char **argv __unused) 848aa9caaf6SPeter Wemm { 8494b88c807SRodney W. Grimes char **ap; 8504b88c807SRodney W. Grimes int i; 8514b88c807SRodney W. Grimes int flg_func = 0; 8524b88c807SRodney W. Grimes int flg_var = 0; 8534b88c807SRodney W. Grimes int ret = 0; 8544b88c807SRodney W. Grimes 8554b88c807SRodney W. Grimes while ((i = nextopt("vf")) != '\0') { 8564b88c807SRodney W. Grimes if (i == 'f') 8574b88c807SRodney W. Grimes flg_func = 1; 8584b88c807SRodney W. Grimes else 8594b88c807SRodney W. Grimes flg_var = 1; 8604b88c807SRodney W. Grimes } 8614b88c807SRodney W. Grimes if (flg_func == 0 && flg_var == 0) 8624b88c807SRodney W. Grimes flg_var = 1; 8634b88c807SRodney W. Grimes 8641632bf1aSJilles Tjoelker INTOFF; 8654b88c807SRodney W. Grimes for (ap = argptr; *ap ; ap++) { 8664b88c807SRodney W. Grimes if (flg_func) 8674b88c807SRodney W. Grimes ret |= unsetfunc(*ap); 8684b88c807SRodney W. Grimes if (flg_var) 8694b88c807SRodney W. Grimes ret |= unsetvar(*ap); 8704b88c807SRodney W. Grimes } 8711632bf1aSJilles Tjoelker INTON; 8724b88c807SRodney W. Grimes return ret; 8734b88c807SRodney W. Grimes } 8744b88c807SRodney W. Grimes 8754b88c807SRodney W. Grimes 8764b88c807SRodney W. Grimes /* 8774b88c807SRodney W. Grimes * Unset the specified variable. 8781632bf1aSJilles Tjoelker * Called with interrupts off. 8794b88c807SRodney W. Grimes */ 8804b88c807SRodney W. Grimes 881ab0a2172SSteve Price int 8822cac6e36SJilles Tjoelker unsetvar(const char *s) 8834b88c807SRodney W. Grimes { 8844b88c807SRodney W. Grimes struct var **vpp; 8854b88c807SRodney W. Grimes struct var *vp; 8864b88c807SRodney W. Grimes 8873a99ed46SJilles Tjoelker vp = find_var(s, &vpp, NULL); 8883a99ed46SJilles Tjoelker if (vp == NULL) 8893a99ed46SJilles Tjoelker return (0); 8904b88c807SRodney W. Grimes if (vp->flags & VREADONLY) 8914b88c807SRodney W. Grimes return (1); 8923a99ed46SJilles Tjoelker if (vp->text[vp->name_len + 1] != '\0') 893781bfb5aSJilles Tjoelker setvar(s, "", 0); 894ba726b8aSAndrey A. Chernov if ((vp->flags & VEXPORT) && localevar(vp->text)) { 8955b78c6c2SSean Farley change_env(s, 0); 896ba726b8aSAndrey A. Chernov setlocale(LC_ALL, ""); 8976ed74a0aSJilles Tjoelker updatecharset(); 898ba726b8aSAndrey A. Chernov } 8994b88c807SRodney W. Grimes vp->flags &= ~VEXPORT; 9004b88c807SRodney W. Grimes vp->flags |= VUNSET; 9014b88c807SRodney W. Grimes if ((vp->flags & VSTRFIXED) == 0) { 9024b88c807SRodney W. Grimes if ((vp->flags & VTEXTFIXED) == 0) 9034b88c807SRodney W. Grimes ckfree(vp->text); 9044b88c807SRodney W. Grimes *vpp = vp->next; 9054b88c807SRodney W. Grimes ckfree(vp); 9064b88c807SRodney W. Grimes } 9074b88c807SRodney W. Grimes return (0); 9084b88c807SRodney W. Grimes } 9094b88c807SRodney W. Grimes 9104b88c807SRodney W. Grimes 9114b88c807SRodney W. Grimes 9124b88c807SRodney W. Grimes /* 9136c7d8328SEitan Adler * Returns true if the two strings specify the same variable. The first 9144b88c807SRodney W. Grimes * variable name is terminated by '='; the second may be terminated by 9154b88c807SRodney W. Grimes * either '=' or '\0'. 9164b88c807SRodney W. Grimes */ 9174b88c807SRodney W. Grimes 91888328642SDavid E. O'Brien static int 9192cac6e36SJilles Tjoelker varequal(const char *p, const char *q) 9204b88c807SRodney W. Grimes { 9214b88c807SRodney W. Grimes while (*p == *q++) { 9224b88c807SRodney W. Grimes if (*p++ == '=') 9234b88c807SRodney W. Grimes return 1; 9244b88c807SRodney W. Grimes } 9254b88c807SRodney W. Grimes if (*p == '=' && *(q - 1) == '\0') 9264b88c807SRodney W. Grimes return 1; 9274b88c807SRodney W. Grimes return 0; 9284b88c807SRodney W. Grimes } 9293a99ed46SJilles Tjoelker 9303a99ed46SJilles Tjoelker /* 9313a99ed46SJilles Tjoelker * Search for a variable. 9323a99ed46SJilles Tjoelker * 'name' may be terminated by '=' or a NUL. 9333a99ed46SJilles Tjoelker * vppp is set to the pointer to vp, or the list head if vp isn't found 9346c7d8328SEitan Adler * lenp is set to the number of characters in 'name' 9353a99ed46SJilles Tjoelker */ 9363a99ed46SJilles Tjoelker 9373a99ed46SJilles Tjoelker static struct var * 9383a99ed46SJilles Tjoelker find_var(const char *name, struct var ***vppp, int *lenp) 9393a99ed46SJilles Tjoelker { 9403a99ed46SJilles Tjoelker unsigned int hashval; 9413a99ed46SJilles Tjoelker int len; 9423a99ed46SJilles Tjoelker struct var *vp, **vpp; 9433a99ed46SJilles Tjoelker const char *p = name; 9443a99ed46SJilles Tjoelker 9453a99ed46SJilles Tjoelker hashval = 0; 9463a99ed46SJilles Tjoelker while (*p && *p != '=') 9473a99ed46SJilles Tjoelker hashval = 2 * hashval + (unsigned char)*p++; 9483a99ed46SJilles Tjoelker len = p - name; 9493a99ed46SJilles Tjoelker 9503a99ed46SJilles Tjoelker if (lenp) 9513a99ed46SJilles Tjoelker *lenp = len; 9523a99ed46SJilles Tjoelker vpp = &vartab[hashval % VTABSIZE]; 9533a99ed46SJilles Tjoelker if (vppp) 9543a99ed46SJilles Tjoelker *vppp = vpp; 9553a99ed46SJilles Tjoelker 9563a99ed46SJilles Tjoelker for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) { 9573a99ed46SJilles Tjoelker if (vp->name_len != len) 9583a99ed46SJilles Tjoelker continue; 9593a99ed46SJilles Tjoelker if (memcmp(vp->text, name, len) != 0) 9603a99ed46SJilles Tjoelker continue; 9613a99ed46SJilles Tjoelker if (vppp) 9623a99ed46SJilles Tjoelker *vppp = vpp; 9633a99ed46SJilles Tjoelker return vp; 9643a99ed46SJilles Tjoelker } 9653a99ed46SJilles Tjoelker return NULL; 9663a99ed46SJilles Tjoelker } 967