14b88c807SRodney W. Grimes /*- 24b88c807SRodney W. Grimes * Copyright (c) 1991, 1993 34b88c807SRodney W. Grimes * The Regents of the University of California. All rights reserved. 44b88c807SRodney W. Grimes * 54b88c807SRodney W. Grimes * This code is derived from software contributed to Berkeley by 64b88c807SRodney W. Grimes * Kenneth Almquist. 74b88c807SRodney W. Grimes * 84b88c807SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 94b88c807SRodney W. Grimes * modification, are permitted provided that the following conditions 104b88c807SRodney W. Grimes * are met: 114b88c807SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 124b88c807SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 134b88c807SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 144b88c807SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 154b88c807SRodney W. Grimes * documentation and/or other materials provided with the distribution. 164b88c807SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 174b88c807SRodney W. Grimes * may be used to endorse or promote products derived from this software 184b88c807SRodney W. Grimes * without specific prior written permission. 194b88c807SRodney W. Grimes * 204b88c807SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 214b88c807SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 224b88c807SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 234b88c807SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 244b88c807SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 254b88c807SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 264b88c807SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 274b88c807SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 284b88c807SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 294b88c807SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 304b88c807SRodney W. Grimes * SUCH DAMAGE. 314b88c807SRodney W. Grimes */ 324b88c807SRodney W. Grimes 334b88c807SRodney W. Grimes #ifndef lint 343d7b5b93SPhilippe Charnier #if 0 353d7b5b93SPhilippe Charnier static char sccsid[] = "@(#)var.c 8.3 (Berkeley) 5/4/95"; 363d7b5b93SPhilippe Charnier #endif 374b88c807SRodney W. Grimes #endif /* not lint */ 382749b141SDavid E. O'Brien #include <sys/cdefs.h> 392749b141SDavid E. O'Brien __FBSDID("$FreeBSD$"); 404b88c807SRodney W. Grimes 41aa9caaf6SPeter Wemm #include <unistd.h> 42aa9caaf6SPeter Wemm #include <stdlib.h> 438d5c19ffSDavid E. O'Brien #include <paths.h> 44aa9caaf6SPeter Wemm 454b88c807SRodney W. Grimes /* 464b88c807SRodney W. Grimes * Shell variables. 474b88c807SRodney W. Grimes */ 484b88c807SRodney W. Grimes 49ba726b8aSAndrey A. Chernov #include <locale.h> 506ed74a0aSJilles Tjoelker #include <langinfo.h> 51ba726b8aSAndrey A. Chernov 524b88c807SRodney W. Grimes #include "shell.h" 534b88c807SRodney W. Grimes #include "output.h" 544b88c807SRodney W. Grimes #include "expand.h" 554b88c807SRodney W. Grimes #include "nodes.h" /* for other headers */ 564b88c807SRodney W. Grimes #include "eval.h" /* defines cmdenviron */ 574b88c807SRodney W. Grimes #include "exec.h" 584b88c807SRodney W. Grimes #include "syntax.h" 594b88c807SRodney W. Grimes #include "options.h" 604b88c807SRodney W. Grimes #include "mail.h" 614b88c807SRodney W. Grimes #include "var.h" 624b88c807SRodney W. Grimes #include "memalloc.h" 634b88c807SRodney W. Grimes #include "error.h" 644b88c807SRodney W. Grimes #include "mystring.h" 65ab0a2172SSteve Price #include "parser.h" 66aa9caaf6SPeter Wemm #ifndef NO_HISTORY 67aa9caaf6SPeter Wemm #include "myhistedit.h" 68aa9caaf6SPeter Wemm #endif 694b88c807SRodney W. Grimes 704b88c807SRodney W. Grimes 714b88c807SRodney W. Grimes #define VTABSIZE 39 724b88c807SRodney W. Grimes 734b88c807SRodney W. Grimes 744b88c807SRodney W. Grimes struct varinit { 754b88c807SRodney W. Grimes struct var *var; 764b88c807SRodney W. Grimes int flags; 77c6c5dd37SJilles Tjoelker const char *text; 785134c3f7SWarner Losh void (*func)(const char *); 794b88c807SRodney W. Grimes }; 804b88c807SRodney W. Grimes 814b88c807SRodney W. Grimes 82aa9caaf6SPeter Wemm #ifndef NO_HISTORY 834b88c807SRodney W. Grimes struct var vhistsize; 84580eefdfSJilles Tjoelker struct var vterm; 85aa9caaf6SPeter Wemm #endif 864b88c807SRodney W. Grimes struct var vifs; 874b88c807SRodney W. Grimes struct var vmail; 884b88c807SRodney W. Grimes struct var vmpath; 894b88c807SRodney W. Grimes struct var vpath; 9039dccc6fSTim J. Robbins struct var vppid; 914b88c807SRodney W. Grimes struct var vps1; 924b88c807SRodney W. Grimes struct var vps2; 93120c8e6cSStefan Farfeleder struct var vps4; 944b88c807SRodney W. Grimes struct var vvers; 95aa7b6f82SDavid E. O'Brien static struct var voptind; 964b88c807SRodney W. Grimes 97*c543e1aeSJilles Tjoelker int forcelocal; 98*c543e1aeSJilles Tjoelker 99aa7b6f82SDavid E. O'Brien static const struct varinit varinit[] = { 100aa9caaf6SPeter Wemm #ifndef NO_HISTORY 101c6c5dd37SJilles Tjoelker { &vhistsize, VUNSET, "HISTSIZE=", 102ab0a2172SSteve Price sethistsize }, 103aa9caaf6SPeter Wemm #endif 104c6c5dd37SJilles Tjoelker { &vifs, 0, "IFS= \t\n", 105ab0a2172SSteve Price NULL }, 106c6c5dd37SJilles Tjoelker { &vmail, VUNSET, "MAIL=", 107ab0a2172SSteve Price NULL }, 108c6c5dd37SJilles Tjoelker { &vmpath, VUNSET, "MAILPATH=", 109ab0a2172SSteve Price NULL }, 110c6c5dd37SJilles Tjoelker { &vpath, 0, "PATH=" _PATH_DEFPATH, 111ab0a2172SSteve Price changepath }, 112c6c5dd37SJilles Tjoelker { &vppid, VUNSET, "PPID=", 11339dccc6fSTim J. Robbins NULL }, 1144b88c807SRodney W. Grimes /* 1154b88c807SRodney W. Grimes * vps1 depends on uid 1164b88c807SRodney W. Grimes */ 117c6c5dd37SJilles Tjoelker { &vps2, 0, "PS2=> ", 118ab0a2172SSteve Price NULL }, 119c6c5dd37SJilles Tjoelker { &vps4, 0, "PS4=+ ", 120120c8e6cSStefan Farfeleder NULL }, 121580eefdfSJilles Tjoelker #ifndef NO_HISTORY 122580eefdfSJilles Tjoelker { &vterm, VUNSET, "TERM=", 123580eefdfSJilles Tjoelker setterm }, 124580eefdfSJilles Tjoelker #endif 125c6c5dd37SJilles Tjoelker { &voptind, 0, "OPTIND=1", 126ab0a2172SSteve Price getoptsreset }, 127ab0a2172SSteve Price { NULL, 0, NULL, 128ab0a2172SSteve Price NULL } 1294b88c807SRodney W. Grimes }; 1304b88c807SRodney W. Grimes 131aa7b6f82SDavid E. O'Brien static struct var *vartab[VTABSIZE]; 1324b88c807SRodney W. Grimes 133aa7b6f82SDavid E. O'Brien static const char *const locale_names[7] = { 134e4b50334SJilles Tjoelker "LC_COLLATE", "LC_CTYPE", "LC_MONETARY", 135e4b50334SJilles Tjoelker "LC_NUMERIC", "LC_TIME", "LC_MESSAGES", NULL 136e4b50334SJilles Tjoelker }; 137aa7b6f82SDavid E. O'Brien static const int locale_categories[7] = { 138e4b50334SJilles Tjoelker LC_COLLATE, LC_CTYPE, LC_MONETARY, LC_NUMERIC, LC_TIME, LC_MESSAGES, 0 139e4b50334SJilles Tjoelker }; 140e4b50334SJilles Tjoelker 14188328642SDavid E. O'Brien static int varequal(const char *, const char *); 1423a99ed46SJilles Tjoelker static struct var *find_var(const char *, struct var ***, int *); 14388328642SDavid E. O'Brien static int localevar(const char *); 1444b88c807SRodney W. Grimes 1454b88c807SRodney W. Grimes /* 146b3decf89SJens Schweikhardt * Initialize the variable symbol tables and import the environment. 1474b88c807SRodney W. Grimes */ 1484b88c807SRodney W. Grimes 1494b88c807SRodney W. Grimes #ifdef mkinit 1504b88c807SRodney W. Grimes INCLUDE "var.h" 151384aedabSJilles Tjoelker MKINIT char **environ; 1524b88c807SRodney W. Grimes INIT { 1534b88c807SRodney W. Grimes char **envp; 1544b88c807SRodney W. Grimes 1554b88c807SRodney W. Grimes initvar(); 1564b88c807SRodney W. Grimes for (envp = environ ; *envp ; envp++) { 1574b88c807SRodney W. Grimes if (strchr(*envp, '=')) { 1584b88c807SRodney W. Grimes setvareq(*envp, VEXPORT|VTEXTFIXED); 1594b88c807SRodney W. Grimes } 1604b88c807SRodney W. Grimes } 1614b88c807SRodney W. Grimes } 1624b88c807SRodney W. Grimes #endif 1634b88c807SRodney W. Grimes 1644b88c807SRodney W. Grimes 1654b88c807SRodney W. Grimes /* 1664b88c807SRodney W. Grimes * This routine initializes the builtin variables. It is called when the 1673835f47cSJilles Tjoelker * shell is initialized. 1684b88c807SRodney W. Grimes */ 1694b88c807SRodney W. Grimes 1704b88c807SRodney W. Grimes void 1715134c3f7SWarner Losh initvar(void) 1725134c3f7SWarner Losh { 17339dccc6fSTim J. Robbins char ppid[20]; 1744b88c807SRodney W. Grimes const struct varinit *ip; 1754b88c807SRodney W. Grimes struct var *vp; 1764b88c807SRodney W. Grimes struct var **vpp; 1774b88c807SRodney W. Grimes 1784b88c807SRodney W. Grimes for (ip = varinit ; (vp = ip->var) != NULL ; ip++) { 1793a99ed46SJilles Tjoelker if (find_var(ip->text, &vpp, &vp->name_len) != NULL) 1803a99ed46SJilles Tjoelker continue; 1814b88c807SRodney W. Grimes vp->next = *vpp; 1824b88c807SRodney W. Grimes *vpp = vp; 183c6c5dd37SJilles Tjoelker vp->text = __DECONST(char *, ip->text); 184c6c5dd37SJilles Tjoelker vp->flags = ip->flags | VSTRFIXED | VTEXTFIXED; 185ab0a2172SSteve Price vp->func = ip->func; 1864b88c807SRodney W. Grimes } 1874b88c807SRodney W. Grimes /* 1884b88c807SRodney W. Grimes * PS1 depends on uid 1894b88c807SRodney W. Grimes */ 1903a99ed46SJilles Tjoelker if (find_var("PS1", &vpp, &vps1.name_len) == NULL) { 1914b88c807SRodney W. Grimes vps1.next = *vpp; 1924b88c807SRodney W. Grimes *vpp = &vps1; 193c6c5dd37SJilles Tjoelker vps1.text = __DECONST(char *, geteuid() ? "PS1=$ " : "PS1=# "); 1944b88c807SRodney W. Grimes vps1.flags = VSTRFIXED|VTEXTFIXED; 1954b88c807SRodney W. Grimes } 19639dccc6fSTim J. Robbins if ((vppid.flags & VEXPORT) == 0) { 19739dccc6fSTim J. Robbins fmtstr(ppid, sizeof(ppid), "%d", (int)getppid()); 19839dccc6fSTim J. Robbins setvarsafe("PPID", ppid, 0); 19939dccc6fSTim J. Robbins } 2004b88c807SRodney W. Grimes } 2014b88c807SRodney W. Grimes 2024b88c807SRodney W. Grimes /* 203ab0a2172SSteve Price * Safe version of setvar, returns 1 on success 0 on failure. 204ab0a2172SSteve Price */ 205ab0a2172SSteve Price 206ab0a2172SSteve Price int 2072cac6e36SJilles Tjoelker setvarsafe(const char *name, const char *val, int flags) 208ab0a2172SSteve Price { 209ab0a2172SSteve Price struct jmploc jmploc; 210224fbf9fSJilles Tjoelker struct jmploc *const savehandler = handler; 211ab0a2172SSteve Price int err = 0; 2129922c6d2SJilles Tjoelker int inton; 213ab0a2172SSteve Price 2149922c6d2SJilles Tjoelker inton = is_int_on(); 215ab0a2172SSteve Price if (setjmp(jmploc.loc)) 216ab0a2172SSteve Price err = 1; 217ab0a2172SSteve Price else { 218ab0a2172SSteve Price handler = &jmploc; 219ab0a2172SSteve Price setvar(name, val, flags); 220ab0a2172SSteve Price } 221ab0a2172SSteve Price handler = savehandler; 2229922c6d2SJilles Tjoelker SETINTON(inton); 223ab0a2172SSteve Price return err; 224ab0a2172SSteve Price } 225ab0a2172SSteve Price 226ab0a2172SSteve Price /* 227b3decf89SJens Schweikhardt * Set the value of a variable. The flags argument is stored with the 2284b88c807SRodney W. Grimes * flags of the variable. If val is NULL, the variable is unset. 2294b88c807SRodney W. Grimes */ 2304b88c807SRodney W. Grimes 2314b88c807SRodney W. Grimes void 2322cac6e36SJilles Tjoelker setvar(const char *name, const char *val, int flags) 2334b88c807SRodney W. Grimes { 2342cac6e36SJilles Tjoelker const char *p; 2354b88c807SRodney W. Grimes int len; 2364b88c807SRodney W. Grimes int namelen; 2374b88c807SRodney W. Grimes char *nameeq; 2384b88c807SRodney W. Grimes int isbad; 2394b88c807SRodney W. Grimes 2404b88c807SRodney W. Grimes isbad = 0; 2414b88c807SRodney W. Grimes p = name; 242ba726b8aSAndrey A. Chernov if (! is_name(*p)) 2434b88c807SRodney W. Grimes isbad = 1; 244ba726b8aSAndrey A. Chernov p++; 2454b88c807SRodney W. Grimes for (;;) { 2464b88c807SRodney W. Grimes if (! is_in_name(*p)) { 2474b88c807SRodney W. Grimes if (*p == '\0' || *p == '=') 2484b88c807SRodney W. Grimes break; 2494b88c807SRodney W. Grimes isbad = 1; 2504b88c807SRodney W. Grimes } 2514b88c807SRodney W. Grimes p++; 2524b88c807SRodney W. Grimes } 2534b88c807SRodney W. Grimes namelen = p - name; 2544b88c807SRodney W. Grimes if (isbad) 2554b88c807SRodney W. Grimes error("%.*s: bad variable name", namelen, name); 2564b88c807SRodney W. Grimes len = namelen + 2; /* 2 is space for '=' and '\0' */ 2574b88c807SRodney W. Grimes if (val == NULL) { 2584b88c807SRodney W. Grimes flags |= VUNSET; 2594b88c807SRodney W. Grimes } else { 2604b88c807SRodney W. Grimes len += strlen(val); 2614b88c807SRodney W. Grimes } 2622cac6e36SJilles Tjoelker nameeq = ckmalloc(len); 2632cac6e36SJilles Tjoelker memcpy(nameeq, name, namelen); 2642cac6e36SJilles Tjoelker nameeq[namelen] = '='; 2654b88c807SRodney W. Grimes if (val) 2662cac6e36SJilles Tjoelker scopy(val, nameeq + namelen + 1); 2672cac6e36SJilles Tjoelker else 2682cac6e36SJilles Tjoelker nameeq[namelen + 1] = '\0'; 2694b88c807SRodney W. Grimes setvareq(nameeq, flags); 2704b88c807SRodney W. Grimes } 2714b88c807SRodney W. Grimes 27288328642SDavid E. O'Brien static int 2732cac6e36SJilles Tjoelker localevar(const char *s) 274ba726b8aSAndrey A. Chernov { 275e4b50334SJilles Tjoelker const char *const *ss; 2764b88c807SRodney W. Grimes 277ba726b8aSAndrey A. Chernov if (*s != 'L') 278ba726b8aSAndrey A. Chernov return 0; 279ba726b8aSAndrey A. Chernov if (varequal(s + 1, "ANG")) 280ba726b8aSAndrey A. Chernov return 1; 281ba726b8aSAndrey A. Chernov if (strncmp(s + 1, "C_", 2) != 0) 282ba726b8aSAndrey A. Chernov return 0; 283e4b50334SJilles Tjoelker if (varequal(s + 3, "ALL")) 284e4b50334SJilles Tjoelker return 1; 285e4b50334SJilles Tjoelker for (ss = locale_names; *ss ; ss++) 286e4b50334SJilles Tjoelker if (varequal(s + 3, *ss + 3)) 287ba726b8aSAndrey A. Chernov return 1; 288ba726b8aSAndrey A. Chernov return 0; 289ba726b8aSAndrey A. Chernov } 2904b88c807SRodney W. Grimes 2915b78c6c2SSean Farley 2925b78c6c2SSean Farley /* 2935b78c6c2SSean Farley * Sets/unsets an environment variable from a pointer that may actually be a 2945b78c6c2SSean Farley * pointer into environ where the string should not be manipulated. 2955b78c6c2SSean Farley */ 29688328642SDavid E. O'Brien static void 2972cac6e36SJilles Tjoelker change_env(const char *s, int set) 2985b78c6c2SSean Farley { 2995b78c6c2SSean Farley char *eqp; 3005b78c6c2SSean Farley char *ss; 3015b78c6c2SSean Farley 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); 3105b78c6c2SSean Farley 3115b78c6c2SSean Farley return; 3125b78c6c2SSean Farley } 3135b78c6c2SSean Farley 3145b78c6c2SSean Farley 3154b88c807SRodney W. Grimes /* 3164b88c807SRodney W. Grimes * Same as setvar except that the variable and value are passed in 3174b88c807SRodney W. Grimes * the first argument as name=value. Since the first argument will 3184b88c807SRodney W. Grimes * be actually stored in the table, it should not be a string that 3194b88c807SRodney W. Grimes * will go away. 3204b88c807SRodney W. Grimes */ 3214b88c807SRodney W. Grimes 3224b88c807SRodney W. Grimes void 3235134c3f7SWarner Losh setvareq(char *s, int flags) 3244b88c807SRodney W. Grimes { 3254b88c807SRodney W. Grimes struct var *vp, **vpp; 3263a99ed46SJilles Tjoelker int nlen; 3274b88c807SRodney W. Grimes 328b8ec435eSMartin Cracauer if (aflag) 329b8ec435eSMartin Cracauer flags |= VEXPORT; 330*c543e1aeSJilles Tjoelker if (forcelocal && !(flags & (VNOSET | VNOLOCAL))) 331*c543e1aeSJilles Tjoelker mklocal(s); 3323a99ed46SJilles Tjoelker vp = find_var(s, &vpp, &nlen); 3333a99ed46SJilles Tjoelker if (vp != NULL) { 3343a99ed46SJilles Tjoelker if (vp->flags & VREADONLY) 3353a99ed46SJilles Tjoelker error("%.*s: is read only", vp->name_len, s); 336850460c0SJilles Tjoelker if (flags & VNOSET) 337850460c0SJilles Tjoelker return; 3384b88c807SRodney W. Grimes INTOFF; 339ab0a2172SSteve Price 340ab0a2172SSteve Price if (vp->func && (flags & VNOFUNC) == 0) 3413a99ed46SJilles Tjoelker (*vp->func)(s + vp->name_len + 1); 342ab0a2172SSteve Price 3434b88c807SRodney W. Grimes if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0) 3444b88c807SRodney W. Grimes ckfree(vp->text); 345ab0a2172SSteve Price 3464b88c807SRodney W. Grimes vp->flags &= ~(VTEXTFIXED|VSTACK|VUNSET); 3474b88c807SRodney W. Grimes vp->flags |= flags; 3484b88c807SRodney W. Grimes vp->text = s; 349ab0a2172SSteve Price 350ab0a2172SSteve Price /* 351ab0a2172SSteve Price * We could roll this to a function, to handle it as 352ab0a2172SSteve Price * a regular variable function callback, but why bother? 35357063576SJilles Tjoelker * 35457063576SJilles Tjoelker * Note: this assumes iflag is not set to 1 initially. 35557063576SJilles Tjoelker * As part of init(), this is called before arguments 35657063576SJilles Tjoelker * are looked at. 357ab0a2172SSteve Price */ 35857063576SJilles Tjoelker if ((vp == &vmpath || (vp == &vmail && ! mpathset())) && 35957063576SJilles Tjoelker iflag == 1) 3604b88c807SRodney W. Grimes chkmail(1); 361ba726b8aSAndrey A. Chernov if ((vp->flags & VEXPORT) && localevar(s)) { 3625b78c6c2SSean Farley change_env(s, 1); 363ba726b8aSAndrey A. Chernov (void) setlocale(LC_ALL, ""); 3646ed74a0aSJilles Tjoelker updatecharset(); 365ba726b8aSAndrey A. Chernov } 3664b88c807SRodney W. Grimes INTON; 3674b88c807SRodney W. Grimes return; 3684b88c807SRodney W. Grimes } 3694b88c807SRodney W. Grimes /* not found */ 370850460c0SJilles Tjoelker if (flags & VNOSET) 371850460c0SJilles Tjoelker return; 3724b88c807SRodney W. Grimes vp = ckmalloc(sizeof (*vp)); 3734b88c807SRodney W. Grimes vp->flags = flags; 3744b88c807SRodney W. Grimes vp->text = s; 3753a99ed46SJilles Tjoelker vp->name_len = nlen; 3764b88c807SRodney W. Grimes vp->next = *vpp; 377ab0a2172SSteve Price vp->func = NULL; 378ba726b8aSAndrey A. Chernov INTOFF; 3794b88c807SRodney W. Grimes *vpp = vp; 380ba726b8aSAndrey A. Chernov if ((vp->flags & VEXPORT) && localevar(s)) { 3815b78c6c2SSean Farley change_env(s, 1); 382ba726b8aSAndrey A. Chernov (void) setlocale(LC_ALL, ""); 3836ed74a0aSJilles Tjoelker updatecharset(); 384ba726b8aSAndrey A. Chernov } 385ba726b8aSAndrey A. Chernov INTON; 3864b88c807SRodney W. Grimes } 3874b88c807SRodney W. Grimes 3884b88c807SRodney W. Grimes 3894b88c807SRodney W. Grimes 3904b88c807SRodney W. Grimes /* 3914b88c807SRodney W. Grimes * Process a linked list of variable assignments. 3924b88c807SRodney W. Grimes */ 3934b88c807SRodney W. Grimes 3944b88c807SRodney W. Grimes void 395850460c0SJilles Tjoelker listsetvar(struct strlist *list, int flags) 3964b88c807SRodney W. Grimes { 3974b88c807SRodney W. Grimes struct strlist *lp; 3984b88c807SRodney W. Grimes 3994b88c807SRodney W. Grimes INTOFF; 4004b88c807SRodney W. Grimes for (lp = list ; lp ; lp = lp->next) { 401850460c0SJilles Tjoelker setvareq(savestr(lp->text), flags); 4024b88c807SRodney W. Grimes } 4034b88c807SRodney W. Grimes INTON; 4044b88c807SRodney W. Grimes } 4054b88c807SRodney W. Grimes 4064b88c807SRodney W. Grimes 4074b88c807SRodney W. Grimes 4084b88c807SRodney W. Grimes /* 4094b88c807SRodney W. Grimes * Find the value of a variable. Returns NULL if not set. 4104b88c807SRodney W. Grimes */ 4114b88c807SRodney W. Grimes 4124b88c807SRodney W. Grimes char * 4132cac6e36SJilles Tjoelker lookupvar(const char *name) 4144b88c807SRodney W. Grimes { 4154b88c807SRodney W. Grimes struct var *v; 4164b88c807SRodney W. Grimes 4173a99ed46SJilles Tjoelker v = find_var(name, NULL, NULL); 4183a99ed46SJilles Tjoelker if (v == NULL || v->flags & VUNSET) 4194b88c807SRodney W. Grimes return NULL; 4203a99ed46SJilles Tjoelker return v->text + v->name_len + 1; 4214b88c807SRodney W. Grimes } 4224b88c807SRodney W. Grimes 4234b88c807SRodney W. Grimes 4244b88c807SRodney W. Grimes 4254b88c807SRodney W. Grimes /* 4264b88c807SRodney W. Grimes * Search the environment of a builtin command. If the second argument 4274b88c807SRodney W. Grimes * is nonzero, return the value of a variable even if it hasn't been 4284b88c807SRodney W. Grimes * exported. 4294b88c807SRodney W. Grimes */ 4304b88c807SRodney W. Grimes 4314b88c807SRodney W. Grimes char * 4322cac6e36SJilles Tjoelker bltinlookup(const char *name, int doall) 4334b88c807SRodney W. Grimes { 4344b88c807SRodney W. Grimes struct strlist *sp; 4354b88c807SRodney W. Grimes struct var *v; 436011d162dSJilles Tjoelker char *result; 4374b88c807SRodney W. Grimes 438011d162dSJilles Tjoelker result = NULL; 4394b88c807SRodney W. Grimes for (sp = cmdenviron ; sp ; sp = sp->next) { 4404b88c807SRodney W. Grimes if (varequal(sp->text, name)) 441011d162dSJilles Tjoelker result = strchr(sp->text, '=') + 1; 4424b88c807SRodney W. Grimes } 443011d162dSJilles Tjoelker if (result != NULL) 444011d162dSJilles Tjoelker return result; 4453a99ed46SJilles Tjoelker 4463a99ed46SJilles Tjoelker v = find_var(name, NULL, NULL); 4473a99ed46SJilles Tjoelker if (v == NULL || v->flags & VUNSET || 4483a99ed46SJilles Tjoelker (!doall && (v->flags & VEXPORT) == 0)) 4494b88c807SRodney W. Grimes return NULL; 4503a99ed46SJilles Tjoelker return v->text + v->name_len + 1; 4514b88c807SRodney W. Grimes } 4524b88c807SRodney W. Grimes 4534b88c807SRodney W. Grimes 454e4b50334SJilles Tjoelker /* 455e4b50334SJilles Tjoelker * Set up locale for a builtin (LANG/LC_* assignments). 456e4b50334SJilles Tjoelker */ 457e4b50334SJilles Tjoelker void 458e4b50334SJilles Tjoelker bltinsetlocale(void) 459e4b50334SJilles Tjoelker { 460e4b50334SJilles Tjoelker struct strlist *lp; 461e4b50334SJilles Tjoelker int act = 0; 462e4b50334SJilles Tjoelker char *loc, *locdef; 463e4b50334SJilles Tjoelker int i; 464e4b50334SJilles Tjoelker 465e4b50334SJilles Tjoelker for (lp = cmdenviron ; lp ; lp = lp->next) { 466e4b50334SJilles Tjoelker if (localevar(lp->text)) { 467e4b50334SJilles Tjoelker act = 1; 468e4b50334SJilles Tjoelker break; 469e4b50334SJilles Tjoelker } 470e4b50334SJilles Tjoelker } 471e4b50334SJilles Tjoelker if (!act) 472e4b50334SJilles Tjoelker return; 473e4b50334SJilles Tjoelker loc = bltinlookup("LC_ALL", 0); 474e4b50334SJilles Tjoelker INTOFF; 475e4b50334SJilles Tjoelker if (loc != NULL) { 476e4b50334SJilles Tjoelker setlocale(LC_ALL, loc); 477e4b50334SJilles Tjoelker INTON; 4786ed74a0aSJilles Tjoelker updatecharset(); 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; 4906ed74a0aSJilles Tjoelker updatecharset(); 491e4b50334SJilles Tjoelker } 492e4b50334SJilles Tjoelker 493e4b50334SJilles Tjoelker /* 494e4b50334SJilles Tjoelker * Undo the effect of bltinlocaleset(). 495e4b50334SJilles Tjoelker */ 496e4b50334SJilles Tjoelker void 497e4b50334SJilles Tjoelker bltinunsetlocale(void) 498e4b50334SJilles Tjoelker { 499e4b50334SJilles Tjoelker struct strlist *lp; 500e4b50334SJilles Tjoelker 501e4b50334SJilles Tjoelker INTOFF; 502e4b50334SJilles Tjoelker for (lp = cmdenviron ; lp ; lp = lp->next) { 503e4b50334SJilles Tjoelker if (localevar(lp->text)) { 504e4b50334SJilles Tjoelker setlocale(LC_ALL, ""); 5056ed74a0aSJilles Tjoelker updatecharset(); 506e4b50334SJilles Tjoelker return; 507e4b50334SJilles Tjoelker } 508e4b50334SJilles Tjoelker } 509e4b50334SJilles Tjoelker INTON; 510e4b50334SJilles Tjoelker } 511e4b50334SJilles Tjoelker 5126ed74a0aSJilles Tjoelker /* 5136ed74a0aSJilles Tjoelker * Update the localeisutf8 flag. 5146ed74a0aSJilles Tjoelker */ 5156ed74a0aSJilles Tjoelker void 5166ed74a0aSJilles Tjoelker updatecharset(void) 5176ed74a0aSJilles Tjoelker { 5186ed74a0aSJilles Tjoelker char *charset; 5196ed74a0aSJilles Tjoelker 5206ed74a0aSJilles Tjoelker charset = nl_langinfo(CODESET); 5216ed74a0aSJilles Tjoelker localeisutf8 = !strcmp(charset, "UTF-8"); 5226ed74a0aSJilles Tjoelker } 5234b88c807SRodney W. Grimes 52407eb7033SJilles Tjoelker void 52507eb7033SJilles Tjoelker initcharset(void) 52607eb7033SJilles Tjoelker { 52707eb7033SJilles Tjoelker updatecharset(); 52807eb7033SJilles Tjoelker initial_localeisutf8 = localeisutf8; 52907eb7033SJilles Tjoelker } 53007eb7033SJilles Tjoelker 5314b88c807SRodney W. Grimes /* 5324b88c807SRodney W. Grimes * Generate a list of exported variables. This routine is used to construct 5334b88c807SRodney W. Grimes * the third argument to execve when executing a program. 5344b88c807SRodney W. Grimes */ 5354b88c807SRodney W. Grimes 5364b88c807SRodney W. Grimes char ** 5375134c3f7SWarner Losh environment(void) 5385134c3f7SWarner Losh { 5394b88c807SRodney W. Grimes int nenv; 5404b88c807SRodney W. Grimes struct var **vpp; 5414b88c807SRodney W. Grimes struct var *vp; 5424b88c807SRodney W. Grimes char **env, **ep; 5434b88c807SRodney W. Grimes 5444b88c807SRodney W. Grimes nenv = 0; 5454b88c807SRodney W. Grimes for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) { 5464b88c807SRodney W. Grimes for (vp = *vpp ; vp ; vp = vp->next) 5474b88c807SRodney W. Grimes if (vp->flags & VEXPORT) 5484b88c807SRodney W. Grimes nenv++; 5494b88c807SRodney W. Grimes } 5504b88c807SRodney W. Grimes ep = env = stalloc((nenv + 1) * sizeof *env); 5514b88c807SRodney W. Grimes for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) { 5524b88c807SRodney W. Grimes for (vp = *vpp ; vp ; vp = vp->next) 5534b88c807SRodney W. Grimes if (vp->flags & VEXPORT) 5544b88c807SRodney W. Grimes *ep++ = vp->text; 5554b88c807SRodney W. Grimes } 5564b88c807SRodney W. Grimes *ep = NULL; 5574b88c807SRodney W. Grimes return env; 5584b88c807SRodney W. Grimes } 5594b88c807SRodney W. Grimes 5604b88c807SRodney W. Grimes 56188328642SDavid E. O'Brien static int 562692f35fdSStefan Farfeleder var_compare(const void *a, const void *b) 563692f35fdSStefan Farfeleder { 564692f35fdSStefan Farfeleder const char *const *sa, *const *sb; 565692f35fdSStefan Farfeleder 566692f35fdSStefan Farfeleder sa = a; 567692f35fdSStefan Farfeleder sb = b; 568692f35fdSStefan Farfeleder /* 569692f35fdSStefan Farfeleder * This compares two var=value strings which creates a different 570692f35fdSStefan Farfeleder * order from what you would probably expect. POSIX is somewhat 571692f35fdSStefan Farfeleder * ambiguous on what should be sorted exactly. 572692f35fdSStefan Farfeleder */ 573692f35fdSStefan Farfeleder return strcoll(*sa, *sb); 574692f35fdSStefan Farfeleder } 575692f35fdSStefan Farfeleder 5764b88c807SRodney W. Grimes 5774b88c807SRodney W. Grimes /* 578cff1d849SJilles Tjoelker * Command to list all variables which are set. This is invoked from the 579cff1d849SJilles Tjoelker * set command when it is called without any options or operands. 5804b88c807SRodney W. Grimes */ 5814b88c807SRodney W. Grimes 5824b88c807SRodney W. Grimes int 5835134c3f7SWarner Losh showvarscmd(int argc __unused, char **argv __unused) 584aa9caaf6SPeter Wemm { 5854b88c807SRodney W. Grimes struct var **vpp; 5864b88c807SRodney W. Grimes struct var *vp; 58759258844STim J. Robbins const char *s; 588692f35fdSStefan Farfeleder const char **vars; 589692f35fdSStefan Farfeleder int i, n; 5904b88c807SRodney W. Grimes 591692f35fdSStefan Farfeleder /* 592692f35fdSStefan Farfeleder * POSIX requires us to sort the variables. 593692f35fdSStefan Farfeleder */ 594692f35fdSStefan Farfeleder n = 0; 5954b88c807SRodney W. Grimes for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) { 5964b88c807SRodney W. Grimes for (vp = *vpp; vp; vp = vp->next) { 597692f35fdSStefan Farfeleder if (!(vp->flags & VUNSET)) 598692f35fdSStefan Farfeleder n++; 599692f35fdSStefan Farfeleder } 600692f35fdSStefan Farfeleder } 601692f35fdSStefan Farfeleder 602692f35fdSStefan Farfeleder INTON; 603692f35fdSStefan Farfeleder vars = ckmalloc(n * sizeof(*vars)); 604692f35fdSStefan Farfeleder i = 0; 605692f35fdSStefan Farfeleder for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) { 606692f35fdSStefan Farfeleder for (vp = *vpp; vp; vp = vp->next) { 607692f35fdSStefan Farfeleder if (!(vp->flags & VUNSET)) 608692f35fdSStefan Farfeleder vars[i++] = vp->text; 609692f35fdSStefan Farfeleder } 610692f35fdSStefan Farfeleder } 611692f35fdSStefan Farfeleder 612692f35fdSStefan Farfeleder qsort(vars, n, sizeof(*vars), var_compare); 613692f35fdSStefan Farfeleder for (i = 0; i < n; i++) { 614aeb5d065SJilles Tjoelker s = strchr(vars[i], '='); 615aeb5d065SJilles Tjoelker s++; 616aeb5d065SJilles Tjoelker outbin(vars[i], s - vars[i], out1); 617aeb5d065SJilles Tjoelker out1qstr(s); 61859258844STim J. Robbins out1c('\n'); 6194b88c807SRodney W. Grimes } 620692f35fdSStefan Farfeleder ckfree(vars); 621692f35fdSStefan Farfeleder INTOFF; 622692f35fdSStefan Farfeleder 6234b88c807SRodney W. Grimes return 0; 6244b88c807SRodney W. Grimes } 6254b88c807SRodney W. Grimes 6264b88c807SRodney W. Grimes 6274b88c807SRodney W. Grimes 6284b88c807SRodney W. Grimes /* 6294b88c807SRodney W. Grimes * The export and readonly commands. 6304b88c807SRodney W. Grimes */ 6314b88c807SRodney W. Grimes 6324b88c807SRodney W. Grimes int 6335134c3f7SWarner Losh exportcmd(int argc, char **argv) 634aa9caaf6SPeter Wemm { 6354b88c807SRodney W. Grimes struct var **vpp; 6364b88c807SRodney W. Grimes struct var *vp; 6374b88c807SRodney W. Grimes char *name; 6384b88c807SRodney W. Grimes char *p; 63945086f8cSTim J. Robbins char *cmdname; 64045086f8cSTim J. Robbins int ch, values; 6414b88c807SRodney W. Grimes int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT; 6424b88c807SRodney W. Grimes 64345086f8cSTim J. Robbins cmdname = argv[0]; 64445086f8cSTim J. Robbins optreset = optind = 1; 645050f7913STim J. Robbins opterr = 0; 64645086f8cSTim J. Robbins values = 0; 64745086f8cSTim J. Robbins while ((ch = getopt(argc, argv, "p")) != -1) { 64845086f8cSTim J. Robbins switch (ch) { 64945086f8cSTim J. Robbins case 'p': 65045086f8cSTim J. Robbins values = 1; 65145086f8cSTim J. Robbins break; 65245086f8cSTim J. Robbins case '?': 65345086f8cSTim J. Robbins default: 65445086f8cSTim J. Robbins error("unknown option: -%c", optopt); 65545086f8cSTim J. Robbins } 65645086f8cSTim J. Robbins } 65745086f8cSTim J. Robbins argc -= optind; 65845086f8cSTim J. Robbins argv += optind; 65945086f8cSTim J. Robbins 6603fda45ecSStefan Farfeleder if (values && argc != 0) 6613fda45ecSStefan Farfeleder error("-p requires no arguments"); 66245086f8cSTim J. Robbins if (argc != 0) { 6633fda45ecSStefan Farfeleder while ((name = *argv++) != NULL) { 6644b88c807SRodney W. Grimes if ((p = strchr(name, '=')) != NULL) { 6654b88c807SRodney W. Grimes p++; 6664b88c807SRodney W. Grimes } else { 6673a99ed46SJilles Tjoelker vp = find_var(name, NULL, NULL); 6683a99ed46SJilles Tjoelker if (vp != NULL) { 6694b88c807SRodney W. Grimes vp->flags |= flag; 670ba726b8aSAndrey A. Chernov if ((vp->flags & VEXPORT) && localevar(vp->text)) { 6715b78c6c2SSean Farley change_env(vp->text, 1); 672ba726b8aSAndrey A. Chernov (void) setlocale(LC_ALL, ""); 6736ed74a0aSJilles Tjoelker updatecharset(); 674ba726b8aSAndrey A. Chernov } 6753a99ed46SJilles Tjoelker continue; 6764b88c807SRodney W. Grimes } 6774b88c807SRodney W. Grimes } 6784b88c807SRodney W. Grimes setvar(name, p, flag); 6794b88c807SRodney W. Grimes } 6804b88c807SRodney W. Grimes } else { 6814b88c807SRodney W. Grimes for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) { 6824b88c807SRodney W. Grimes for (vp = *vpp ; vp ; vp = vp->next) { 6834b88c807SRodney W. Grimes if (vp->flags & flag) { 68445086f8cSTim J. Robbins if (values) { 68545086f8cSTim J. Robbins out1str(cmdname); 68645086f8cSTim J. Robbins out1c(' '); 68745086f8cSTim J. Robbins } 68845086f8cSTim J. Robbins if (values && !(vp->flags & VUNSET)) { 689258ef734SJilles Tjoelker outbin(vp->text, 690258ef734SJilles Tjoelker vp->name_len + 1, out1); 691258ef734SJilles Tjoelker out1qstr(vp->text + 692258ef734SJilles Tjoelker vp->name_len + 1); 693aeb5d065SJilles Tjoelker } else 694258ef734SJilles Tjoelker outbin(vp->text, vp->name_len, 695aeb5d065SJilles Tjoelker out1); 6964b88c807SRodney W. Grimes out1c('\n'); 6974b88c807SRodney W. Grimes } 6984b88c807SRodney W. Grimes } 6994b88c807SRodney W. Grimes } 7004b88c807SRodney W. Grimes } 7014b88c807SRodney W. Grimes return 0; 7024b88c807SRodney W. Grimes } 7034b88c807SRodney W. Grimes 7044b88c807SRodney W. Grimes 7054b88c807SRodney W. Grimes /* 7064b88c807SRodney W. Grimes * The "local" command. 7074b88c807SRodney W. Grimes */ 7084b88c807SRodney W. Grimes 709aa9caaf6SPeter Wemm int 7105134c3f7SWarner Losh localcmd(int argc __unused, char **argv __unused) 711aa9caaf6SPeter Wemm { 7124b88c807SRodney W. Grimes char *name; 7134b88c807SRodney W. Grimes 7144b88c807SRodney W. Grimes if (! in_function()) 7154b88c807SRodney W. Grimes error("Not in a function"); 7164b88c807SRodney W. Grimes while ((name = *argptr++) != NULL) { 7174b88c807SRodney W. Grimes mklocal(name); 7184b88c807SRodney W. Grimes } 7194b88c807SRodney W. Grimes return 0; 7204b88c807SRodney W. Grimes } 7214b88c807SRodney W. Grimes 7224b88c807SRodney W. Grimes 7234b88c807SRodney W. Grimes /* 7244b88c807SRodney W. Grimes * Make a variable a local variable. When a variable is made local, it's 7254b88c807SRodney W. Grimes * value and flags are saved in a localvar structure. The saved values 7264b88c807SRodney W. Grimes * will be restored when the shell function returns. We handle the name 7274b88c807SRodney W. Grimes * "-" as a special case. 7284b88c807SRodney W. Grimes */ 7294b88c807SRodney W. Grimes 7304b88c807SRodney W. Grimes void 7315134c3f7SWarner Losh mklocal(char *name) 7324b88c807SRodney W. Grimes { 7334b88c807SRodney W. Grimes struct localvar *lvp; 7344b88c807SRodney W. Grimes struct var **vpp; 7354b88c807SRodney W. Grimes struct var *vp; 7364b88c807SRodney W. Grimes 7374b88c807SRodney W. Grimes INTOFF; 7384b88c807SRodney W. Grimes lvp = ckmalloc(sizeof (struct localvar)); 7394b88c807SRodney W. Grimes if (name[0] == '-' && name[1] == '\0') { 7404b88c807SRodney W. Grimes lvp->text = ckmalloc(sizeof optlist); 741aa9caaf6SPeter Wemm memcpy(lvp->text, optlist, sizeof optlist); 7424b88c807SRodney W. Grimes vp = NULL; 7434b88c807SRodney W. Grimes } else { 7443a99ed46SJilles Tjoelker vp = find_var(name, &vpp, NULL); 7454b88c807SRodney W. Grimes if (vp == NULL) { 7464b88c807SRodney W. Grimes if (strchr(name, '=')) 747*c543e1aeSJilles Tjoelker setvareq(savestr(name), VSTRFIXED | VNOLOCAL); 7484b88c807SRodney W. Grimes else 749*c543e1aeSJilles Tjoelker setvar(name, NULL, VSTRFIXED | VNOLOCAL); 7504b88c807SRodney W. Grimes vp = *vpp; /* the new variable */ 7514b88c807SRodney W. Grimes lvp->text = NULL; 7524b88c807SRodney W. Grimes lvp->flags = VUNSET; 7534b88c807SRodney W. Grimes } else { 7544b88c807SRodney W. Grimes lvp->text = vp->text; 7554b88c807SRodney W. Grimes lvp->flags = vp->flags; 7564b88c807SRodney W. Grimes vp->flags |= VSTRFIXED|VTEXTFIXED; 7573a99ed46SJilles Tjoelker if (name[vp->name_len] == '=') 758*c543e1aeSJilles Tjoelker setvareq(savestr(name), VNOLOCAL); 7594b88c807SRodney W. Grimes } 7604b88c807SRodney W. Grimes } 7614b88c807SRodney W. Grimes lvp->vp = vp; 7624b88c807SRodney W. Grimes lvp->next = localvars; 7634b88c807SRodney W. Grimes localvars = lvp; 7644b88c807SRodney W. Grimes INTON; 7654b88c807SRodney W. Grimes } 7664b88c807SRodney W. Grimes 7674b88c807SRodney W. Grimes 7684b88c807SRodney W. Grimes /* 7694b88c807SRodney W. Grimes * Called after a function returns. 7704b88c807SRodney W. Grimes */ 7714b88c807SRodney W. Grimes 7724b88c807SRodney W. Grimes void 7735134c3f7SWarner Losh poplocalvars(void) 7745134c3f7SWarner Losh { 7754b88c807SRodney W. Grimes struct localvar *lvp; 7764b88c807SRodney W. Grimes struct var *vp; 7774b88c807SRodney W. Grimes 7784b88c807SRodney W. Grimes while ((lvp = localvars) != NULL) { 7794b88c807SRodney W. Grimes localvars = lvp->next; 7804b88c807SRodney W. Grimes vp = lvp->vp; 7814b88c807SRodney W. Grimes if (vp == NULL) { /* $- saved */ 782aa9caaf6SPeter Wemm memcpy(optlist, lvp->text, sizeof optlist); 7834b88c807SRodney W. Grimes ckfree(lvp->text); 7844a7b1013SJilles Tjoelker optschanged(); 7854b88c807SRodney W. Grimes } else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) { 7864b88c807SRodney W. Grimes (void)unsetvar(vp->text); 7874b88c807SRodney W. Grimes } else { 7884b88c807SRodney W. Grimes if ((vp->flags & VTEXTFIXED) == 0) 7894b88c807SRodney W. Grimes ckfree(vp->text); 7904b88c807SRodney W. Grimes vp->flags = lvp->flags; 7914b88c807SRodney W. Grimes vp->text = lvp->text; 7924b88c807SRodney W. Grimes } 7934b88c807SRodney W. Grimes ckfree(lvp); 7944b88c807SRodney W. Grimes } 7954b88c807SRodney W. Grimes } 7964b88c807SRodney W. Grimes 7974b88c807SRodney W. Grimes 798aa9caaf6SPeter Wemm int 7995134c3f7SWarner Losh setvarcmd(int argc, char **argv) 800aa9caaf6SPeter Wemm { 8014b88c807SRodney W. Grimes if (argc <= 2) 8024b88c807SRodney W. Grimes return unsetcmd(argc, argv); 8034b88c807SRodney W. Grimes else if (argc == 3) 8044b88c807SRodney W. Grimes setvar(argv[1], argv[2], 0); 8054b88c807SRodney W. Grimes else 806274110dfSJilles Tjoelker error("too many arguments"); 8074b88c807SRodney W. Grimes return 0; 8084b88c807SRodney W. Grimes } 8094b88c807SRodney W. Grimes 8104b88c807SRodney W. Grimes 8114b88c807SRodney W. Grimes /* 812cff1d849SJilles Tjoelker * The unset builtin command. 8134b88c807SRodney W. Grimes */ 8144b88c807SRodney W. Grimes 815aa9caaf6SPeter Wemm int 8165134c3f7SWarner Losh unsetcmd(int argc __unused, char **argv __unused) 817aa9caaf6SPeter Wemm { 8184b88c807SRodney W. Grimes char **ap; 8194b88c807SRodney W. Grimes int i; 8204b88c807SRodney W. Grimes int flg_func = 0; 8214b88c807SRodney W. Grimes int flg_var = 0; 8224b88c807SRodney W. Grimes int ret = 0; 8234b88c807SRodney W. Grimes 8244b88c807SRodney W. Grimes while ((i = nextopt("vf")) != '\0') { 8254b88c807SRodney W. Grimes if (i == 'f') 8264b88c807SRodney W. Grimes flg_func = 1; 8274b88c807SRodney W. Grimes else 8284b88c807SRodney W. Grimes flg_var = 1; 8294b88c807SRodney W. Grimes } 8304b88c807SRodney W. Grimes if (flg_func == 0 && flg_var == 0) 8314b88c807SRodney W. Grimes flg_var = 1; 8324b88c807SRodney W. Grimes 8334b88c807SRodney W. Grimes for (ap = argptr; *ap ; ap++) { 8344b88c807SRodney W. Grimes if (flg_func) 8354b88c807SRodney W. Grimes ret |= unsetfunc(*ap); 8364b88c807SRodney W. Grimes if (flg_var) 8374b88c807SRodney W. Grimes ret |= unsetvar(*ap); 8384b88c807SRodney W. Grimes } 8394b88c807SRodney W. Grimes return ret; 8404b88c807SRodney W. Grimes } 8414b88c807SRodney W. Grimes 8424b88c807SRodney W. Grimes 8434b88c807SRodney W. Grimes /* 8444b88c807SRodney W. Grimes * Unset the specified variable. 8454b88c807SRodney W. Grimes */ 8464b88c807SRodney W. Grimes 847ab0a2172SSteve Price int 8482cac6e36SJilles Tjoelker unsetvar(const char *s) 8494b88c807SRodney W. Grimes { 8504b88c807SRodney W. Grimes struct var **vpp; 8514b88c807SRodney W. Grimes struct var *vp; 8524b88c807SRodney W. Grimes 8533a99ed46SJilles Tjoelker vp = find_var(s, &vpp, NULL); 8543a99ed46SJilles Tjoelker if (vp == NULL) 8553a99ed46SJilles Tjoelker return (0); 8564b88c807SRodney W. Grimes if (vp->flags & VREADONLY) 8574b88c807SRodney W. Grimes return (1); 8584b88c807SRodney W. Grimes INTOFF; 8593a99ed46SJilles Tjoelker if (vp->text[vp->name_len + 1] != '\0') 8604b88c807SRodney W. Grimes setvar(s, nullstr, 0); 861ba726b8aSAndrey A. Chernov if ((vp->flags & VEXPORT) && localevar(vp->text)) { 8625b78c6c2SSean Farley change_env(s, 0); 863ba726b8aSAndrey A. Chernov setlocale(LC_ALL, ""); 8646ed74a0aSJilles Tjoelker updatecharset(); 865ba726b8aSAndrey A. Chernov } 8664b88c807SRodney W. Grimes vp->flags &= ~VEXPORT; 8674b88c807SRodney W. Grimes vp->flags |= VUNSET; 8684b88c807SRodney W. Grimes if ((vp->flags & VSTRFIXED) == 0) { 8694b88c807SRodney W. Grimes if ((vp->flags & VTEXTFIXED) == 0) 8704b88c807SRodney W. Grimes ckfree(vp->text); 8714b88c807SRodney W. Grimes *vpp = vp->next; 8724b88c807SRodney W. Grimes ckfree(vp); 8734b88c807SRodney W. Grimes } 8744b88c807SRodney W. Grimes INTON; 8754b88c807SRodney W. Grimes return (0); 8764b88c807SRodney W. Grimes } 8774b88c807SRodney W. Grimes 8784b88c807SRodney W. Grimes 8794b88c807SRodney W. Grimes 8804b88c807SRodney W. Grimes /* 8814b88c807SRodney W. Grimes * Returns true if the two strings specify the same varable. The first 8824b88c807SRodney W. Grimes * variable name is terminated by '='; the second may be terminated by 8834b88c807SRodney W. Grimes * either '=' or '\0'. 8844b88c807SRodney W. Grimes */ 8854b88c807SRodney W. Grimes 88688328642SDavid E. O'Brien static int 8872cac6e36SJilles Tjoelker varequal(const char *p, const char *q) 8884b88c807SRodney W. Grimes { 8894b88c807SRodney W. Grimes while (*p == *q++) { 8904b88c807SRodney W. Grimes if (*p++ == '=') 8914b88c807SRodney W. Grimes return 1; 8924b88c807SRodney W. Grimes } 8934b88c807SRodney W. Grimes if (*p == '=' && *(q - 1) == '\0') 8944b88c807SRodney W. Grimes return 1; 8954b88c807SRodney W. Grimes return 0; 8964b88c807SRodney W. Grimes } 8973a99ed46SJilles Tjoelker 8983a99ed46SJilles Tjoelker /* 8993a99ed46SJilles Tjoelker * Search for a variable. 9003a99ed46SJilles Tjoelker * 'name' may be terminated by '=' or a NUL. 9013a99ed46SJilles Tjoelker * vppp is set to the pointer to vp, or the list head if vp isn't found 9023a99ed46SJilles Tjoelker * lenp is set to the number of charactets in 'name' 9033a99ed46SJilles Tjoelker */ 9043a99ed46SJilles Tjoelker 9053a99ed46SJilles Tjoelker static struct var * 9063a99ed46SJilles Tjoelker find_var(const char *name, struct var ***vppp, int *lenp) 9073a99ed46SJilles Tjoelker { 9083a99ed46SJilles Tjoelker unsigned int hashval; 9093a99ed46SJilles Tjoelker int len; 9103a99ed46SJilles Tjoelker struct var *vp, **vpp; 9113a99ed46SJilles Tjoelker const char *p = name; 9123a99ed46SJilles Tjoelker 9133a99ed46SJilles Tjoelker hashval = 0; 9143a99ed46SJilles Tjoelker while (*p && *p != '=') 9153a99ed46SJilles Tjoelker hashval = 2 * hashval + (unsigned char)*p++; 9163a99ed46SJilles Tjoelker len = p - name; 9173a99ed46SJilles Tjoelker 9183a99ed46SJilles Tjoelker if (lenp) 9193a99ed46SJilles Tjoelker *lenp = len; 9203a99ed46SJilles Tjoelker vpp = &vartab[hashval % VTABSIZE]; 9213a99ed46SJilles Tjoelker if (vppp) 9223a99ed46SJilles Tjoelker *vppp = vpp; 9233a99ed46SJilles Tjoelker 9243a99ed46SJilles Tjoelker for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) { 9253a99ed46SJilles Tjoelker if (vp->name_len != len) 9263a99ed46SJilles Tjoelker continue; 9273a99ed46SJilles Tjoelker if (memcmp(vp->text, name, len) != 0) 9283a99ed46SJilles Tjoelker continue; 9293a99ed46SJilles Tjoelker if (vppp) 9303a99ed46SJilles Tjoelker *vppp = vpp; 9313a99ed46SJilles Tjoelker return vp; 9323a99ed46SJilles Tjoelker } 9333a99ed46SJilles Tjoelker return NULL; 9343a99ed46SJilles Tjoelker } 935