var.c revision 207678
11556Srgrimes/*- 21556Srgrimes * Copyright (c) 1991, 1993 31556Srgrimes * The Regents of the University of California. All rights reserved. 41556Srgrimes * 51556Srgrimes * This code is derived from software contributed to Berkeley by 61556Srgrimes * Kenneth Almquist. 71556Srgrimes * 81556Srgrimes * Redistribution and use in source and binary forms, with or without 91556Srgrimes * modification, are permitted provided that the following conditions 101556Srgrimes * are met: 111556Srgrimes * 1. Redistributions of source code must retain the above copyright 121556Srgrimes * notice, this list of conditions and the following disclaimer. 131556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 141556Srgrimes * notice, this list of conditions and the following disclaimer in the 151556Srgrimes * documentation and/or other materials provided with the distribution. 161556Srgrimes * 4. Neither the name of the University nor the names of its contributors 171556Srgrimes * may be used to endorse or promote products derived from this software 181556Srgrimes * without specific prior written permission. 191556Srgrimes * 201556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 211556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 221556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 231556Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 241556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 251556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 261556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 271556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 281556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 291556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 301556Srgrimes * SUCH DAMAGE. 311556Srgrimes */ 321556Srgrimes 331556Srgrimes#ifndef lint 3436150Scharnier#if 0 3536150Scharnierstatic char sccsid[] = "@(#)var.c 8.3 (Berkeley) 5/4/95"; 3636150Scharnier#endif 371556Srgrimes#endif /* not lint */ 3899110Sobrien#include <sys/cdefs.h> 3999110Sobrien__FBSDID("$FreeBSD: head/bin/sh/var.c 207678 2010-05-05 21:48:40Z jilles $"); 401556Srgrimes 4117987Speter#include <unistd.h> 4217987Speter#include <stdlib.h> 43114763Sobrien#include <paths.h> 4417987Speter 451556Srgrimes/* 461556Srgrimes * Shell variables. 471556Srgrimes */ 481556Srgrimes 4917525Sache#include <locale.h> 5017525Sache 511556Srgrimes#include "shell.h" 521556Srgrimes#include "output.h" 531556Srgrimes#include "expand.h" 541556Srgrimes#include "nodes.h" /* for other headers */ 551556Srgrimes#include "eval.h" /* defines cmdenviron */ 561556Srgrimes#include "exec.h" 571556Srgrimes#include "syntax.h" 581556Srgrimes#include "options.h" 591556Srgrimes#include "mail.h" 601556Srgrimes#include "var.h" 611556Srgrimes#include "memalloc.h" 621556Srgrimes#include "error.h" 631556Srgrimes#include "mystring.h" 6420425Ssteve#include "parser.h" 6517987Speter#ifndef NO_HISTORY 6617987Speter#include "myhistedit.h" 6717987Speter#endif 681556Srgrimes 691556Srgrimes 701556Srgrimes#define VTABSIZE 39 711556Srgrimes 721556Srgrimes 731556Srgrimesstruct varinit { 741556Srgrimes struct var *var; 751556Srgrimes int flags; 76201056Sjilles const char *text; 7790111Simp void (*func)(const char *); 781556Srgrimes}; 791556Srgrimes 801556Srgrimes 8117987Speter#ifndef NO_HISTORY 821556Srgrimesstruct var vhistsize; 8317987Speter#endif 841556Srgrimesstruct var vifs; 851556Srgrimesstruct var vmail; 861556Srgrimesstruct var vmpath; 871556Srgrimesstruct var vpath; 8897689Stjrstruct var vppid; 891556Srgrimesstruct var vps1; 901556Srgrimesstruct var vps2; 91159632Sstefanfstruct var vps4; 921556Srgrimesstruct var vvers; 93117261SddsSTATIC struct var voptind; 941556Srgrimes 95117261SddsSTATIC const struct varinit varinit[] = { 9617987Speter#ifndef NO_HISTORY 97201056Sjilles { &vhistsize, VUNSET, "HISTSIZE=", 9820425Ssteve sethistsize }, 9917987Speter#endif 100201056Sjilles { &vifs, 0, "IFS= \t\n", 10120425Ssteve NULL }, 102201056Sjilles { &vmail, VUNSET, "MAIL=", 10320425Ssteve NULL }, 104201056Sjilles { &vmpath, VUNSET, "MAILPATH=", 10520425Ssteve NULL }, 106201056Sjilles { &vpath, 0, "PATH=" _PATH_DEFPATH, 10720425Ssteve changepath }, 108201056Sjilles { &vppid, VUNSET, "PPID=", 10997689Stjr NULL }, 1108855Srgrimes /* 1111556Srgrimes * vps1 depends on uid 1121556Srgrimes */ 113201056Sjilles { &vps2, 0, "PS2=> ", 11420425Ssteve NULL }, 115201056Sjilles { &vps4, 0, "PS4=+ ", 116159632Sstefanf NULL }, 117201056Sjilles { &voptind, 0, "OPTIND=1", 11820425Ssteve getoptsreset }, 11920425Ssteve { NULL, 0, NULL, 12020425Ssteve NULL } 1211556Srgrimes}; 1221556Srgrimes 123117261SddsSTATIC struct var *vartab[VTABSIZE]; 1241556Srgrimes 125207678SjillesSTATIC const char *const locale_names[7] = { 126207678Sjilles "LC_COLLATE", "LC_CTYPE", "LC_MONETARY", 127207678Sjilles "LC_NUMERIC", "LC_TIME", "LC_MESSAGES", NULL 128207678Sjilles}; 129207678SjillesSTATIC const int locale_categories[7] = { 130207678Sjilles LC_COLLATE, LC_CTYPE, LC_MONETARY, LC_NUMERIC, LC_TIME, LC_MESSAGES, 0 131207678Sjilles}; 132207678Sjilles 133200956SjillesSTATIC struct var **hashvar(const char *); 134200956SjillesSTATIC int varequal(const char *, const char *); 135200956SjillesSTATIC int localevar(const char *); 1361556Srgrimes 1371556Srgrimes/* 138155302Sschweikh * Initialize the variable symbol tables and import the environment. 1391556Srgrimes */ 1401556Srgrimes 1411556Srgrimes#ifdef mkinit 1421556SrgrimesINCLUDE "var.h" 143201053SjillesMKINIT char **environ; 1441556SrgrimesINIT { 1451556Srgrimes char **envp; 1461556Srgrimes 1471556Srgrimes initvar(); 1481556Srgrimes for (envp = environ ; *envp ; envp++) { 1491556Srgrimes if (strchr(*envp, '=')) { 1501556Srgrimes setvareq(*envp, VEXPORT|VTEXTFIXED); 1511556Srgrimes } 1521556Srgrimes } 1531556Srgrimes} 1541556Srgrimes#endif 1551556Srgrimes 1561556Srgrimes 1571556Srgrimes/* 1581556Srgrimes * This routine initializes the builtin variables. It is called when the 1591556Srgrimes * shell is initialized and again when a shell procedure is spawned. 1601556Srgrimes */ 1611556Srgrimes 1621556Srgrimesvoid 16390111Simpinitvar(void) 16490111Simp{ 16597689Stjr char ppid[20]; 1661556Srgrimes const struct varinit *ip; 1671556Srgrimes struct var *vp; 1681556Srgrimes struct var **vpp; 1691556Srgrimes 1701556Srgrimes for (ip = varinit ; (vp = ip->var) != NULL ; ip++) { 1711556Srgrimes if ((vp->flags & VEXPORT) == 0) { 1721556Srgrimes vpp = hashvar(ip->text); 1731556Srgrimes vp->next = *vpp; 1741556Srgrimes *vpp = vp; 175201056Sjilles vp->text = __DECONST(char *, ip->text); 176201056Sjilles vp->flags = ip->flags | VSTRFIXED | VTEXTFIXED; 17720425Ssteve vp->func = ip->func; 1781556Srgrimes } 1791556Srgrimes } 1801556Srgrimes /* 1811556Srgrimes * PS1 depends on uid 1821556Srgrimes */ 1831556Srgrimes if ((vps1.flags & VEXPORT) == 0) { 1841556Srgrimes vpp = hashvar("PS1="); 1851556Srgrimes vps1.next = *vpp; 1861556Srgrimes *vpp = &vps1; 187201056Sjilles vps1.text = __DECONST(char *, geteuid() ? "PS1=$ " : "PS1=# "); 1881556Srgrimes vps1.flags = VSTRFIXED|VTEXTFIXED; 1891556Srgrimes } 19097689Stjr if ((vppid.flags & VEXPORT) == 0) { 19197689Stjr fmtstr(ppid, sizeof(ppid), "%d", (int)getppid()); 19297689Stjr setvarsafe("PPID", ppid, 0); 19397689Stjr } 1941556Srgrimes} 1951556Srgrimes 1961556Srgrimes/* 19720425Ssteve * Safe version of setvar, returns 1 on success 0 on failure. 19820425Ssteve */ 19920425Ssteve 20020425Ssteveint 201200956Sjillessetvarsafe(const char *name, const char *val, int flags) 20220425Ssteve{ 20320425Ssteve struct jmploc jmploc; 204194765Sjilles struct jmploc *const savehandler = handler; 20520425Ssteve int err = 0; 206199660Sjilles int inton; 20720425Ssteve 208199660Sjilles inton = is_int_on(); 20920425Ssteve if (setjmp(jmploc.loc)) 21020425Ssteve err = 1; 21120425Ssteve else { 21220425Ssteve handler = &jmploc; 21320425Ssteve setvar(name, val, flags); 21420425Ssteve } 21520425Ssteve handler = savehandler; 216199660Sjilles SETINTON(inton); 21720425Ssteve return err; 21820425Ssteve} 21920425Ssteve 22020425Ssteve/* 221155302Sschweikh * Set the value of a variable. The flags argument is stored with the 2221556Srgrimes * flags of the variable. If val is NULL, the variable is unset. 2231556Srgrimes */ 2241556Srgrimes 2251556Srgrimesvoid 226200956Sjillessetvar(const char *name, const char *val, int flags) 22717987Speter{ 228200956Sjilles const char *p; 2291556Srgrimes int len; 2301556Srgrimes int namelen; 2311556Srgrimes char *nameeq; 2321556Srgrimes int isbad; 2331556Srgrimes 2341556Srgrimes isbad = 0; 2351556Srgrimes p = name; 23617525Sache if (! is_name(*p)) 2371556Srgrimes isbad = 1; 23817525Sache p++; 2391556Srgrimes for (;;) { 2401556Srgrimes if (! is_in_name(*p)) { 2411556Srgrimes if (*p == '\0' || *p == '=') 2421556Srgrimes break; 2431556Srgrimes isbad = 1; 2441556Srgrimes } 2451556Srgrimes p++; 2461556Srgrimes } 2471556Srgrimes namelen = p - name; 2481556Srgrimes if (isbad) 2491556Srgrimes error("%.*s: bad variable name", namelen, name); 2501556Srgrimes len = namelen + 2; /* 2 is space for '=' and '\0' */ 2511556Srgrimes if (val == NULL) { 2521556Srgrimes flags |= VUNSET; 2531556Srgrimes } else { 2541556Srgrimes len += strlen(val); 2551556Srgrimes } 256200956Sjilles nameeq = ckmalloc(len); 257200956Sjilles memcpy(nameeq, name, namelen); 258200956Sjilles nameeq[namelen] = '='; 2591556Srgrimes if (val) 260200956Sjilles scopy(val, nameeq + namelen + 1); 261200956Sjilles else 262200956Sjilles nameeq[namelen + 1] = '\0'; 2631556Srgrimes setvareq(nameeq, flags); 2641556Srgrimes} 2651556Srgrimes 26617525SacheSTATIC int 267200956Sjilleslocalevar(const char *s) 26890111Simp{ 269207678Sjilles const char *const *ss; 2701556Srgrimes 27117525Sache if (*s != 'L') 27217525Sache return 0; 27317525Sache if (varequal(s + 1, "ANG")) 27417525Sache return 1; 27517525Sache if (strncmp(s + 1, "C_", 2) != 0) 27617525Sache return 0; 277207678Sjilles if (varequal(s + 3, "ALL")) 278207678Sjilles return 1; 279207678Sjilles for (ss = locale_names; *ss ; ss++) 280207678Sjilles if (varequal(s + 3, *ss + 3)) 28117525Sache return 1; 28217525Sache return 0; 28317525Sache} 2841556Srgrimes 285171268Sscf 2861556Srgrimes/* 287171268Sscf * Sets/unsets an environment variable from a pointer that may actually be a 288171268Sscf * pointer into environ where the string should not be manipulated. 289171268Sscf */ 290171268Sscfstatic void 291200956Sjilleschange_env(const char *s, int set) 292171268Sscf{ 293171268Sscf char *eqp; 294171268Sscf char *ss; 295171268Sscf 296171268Sscf ss = savestr(s); 297171268Sscf if ((eqp = strchr(ss, '=')) != NULL) 298171268Sscf *eqp = '\0'; 299171268Sscf if (set && eqp != NULL) 300171268Sscf (void) setenv(ss, eqp + 1, 1); 301171268Sscf else 302171268Sscf (void) unsetenv(ss); 303171268Sscf ckfree(ss); 304171268Sscf 305171268Sscf return; 306171268Sscf} 307171268Sscf 308171268Sscf 309171268Sscf/* 3101556Srgrimes * Same as setvar except that the variable and value are passed in 3111556Srgrimes * the first argument as name=value. Since the first argument will 3121556Srgrimes * be actually stored in the table, it should not be a string that 3131556Srgrimes * will go away. 3141556Srgrimes */ 3151556Srgrimes 3161556Srgrimesvoid 31790111Simpsetvareq(char *s, int flags) 31817987Speter{ 3191556Srgrimes struct var *vp, **vpp; 32090112Simp int len; 3211556Srgrimes 32245263Scracauer if (aflag) 32345263Scracauer flags |= VEXPORT; 3241556Srgrimes vpp = hashvar(s); 3251556Srgrimes for (vp = *vpp ; vp ; vp = vp->next) { 3261556Srgrimes if (varequal(s, vp->text)) { 3271556Srgrimes if (vp->flags & VREADONLY) { 32890112Simp len = strchr(s, '=') - s; 3291556Srgrimes error("%.*s: is read only", len, s); 3301556Srgrimes } 3311556Srgrimes INTOFF; 33220425Ssteve 33320425Ssteve if (vp->func && (flags & VNOFUNC) == 0) 33420425Ssteve (*vp->func)(strchr(s, '=') + 1); 33520425Ssteve 3361556Srgrimes if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0) 3371556Srgrimes ckfree(vp->text); 33820425Ssteve 33920425Ssteve vp->flags &= ~(VTEXTFIXED|VSTACK|VUNSET); 3401556Srgrimes vp->flags |= flags; 3411556Srgrimes vp->text = s; 34220425Ssteve 34320425Ssteve /* 34420425Ssteve * We could roll this to a function, to handle it as 34520425Ssteve * a regular variable function callback, but why bother? 346203576Sjilles * 347203576Sjilles * Note: this assumes iflag is not set to 1 initially. 348203576Sjilles * As part of init(), this is called before arguments 349203576Sjilles * are looked at. 35020425Ssteve */ 351203576Sjilles if ((vp == &vmpath || (vp == &vmail && ! mpathset())) && 352203576Sjilles iflag == 1) 3531556Srgrimes chkmail(1); 35417525Sache if ((vp->flags & VEXPORT) && localevar(s)) { 355171268Sscf change_env(s, 1); 35617525Sache (void) setlocale(LC_ALL, ""); 35717525Sache } 3581556Srgrimes INTON; 3591556Srgrimes return; 3601556Srgrimes } 3611556Srgrimes } 3621556Srgrimes /* not found */ 3631556Srgrimes vp = ckmalloc(sizeof (*vp)); 3641556Srgrimes vp->flags = flags; 3651556Srgrimes vp->text = s; 3661556Srgrimes vp->next = *vpp; 36720425Ssteve vp->func = NULL; 36817525Sache INTOFF; 3691556Srgrimes *vpp = vp; 37017525Sache if ((vp->flags & VEXPORT) && localevar(s)) { 371171268Sscf change_env(s, 1); 37217525Sache (void) setlocale(LC_ALL, ""); 37317525Sache } 37417525Sache INTON; 3751556Srgrimes} 3761556Srgrimes 3771556Srgrimes 3781556Srgrimes 3791556Srgrimes/* 3801556Srgrimes * Process a linked list of variable assignments. 3811556Srgrimes */ 3821556Srgrimes 3831556Srgrimesvoid 38490111Simplistsetvar(struct strlist *list) 38590111Simp{ 3861556Srgrimes struct strlist *lp; 3871556Srgrimes 3881556Srgrimes INTOFF; 3891556Srgrimes for (lp = list ; lp ; lp = lp->next) { 3901556Srgrimes setvareq(savestr(lp->text), 0); 3911556Srgrimes } 3921556Srgrimes INTON; 3931556Srgrimes} 3941556Srgrimes 3951556Srgrimes 3961556Srgrimes 3971556Srgrimes/* 3981556Srgrimes * Find the value of a variable. Returns NULL if not set. 3991556Srgrimes */ 4001556Srgrimes 4011556Srgrimeschar * 402200956Sjilleslookupvar(const char *name) 40390111Simp{ 4041556Srgrimes struct var *v; 4051556Srgrimes 4061556Srgrimes for (v = *hashvar(name) ; v ; v = v->next) { 4071556Srgrimes if (varequal(v->text, name)) { 4081556Srgrimes if (v->flags & VUNSET) 4091556Srgrimes return NULL; 4101556Srgrimes return strchr(v->text, '=') + 1; 4111556Srgrimes } 4121556Srgrimes } 4131556Srgrimes return NULL; 4141556Srgrimes} 4151556Srgrimes 4161556Srgrimes 4171556Srgrimes 4181556Srgrimes/* 4191556Srgrimes * Search the environment of a builtin command. If the second argument 4201556Srgrimes * is nonzero, return the value of a variable even if it hasn't been 4211556Srgrimes * exported. 4221556Srgrimes */ 4231556Srgrimes 4241556Srgrimeschar * 425200956Sjillesbltinlookup(const char *name, int doall) 42617987Speter{ 4271556Srgrimes struct strlist *sp; 4281556Srgrimes struct var *v; 4291556Srgrimes 4301556Srgrimes for (sp = cmdenviron ; sp ; sp = sp->next) { 4311556Srgrimes if (varequal(sp->text, name)) 4321556Srgrimes return strchr(sp->text, '=') + 1; 4331556Srgrimes } 4341556Srgrimes for (v = *hashvar(name) ; v ; v = v->next) { 4351556Srgrimes if (varequal(v->text, name)) { 43617987Speter if ((v->flags & VUNSET) 43717987Speter || (!doall && (v->flags & VEXPORT) == 0)) 4381556Srgrimes return NULL; 4391556Srgrimes return strchr(v->text, '=') + 1; 4401556Srgrimes } 4411556Srgrimes } 4421556Srgrimes return NULL; 4431556Srgrimes} 4441556Srgrimes 4451556Srgrimes 446207678Sjilles/* 447207678Sjilles * Set up locale for a builtin (LANG/LC_* assignments). 448207678Sjilles */ 449207678Sjillesvoid 450207678Sjillesbltinsetlocale(void) 451207678Sjilles{ 452207678Sjilles struct strlist *lp; 453207678Sjilles int act = 0; 454207678Sjilles char *loc, *locdef; 455207678Sjilles int i; 4561556Srgrimes 457207678Sjilles for (lp = cmdenviron ; lp ; lp = lp->next) { 458207678Sjilles if (localevar(lp->text)) { 459207678Sjilles act = 1; 460207678Sjilles break; 461207678Sjilles } 462207678Sjilles } 463207678Sjilles if (!act) 464207678Sjilles return; 465207678Sjilles loc = bltinlookup("LC_ALL", 0); 466207678Sjilles INTOFF; 467207678Sjilles if (loc != NULL) { 468207678Sjilles setlocale(LC_ALL, loc); 469207678Sjilles INTON; 470207678Sjilles return; 471207678Sjilles } 472207678Sjilles locdef = bltinlookup("LANG", 0); 473207678Sjilles for (i = 0; locale_names[i] != NULL; i++) { 474207678Sjilles loc = bltinlookup(locale_names[i], 0); 475207678Sjilles if (loc == NULL) 476207678Sjilles loc = locdef; 477207678Sjilles if (loc != NULL) 478207678Sjilles setlocale(locale_categories[i], loc); 479207678Sjilles } 480207678Sjilles INTON; 481207678Sjilles} 482207678Sjilles 4831556Srgrimes/* 484207678Sjilles * Undo the effect of bltinlocaleset(). 485207678Sjilles */ 486207678Sjillesvoid 487207678Sjillesbltinunsetlocale(void) 488207678Sjilles{ 489207678Sjilles struct strlist *lp; 490207678Sjilles 491207678Sjilles INTOFF; 492207678Sjilles for (lp = cmdenviron ; lp ; lp = lp->next) { 493207678Sjilles if (localevar(lp->text)) { 494207678Sjilles setlocale(LC_ALL, ""); 495207678Sjilles return; 496207678Sjilles } 497207678Sjilles } 498207678Sjilles INTON; 499207678Sjilles} 500207678Sjilles 501207678Sjilles 502207678Sjilles/* 5031556Srgrimes * Generate a list of exported variables. This routine is used to construct 5041556Srgrimes * the third argument to execve when executing a program. 5051556Srgrimes */ 5061556Srgrimes 5071556Srgrimeschar ** 50890111Simpenvironment(void) 50990111Simp{ 5101556Srgrimes int nenv; 5111556Srgrimes struct var **vpp; 5121556Srgrimes struct var *vp; 5131556Srgrimes char **env, **ep; 5141556Srgrimes 5151556Srgrimes nenv = 0; 5161556Srgrimes for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) { 5171556Srgrimes for (vp = *vpp ; vp ; vp = vp->next) 5181556Srgrimes if (vp->flags & VEXPORT) 5191556Srgrimes nenv++; 5201556Srgrimes } 5211556Srgrimes ep = env = stalloc((nenv + 1) * sizeof *env); 5221556Srgrimes for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) { 5231556Srgrimes for (vp = *vpp ; vp ; vp = vp->next) 5241556Srgrimes if (vp->flags & VEXPORT) 5251556Srgrimes *ep++ = vp->text; 5261556Srgrimes } 5271556Srgrimes *ep = NULL; 5281556Srgrimes return env; 5291556Srgrimes} 5301556Srgrimes 5311556Srgrimes 5321556Srgrimes/* 5331556Srgrimes * Called when a shell procedure is invoked to clear out nonexported 5341556Srgrimes * variables. It is also necessary to reallocate variables of with 5351556Srgrimes * VSTACK set since these are currently allocated on the stack. 5361556Srgrimes */ 5371556Srgrimes 538149016SstefanfMKINIT void shprocvar(void); 5391556Srgrimes 540201053Sjilles#ifdef mkinit 5411556SrgrimesSHELLPROC { 5421556Srgrimes shprocvar(); 5431556Srgrimes} 5441556Srgrimes#endif 5451556Srgrimes 5461556Srgrimesvoid 54790111Simpshprocvar(void) 54890111Simp{ 5491556Srgrimes struct var **vpp; 5501556Srgrimes struct var *vp, **prev; 5511556Srgrimes 5521556Srgrimes for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) { 5531556Srgrimes for (prev = vpp ; (vp = *prev) != NULL ; ) { 5541556Srgrimes if ((vp->flags & VEXPORT) == 0) { 5551556Srgrimes *prev = vp->next; 5561556Srgrimes if ((vp->flags & VTEXTFIXED) == 0) 5571556Srgrimes ckfree(vp->text); 5581556Srgrimes if ((vp->flags & VSTRFIXED) == 0) 5591556Srgrimes ckfree(vp); 5601556Srgrimes } else { 5611556Srgrimes if (vp->flags & VSTACK) { 5621556Srgrimes vp->text = savestr(vp->text); 5631556Srgrimes vp->flags &=~ VSTACK; 5641556Srgrimes } 5651556Srgrimes prev = &vp->next; 5661556Srgrimes } 5671556Srgrimes } 5681556Srgrimes } 5691556Srgrimes initvar(); 5701556Srgrimes} 5711556Srgrimes 5721556Srgrimes 573158145Sstefanfstatic int 574158145Sstefanfvar_compare(const void *a, const void *b) 575158145Sstefanf{ 576158145Sstefanf const char *const *sa, *const *sb; 5771556Srgrimes 578158145Sstefanf sa = a; 579158145Sstefanf sb = b; 580158145Sstefanf /* 581158145Sstefanf * This compares two var=value strings which creates a different 582158145Sstefanf * order from what you would probably expect. POSIX is somewhat 583158145Sstefanf * ambiguous on what should be sorted exactly. 584158145Sstefanf */ 585158145Sstefanf return strcoll(*sa, *sb); 586158145Sstefanf} 587158145Sstefanf 588158145Sstefanf 5891556Srgrimes/* 5901556Srgrimes * Command to list all variables which are set. Currently this command 5911556Srgrimes * is invoked from the set command when the set command is called without 5921556Srgrimes * any variables. 5931556Srgrimes */ 5941556Srgrimes 5951556Srgrimesint 59690111Simpshowvarscmd(int argc __unused, char **argv __unused) 59717987Speter{ 5981556Srgrimes struct var **vpp; 5991556Srgrimes struct var *vp; 60097915Stjr const char *s; 601158145Sstefanf const char **vars; 602158145Sstefanf int i, n; 6031556Srgrimes 604158145Sstefanf /* 605158145Sstefanf * POSIX requires us to sort the variables. 606158145Sstefanf */ 607158145Sstefanf n = 0; 608158145Sstefanf for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) { 609158145Sstefanf for (vp = *vpp; vp; vp = vp->next) { 610158145Sstefanf if (!(vp->flags & VUNSET)) 611158145Sstefanf n++; 6121556Srgrimes } 6131556Srgrimes } 614158145Sstefanf 615158145Sstefanf INTON; 616158145Sstefanf vars = ckmalloc(n * sizeof(*vars)); 617158145Sstefanf i = 0; 618158145Sstefanf for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) { 619158145Sstefanf for (vp = *vpp; vp; vp = vp->next) { 620158145Sstefanf if (!(vp->flags & VUNSET)) 621158145Sstefanf vars[i++] = vp->text; 622158145Sstefanf } 623158145Sstefanf } 624158145Sstefanf 625158145Sstefanf qsort(vars, n, sizeof(*vars), var_compare); 626158145Sstefanf for (i = 0; i < n; i++) { 627158145Sstefanf for (s = vars[i]; *s != '='; s++) 628158145Sstefanf out1c(*s); 629158145Sstefanf out1c('='); 630158145Sstefanf out1qstr(s + 1); 631158145Sstefanf out1c('\n'); 632158145Sstefanf } 633158145Sstefanf ckfree(vars); 634158145Sstefanf INTOFF; 635158145Sstefanf 6361556Srgrimes return 0; 6371556Srgrimes} 6381556Srgrimes 6391556Srgrimes 6401556Srgrimes 6411556Srgrimes/* 6421556Srgrimes * The export and readonly commands. 6431556Srgrimes */ 6441556Srgrimes 6451556Srgrimesint 64690111Simpexportcmd(int argc, char **argv) 64717987Speter{ 6481556Srgrimes struct var **vpp; 6491556Srgrimes struct var *vp; 6501556Srgrimes char *name; 6511556Srgrimes char *p; 65297914Stjr char *cmdname; 65397914Stjr int ch, values; 6541556Srgrimes int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT; 6551556Srgrimes 65697914Stjr cmdname = argv[0]; 65797914Stjr optreset = optind = 1; 658100663Stjr opterr = 0; 65997914Stjr values = 0; 66097914Stjr while ((ch = getopt(argc, argv, "p")) != -1) { 66197914Stjr switch (ch) { 66297914Stjr case 'p': 66397914Stjr values = 1; 66497914Stjr break; 66597914Stjr case '?': 66697914Stjr default: 66797914Stjr error("unknown option: -%c", optopt); 66897914Stjr } 66997914Stjr } 67097914Stjr argc -= optind; 67197914Stjr argv += optind; 67297914Stjr 673149919Sstefanf if (values && argc != 0) 674149919Sstefanf error("-p requires no arguments"); 67597914Stjr if (argc != 0) { 676149919Sstefanf while ((name = *argv++) != NULL) { 6771556Srgrimes if ((p = strchr(name, '=')) != NULL) { 6781556Srgrimes p++; 6791556Srgrimes } else { 6801556Srgrimes vpp = hashvar(name); 6811556Srgrimes for (vp = *vpp ; vp ; vp = vp->next) { 6821556Srgrimes if (varequal(vp->text, name)) { 68390111Simp 6841556Srgrimes vp->flags |= flag; 68517525Sache if ((vp->flags & VEXPORT) && localevar(vp->text)) { 686171268Sscf change_env(vp->text, 1); 68717525Sache (void) setlocale(LC_ALL, ""); 68817525Sache } 6891556Srgrimes goto found; 6901556Srgrimes } 6911556Srgrimes } 6921556Srgrimes } 6931556Srgrimes setvar(name, p, flag); 6941556Srgrimesfound:; 6951556Srgrimes } 6961556Srgrimes } else { 6971556Srgrimes for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) { 6981556Srgrimes for (vp = *vpp ; vp ; vp = vp->next) { 6991556Srgrimes if (vp->flags & flag) { 70097914Stjr if (values) { 70197914Stjr out1str(cmdname); 70297914Stjr out1c(' '); 70397914Stjr } 7041556Srgrimes for (p = vp->text ; *p != '=' ; p++) 7051556Srgrimes out1c(*p); 70697914Stjr if (values && !(vp->flags & VUNSET)) { 70797914Stjr out1c('='); 70897914Stjr out1qstr(p + 1); 70997914Stjr } 7101556Srgrimes out1c('\n'); 7111556Srgrimes } 7121556Srgrimes } 7131556Srgrimes } 7141556Srgrimes } 7151556Srgrimes return 0; 7161556Srgrimes} 7171556Srgrimes 7181556Srgrimes 7191556Srgrimes/* 7201556Srgrimes * The "local" command. 7211556Srgrimes */ 7221556Srgrimes 72317987Speterint 72490111Simplocalcmd(int argc __unused, char **argv __unused) 72517987Speter{ 7261556Srgrimes char *name; 7271556Srgrimes 7281556Srgrimes if (! in_function()) 7291556Srgrimes error("Not in a function"); 7301556Srgrimes while ((name = *argptr++) != NULL) { 7311556Srgrimes mklocal(name); 7321556Srgrimes } 7331556Srgrimes return 0; 7341556Srgrimes} 7351556Srgrimes 7361556Srgrimes 7371556Srgrimes/* 7381556Srgrimes * Make a variable a local variable. When a variable is made local, it's 7391556Srgrimes * value and flags are saved in a localvar structure. The saved values 7401556Srgrimes * will be restored when the shell function returns. We handle the name 7411556Srgrimes * "-" as a special case. 7421556Srgrimes */ 7431556Srgrimes 7441556Srgrimesvoid 74590111Simpmklocal(char *name) 74690111Simp{ 7471556Srgrimes struct localvar *lvp; 7481556Srgrimes struct var **vpp; 7491556Srgrimes struct var *vp; 7501556Srgrimes 7511556Srgrimes INTOFF; 7521556Srgrimes lvp = ckmalloc(sizeof (struct localvar)); 7531556Srgrimes if (name[0] == '-' && name[1] == '\0') { 7541556Srgrimes lvp->text = ckmalloc(sizeof optlist); 75517987Speter memcpy(lvp->text, optlist, sizeof optlist); 7561556Srgrimes vp = NULL; 7571556Srgrimes } else { 7581556Srgrimes vpp = hashvar(name); 7591556Srgrimes for (vp = *vpp ; vp && ! varequal(vp->text, name) ; vp = vp->next); 7601556Srgrimes if (vp == NULL) { 7611556Srgrimes if (strchr(name, '=')) 7621556Srgrimes setvareq(savestr(name), VSTRFIXED); 7631556Srgrimes else 7641556Srgrimes setvar(name, NULL, VSTRFIXED); 7651556Srgrimes vp = *vpp; /* the new variable */ 7661556Srgrimes lvp->text = NULL; 7671556Srgrimes lvp->flags = VUNSET; 7681556Srgrimes } else { 7691556Srgrimes lvp->text = vp->text; 7701556Srgrimes lvp->flags = vp->flags; 7711556Srgrimes vp->flags |= VSTRFIXED|VTEXTFIXED; 7721556Srgrimes if (strchr(name, '=')) 7731556Srgrimes setvareq(savestr(name), 0); 7741556Srgrimes } 7751556Srgrimes } 7761556Srgrimes lvp->vp = vp; 7771556Srgrimes lvp->next = localvars; 7781556Srgrimes localvars = lvp; 7791556Srgrimes INTON; 7801556Srgrimes} 7811556Srgrimes 7821556Srgrimes 7831556Srgrimes/* 7841556Srgrimes * Called after a function returns. 7851556Srgrimes */ 7861556Srgrimes 7871556Srgrimesvoid 78890111Simppoplocalvars(void) 78990111Simp{ 7901556Srgrimes struct localvar *lvp; 7911556Srgrimes struct var *vp; 7921556Srgrimes 7931556Srgrimes while ((lvp = localvars) != NULL) { 7941556Srgrimes localvars = lvp->next; 7951556Srgrimes vp = lvp->vp; 7961556Srgrimes if (vp == NULL) { /* $- saved */ 79717987Speter memcpy(optlist, lvp->text, sizeof optlist); 7981556Srgrimes ckfree(lvp->text); 7991556Srgrimes } else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) { 8001556Srgrimes (void)unsetvar(vp->text); 8011556Srgrimes } else { 8021556Srgrimes if ((vp->flags & VTEXTFIXED) == 0) 8031556Srgrimes ckfree(vp->text); 8041556Srgrimes vp->flags = lvp->flags; 8051556Srgrimes vp->text = lvp->text; 8061556Srgrimes } 8071556Srgrimes ckfree(lvp); 8081556Srgrimes } 8091556Srgrimes} 8101556Srgrimes 8111556Srgrimes 81217987Speterint 81390111Simpsetvarcmd(int argc, char **argv) 81417987Speter{ 8151556Srgrimes if (argc <= 2) 8161556Srgrimes return unsetcmd(argc, argv); 8171556Srgrimes else if (argc == 3) 8181556Srgrimes setvar(argv[1], argv[2], 0); 8191556Srgrimes else 8201556Srgrimes error("List assignment not implemented"); 8211556Srgrimes return 0; 8221556Srgrimes} 8231556Srgrimes 8241556Srgrimes 8251556Srgrimes/* 8261556Srgrimes * The unset builtin command. We unset the function before we unset the 8271556Srgrimes * variable to allow a function to be unset when there is a readonly variable 8281556Srgrimes * with the same name. 8291556Srgrimes */ 8301556Srgrimes 83117987Speterint 83290111Simpunsetcmd(int argc __unused, char **argv __unused) 83317987Speter{ 8341556Srgrimes char **ap; 8351556Srgrimes int i; 8361556Srgrimes int flg_func = 0; 8371556Srgrimes int flg_var = 0; 8381556Srgrimes int ret = 0; 8391556Srgrimes 8401556Srgrimes while ((i = nextopt("vf")) != '\0') { 8411556Srgrimes if (i == 'f') 8421556Srgrimes flg_func = 1; 8431556Srgrimes else 8441556Srgrimes flg_var = 1; 8451556Srgrimes } 8461556Srgrimes if (flg_func == 0 && flg_var == 0) 8471556Srgrimes flg_var = 1; 8488855Srgrimes 8491556Srgrimes for (ap = argptr; *ap ; ap++) { 8501556Srgrimes if (flg_func) 8511556Srgrimes ret |= unsetfunc(*ap); 8521556Srgrimes if (flg_var) 8531556Srgrimes ret |= unsetvar(*ap); 8541556Srgrimes } 8551556Srgrimes return ret; 8561556Srgrimes} 8571556Srgrimes 8581556Srgrimes 8591556Srgrimes/* 8601556Srgrimes * Unset the specified variable. 8611556Srgrimes */ 8621556Srgrimes 86320425Ssteveint 864200956Sjillesunsetvar(const char *s) 86590111Simp{ 8661556Srgrimes struct var **vpp; 8671556Srgrimes struct var *vp; 8681556Srgrimes 8691556Srgrimes vpp = hashvar(s); 8701556Srgrimes for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) { 8711556Srgrimes if (varequal(vp->text, s)) { 8721556Srgrimes if (vp->flags & VREADONLY) 8731556Srgrimes return (1); 8741556Srgrimes INTOFF; 8751556Srgrimes if (*(strchr(vp->text, '=') + 1) != '\0') 8761556Srgrimes setvar(s, nullstr, 0); 87717525Sache if ((vp->flags & VEXPORT) && localevar(vp->text)) { 878171268Sscf change_env(s, 0); 87917525Sache setlocale(LC_ALL, ""); 88017525Sache } 88120425Ssteve vp->flags &= ~VEXPORT; 8821556Srgrimes vp->flags |= VUNSET; 8831556Srgrimes if ((vp->flags & VSTRFIXED) == 0) { 8841556Srgrimes if ((vp->flags & VTEXTFIXED) == 0) 8851556Srgrimes ckfree(vp->text); 8861556Srgrimes *vpp = vp->next; 8871556Srgrimes ckfree(vp); 8881556Srgrimes } 8891556Srgrimes INTON; 8901556Srgrimes return (0); 8911556Srgrimes } 8921556Srgrimes } 8931556Srgrimes 894135856Sdes return (0); 8951556Srgrimes} 8961556Srgrimes 8971556Srgrimes 8981556Srgrimes 8991556Srgrimes/* 9001556Srgrimes * Find the appropriate entry in the hash table from the name. 9011556Srgrimes */ 9021556Srgrimes 9031556SrgrimesSTATIC struct var ** 904200956Sjilleshashvar(const char *p) 90590111Simp{ 9061556Srgrimes unsigned int hashval; 9071556Srgrimes 90820425Ssteve hashval = ((unsigned char) *p) << 4; 9091556Srgrimes while (*p && *p != '=') 91020425Ssteve hashval += (unsigned char) *p++; 9111556Srgrimes return &vartab[hashval % VTABSIZE]; 9121556Srgrimes} 9131556Srgrimes 9141556Srgrimes 9151556Srgrimes 9161556Srgrimes/* 9171556Srgrimes * Returns true if the two strings specify the same varable. The first 9181556Srgrimes * variable name is terminated by '='; the second may be terminated by 9191556Srgrimes * either '=' or '\0'. 9201556Srgrimes */ 9211556Srgrimes 9221556SrgrimesSTATIC int 923200956Sjillesvarequal(const char *p, const char *q) 92490111Simp{ 9251556Srgrimes while (*p == *q++) { 9261556Srgrimes if (*p++ == '=') 9271556Srgrimes return 1; 9281556Srgrimes } 9291556Srgrimes if (*p == '=' && *(q - 1) == '\0') 9301556Srgrimes return 1; 9311556Srgrimes return 0; 9321556Srgrimes} 933