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. 16fbbd9655SWarner Losh * 3. 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); 1897cca93e6SJilles Tjoelker setvareq_const("IFS= \t\n", 0); 1904b88c807SRodney W. Grimes } 1914b88c807SRodney W. Grimes 1924b88c807SRodney W. Grimes /* 193ab0a2172SSteve Price * Safe version of setvar, returns 1 on success 0 on failure. 194ab0a2172SSteve Price */ 195ab0a2172SSteve Price 196ab0a2172SSteve Price int 1972cac6e36SJilles Tjoelker setvarsafe(const char *name, const char *val, int flags) 198ab0a2172SSteve Price { 199ab0a2172SSteve Price struct jmploc jmploc; 200224fbf9fSJilles Tjoelker struct jmploc *const savehandler = handler; 201ab0a2172SSteve Price int err = 0; 2029922c6d2SJilles Tjoelker int inton; 203ab0a2172SSteve Price 2049922c6d2SJilles Tjoelker inton = is_int_on(); 205ab0a2172SSteve Price if (setjmp(jmploc.loc)) 206ab0a2172SSteve Price err = 1; 207ab0a2172SSteve Price else { 208ab0a2172SSteve Price handler = &jmploc; 209ab0a2172SSteve Price setvar(name, val, flags); 210ab0a2172SSteve Price } 211ab0a2172SSteve Price handler = savehandler; 2129922c6d2SJilles Tjoelker SETINTON(inton); 213ab0a2172SSteve Price return err; 214ab0a2172SSteve Price } 215ab0a2172SSteve Price 216ab0a2172SSteve Price /* 217b3decf89SJens Schweikhardt * Set the value of a variable. The flags argument is stored with the 2184b88c807SRodney W. Grimes * flags of the variable. If val is NULL, the variable is unset. 2194b88c807SRodney W. Grimes */ 2204b88c807SRodney W. Grimes 2214b88c807SRodney W. Grimes void 2222cac6e36SJilles Tjoelker setvar(const char *name, const char *val, int flags) 2234b88c807SRodney W. Grimes { 2242cac6e36SJilles Tjoelker const char *p; 225670dd3f0SJilles Tjoelker size_t len; 226670dd3f0SJilles Tjoelker size_t namelen; 227670dd3f0SJilles Tjoelker size_t vallen; 2284b88c807SRodney W. Grimes char *nameeq; 2294b88c807SRodney W. Grimes int isbad; 2304b88c807SRodney W. Grimes 2314b88c807SRodney W. Grimes isbad = 0; 2324b88c807SRodney W. Grimes p = name; 233ba726b8aSAndrey A. Chernov if (! is_name(*p)) 2344b88c807SRodney W. Grimes isbad = 1; 235ba726b8aSAndrey A. Chernov p++; 2364b88c807SRodney W. Grimes for (;;) { 2374b88c807SRodney W. Grimes if (! is_in_name(*p)) { 2384b88c807SRodney W. Grimes if (*p == '\0' || *p == '=') 2394b88c807SRodney W. Grimes break; 2404b88c807SRodney W. Grimes isbad = 1; 2414b88c807SRodney W. Grimes } 2424b88c807SRodney W. Grimes p++; 2434b88c807SRodney W. Grimes } 2444b88c807SRodney W. Grimes namelen = p - name; 2454b88c807SRodney W. Grimes if (isbad) 246670dd3f0SJilles Tjoelker error("%.*s: bad variable name", (int)namelen, name); 2474b88c807SRodney W. Grimes len = namelen + 2; /* 2 is space for '=' and '\0' */ 2484b88c807SRodney W. Grimes if (val == NULL) { 2494b88c807SRodney W. Grimes flags |= VUNSET; 250670dd3f0SJilles Tjoelker vallen = 0; 2514b88c807SRodney W. Grimes } else { 252670dd3f0SJilles Tjoelker vallen = strlen(val); 253670dd3f0SJilles Tjoelker len += vallen; 2544b88c807SRodney W. Grimes } 2551632bf1aSJilles Tjoelker INTOFF; 2562cac6e36SJilles Tjoelker nameeq = ckmalloc(len); 2572cac6e36SJilles Tjoelker memcpy(nameeq, name, namelen); 2582cac6e36SJilles Tjoelker nameeq[namelen] = '='; 2594b88c807SRodney W. Grimes if (val) 260670dd3f0SJilles Tjoelker memcpy(nameeq + namelen + 1, val, vallen + 1); 2612cac6e36SJilles Tjoelker else 2622cac6e36SJilles Tjoelker nameeq[namelen + 1] = '\0'; 2634b88c807SRodney W. Grimes setvareq(nameeq, flags); 2641632bf1aSJilles Tjoelker INTON; 2654b88c807SRodney W. Grimes } 2664b88c807SRodney W. Grimes 26788328642SDavid E. O'Brien static int 2682cac6e36SJilles Tjoelker localevar(const char *s) 269ba726b8aSAndrey A. Chernov { 270e4b50334SJilles Tjoelker const char *const *ss; 2714b88c807SRodney W. Grimes 272ba726b8aSAndrey A. Chernov if (*s != 'L') 273ba726b8aSAndrey A. Chernov return 0; 274ba726b8aSAndrey A. Chernov if (varequal(s + 1, "ANG")) 275ba726b8aSAndrey A. Chernov return 1; 276ba726b8aSAndrey A. Chernov if (strncmp(s + 1, "C_", 2) != 0) 277ba726b8aSAndrey A. Chernov return 0; 278e4b50334SJilles Tjoelker if (varequal(s + 3, "ALL")) 279e4b50334SJilles Tjoelker return 1; 280e4b50334SJilles Tjoelker for (ss = locale_names; *ss ; ss++) 281e4b50334SJilles Tjoelker if (varequal(s + 3, *ss + 3)) 282ba726b8aSAndrey A. Chernov return 1; 283ba726b8aSAndrey A. Chernov return 0; 284ba726b8aSAndrey A. Chernov } 2854b88c807SRodney W. Grimes 2865b78c6c2SSean Farley 2875b78c6c2SSean Farley /* 2885b78c6c2SSean Farley * Sets/unsets an environment variable from a pointer that may actually be a 2895b78c6c2SSean Farley * pointer into environ where the string should not be manipulated. 2905b78c6c2SSean Farley */ 29188328642SDavid E. O'Brien static void 2922cac6e36SJilles Tjoelker change_env(const char *s, int set) 2935b78c6c2SSean Farley { 2945b78c6c2SSean Farley char *eqp; 2955b78c6c2SSean Farley char *ss; 2965b78c6c2SSean Farley 2971632bf1aSJilles Tjoelker INTOFF; 2985b78c6c2SSean Farley ss = savestr(s); 2995b78c6c2SSean Farley if ((eqp = strchr(ss, '=')) != NULL) 3005b78c6c2SSean Farley *eqp = '\0'; 3015b78c6c2SSean Farley if (set && eqp != NULL) 3025b78c6c2SSean Farley (void) setenv(ss, eqp + 1, 1); 3035b78c6c2SSean Farley else 3045b78c6c2SSean Farley (void) unsetenv(ss); 3055b78c6c2SSean Farley ckfree(ss); 3061632bf1aSJilles Tjoelker INTON; 3075b78c6c2SSean Farley 3085b78c6c2SSean Farley return; 3095b78c6c2SSean Farley } 3105b78c6c2SSean Farley 3115b78c6c2SSean Farley 3124b88c807SRodney W. Grimes /* 3134b88c807SRodney W. Grimes * Same as setvar except that the variable and value are passed in 3144b88c807SRodney W. Grimes * the first argument as name=value. Since the first argument will 3154b88c807SRodney W. Grimes * be actually stored in the table, it should not be a string that 3164b88c807SRodney W. Grimes * will go away. 3174b88c807SRodney W. Grimes */ 3184b88c807SRodney W. Grimes 3194b88c807SRodney W. Grimes void 3205134c3f7SWarner Losh setvareq(char *s, int flags) 3214b88c807SRodney W. Grimes { 3224b88c807SRodney W. Grimes struct var *vp, **vpp; 3233a99ed46SJilles Tjoelker int nlen; 3244b88c807SRodney W. Grimes 325b8ec435eSMartin Cracauer if (aflag) 326b8ec435eSMartin Cracauer flags |= VEXPORT; 327c543e1aeSJilles Tjoelker if (forcelocal && !(flags & (VNOSET | VNOLOCAL))) 328c543e1aeSJilles Tjoelker mklocal(s); 3293a99ed46SJilles Tjoelker vp = find_var(s, &vpp, &nlen); 3303a99ed46SJilles Tjoelker if (vp != NULL) { 33189d4f883SJilles Tjoelker if (vp->flags & VREADONLY) { 33289d4f883SJilles Tjoelker if ((flags & (VTEXTFIXED|VSTACK)) == 0) 33389d4f883SJilles Tjoelker ckfree(s); 334d41b2be1SJilles Tjoelker error("%.*s: is read only", vp->name_len, vp->text); 33589d4f883SJilles Tjoelker } 336d73dba75SJilles Tjoelker if (flags & VNOSET) { 337d73dba75SJilles Tjoelker if ((flags & (VTEXTFIXED|VSTACK)) == 0) 338d73dba75SJilles Tjoelker ckfree(s); 339850460c0SJilles Tjoelker return; 340d73dba75SJilles Tjoelker } 3414b88c807SRodney W. Grimes INTOFF; 342ab0a2172SSteve Price 343ab0a2172SSteve Price if (vp->func && (flags & VNOFUNC) == 0) 3443a99ed46SJilles Tjoelker (*vp->func)(s + vp->name_len + 1); 345ab0a2172SSteve Price 3464b88c807SRodney W. Grimes if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0) 3474b88c807SRodney W. Grimes ckfree(vp->text); 348ab0a2172SSteve Price 3494b88c807SRodney W. Grimes vp->flags &= ~(VTEXTFIXED|VSTACK|VUNSET); 3504b88c807SRodney W. Grimes vp->flags |= flags; 3514b88c807SRodney W. Grimes vp->text = s; 352ab0a2172SSteve Price 353ab0a2172SSteve Price /* 354ab0a2172SSteve Price * We could roll this to a function, to handle it as 355ab0a2172SSteve Price * a regular variable function callback, but why bother? 35657063576SJilles Tjoelker * 35757063576SJilles Tjoelker * Note: this assumes iflag is not set to 1 initially. 35859e0cc8eSJilles Tjoelker * As part of initvar(), this is called before arguments 35957063576SJilles Tjoelker * are looked at. 360ab0a2172SSteve Price */ 36157063576SJilles Tjoelker if ((vp == &vmpath || (vp == &vmail && ! mpathset())) && 36257063576SJilles Tjoelker iflag == 1) 3634b88c807SRodney W. Grimes chkmail(1); 364ba726b8aSAndrey A. Chernov if ((vp->flags & VEXPORT) && localevar(s)) { 3655b78c6c2SSean Farley change_env(s, 1); 366ba726b8aSAndrey A. Chernov (void) setlocale(LC_ALL, ""); 3676ed74a0aSJilles Tjoelker updatecharset(); 368ba726b8aSAndrey A. Chernov } 3694b88c807SRodney W. Grimes INTON; 3704b88c807SRodney W. Grimes return; 3714b88c807SRodney W. Grimes } 3724b88c807SRodney W. Grimes /* not found */ 373d73dba75SJilles Tjoelker if (flags & VNOSET) { 374d73dba75SJilles Tjoelker if ((flags & (VTEXTFIXED|VSTACK)) == 0) 375d73dba75SJilles Tjoelker ckfree(s); 376850460c0SJilles Tjoelker return; 377d73dba75SJilles Tjoelker } 3781632bf1aSJilles Tjoelker INTOFF; 3794b88c807SRodney W. Grimes vp = ckmalloc(sizeof (*vp)); 3804b88c807SRodney W. Grimes vp->flags = flags; 3814b88c807SRodney W. Grimes vp->text = s; 3823a99ed46SJilles Tjoelker vp->name_len = nlen; 3834b88c807SRodney W. Grimes vp->next = *vpp; 384ab0a2172SSteve Price vp->func = NULL; 3854b88c807SRodney W. Grimes *vpp = vp; 386ba726b8aSAndrey A. Chernov if ((vp->flags & VEXPORT) && localevar(s)) { 3875b78c6c2SSean Farley change_env(s, 1); 388ba726b8aSAndrey A. Chernov (void) setlocale(LC_ALL, ""); 3896ed74a0aSJilles Tjoelker updatecharset(); 390ba726b8aSAndrey A. Chernov } 391ba726b8aSAndrey A. Chernov INTON; 3924b88c807SRodney W. Grimes } 3934b88c807SRodney W. Grimes 3944b88c807SRodney W. Grimes 39522afca9bSJilles Tjoelker static void 39622afca9bSJilles Tjoelker setvareq_const(const char *s, int flags) 39722afca9bSJilles Tjoelker { 39822afca9bSJilles Tjoelker setvareq(__DECONST(char *, s), flags | VTEXTFIXED); 39922afca9bSJilles Tjoelker } 40022afca9bSJilles Tjoelker 4014b88c807SRodney W. Grimes 4024b88c807SRodney W. Grimes /* 4034b88c807SRodney W. Grimes * Process a linked list of variable assignments. 4044b88c807SRodney W. Grimes */ 4054b88c807SRodney W. Grimes 4064b88c807SRodney W. Grimes void 4078ef0ae8aSJilles Tjoelker listsetvar(struct arglist *list, int flags) 4084b88c807SRodney W. Grimes { 4098ef0ae8aSJilles Tjoelker int i; 4104b88c807SRodney W. Grimes 4114b88c807SRodney W. Grimes INTOFF; 4128ef0ae8aSJilles Tjoelker for (i = 0; i < list->count; i++) 4138ef0ae8aSJilles Tjoelker setvareq(savestr(list->args[i]), flags); 4144b88c807SRodney W. Grimes INTON; 4154b88c807SRodney W. Grimes } 4164b88c807SRodney W. Grimes 4174b88c807SRodney W. Grimes 4184b88c807SRodney W. Grimes 4194b88c807SRodney W. Grimes /* 4204b88c807SRodney W. Grimes * Find the value of a variable. Returns NULL if not set. 4214b88c807SRodney W. Grimes */ 4224b88c807SRodney W. Grimes 4234b88c807SRodney W. Grimes char * 4242cac6e36SJilles Tjoelker lookupvar(const char *name) 4254b88c807SRodney W. Grimes { 4264b88c807SRodney W. Grimes struct var *v; 4274b88c807SRodney W. Grimes 4283a99ed46SJilles Tjoelker v = find_var(name, NULL, NULL); 4293a99ed46SJilles Tjoelker if (v == NULL || v->flags & VUNSET) 4304b88c807SRodney W. Grimes return NULL; 4313a99ed46SJilles Tjoelker return v->text + v->name_len + 1; 4324b88c807SRodney W. Grimes } 4334b88c807SRodney W. Grimes 4344b88c807SRodney W. Grimes 4354b88c807SRodney W. Grimes 4364b88c807SRodney W. Grimes /* 4374b88c807SRodney W. Grimes * Search the environment of a builtin command. If the second argument 4384b88c807SRodney W. Grimes * is nonzero, return the value of a variable even if it hasn't been 4394b88c807SRodney W. Grimes * exported. 4404b88c807SRodney W. Grimes */ 4414b88c807SRodney W. Grimes 4424b88c807SRodney W. Grimes char * 4432cac6e36SJilles Tjoelker bltinlookup(const char *name, int doall) 4444b88c807SRodney W. Grimes { 4454b88c807SRodney W. Grimes struct var *v; 446011d162dSJilles Tjoelker char *result; 4478ef0ae8aSJilles Tjoelker int i; 4484b88c807SRodney W. Grimes 449011d162dSJilles Tjoelker result = NULL; 4508ef0ae8aSJilles Tjoelker if (cmdenviron) for (i = 0; i < cmdenviron->count; i++) { 4518ef0ae8aSJilles Tjoelker if (varequal(cmdenviron->args[i], name)) 4528ef0ae8aSJilles Tjoelker result = strchr(cmdenviron->args[i], '=') + 1; 4534b88c807SRodney W. Grimes } 454011d162dSJilles Tjoelker if (result != NULL) 455011d162dSJilles Tjoelker return result; 4563a99ed46SJilles Tjoelker 4573a99ed46SJilles Tjoelker v = find_var(name, NULL, NULL); 4583a99ed46SJilles Tjoelker if (v == NULL || v->flags & VUNSET || 4593a99ed46SJilles Tjoelker (!doall && (v->flags & VEXPORT) == 0)) 4604b88c807SRodney W. Grimes return NULL; 4613a99ed46SJilles Tjoelker return v->text + v->name_len + 1; 4624b88c807SRodney W. Grimes } 4634b88c807SRodney W. Grimes 4644b88c807SRodney W. Grimes 465e4b50334SJilles Tjoelker /* 466e4b50334SJilles Tjoelker * Set up locale for a builtin (LANG/LC_* assignments). 467e4b50334SJilles Tjoelker */ 468e4b50334SJilles Tjoelker void 469e4b50334SJilles Tjoelker bltinsetlocale(void) 470e4b50334SJilles Tjoelker { 471e4b50334SJilles Tjoelker int act = 0; 472e4b50334SJilles Tjoelker char *loc, *locdef; 473e4b50334SJilles Tjoelker int i; 474e4b50334SJilles Tjoelker 4758ef0ae8aSJilles Tjoelker if (cmdenviron) for (i = 0; i < cmdenviron->count; i++) { 4768ef0ae8aSJilles Tjoelker if (localevar(cmdenviron->args[i])) { 477e4b50334SJilles Tjoelker act = 1; 478e4b50334SJilles Tjoelker break; 479e4b50334SJilles Tjoelker } 480e4b50334SJilles Tjoelker } 481e4b50334SJilles Tjoelker if (!act) 482e4b50334SJilles Tjoelker return; 483e4b50334SJilles Tjoelker loc = bltinlookup("LC_ALL", 0); 484e4b50334SJilles Tjoelker INTOFF; 485e4b50334SJilles Tjoelker if (loc != NULL) { 486e4b50334SJilles Tjoelker setlocale(LC_ALL, loc); 487e4b50334SJilles Tjoelker INTON; 4886ed74a0aSJilles Tjoelker updatecharset(); 489e4b50334SJilles Tjoelker return; 490e4b50334SJilles Tjoelker } 491e4b50334SJilles Tjoelker locdef = bltinlookup("LANG", 0); 492e4b50334SJilles Tjoelker for (i = 0; locale_names[i] != NULL; i++) { 493e4b50334SJilles Tjoelker loc = bltinlookup(locale_names[i], 0); 494e4b50334SJilles Tjoelker if (loc == NULL) 495e4b50334SJilles Tjoelker loc = locdef; 496e4b50334SJilles Tjoelker if (loc != NULL) 497e4b50334SJilles Tjoelker setlocale(locale_categories[i], loc); 498e4b50334SJilles Tjoelker } 499e4b50334SJilles Tjoelker INTON; 5006ed74a0aSJilles Tjoelker updatecharset(); 501e4b50334SJilles Tjoelker } 502e4b50334SJilles Tjoelker 503e4b50334SJilles Tjoelker /* 504e4b50334SJilles Tjoelker * Undo the effect of bltinlocaleset(). 505e4b50334SJilles Tjoelker */ 506e4b50334SJilles Tjoelker void 507e4b50334SJilles Tjoelker bltinunsetlocale(void) 508e4b50334SJilles Tjoelker { 5098ef0ae8aSJilles Tjoelker int i; 510e4b50334SJilles Tjoelker 511e4b50334SJilles Tjoelker INTOFF; 5128ef0ae8aSJilles Tjoelker if (cmdenviron) for (i = 0; i < cmdenviron->count; i++) { 5138ef0ae8aSJilles Tjoelker if (localevar(cmdenviron->args[i])) { 514e4b50334SJilles Tjoelker setlocale(LC_ALL, ""); 5156ed74a0aSJilles Tjoelker updatecharset(); 516*3f2da875SJilles Tjoelker break; 517e4b50334SJilles Tjoelker } 518e4b50334SJilles Tjoelker } 519e4b50334SJilles Tjoelker INTON; 520e4b50334SJilles Tjoelker } 521e4b50334SJilles Tjoelker 5226ed74a0aSJilles Tjoelker /* 5236ed74a0aSJilles Tjoelker * Update the localeisutf8 flag. 5246ed74a0aSJilles Tjoelker */ 5256ed74a0aSJilles Tjoelker void 5266ed74a0aSJilles Tjoelker updatecharset(void) 5276ed74a0aSJilles Tjoelker { 5286ed74a0aSJilles Tjoelker char *charset; 5296ed74a0aSJilles Tjoelker 5306ed74a0aSJilles Tjoelker charset = nl_langinfo(CODESET); 5316ed74a0aSJilles Tjoelker localeisutf8 = !strcmp(charset, "UTF-8"); 5326ed74a0aSJilles Tjoelker } 5334b88c807SRodney W. Grimes 53407eb7033SJilles Tjoelker void 53507eb7033SJilles Tjoelker initcharset(void) 53607eb7033SJilles Tjoelker { 53707eb7033SJilles Tjoelker updatecharset(); 53807eb7033SJilles Tjoelker initial_localeisutf8 = localeisutf8; 53907eb7033SJilles Tjoelker } 54007eb7033SJilles Tjoelker 5414b88c807SRodney W. Grimes /* 5424b88c807SRodney W. Grimes * Generate a list of exported variables. This routine is used to construct 5434b88c807SRodney W. Grimes * the third argument to execve when executing a program. 5444b88c807SRodney W. Grimes */ 5454b88c807SRodney W. Grimes 5464b88c807SRodney W. Grimes char ** 5475134c3f7SWarner Losh environment(void) 5485134c3f7SWarner Losh { 5494b88c807SRodney W. Grimes int nenv; 5504b88c807SRodney W. Grimes struct var **vpp; 5514b88c807SRodney W. Grimes struct var *vp; 5524b88c807SRodney W. Grimes char **env, **ep; 5534b88c807SRodney W. Grimes 5544b88c807SRodney W. Grimes nenv = 0; 5554b88c807SRodney W. Grimes for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) { 5564b88c807SRodney W. Grimes for (vp = *vpp ; vp ; vp = vp->next) 5574b88c807SRodney W. Grimes if (vp->flags & VEXPORT) 5584b88c807SRodney W. Grimes nenv++; 5594b88c807SRodney W. Grimes } 5604b88c807SRodney W. Grimes ep = env = stalloc((nenv + 1) * sizeof *env); 5614b88c807SRodney W. Grimes for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) { 5624b88c807SRodney W. Grimes for (vp = *vpp ; vp ; vp = vp->next) 5634b88c807SRodney W. Grimes if (vp->flags & VEXPORT) 5644b88c807SRodney W. Grimes *ep++ = vp->text; 5654b88c807SRodney W. Grimes } 5664b88c807SRodney W. Grimes *ep = NULL; 5674b88c807SRodney W. Grimes return env; 5684b88c807SRodney W. Grimes } 5694b88c807SRodney W. Grimes 5704b88c807SRodney W. Grimes 57188328642SDavid E. O'Brien static int 572692f35fdSStefan Farfeleder var_compare(const void *a, const void *b) 573692f35fdSStefan Farfeleder { 574692f35fdSStefan Farfeleder const char *const *sa, *const *sb; 575692f35fdSStefan Farfeleder 576692f35fdSStefan Farfeleder sa = a; 577692f35fdSStefan Farfeleder sb = b; 578692f35fdSStefan Farfeleder /* 579692f35fdSStefan Farfeleder * This compares two var=value strings which creates a different 580692f35fdSStefan Farfeleder * order from what you would probably expect. POSIX is somewhat 581692f35fdSStefan Farfeleder * ambiguous on what should be sorted exactly. 582692f35fdSStefan Farfeleder */ 583692f35fdSStefan Farfeleder return strcoll(*sa, *sb); 584692f35fdSStefan Farfeleder } 585692f35fdSStefan Farfeleder 5864b88c807SRodney W. Grimes 5874b88c807SRodney W. Grimes /* 588cff1d849SJilles Tjoelker * Command to list all variables which are set. This is invoked from the 589cff1d849SJilles Tjoelker * set command when it is called without any options or operands. 5904b88c807SRodney W. Grimes */ 5914b88c807SRodney W. Grimes 5924b88c807SRodney W. Grimes int 5935134c3f7SWarner Losh showvarscmd(int argc __unused, char **argv __unused) 594aa9caaf6SPeter Wemm { 5954b88c807SRodney W. Grimes struct var **vpp; 5964b88c807SRodney W. Grimes struct var *vp; 59759258844STim J. Robbins const char *s; 598692f35fdSStefan Farfeleder const char **vars; 599692f35fdSStefan Farfeleder int i, n; 6004b88c807SRodney W. Grimes 601692f35fdSStefan Farfeleder /* 602692f35fdSStefan Farfeleder * POSIX requires us to sort the variables. 603692f35fdSStefan Farfeleder */ 604692f35fdSStefan Farfeleder n = 0; 6054b88c807SRodney W. Grimes for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) { 6064b88c807SRodney W. Grimes for (vp = *vpp; vp; vp = vp->next) { 607692f35fdSStefan Farfeleder if (!(vp->flags & VUNSET)) 608692f35fdSStefan Farfeleder n++; 609692f35fdSStefan Farfeleder } 610692f35fdSStefan Farfeleder } 611692f35fdSStefan Farfeleder 61233233ec7SJilles Tjoelker INTOFF; 613692f35fdSStefan Farfeleder vars = ckmalloc(n * sizeof(*vars)); 614692f35fdSStefan Farfeleder i = 0; 615692f35fdSStefan Farfeleder for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) { 616692f35fdSStefan Farfeleder for (vp = *vpp; vp; vp = vp->next) { 617692f35fdSStefan Farfeleder if (!(vp->flags & VUNSET)) 618692f35fdSStefan Farfeleder vars[i++] = vp->text; 619692f35fdSStefan Farfeleder } 620692f35fdSStefan Farfeleder } 621692f35fdSStefan Farfeleder 622692f35fdSStefan Farfeleder qsort(vars, n, sizeof(*vars), var_compare); 623692f35fdSStefan Farfeleder for (i = 0; i < n; i++) { 624f5f215e2SJilles Tjoelker /* 625f5f215e2SJilles Tjoelker * Skip improper variable names so the output remains usable as 626f5f215e2SJilles Tjoelker * shell input. 627f5f215e2SJilles Tjoelker */ 628f5f215e2SJilles Tjoelker if (!isassignment(vars[i])) 629f5f215e2SJilles Tjoelker continue; 630aeb5d065SJilles Tjoelker s = strchr(vars[i], '='); 631aeb5d065SJilles Tjoelker s++; 632aeb5d065SJilles Tjoelker outbin(vars[i], s - vars[i], out1); 633aeb5d065SJilles Tjoelker out1qstr(s); 63459258844STim J. Robbins out1c('\n'); 6354b88c807SRodney W. Grimes } 636692f35fdSStefan Farfeleder ckfree(vars); 63733233ec7SJilles Tjoelker INTON; 638692f35fdSStefan Farfeleder 6394b88c807SRodney W. Grimes return 0; 6404b88c807SRodney W. Grimes } 6414b88c807SRodney W. Grimes 6424b88c807SRodney W. Grimes 6434b88c807SRodney W. Grimes 6444b88c807SRodney W. Grimes /* 6454b88c807SRodney W. Grimes * The export and readonly commands. 6464b88c807SRodney W. Grimes */ 6474b88c807SRodney W. Grimes 6484b88c807SRodney W. Grimes int 6497cbda738SJilles Tjoelker exportcmd(int argc __unused, char **argv) 650aa9caaf6SPeter Wemm { 6514b88c807SRodney W. Grimes struct var **vpp; 6524b88c807SRodney W. Grimes struct var *vp; 6537cbda738SJilles Tjoelker char **ap; 6544b88c807SRodney W. Grimes char *name; 6554b88c807SRodney W. Grimes char *p; 65645086f8cSTim J. Robbins char *cmdname; 65745086f8cSTim J. Robbins int ch, values; 6584b88c807SRodney W. Grimes int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT; 6594b88c807SRodney W. Grimes 66045086f8cSTim J. Robbins cmdname = argv[0]; 66145086f8cSTim J. Robbins values = 0; 6627cbda738SJilles Tjoelker while ((ch = nextopt("p")) != '\0') { 66345086f8cSTim J. Robbins switch (ch) { 66445086f8cSTim J. Robbins case 'p': 66545086f8cSTim J. Robbins values = 1; 66645086f8cSTim J. Robbins break; 66745086f8cSTim J. Robbins } 66845086f8cSTim J. Robbins } 66945086f8cSTim J. Robbins 6707cbda738SJilles Tjoelker if (values && *argptr != NULL) 6713fda45ecSStefan Farfeleder error("-p requires no arguments"); 6727cbda738SJilles Tjoelker if (*argptr != NULL) { 6737cbda738SJilles Tjoelker for (ap = argptr; (name = *ap) != NULL; ap++) { 6744b88c807SRodney W. Grimes if ((p = strchr(name, '=')) != NULL) { 6754b88c807SRodney W. Grimes p++; 6764b88c807SRodney W. Grimes } else { 6773a99ed46SJilles Tjoelker vp = find_var(name, NULL, NULL); 6783a99ed46SJilles Tjoelker if (vp != NULL) { 6794b88c807SRodney W. Grimes vp->flags |= flag; 680ba726b8aSAndrey A. Chernov if ((vp->flags & VEXPORT) && localevar(vp->text)) { 6815b78c6c2SSean Farley change_env(vp->text, 1); 682ba726b8aSAndrey A. Chernov (void) setlocale(LC_ALL, ""); 6836ed74a0aSJilles Tjoelker updatecharset(); 684ba726b8aSAndrey A. Chernov } 6853a99ed46SJilles Tjoelker continue; 6864b88c807SRodney W. Grimes } 6874b88c807SRodney W. Grimes } 6884b88c807SRodney W. Grimes setvar(name, p, flag); 6894b88c807SRodney W. Grimes } 6904b88c807SRodney W. Grimes } else { 6914b88c807SRodney W. Grimes for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) { 6924b88c807SRodney W. Grimes for (vp = *vpp ; vp ; vp = vp->next) { 6934b88c807SRodney W. Grimes if (vp->flags & flag) { 69445086f8cSTim J. Robbins if (values) { 695f5f215e2SJilles Tjoelker /* 696f5f215e2SJilles Tjoelker * Skip improper variable names 697f5f215e2SJilles Tjoelker * so the output remains usable 698f5f215e2SJilles Tjoelker * as shell input. 699f5f215e2SJilles Tjoelker */ 700f5f215e2SJilles Tjoelker if (!isassignment(vp->text)) 701f5f215e2SJilles Tjoelker continue; 70245086f8cSTim J. Robbins out1str(cmdname); 70345086f8cSTim J. Robbins out1c(' '); 70445086f8cSTim J. Robbins } 70545086f8cSTim J. Robbins if (values && !(vp->flags & VUNSET)) { 706258ef734SJilles Tjoelker outbin(vp->text, 707258ef734SJilles Tjoelker vp->name_len + 1, out1); 708258ef734SJilles Tjoelker out1qstr(vp->text + 709258ef734SJilles Tjoelker vp->name_len + 1); 710aeb5d065SJilles Tjoelker } else 711258ef734SJilles Tjoelker outbin(vp->text, vp->name_len, 712aeb5d065SJilles Tjoelker out1); 7134b88c807SRodney W. Grimes out1c('\n'); 7144b88c807SRodney W. Grimes } 7154b88c807SRodney W. Grimes } 7164b88c807SRodney W. Grimes } 7174b88c807SRodney W. Grimes } 7184b88c807SRodney W. Grimes return 0; 7194b88c807SRodney W. Grimes } 7204b88c807SRodney W. Grimes 7214b88c807SRodney W. Grimes 7224b88c807SRodney W. Grimes /* 7234b88c807SRodney W. Grimes * The "local" command. 7244b88c807SRodney W. Grimes */ 7254b88c807SRodney W. Grimes 726aa9caaf6SPeter Wemm int 7275134c3f7SWarner Losh localcmd(int argc __unused, char **argv __unused) 728aa9caaf6SPeter Wemm { 7294b88c807SRodney W. Grimes char *name; 7304b88c807SRodney W. Grimes 731056fd329SJilles Tjoelker nextopt(""); 7324b88c807SRodney W. Grimes if (! in_function()) 7334b88c807SRodney W. Grimes error("Not in a function"); 7344b88c807SRodney W. Grimes while ((name = *argptr++) != NULL) { 7354b88c807SRodney W. Grimes mklocal(name); 7364b88c807SRodney W. Grimes } 7374b88c807SRodney W. Grimes return 0; 7384b88c807SRodney W. Grimes } 7394b88c807SRodney W. Grimes 7404b88c807SRodney W. Grimes 7414b88c807SRodney W. Grimes /* 7424b88c807SRodney W. Grimes * Make a variable a local variable. When a variable is made local, it's 7434b88c807SRodney W. Grimes * value and flags are saved in a localvar structure. The saved values 7444b88c807SRodney W. Grimes * will be restored when the shell function returns. We handle the name 7454b88c807SRodney W. Grimes * "-" as a special case. 7464b88c807SRodney W. Grimes */ 7474b88c807SRodney W. Grimes 7484b88c807SRodney W. Grimes void 7495134c3f7SWarner Losh mklocal(char *name) 7504b88c807SRodney W. Grimes { 7514b88c807SRodney W. Grimes struct localvar *lvp; 7524b88c807SRodney W. Grimes struct var **vpp; 7534b88c807SRodney W. Grimes struct var *vp; 7544b88c807SRodney W. Grimes 7554b88c807SRodney W. Grimes INTOFF; 7564b88c807SRodney W. Grimes lvp = ckmalloc(sizeof (struct localvar)); 7574b88c807SRodney W. Grimes if (name[0] == '-' && name[1] == '\0') { 7583da40d4aSJilles Tjoelker lvp->text = ckmalloc(sizeof optval); 7593da40d4aSJilles Tjoelker memcpy(lvp->text, optval, sizeof optval); 7604b88c807SRodney W. Grimes vp = NULL; 7614b88c807SRodney W. Grimes } else { 7623a99ed46SJilles Tjoelker vp = find_var(name, &vpp, NULL); 7634b88c807SRodney W. Grimes if (vp == NULL) { 7644b88c807SRodney W. Grimes if (strchr(name, '=')) 765c543e1aeSJilles Tjoelker setvareq(savestr(name), VSTRFIXED | VNOLOCAL); 7664b88c807SRodney W. Grimes else 767c543e1aeSJilles Tjoelker setvar(name, NULL, VSTRFIXED | VNOLOCAL); 7684b88c807SRodney W. Grimes vp = *vpp; /* the new variable */ 7694b88c807SRodney W. Grimes lvp->text = NULL; 7704b88c807SRodney W. Grimes lvp->flags = VUNSET; 7714b88c807SRodney W. Grimes } else { 7724b88c807SRodney W. Grimes lvp->text = vp->text; 7734b88c807SRodney W. Grimes lvp->flags = vp->flags; 7744b88c807SRodney W. Grimes vp->flags |= VSTRFIXED|VTEXTFIXED; 7753a99ed46SJilles Tjoelker if (name[vp->name_len] == '=') 776c543e1aeSJilles Tjoelker setvareq(savestr(name), VNOLOCAL); 7774b88c807SRodney W. Grimes } 7784b88c807SRodney W. Grimes } 7794b88c807SRodney W. Grimes lvp->vp = vp; 7804b88c807SRodney W. Grimes lvp->next = localvars; 7814b88c807SRodney W. Grimes localvars = lvp; 7824b88c807SRodney W. Grimes INTON; 7834b88c807SRodney W. Grimes } 7844b88c807SRodney W. Grimes 7854b88c807SRodney W. Grimes 7864b88c807SRodney W. Grimes /* 7874b88c807SRodney W. Grimes * Called after a function returns. 7884b88c807SRodney W. Grimes */ 7894b88c807SRodney W. Grimes 7904b88c807SRodney W. Grimes void 7915134c3f7SWarner Losh poplocalvars(void) 7925134c3f7SWarner Losh { 7934b88c807SRodney W. Grimes struct localvar *lvp; 7944b88c807SRodney W. Grimes struct var *vp; 795cf45f124SJilles Tjoelker int islocalevar; 7964b88c807SRodney W. Grimes 7971632bf1aSJilles Tjoelker INTOFF; 7984b88c807SRodney W. Grimes while ((lvp = localvars) != NULL) { 7994b88c807SRodney W. Grimes localvars = lvp->next; 8004b88c807SRodney W. Grimes vp = lvp->vp; 8014b88c807SRodney W. Grimes if (vp == NULL) { /* $- saved */ 8023da40d4aSJilles Tjoelker memcpy(optval, lvp->text, sizeof optval); 8034b88c807SRodney W. Grimes ckfree(lvp->text); 8044a7b1013SJilles Tjoelker optschanged(); 8054b88c807SRodney W. Grimes } else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) { 806a9bdd616SJilles Tjoelker vp->flags &= ~VREADONLY; 8074b88c807SRodney W. Grimes (void)unsetvar(vp->text); 8084b88c807SRodney W. Grimes } else { 809cf45f124SJilles Tjoelker islocalevar = (vp->flags | lvp->flags) & VEXPORT && 810cf45f124SJilles Tjoelker localevar(lvp->text); 8114b88c807SRodney W. Grimes if ((vp->flags & VTEXTFIXED) == 0) 8124b88c807SRodney W. Grimes ckfree(vp->text); 8134b88c807SRodney W. Grimes vp->flags = lvp->flags; 8144b88c807SRodney W. Grimes vp->text = lvp->text; 815cf45f124SJilles Tjoelker if (vp->func) 816cf45f124SJilles Tjoelker (*vp->func)(vp->text + vp->name_len + 1); 817cf45f124SJilles Tjoelker if (islocalevar) { 818cf45f124SJilles Tjoelker change_env(vp->text, vp->flags & VEXPORT && 819cf45f124SJilles Tjoelker (vp->flags & VUNSET) == 0); 820cf45f124SJilles Tjoelker setlocale(LC_ALL, ""); 821cf45f124SJilles Tjoelker updatecharset(); 822cf45f124SJilles Tjoelker } 8234b88c807SRodney W. Grimes } 8244b88c807SRodney W. Grimes ckfree(lvp); 8254b88c807SRodney W. Grimes } 8261632bf1aSJilles Tjoelker INTON; 8274b88c807SRodney W. Grimes } 8284b88c807SRodney W. Grimes 8294b88c807SRodney W. Grimes 830aa9caaf6SPeter Wemm int 8315134c3f7SWarner Losh setvarcmd(int argc, char **argv) 832aa9caaf6SPeter Wemm { 8334b88c807SRodney W. Grimes if (argc <= 2) 8344b88c807SRodney W. Grimes return unsetcmd(argc, argv); 8354b88c807SRodney W. Grimes else if (argc == 3) 8364b88c807SRodney W. Grimes setvar(argv[1], argv[2], 0); 8374b88c807SRodney W. Grimes else 838274110dfSJilles Tjoelker error("too many arguments"); 8394b88c807SRodney W. Grimes return 0; 8404b88c807SRodney W. Grimes } 8414b88c807SRodney W. Grimes 8424b88c807SRodney W. Grimes 8434b88c807SRodney W. Grimes /* 844cff1d849SJilles Tjoelker * The unset builtin command. 8454b88c807SRodney W. Grimes */ 8464b88c807SRodney W. Grimes 847aa9caaf6SPeter Wemm int 8485134c3f7SWarner Losh unsetcmd(int argc __unused, char **argv __unused) 849aa9caaf6SPeter Wemm { 8504b88c807SRodney W. Grimes char **ap; 8514b88c807SRodney W. Grimes int i; 8524b88c807SRodney W. Grimes int flg_func = 0; 8534b88c807SRodney W. Grimes int flg_var = 0; 8544b88c807SRodney W. Grimes int ret = 0; 8554b88c807SRodney W. Grimes 8564b88c807SRodney W. Grimes while ((i = nextopt("vf")) != '\0') { 8574b88c807SRodney W. Grimes if (i == 'f') 8584b88c807SRodney W. Grimes flg_func = 1; 8594b88c807SRodney W. Grimes else 8604b88c807SRodney W. Grimes flg_var = 1; 8614b88c807SRodney W. Grimes } 8624b88c807SRodney W. Grimes if (flg_func == 0 && flg_var == 0) 8634b88c807SRodney W. Grimes flg_var = 1; 8644b88c807SRodney W. Grimes 8651632bf1aSJilles Tjoelker INTOFF; 8664b88c807SRodney W. Grimes for (ap = argptr; *ap ; ap++) { 8674b88c807SRodney W. Grimes if (flg_func) 8684b88c807SRodney W. Grimes ret |= unsetfunc(*ap); 8694b88c807SRodney W. Grimes if (flg_var) 8704b88c807SRodney W. Grimes ret |= unsetvar(*ap); 8714b88c807SRodney W. Grimes } 8721632bf1aSJilles Tjoelker INTON; 8734b88c807SRodney W. Grimes return ret; 8744b88c807SRodney W. Grimes } 8754b88c807SRodney W. Grimes 8764b88c807SRodney W. Grimes 8774b88c807SRodney W. Grimes /* 8784b88c807SRodney W. Grimes * Unset the specified variable. 8791632bf1aSJilles Tjoelker * Called with interrupts off. 8804b88c807SRodney W. Grimes */ 8814b88c807SRodney W. Grimes 882ab0a2172SSteve Price int 8832cac6e36SJilles Tjoelker unsetvar(const char *s) 8844b88c807SRodney W. Grimes { 8854b88c807SRodney W. Grimes struct var **vpp; 8864b88c807SRodney W. Grimes struct var *vp; 8874b88c807SRodney W. Grimes 8883a99ed46SJilles Tjoelker vp = find_var(s, &vpp, NULL); 8893a99ed46SJilles Tjoelker if (vp == NULL) 8903a99ed46SJilles Tjoelker return (0); 8914b88c807SRodney W. Grimes if (vp->flags & VREADONLY) 8924b88c807SRodney W. Grimes return (1); 8933a99ed46SJilles Tjoelker if (vp->text[vp->name_len + 1] != '\0') 894781bfb5aSJilles Tjoelker setvar(s, "", 0); 895ba726b8aSAndrey A. Chernov if ((vp->flags & VEXPORT) && localevar(vp->text)) { 8965b78c6c2SSean Farley change_env(s, 0); 897ba726b8aSAndrey A. Chernov setlocale(LC_ALL, ""); 8986ed74a0aSJilles Tjoelker updatecharset(); 899ba726b8aSAndrey A. Chernov } 9004b88c807SRodney W. Grimes vp->flags &= ~VEXPORT; 9014b88c807SRodney W. Grimes vp->flags |= VUNSET; 9024b88c807SRodney W. Grimes if ((vp->flags & VSTRFIXED) == 0) { 9034b88c807SRodney W. Grimes if ((vp->flags & VTEXTFIXED) == 0) 9044b88c807SRodney W. Grimes ckfree(vp->text); 9054b88c807SRodney W. Grimes *vpp = vp->next; 9064b88c807SRodney W. Grimes ckfree(vp); 9074b88c807SRodney W. Grimes } 9084b88c807SRodney W. Grimes return (0); 9094b88c807SRodney W. Grimes } 9104b88c807SRodney W. Grimes 9114b88c807SRodney W. Grimes 9124b88c807SRodney W. Grimes 9134b88c807SRodney W. Grimes /* 9146c7d8328SEitan Adler * Returns true if the two strings specify the same variable. The first 9154b88c807SRodney W. Grimes * variable name is terminated by '='; the second may be terminated by 9164b88c807SRodney W. Grimes * either '=' or '\0'. 9174b88c807SRodney W. Grimes */ 9184b88c807SRodney W. Grimes 91988328642SDavid E. O'Brien static int 9202cac6e36SJilles Tjoelker varequal(const char *p, const char *q) 9214b88c807SRodney W. Grimes { 9224b88c807SRodney W. Grimes while (*p == *q++) { 9234b88c807SRodney W. Grimes if (*p++ == '=') 9244b88c807SRodney W. Grimes return 1; 9254b88c807SRodney W. Grimes } 9264b88c807SRodney W. Grimes if (*p == '=' && *(q - 1) == '\0') 9274b88c807SRodney W. Grimes return 1; 9284b88c807SRodney W. Grimes return 0; 9294b88c807SRodney W. Grimes } 9303a99ed46SJilles Tjoelker 9313a99ed46SJilles Tjoelker /* 9323a99ed46SJilles Tjoelker * Search for a variable. 9333a99ed46SJilles Tjoelker * 'name' may be terminated by '=' or a NUL. 9343a99ed46SJilles Tjoelker * vppp is set to the pointer to vp, or the list head if vp isn't found 9356c7d8328SEitan Adler * lenp is set to the number of characters in 'name' 9363a99ed46SJilles Tjoelker */ 9373a99ed46SJilles Tjoelker 9383a99ed46SJilles Tjoelker static struct var * 9393a99ed46SJilles Tjoelker find_var(const char *name, struct var ***vppp, int *lenp) 9403a99ed46SJilles Tjoelker { 9413a99ed46SJilles Tjoelker unsigned int hashval; 9423a99ed46SJilles Tjoelker int len; 9433a99ed46SJilles Tjoelker struct var *vp, **vpp; 9443a99ed46SJilles Tjoelker const char *p = name; 9453a99ed46SJilles Tjoelker 9463a99ed46SJilles Tjoelker hashval = 0; 9473a99ed46SJilles Tjoelker while (*p && *p != '=') 9483a99ed46SJilles Tjoelker hashval = 2 * hashval + (unsigned char)*p++; 9493a99ed46SJilles Tjoelker len = p - name; 9503a99ed46SJilles Tjoelker 9513a99ed46SJilles Tjoelker if (lenp) 9523a99ed46SJilles Tjoelker *lenp = len; 9533a99ed46SJilles Tjoelker vpp = &vartab[hashval % VTABSIZE]; 9543a99ed46SJilles Tjoelker if (vppp) 9553a99ed46SJilles Tjoelker *vppp = vpp; 9563a99ed46SJilles Tjoelker 9573a99ed46SJilles Tjoelker for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) { 9583a99ed46SJilles Tjoelker if (vp->name_len != len) 9593a99ed46SJilles Tjoelker continue; 9603a99ed46SJilles Tjoelker if (memcmp(vp->text, name, len) != 0) 9613a99ed46SJilles Tjoelker continue; 9623a99ed46SJilles Tjoelker if (vppp) 9633a99ed46SJilles Tjoelker *vppp = vpp; 9643a99ed46SJilles Tjoelker return vp; 9653a99ed46SJilles Tjoelker } 9663a99ed46SJilles Tjoelker return NULL; 9673a99ed46SJilles Tjoelker } 968