var.c revision 240541
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 240541 2012-09-15 21:56:30Z 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> 50221559Sjilles#include <langinfo.h> 5117525Sache 521556Srgrimes#include "shell.h" 531556Srgrimes#include "output.h" 541556Srgrimes#include "expand.h" 551556Srgrimes#include "nodes.h" /* for other headers */ 561556Srgrimes#include "eval.h" /* defines cmdenviron */ 571556Srgrimes#include "exec.h" 581556Srgrimes#include "syntax.h" 591556Srgrimes#include "options.h" 601556Srgrimes#include "mail.h" 611556Srgrimes#include "var.h" 621556Srgrimes#include "memalloc.h" 631556Srgrimes#include "error.h" 641556Srgrimes#include "mystring.h" 6520425Ssteve#include "parser.h" 66223060Sjilles#include "builtins.h" 6717987Speter#ifndef NO_HISTORY 6817987Speter#include "myhistedit.h" 6917987Speter#endif 701556Srgrimes 711556Srgrimes 721556Srgrimes#define VTABSIZE 39 731556Srgrimes 741556Srgrimes 751556Srgrimesstruct varinit { 761556Srgrimes struct var *var; 771556Srgrimes int flags; 78201056Sjilles const char *text; 7990111Simp void (*func)(const char *); 801556Srgrimes}; 811556Srgrimes 821556Srgrimes 8317987Speter#ifndef NO_HISTORY 841556Srgrimesstruct var vhistsize; 85208755Sjillesstruct var vterm; 8617987Speter#endif 871556Srgrimesstruct var vifs; 881556Srgrimesstruct var vmail; 891556Srgrimesstruct var vmpath; 901556Srgrimesstruct var vpath; 9197689Stjrstruct var vppid; 921556Srgrimesstruct var vps1; 931556Srgrimesstruct var vps2; 94159632Sstefanfstruct var vps4; 951556Srgrimesstruct var vvers; 96213760Sobrienstatic struct var voptind; 97230998Sjillesstruct var vdisvfork; 981556Srgrimes 99223024Sjillesint forcelocal; 100223024Sjilles 101213760Sobrienstatic const struct varinit varinit[] = { 10217987Speter#ifndef NO_HISTORY 103201056Sjilles { &vhistsize, VUNSET, "HISTSIZE=", 10420425Ssteve sethistsize }, 10517987Speter#endif 106201056Sjilles { &vifs, 0, "IFS= \t\n", 10720425Ssteve NULL }, 108201056Sjilles { &vmail, VUNSET, "MAIL=", 10920425Ssteve NULL }, 110201056Sjilles { &vmpath, VUNSET, "MAILPATH=", 11120425Ssteve NULL }, 112201056Sjilles { &vpath, 0, "PATH=" _PATH_DEFPATH, 11320425Ssteve changepath }, 114201056Sjilles { &vppid, VUNSET, "PPID=", 11597689Stjr NULL }, 1168855Srgrimes /* 1171556Srgrimes * vps1 depends on uid 1181556Srgrimes */ 119201056Sjilles { &vps2, 0, "PS2=> ", 12020425Ssteve NULL }, 121201056Sjilles { &vps4, 0, "PS4=+ ", 122159632Sstefanf NULL }, 123208755Sjilles#ifndef NO_HISTORY 124208755Sjilles { &vterm, VUNSET, "TERM=", 125208755Sjilles setterm }, 126208755Sjilles#endif 127201056Sjilles { &voptind, 0, "OPTIND=1", 12820425Ssteve getoptsreset }, 129230998Sjilles { &vdisvfork, VUNSET, "SH_DISABLE_VFORK=", 130230998Sjilles NULL }, 13120425Ssteve { NULL, 0, NULL, 13220425Ssteve NULL } 1331556Srgrimes}; 1341556Srgrimes 135213760Sobrienstatic struct var *vartab[VTABSIZE]; 1361556Srgrimes 137213760Sobrienstatic const char *const locale_names[7] = { 138207678Sjilles "LC_COLLATE", "LC_CTYPE", "LC_MONETARY", 139207678Sjilles "LC_NUMERIC", "LC_TIME", "LC_MESSAGES", NULL 140207678Sjilles}; 141213760Sobrienstatic const int locale_categories[7] = { 142207678Sjilles LC_COLLATE, LC_CTYPE, LC_MONETARY, LC_NUMERIC, LC_TIME, LC_MESSAGES, 0 143207678Sjilles}; 144207678Sjilles 145213811Sobrienstatic int varequal(const char *, const char *); 146221668Sjillesstatic struct var *find_var(const char *, struct var ***, int *); 147213811Sobrienstatic int localevar(const char *); 1481556Srgrimes 1491556Srgrimes/* 150155302Sschweikh * Initialize the variable symbol tables and import the environment. 1511556Srgrimes */ 1521556Srgrimes 1531556Srgrimes#ifdef mkinit 1541556SrgrimesINCLUDE "var.h" 155201053SjillesMKINIT char **environ; 1561556SrgrimesINIT { 1571556Srgrimes char **envp; 1581556Srgrimes 1591556Srgrimes initvar(); 1601556Srgrimes for (envp = environ ; *envp ; envp++) { 1611556Srgrimes if (strchr(*envp, '=')) { 1621556Srgrimes setvareq(*envp, VEXPORT|VTEXTFIXED); 1631556Srgrimes } 1641556Srgrimes } 1651556Srgrimes} 1661556Srgrimes#endif 1671556Srgrimes 1681556Srgrimes 1691556Srgrimes/* 1701556Srgrimes * This routine initializes the builtin variables. It is called when the 171218306Sjilles * shell is initialized. 1721556Srgrimes */ 1731556Srgrimes 1741556Srgrimesvoid 17590111Simpinitvar(void) 17690111Simp{ 17797689Stjr char ppid[20]; 1781556Srgrimes const struct varinit *ip; 1791556Srgrimes struct var *vp; 1801556Srgrimes struct var **vpp; 1811556Srgrimes 1821556Srgrimes for (ip = varinit ; (vp = ip->var) != NULL ; ip++) { 183221668Sjilles if (find_var(ip->text, &vpp, &vp->name_len) != NULL) 184221668Sjilles continue; 185221668Sjilles vp->next = *vpp; 186221668Sjilles *vpp = vp; 187221668Sjilles vp->text = __DECONST(char *, ip->text); 188221668Sjilles vp->flags = ip->flags | VSTRFIXED | VTEXTFIXED; 189221668Sjilles vp->func = ip->func; 1901556Srgrimes } 1911556Srgrimes /* 1921556Srgrimes * PS1 depends on uid 1931556Srgrimes */ 194221668Sjilles if (find_var("PS1", &vpp, &vps1.name_len) == NULL) { 1951556Srgrimes vps1.next = *vpp; 1961556Srgrimes *vpp = &vps1; 197201056Sjilles vps1.text = __DECONST(char *, geteuid() ? "PS1=$ " : "PS1=# "); 1981556Srgrimes vps1.flags = VSTRFIXED|VTEXTFIXED; 1991556Srgrimes } 20097689Stjr if ((vppid.flags & VEXPORT) == 0) { 20197689Stjr fmtstr(ppid, sizeof(ppid), "%d", (int)getppid()); 20297689Stjr setvarsafe("PPID", ppid, 0); 20397689Stjr } 2041556Srgrimes} 2051556Srgrimes 2061556Srgrimes/* 20720425Ssteve * Safe version of setvar, returns 1 on success 0 on failure. 20820425Ssteve */ 20920425Ssteve 21020425Ssteveint 211200956Sjillessetvarsafe(const char *name, const char *val, int flags) 21220425Ssteve{ 21320425Ssteve struct jmploc jmploc; 214194765Sjilles struct jmploc *const savehandler = handler; 21520425Ssteve int err = 0; 216199660Sjilles int inton; 21720425Ssteve 218199660Sjilles inton = is_int_on(); 21920425Ssteve if (setjmp(jmploc.loc)) 22020425Ssteve err = 1; 22120425Ssteve else { 22220425Ssteve handler = &jmploc; 22320425Ssteve setvar(name, val, flags); 22420425Ssteve } 22520425Ssteve handler = savehandler; 226199660Sjilles SETINTON(inton); 22720425Ssteve return err; 22820425Ssteve} 22920425Ssteve 23020425Ssteve/* 231155302Sschweikh * Set the value of a variable. The flags argument is stored with the 2321556Srgrimes * flags of the variable. If val is NULL, the variable is unset. 2331556Srgrimes */ 2341556Srgrimes 2351556Srgrimesvoid 236200956Sjillessetvar(const char *name, const char *val, int flags) 23717987Speter{ 238200956Sjilles const char *p; 2391556Srgrimes int len; 2401556Srgrimes int namelen; 2411556Srgrimes char *nameeq; 2421556Srgrimes int isbad; 2431556Srgrimes 2441556Srgrimes isbad = 0; 2451556Srgrimes p = name; 24617525Sache if (! is_name(*p)) 2471556Srgrimes isbad = 1; 24817525Sache p++; 2491556Srgrimes for (;;) { 2501556Srgrimes if (! is_in_name(*p)) { 2511556Srgrimes if (*p == '\0' || *p == '=') 2521556Srgrimes break; 2531556Srgrimes isbad = 1; 2541556Srgrimes } 2551556Srgrimes p++; 2561556Srgrimes } 2571556Srgrimes namelen = p - name; 2581556Srgrimes if (isbad) 2591556Srgrimes error("%.*s: bad variable name", namelen, name); 2601556Srgrimes len = namelen + 2; /* 2 is space for '=' and '\0' */ 2611556Srgrimes if (val == NULL) { 2621556Srgrimes flags |= VUNSET; 2631556Srgrimes } else { 2641556Srgrimes len += strlen(val); 2651556Srgrimes } 266200956Sjilles nameeq = ckmalloc(len); 267200956Sjilles memcpy(nameeq, name, namelen); 268200956Sjilles nameeq[namelen] = '='; 2691556Srgrimes if (val) 270200956Sjilles scopy(val, nameeq + namelen + 1); 271200956Sjilles else 272200956Sjilles nameeq[namelen + 1] = '\0'; 2731556Srgrimes setvareq(nameeq, flags); 2741556Srgrimes} 2751556Srgrimes 276213811Sobrienstatic int 277200956Sjilleslocalevar(const char *s) 27890111Simp{ 279207678Sjilles const char *const *ss; 2801556Srgrimes 28117525Sache if (*s != 'L') 28217525Sache return 0; 28317525Sache if (varequal(s + 1, "ANG")) 28417525Sache return 1; 28517525Sache if (strncmp(s + 1, "C_", 2) != 0) 28617525Sache return 0; 287207678Sjilles if (varequal(s + 3, "ALL")) 288207678Sjilles return 1; 289207678Sjilles for (ss = locale_names; *ss ; ss++) 290207678Sjilles if (varequal(s + 3, *ss + 3)) 29117525Sache return 1; 29217525Sache return 0; 29317525Sache} 2941556Srgrimes 295171268Sscf 2961556Srgrimes/* 297171268Sscf * Sets/unsets an environment variable from a pointer that may actually be a 298171268Sscf * pointer into environ where the string should not be manipulated. 299171268Sscf */ 300213811Sobrienstatic void 301200956Sjilleschange_env(const char *s, int set) 302171268Sscf{ 303171268Sscf char *eqp; 304171268Sscf char *ss; 305171268Sscf 306171268Sscf ss = savestr(s); 307171268Sscf if ((eqp = strchr(ss, '=')) != NULL) 308171268Sscf *eqp = '\0'; 309171268Sscf if (set && eqp != NULL) 310171268Sscf (void) setenv(ss, eqp + 1, 1); 311171268Sscf else 312171268Sscf (void) unsetenv(ss); 313171268Sscf ckfree(ss); 314171268Sscf 315171268Sscf return; 316171268Sscf} 317171268Sscf 318171268Sscf 319171268Sscf/* 3201556Srgrimes * Same as setvar except that the variable and value are passed in 3211556Srgrimes * the first argument as name=value. Since the first argument will 3221556Srgrimes * be actually stored in the table, it should not be a string that 3231556Srgrimes * will go away. 3241556Srgrimes */ 3251556Srgrimes 3261556Srgrimesvoid 32790111Simpsetvareq(char *s, int flags) 32817987Speter{ 3291556Srgrimes struct var *vp, **vpp; 330221668Sjilles int nlen; 3311556Srgrimes 33245263Scracauer if (aflag) 33345263Scracauer flags |= VEXPORT; 334223024Sjilles if (forcelocal && !(flags & (VNOSET | VNOLOCAL))) 335223024Sjilles mklocal(s); 336221668Sjilles vp = find_var(s, &vpp, &nlen); 337221668Sjilles if (vp != NULL) { 338221668Sjilles if (vp->flags & VREADONLY) 339221668Sjilles error("%.*s: is read only", vp->name_len, s); 340221668Sjilles if (flags & VNOSET) 341221668Sjilles return; 342221668Sjilles INTOFF; 34320425Ssteve 344221668Sjilles if (vp->func && (flags & VNOFUNC) == 0) 345221668Sjilles (*vp->func)(s + vp->name_len + 1); 34620425Ssteve 347221668Sjilles if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0) 348221668Sjilles ckfree(vp->text); 34920425Ssteve 350221668Sjilles vp->flags &= ~(VTEXTFIXED|VSTACK|VUNSET); 351221668Sjilles vp->flags |= flags; 352221668Sjilles vp->text = s; 35320425Ssteve 354221668Sjilles /* 355221668Sjilles * We could roll this to a function, to handle it as 356221668Sjilles * a regular variable function callback, but why bother? 357221668Sjilles * 358221668Sjilles * Note: this assumes iflag is not set to 1 initially. 359221668Sjilles * As part of init(), this is called before arguments 360221668Sjilles * are looked at. 361221668Sjilles */ 362221668Sjilles if ((vp == &vmpath || (vp == &vmail && ! mpathset())) && 363221668Sjilles iflag == 1) 364221668Sjilles chkmail(1); 365221668Sjilles if ((vp->flags & VEXPORT) && localevar(s)) { 366221668Sjilles change_env(s, 1); 367221668Sjilles (void) setlocale(LC_ALL, ""); 368221668Sjilles updatecharset(); 3691556Srgrimes } 370221668Sjilles INTON; 371221668Sjilles return; 3721556Srgrimes } 3731556Srgrimes /* not found */ 374216870Sjilles if (flags & VNOSET) 375216870Sjilles return; 3761556Srgrimes vp = ckmalloc(sizeof (*vp)); 3771556Srgrimes vp->flags = flags; 3781556Srgrimes vp->text = s; 379221668Sjilles vp->name_len = nlen; 3801556Srgrimes vp->next = *vpp; 38120425Ssteve vp->func = NULL; 38217525Sache INTOFF; 3831556Srgrimes *vpp = vp; 38417525Sache if ((vp->flags & VEXPORT) && localevar(s)) { 385171268Sscf change_env(s, 1); 38617525Sache (void) setlocale(LC_ALL, ""); 387221559Sjilles updatecharset(); 38817525Sache } 38917525Sache INTON; 3901556Srgrimes} 3911556Srgrimes 3921556Srgrimes 3931556Srgrimes 3941556Srgrimes/* 3951556Srgrimes * Process a linked list of variable assignments. 3961556Srgrimes */ 3971556Srgrimes 3981556Srgrimesvoid 399216870Sjilleslistsetvar(struct strlist *list, int flags) 40090111Simp{ 4011556Srgrimes struct strlist *lp; 4021556Srgrimes 4031556Srgrimes INTOFF; 4041556Srgrimes for (lp = list ; lp ; lp = lp->next) { 405216870Sjilles setvareq(savestr(lp->text), flags); 4061556Srgrimes } 4071556Srgrimes INTON; 4081556Srgrimes} 4091556Srgrimes 4101556Srgrimes 4111556Srgrimes 4121556Srgrimes/* 4131556Srgrimes * Find the value of a variable. Returns NULL if not set. 4141556Srgrimes */ 4151556Srgrimes 4161556Srgrimeschar * 417200956Sjilleslookupvar(const char *name) 41890111Simp{ 4191556Srgrimes struct var *v; 4201556Srgrimes 421221668Sjilles v = find_var(name, NULL, NULL); 422221668Sjilles if (v == NULL || v->flags & VUNSET) 423221668Sjilles return NULL; 424221668Sjilles return v->text + v->name_len + 1; 4251556Srgrimes} 4261556Srgrimes 4271556Srgrimes 4281556Srgrimes 4291556Srgrimes/* 4301556Srgrimes * Search the environment of a builtin command. If the second argument 4311556Srgrimes * is nonzero, return the value of a variable even if it hasn't been 4321556Srgrimes * exported. 4331556Srgrimes */ 4341556Srgrimes 4351556Srgrimeschar * 436200956Sjillesbltinlookup(const char *name, int doall) 43717987Speter{ 4381556Srgrimes struct strlist *sp; 4391556Srgrimes struct var *v; 440212467Sjilles char *result; 4411556Srgrimes 442212467Sjilles result = NULL; 4431556Srgrimes for (sp = cmdenviron ; sp ; sp = sp->next) { 4441556Srgrimes if (varequal(sp->text, name)) 445212467Sjilles result = strchr(sp->text, '=') + 1; 4461556Srgrimes } 447212467Sjilles if (result != NULL) 448212467Sjilles return result; 449221668Sjilles 450221668Sjilles v = find_var(name, NULL, NULL); 451221668Sjilles if (v == NULL || v->flags & VUNSET || 452221668Sjilles (!doall && (v->flags & VEXPORT) == 0)) 453221668Sjilles return NULL; 454221668Sjilles return v->text + v->name_len + 1; 4551556Srgrimes} 4561556Srgrimes 4571556Srgrimes 458207678Sjilles/* 459207678Sjilles * Set up locale for a builtin (LANG/LC_* assignments). 460207678Sjilles */ 461207678Sjillesvoid 462207678Sjillesbltinsetlocale(void) 463207678Sjilles{ 464207678Sjilles struct strlist *lp; 465207678Sjilles int act = 0; 466207678Sjilles char *loc, *locdef; 467207678Sjilles int i; 4681556Srgrimes 469207678Sjilles for (lp = cmdenviron ; lp ; lp = lp->next) { 470207678Sjilles if (localevar(lp->text)) { 471207678Sjilles act = 1; 472207678Sjilles break; 473207678Sjilles } 474207678Sjilles } 475207678Sjilles if (!act) 476207678Sjilles return; 477207678Sjilles loc = bltinlookup("LC_ALL", 0); 478207678Sjilles INTOFF; 479207678Sjilles if (loc != NULL) { 480207678Sjilles setlocale(LC_ALL, loc); 481207678Sjilles INTON; 482221559Sjilles updatecharset(); 483207678Sjilles return; 484207678Sjilles } 485207678Sjilles locdef = bltinlookup("LANG", 0); 486207678Sjilles for (i = 0; locale_names[i] != NULL; i++) { 487207678Sjilles loc = bltinlookup(locale_names[i], 0); 488207678Sjilles if (loc == NULL) 489207678Sjilles loc = locdef; 490207678Sjilles if (loc != NULL) 491207678Sjilles setlocale(locale_categories[i], loc); 492207678Sjilles } 493207678Sjilles INTON; 494221559Sjilles updatecharset(); 495207678Sjilles} 496207678Sjilles 4971556Srgrimes/* 498207678Sjilles * Undo the effect of bltinlocaleset(). 499207678Sjilles */ 500207678Sjillesvoid 501207678Sjillesbltinunsetlocale(void) 502207678Sjilles{ 503207678Sjilles struct strlist *lp; 504207678Sjilles 505207678Sjilles INTOFF; 506207678Sjilles for (lp = cmdenviron ; lp ; lp = lp->next) { 507207678Sjilles if (localevar(lp->text)) { 508207678Sjilles setlocale(LC_ALL, ""); 509221559Sjilles updatecharset(); 510207678Sjilles return; 511207678Sjilles } 512207678Sjilles } 513207678Sjilles INTON; 514207678Sjilles} 515207678Sjilles 516221559Sjilles/* 517221559Sjilles * Update the localeisutf8 flag. 518221559Sjilles */ 519221559Sjillesvoid 520221559Sjillesupdatecharset(void) 521221559Sjilles{ 522221559Sjilles char *charset; 523207678Sjilles 524221559Sjilles charset = nl_langinfo(CODESET); 525221559Sjilles localeisutf8 = !strcmp(charset, "UTF-8"); 526221559Sjilles} 527221559Sjilles 528221669Sjillesvoid 529221669Sjillesinitcharset(void) 530221669Sjilles{ 531221669Sjilles updatecharset(); 532221669Sjilles initial_localeisutf8 = localeisutf8; 533221669Sjilles} 534221669Sjilles 535207678Sjilles/* 5361556Srgrimes * Generate a list of exported variables. This routine is used to construct 5371556Srgrimes * the third argument to execve when executing a program. 5381556Srgrimes */ 5391556Srgrimes 5401556Srgrimeschar ** 54190111Simpenvironment(void) 54290111Simp{ 5431556Srgrimes int nenv; 5441556Srgrimes struct var **vpp; 5451556Srgrimes struct var *vp; 5461556Srgrimes char **env, **ep; 5471556Srgrimes 5481556Srgrimes nenv = 0; 5491556Srgrimes for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) { 5501556Srgrimes for (vp = *vpp ; vp ; vp = vp->next) 5511556Srgrimes if (vp->flags & VEXPORT) 5521556Srgrimes nenv++; 5531556Srgrimes } 5541556Srgrimes ep = env = stalloc((nenv + 1) * sizeof *env); 5551556Srgrimes for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) { 5561556Srgrimes for (vp = *vpp ; vp ; vp = vp->next) 5571556Srgrimes if (vp->flags & VEXPORT) 5581556Srgrimes *ep++ = vp->text; 5591556Srgrimes } 5601556Srgrimes *ep = NULL; 5611556Srgrimes return env; 5621556Srgrimes} 5631556Srgrimes 5641556Srgrimes 565213811Sobrienstatic int 566158145Sstefanfvar_compare(const void *a, const void *b) 567158145Sstefanf{ 568158145Sstefanf const char *const *sa, *const *sb; 5691556Srgrimes 570158145Sstefanf sa = a; 571158145Sstefanf sb = b; 572158145Sstefanf /* 573158145Sstefanf * This compares two var=value strings which creates a different 574158145Sstefanf * order from what you would probably expect. POSIX is somewhat 575158145Sstefanf * ambiguous on what should be sorted exactly. 576158145Sstefanf */ 577158145Sstefanf return strcoll(*sa, *sb); 578158145Sstefanf} 579158145Sstefanf 580158145Sstefanf 5811556Srgrimes/* 582217847Sjilles * Command to list all variables which are set. This is invoked from the 583217847Sjilles * set command when it is called without any options or operands. 5841556Srgrimes */ 5851556Srgrimes 5861556Srgrimesint 58790111Simpshowvarscmd(int argc __unused, char **argv __unused) 58817987Speter{ 5891556Srgrimes struct var **vpp; 5901556Srgrimes struct var *vp; 59197915Stjr const char *s; 592158145Sstefanf const char **vars; 593158145Sstefanf int i, n; 5941556Srgrimes 595158145Sstefanf /* 596158145Sstefanf * POSIX requires us to sort the variables. 597158145Sstefanf */ 598158145Sstefanf n = 0; 599158145Sstefanf for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) { 600158145Sstefanf for (vp = *vpp; vp; vp = vp->next) { 601158145Sstefanf if (!(vp->flags & VUNSET)) 602158145Sstefanf n++; 6031556Srgrimes } 6041556Srgrimes } 605158145Sstefanf 606231001Sjilles INTOFF; 607158145Sstefanf vars = ckmalloc(n * sizeof(*vars)); 608158145Sstefanf i = 0; 609158145Sstefanf for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) { 610158145Sstefanf for (vp = *vpp; vp; vp = vp->next) { 611158145Sstefanf if (!(vp->flags & VUNSET)) 612158145Sstefanf vars[i++] = vp->text; 613158145Sstefanf } 614158145Sstefanf } 615158145Sstefanf 616158145Sstefanf qsort(vars, n, sizeof(*vars), var_compare); 617158145Sstefanf for (i = 0; i < n; i++) { 618223183Sjilles /* 619223183Sjilles * Skip improper variable names so the output remains usable as 620223183Sjilles * shell input. 621223183Sjilles */ 622223183Sjilles if (!isassignment(vars[i])) 623223183Sjilles continue; 624215567Sjilles s = strchr(vars[i], '='); 625215567Sjilles s++; 626215567Sjilles outbin(vars[i], s - vars[i], out1); 627215567Sjilles out1qstr(s); 628158145Sstefanf out1c('\n'); 629158145Sstefanf } 630158145Sstefanf ckfree(vars); 631231001Sjilles INTON; 632158145Sstefanf 6331556Srgrimes return 0; 6341556Srgrimes} 6351556Srgrimes 6361556Srgrimes 6371556Srgrimes 6381556Srgrimes/* 6391556Srgrimes * The export and readonly commands. 6401556Srgrimes */ 6411556Srgrimes 6421556Srgrimesint 643240541Sjillesexportcmd(int argc __unused, char **argv) 64417987Speter{ 6451556Srgrimes struct var **vpp; 6461556Srgrimes struct var *vp; 647240541Sjilles char **ap; 6481556Srgrimes char *name; 6491556Srgrimes char *p; 65097914Stjr char *cmdname; 65197914Stjr int ch, values; 6521556Srgrimes int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT; 6531556Srgrimes 65497914Stjr cmdname = argv[0]; 65597914Stjr values = 0; 656240541Sjilles while ((ch = nextopt("p")) != '\0') { 65797914Stjr switch (ch) { 65897914Stjr case 'p': 65997914Stjr values = 1; 66097914Stjr break; 66197914Stjr } 66297914Stjr } 66397914Stjr 664240541Sjilles if (values && *argptr != NULL) 665149919Sstefanf error("-p requires no arguments"); 666240541Sjilles if (*argptr != NULL) { 667240541Sjilles for (ap = argptr; (name = *ap) != NULL; ap++) { 6681556Srgrimes if ((p = strchr(name, '=')) != NULL) { 6691556Srgrimes p++; 6701556Srgrimes } else { 671221668Sjilles vp = find_var(name, NULL, NULL); 672221668Sjilles if (vp != NULL) { 673221668Sjilles vp->flags |= flag; 674221668Sjilles if ((vp->flags & VEXPORT) && localevar(vp->text)) { 675221668Sjilles change_env(vp->text, 1); 676221668Sjilles (void) setlocale(LC_ALL, ""); 677221668Sjilles updatecharset(); 6781556Srgrimes } 679221668Sjilles continue; 6801556Srgrimes } 6811556Srgrimes } 6821556Srgrimes setvar(name, p, flag); 6831556Srgrimes } 6841556Srgrimes } else { 6851556Srgrimes for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) { 6861556Srgrimes for (vp = *vpp ; vp ; vp = vp->next) { 6871556Srgrimes if (vp->flags & flag) { 68897914Stjr if (values) { 689223183Sjilles /* 690223183Sjilles * Skip improper variable names 691223183Sjilles * so the output remains usable 692223183Sjilles * as shell input. 693223183Sjilles */ 694223183Sjilles if (!isassignment(vp->text)) 695223183Sjilles continue; 69697914Stjr out1str(cmdname); 69797914Stjr out1c(' '); 69897914Stjr } 69997914Stjr if (values && !(vp->flags & VUNSET)) { 700221975Sjilles outbin(vp->text, 701221975Sjilles vp->name_len + 1, out1); 702221975Sjilles out1qstr(vp->text + 703221975Sjilles vp->name_len + 1); 704215567Sjilles } else 705221975Sjilles outbin(vp->text, vp->name_len, 706215567Sjilles out1); 7071556Srgrimes out1c('\n'); 7081556Srgrimes } 7091556Srgrimes } 7101556Srgrimes } 7111556Srgrimes } 7121556Srgrimes return 0; 7131556Srgrimes} 7141556Srgrimes 7151556Srgrimes 7161556Srgrimes/* 7171556Srgrimes * The "local" command. 7181556Srgrimes */ 7191556Srgrimes 72017987Speterint 72190111Simplocalcmd(int argc __unused, char **argv __unused) 72217987Speter{ 7231556Srgrimes char *name; 7241556Srgrimes 7251556Srgrimes if (! in_function()) 7261556Srgrimes error("Not in a function"); 7271556Srgrimes while ((name = *argptr++) != NULL) { 7281556Srgrimes mklocal(name); 7291556Srgrimes } 7301556Srgrimes return 0; 7311556Srgrimes} 7321556Srgrimes 7331556Srgrimes 7341556Srgrimes/* 7351556Srgrimes * Make a variable a local variable. When a variable is made local, it's 7361556Srgrimes * value and flags are saved in a localvar structure. The saved values 7371556Srgrimes * will be restored when the shell function returns. We handle the name 7381556Srgrimes * "-" as a special case. 7391556Srgrimes */ 7401556Srgrimes 7411556Srgrimesvoid 74290111Simpmklocal(char *name) 74390111Simp{ 7441556Srgrimes struct localvar *lvp; 7451556Srgrimes struct var **vpp; 7461556Srgrimes struct var *vp; 7471556Srgrimes 7481556Srgrimes INTOFF; 7491556Srgrimes lvp = ckmalloc(sizeof (struct localvar)); 7501556Srgrimes if (name[0] == '-' && name[1] == '\0') { 7511556Srgrimes lvp->text = ckmalloc(sizeof optlist); 75217987Speter memcpy(lvp->text, optlist, sizeof optlist); 7531556Srgrimes vp = NULL; 7541556Srgrimes } else { 755221668Sjilles vp = find_var(name, &vpp, NULL); 7561556Srgrimes if (vp == NULL) { 7571556Srgrimes if (strchr(name, '=')) 758223024Sjilles setvareq(savestr(name), VSTRFIXED | VNOLOCAL); 7591556Srgrimes else 760223024Sjilles setvar(name, NULL, VSTRFIXED | VNOLOCAL); 7611556Srgrimes vp = *vpp; /* the new variable */ 7621556Srgrimes lvp->text = NULL; 7631556Srgrimes lvp->flags = VUNSET; 7641556Srgrimes } else { 7651556Srgrimes lvp->text = vp->text; 7661556Srgrimes lvp->flags = vp->flags; 7671556Srgrimes vp->flags |= VSTRFIXED|VTEXTFIXED; 768221668Sjilles if (name[vp->name_len] == '=') 769223024Sjilles setvareq(savestr(name), VNOLOCAL); 7701556Srgrimes } 7711556Srgrimes } 7721556Srgrimes lvp->vp = vp; 7731556Srgrimes lvp->next = localvars; 7741556Srgrimes localvars = lvp; 7751556Srgrimes INTON; 7761556Srgrimes} 7771556Srgrimes 7781556Srgrimes 7791556Srgrimes/* 7801556Srgrimes * Called after a function returns. 7811556Srgrimes */ 7821556Srgrimes 7831556Srgrimesvoid 78490111Simppoplocalvars(void) 78590111Simp{ 7861556Srgrimes struct localvar *lvp; 7871556Srgrimes struct var *vp; 7881556Srgrimes 7891556Srgrimes while ((lvp = localvars) != NULL) { 7901556Srgrimes localvars = lvp->next; 7911556Srgrimes vp = lvp->vp; 7921556Srgrimes if (vp == NULL) { /* $- saved */ 79317987Speter memcpy(optlist, lvp->text, sizeof optlist); 7941556Srgrimes ckfree(lvp->text); 795215266Sjilles optschanged(); 7961556Srgrimes } else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) { 7971556Srgrimes (void)unsetvar(vp->text); 7981556Srgrimes } else { 7991556Srgrimes if ((vp->flags & VTEXTFIXED) == 0) 8001556Srgrimes ckfree(vp->text); 8011556Srgrimes vp->flags = lvp->flags; 8021556Srgrimes vp->text = lvp->text; 8031556Srgrimes } 8041556Srgrimes ckfree(lvp); 8051556Srgrimes } 8061556Srgrimes} 8071556Srgrimes 8081556Srgrimes 80917987Speterint 81090111Simpsetvarcmd(int argc, char **argv) 81117987Speter{ 8121556Srgrimes if (argc <= 2) 8131556Srgrimes return unsetcmd(argc, argv); 8141556Srgrimes else if (argc == 3) 8151556Srgrimes setvar(argv[1], argv[2], 0); 8161556Srgrimes else 817214538Sjilles error("too many arguments"); 8181556Srgrimes return 0; 8191556Srgrimes} 8201556Srgrimes 8211556Srgrimes 8221556Srgrimes/* 823217847Sjilles * The unset builtin command. 8241556Srgrimes */ 8251556Srgrimes 82617987Speterint 82790111Simpunsetcmd(int argc __unused, char **argv __unused) 82817987Speter{ 8291556Srgrimes char **ap; 8301556Srgrimes int i; 8311556Srgrimes int flg_func = 0; 8321556Srgrimes int flg_var = 0; 8331556Srgrimes int ret = 0; 8341556Srgrimes 8351556Srgrimes while ((i = nextopt("vf")) != '\0') { 8361556Srgrimes if (i == 'f') 8371556Srgrimes flg_func = 1; 8381556Srgrimes else 8391556Srgrimes flg_var = 1; 8401556Srgrimes } 8411556Srgrimes if (flg_func == 0 && flg_var == 0) 8421556Srgrimes flg_var = 1; 8438855Srgrimes 8441556Srgrimes for (ap = argptr; *ap ; ap++) { 8451556Srgrimes if (flg_func) 8461556Srgrimes ret |= unsetfunc(*ap); 8471556Srgrimes if (flg_var) 8481556Srgrimes ret |= unsetvar(*ap); 8491556Srgrimes } 8501556Srgrimes return ret; 8511556Srgrimes} 8521556Srgrimes 8531556Srgrimes 8541556Srgrimes/* 8551556Srgrimes * Unset the specified variable. 8561556Srgrimes */ 8571556Srgrimes 85820425Ssteveint 859200956Sjillesunsetvar(const char *s) 86090111Simp{ 8611556Srgrimes struct var **vpp; 8621556Srgrimes struct var *vp; 8631556Srgrimes 864221668Sjilles vp = find_var(s, &vpp, NULL); 865221668Sjilles if (vp == NULL) 866221668Sjilles return (0); 867221668Sjilles if (vp->flags & VREADONLY) 868221668Sjilles return (1); 869221668Sjilles INTOFF; 870221668Sjilles if (vp->text[vp->name_len + 1] != '\0') 871221668Sjilles setvar(s, nullstr, 0); 872221668Sjilles if ((vp->flags & VEXPORT) && localevar(vp->text)) { 873221668Sjilles change_env(s, 0); 874221668Sjilles setlocale(LC_ALL, ""); 875221668Sjilles updatecharset(); 8761556Srgrimes } 877221668Sjilles vp->flags &= ~VEXPORT; 878221668Sjilles vp->flags |= VUNSET; 879221668Sjilles if ((vp->flags & VSTRFIXED) == 0) { 880221668Sjilles if ((vp->flags & VTEXTFIXED) == 0) 881221668Sjilles ckfree(vp->text); 882221668Sjilles *vpp = vp->next; 883221668Sjilles ckfree(vp); 884221668Sjilles } 885221668Sjilles INTON; 886135856Sdes return (0); 8871556Srgrimes} 8881556Srgrimes 8891556Srgrimes 8901556Srgrimes 8911556Srgrimes/* 8921556Srgrimes * Returns true if the two strings specify the same varable. The first 8931556Srgrimes * variable name is terminated by '='; the second may be terminated by 8941556Srgrimes * either '=' or '\0'. 8951556Srgrimes */ 8961556Srgrimes 897213811Sobrienstatic int 898200956Sjillesvarequal(const char *p, const char *q) 89990111Simp{ 9001556Srgrimes while (*p == *q++) { 9011556Srgrimes if (*p++ == '=') 9021556Srgrimes return 1; 9031556Srgrimes } 9041556Srgrimes if (*p == '=' && *(q - 1) == '\0') 9051556Srgrimes return 1; 9061556Srgrimes return 0; 9071556Srgrimes} 908221668Sjilles 909221668Sjilles/* 910221668Sjilles * Search for a variable. 911221668Sjilles * 'name' may be terminated by '=' or a NUL. 912221668Sjilles * vppp is set to the pointer to vp, or the list head if vp isn't found 913221668Sjilles * lenp is set to the number of charactets in 'name' 914221668Sjilles */ 915221668Sjilles 916221668Sjillesstatic struct var * 917221668Sjillesfind_var(const char *name, struct var ***vppp, int *lenp) 918221668Sjilles{ 919221668Sjilles unsigned int hashval; 920221668Sjilles int len; 921221668Sjilles struct var *vp, **vpp; 922221668Sjilles const char *p = name; 923221668Sjilles 924221668Sjilles hashval = 0; 925221668Sjilles while (*p && *p != '=') 926221668Sjilles hashval = 2 * hashval + (unsigned char)*p++; 927221668Sjilles len = p - name; 928221668Sjilles 929221668Sjilles if (lenp) 930221668Sjilles *lenp = len; 931221668Sjilles vpp = &vartab[hashval % VTABSIZE]; 932221668Sjilles if (vppp) 933221668Sjilles *vppp = vpp; 934221668Sjilles 935221668Sjilles for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) { 936221668Sjilles if (vp->name_len != len) 937221668Sjilles continue; 938221668Sjilles if (memcmp(vp->text, name, len) != 0) 939221668Sjilles continue; 940221668Sjilles if (vppp) 941221668Sjilles *vppp = vpp; 942221668Sjilles return vp; 943221668Sjilles } 944221668Sjilles return NULL; 945221668Sjilles} 946