14b88c807SRodney W. Grimes /*- 24b88c807SRodney W. Grimes * Copyright (c) 1991, 1993 34b88c807SRodney W. Grimes * The Regents of the University of California. All rights reserved. 44b88c807SRodney W. Grimes * 54b88c807SRodney W. Grimes * This code is derived from software contributed to Berkeley by 64b88c807SRodney W. Grimes * Kenneth Almquist. 74b88c807SRodney W. Grimes * 84b88c807SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 94b88c807SRodney W. Grimes * modification, are permitted provided that the following conditions 104b88c807SRodney W. Grimes * are met: 114b88c807SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 124b88c807SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 134b88c807SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 144b88c807SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 154b88c807SRodney W. Grimes * documentation and/or other materials provided with the distribution. 164b88c807SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 174b88c807SRodney W. Grimes * may be used to endorse or promote products derived from this software 184b88c807SRodney W. Grimes * without specific prior written permission. 194b88c807SRodney W. Grimes * 204b88c807SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 214b88c807SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 224b88c807SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 234b88c807SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 244b88c807SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 254b88c807SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 264b88c807SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 274b88c807SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 284b88c807SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 294b88c807SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 304b88c807SRodney W. Grimes * SUCH DAMAGE. 314b88c807SRodney W. Grimes */ 324b88c807SRodney W. Grimes 334b88c807SRodney W. Grimes #ifndef lint 343d7b5b93SPhilippe Charnier #if 0 353d7b5b93SPhilippe Charnier static char sccsid[] = "@(#)var.c 8.3 (Berkeley) 5/4/95"; 363d7b5b93SPhilippe Charnier #endif 374b88c807SRodney W. Grimes #endif /* not lint */ 382749b141SDavid E. O'Brien #include <sys/cdefs.h> 392749b141SDavid E. O'Brien __FBSDID("$FreeBSD$"); 404b88c807SRodney W. Grimes 41aa9caaf6SPeter Wemm #include <unistd.h> 42aa9caaf6SPeter Wemm #include <stdlib.h> 438d5c19ffSDavid E. O'Brien #include <paths.h> 44aa9caaf6SPeter Wemm 454b88c807SRodney W. Grimes /* 464b88c807SRodney W. Grimes * Shell variables. 474b88c807SRodney W. Grimes */ 484b88c807SRodney W. Grimes 49ba726b8aSAndrey A. Chernov #include <locale.h> 506ed74a0aSJilles Tjoelker #include <langinfo.h> 51ba726b8aSAndrey A. Chernov 524b88c807SRodney W. Grimes #include "shell.h" 534b88c807SRodney W. Grimes #include "output.h" 544b88c807SRodney W. Grimes #include "expand.h" 554b88c807SRodney W. Grimes #include "nodes.h" /* for other headers */ 564b88c807SRodney W. Grimes #include "eval.h" /* defines cmdenviron */ 574b88c807SRodney W. Grimes #include "exec.h" 584b88c807SRodney W. Grimes #include "syntax.h" 594b88c807SRodney W. Grimes #include "options.h" 604b88c807SRodney W. Grimes #include "mail.h" 614b88c807SRodney W. Grimes #include "var.h" 624b88c807SRodney W. Grimes #include "memalloc.h" 634b88c807SRodney W. Grimes #include "error.h" 644b88c807SRodney W. Grimes #include "mystring.h" 65ab0a2172SSteve Price #include "parser.h" 66454a02b3SJilles Tjoelker #include "builtins.h" 67aa9caaf6SPeter Wemm #ifndef NO_HISTORY 68aa9caaf6SPeter Wemm #include "myhistedit.h" 69aa9caaf6SPeter Wemm #endif 704b88c807SRodney W. Grimes 714b88c807SRodney W. Grimes 724b88c807SRodney W. Grimes #define VTABSIZE 39 734b88c807SRodney W. Grimes 744b88c807SRodney W. Grimes 754b88c807SRodney W. Grimes struct varinit { 764b88c807SRodney W. Grimes struct var *var; 774b88c807SRodney W. Grimes int flags; 78c6c5dd37SJilles Tjoelker const char *text; 795134c3f7SWarner Losh void (*func)(const char *); 804b88c807SRodney W. Grimes }; 814b88c807SRodney W. Grimes 824b88c807SRodney W. Grimes 83aa9caaf6SPeter Wemm #ifndef NO_HISTORY 844b88c807SRodney W. Grimes struct var vhistsize; 85580eefdfSJilles Tjoelker struct var vterm; 86aa9caaf6SPeter Wemm #endif 874b88c807SRodney W. Grimes struct var vifs; 884b88c807SRodney W. Grimes struct var vmail; 894b88c807SRodney W. Grimes struct var vmpath; 904b88c807SRodney W. Grimes struct var vpath; 914b88c807SRodney W. Grimes struct var vps1; 924b88c807SRodney W. Grimes struct var vps2; 93120c8e6cSStefan Farfeleder struct var vps4; 94aa7b6f82SDavid E. O'Brien static struct var voptind; 95caf29fabSJilles Tjoelker struct var vdisvfork; 964b88c807SRodney W. Grimes 97c543e1aeSJilles Tjoelker int forcelocal; 98c543e1aeSJilles 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 }, 1124b88c807SRodney W. Grimes /* 1134b88c807SRodney W. Grimes * vps1 depends on uid 1144b88c807SRodney W. Grimes */ 115c6c5dd37SJilles Tjoelker { &vps2, 0, "PS2=> ", 116ab0a2172SSteve Price NULL }, 117c6c5dd37SJilles Tjoelker { &vps4, 0, "PS4=+ ", 118120c8e6cSStefan Farfeleder NULL }, 119580eefdfSJilles Tjoelker #ifndef NO_HISTORY 120580eefdfSJilles Tjoelker { &vterm, VUNSET, "TERM=", 121580eefdfSJilles Tjoelker setterm }, 122580eefdfSJilles Tjoelker #endif 123c6c5dd37SJilles Tjoelker { &voptind, 0, "OPTIND=1", 124ab0a2172SSteve Price getoptsreset }, 125caf29fabSJilles Tjoelker { &vdisvfork, VUNSET, "SH_DISABLE_VFORK=", 126caf29fabSJilles Tjoelker NULL }, 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 14559e0cc8eSJilles Tjoelker extern char **environ; 1464b88c807SRodney W. Grimes 1474b88c807SRodney W. Grimes /* 14859e0cc8eSJilles Tjoelker * This routine initializes the builtin variables and imports the environment. 14959e0cc8eSJilles Tjoelker * It is called when the shell is initialized. 1504b88c807SRodney W. Grimes */ 1514b88c807SRodney W. Grimes 1524b88c807SRodney W. Grimes void 1535134c3f7SWarner Losh initvar(void) 1545134c3f7SWarner Losh { 15539dccc6fSTim J. Robbins char ppid[20]; 1564b88c807SRodney W. Grimes const struct varinit *ip; 1574b88c807SRodney W. Grimes struct var *vp; 1584b88c807SRodney W. Grimes struct var **vpp; 15959e0cc8eSJilles Tjoelker char **envp; 1604b88c807SRodney W. Grimes 1614b88c807SRodney W. Grimes for (ip = varinit ; (vp = ip->var) != NULL ; ip++) { 1623a99ed46SJilles Tjoelker if (find_var(ip->text, &vpp, &vp->name_len) != NULL) 1633a99ed46SJilles Tjoelker continue; 1644b88c807SRodney W. Grimes vp->next = *vpp; 1654b88c807SRodney W. Grimes *vpp = vp; 166c6c5dd37SJilles Tjoelker vp->text = __DECONST(char *, ip->text); 167c6c5dd37SJilles Tjoelker vp->flags = ip->flags | VSTRFIXED | VTEXTFIXED; 168ab0a2172SSteve Price vp->func = ip->func; 1694b88c807SRodney W. Grimes } 1704b88c807SRodney W. Grimes /* 1714b88c807SRodney W. Grimes * PS1 depends on uid 1724b88c807SRodney W. Grimes */ 1733a99ed46SJilles Tjoelker if (find_var("PS1", &vpp, &vps1.name_len) == NULL) { 1744b88c807SRodney W. Grimes vps1.next = *vpp; 1754b88c807SRodney W. Grimes *vpp = &vps1; 176c6c5dd37SJilles Tjoelker vps1.text = __DECONST(char *, geteuid() ? "PS1=$ " : "PS1=# "); 1774b88c807SRodney W. Grimes vps1.flags = VSTRFIXED|VTEXTFIXED; 1784b88c807SRodney W. Grimes } 17939dccc6fSTim J. Robbins fmtstr(ppid, sizeof(ppid), "%d", (int)getppid()); 18039dccc6fSTim J. Robbins setvarsafe("PPID", ppid, 0); 18159e0cc8eSJilles Tjoelker for (envp = environ ; *envp ; envp++) { 18259e0cc8eSJilles Tjoelker if (strchr(*envp, '=')) { 18359e0cc8eSJilles Tjoelker setvareq(*envp, VEXPORT|VTEXTFIXED); 18459e0cc8eSJilles Tjoelker } 18559e0cc8eSJilles Tjoelker } 1867e81d0c8SJilles Tjoelker setvareq("OPTIND=1", VTEXTFIXED); 1874b88c807SRodney W. Grimes } 1884b88c807SRodney W. Grimes 1894b88c807SRodney W. Grimes /* 190ab0a2172SSteve Price * Safe version of setvar, returns 1 on success 0 on failure. 191ab0a2172SSteve Price */ 192ab0a2172SSteve Price 193ab0a2172SSteve Price int 1942cac6e36SJilles Tjoelker setvarsafe(const char *name, const char *val, int flags) 195ab0a2172SSteve Price { 196ab0a2172SSteve Price struct jmploc jmploc; 197224fbf9fSJilles Tjoelker struct jmploc *const savehandler = handler; 198ab0a2172SSteve Price int err = 0; 1999922c6d2SJilles Tjoelker int inton; 200ab0a2172SSteve Price 2019922c6d2SJilles Tjoelker inton = is_int_on(); 202ab0a2172SSteve Price if (setjmp(jmploc.loc)) 203ab0a2172SSteve Price err = 1; 204ab0a2172SSteve Price else { 205ab0a2172SSteve Price handler = &jmploc; 206ab0a2172SSteve Price setvar(name, val, flags); 207ab0a2172SSteve Price } 208ab0a2172SSteve Price handler = savehandler; 2099922c6d2SJilles Tjoelker SETINTON(inton); 210ab0a2172SSteve Price return err; 211ab0a2172SSteve Price } 212ab0a2172SSteve Price 213ab0a2172SSteve Price /* 214b3decf89SJens Schweikhardt * Set the value of a variable. The flags argument is stored with the 2154b88c807SRodney W. Grimes * flags of the variable. If val is NULL, the variable is unset. 2164b88c807SRodney W. Grimes */ 2174b88c807SRodney W. Grimes 2184b88c807SRodney W. Grimes void 2192cac6e36SJilles Tjoelker setvar(const char *name, const char *val, int flags) 2204b88c807SRodney W. Grimes { 2212cac6e36SJilles Tjoelker const char *p; 222670dd3f0SJilles Tjoelker size_t len; 223670dd3f0SJilles Tjoelker size_t namelen; 224670dd3f0SJilles Tjoelker size_t vallen; 2254b88c807SRodney W. Grimes char *nameeq; 2264b88c807SRodney W. Grimes int isbad; 2274b88c807SRodney W. Grimes 2284b88c807SRodney W. Grimes isbad = 0; 2294b88c807SRodney W. Grimes p = name; 230ba726b8aSAndrey A. Chernov if (! is_name(*p)) 2314b88c807SRodney W. Grimes isbad = 1; 232ba726b8aSAndrey A. Chernov p++; 2334b88c807SRodney W. Grimes for (;;) { 2344b88c807SRodney W. Grimes if (! is_in_name(*p)) { 2354b88c807SRodney W. Grimes if (*p == '\0' || *p == '=') 2364b88c807SRodney W. Grimes break; 2374b88c807SRodney W. Grimes isbad = 1; 2384b88c807SRodney W. Grimes } 2394b88c807SRodney W. Grimes p++; 2404b88c807SRodney W. Grimes } 2414b88c807SRodney W. Grimes namelen = p - name; 2424b88c807SRodney W. Grimes if (isbad) 243670dd3f0SJilles Tjoelker error("%.*s: bad variable name", (int)namelen, name); 2444b88c807SRodney W. Grimes len = namelen + 2; /* 2 is space for '=' and '\0' */ 2454b88c807SRodney W. Grimes if (val == NULL) { 2464b88c807SRodney W. Grimes flags |= VUNSET; 247670dd3f0SJilles Tjoelker vallen = 0; 2484b88c807SRodney W. Grimes } else { 249670dd3f0SJilles Tjoelker vallen = strlen(val); 250670dd3f0SJilles Tjoelker len += vallen; 2514b88c807SRodney W. Grimes } 2521632bf1aSJilles Tjoelker INTOFF; 2532cac6e36SJilles Tjoelker nameeq = ckmalloc(len); 2542cac6e36SJilles Tjoelker memcpy(nameeq, name, namelen); 2552cac6e36SJilles Tjoelker nameeq[namelen] = '='; 2564b88c807SRodney W. Grimes if (val) 257670dd3f0SJilles Tjoelker memcpy(nameeq + namelen + 1, val, vallen + 1); 2582cac6e36SJilles Tjoelker else 2592cac6e36SJilles Tjoelker nameeq[namelen + 1] = '\0'; 2604b88c807SRodney W. Grimes setvareq(nameeq, flags); 2611632bf1aSJilles Tjoelker INTON; 2624b88c807SRodney W. Grimes } 2634b88c807SRodney W. Grimes 26488328642SDavid E. O'Brien static int 2652cac6e36SJilles Tjoelker localevar(const char *s) 266ba726b8aSAndrey A. Chernov { 267e4b50334SJilles Tjoelker const char *const *ss; 2684b88c807SRodney W. Grimes 269ba726b8aSAndrey A. Chernov if (*s != 'L') 270ba726b8aSAndrey A. Chernov return 0; 271ba726b8aSAndrey A. Chernov if (varequal(s + 1, "ANG")) 272ba726b8aSAndrey A. Chernov return 1; 273ba726b8aSAndrey A. Chernov if (strncmp(s + 1, "C_", 2) != 0) 274ba726b8aSAndrey A. Chernov return 0; 275e4b50334SJilles Tjoelker if (varequal(s + 3, "ALL")) 276e4b50334SJilles Tjoelker return 1; 277e4b50334SJilles Tjoelker for (ss = locale_names; *ss ; ss++) 278e4b50334SJilles Tjoelker if (varequal(s + 3, *ss + 3)) 279ba726b8aSAndrey A. Chernov return 1; 280ba726b8aSAndrey A. Chernov return 0; 281ba726b8aSAndrey A. Chernov } 2824b88c807SRodney W. Grimes 2835b78c6c2SSean Farley 2845b78c6c2SSean Farley /* 2855b78c6c2SSean Farley * Sets/unsets an environment variable from a pointer that may actually be a 2865b78c6c2SSean Farley * pointer into environ where the string should not be manipulated. 2875b78c6c2SSean Farley */ 28888328642SDavid E. O'Brien static void 2892cac6e36SJilles Tjoelker change_env(const char *s, int set) 2905b78c6c2SSean Farley { 2915b78c6c2SSean Farley char *eqp; 2925b78c6c2SSean Farley char *ss; 2935b78c6c2SSean Farley 2941632bf1aSJilles Tjoelker INTOFF; 2955b78c6c2SSean Farley ss = savestr(s); 2965b78c6c2SSean Farley if ((eqp = strchr(ss, '=')) != NULL) 2975b78c6c2SSean Farley *eqp = '\0'; 2985b78c6c2SSean Farley if (set && eqp != NULL) 2995b78c6c2SSean Farley (void) setenv(ss, eqp + 1, 1); 3005b78c6c2SSean Farley else 3015b78c6c2SSean Farley (void) unsetenv(ss); 3025b78c6c2SSean Farley ckfree(ss); 3031632bf1aSJilles Tjoelker INTON; 3045b78c6c2SSean Farley 3055b78c6c2SSean Farley return; 3065b78c6c2SSean Farley } 3075b78c6c2SSean Farley 3085b78c6c2SSean Farley 3094b88c807SRodney W. Grimes /* 3104b88c807SRodney W. Grimes * Same as setvar except that the variable and value are passed in 3114b88c807SRodney W. Grimes * the first argument as name=value. Since the first argument will 3124b88c807SRodney W. Grimes * be actually stored in the table, it should not be a string that 3134b88c807SRodney W. Grimes * will go away. 3144b88c807SRodney W. Grimes */ 3154b88c807SRodney W. Grimes 3164b88c807SRodney W. Grimes void 3175134c3f7SWarner Losh setvareq(char *s, int flags) 3184b88c807SRodney W. Grimes { 3194b88c807SRodney W. Grimes struct var *vp, **vpp; 3203a99ed46SJilles Tjoelker int nlen; 3214b88c807SRodney W. Grimes 322b8ec435eSMartin Cracauer if (aflag) 323b8ec435eSMartin Cracauer flags |= VEXPORT; 324c543e1aeSJilles Tjoelker if (forcelocal && !(flags & (VNOSET | VNOLOCAL))) 325c543e1aeSJilles Tjoelker mklocal(s); 3263a99ed46SJilles Tjoelker vp = find_var(s, &vpp, &nlen); 3273a99ed46SJilles Tjoelker if (vp != NULL) { 328*89d4f883SJilles Tjoelker if (vp->flags & VREADONLY) { 329*89d4f883SJilles Tjoelker if ((flags & (VTEXTFIXED|VSTACK)) == 0) 330*89d4f883SJilles Tjoelker ckfree(s); 3313a99ed46SJilles Tjoelker error("%.*s: is read only", vp->name_len, s); 332*89d4f883SJilles Tjoelker } 333850460c0SJilles Tjoelker if (flags & VNOSET) 334850460c0SJilles Tjoelker return; 3354b88c807SRodney W. Grimes INTOFF; 336ab0a2172SSteve Price 337ab0a2172SSteve Price if (vp->func && (flags & VNOFUNC) == 0) 3383a99ed46SJilles Tjoelker (*vp->func)(s + vp->name_len + 1); 339ab0a2172SSteve Price 3404b88c807SRodney W. Grimes if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0) 3414b88c807SRodney W. Grimes ckfree(vp->text); 342ab0a2172SSteve Price 3434b88c807SRodney W. Grimes vp->flags &= ~(VTEXTFIXED|VSTACK|VUNSET); 3444b88c807SRodney W. Grimes vp->flags |= flags; 3454b88c807SRodney W. Grimes vp->text = s; 346ab0a2172SSteve Price 347ab0a2172SSteve Price /* 348ab0a2172SSteve Price * We could roll this to a function, to handle it as 349ab0a2172SSteve Price * a regular variable function callback, but why bother? 35057063576SJilles Tjoelker * 35157063576SJilles Tjoelker * Note: this assumes iflag is not set to 1 initially. 35259e0cc8eSJilles Tjoelker * As part of initvar(), this is called before arguments 35357063576SJilles Tjoelker * are looked at. 354ab0a2172SSteve Price */ 35557063576SJilles Tjoelker if ((vp == &vmpath || (vp == &vmail && ! mpathset())) && 35657063576SJilles Tjoelker iflag == 1) 3574b88c807SRodney W. Grimes chkmail(1); 358ba726b8aSAndrey A. Chernov if ((vp->flags & VEXPORT) && localevar(s)) { 3595b78c6c2SSean Farley change_env(s, 1); 360ba726b8aSAndrey A. Chernov (void) setlocale(LC_ALL, ""); 3616ed74a0aSJilles Tjoelker updatecharset(); 362ba726b8aSAndrey A. Chernov } 3634b88c807SRodney W. Grimes INTON; 3644b88c807SRodney W. Grimes return; 3654b88c807SRodney W. Grimes } 3664b88c807SRodney W. Grimes /* not found */ 367850460c0SJilles Tjoelker if (flags & VNOSET) 368850460c0SJilles Tjoelker return; 3691632bf1aSJilles Tjoelker INTOFF; 3704b88c807SRodney W. Grimes vp = ckmalloc(sizeof (*vp)); 3714b88c807SRodney W. Grimes vp->flags = flags; 3724b88c807SRodney W. Grimes vp->text = s; 3733a99ed46SJilles Tjoelker vp->name_len = nlen; 3744b88c807SRodney W. Grimes vp->next = *vpp; 375ab0a2172SSteve Price vp->func = NULL; 3764b88c807SRodney W. Grimes *vpp = vp; 377ba726b8aSAndrey A. Chernov if ((vp->flags & VEXPORT) && localevar(s)) { 3785b78c6c2SSean Farley change_env(s, 1); 379ba726b8aSAndrey A. Chernov (void) setlocale(LC_ALL, ""); 3806ed74a0aSJilles Tjoelker updatecharset(); 381ba726b8aSAndrey A. Chernov } 382ba726b8aSAndrey A. Chernov INTON; 3834b88c807SRodney W. Grimes } 3844b88c807SRodney W. Grimes 3854b88c807SRodney W. Grimes 3864b88c807SRodney W. Grimes 3874b88c807SRodney W. Grimes /* 3884b88c807SRodney W. Grimes * Process a linked list of variable assignments. 3894b88c807SRodney W. Grimes */ 3904b88c807SRodney W. Grimes 3914b88c807SRodney W. Grimes void 392850460c0SJilles Tjoelker listsetvar(struct strlist *list, int flags) 3934b88c807SRodney W. Grimes { 3944b88c807SRodney W. Grimes struct strlist *lp; 3954b88c807SRodney W. Grimes 3964b88c807SRodney W. Grimes INTOFF; 3974b88c807SRodney W. Grimes for (lp = list ; lp ; lp = lp->next) { 398850460c0SJilles Tjoelker setvareq(savestr(lp->text), flags); 3994b88c807SRodney W. Grimes } 4004b88c807SRodney W. Grimes INTON; 4014b88c807SRodney W. Grimes } 4024b88c807SRodney W. Grimes 4034b88c807SRodney W. Grimes 4044b88c807SRodney W. Grimes 4054b88c807SRodney W. Grimes /* 4064b88c807SRodney W. Grimes * Find the value of a variable. Returns NULL if not set. 4074b88c807SRodney W. Grimes */ 4084b88c807SRodney W. Grimes 4094b88c807SRodney W. Grimes char * 4102cac6e36SJilles Tjoelker lookupvar(const char *name) 4114b88c807SRodney W. Grimes { 4124b88c807SRodney W. Grimes struct var *v; 4134b88c807SRodney W. Grimes 4143a99ed46SJilles Tjoelker v = find_var(name, NULL, NULL); 4153a99ed46SJilles Tjoelker if (v == NULL || v->flags & VUNSET) 4164b88c807SRodney W. Grimes return NULL; 4173a99ed46SJilles Tjoelker return v->text + v->name_len + 1; 4184b88c807SRodney W. Grimes } 4194b88c807SRodney W. Grimes 4204b88c807SRodney W. Grimes 4214b88c807SRodney W. Grimes 4224b88c807SRodney W. Grimes /* 4234b88c807SRodney W. Grimes * Search the environment of a builtin command. If the second argument 4244b88c807SRodney W. Grimes * is nonzero, return the value of a variable even if it hasn't been 4254b88c807SRodney W. Grimes * exported. 4264b88c807SRodney W. Grimes */ 4274b88c807SRodney W. Grimes 4284b88c807SRodney W. Grimes char * 4292cac6e36SJilles Tjoelker bltinlookup(const char *name, int doall) 4304b88c807SRodney W. Grimes { 4314b88c807SRodney W. Grimes struct strlist *sp; 4324b88c807SRodney W. Grimes struct var *v; 433011d162dSJilles Tjoelker char *result; 4344b88c807SRodney W. Grimes 435011d162dSJilles Tjoelker result = NULL; 4364b88c807SRodney W. Grimes for (sp = cmdenviron ; sp ; sp = sp->next) { 4374b88c807SRodney W. Grimes if (varequal(sp->text, name)) 438011d162dSJilles Tjoelker result = strchr(sp->text, '=') + 1; 4394b88c807SRodney W. Grimes } 440011d162dSJilles Tjoelker if (result != NULL) 441011d162dSJilles Tjoelker return result; 4423a99ed46SJilles Tjoelker 4433a99ed46SJilles Tjoelker v = find_var(name, NULL, NULL); 4443a99ed46SJilles Tjoelker if (v == NULL || v->flags & VUNSET || 4453a99ed46SJilles Tjoelker (!doall && (v->flags & VEXPORT) == 0)) 4464b88c807SRodney W. Grimes return NULL; 4473a99ed46SJilles Tjoelker return v->text + v->name_len + 1; 4484b88c807SRodney W. Grimes } 4494b88c807SRodney W. Grimes 4504b88c807SRodney W. Grimes 451e4b50334SJilles Tjoelker /* 452e4b50334SJilles Tjoelker * Set up locale for a builtin (LANG/LC_* assignments). 453e4b50334SJilles Tjoelker */ 454e4b50334SJilles Tjoelker void 455e4b50334SJilles Tjoelker bltinsetlocale(void) 456e4b50334SJilles Tjoelker { 457e4b50334SJilles Tjoelker struct strlist *lp; 458e4b50334SJilles Tjoelker int act = 0; 459e4b50334SJilles Tjoelker char *loc, *locdef; 460e4b50334SJilles Tjoelker int i; 461e4b50334SJilles Tjoelker 462e4b50334SJilles Tjoelker for (lp = cmdenviron ; lp ; lp = lp->next) { 463e4b50334SJilles Tjoelker if (localevar(lp->text)) { 464e4b50334SJilles Tjoelker act = 1; 465e4b50334SJilles Tjoelker break; 466e4b50334SJilles Tjoelker } 467e4b50334SJilles Tjoelker } 468e4b50334SJilles Tjoelker if (!act) 469e4b50334SJilles Tjoelker return; 470e4b50334SJilles Tjoelker loc = bltinlookup("LC_ALL", 0); 471e4b50334SJilles Tjoelker INTOFF; 472e4b50334SJilles Tjoelker if (loc != NULL) { 473e4b50334SJilles Tjoelker setlocale(LC_ALL, loc); 474e4b50334SJilles Tjoelker INTON; 4756ed74a0aSJilles Tjoelker updatecharset(); 476e4b50334SJilles Tjoelker return; 477e4b50334SJilles Tjoelker } 478e4b50334SJilles Tjoelker locdef = bltinlookup("LANG", 0); 479e4b50334SJilles Tjoelker for (i = 0; locale_names[i] != NULL; i++) { 480e4b50334SJilles Tjoelker loc = bltinlookup(locale_names[i], 0); 481e4b50334SJilles Tjoelker if (loc == NULL) 482e4b50334SJilles Tjoelker loc = locdef; 483e4b50334SJilles Tjoelker if (loc != NULL) 484e4b50334SJilles Tjoelker setlocale(locale_categories[i], loc); 485e4b50334SJilles Tjoelker } 486e4b50334SJilles Tjoelker INTON; 4876ed74a0aSJilles Tjoelker updatecharset(); 488e4b50334SJilles Tjoelker } 489e4b50334SJilles Tjoelker 490e4b50334SJilles Tjoelker /* 491e4b50334SJilles Tjoelker * Undo the effect of bltinlocaleset(). 492e4b50334SJilles Tjoelker */ 493e4b50334SJilles Tjoelker void 494e4b50334SJilles Tjoelker bltinunsetlocale(void) 495e4b50334SJilles Tjoelker { 496e4b50334SJilles Tjoelker struct strlist *lp; 497e4b50334SJilles Tjoelker 498e4b50334SJilles Tjoelker INTOFF; 499e4b50334SJilles Tjoelker for (lp = cmdenviron ; lp ; lp = lp->next) { 500e4b50334SJilles Tjoelker if (localevar(lp->text)) { 501e4b50334SJilles Tjoelker setlocale(LC_ALL, ""); 5026ed74a0aSJilles Tjoelker updatecharset(); 503e4b50334SJilles Tjoelker return; 504e4b50334SJilles Tjoelker } 505e4b50334SJilles Tjoelker } 506e4b50334SJilles Tjoelker INTON; 507e4b50334SJilles Tjoelker } 508e4b50334SJilles Tjoelker 5096ed74a0aSJilles Tjoelker /* 5106ed74a0aSJilles Tjoelker * Update the localeisutf8 flag. 5116ed74a0aSJilles Tjoelker */ 5126ed74a0aSJilles Tjoelker void 5136ed74a0aSJilles Tjoelker updatecharset(void) 5146ed74a0aSJilles Tjoelker { 5156ed74a0aSJilles Tjoelker char *charset; 5166ed74a0aSJilles Tjoelker 5176ed74a0aSJilles Tjoelker charset = nl_langinfo(CODESET); 5186ed74a0aSJilles Tjoelker localeisutf8 = !strcmp(charset, "UTF-8"); 5196ed74a0aSJilles Tjoelker } 5204b88c807SRodney W. Grimes 52107eb7033SJilles Tjoelker void 52207eb7033SJilles Tjoelker initcharset(void) 52307eb7033SJilles Tjoelker { 52407eb7033SJilles Tjoelker updatecharset(); 52507eb7033SJilles Tjoelker initial_localeisutf8 = localeisutf8; 52607eb7033SJilles Tjoelker } 52707eb7033SJilles Tjoelker 5284b88c807SRodney W. Grimes /* 5294b88c807SRodney W. Grimes * Generate a list of exported variables. This routine is used to construct 5304b88c807SRodney W. Grimes * the third argument to execve when executing a program. 5314b88c807SRodney W. Grimes */ 5324b88c807SRodney W. Grimes 5334b88c807SRodney W. Grimes char ** 5345134c3f7SWarner Losh environment(void) 5355134c3f7SWarner Losh { 5364b88c807SRodney W. Grimes int nenv; 5374b88c807SRodney W. Grimes struct var **vpp; 5384b88c807SRodney W. Grimes struct var *vp; 5394b88c807SRodney W. Grimes char **env, **ep; 5404b88c807SRodney W. Grimes 5414b88c807SRodney W. Grimes nenv = 0; 5424b88c807SRodney W. Grimes for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) { 5434b88c807SRodney W. Grimes for (vp = *vpp ; vp ; vp = vp->next) 5444b88c807SRodney W. Grimes if (vp->flags & VEXPORT) 5454b88c807SRodney W. Grimes nenv++; 5464b88c807SRodney W. Grimes } 5474b88c807SRodney W. Grimes ep = env = stalloc((nenv + 1) * sizeof *env); 5484b88c807SRodney W. Grimes for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) { 5494b88c807SRodney W. Grimes for (vp = *vpp ; vp ; vp = vp->next) 5504b88c807SRodney W. Grimes if (vp->flags & VEXPORT) 5514b88c807SRodney W. Grimes *ep++ = vp->text; 5524b88c807SRodney W. Grimes } 5534b88c807SRodney W. Grimes *ep = NULL; 5544b88c807SRodney W. Grimes return env; 5554b88c807SRodney W. Grimes } 5564b88c807SRodney W. Grimes 5574b88c807SRodney W. Grimes 55888328642SDavid E. O'Brien static int 559692f35fdSStefan Farfeleder var_compare(const void *a, const void *b) 560692f35fdSStefan Farfeleder { 561692f35fdSStefan Farfeleder const char *const *sa, *const *sb; 562692f35fdSStefan Farfeleder 563692f35fdSStefan Farfeleder sa = a; 564692f35fdSStefan Farfeleder sb = b; 565692f35fdSStefan Farfeleder /* 566692f35fdSStefan Farfeleder * This compares two var=value strings which creates a different 567692f35fdSStefan Farfeleder * order from what you would probably expect. POSIX is somewhat 568692f35fdSStefan Farfeleder * ambiguous on what should be sorted exactly. 569692f35fdSStefan Farfeleder */ 570692f35fdSStefan Farfeleder return strcoll(*sa, *sb); 571692f35fdSStefan Farfeleder } 572692f35fdSStefan Farfeleder 5734b88c807SRodney W. Grimes 5744b88c807SRodney W. Grimes /* 575cff1d849SJilles Tjoelker * Command to list all variables which are set. This is invoked from the 576cff1d849SJilles Tjoelker * set command when it is called without any options or operands. 5774b88c807SRodney W. Grimes */ 5784b88c807SRodney W. Grimes 5794b88c807SRodney W. Grimes int 5805134c3f7SWarner Losh showvarscmd(int argc __unused, char **argv __unused) 581aa9caaf6SPeter Wemm { 5824b88c807SRodney W. Grimes struct var **vpp; 5834b88c807SRodney W. Grimes struct var *vp; 58459258844STim J. Robbins const char *s; 585692f35fdSStefan Farfeleder const char **vars; 586692f35fdSStefan Farfeleder int i, n; 5874b88c807SRodney W. Grimes 588692f35fdSStefan Farfeleder /* 589692f35fdSStefan Farfeleder * POSIX requires us to sort the variables. 590692f35fdSStefan Farfeleder */ 591692f35fdSStefan Farfeleder n = 0; 5924b88c807SRodney W. Grimes for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) { 5934b88c807SRodney W. Grimes for (vp = *vpp; vp; vp = vp->next) { 594692f35fdSStefan Farfeleder if (!(vp->flags & VUNSET)) 595692f35fdSStefan Farfeleder n++; 596692f35fdSStefan Farfeleder } 597692f35fdSStefan Farfeleder } 598692f35fdSStefan Farfeleder 59933233ec7SJilles Tjoelker INTOFF; 600692f35fdSStefan Farfeleder vars = ckmalloc(n * sizeof(*vars)); 601692f35fdSStefan Farfeleder i = 0; 602692f35fdSStefan Farfeleder for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) { 603692f35fdSStefan Farfeleder for (vp = *vpp; vp; vp = vp->next) { 604692f35fdSStefan Farfeleder if (!(vp->flags & VUNSET)) 605692f35fdSStefan Farfeleder vars[i++] = vp->text; 606692f35fdSStefan Farfeleder } 607692f35fdSStefan Farfeleder } 608692f35fdSStefan Farfeleder 609692f35fdSStefan Farfeleder qsort(vars, n, sizeof(*vars), var_compare); 610692f35fdSStefan Farfeleder for (i = 0; i < n; i++) { 611f5f215e2SJilles Tjoelker /* 612f5f215e2SJilles Tjoelker * Skip improper variable names so the output remains usable as 613f5f215e2SJilles Tjoelker * shell input. 614f5f215e2SJilles Tjoelker */ 615f5f215e2SJilles Tjoelker if (!isassignment(vars[i])) 616f5f215e2SJilles Tjoelker continue; 617aeb5d065SJilles Tjoelker s = strchr(vars[i], '='); 618aeb5d065SJilles Tjoelker s++; 619aeb5d065SJilles Tjoelker outbin(vars[i], s - vars[i], out1); 620aeb5d065SJilles Tjoelker out1qstr(s); 62159258844STim J. Robbins out1c('\n'); 6224b88c807SRodney W. Grimes } 623692f35fdSStefan Farfeleder ckfree(vars); 62433233ec7SJilles Tjoelker INTON; 625692f35fdSStefan Farfeleder 6264b88c807SRodney W. Grimes return 0; 6274b88c807SRodney W. Grimes } 6284b88c807SRodney W. Grimes 6294b88c807SRodney W. Grimes 6304b88c807SRodney W. Grimes 6314b88c807SRodney W. Grimes /* 6324b88c807SRodney W. Grimes * The export and readonly commands. 6334b88c807SRodney W. Grimes */ 6344b88c807SRodney W. Grimes 6354b88c807SRodney W. Grimes int 6367cbda738SJilles Tjoelker exportcmd(int argc __unused, char **argv) 637aa9caaf6SPeter Wemm { 6384b88c807SRodney W. Grimes struct var **vpp; 6394b88c807SRodney W. Grimes struct var *vp; 6407cbda738SJilles Tjoelker char **ap; 6414b88c807SRodney W. Grimes char *name; 6424b88c807SRodney W. Grimes char *p; 64345086f8cSTim J. Robbins char *cmdname; 64445086f8cSTim J. Robbins int ch, values; 6454b88c807SRodney W. Grimes int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT; 6464b88c807SRodney W. Grimes 64745086f8cSTim J. Robbins cmdname = argv[0]; 64845086f8cSTim J. Robbins values = 0; 6497cbda738SJilles Tjoelker while ((ch = nextopt("p")) != '\0') { 65045086f8cSTim J. Robbins switch (ch) { 65145086f8cSTim J. Robbins case 'p': 65245086f8cSTim J. Robbins values = 1; 65345086f8cSTim J. Robbins break; 65445086f8cSTim J. Robbins } 65545086f8cSTim J. Robbins } 65645086f8cSTim J. Robbins 6577cbda738SJilles Tjoelker if (values && *argptr != NULL) 6583fda45ecSStefan Farfeleder error("-p requires no arguments"); 6597cbda738SJilles Tjoelker if (*argptr != NULL) { 6607cbda738SJilles Tjoelker for (ap = argptr; (name = *ap) != NULL; ap++) { 6614b88c807SRodney W. Grimes if ((p = strchr(name, '=')) != NULL) { 6624b88c807SRodney W. Grimes p++; 6634b88c807SRodney W. Grimes } else { 6643a99ed46SJilles Tjoelker vp = find_var(name, NULL, NULL); 6653a99ed46SJilles Tjoelker if (vp != NULL) { 6664b88c807SRodney W. Grimes vp->flags |= flag; 667ba726b8aSAndrey A. Chernov if ((vp->flags & VEXPORT) && localevar(vp->text)) { 6685b78c6c2SSean Farley change_env(vp->text, 1); 669ba726b8aSAndrey A. Chernov (void) setlocale(LC_ALL, ""); 6706ed74a0aSJilles Tjoelker updatecharset(); 671ba726b8aSAndrey A. Chernov } 6723a99ed46SJilles Tjoelker continue; 6734b88c807SRodney W. Grimes } 6744b88c807SRodney W. Grimes } 6754b88c807SRodney W. Grimes setvar(name, p, flag); 6764b88c807SRodney W. Grimes } 6774b88c807SRodney W. Grimes } else { 6784b88c807SRodney W. Grimes for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) { 6794b88c807SRodney W. Grimes for (vp = *vpp ; vp ; vp = vp->next) { 6804b88c807SRodney W. Grimes if (vp->flags & flag) { 68145086f8cSTim J. Robbins if (values) { 682f5f215e2SJilles Tjoelker /* 683f5f215e2SJilles Tjoelker * Skip improper variable names 684f5f215e2SJilles Tjoelker * so the output remains usable 685f5f215e2SJilles Tjoelker * as shell input. 686f5f215e2SJilles Tjoelker */ 687f5f215e2SJilles Tjoelker if (!isassignment(vp->text)) 688f5f215e2SJilles Tjoelker continue; 68945086f8cSTim J. Robbins out1str(cmdname); 69045086f8cSTim J. Robbins out1c(' '); 69145086f8cSTim J. Robbins } 69245086f8cSTim J. Robbins if (values && !(vp->flags & VUNSET)) { 693258ef734SJilles Tjoelker outbin(vp->text, 694258ef734SJilles Tjoelker vp->name_len + 1, out1); 695258ef734SJilles Tjoelker out1qstr(vp->text + 696258ef734SJilles Tjoelker vp->name_len + 1); 697aeb5d065SJilles Tjoelker } else 698258ef734SJilles Tjoelker outbin(vp->text, vp->name_len, 699aeb5d065SJilles Tjoelker out1); 7004b88c807SRodney W. Grimes out1c('\n'); 7014b88c807SRodney W. Grimes } 7024b88c807SRodney W. Grimes } 7034b88c807SRodney W. Grimes } 7044b88c807SRodney W. Grimes } 7054b88c807SRodney W. Grimes return 0; 7064b88c807SRodney W. Grimes } 7074b88c807SRodney W. Grimes 7084b88c807SRodney W. Grimes 7094b88c807SRodney W. Grimes /* 7104b88c807SRodney W. Grimes * The "local" command. 7114b88c807SRodney W. Grimes */ 7124b88c807SRodney W. Grimes 713aa9caaf6SPeter Wemm int 7145134c3f7SWarner Losh localcmd(int argc __unused, char **argv __unused) 715aa9caaf6SPeter Wemm { 7164b88c807SRodney W. Grimes char *name; 7174b88c807SRodney W. Grimes 718056fd329SJilles Tjoelker nextopt(""); 7194b88c807SRodney W. Grimes if (! in_function()) 7204b88c807SRodney W. Grimes error("Not in a function"); 7214b88c807SRodney W. Grimes while ((name = *argptr++) != NULL) { 7224b88c807SRodney W. Grimes mklocal(name); 7234b88c807SRodney W. Grimes } 7244b88c807SRodney W. Grimes return 0; 7254b88c807SRodney W. Grimes } 7264b88c807SRodney W. Grimes 7274b88c807SRodney W. Grimes 7284b88c807SRodney W. Grimes /* 7294b88c807SRodney W. Grimes * Make a variable a local variable. When a variable is made local, it's 7304b88c807SRodney W. Grimes * value and flags are saved in a localvar structure. The saved values 7314b88c807SRodney W. Grimes * will be restored when the shell function returns. We handle the name 7324b88c807SRodney W. Grimes * "-" as a special case. 7334b88c807SRodney W. Grimes */ 7344b88c807SRodney W. Grimes 7354b88c807SRodney W. Grimes void 7365134c3f7SWarner Losh mklocal(char *name) 7374b88c807SRodney W. Grimes { 7384b88c807SRodney W. Grimes struct localvar *lvp; 7394b88c807SRodney W. Grimes struct var **vpp; 7404b88c807SRodney W. Grimes struct var *vp; 7414b88c807SRodney W. Grimes 7424b88c807SRodney W. Grimes INTOFF; 7434b88c807SRodney W. Grimes lvp = ckmalloc(sizeof (struct localvar)); 7444b88c807SRodney W. Grimes if (name[0] == '-' && name[1] == '\0') { 7454b88c807SRodney W. Grimes lvp->text = ckmalloc(sizeof optlist); 746aa9caaf6SPeter Wemm memcpy(lvp->text, optlist, sizeof optlist); 7474b88c807SRodney W. Grimes vp = NULL; 7484b88c807SRodney W. Grimes } else { 7493a99ed46SJilles Tjoelker vp = find_var(name, &vpp, NULL); 7504b88c807SRodney W. Grimes if (vp == NULL) { 7514b88c807SRodney W. Grimes if (strchr(name, '=')) 752c543e1aeSJilles Tjoelker setvareq(savestr(name), VSTRFIXED | VNOLOCAL); 7534b88c807SRodney W. Grimes else 754c543e1aeSJilles Tjoelker setvar(name, NULL, VSTRFIXED | VNOLOCAL); 7554b88c807SRodney W. Grimes vp = *vpp; /* the new variable */ 7564b88c807SRodney W. Grimes lvp->text = NULL; 7574b88c807SRodney W. Grimes lvp->flags = VUNSET; 7584b88c807SRodney W. Grimes } else { 7594b88c807SRodney W. Grimes lvp->text = vp->text; 7604b88c807SRodney W. Grimes lvp->flags = vp->flags; 7614b88c807SRodney W. Grimes vp->flags |= VSTRFIXED|VTEXTFIXED; 7623a99ed46SJilles Tjoelker if (name[vp->name_len] == '=') 763c543e1aeSJilles Tjoelker setvareq(savestr(name), VNOLOCAL); 7644b88c807SRodney W. Grimes } 7654b88c807SRodney W. Grimes } 7664b88c807SRodney W. Grimes lvp->vp = vp; 7674b88c807SRodney W. Grimes lvp->next = localvars; 7684b88c807SRodney W. Grimes localvars = lvp; 7694b88c807SRodney W. Grimes INTON; 7704b88c807SRodney W. Grimes } 7714b88c807SRodney W. Grimes 7724b88c807SRodney W. Grimes 7734b88c807SRodney W. Grimes /* 7744b88c807SRodney W. Grimes * Called after a function returns. 7754b88c807SRodney W. Grimes */ 7764b88c807SRodney W. Grimes 7774b88c807SRodney W. Grimes void 7785134c3f7SWarner Losh poplocalvars(void) 7795134c3f7SWarner Losh { 7804b88c807SRodney W. Grimes struct localvar *lvp; 7814b88c807SRodney W. Grimes struct var *vp; 7824b88c807SRodney W. Grimes 7831632bf1aSJilles Tjoelker INTOFF; 7844b88c807SRodney W. Grimes while ((lvp = localvars) != NULL) { 7854b88c807SRodney W. Grimes localvars = lvp->next; 7864b88c807SRodney W. Grimes vp = lvp->vp; 7874b88c807SRodney W. Grimes if (vp == NULL) { /* $- saved */ 788aa9caaf6SPeter Wemm memcpy(optlist, lvp->text, sizeof optlist); 7894b88c807SRodney W. Grimes ckfree(lvp->text); 7904a7b1013SJilles Tjoelker optschanged(); 7914b88c807SRodney W. Grimes } else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) { 7924b88c807SRodney W. Grimes (void)unsetvar(vp->text); 7934b88c807SRodney W. Grimes } else { 7944b88c807SRodney W. Grimes if ((vp->flags & VTEXTFIXED) == 0) 7954b88c807SRodney W. Grimes ckfree(vp->text); 7964b88c807SRodney W. Grimes vp->flags = lvp->flags; 7974b88c807SRodney W. Grimes vp->text = lvp->text; 7984b88c807SRodney W. Grimes } 7994b88c807SRodney W. Grimes ckfree(lvp); 8004b88c807SRodney W. Grimes } 8011632bf1aSJilles Tjoelker INTON; 8024b88c807SRodney W. Grimes } 8034b88c807SRodney W. Grimes 8044b88c807SRodney W. Grimes 805aa9caaf6SPeter Wemm int 8065134c3f7SWarner Losh setvarcmd(int argc, char **argv) 807aa9caaf6SPeter Wemm { 8084b88c807SRodney W. Grimes if (argc <= 2) 8094b88c807SRodney W. Grimes return unsetcmd(argc, argv); 8104b88c807SRodney W. Grimes else if (argc == 3) 8114b88c807SRodney W. Grimes setvar(argv[1], argv[2], 0); 8124b88c807SRodney W. Grimes else 813274110dfSJilles Tjoelker error("too many arguments"); 8144b88c807SRodney W. Grimes return 0; 8154b88c807SRodney W. Grimes } 8164b88c807SRodney W. Grimes 8174b88c807SRodney W. Grimes 8184b88c807SRodney W. Grimes /* 819cff1d849SJilles Tjoelker * The unset builtin command. 8204b88c807SRodney W. Grimes */ 8214b88c807SRodney W. Grimes 822aa9caaf6SPeter Wemm int 8235134c3f7SWarner Losh unsetcmd(int argc __unused, char **argv __unused) 824aa9caaf6SPeter Wemm { 8254b88c807SRodney W. Grimes char **ap; 8264b88c807SRodney W. Grimes int i; 8274b88c807SRodney W. Grimes int flg_func = 0; 8284b88c807SRodney W. Grimes int flg_var = 0; 8294b88c807SRodney W. Grimes int ret = 0; 8304b88c807SRodney W. Grimes 8314b88c807SRodney W. Grimes while ((i = nextopt("vf")) != '\0') { 8324b88c807SRodney W. Grimes if (i == 'f') 8334b88c807SRodney W. Grimes flg_func = 1; 8344b88c807SRodney W. Grimes else 8354b88c807SRodney W. Grimes flg_var = 1; 8364b88c807SRodney W. Grimes } 8374b88c807SRodney W. Grimes if (flg_func == 0 && flg_var == 0) 8384b88c807SRodney W. Grimes flg_var = 1; 8394b88c807SRodney W. Grimes 8401632bf1aSJilles Tjoelker INTOFF; 8414b88c807SRodney W. Grimes for (ap = argptr; *ap ; ap++) { 8424b88c807SRodney W. Grimes if (flg_func) 8434b88c807SRodney W. Grimes ret |= unsetfunc(*ap); 8444b88c807SRodney W. Grimes if (flg_var) 8454b88c807SRodney W. Grimes ret |= unsetvar(*ap); 8464b88c807SRodney W. Grimes } 8471632bf1aSJilles Tjoelker INTON; 8484b88c807SRodney W. Grimes return ret; 8494b88c807SRodney W. Grimes } 8504b88c807SRodney W. Grimes 8514b88c807SRodney W. Grimes 8524b88c807SRodney W. Grimes /* 8534b88c807SRodney W. Grimes * Unset the specified variable. 8541632bf1aSJilles Tjoelker * Called with interrupts off. 8554b88c807SRodney W. Grimes */ 8564b88c807SRodney W. Grimes 857ab0a2172SSteve Price int 8582cac6e36SJilles Tjoelker unsetvar(const char *s) 8594b88c807SRodney W. Grimes { 8604b88c807SRodney W. Grimes struct var **vpp; 8614b88c807SRodney W. Grimes struct var *vp; 8624b88c807SRodney W. Grimes 8633a99ed46SJilles Tjoelker vp = find_var(s, &vpp, NULL); 8643a99ed46SJilles Tjoelker if (vp == NULL) 8653a99ed46SJilles Tjoelker return (0); 8664b88c807SRodney W. Grimes if (vp->flags & VREADONLY) 8674b88c807SRodney W. Grimes return (1); 8683a99ed46SJilles Tjoelker if (vp->text[vp->name_len + 1] != '\0') 8694b88c807SRodney W. Grimes setvar(s, nullstr, 0); 870ba726b8aSAndrey A. Chernov if ((vp->flags & VEXPORT) && localevar(vp->text)) { 8715b78c6c2SSean Farley change_env(s, 0); 872ba726b8aSAndrey A. Chernov setlocale(LC_ALL, ""); 8736ed74a0aSJilles Tjoelker updatecharset(); 874ba726b8aSAndrey A. Chernov } 8754b88c807SRodney W. Grimes vp->flags &= ~VEXPORT; 8764b88c807SRodney W. Grimes vp->flags |= VUNSET; 8774b88c807SRodney W. Grimes if ((vp->flags & VSTRFIXED) == 0) { 8784b88c807SRodney W. Grimes if ((vp->flags & VTEXTFIXED) == 0) 8794b88c807SRodney W. Grimes ckfree(vp->text); 8804b88c807SRodney W. Grimes *vpp = vp->next; 8814b88c807SRodney W. Grimes ckfree(vp); 8824b88c807SRodney W. Grimes } 8834b88c807SRodney W. Grimes return (0); 8844b88c807SRodney W. Grimes } 8854b88c807SRodney W. Grimes 8864b88c807SRodney W. Grimes 8874b88c807SRodney W. Grimes 8884b88c807SRodney W. Grimes /* 8896c7d8328SEitan Adler * Returns true if the two strings specify the same variable. The first 8904b88c807SRodney W. Grimes * variable name is terminated by '='; the second may be terminated by 8914b88c807SRodney W. Grimes * either '=' or '\0'. 8924b88c807SRodney W. Grimes */ 8934b88c807SRodney W. Grimes 89488328642SDavid E. O'Brien static int 8952cac6e36SJilles Tjoelker varequal(const char *p, const char *q) 8964b88c807SRodney W. Grimes { 8974b88c807SRodney W. Grimes while (*p == *q++) { 8984b88c807SRodney W. Grimes if (*p++ == '=') 8994b88c807SRodney W. Grimes return 1; 9004b88c807SRodney W. Grimes } 9014b88c807SRodney W. Grimes if (*p == '=' && *(q - 1) == '\0') 9024b88c807SRodney W. Grimes return 1; 9034b88c807SRodney W. Grimes return 0; 9044b88c807SRodney W. Grimes } 9053a99ed46SJilles Tjoelker 9063a99ed46SJilles Tjoelker /* 9073a99ed46SJilles Tjoelker * Search for a variable. 9083a99ed46SJilles Tjoelker * 'name' may be terminated by '=' or a NUL. 9093a99ed46SJilles Tjoelker * vppp is set to the pointer to vp, or the list head if vp isn't found 9106c7d8328SEitan Adler * lenp is set to the number of characters in 'name' 9113a99ed46SJilles Tjoelker */ 9123a99ed46SJilles Tjoelker 9133a99ed46SJilles Tjoelker static struct var * 9143a99ed46SJilles Tjoelker find_var(const char *name, struct var ***vppp, int *lenp) 9153a99ed46SJilles Tjoelker { 9163a99ed46SJilles Tjoelker unsigned int hashval; 9173a99ed46SJilles Tjoelker int len; 9183a99ed46SJilles Tjoelker struct var *vp, **vpp; 9193a99ed46SJilles Tjoelker const char *p = name; 9203a99ed46SJilles Tjoelker 9213a99ed46SJilles Tjoelker hashval = 0; 9223a99ed46SJilles Tjoelker while (*p && *p != '=') 9233a99ed46SJilles Tjoelker hashval = 2 * hashval + (unsigned char)*p++; 9243a99ed46SJilles Tjoelker len = p - name; 9253a99ed46SJilles Tjoelker 9263a99ed46SJilles Tjoelker if (lenp) 9273a99ed46SJilles Tjoelker *lenp = len; 9283a99ed46SJilles Tjoelker vpp = &vartab[hashval % VTABSIZE]; 9293a99ed46SJilles Tjoelker if (vppp) 9303a99ed46SJilles Tjoelker *vppp = vpp; 9313a99ed46SJilles Tjoelker 9323a99ed46SJilles Tjoelker for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) { 9333a99ed46SJilles Tjoelker if (vp->name_len != len) 9343a99ed46SJilles Tjoelker continue; 9353a99ed46SJilles Tjoelker if (memcmp(vp->text, name, len) != 0) 9363a99ed46SJilles Tjoelker continue; 9373a99ed46SJilles Tjoelker if (vppp) 9383a99ed46SJilles Tjoelker *vppp = vpp; 9393a99ed46SJilles Tjoelker return vp; 9403a99ed46SJilles Tjoelker } 9413a99ed46SJilles Tjoelker return NULL; 9423a99ed46SJilles Tjoelker } 943