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> 50ba726b8aSAndrey A. Chernov 514b88c807SRodney W. Grimes #include "shell.h" 524b88c807SRodney W. Grimes #include "output.h" 534b88c807SRodney W. Grimes #include "expand.h" 544b88c807SRodney W. Grimes #include "nodes.h" /* for other headers */ 554b88c807SRodney W. Grimes #include "eval.h" /* defines cmdenviron */ 564b88c807SRodney W. Grimes #include "exec.h" 574b88c807SRodney W. Grimes #include "syntax.h" 584b88c807SRodney W. Grimes #include "options.h" 594b88c807SRodney W. Grimes #include "mail.h" 604b88c807SRodney W. Grimes #include "var.h" 614b88c807SRodney W. Grimes #include "memalloc.h" 624b88c807SRodney W. Grimes #include "error.h" 634b88c807SRodney W. Grimes #include "mystring.h" 64ab0a2172SSteve Price #include "parser.h" 65aa9caaf6SPeter Wemm #ifndef NO_HISTORY 66aa9caaf6SPeter Wemm #include "myhistedit.h" 67aa9caaf6SPeter Wemm #endif 684b88c807SRodney W. Grimes 694b88c807SRodney W. Grimes 704b88c807SRodney W. Grimes #define VTABSIZE 39 714b88c807SRodney W. Grimes 724b88c807SRodney W. Grimes 734b88c807SRodney W. Grimes struct varinit { 744b88c807SRodney W. Grimes struct var *var; 754b88c807SRodney W. Grimes int flags; 76c6c5dd37SJilles Tjoelker const char *text; 775134c3f7SWarner Losh void (*func)(const char *); 784b88c807SRodney W. Grimes }; 794b88c807SRodney W. Grimes 804b88c807SRodney W. Grimes 81aa9caaf6SPeter Wemm #ifndef NO_HISTORY 824b88c807SRodney W. Grimes struct var vhistsize; 83580eefdfSJilles Tjoelker struct var vterm; 84aa9caaf6SPeter Wemm #endif 854b88c807SRodney W. Grimes struct var vifs; 864b88c807SRodney W. Grimes struct var vmail; 874b88c807SRodney W. Grimes struct var vmpath; 884b88c807SRodney W. Grimes struct var vpath; 8939dccc6fSTim J. Robbins struct var vppid; 904b88c807SRodney W. Grimes struct var vps1; 914b88c807SRodney W. Grimes struct var vps2; 92120c8e6cSStefan Farfeleder struct var vps4; 934b88c807SRodney W. Grimes struct var vvers; 94aa7b6f82SDavid E. O'Brien static struct var voptind; 954b88c807SRodney W. Grimes 96aa7b6f82SDavid E. O'Brien static const struct varinit varinit[] = { 97aa9caaf6SPeter Wemm #ifndef NO_HISTORY 98c6c5dd37SJilles Tjoelker { &vhistsize, VUNSET, "HISTSIZE=", 99ab0a2172SSteve Price sethistsize }, 100aa9caaf6SPeter Wemm #endif 101c6c5dd37SJilles Tjoelker { &vifs, 0, "IFS= \t\n", 102ab0a2172SSteve Price NULL }, 103c6c5dd37SJilles Tjoelker { &vmail, VUNSET, "MAIL=", 104ab0a2172SSteve Price NULL }, 105c6c5dd37SJilles Tjoelker { &vmpath, VUNSET, "MAILPATH=", 106ab0a2172SSteve Price NULL }, 107c6c5dd37SJilles Tjoelker { &vpath, 0, "PATH=" _PATH_DEFPATH, 108ab0a2172SSteve Price changepath }, 109c6c5dd37SJilles Tjoelker { &vppid, VUNSET, "PPID=", 11039dccc6fSTim J. Robbins NULL }, 1114b88c807SRodney W. Grimes /* 1124b88c807SRodney W. Grimes * vps1 depends on uid 1134b88c807SRodney W. Grimes */ 114c6c5dd37SJilles Tjoelker { &vps2, 0, "PS2=> ", 115ab0a2172SSteve Price NULL }, 116c6c5dd37SJilles Tjoelker { &vps4, 0, "PS4=+ ", 117120c8e6cSStefan Farfeleder NULL }, 118580eefdfSJilles Tjoelker #ifndef NO_HISTORY 119580eefdfSJilles Tjoelker { &vterm, VUNSET, "TERM=", 120580eefdfSJilles Tjoelker setterm }, 121580eefdfSJilles Tjoelker #endif 122c6c5dd37SJilles Tjoelker { &voptind, 0, "OPTIND=1", 123ab0a2172SSteve Price getoptsreset }, 124ab0a2172SSteve Price { NULL, 0, NULL, 125ab0a2172SSteve Price NULL } 1264b88c807SRodney W. Grimes }; 1274b88c807SRodney W. Grimes 128aa7b6f82SDavid E. O'Brien static struct var *vartab[VTABSIZE]; 1294b88c807SRodney W. Grimes 130aa7b6f82SDavid E. O'Brien static const char *const locale_names[7] = { 131e4b50334SJilles Tjoelker "LC_COLLATE", "LC_CTYPE", "LC_MONETARY", 132e4b50334SJilles Tjoelker "LC_NUMERIC", "LC_TIME", "LC_MESSAGES", NULL 133e4b50334SJilles Tjoelker }; 134aa7b6f82SDavid E. O'Brien static const int locale_categories[7] = { 135e4b50334SJilles Tjoelker LC_COLLATE, LC_CTYPE, LC_MONETARY, LC_NUMERIC, LC_TIME, LC_MESSAGES, 0 136e4b50334SJilles Tjoelker }; 137e4b50334SJilles Tjoelker 13888328642SDavid E. O'Brien static struct var **hashvar(const char *); 13988328642SDavid E. O'Brien static int varequal(const char *, const char *); 14088328642SDavid E. O'Brien static int localevar(const char *); 1414b88c807SRodney W. Grimes 1424b88c807SRodney W. Grimes /* 143b3decf89SJens Schweikhardt * Initialize the variable symbol tables and import the environment. 1444b88c807SRodney W. Grimes */ 1454b88c807SRodney W. Grimes 1464b88c807SRodney W. Grimes #ifdef mkinit 1474b88c807SRodney W. Grimes INCLUDE "var.h" 148384aedabSJilles Tjoelker MKINIT char **environ; 1494b88c807SRodney W. Grimes INIT { 1504b88c807SRodney W. Grimes char **envp; 1514b88c807SRodney W. Grimes 1524b88c807SRodney W. Grimes initvar(); 1534b88c807SRodney W. Grimes for (envp = environ ; *envp ; envp++) { 1544b88c807SRodney W. Grimes if (strchr(*envp, '=')) { 1554b88c807SRodney W. Grimes setvareq(*envp, VEXPORT|VTEXTFIXED); 1564b88c807SRodney W. Grimes } 1574b88c807SRodney W. Grimes } 1584b88c807SRodney W. Grimes } 1594b88c807SRodney W. Grimes #endif 1604b88c807SRodney W. Grimes 1614b88c807SRodney W. Grimes 1624b88c807SRodney W. Grimes /* 1634b88c807SRodney W. Grimes * This routine initializes the builtin variables. It is called when the 1644b88c807SRodney W. Grimes * shell is initialized and again when a shell procedure is spawned. 1654b88c807SRodney W. Grimes */ 1664b88c807SRodney W. Grimes 1674b88c807SRodney W. Grimes void 1685134c3f7SWarner Losh initvar(void) 1695134c3f7SWarner Losh { 17039dccc6fSTim J. Robbins char ppid[20]; 1714b88c807SRodney W. Grimes const struct varinit *ip; 1724b88c807SRodney W. Grimes struct var *vp; 1734b88c807SRodney W. Grimes struct var **vpp; 1744b88c807SRodney W. Grimes 1754b88c807SRodney W. Grimes for (ip = varinit ; (vp = ip->var) != NULL ; ip++) { 1764b88c807SRodney W. Grimes if ((vp->flags & VEXPORT) == 0) { 1774b88c807SRodney W. Grimes vpp = hashvar(ip->text); 1784b88c807SRodney W. Grimes vp->next = *vpp; 1794b88c807SRodney W. Grimes *vpp = vp; 180c6c5dd37SJilles Tjoelker vp->text = __DECONST(char *, ip->text); 181c6c5dd37SJilles Tjoelker vp->flags = ip->flags | VSTRFIXED | VTEXTFIXED; 182ab0a2172SSteve Price vp->func = ip->func; 1834b88c807SRodney W. Grimes } 1844b88c807SRodney W. Grimes } 1854b88c807SRodney W. Grimes /* 1864b88c807SRodney W. Grimes * PS1 depends on uid 1874b88c807SRodney W. Grimes */ 1884b88c807SRodney W. Grimes if ((vps1.flags & VEXPORT) == 0) { 1894b88c807SRodney W. Grimes vpp = hashvar("PS1="); 1904b88c807SRodney W. Grimes vps1.next = *vpp; 1914b88c807SRodney W. Grimes *vpp = &vps1; 192c6c5dd37SJilles Tjoelker vps1.text = __DECONST(char *, geteuid() ? "PS1=$ " : "PS1=# "); 1934b88c807SRodney W. Grimes vps1.flags = VSTRFIXED|VTEXTFIXED; 1944b88c807SRodney W. Grimes } 19539dccc6fSTim J. Robbins if ((vppid.flags & VEXPORT) == 0) { 19639dccc6fSTim J. Robbins fmtstr(ppid, sizeof(ppid), "%d", (int)getppid()); 19739dccc6fSTim J. Robbins setvarsafe("PPID", ppid, 0); 19839dccc6fSTim J. Robbins } 1994b88c807SRodney W. Grimes } 2004b88c807SRodney W. Grimes 2014b88c807SRodney W. Grimes /* 202ab0a2172SSteve Price * Safe version of setvar, returns 1 on success 0 on failure. 203ab0a2172SSteve Price */ 204ab0a2172SSteve Price 205ab0a2172SSteve Price int 2062cac6e36SJilles Tjoelker setvarsafe(const char *name, const char *val, int flags) 207ab0a2172SSteve Price { 208ab0a2172SSteve Price struct jmploc jmploc; 209224fbf9fSJilles Tjoelker struct jmploc *const savehandler = handler; 210ab0a2172SSteve Price int err = 0; 2119922c6d2SJilles Tjoelker int inton; 212ab0a2172SSteve Price 2139922c6d2SJilles Tjoelker inton = is_int_on(); 214ab0a2172SSteve Price if (setjmp(jmploc.loc)) 215ab0a2172SSteve Price err = 1; 216ab0a2172SSteve Price else { 217ab0a2172SSteve Price handler = &jmploc; 218ab0a2172SSteve Price setvar(name, val, flags); 219ab0a2172SSteve Price } 220ab0a2172SSteve Price handler = savehandler; 2219922c6d2SJilles Tjoelker SETINTON(inton); 222ab0a2172SSteve Price return err; 223ab0a2172SSteve Price } 224ab0a2172SSteve Price 225ab0a2172SSteve Price /* 226b3decf89SJens Schweikhardt * Set the value of a variable. The flags argument is stored with the 2274b88c807SRodney W. Grimes * flags of the variable. If val is NULL, the variable is unset. 2284b88c807SRodney W. Grimes */ 2294b88c807SRodney W. Grimes 2304b88c807SRodney W. Grimes void 2312cac6e36SJilles Tjoelker setvar(const char *name, const char *val, int flags) 2324b88c807SRodney W. Grimes { 2332cac6e36SJilles Tjoelker const char *p; 2344b88c807SRodney W. Grimes int len; 2354b88c807SRodney W. Grimes int namelen; 2364b88c807SRodney W. Grimes char *nameeq; 2374b88c807SRodney W. Grimes int isbad; 2384b88c807SRodney W. Grimes 2394b88c807SRodney W. Grimes isbad = 0; 2404b88c807SRodney W. Grimes p = name; 241ba726b8aSAndrey A. Chernov if (! is_name(*p)) 2424b88c807SRodney W. Grimes isbad = 1; 243ba726b8aSAndrey A. Chernov p++; 2444b88c807SRodney W. Grimes for (;;) { 2454b88c807SRodney W. Grimes if (! is_in_name(*p)) { 2464b88c807SRodney W. Grimes if (*p == '\0' || *p == '=') 2474b88c807SRodney W. Grimes break; 2484b88c807SRodney W. Grimes isbad = 1; 2494b88c807SRodney W. Grimes } 2504b88c807SRodney W. Grimes p++; 2514b88c807SRodney W. Grimes } 2524b88c807SRodney W. Grimes namelen = p - name; 2534b88c807SRodney W. Grimes if (isbad) 2544b88c807SRodney W. Grimes error("%.*s: bad variable name", namelen, name); 2554b88c807SRodney W. Grimes len = namelen + 2; /* 2 is space for '=' and '\0' */ 2564b88c807SRodney W. Grimes if (val == NULL) { 2574b88c807SRodney W. Grimes flags |= VUNSET; 2584b88c807SRodney W. Grimes } else { 2594b88c807SRodney W. Grimes len += strlen(val); 2604b88c807SRodney W. Grimes } 2612cac6e36SJilles Tjoelker nameeq = ckmalloc(len); 2622cac6e36SJilles Tjoelker memcpy(nameeq, name, namelen); 2632cac6e36SJilles Tjoelker nameeq[namelen] = '='; 2644b88c807SRodney W. Grimes if (val) 2652cac6e36SJilles Tjoelker scopy(val, nameeq + namelen + 1); 2662cac6e36SJilles Tjoelker else 2672cac6e36SJilles Tjoelker nameeq[namelen + 1] = '\0'; 2684b88c807SRodney W. Grimes setvareq(nameeq, flags); 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 3015b78c6c2SSean Farley ss = savestr(s); 3025b78c6c2SSean Farley if ((eqp = strchr(ss, '=')) != NULL) 3035b78c6c2SSean Farley *eqp = '\0'; 3045b78c6c2SSean Farley if (set && eqp != NULL) 3055b78c6c2SSean Farley (void) setenv(ss, eqp + 1, 1); 3065b78c6c2SSean Farley else 3075b78c6c2SSean Farley (void) unsetenv(ss); 3085b78c6c2SSean Farley ckfree(ss); 3095b78c6c2SSean Farley 3105b78c6c2SSean Farley return; 3115b78c6c2SSean Farley } 3125b78c6c2SSean Farley 3135b78c6c2SSean Farley 3144b88c807SRodney W. Grimes /* 3154b88c807SRodney W. Grimes * Same as setvar except that the variable and value are passed in 3164b88c807SRodney W. Grimes * the first argument as name=value. Since the first argument will 3174b88c807SRodney W. Grimes * be actually stored in the table, it should not be a string that 3184b88c807SRodney W. Grimes * will go away. 3194b88c807SRodney W. Grimes */ 3204b88c807SRodney W. Grimes 3214b88c807SRodney W. Grimes void 3225134c3f7SWarner Losh setvareq(char *s, int flags) 3234b88c807SRodney W. Grimes { 3244b88c807SRodney W. Grimes struct var *vp, **vpp; 32590e41fc4SWarner Losh int len; 3264b88c807SRodney W. Grimes 327b8ec435eSMartin Cracauer if (aflag) 328b8ec435eSMartin Cracauer flags |= VEXPORT; 3294b88c807SRodney W. Grimes vpp = hashvar(s); 3304b88c807SRodney W. Grimes for (vp = *vpp ; vp ; vp = vp->next) { 3314b88c807SRodney W. Grimes if (varequal(s, vp->text)) { 3324b88c807SRodney W. Grimes if (vp->flags & VREADONLY) { 33390e41fc4SWarner Losh len = strchr(s, '=') - s; 3344b88c807SRodney W. Grimes error("%.*s: is read only", len, s); 3354b88c807SRodney W. Grimes } 3364b88c807SRodney W. Grimes INTOFF; 337ab0a2172SSteve Price 338ab0a2172SSteve Price if (vp->func && (flags & VNOFUNC) == 0) 339ab0a2172SSteve Price (*vp->func)(strchr(s, '=') + 1); 340ab0a2172SSteve Price 3414b88c807SRodney W. Grimes if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0) 3424b88c807SRodney W. Grimes ckfree(vp->text); 343ab0a2172SSteve Price 3444b88c807SRodney W. Grimes vp->flags &= ~(VTEXTFIXED|VSTACK|VUNSET); 3454b88c807SRodney W. Grimes vp->flags |= flags; 3464b88c807SRodney W. Grimes vp->text = s; 347ab0a2172SSteve Price 348ab0a2172SSteve Price /* 349ab0a2172SSteve Price * We could roll this to a function, to handle it as 350ab0a2172SSteve Price * a regular variable function callback, but why bother? 35157063576SJilles Tjoelker * 35257063576SJilles Tjoelker * Note: this assumes iflag is not set to 1 initially. 35357063576SJilles Tjoelker * As part of init(), this is called before arguments 35457063576SJilles Tjoelker * are looked at. 355ab0a2172SSteve Price */ 35657063576SJilles Tjoelker if ((vp == &vmpath || (vp == &vmail && ! mpathset())) && 35757063576SJilles Tjoelker iflag == 1) 3584b88c807SRodney W. Grimes chkmail(1); 359ba726b8aSAndrey A. Chernov if ((vp->flags & VEXPORT) && localevar(s)) { 3605b78c6c2SSean Farley change_env(s, 1); 361ba726b8aSAndrey A. Chernov (void) setlocale(LC_ALL, ""); 362ba726b8aSAndrey A. Chernov } 3634b88c807SRodney W. Grimes INTON; 3644b88c807SRodney W. Grimes return; 3654b88c807SRodney W. Grimes } 3664b88c807SRodney W. Grimes } 3674b88c807SRodney W. Grimes /* not found */ 3684b88c807SRodney W. Grimes vp = ckmalloc(sizeof (*vp)); 3694b88c807SRodney W. Grimes vp->flags = flags; 3704b88c807SRodney W. Grimes vp->text = s; 3714b88c807SRodney W. Grimes vp->next = *vpp; 372ab0a2172SSteve Price vp->func = NULL; 373ba726b8aSAndrey A. Chernov INTOFF; 3744b88c807SRodney W. Grimes *vpp = vp; 375ba726b8aSAndrey A. Chernov if ((vp->flags & VEXPORT) && localevar(s)) { 3765b78c6c2SSean Farley change_env(s, 1); 377ba726b8aSAndrey A. Chernov (void) setlocale(LC_ALL, ""); 378ba726b8aSAndrey A. Chernov } 379ba726b8aSAndrey A. Chernov INTON; 3804b88c807SRodney W. Grimes } 3814b88c807SRodney W. Grimes 3824b88c807SRodney W. Grimes 3834b88c807SRodney W. Grimes 3844b88c807SRodney W. Grimes /* 3854b88c807SRodney W. Grimes * Process a linked list of variable assignments. 3864b88c807SRodney W. Grimes */ 3874b88c807SRodney W. Grimes 3884b88c807SRodney W. Grimes void 3895134c3f7SWarner Losh listsetvar(struct strlist *list) 3904b88c807SRodney W. Grimes { 3914b88c807SRodney W. Grimes struct strlist *lp; 3924b88c807SRodney W. Grimes 3934b88c807SRodney W. Grimes INTOFF; 3944b88c807SRodney W. Grimes for (lp = list ; lp ; lp = lp->next) { 3954b88c807SRodney W. Grimes setvareq(savestr(lp->text), 0); 3964b88c807SRodney W. Grimes } 3974b88c807SRodney W. Grimes INTON; 3984b88c807SRodney W. Grimes } 3994b88c807SRodney W. Grimes 4004b88c807SRodney W. Grimes 4014b88c807SRodney W. Grimes 4024b88c807SRodney W. Grimes /* 4034b88c807SRodney W. Grimes * Find the value of a variable. Returns NULL if not set. 4044b88c807SRodney W. Grimes */ 4054b88c807SRodney W. Grimes 4064b88c807SRodney W. Grimes char * 4072cac6e36SJilles Tjoelker lookupvar(const char *name) 4084b88c807SRodney W. Grimes { 4094b88c807SRodney W. Grimes struct var *v; 4104b88c807SRodney W. Grimes 4114b88c807SRodney W. Grimes for (v = *hashvar(name) ; v ; v = v->next) { 4124b88c807SRodney W. Grimes if (varequal(v->text, name)) { 4134b88c807SRodney W. Grimes if (v->flags & VUNSET) 4144b88c807SRodney W. Grimes return NULL; 4154b88c807SRodney W. Grimes return strchr(v->text, '=') + 1; 4164b88c807SRodney W. Grimes } 4174b88c807SRodney W. Grimes } 4184b88c807SRodney W. Grimes return NULL; 4194b88c807SRodney W. Grimes } 4204b88c807SRodney W. Grimes 4214b88c807SRodney W. Grimes 4224b88c807SRodney W. Grimes 4234b88c807SRodney W. Grimes /* 4244b88c807SRodney W. Grimes * Search the environment of a builtin command. If the second argument 4254b88c807SRodney W. Grimes * is nonzero, return the value of a variable even if it hasn't been 4264b88c807SRodney W. Grimes * exported. 4274b88c807SRodney W. Grimes */ 4284b88c807SRodney W. Grimes 4294b88c807SRodney W. Grimes char * 4302cac6e36SJilles Tjoelker bltinlookup(const char *name, int doall) 4314b88c807SRodney W. Grimes { 4324b88c807SRodney W. Grimes struct strlist *sp; 4334b88c807SRodney W. Grimes struct var *v; 434011d162dSJilles Tjoelker char *result; 4354b88c807SRodney W. Grimes 436011d162dSJilles Tjoelker result = NULL; 4374b88c807SRodney W. Grimes for (sp = cmdenviron ; sp ; sp = sp->next) { 4384b88c807SRodney W. Grimes if (varequal(sp->text, name)) 439011d162dSJilles Tjoelker result = strchr(sp->text, '=') + 1; 4404b88c807SRodney W. Grimes } 441011d162dSJilles Tjoelker if (result != NULL) 442011d162dSJilles Tjoelker return result; 4434b88c807SRodney W. Grimes for (v = *hashvar(name) ; v ; v = v->next) { 4444b88c807SRodney W. Grimes if (varequal(v->text, name)) { 445aa9caaf6SPeter Wemm if ((v->flags & VUNSET) 446aa9caaf6SPeter Wemm || (!doall && (v->flags & VEXPORT) == 0)) 4474b88c807SRodney W. Grimes return NULL; 4484b88c807SRodney W. Grimes return strchr(v->text, '=') + 1; 4494b88c807SRodney W. Grimes } 4504b88c807SRodney W. Grimes } 4514b88c807SRodney W. Grimes return NULL; 4524b88c807SRodney W. Grimes } 4534b88c807SRodney W. Grimes 4544b88c807SRodney W. Grimes 455e4b50334SJilles Tjoelker /* 456e4b50334SJilles Tjoelker * Set up locale for a builtin (LANG/LC_* assignments). 457e4b50334SJilles Tjoelker */ 458e4b50334SJilles Tjoelker void 459e4b50334SJilles Tjoelker bltinsetlocale(void) 460e4b50334SJilles Tjoelker { 461e4b50334SJilles Tjoelker struct strlist *lp; 462e4b50334SJilles Tjoelker int act = 0; 463e4b50334SJilles Tjoelker char *loc, *locdef; 464e4b50334SJilles Tjoelker int i; 465e4b50334SJilles Tjoelker 466e4b50334SJilles Tjoelker for (lp = cmdenviron ; lp ; lp = lp->next) { 467e4b50334SJilles Tjoelker if (localevar(lp->text)) { 468e4b50334SJilles Tjoelker act = 1; 469e4b50334SJilles Tjoelker break; 470e4b50334SJilles Tjoelker } 471e4b50334SJilles Tjoelker } 472e4b50334SJilles Tjoelker if (!act) 473e4b50334SJilles Tjoelker return; 474e4b50334SJilles Tjoelker loc = bltinlookup("LC_ALL", 0); 475e4b50334SJilles Tjoelker INTOFF; 476e4b50334SJilles Tjoelker if (loc != NULL) { 477e4b50334SJilles Tjoelker setlocale(LC_ALL, loc); 478e4b50334SJilles Tjoelker INTON; 479e4b50334SJilles Tjoelker return; 480e4b50334SJilles Tjoelker } 481e4b50334SJilles Tjoelker locdef = bltinlookup("LANG", 0); 482e4b50334SJilles Tjoelker for (i = 0; locale_names[i] != NULL; i++) { 483e4b50334SJilles Tjoelker loc = bltinlookup(locale_names[i], 0); 484e4b50334SJilles Tjoelker if (loc == NULL) 485e4b50334SJilles Tjoelker loc = locdef; 486e4b50334SJilles Tjoelker if (loc != NULL) 487e4b50334SJilles Tjoelker setlocale(locale_categories[i], loc); 488e4b50334SJilles Tjoelker } 489e4b50334SJilles Tjoelker INTON; 490e4b50334SJilles Tjoelker } 491e4b50334SJilles Tjoelker 492e4b50334SJilles Tjoelker /* 493e4b50334SJilles Tjoelker * Undo the effect of bltinlocaleset(). 494e4b50334SJilles Tjoelker */ 495e4b50334SJilles Tjoelker void 496e4b50334SJilles Tjoelker bltinunsetlocale(void) 497e4b50334SJilles Tjoelker { 498e4b50334SJilles Tjoelker struct strlist *lp; 499e4b50334SJilles Tjoelker 500e4b50334SJilles Tjoelker INTOFF; 501e4b50334SJilles Tjoelker for (lp = cmdenviron ; lp ; lp = lp->next) { 502e4b50334SJilles Tjoelker if (localevar(lp->text)) { 503e4b50334SJilles Tjoelker setlocale(LC_ALL, ""); 504e4b50334SJilles Tjoelker return; 505e4b50334SJilles Tjoelker } 506e4b50334SJilles Tjoelker } 507e4b50334SJilles Tjoelker INTON; 508e4b50334SJilles Tjoelker } 509e4b50334SJilles Tjoelker 5104b88c807SRodney W. Grimes 5114b88c807SRodney W. Grimes /* 5124b88c807SRodney W. Grimes * Generate a list of exported variables. This routine is used to construct 5134b88c807SRodney W. Grimes * the third argument to execve when executing a program. 5144b88c807SRodney W. Grimes */ 5154b88c807SRodney W. Grimes 5164b88c807SRodney W. Grimes char ** 5175134c3f7SWarner Losh environment(void) 5185134c3f7SWarner Losh { 5194b88c807SRodney W. Grimes int nenv; 5204b88c807SRodney W. Grimes struct var **vpp; 5214b88c807SRodney W. Grimes struct var *vp; 5224b88c807SRodney W. Grimes char **env, **ep; 5234b88c807SRodney W. Grimes 5244b88c807SRodney W. Grimes nenv = 0; 5254b88c807SRodney W. Grimes for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) { 5264b88c807SRodney W. Grimes for (vp = *vpp ; vp ; vp = vp->next) 5274b88c807SRodney W. Grimes if (vp->flags & VEXPORT) 5284b88c807SRodney W. Grimes nenv++; 5294b88c807SRodney W. Grimes } 5304b88c807SRodney W. Grimes ep = env = stalloc((nenv + 1) * sizeof *env); 5314b88c807SRodney W. Grimes for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) { 5324b88c807SRodney W. Grimes for (vp = *vpp ; vp ; vp = vp->next) 5334b88c807SRodney W. Grimes if (vp->flags & VEXPORT) 5344b88c807SRodney W. Grimes *ep++ = vp->text; 5354b88c807SRodney W. Grimes } 5364b88c807SRodney W. Grimes *ep = NULL; 5374b88c807SRodney W. Grimes return env; 5384b88c807SRodney W. Grimes } 5394b88c807SRodney W. Grimes 5404b88c807SRodney W. Grimes 5414b88c807SRodney W. Grimes /* 5424b88c807SRodney W. Grimes * Called when a shell procedure is invoked to clear out nonexported 5434b88c807SRodney W. Grimes * variables. It is also necessary to reallocate variables of with 5444b88c807SRodney W. Grimes * VSTACK set since these are currently allocated on the stack. 5454b88c807SRodney W. Grimes */ 5464b88c807SRodney W. Grimes 547c7893739SStefan Farfeleder MKINIT void shprocvar(void); 5484b88c807SRodney W. Grimes 549384aedabSJilles Tjoelker #ifdef mkinit 5504b88c807SRodney W. Grimes SHELLPROC { 5514b88c807SRodney W. Grimes shprocvar(); 5524b88c807SRodney W. Grimes } 5534b88c807SRodney W. Grimes #endif 5544b88c807SRodney W. Grimes 5554b88c807SRodney W. Grimes void 5565134c3f7SWarner Losh shprocvar(void) 5575134c3f7SWarner Losh { 5584b88c807SRodney W. Grimes struct var **vpp; 5594b88c807SRodney W. Grimes struct var *vp, **prev; 5604b88c807SRodney W. Grimes 5614b88c807SRodney W. Grimes for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) { 5624b88c807SRodney W. Grimes for (prev = vpp ; (vp = *prev) != NULL ; ) { 5634b88c807SRodney W. Grimes if ((vp->flags & VEXPORT) == 0) { 5644b88c807SRodney W. Grimes *prev = vp->next; 5654b88c807SRodney W. Grimes if ((vp->flags & VTEXTFIXED) == 0) 5664b88c807SRodney W. Grimes ckfree(vp->text); 5674b88c807SRodney W. Grimes if ((vp->flags & VSTRFIXED) == 0) 5684b88c807SRodney W. Grimes ckfree(vp); 5694b88c807SRodney W. Grimes } else { 5704b88c807SRodney W. Grimes if (vp->flags & VSTACK) { 5714b88c807SRodney W. Grimes vp->text = savestr(vp->text); 5724b88c807SRodney W. Grimes vp->flags &=~ VSTACK; 5734b88c807SRodney W. Grimes } 5744b88c807SRodney W. Grimes prev = &vp->next; 5754b88c807SRodney W. Grimes } 5764b88c807SRodney W. Grimes } 5774b88c807SRodney W. Grimes } 5784b88c807SRodney W. Grimes initvar(); 5794b88c807SRodney W. Grimes } 5804b88c807SRodney W. Grimes 5814b88c807SRodney W. Grimes 58288328642SDavid E. O'Brien static int 583692f35fdSStefan Farfeleder var_compare(const void *a, const void *b) 584692f35fdSStefan Farfeleder { 585692f35fdSStefan Farfeleder const char *const *sa, *const *sb; 586692f35fdSStefan Farfeleder 587692f35fdSStefan Farfeleder sa = a; 588692f35fdSStefan Farfeleder sb = b; 589692f35fdSStefan Farfeleder /* 590692f35fdSStefan Farfeleder * This compares two var=value strings which creates a different 591692f35fdSStefan Farfeleder * order from what you would probably expect. POSIX is somewhat 592692f35fdSStefan Farfeleder * ambiguous on what should be sorted exactly. 593692f35fdSStefan Farfeleder */ 594692f35fdSStefan Farfeleder return strcoll(*sa, *sb); 595692f35fdSStefan Farfeleder } 596692f35fdSStefan Farfeleder 5974b88c807SRodney W. Grimes 5984b88c807SRodney W. Grimes /* 5994b88c807SRodney W. Grimes * Command to list all variables which are set. Currently this command 6004b88c807SRodney W. Grimes * is invoked from the set command when the set command is called without 6014b88c807SRodney W. Grimes * any variables. 6024b88c807SRodney W. Grimes */ 6034b88c807SRodney W. Grimes 6044b88c807SRodney W. Grimes int 6055134c3f7SWarner Losh showvarscmd(int argc __unused, char **argv __unused) 606aa9caaf6SPeter Wemm { 6074b88c807SRodney W. Grimes struct var **vpp; 6084b88c807SRodney W. Grimes struct var *vp; 60959258844STim J. Robbins const char *s; 610692f35fdSStefan Farfeleder const char **vars; 611692f35fdSStefan Farfeleder int i, n; 6124b88c807SRodney W. Grimes 613692f35fdSStefan Farfeleder /* 614692f35fdSStefan Farfeleder * POSIX requires us to sort the variables. 615692f35fdSStefan Farfeleder */ 616692f35fdSStefan Farfeleder n = 0; 6174b88c807SRodney W. Grimes for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) { 6184b88c807SRodney W. Grimes for (vp = *vpp; vp; vp = vp->next) { 619692f35fdSStefan Farfeleder if (!(vp->flags & VUNSET)) 620692f35fdSStefan Farfeleder n++; 621692f35fdSStefan Farfeleder } 622692f35fdSStefan Farfeleder } 623692f35fdSStefan Farfeleder 624692f35fdSStefan Farfeleder INTON; 625692f35fdSStefan Farfeleder vars = ckmalloc(n * sizeof(*vars)); 626692f35fdSStefan Farfeleder i = 0; 627692f35fdSStefan Farfeleder for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) { 628692f35fdSStefan Farfeleder for (vp = *vpp; vp; vp = vp->next) { 629692f35fdSStefan Farfeleder if (!(vp->flags & VUNSET)) 630692f35fdSStefan Farfeleder vars[i++] = vp->text; 631692f35fdSStefan Farfeleder } 632692f35fdSStefan Farfeleder } 633692f35fdSStefan Farfeleder 634692f35fdSStefan Farfeleder qsort(vars, n, sizeof(*vars), var_compare); 635692f35fdSStefan Farfeleder for (i = 0; i < n; i++) { 636*aeb5d065SJilles Tjoelker s = strchr(vars[i], '='); 637*aeb5d065SJilles Tjoelker s++; 638*aeb5d065SJilles Tjoelker outbin(vars[i], s - vars[i], out1); 639*aeb5d065SJilles Tjoelker out1qstr(s); 64059258844STim J. Robbins out1c('\n'); 6414b88c807SRodney W. Grimes } 642692f35fdSStefan Farfeleder ckfree(vars); 643692f35fdSStefan Farfeleder INTOFF; 644692f35fdSStefan Farfeleder 6454b88c807SRodney W. Grimes return 0; 6464b88c807SRodney W. Grimes } 6474b88c807SRodney W. Grimes 6484b88c807SRodney W. Grimes 6494b88c807SRodney W. Grimes 6504b88c807SRodney W. Grimes /* 6514b88c807SRodney W. Grimes * The export and readonly commands. 6524b88c807SRodney W. Grimes */ 6534b88c807SRodney W. Grimes 6544b88c807SRodney W. Grimes int 6555134c3f7SWarner Losh exportcmd(int argc, char **argv) 656aa9caaf6SPeter Wemm { 6574b88c807SRodney W. Grimes struct var **vpp; 6584b88c807SRodney W. Grimes struct var *vp; 6594b88c807SRodney W. Grimes char *name; 6604b88c807SRodney W. Grimes char *p; 66145086f8cSTim J. Robbins char *cmdname; 66245086f8cSTim J. Robbins int ch, values; 6634b88c807SRodney W. Grimes int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT; 6644b88c807SRodney W. Grimes 66545086f8cSTim J. Robbins cmdname = argv[0]; 66645086f8cSTim J. Robbins optreset = optind = 1; 667050f7913STim J. Robbins opterr = 0; 66845086f8cSTim J. Robbins values = 0; 66945086f8cSTim J. Robbins while ((ch = getopt(argc, argv, "p")) != -1) { 67045086f8cSTim J. Robbins switch (ch) { 67145086f8cSTim J. Robbins case 'p': 67245086f8cSTim J. Robbins values = 1; 67345086f8cSTim J. Robbins break; 67445086f8cSTim J. Robbins case '?': 67545086f8cSTim J. Robbins default: 67645086f8cSTim J. Robbins error("unknown option: -%c", optopt); 67745086f8cSTim J. Robbins } 67845086f8cSTim J. Robbins } 67945086f8cSTim J. Robbins argc -= optind; 68045086f8cSTim J. Robbins argv += optind; 68145086f8cSTim J. Robbins 6823fda45ecSStefan Farfeleder if (values && argc != 0) 6833fda45ecSStefan Farfeleder error("-p requires no arguments"); 68445086f8cSTim J. Robbins if (argc != 0) { 6853fda45ecSStefan Farfeleder while ((name = *argv++) != NULL) { 6864b88c807SRodney W. Grimes if ((p = strchr(name, '=')) != NULL) { 6874b88c807SRodney W. Grimes p++; 6884b88c807SRodney W. Grimes } else { 6894b88c807SRodney W. Grimes vpp = hashvar(name); 6904b88c807SRodney W. Grimes for (vp = *vpp ; vp ; vp = vp->next) { 6914b88c807SRodney W. Grimes if (varequal(vp->text, name)) { 6925134c3f7SWarner Losh 6934b88c807SRodney W. Grimes vp->flags |= flag; 694ba726b8aSAndrey A. Chernov if ((vp->flags & VEXPORT) && localevar(vp->text)) { 6955b78c6c2SSean Farley change_env(vp->text, 1); 696ba726b8aSAndrey A. Chernov (void) setlocale(LC_ALL, ""); 697ba726b8aSAndrey A. Chernov } 6984b88c807SRodney W. Grimes goto found; 6994b88c807SRodney W. Grimes } 7004b88c807SRodney W. Grimes } 7014b88c807SRodney W. Grimes } 7024b88c807SRodney W. Grimes setvar(name, p, flag); 7034b88c807SRodney W. Grimes found:; 7044b88c807SRodney W. Grimes } 7054b88c807SRodney W. Grimes } else { 7064b88c807SRodney W. Grimes for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) { 7074b88c807SRodney W. Grimes for (vp = *vpp ; vp ; vp = vp->next) { 7084b88c807SRodney W. Grimes if (vp->flags & flag) { 70945086f8cSTim J. Robbins if (values) { 71045086f8cSTim J. Robbins out1str(cmdname); 71145086f8cSTim J. Robbins out1c(' '); 71245086f8cSTim J. Robbins } 713*aeb5d065SJilles Tjoelker p = strchr(vp->text, '='); 71445086f8cSTim J. Robbins if (values && !(vp->flags & VUNSET)) { 715*aeb5d065SJilles Tjoelker p++; 716*aeb5d065SJilles Tjoelker outbin(vp->text, p - vp->text, 717*aeb5d065SJilles Tjoelker out1); 718*aeb5d065SJilles Tjoelker out1qstr(p); 719*aeb5d065SJilles Tjoelker } else 720*aeb5d065SJilles Tjoelker outbin(vp->text, p - vp->text, 721*aeb5d065SJilles Tjoelker out1); 7224b88c807SRodney W. Grimes out1c('\n'); 7234b88c807SRodney W. Grimes } 7244b88c807SRodney W. Grimes } 7254b88c807SRodney W. Grimes } 7264b88c807SRodney W. Grimes } 7274b88c807SRodney W. Grimes return 0; 7284b88c807SRodney W. Grimes } 7294b88c807SRodney W. Grimes 7304b88c807SRodney W. Grimes 7314b88c807SRodney W. Grimes /* 7324b88c807SRodney W. Grimes * The "local" command. 7334b88c807SRodney W. Grimes */ 7344b88c807SRodney W. Grimes 735aa9caaf6SPeter Wemm int 7365134c3f7SWarner Losh localcmd(int argc __unused, char **argv __unused) 737aa9caaf6SPeter Wemm { 7384b88c807SRodney W. Grimes char *name; 7394b88c807SRodney W. Grimes 7404b88c807SRodney W. Grimes if (! in_function()) 7414b88c807SRodney W. Grimes error("Not in a function"); 7424b88c807SRodney W. Grimes while ((name = *argptr++) != NULL) { 7434b88c807SRodney W. Grimes mklocal(name); 7444b88c807SRodney W. Grimes } 7454b88c807SRodney W. Grimes return 0; 7464b88c807SRodney W. Grimes } 7474b88c807SRodney W. Grimes 7484b88c807SRodney W. Grimes 7494b88c807SRodney W. Grimes /* 7504b88c807SRodney W. Grimes * Make a variable a local variable. When a variable is made local, it's 7514b88c807SRodney W. Grimes * value and flags are saved in a localvar structure. The saved values 7524b88c807SRodney W. Grimes * will be restored when the shell function returns. We handle the name 7534b88c807SRodney W. Grimes * "-" as a special case. 7544b88c807SRodney W. Grimes */ 7554b88c807SRodney W. Grimes 7564b88c807SRodney W. Grimes void 7575134c3f7SWarner Losh mklocal(char *name) 7584b88c807SRodney W. Grimes { 7594b88c807SRodney W. Grimes struct localvar *lvp; 7604b88c807SRodney W. Grimes struct var **vpp; 7614b88c807SRodney W. Grimes struct var *vp; 7624b88c807SRodney W. Grimes 7634b88c807SRodney W. Grimes INTOFF; 7644b88c807SRodney W. Grimes lvp = ckmalloc(sizeof (struct localvar)); 7654b88c807SRodney W. Grimes if (name[0] == '-' && name[1] == '\0') { 7664b88c807SRodney W. Grimes lvp->text = ckmalloc(sizeof optlist); 767aa9caaf6SPeter Wemm memcpy(lvp->text, optlist, sizeof optlist); 7684b88c807SRodney W. Grimes vp = NULL; 7694b88c807SRodney W. Grimes } else { 7704b88c807SRodney W. Grimes vpp = hashvar(name); 7714b88c807SRodney W. Grimes for (vp = *vpp ; vp && ! varequal(vp->text, name) ; vp = vp->next); 7724b88c807SRodney W. Grimes if (vp == NULL) { 7734b88c807SRodney W. Grimes if (strchr(name, '=')) 7744b88c807SRodney W. Grimes setvareq(savestr(name), VSTRFIXED); 7754b88c807SRodney W. Grimes else 7764b88c807SRodney W. Grimes setvar(name, NULL, VSTRFIXED); 7774b88c807SRodney W. Grimes vp = *vpp; /* the new variable */ 7784b88c807SRodney W. Grimes lvp->text = NULL; 7794b88c807SRodney W. Grimes lvp->flags = VUNSET; 7804b88c807SRodney W. Grimes } else { 7814b88c807SRodney W. Grimes lvp->text = vp->text; 7824b88c807SRodney W. Grimes lvp->flags = vp->flags; 7834b88c807SRodney W. Grimes vp->flags |= VSTRFIXED|VTEXTFIXED; 7844b88c807SRodney W. Grimes if (strchr(name, '=')) 7854b88c807SRodney W. Grimes setvareq(savestr(name), 0); 7864b88c807SRodney W. Grimes } 7874b88c807SRodney W. Grimes } 7884b88c807SRodney W. Grimes lvp->vp = vp; 7894b88c807SRodney W. Grimes lvp->next = localvars; 7904b88c807SRodney W. Grimes localvars = lvp; 7914b88c807SRodney W. Grimes INTON; 7924b88c807SRodney W. Grimes } 7934b88c807SRodney W. Grimes 7944b88c807SRodney W. Grimes 7954b88c807SRodney W. Grimes /* 7964b88c807SRodney W. Grimes * Called after a function returns. 7974b88c807SRodney W. Grimes */ 7984b88c807SRodney W. Grimes 7994b88c807SRodney W. Grimes void 8005134c3f7SWarner Losh poplocalvars(void) 8015134c3f7SWarner Losh { 8024b88c807SRodney W. Grimes struct localvar *lvp; 8034b88c807SRodney W. Grimes struct var *vp; 8044b88c807SRodney W. Grimes 8054b88c807SRodney W. Grimes while ((lvp = localvars) != NULL) { 8064b88c807SRodney W. Grimes localvars = lvp->next; 8074b88c807SRodney W. Grimes vp = lvp->vp; 8084b88c807SRodney W. Grimes if (vp == NULL) { /* $- saved */ 809aa9caaf6SPeter Wemm memcpy(optlist, lvp->text, sizeof optlist); 8104b88c807SRodney W. Grimes ckfree(lvp->text); 8114a7b1013SJilles Tjoelker optschanged(); 8124b88c807SRodney W. Grimes } else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) { 8134b88c807SRodney W. Grimes (void)unsetvar(vp->text); 8144b88c807SRodney W. Grimes } else { 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; 8194b88c807SRodney W. Grimes } 8204b88c807SRodney W. Grimes ckfree(lvp); 8214b88c807SRodney W. Grimes } 8224b88c807SRodney W. Grimes } 8234b88c807SRodney W. Grimes 8244b88c807SRodney W. Grimes 825aa9caaf6SPeter Wemm int 8265134c3f7SWarner Losh setvarcmd(int argc, char **argv) 827aa9caaf6SPeter Wemm { 8284b88c807SRodney W. Grimes if (argc <= 2) 8294b88c807SRodney W. Grimes return unsetcmd(argc, argv); 8304b88c807SRodney W. Grimes else if (argc == 3) 8314b88c807SRodney W. Grimes setvar(argv[1], argv[2], 0); 8324b88c807SRodney W. Grimes else 833274110dfSJilles Tjoelker error("too many arguments"); 8344b88c807SRodney W. Grimes return 0; 8354b88c807SRodney W. Grimes } 8364b88c807SRodney W. Grimes 8374b88c807SRodney W. Grimes 8384b88c807SRodney W. Grimes /* 8394b88c807SRodney W. Grimes * The unset builtin command. We unset the function before we unset the 8404b88c807SRodney W. Grimes * variable to allow a function to be unset when there is a readonly variable 8414b88c807SRodney W. Grimes * with the same name. 8424b88c807SRodney W. Grimes */ 8434b88c807SRodney W. Grimes 844aa9caaf6SPeter Wemm int 8455134c3f7SWarner Losh unsetcmd(int argc __unused, char **argv __unused) 846aa9caaf6SPeter Wemm { 8474b88c807SRodney W. Grimes char **ap; 8484b88c807SRodney W. Grimes int i; 8494b88c807SRodney W. Grimes int flg_func = 0; 8504b88c807SRodney W. Grimes int flg_var = 0; 8514b88c807SRodney W. Grimes int ret = 0; 8524b88c807SRodney W. Grimes 8534b88c807SRodney W. Grimes while ((i = nextopt("vf")) != '\0') { 8544b88c807SRodney W. Grimes if (i == 'f') 8554b88c807SRodney W. Grimes flg_func = 1; 8564b88c807SRodney W. Grimes else 8574b88c807SRodney W. Grimes flg_var = 1; 8584b88c807SRodney W. Grimes } 8594b88c807SRodney W. Grimes if (flg_func == 0 && flg_var == 0) 8604b88c807SRodney W. Grimes flg_var = 1; 8614b88c807SRodney W. Grimes 8624b88c807SRodney W. Grimes for (ap = argptr; *ap ; ap++) { 8634b88c807SRodney W. Grimes if (flg_func) 8644b88c807SRodney W. Grimes ret |= unsetfunc(*ap); 8654b88c807SRodney W. Grimes if (flg_var) 8664b88c807SRodney W. Grimes ret |= unsetvar(*ap); 8674b88c807SRodney W. Grimes } 8684b88c807SRodney W. Grimes return ret; 8694b88c807SRodney W. Grimes } 8704b88c807SRodney W. Grimes 8714b88c807SRodney W. Grimes 8724b88c807SRodney W. Grimes /* 8734b88c807SRodney W. Grimes * Unset the specified variable. 8744b88c807SRodney W. Grimes */ 8754b88c807SRodney W. Grimes 876ab0a2172SSteve Price int 8772cac6e36SJilles Tjoelker unsetvar(const char *s) 8784b88c807SRodney W. Grimes { 8794b88c807SRodney W. Grimes struct var **vpp; 8804b88c807SRodney W. Grimes struct var *vp; 8814b88c807SRodney W. Grimes 8824b88c807SRodney W. Grimes vpp = hashvar(s); 8834b88c807SRodney W. Grimes for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) { 8844b88c807SRodney W. Grimes if (varequal(vp->text, s)) { 8854b88c807SRodney W. Grimes if (vp->flags & VREADONLY) 8864b88c807SRodney W. Grimes return (1); 8874b88c807SRodney W. Grimes INTOFF; 8884b88c807SRodney W. Grimes if (*(strchr(vp->text, '=') + 1) != '\0') 8894b88c807SRodney W. Grimes setvar(s, nullstr, 0); 890ba726b8aSAndrey A. Chernov if ((vp->flags & VEXPORT) && localevar(vp->text)) { 8915b78c6c2SSean Farley change_env(s, 0); 892ba726b8aSAndrey A. Chernov setlocale(LC_ALL, ""); 893ba726b8aSAndrey A. Chernov } 8944b88c807SRodney W. Grimes vp->flags &= ~VEXPORT; 8954b88c807SRodney W. Grimes vp->flags |= VUNSET; 8964b88c807SRodney W. Grimes if ((vp->flags & VSTRFIXED) == 0) { 8974b88c807SRodney W. Grimes if ((vp->flags & VTEXTFIXED) == 0) 8984b88c807SRodney W. Grimes ckfree(vp->text); 8994b88c807SRodney W. Grimes *vpp = vp->next; 9004b88c807SRodney W. Grimes ckfree(vp); 9014b88c807SRodney W. Grimes } 9024b88c807SRodney W. Grimes INTON; 9034b88c807SRodney W. Grimes return (0); 9044b88c807SRodney W. Grimes } 9054b88c807SRodney W. Grimes } 9064b88c807SRodney W. Grimes 907bd766773SDag-Erling Smørgrav return (0); 9084b88c807SRodney W. Grimes } 9094b88c807SRodney W. Grimes 9104b88c807SRodney W. Grimes 9114b88c807SRodney W. Grimes 9124b88c807SRodney W. Grimes /* 9134b88c807SRodney W. Grimes * Find the appropriate entry in the hash table from the name. 9144b88c807SRodney W. Grimes */ 9154b88c807SRodney W. Grimes 91688328642SDavid E. O'Brien static struct var ** 9172cac6e36SJilles Tjoelker hashvar(const char *p) 9184b88c807SRodney W. Grimes { 9194b88c807SRodney W. Grimes unsigned int hashval; 9204b88c807SRodney W. Grimes 921ba726b8aSAndrey A. Chernov hashval = ((unsigned char) *p) << 4; 9224b88c807SRodney W. Grimes while (*p && *p != '=') 923ba726b8aSAndrey A. Chernov hashval += (unsigned char) *p++; 9244b88c807SRodney W. Grimes return &vartab[hashval % VTABSIZE]; 9254b88c807SRodney W. Grimes } 9264b88c807SRodney W. Grimes 9274b88c807SRodney W. Grimes 9284b88c807SRodney W. Grimes 9294b88c807SRodney W. Grimes /* 9304b88c807SRodney W. Grimes * Returns true if the two strings specify the same varable. The first 9314b88c807SRodney W. Grimes * variable name is terminated by '='; the second may be terminated by 9324b88c807SRodney W. Grimes * either '=' or '\0'. 9334b88c807SRodney W. Grimes */ 9344b88c807SRodney W. Grimes 93588328642SDavid E. O'Brien static int 9362cac6e36SJilles Tjoelker varequal(const char *p, const char *q) 9374b88c807SRodney W. Grimes { 9384b88c807SRodney W. Grimes while (*p == *q++) { 9394b88c807SRodney W. Grimes if (*p++ == '=') 9404b88c807SRodney W. Grimes return 1; 9414b88c807SRodney W. Grimes } 9424b88c807SRodney W. Grimes if (*p == '=' && *(q - 1) == '\0') 9434b88c807SRodney W. Grimes return 1; 9444b88c807SRodney W. Grimes return 0; 9454b88c807SRodney W. Grimes } 946