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 354b88c807SRodney W. Grimes #ifndef lint 363d7b5b93SPhilippe Charnier #if 0 373d7b5b93SPhilippe Charnier static char sccsid[] = "@(#)var.c 8.3 (Berkeley) 5/4/95"; 383d7b5b93SPhilippe Charnier #endif 394b88c807SRodney W. Grimes #endif /* not lint */ 402749b141SDavid E. O'Brien #include <sys/cdefs.h> 412749b141SDavid E. O'Brien __FBSDID("$FreeBSD$"); 424b88c807SRodney W. Grimes 43aa9caaf6SPeter Wemm #include <unistd.h> 44aa9caaf6SPeter Wemm #include <stdlib.h> 458d5c19ffSDavid E. O'Brien #include <paths.h> 46aa9caaf6SPeter Wemm 474b88c807SRodney W. Grimes /* 484b88c807SRodney W. Grimes * Shell variables. 494b88c807SRodney W. Grimes */ 504b88c807SRodney W. Grimes 51ba726b8aSAndrey A. Chernov #include <locale.h> 526ed74a0aSJilles Tjoelker #include <langinfo.h> 53ba726b8aSAndrey A. Chernov 544b88c807SRodney W. Grimes #include "shell.h" 554b88c807SRodney W. Grimes #include "output.h" 564b88c807SRodney W. Grimes #include "expand.h" 574b88c807SRodney W. Grimes #include "nodes.h" /* for other headers */ 584b88c807SRodney W. Grimes #include "eval.h" /* defines cmdenviron */ 594b88c807SRodney W. Grimes #include "exec.h" 604b88c807SRodney W. Grimes #include "syntax.h" 614b88c807SRodney W. Grimes #include "options.h" 624b88c807SRodney W. Grimes #include "mail.h" 634b88c807SRodney W. Grimes #include "var.h" 644b88c807SRodney W. Grimes #include "memalloc.h" 654b88c807SRodney W. Grimes #include "error.h" 664b88c807SRodney W. Grimes #include "mystring.h" 67ab0a2172SSteve Price #include "parser.h" 68454a02b3SJilles Tjoelker #include "builtins.h" 69aa9caaf6SPeter Wemm #ifndef NO_HISTORY 70aa9caaf6SPeter Wemm #include "myhistedit.h" 71aa9caaf6SPeter Wemm #endif 724b88c807SRodney W. Grimes 734b88c807SRodney W. Grimes 743cdd74bbSBryan Drewery #ifndef VTABSIZE 754b88c807SRodney W. Grimes #define VTABSIZE 39 763cdd74bbSBryan Drewery #endif 774b88c807SRodney W. Grimes 784b88c807SRodney W. Grimes 794b88c807SRodney W. Grimes struct varinit { 804b88c807SRodney W. Grimes struct var *var; 814b88c807SRodney W. Grimes int flags; 82c6c5dd37SJilles Tjoelker const char *text; 835134c3f7SWarner Losh void (*func)(const char *); 844b88c807SRodney W. Grimes }; 854b88c807SRodney W. Grimes 864b88c807SRodney W. Grimes 87aa9caaf6SPeter Wemm #ifndef NO_HISTORY 884b88c807SRodney W. Grimes struct var vhistsize; 89580eefdfSJilles Tjoelker struct var vterm; 90aa9caaf6SPeter Wemm #endif 914b88c807SRodney W. Grimes struct var vifs; 924b88c807SRodney W. Grimes struct var vmail; 934b88c807SRodney W. Grimes struct var vmpath; 944b88c807SRodney W. Grimes struct var vpath; 954b88c807SRodney W. Grimes struct var vps1; 964b88c807SRodney W. Grimes struct var vps2; 97120c8e6cSStefan Farfeleder struct var vps4; 98aa7b6f82SDavid E. O'Brien static struct var voptind; 99caf29fabSJilles Tjoelker struct var vdisvfork; 1004b88c807SRodney W. Grimes 10158bdb076SJilles Tjoelker struct localvar *localvars; 102c543e1aeSJilles Tjoelker int forcelocal; 103c543e1aeSJilles Tjoelker 104aa7b6f82SDavid E. O'Brien static const struct varinit varinit[] = { 105aa9caaf6SPeter Wemm #ifndef NO_HISTORY 106c6c5dd37SJilles Tjoelker { &vhistsize, VUNSET, "HISTSIZE=", 107ab0a2172SSteve Price sethistsize }, 108aa9caaf6SPeter Wemm #endif 109c6c5dd37SJilles Tjoelker { &vifs, 0, "IFS= \t\n", 110ab0a2172SSteve Price NULL }, 111c6c5dd37SJilles Tjoelker { &vmail, VUNSET, "MAIL=", 112ab0a2172SSteve Price NULL }, 113c6c5dd37SJilles Tjoelker { &vmpath, VUNSET, "MAILPATH=", 114ab0a2172SSteve Price NULL }, 115c6c5dd37SJilles Tjoelker { &vpath, 0, "PATH=" _PATH_DEFPATH, 116ab0a2172SSteve Price changepath }, 1174b88c807SRodney W. Grimes /* 1184b88c807SRodney W. Grimes * vps1 depends on uid 1194b88c807SRodney W. Grimes */ 120c6c5dd37SJilles Tjoelker { &vps2, 0, "PS2=> ", 121ab0a2172SSteve Price NULL }, 122c6c5dd37SJilles Tjoelker { &vps4, 0, "PS4=+ ", 123120c8e6cSStefan Farfeleder NULL }, 124580eefdfSJilles Tjoelker #ifndef NO_HISTORY 125580eefdfSJilles Tjoelker { &vterm, VUNSET, "TERM=", 126580eefdfSJilles Tjoelker setterm }, 127580eefdfSJilles Tjoelker #endif 128c6c5dd37SJilles Tjoelker { &voptind, 0, "OPTIND=1", 129ab0a2172SSteve Price getoptsreset }, 130caf29fabSJilles Tjoelker { &vdisvfork, VUNSET, "SH_DISABLE_VFORK=", 131caf29fabSJilles Tjoelker NULL }, 132ab0a2172SSteve Price { NULL, 0, NULL, 133ab0a2172SSteve Price NULL } 1344b88c807SRodney W. Grimes }; 1354b88c807SRodney W. Grimes 136aa7b6f82SDavid E. O'Brien static struct var *vartab[VTABSIZE]; 1374b88c807SRodney W. Grimes 138aa7b6f82SDavid E. O'Brien static const char *const locale_names[7] = { 139e4b50334SJilles Tjoelker "LC_COLLATE", "LC_CTYPE", "LC_MONETARY", 140e4b50334SJilles Tjoelker "LC_NUMERIC", "LC_TIME", "LC_MESSAGES", NULL 141e4b50334SJilles Tjoelker }; 142aa7b6f82SDavid E. O'Brien static const int locale_categories[7] = { 143e4b50334SJilles Tjoelker LC_COLLATE, LC_CTYPE, LC_MONETARY, LC_NUMERIC, LC_TIME, LC_MESSAGES, 0 144e4b50334SJilles Tjoelker }; 145e4b50334SJilles Tjoelker 14688328642SDavid E. O'Brien static int varequal(const char *, const char *); 1473a99ed46SJilles Tjoelker static struct var *find_var(const char *, struct var ***, int *); 14888328642SDavid E. O'Brien static int localevar(const char *); 14922afca9bSJilles Tjoelker static void setvareq_const(const char *s, int flags); 1504b88c807SRodney W. Grimes 15159e0cc8eSJilles Tjoelker extern char **environ; 1524b88c807SRodney W. Grimes 1534b88c807SRodney W. Grimes /* 15459e0cc8eSJilles Tjoelker * This routine initializes the builtin variables and imports the environment. 15559e0cc8eSJilles Tjoelker * It is called when the shell is initialized. 1564b88c807SRodney W. Grimes */ 1574b88c807SRodney W. Grimes 1584b88c807SRodney W. Grimes void 1595134c3f7SWarner Losh initvar(void) 1605134c3f7SWarner Losh { 16139dccc6fSTim J. Robbins char ppid[20]; 1624b88c807SRodney W. Grimes const struct varinit *ip; 1634b88c807SRodney W. Grimes struct var *vp; 1644b88c807SRodney W. Grimes struct var **vpp; 16559e0cc8eSJilles Tjoelker char **envp; 1664b88c807SRodney W. Grimes 1674b88c807SRodney W. Grimes for (ip = varinit ; (vp = ip->var) != NULL ; ip++) { 1683a99ed46SJilles Tjoelker if (find_var(ip->text, &vpp, &vp->name_len) != NULL) 1693a99ed46SJilles Tjoelker continue; 1704b88c807SRodney W. Grimes vp->next = *vpp; 1714b88c807SRodney W. Grimes *vpp = vp; 172c6c5dd37SJilles Tjoelker vp->text = __DECONST(char *, ip->text); 173c6c5dd37SJilles Tjoelker vp->flags = ip->flags | VSTRFIXED | VTEXTFIXED; 174ab0a2172SSteve Price vp->func = ip->func; 1754b88c807SRodney W. Grimes } 1764b88c807SRodney W. Grimes /* 1774b88c807SRodney W. Grimes * PS1 depends on uid 1784b88c807SRodney W. Grimes */ 1793a99ed46SJilles Tjoelker if (find_var("PS1", &vpp, &vps1.name_len) == NULL) { 1804b88c807SRodney W. Grimes vps1.next = *vpp; 1814b88c807SRodney W. Grimes *vpp = &vps1; 182c6c5dd37SJilles Tjoelker vps1.text = __DECONST(char *, geteuid() ? "PS1=$ " : "PS1=# "); 1834b88c807SRodney W. Grimes vps1.flags = VSTRFIXED|VTEXTFIXED; 1844b88c807SRodney W. Grimes } 18539dccc6fSTim J. Robbins fmtstr(ppid, sizeof(ppid), "%d", (int)getppid()); 18639dccc6fSTim J. Robbins setvarsafe("PPID", ppid, 0); 18759e0cc8eSJilles Tjoelker for (envp = environ ; *envp ; envp++) { 18859e0cc8eSJilles Tjoelker if (strchr(*envp, '=')) { 18959e0cc8eSJilles Tjoelker setvareq(*envp, VEXPORT|VTEXTFIXED); 19059e0cc8eSJilles Tjoelker } 19159e0cc8eSJilles Tjoelker } 19222afca9bSJilles Tjoelker setvareq_const("OPTIND=1", 0); 1937cca93e6SJilles Tjoelker setvareq_const("IFS= \t\n", 0); 1944b88c807SRodney W. Grimes } 1954b88c807SRodney W. Grimes 1964b88c807SRodney W. Grimes /* 197ab0a2172SSteve Price * Safe version of setvar, returns 1 on success 0 on failure. 198ab0a2172SSteve Price */ 199ab0a2172SSteve Price 200ab0a2172SSteve Price int 2012cac6e36SJilles Tjoelker setvarsafe(const char *name, const char *val, int flags) 202ab0a2172SSteve Price { 203ab0a2172SSteve Price struct jmploc jmploc; 204224fbf9fSJilles Tjoelker struct jmploc *const savehandler = handler; 205ab0a2172SSteve Price int err = 0; 2069922c6d2SJilles Tjoelker int inton; 207ab0a2172SSteve Price 2089922c6d2SJilles Tjoelker inton = is_int_on(); 209ab0a2172SSteve Price if (setjmp(jmploc.loc)) 210ab0a2172SSteve Price err = 1; 211ab0a2172SSteve Price else { 212ab0a2172SSteve Price handler = &jmploc; 213ab0a2172SSteve Price setvar(name, val, flags); 214ab0a2172SSteve Price } 215ab0a2172SSteve Price handler = savehandler; 2169922c6d2SJilles Tjoelker SETINTON(inton); 217ab0a2172SSteve Price return err; 218ab0a2172SSteve Price } 219ab0a2172SSteve Price 220ab0a2172SSteve Price /* 221b3decf89SJens Schweikhardt * Set the value of a variable. The flags argument is stored with the 2224b88c807SRodney W. Grimes * flags of the variable. If val is NULL, the variable is unset. 2234b88c807SRodney W. Grimes */ 2244b88c807SRodney W. Grimes 2254b88c807SRodney W. Grimes void 2262cac6e36SJilles Tjoelker setvar(const char *name, const char *val, int flags) 2274b88c807SRodney W. Grimes { 2282cac6e36SJilles Tjoelker const char *p; 229670dd3f0SJilles Tjoelker size_t len; 230670dd3f0SJilles Tjoelker size_t namelen; 231670dd3f0SJilles Tjoelker size_t vallen; 2324b88c807SRodney W. Grimes char *nameeq; 2334b88c807SRodney W. Grimes int isbad; 2344b88c807SRodney W. Grimes 2354b88c807SRodney W. Grimes isbad = 0; 2364b88c807SRodney W. Grimes p = name; 237ba726b8aSAndrey A. Chernov if (! is_name(*p)) 2384b88c807SRodney W. Grimes isbad = 1; 239ba726b8aSAndrey A. Chernov p++; 2404b88c807SRodney W. Grimes for (;;) { 2414b88c807SRodney W. Grimes if (! is_in_name(*p)) { 2424b88c807SRodney W. Grimes if (*p == '\0' || *p == '=') 2434b88c807SRodney W. Grimes break; 2444b88c807SRodney W. Grimes isbad = 1; 2454b88c807SRodney W. Grimes } 2464b88c807SRodney W. Grimes p++; 2474b88c807SRodney W. Grimes } 2484b88c807SRodney W. Grimes namelen = p - name; 2494b88c807SRodney W. Grimes if (isbad) 250670dd3f0SJilles Tjoelker error("%.*s: bad variable name", (int)namelen, name); 2514b88c807SRodney W. Grimes len = namelen + 2; /* 2 is space for '=' and '\0' */ 2524b88c807SRodney W. Grimes if (val == NULL) { 2534b88c807SRodney W. Grimes flags |= VUNSET; 254670dd3f0SJilles Tjoelker vallen = 0; 2554b88c807SRodney W. Grimes } else { 256670dd3f0SJilles Tjoelker vallen = strlen(val); 257670dd3f0SJilles Tjoelker len += vallen; 2584b88c807SRodney W. Grimes } 2591632bf1aSJilles Tjoelker INTOFF; 2602cac6e36SJilles Tjoelker nameeq = ckmalloc(len); 2612cac6e36SJilles Tjoelker memcpy(nameeq, name, namelen); 2622cac6e36SJilles Tjoelker nameeq[namelen] = '='; 2634b88c807SRodney W. Grimes if (val) 264670dd3f0SJilles Tjoelker memcpy(nameeq + namelen + 1, val, vallen + 1); 2652cac6e36SJilles Tjoelker else 2662cac6e36SJilles Tjoelker nameeq[namelen + 1] = '\0'; 2674b88c807SRodney W. Grimes setvareq(nameeq, flags); 2681632bf1aSJilles Tjoelker INTON; 2694b88c807SRodney W. Grimes } 2704b88c807SRodney W. Grimes 27188328642SDavid E. O'Brien static int 2722cac6e36SJilles Tjoelker localevar(const char *s) 273ba726b8aSAndrey A. Chernov { 274e4b50334SJilles Tjoelker const char *const *ss; 2754b88c807SRodney W. Grimes 276ba726b8aSAndrey A. Chernov if (*s != 'L') 277ba726b8aSAndrey A. Chernov return 0; 278ba726b8aSAndrey A. Chernov if (varequal(s + 1, "ANG")) 279ba726b8aSAndrey A. Chernov return 1; 280ba726b8aSAndrey A. Chernov if (strncmp(s + 1, "C_", 2) != 0) 281ba726b8aSAndrey A. Chernov return 0; 282e4b50334SJilles Tjoelker if (varequal(s + 3, "ALL")) 283e4b50334SJilles Tjoelker return 1; 284e4b50334SJilles Tjoelker for (ss = locale_names; *ss ; ss++) 285e4b50334SJilles Tjoelker if (varequal(s + 3, *ss + 3)) 286ba726b8aSAndrey A. Chernov return 1; 287ba726b8aSAndrey A. Chernov return 0; 288ba726b8aSAndrey A. Chernov } 2894b88c807SRodney W. Grimes 2905b78c6c2SSean Farley 2915b78c6c2SSean Farley /* 2925b78c6c2SSean Farley * Sets/unsets an environment variable from a pointer that may actually be a 2935b78c6c2SSean Farley * pointer into environ where the string should not be manipulated. 2945b78c6c2SSean Farley */ 29588328642SDavid E. O'Brien static void 2962cac6e36SJilles Tjoelker change_env(const char *s, int set) 2975b78c6c2SSean Farley { 2985b78c6c2SSean Farley char *eqp; 2995b78c6c2SSean Farley char *ss; 3005b78c6c2SSean Farley 3011632bf1aSJilles Tjoelker INTOFF; 3025b78c6c2SSean Farley ss = savestr(s); 3035b78c6c2SSean Farley if ((eqp = strchr(ss, '=')) != NULL) 3045b78c6c2SSean Farley *eqp = '\0'; 3055b78c6c2SSean Farley if (set && eqp != NULL) 3065b78c6c2SSean Farley (void) setenv(ss, eqp + 1, 1); 3075b78c6c2SSean Farley else 3085b78c6c2SSean Farley (void) unsetenv(ss); 3095b78c6c2SSean Farley ckfree(ss); 3101632bf1aSJilles Tjoelker INTON; 3115b78c6c2SSean Farley 3125b78c6c2SSean Farley return; 3135b78c6c2SSean Farley } 3145b78c6c2SSean Farley 3155b78c6c2SSean Farley 3164b88c807SRodney W. Grimes /* 3174b88c807SRodney W. Grimes * Same as setvar except that the variable and value are passed in 3184b88c807SRodney W. Grimes * the first argument as name=value. Since the first argument will 3194b88c807SRodney W. Grimes * be actually stored in the table, it should not be a string that 3204b88c807SRodney W. Grimes * will go away. 3214b88c807SRodney W. Grimes */ 3224b88c807SRodney W. Grimes 3234b88c807SRodney W. Grimes void 3245134c3f7SWarner Losh setvareq(char *s, int flags) 3254b88c807SRodney W. Grimes { 3264b88c807SRodney W. Grimes struct var *vp, **vpp; 3273a99ed46SJilles Tjoelker int nlen; 3284b88c807SRodney W. Grimes 329b8ec435eSMartin Cracauer if (aflag) 330b8ec435eSMartin Cracauer flags |= VEXPORT; 331c543e1aeSJilles Tjoelker if (forcelocal && !(flags & (VNOSET | VNOLOCAL))) 332c543e1aeSJilles Tjoelker mklocal(s); 3333a99ed46SJilles Tjoelker vp = find_var(s, &vpp, &nlen); 3343a99ed46SJilles Tjoelker if (vp != NULL) { 33589d4f883SJilles Tjoelker if (vp->flags & VREADONLY) { 33689d4f883SJilles Tjoelker if ((flags & (VTEXTFIXED|VSTACK)) == 0) 33789d4f883SJilles Tjoelker ckfree(s); 338d41b2be1SJilles Tjoelker error("%.*s: is read only", vp->name_len, vp->text); 33989d4f883SJilles Tjoelker } 340d73dba75SJilles Tjoelker if (flags & VNOSET) { 341d73dba75SJilles Tjoelker if ((flags & (VTEXTFIXED|VSTACK)) == 0) 342d73dba75SJilles Tjoelker ckfree(s); 343850460c0SJilles Tjoelker return; 344d73dba75SJilles Tjoelker } 3454b88c807SRodney W. Grimes INTOFF; 346ab0a2172SSteve Price 347ab0a2172SSteve Price if (vp->func && (flags & VNOFUNC) == 0) 3483a99ed46SJilles Tjoelker (*vp->func)(s + vp->name_len + 1); 349ab0a2172SSteve Price 3504b88c807SRodney W. Grimes if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0) 3514b88c807SRodney W. Grimes ckfree(vp->text); 352ab0a2172SSteve Price 3534b88c807SRodney W. Grimes vp->flags &= ~(VTEXTFIXED|VSTACK|VUNSET); 3544b88c807SRodney W. Grimes vp->flags |= flags; 3554b88c807SRodney W. Grimes vp->text = s; 356ab0a2172SSteve Price 357ab0a2172SSteve Price /* 358ab0a2172SSteve Price * We could roll this to a function, to handle it as 359ab0a2172SSteve Price * a regular variable function callback, but why bother? 36057063576SJilles Tjoelker * 36157063576SJilles Tjoelker * Note: this assumes iflag is not set to 1 initially. 36259e0cc8eSJilles Tjoelker * As part of initvar(), this is called before arguments 36357063576SJilles Tjoelker * are looked at. 364ab0a2172SSteve Price */ 36557063576SJilles Tjoelker if ((vp == &vmpath || (vp == &vmail && ! mpathset())) && 36657063576SJilles Tjoelker iflag == 1) 3674b88c807SRodney W. Grimes chkmail(1); 368ba726b8aSAndrey A. Chernov if ((vp->flags & VEXPORT) && localevar(s)) { 3695b78c6c2SSean Farley change_env(s, 1); 370ba726b8aSAndrey A. Chernov (void) setlocale(LC_ALL, ""); 3716ed74a0aSJilles Tjoelker updatecharset(); 372ba726b8aSAndrey A. Chernov } 3734b88c807SRodney W. Grimes INTON; 3744b88c807SRodney W. Grimes return; 3754b88c807SRodney W. Grimes } 3764b88c807SRodney W. Grimes /* not found */ 377d73dba75SJilles Tjoelker if (flags & VNOSET) { 378d73dba75SJilles Tjoelker if ((flags & (VTEXTFIXED|VSTACK)) == 0) 379d73dba75SJilles Tjoelker ckfree(s); 380850460c0SJilles Tjoelker return; 381d73dba75SJilles Tjoelker } 3821632bf1aSJilles Tjoelker INTOFF; 3834b88c807SRodney W. Grimes vp = ckmalloc(sizeof (*vp)); 3844b88c807SRodney W. Grimes vp->flags = flags; 3854b88c807SRodney W. Grimes vp->text = s; 3863a99ed46SJilles Tjoelker vp->name_len = nlen; 3874b88c807SRodney W. Grimes vp->next = *vpp; 388ab0a2172SSteve Price vp->func = NULL; 3894b88c807SRodney W. Grimes *vpp = vp; 390ba726b8aSAndrey A. Chernov if ((vp->flags & VEXPORT) && localevar(s)) { 3915b78c6c2SSean Farley change_env(s, 1); 392ba726b8aSAndrey A. Chernov (void) setlocale(LC_ALL, ""); 3936ed74a0aSJilles Tjoelker updatecharset(); 394ba726b8aSAndrey A. Chernov } 395ba726b8aSAndrey A. Chernov INTON; 3964b88c807SRodney W. Grimes } 3974b88c807SRodney W. Grimes 3984b88c807SRodney W. Grimes 39922afca9bSJilles Tjoelker static void 40022afca9bSJilles Tjoelker setvareq_const(const char *s, int flags) 40122afca9bSJilles Tjoelker { 40222afca9bSJilles Tjoelker setvareq(__DECONST(char *, s), flags | VTEXTFIXED); 40322afca9bSJilles Tjoelker } 40422afca9bSJilles Tjoelker 4054b88c807SRodney W. Grimes 4064b88c807SRodney W. Grimes /* 4074b88c807SRodney W. Grimes * Process a linked list of variable assignments. 4084b88c807SRodney W. Grimes */ 4094b88c807SRodney W. Grimes 4104b88c807SRodney W. Grimes void 4118ef0ae8aSJilles Tjoelker listsetvar(struct arglist *list, int flags) 4124b88c807SRodney W. Grimes { 4138ef0ae8aSJilles Tjoelker int i; 4144b88c807SRodney W. Grimes 4154b88c807SRodney W. Grimes INTOFF; 4168ef0ae8aSJilles Tjoelker for (i = 0; i < list->count; i++) 4178ef0ae8aSJilles Tjoelker setvareq(savestr(list->args[i]), flags); 4184b88c807SRodney W. Grimes INTON; 4194b88c807SRodney W. Grimes } 4204b88c807SRodney W. Grimes 4214b88c807SRodney W. Grimes 4224b88c807SRodney W. Grimes 4234b88c807SRodney W. Grimes /* 4244b88c807SRodney W. Grimes * Find the value of a variable. Returns NULL if not set. 4254b88c807SRodney W. Grimes */ 4264b88c807SRodney W. Grimes 4274b88c807SRodney W. Grimes char * 4282cac6e36SJilles Tjoelker lookupvar(const char *name) 4294b88c807SRodney W. Grimes { 4304b88c807SRodney W. Grimes struct var *v; 4314b88c807SRodney W. Grimes 4323a99ed46SJilles Tjoelker v = find_var(name, NULL, NULL); 4333a99ed46SJilles Tjoelker if (v == NULL || v->flags & VUNSET) 4344b88c807SRodney W. Grimes return NULL; 4353a99ed46SJilles Tjoelker return v->text + v->name_len + 1; 4364b88c807SRodney W. Grimes } 4374b88c807SRodney W. Grimes 4384b88c807SRodney W. Grimes 4394b88c807SRodney W. Grimes 4404b88c807SRodney W. Grimes /* 4414b88c807SRodney W. Grimes * Search the environment of a builtin command. If the second argument 4424b88c807SRodney W. Grimes * is nonzero, return the value of a variable even if it hasn't been 4434b88c807SRodney W. Grimes * exported. 4444b88c807SRodney W. Grimes */ 4454b88c807SRodney W. Grimes 4464b88c807SRodney W. Grimes char * 4472cac6e36SJilles Tjoelker bltinlookup(const char *name, int doall) 4484b88c807SRodney W. Grimes { 4494b88c807SRodney W. Grimes struct var *v; 450011d162dSJilles Tjoelker char *result; 4518ef0ae8aSJilles Tjoelker int i; 4524b88c807SRodney W. Grimes 453011d162dSJilles Tjoelker result = NULL; 4548ef0ae8aSJilles Tjoelker if (cmdenviron) for (i = 0; i < cmdenviron->count; i++) { 4558ef0ae8aSJilles Tjoelker if (varequal(cmdenviron->args[i], name)) 4568ef0ae8aSJilles Tjoelker result = strchr(cmdenviron->args[i], '=') + 1; 4574b88c807SRodney W. Grimes } 458011d162dSJilles Tjoelker if (result != NULL) 459011d162dSJilles Tjoelker return result; 4603a99ed46SJilles Tjoelker 4613a99ed46SJilles Tjoelker v = find_var(name, NULL, NULL); 4623a99ed46SJilles Tjoelker if (v == NULL || v->flags & VUNSET || 4633a99ed46SJilles Tjoelker (!doall && (v->flags & VEXPORT) == 0)) 4644b88c807SRodney W. Grimes return NULL; 4653a99ed46SJilles Tjoelker return v->text + v->name_len + 1; 4664b88c807SRodney W. Grimes } 4674b88c807SRodney W. Grimes 4684b88c807SRodney W. Grimes 469e4b50334SJilles Tjoelker /* 470e4b50334SJilles Tjoelker * Set up locale for a builtin (LANG/LC_* assignments). 471e4b50334SJilles Tjoelker */ 472e4b50334SJilles Tjoelker void 473e4b50334SJilles Tjoelker bltinsetlocale(void) 474e4b50334SJilles Tjoelker { 475e4b50334SJilles Tjoelker int act = 0; 476e4b50334SJilles Tjoelker char *loc, *locdef; 477e4b50334SJilles Tjoelker int i; 478e4b50334SJilles Tjoelker 4798ef0ae8aSJilles Tjoelker if (cmdenviron) for (i = 0; i < cmdenviron->count; i++) { 4808ef0ae8aSJilles Tjoelker if (localevar(cmdenviron->args[i])) { 481e4b50334SJilles Tjoelker act = 1; 482e4b50334SJilles Tjoelker break; 483e4b50334SJilles Tjoelker } 484e4b50334SJilles Tjoelker } 485e4b50334SJilles Tjoelker if (!act) 486e4b50334SJilles Tjoelker return; 487e4b50334SJilles Tjoelker loc = bltinlookup("LC_ALL", 0); 488e4b50334SJilles Tjoelker INTOFF; 489e4b50334SJilles Tjoelker if (loc != NULL) { 490e4b50334SJilles Tjoelker setlocale(LC_ALL, loc); 491e4b50334SJilles Tjoelker INTON; 4926ed74a0aSJilles Tjoelker updatecharset(); 493e4b50334SJilles Tjoelker return; 494e4b50334SJilles Tjoelker } 495e4b50334SJilles Tjoelker locdef = bltinlookup("LANG", 0); 496e4b50334SJilles Tjoelker for (i = 0; locale_names[i] != NULL; i++) { 497e4b50334SJilles Tjoelker loc = bltinlookup(locale_names[i], 0); 498e4b50334SJilles Tjoelker if (loc == NULL) 499e4b50334SJilles Tjoelker loc = locdef; 500e4b50334SJilles Tjoelker if (loc != NULL) 501e4b50334SJilles Tjoelker setlocale(locale_categories[i], loc); 502e4b50334SJilles Tjoelker } 503e4b50334SJilles Tjoelker INTON; 5046ed74a0aSJilles Tjoelker updatecharset(); 505e4b50334SJilles Tjoelker } 506e4b50334SJilles Tjoelker 507e4b50334SJilles Tjoelker /* 508e4b50334SJilles Tjoelker * Undo the effect of bltinlocaleset(). 509e4b50334SJilles Tjoelker */ 510e4b50334SJilles Tjoelker void 511e4b50334SJilles Tjoelker bltinunsetlocale(void) 512e4b50334SJilles Tjoelker { 5138ef0ae8aSJilles Tjoelker int i; 514e4b50334SJilles Tjoelker 515e4b50334SJilles Tjoelker INTOFF; 5168ef0ae8aSJilles Tjoelker if (cmdenviron) for (i = 0; i < cmdenviron->count; i++) { 5178ef0ae8aSJilles Tjoelker if (localevar(cmdenviron->args[i])) { 518e4b50334SJilles Tjoelker setlocale(LC_ALL, ""); 5196ed74a0aSJilles Tjoelker updatecharset(); 5203f2da875SJilles Tjoelker break; 521e4b50334SJilles Tjoelker } 522e4b50334SJilles Tjoelker } 523e4b50334SJilles Tjoelker INTON; 524e4b50334SJilles Tjoelker } 525e4b50334SJilles Tjoelker 5266ed74a0aSJilles Tjoelker /* 5276ed74a0aSJilles Tjoelker * Update the localeisutf8 flag. 5286ed74a0aSJilles Tjoelker */ 5296ed74a0aSJilles Tjoelker void 5306ed74a0aSJilles Tjoelker updatecharset(void) 5316ed74a0aSJilles Tjoelker { 5326ed74a0aSJilles Tjoelker char *charset; 5336ed74a0aSJilles Tjoelker 5346ed74a0aSJilles Tjoelker charset = nl_langinfo(CODESET); 5356ed74a0aSJilles Tjoelker localeisutf8 = !strcmp(charset, "UTF-8"); 5366ed74a0aSJilles Tjoelker } 5374b88c807SRodney W. Grimes 53807eb7033SJilles Tjoelker void 53907eb7033SJilles Tjoelker initcharset(void) 54007eb7033SJilles Tjoelker { 54107eb7033SJilles Tjoelker updatecharset(); 54207eb7033SJilles Tjoelker initial_localeisutf8 = localeisutf8; 54307eb7033SJilles Tjoelker } 54407eb7033SJilles Tjoelker 5454b88c807SRodney W. Grimes /* 5464b88c807SRodney W. Grimes * Generate a list of exported variables. This routine is used to construct 5474b88c807SRodney W. Grimes * the third argument to execve when executing a program. 5484b88c807SRodney W. Grimes */ 5494b88c807SRodney W. Grimes 5504b88c807SRodney W. Grimes char ** 5515134c3f7SWarner Losh environment(void) 5525134c3f7SWarner Losh { 5534b88c807SRodney W. Grimes int nenv; 5544b88c807SRodney W. Grimes struct var **vpp; 5554b88c807SRodney W. Grimes struct var *vp; 5564b88c807SRodney W. Grimes char **env, **ep; 5574b88c807SRodney W. Grimes 5584b88c807SRodney W. Grimes nenv = 0; 5594b88c807SRodney W. Grimes for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) { 5604b88c807SRodney W. Grimes for (vp = *vpp ; vp ; vp = vp->next) 561*56f33d07SJilles Tjoelker if ((vp->flags & (VEXPORT|VUNSET)) == VEXPORT) 5624b88c807SRodney W. Grimes nenv++; 5634b88c807SRodney W. Grimes } 5644b88c807SRodney W. Grimes ep = env = stalloc((nenv + 1) * sizeof *env); 5654b88c807SRodney W. Grimes for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) { 5664b88c807SRodney W. Grimes for (vp = *vpp ; vp ; vp = vp->next) 567*56f33d07SJilles Tjoelker if ((vp->flags & (VEXPORT|VUNSET)) == VEXPORT) 5684b88c807SRodney W. Grimes *ep++ = vp->text; 5694b88c807SRodney W. Grimes } 5704b88c807SRodney W. Grimes *ep = NULL; 5714b88c807SRodney W. Grimes return env; 5724b88c807SRodney W. Grimes } 5734b88c807SRodney W. Grimes 5744b88c807SRodney W. Grimes 57588328642SDavid E. O'Brien static int 576692f35fdSStefan Farfeleder var_compare(const void *a, const void *b) 577692f35fdSStefan Farfeleder { 578692f35fdSStefan Farfeleder const char *const *sa, *const *sb; 579692f35fdSStefan Farfeleder 580692f35fdSStefan Farfeleder sa = a; 581692f35fdSStefan Farfeleder sb = b; 582692f35fdSStefan Farfeleder /* 583692f35fdSStefan Farfeleder * This compares two var=value strings which creates a different 584692f35fdSStefan Farfeleder * order from what you would probably expect. POSIX is somewhat 585692f35fdSStefan Farfeleder * ambiguous on what should be sorted exactly. 586692f35fdSStefan Farfeleder */ 587692f35fdSStefan Farfeleder return strcoll(*sa, *sb); 588692f35fdSStefan Farfeleder } 589692f35fdSStefan Farfeleder 5904b88c807SRodney W. Grimes 5914b88c807SRodney W. Grimes /* 592cff1d849SJilles Tjoelker * Command to list all variables which are set. This is invoked from the 593cff1d849SJilles Tjoelker * set command when it is called without any options or operands. 5944b88c807SRodney W. Grimes */ 5954b88c807SRodney W. Grimes 5964b88c807SRodney W. Grimes int 5975134c3f7SWarner Losh showvarscmd(int argc __unused, char **argv __unused) 598aa9caaf6SPeter Wemm { 5994b88c807SRodney W. Grimes struct var **vpp; 6004b88c807SRodney W. Grimes struct var *vp; 60159258844STim J. Robbins const char *s; 602692f35fdSStefan Farfeleder const char **vars; 603692f35fdSStefan Farfeleder int i, n; 6044b88c807SRodney W. Grimes 605692f35fdSStefan Farfeleder /* 606692f35fdSStefan Farfeleder * POSIX requires us to sort the variables. 607692f35fdSStefan Farfeleder */ 608692f35fdSStefan Farfeleder n = 0; 6094b88c807SRodney W. Grimes for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) { 6104b88c807SRodney W. Grimes for (vp = *vpp; vp; vp = vp->next) { 611692f35fdSStefan Farfeleder if (!(vp->flags & VUNSET)) 612692f35fdSStefan Farfeleder n++; 613692f35fdSStefan Farfeleder } 614692f35fdSStefan Farfeleder } 615692f35fdSStefan Farfeleder 61633233ec7SJilles Tjoelker INTOFF; 617692f35fdSStefan Farfeleder vars = ckmalloc(n * sizeof(*vars)); 618692f35fdSStefan Farfeleder i = 0; 619692f35fdSStefan Farfeleder for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) { 620692f35fdSStefan Farfeleder for (vp = *vpp; vp; vp = vp->next) { 621692f35fdSStefan Farfeleder if (!(vp->flags & VUNSET)) 622692f35fdSStefan Farfeleder vars[i++] = vp->text; 623692f35fdSStefan Farfeleder } 624692f35fdSStefan Farfeleder } 625692f35fdSStefan Farfeleder 626692f35fdSStefan Farfeleder qsort(vars, n, sizeof(*vars), var_compare); 627692f35fdSStefan Farfeleder for (i = 0; i < n; i++) { 628f5f215e2SJilles Tjoelker /* 629f5f215e2SJilles Tjoelker * Skip improper variable names so the output remains usable as 630f5f215e2SJilles Tjoelker * shell input. 631f5f215e2SJilles Tjoelker */ 632f5f215e2SJilles Tjoelker if (!isassignment(vars[i])) 633f5f215e2SJilles Tjoelker continue; 634aeb5d065SJilles Tjoelker s = strchr(vars[i], '='); 635aeb5d065SJilles Tjoelker s++; 636aeb5d065SJilles Tjoelker outbin(vars[i], s - vars[i], out1); 637aeb5d065SJilles Tjoelker out1qstr(s); 63859258844STim J. Robbins out1c('\n'); 6394b88c807SRodney W. Grimes } 640692f35fdSStefan Farfeleder ckfree(vars); 64133233ec7SJilles Tjoelker INTON; 642692f35fdSStefan Farfeleder 6434b88c807SRodney W. Grimes return 0; 6444b88c807SRodney W. Grimes } 6454b88c807SRodney W. Grimes 6464b88c807SRodney W. Grimes 6474b88c807SRodney W. Grimes 6484b88c807SRodney W. Grimes /* 6494b88c807SRodney W. Grimes * The export and readonly commands. 6504b88c807SRodney W. Grimes */ 6514b88c807SRodney W. Grimes 6524b88c807SRodney W. Grimes int 6537cbda738SJilles Tjoelker exportcmd(int argc __unused, char **argv) 654aa9caaf6SPeter Wemm { 6554b88c807SRodney W. Grimes struct var **vpp; 6564b88c807SRodney W. Grimes struct var *vp; 6577cbda738SJilles Tjoelker char **ap; 6584b88c807SRodney W. Grimes char *name; 6594b88c807SRodney W. Grimes char *p; 66045086f8cSTim J. Robbins char *cmdname; 66145086f8cSTim J. Robbins int ch, values; 6624b88c807SRodney W. Grimes int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT; 6634b88c807SRodney W. Grimes 66445086f8cSTim J. Robbins cmdname = argv[0]; 66545086f8cSTim J. Robbins values = 0; 6667cbda738SJilles Tjoelker while ((ch = nextopt("p")) != '\0') { 66745086f8cSTim J. Robbins switch (ch) { 66845086f8cSTim J. Robbins case 'p': 66945086f8cSTim J. Robbins values = 1; 67045086f8cSTim J. Robbins break; 67145086f8cSTim J. Robbins } 67245086f8cSTim J. Robbins } 67345086f8cSTim J. Robbins 6747cbda738SJilles Tjoelker if (values && *argptr != NULL) 6753fda45ecSStefan Farfeleder error("-p requires no arguments"); 6767cbda738SJilles Tjoelker if (*argptr != NULL) { 6777cbda738SJilles Tjoelker for (ap = argptr; (name = *ap) != NULL; ap++) { 6784b88c807SRodney W. Grimes if ((p = strchr(name, '=')) != NULL) { 6794b88c807SRodney W. Grimes p++; 6804b88c807SRodney W. Grimes } else { 6813a99ed46SJilles Tjoelker vp = find_var(name, NULL, NULL); 6823a99ed46SJilles Tjoelker if (vp != NULL) { 6834b88c807SRodney W. Grimes vp->flags |= flag; 684ba726b8aSAndrey A. Chernov if ((vp->flags & VEXPORT) && localevar(vp->text)) { 6855b78c6c2SSean Farley change_env(vp->text, 1); 686ba726b8aSAndrey A. Chernov (void) setlocale(LC_ALL, ""); 6876ed74a0aSJilles Tjoelker updatecharset(); 688ba726b8aSAndrey A. Chernov } 6893a99ed46SJilles Tjoelker continue; 6904b88c807SRodney W. Grimes } 6914b88c807SRodney W. Grimes } 6924b88c807SRodney W. Grimes setvar(name, p, flag); 6934b88c807SRodney W. Grimes } 6944b88c807SRodney W. Grimes } else { 6954b88c807SRodney W. Grimes for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) { 6964b88c807SRodney W. Grimes for (vp = *vpp ; vp ; vp = vp->next) { 6974b88c807SRodney W. Grimes if (vp->flags & flag) { 69845086f8cSTim J. Robbins if (values) { 699f5f215e2SJilles Tjoelker /* 700f5f215e2SJilles Tjoelker * Skip improper variable names 701f5f215e2SJilles Tjoelker * so the output remains usable 702f5f215e2SJilles Tjoelker * as shell input. 703f5f215e2SJilles Tjoelker */ 704f5f215e2SJilles Tjoelker if (!isassignment(vp->text)) 705f5f215e2SJilles Tjoelker continue; 70645086f8cSTim J. Robbins out1str(cmdname); 70745086f8cSTim J. Robbins out1c(' '); 70845086f8cSTim J. Robbins } 70945086f8cSTim J. Robbins if (values && !(vp->flags & VUNSET)) { 710258ef734SJilles Tjoelker outbin(vp->text, 711258ef734SJilles Tjoelker vp->name_len + 1, out1); 712258ef734SJilles Tjoelker out1qstr(vp->text + 713258ef734SJilles Tjoelker vp->name_len + 1); 714aeb5d065SJilles Tjoelker } else 715258ef734SJilles Tjoelker outbin(vp->text, vp->name_len, 716aeb5d065SJilles Tjoelker out1); 7174b88c807SRodney W. Grimes out1c('\n'); 7184b88c807SRodney W. Grimes } 7194b88c807SRodney W. Grimes } 7204b88c807SRodney W. Grimes } 7214b88c807SRodney W. Grimes } 7224b88c807SRodney W. Grimes return 0; 7234b88c807SRodney W. Grimes } 7244b88c807SRodney W. Grimes 7254b88c807SRodney W. Grimes 7264b88c807SRodney W. Grimes /* 7274b88c807SRodney W. Grimes * The "local" command. 7284b88c807SRodney W. Grimes */ 7294b88c807SRodney W. Grimes 730aa9caaf6SPeter Wemm int 7315134c3f7SWarner Losh localcmd(int argc __unused, char **argv __unused) 732aa9caaf6SPeter Wemm { 7334b88c807SRodney W. Grimes char *name; 7344b88c807SRodney W. Grimes 735056fd329SJilles Tjoelker nextopt(""); 7364b88c807SRodney W. Grimes if (! in_function()) 7374b88c807SRodney W. Grimes error("Not in a function"); 7384b88c807SRodney W. Grimes while ((name = *argptr++) != NULL) { 7394b88c807SRodney W. Grimes mklocal(name); 7404b88c807SRodney W. Grimes } 7414b88c807SRodney W. Grimes return 0; 7424b88c807SRodney W. Grimes } 7434b88c807SRodney W. Grimes 7444b88c807SRodney W. Grimes 7454b88c807SRodney W. Grimes /* 7464b88c807SRodney W. Grimes * Make a variable a local variable. When a variable is made local, it's 7474b88c807SRodney W. Grimes * value and flags are saved in a localvar structure. The saved values 7484b88c807SRodney W. Grimes * will be restored when the shell function returns. We handle the name 7494b88c807SRodney W. Grimes * "-" as a special case. 7504b88c807SRodney W. Grimes */ 7514b88c807SRodney W. Grimes 7524b88c807SRodney W. Grimes void 7535134c3f7SWarner Losh mklocal(char *name) 7544b88c807SRodney W. Grimes { 7554b88c807SRodney W. Grimes struct localvar *lvp; 7564b88c807SRodney W. Grimes struct var **vpp; 7574b88c807SRodney W. Grimes struct var *vp; 7584b88c807SRodney W. Grimes 7594b88c807SRodney W. Grimes INTOFF; 7604b88c807SRodney W. Grimes lvp = ckmalloc(sizeof (struct localvar)); 7614b88c807SRodney W. Grimes if (name[0] == '-' && name[1] == '\0') { 7623da40d4aSJilles Tjoelker lvp->text = ckmalloc(sizeof optval); 7633da40d4aSJilles Tjoelker memcpy(lvp->text, optval, sizeof optval); 7644b88c807SRodney W. Grimes vp = NULL; 7654b88c807SRodney W. Grimes } else { 7663a99ed46SJilles Tjoelker vp = find_var(name, &vpp, NULL); 7674b88c807SRodney W. Grimes if (vp == NULL) { 7684b88c807SRodney W. Grimes if (strchr(name, '=')) 769c543e1aeSJilles Tjoelker setvareq(savestr(name), VSTRFIXED | VNOLOCAL); 7704b88c807SRodney W. Grimes else 771c543e1aeSJilles Tjoelker setvar(name, NULL, VSTRFIXED | VNOLOCAL); 7724b88c807SRodney W. Grimes vp = *vpp; /* the new variable */ 7734b88c807SRodney W. Grimes lvp->text = NULL; 7744b88c807SRodney W. Grimes lvp->flags = VUNSET; 7754b88c807SRodney W. Grimes } else { 7764b88c807SRodney W. Grimes lvp->text = vp->text; 7774b88c807SRodney W. Grimes lvp->flags = vp->flags; 7784b88c807SRodney W. Grimes vp->flags |= VSTRFIXED|VTEXTFIXED; 7793a99ed46SJilles Tjoelker if (name[vp->name_len] == '=') 780c543e1aeSJilles Tjoelker setvareq(savestr(name), VNOLOCAL); 7814b88c807SRodney W. Grimes } 7824b88c807SRodney W. Grimes } 7834b88c807SRodney W. Grimes lvp->vp = vp; 7844b88c807SRodney W. Grimes lvp->next = localvars; 7854b88c807SRodney W. Grimes localvars = lvp; 7864b88c807SRodney W. Grimes INTON; 7874b88c807SRodney W. Grimes } 7884b88c807SRodney W. Grimes 7894b88c807SRodney W. Grimes 7904b88c807SRodney W. Grimes /* 7914b88c807SRodney W. Grimes * Called after a function returns. 7924b88c807SRodney W. Grimes */ 7934b88c807SRodney W. Grimes 7944b88c807SRodney W. Grimes void 7955134c3f7SWarner Losh poplocalvars(void) 7965134c3f7SWarner Losh { 7974b88c807SRodney W. Grimes struct localvar *lvp; 7984b88c807SRodney W. Grimes struct var *vp; 799cf45f124SJilles Tjoelker int islocalevar; 8004b88c807SRodney W. Grimes 8011632bf1aSJilles Tjoelker INTOFF; 8024b88c807SRodney W. Grimes while ((lvp = localvars) != NULL) { 8034b88c807SRodney W. Grimes localvars = lvp->next; 8044b88c807SRodney W. Grimes vp = lvp->vp; 8054b88c807SRodney W. Grimes if (vp == NULL) { /* $- saved */ 8063da40d4aSJilles Tjoelker memcpy(optval, lvp->text, sizeof optval); 8074b88c807SRodney W. Grimes ckfree(lvp->text); 8084a7b1013SJilles Tjoelker optschanged(); 8094b88c807SRodney W. Grimes } else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) { 810a9bdd616SJilles Tjoelker vp->flags &= ~VREADONLY; 8114b88c807SRodney W. Grimes (void)unsetvar(vp->text); 8124b88c807SRodney W. Grimes } else { 813cf45f124SJilles Tjoelker islocalevar = (vp->flags | lvp->flags) & VEXPORT && 814cf45f124SJilles Tjoelker localevar(lvp->text); 8154b88c807SRodney W. Grimes if ((vp->flags & VTEXTFIXED) == 0) 8164b88c807SRodney W. Grimes ckfree(vp->text); 8174b88c807SRodney W. Grimes vp->flags = lvp->flags; 8184b88c807SRodney W. Grimes vp->text = lvp->text; 819cf45f124SJilles Tjoelker if (vp->func) 820cf45f124SJilles Tjoelker (*vp->func)(vp->text + vp->name_len + 1); 821cf45f124SJilles Tjoelker if (islocalevar) { 822cf45f124SJilles Tjoelker change_env(vp->text, vp->flags & VEXPORT && 823cf45f124SJilles Tjoelker (vp->flags & VUNSET) == 0); 824cf45f124SJilles Tjoelker setlocale(LC_ALL, ""); 825cf45f124SJilles Tjoelker updatecharset(); 826cf45f124SJilles Tjoelker } 8274b88c807SRodney W. Grimes } 8284b88c807SRodney W. Grimes ckfree(lvp); 8294b88c807SRodney W. Grimes } 8301632bf1aSJilles Tjoelker INTON; 8314b88c807SRodney W. Grimes } 8324b88c807SRodney W. Grimes 8334b88c807SRodney W. Grimes 834aa9caaf6SPeter Wemm int 8355134c3f7SWarner Losh setvarcmd(int argc, char **argv) 836aa9caaf6SPeter Wemm { 8374b88c807SRodney W. Grimes if (argc <= 2) 8384b88c807SRodney W. Grimes return unsetcmd(argc, argv); 8394b88c807SRodney W. Grimes else if (argc == 3) 8404b88c807SRodney W. Grimes setvar(argv[1], argv[2], 0); 8414b88c807SRodney W. Grimes else 842274110dfSJilles Tjoelker error("too many arguments"); 8434b88c807SRodney W. Grimes return 0; 8444b88c807SRodney W. Grimes } 8454b88c807SRodney W. Grimes 8464b88c807SRodney W. Grimes 8474b88c807SRodney W. Grimes /* 848cff1d849SJilles Tjoelker * The unset builtin command. 8494b88c807SRodney W. Grimes */ 8504b88c807SRodney W. Grimes 851aa9caaf6SPeter Wemm int 8525134c3f7SWarner Losh unsetcmd(int argc __unused, char **argv __unused) 853aa9caaf6SPeter Wemm { 8544b88c807SRodney W. Grimes char **ap; 8554b88c807SRodney W. Grimes int i; 8564b88c807SRodney W. Grimes int flg_func = 0; 8574b88c807SRodney W. Grimes int flg_var = 0; 8584b88c807SRodney W. Grimes int ret = 0; 8594b88c807SRodney W. Grimes 8604b88c807SRodney W. Grimes while ((i = nextopt("vf")) != '\0') { 8614b88c807SRodney W. Grimes if (i == 'f') 8624b88c807SRodney W. Grimes flg_func = 1; 8634b88c807SRodney W. Grimes else 8644b88c807SRodney W. Grimes flg_var = 1; 8654b88c807SRodney W. Grimes } 8664b88c807SRodney W. Grimes if (flg_func == 0 && flg_var == 0) 8674b88c807SRodney W. Grimes flg_var = 1; 8684b88c807SRodney W. Grimes 8691632bf1aSJilles Tjoelker INTOFF; 8704b88c807SRodney W. Grimes for (ap = argptr; *ap ; ap++) { 8714b88c807SRodney W. Grimes if (flg_func) 8724b88c807SRodney W. Grimes ret |= unsetfunc(*ap); 8734b88c807SRodney W. Grimes if (flg_var) 8744b88c807SRodney W. Grimes ret |= unsetvar(*ap); 8754b88c807SRodney W. Grimes } 8761632bf1aSJilles Tjoelker INTON; 8774b88c807SRodney W. Grimes return ret; 8784b88c807SRodney W. Grimes } 8794b88c807SRodney W. Grimes 8804b88c807SRodney W. Grimes 8814b88c807SRodney W. Grimes /* 8824b88c807SRodney W. Grimes * Unset the specified variable. 8831632bf1aSJilles Tjoelker * Called with interrupts off. 8844b88c807SRodney W. Grimes */ 8854b88c807SRodney W. Grimes 886ab0a2172SSteve Price int 8872cac6e36SJilles Tjoelker unsetvar(const char *s) 8884b88c807SRodney W. Grimes { 8894b88c807SRodney W. Grimes struct var **vpp; 8904b88c807SRodney W. Grimes struct var *vp; 8914b88c807SRodney W. Grimes 8923a99ed46SJilles Tjoelker vp = find_var(s, &vpp, NULL); 8933a99ed46SJilles Tjoelker if (vp == NULL) 8943a99ed46SJilles Tjoelker return (0); 8954b88c807SRodney W. Grimes if (vp->flags & VREADONLY) 8964b88c807SRodney W. Grimes return (1); 8973a99ed46SJilles Tjoelker if (vp->text[vp->name_len + 1] != '\0') 898781bfb5aSJilles Tjoelker setvar(s, "", 0); 899ba726b8aSAndrey A. Chernov if ((vp->flags & VEXPORT) && localevar(vp->text)) { 9005b78c6c2SSean Farley change_env(s, 0); 901ba726b8aSAndrey A. Chernov setlocale(LC_ALL, ""); 9026ed74a0aSJilles Tjoelker updatecharset(); 903ba726b8aSAndrey A. Chernov } 9044b88c807SRodney W. Grimes vp->flags &= ~VEXPORT; 9054b88c807SRodney W. Grimes vp->flags |= VUNSET; 9064b88c807SRodney W. Grimes if ((vp->flags & VSTRFIXED) == 0) { 9074b88c807SRodney W. Grimes if ((vp->flags & VTEXTFIXED) == 0) 9084b88c807SRodney W. Grimes ckfree(vp->text); 9094b88c807SRodney W. Grimes *vpp = vp->next; 9104b88c807SRodney W. Grimes ckfree(vp); 9114b88c807SRodney W. Grimes } 9124b88c807SRodney W. Grimes return (0); 9134b88c807SRodney W. Grimes } 9144b88c807SRodney W. Grimes 9154b88c807SRodney W. Grimes 9164b88c807SRodney W. Grimes 9174b88c807SRodney W. Grimes /* 9186c7d8328SEitan Adler * Returns true if the two strings specify the same variable. The first 9194b88c807SRodney W. Grimes * variable name is terminated by '='; the second may be terminated by 9204b88c807SRodney W. Grimes * either '=' or '\0'. 9214b88c807SRodney W. Grimes */ 9224b88c807SRodney W. Grimes 92388328642SDavid E. O'Brien static int 9242cac6e36SJilles Tjoelker varequal(const char *p, const char *q) 9254b88c807SRodney W. Grimes { 9264b88c807SRodney W. Grimes while (*p == *q++) { 9274b88c807SRodney W. Grimes if (*p++ == '=') 9284b88c807SRodney W. Grimes return 1; 9294b88c807SRodney W. Grimes } 9304b88c807SRodney W. Grimes if (*p == '=' && *(q - 1) == '\0') 9314b88c807SRodney W. Grimes return 1; 9324b88c807SRodney W. Grimes return 0; 9334b88c807SRodney W. Grimes } 9343a99ed46SJilles Tjoelker 9353a99ed46SJilles Tjoelker /* 9363a99ed46SJilles Tjoelker * Search for a variable. 9373a99ed46SJilles Tjoelker * 'name' may be terminated by '=' or a NUL. 9383a99ed46SJilles Tjoelker * vppp is set to the pointer to vp, or the list head if vp isn't found 9396c7d8328SEitan Adler * lenp is set to the number of characters in 'name' 9403a99ed46SJilles Tjoelker */ 9413a99ed46SJilles Tjoelker 9423a99ed46SJilles Tjoelker static struct var * 9433a99ed46SJilles Tjoelker find_var(const char *name, struct var ***vppp, int *lenp) 9443a99ed46SJilles Tjoelker { 9453a99ed46SJilles Tjoelker unsigned int hashval; 9463a99ed46SJilles Tjoelker int len; 9473a99ed46SJilles Tjoelker struct var *vp, **vpp; 9483a99ed46SJilles Tjoelker const char *p = name; 9493a99ed46SJilles Tjoelker 9503a99ed46SJilles Tjoelker hashval = 0; 9513a99ed46SJilles Tjoelker while (*p && *p != '=') 9523a99ed46SJilles Tjoelker hashval = 2 * hashval + (unsigned char)*p++; 9533a99ed46SJilles Tjoelker len = p - name; 9543a99ed46SJilles Tjoelker 9553a99ed46SJilles Tjoelker if (lenp) 9563a99ed46SJilles Tjoelker *lenp = len; 9573a99ed46SJilles Tjoelker vpp = &vartab[hashval % VTABSIZE]; 9583a99ed46SJilles Tjoelker if (vppp) 9593a99ed46SJilles Tjoelker *vppp = vpp; 9603a99ed46SJilles Tjoelker 9613a99ed46SJilles Tjoelker for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) { 9623a99ed46SJilles Tjoelker if (vp->name_len != len) 9633a99ed46SJilles Tjoelker continue; 9643a99ed46SJilles Tjoelker if (memcmp(vp->text, name, len) != 0) 9653a99ed46SJilles Tjoelker continue; 9663a99ed46SJilles Tjoelker if (vppp) 9673a99ed46SJilles Tjoelker *vppp = vpp; 9683a99ed46SJilles Tjoelker return vp; 9693a99ed46SJilles Tjoelker } 9703a99ed46SJilles Tjoelker return NULL; 9713a99ed46SJilles Tjoelker } 972