var.c revision 223024
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 223024 2011-06-12 23:06:04Z 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" 6617987Speter#ifndef NO_HISTORY 6717987Speter#include "myhistedit.h" 6817987Speter#endif 691556Srgrimes 701556Srgrimes 711556Srgrimes#define VTABSIZE 39 721556Srgrimes 731556Srgrimes 741556Srgrimesstruct varinit { 751556Srgrimes struct var *var; 761556Srgrimes int flags; 77201056Sjilles const char *text; 7890111Simp void (*func)(const char *); 791556Srgrimes}; 801556Srgrimes 811556Srgrimes 8217987Speter#ifndef NO_HISTORY 831556Srgrimesstruct var vhistsize; 84208755Sjillesstruct var vterm; 8517987Speter#endif 861556Srgrimesstruct var vifs; 871556Srgrimesstruct var vmail; 881556Srgrimesstruct var vmpath; 891556Srgrimesstruct var vpath; 9097689Stjrstruct var vppid; 911556Srgrimesstruct var vps1; 921556Srgrimesstruct var vps2; 93159632Sstefanfstruct var vps4; 941556Srgrimesstruct var vvers; 95213760Sobrienstatic struct var voptind; 961556Srgrimes 97223024Sjillesint forcelocal; 98223024Sjilles 99213760Sobrienstatic const struct varinit varinit[] = { 10017987Speter#ifndef NO_HISTORY 101201056Sjilles { &vhistsize, VUNSET, "HISTSIZE=", 10220425Ssteve sethistsize }, 10317987Speter#endif 104201056Sjilles { &vifs, 0, "IFS= \t\n", 10520425Ssteve NULL }, 106201056Sjilles { &vmail, VUNSET, "MAIL=", 10720425Ssteve NULL }, 108201056Sjilles { &vmpath, VUNSET, "MAILPATH=", 10920425Ssteve NULL }, 110201056Sjilles { &vpath, 0, "PATH=" _PATH_DEFPATH, 11120425Ssteve changepath }, 112201056Sjilles { &vppid, VUNSET, "PPID=", 11397689Stjr NULL }, 1148855Srgrimes /* 1151556Srgrimes * vps1 depends on uid 1161556Srgrimes */ 117201056Sjilles { &vps2, 0, "PS2=> ", 11820425Ssteve NULL }, 119201056Sjilles { &vps4, 0, "PS4=+ ", 120159632Sstefanf NULL }, 121208755Sjilles#ifndef NO_HISTORY 122208755Sjilles { &vterm, VUNSET, "TERM=", 123208755Sjilles setterm }, 124208755Sjilles#endif 125201056Sjilles { &voptind, 0, "OPTIND=1", 12620425Ssteve getoptsreset }, 12720425Ssteve { NULL, 0, NULL, 12820425Ssteve NULL } 1291556Srgrimes}; 1301556Srgrimes 131213760Sobrienstatic struct var *vartab[VTABSIZE]; 1321556Srgrimes 133213760Sobrienstatic const char *const locale_names[7] = { 134207678Sjilles "LC_COLLATE", "LC_CTYPE", "LC_MONETARY", 135207678Sjilles "LC_NUMERIC", "LC_TIME", "LC_MESSAGES", NULL 136207678Sjilles}; 137213760Sobrienstatic const int locale_categories[7] = { 138207678Sjilles LC_COLLATE, LC_CTYPE, LC_MONETARY, LC_NUMERIC, LC_TIME, LC_MESSAGES, 0 139207678Sjilles}; 140207678Sjilles 141213811Sobrienstatic int varequal(const char *, const char *); 142221668Sjillesstatic struct var *find_var(const char *, struct var ***, int *); 143213811Sobrienstatic int localevar(const char *); 1441556Srgrimes 1451556Srgrimes/* 146155302Sschweikh * Initialize the variable symbol tables and import the environment. 1471556Srgrimes */ 1481556Srgrimes 1491556Srgrimes#ifdef mkinit 1501556SrgrimesINCLUDE "var.h" 151201053SjillesMKINIT char **environ; 1521556SrgrimesINIT { 1531556Srgrimes char **envp; 1541556Srgrimes 1551556Srgrimes initvar(); 1561556Srgrimes for (envp = environ ; *envp ; envp++) { 1571556Srgrimes if (strchr(*envp, '=')) { 1581556Srgrimes setvareq(*envp, VEXPORT|VTEXTFIXED); 1591556Srgrimes } 1601556Srgrimes } 1611556Srgrimes} 1621556Srgrimes#endif 1631556Srgrimes 1641556Srgrimes 1651556Srgrimes/* 1661556Srgrimes * This routine initializes the builtin variables. It is called when the 167218306Sjilles * shell is initialized. 1681556Srgrimes */ 1691556Srgrimes 1701556Srgrimesvoid 17190111Simpinitvar(void) 17290111Simp{ 17397689Stjr char ppid[20]; 1741556Srgrimes const struct varinit *ip; 1751556Srgrimes struct var *vp; 1761556Srgrimes struct var **vpp; 1771556Srgrimes 1781556Srgrimes for (ip = varinit ; (vp = ip->var) != NULL ; ip++) { 179221668Sjilles if (find_var(ip->text, &vpp, &vp->name_len) != NULL) 180221668Sjilles continue; 181221668Sjilles vp->next = *vpp; 182221668Sjilles *vpp = vp; 183221668Sjilles vp->text = __DECONST(char *, ip->text); 184221668Sjilles vp->flags = ip->flags | VSTRFIXED | VTEXTFIXED; 185221668Sjilles vp->func = ip->func; 1861556Srgrimes } 1871556Srgrimes /* 1881556Srgrimes * PS1 depends on uid 1891556Srgrimes */ 190221668Sjilles if (find_var("PS1", &vpp, &vps1.name_len) == NULL) { 1911556Srgrimes vps1.next = *vpp; 1921556Srgrimes *vpp = &vps1; 193201056Sjilles vps1.text = __DECONST(char *, geteuid() ? "PS1=$ " : "PS1=# "); 1941556Srgrimes vps1.flags = VSTRFIXED|VTEXTFIXED; 1951556Srgrimes } 19697689Stjr if ((vppid.flags & VEXPORT) == 0) { 19797689Stjr fmtstr(ppid, sizeof(ppid), "%d", (int)getppid()); 19897689Stjr setvarsafe("PPID", ppid, 0); 19997689Stjr } 2001556Srgrimes} 2011556Srgrimes 2021556Srgrimes/* 20320425Ssteve * Safe version of setvar, returns 1 on success 0 on failure. 20420425Ssteve */ 20520425Ssteve 20620425Ssteveint 207200956Sjillessetvarsafe(const char *name, const char *val, int flags) 20820425Ssteve{ 20920425Ssteve struct jmploc jmploc; 210194765Sjilles struct jmploc *const savehandler = handler; 21120425Ssteve int err = 0; 212199660Sjilles int inton; 21320425Ssteve 214199660Sjilles inton = is_int_on(); 21520425Ssteve if (setjmp(jmploc.loc)) 21620425Ssteve err = 1; 21720425Ssteve else { 21820425Ssteve handler = &jmploc; 21920425Ssteve setvar(name, val, flags); 22020425Ssteve } 22120425Ssteve handler = savehandler; 222199660Sjilles SETINTON(inton); 22320425Ssteve return err; 22420425Ssteve} 22520425Ssteve 22620425Ssteve/* 227155302Sschweikh * Set the value of a variable. The flags argument is stored with the 2281556Srgrimes * flags of the variable. If val is NULL, the variable is unset. 2291556Srgrimes */ 2301556Srgrimes 2311556Srgrimesvoid 232200956Sjillessetvar(const char *name, const char *val, int flags) 23317987Speter{ 234200956Sjilles const char *p; 2351556Srgrimes int len; 2361556Srgrimes int namelen; 2371556Srgrimes char *nameeq; 2381556Srgrimes int isbad; 2391556Srgrimes 2401556Srgrimes isbad = 0; 2411556Srgrimes p = name; 24217525Sache if (! is_name(*p)) 2431556Srgrimes isbad = 1; 24417525Sache p++; 2451556Srgrimes for (;;) { 2461556Srgrimes if (! is_in_name(*p)) { 2471556Srgrimes if (*p == '\0' || *p == '=') 2481556Srgrimes break; 2491556Srgrimes isbad = 1; 2501556Srgrimes } 2511556Srgrimes p++; 2521556Srgrimes } 2531556Srgrimes namelen = p - name; 2541556Srgrimes if (isbad) 2551556Srgrimes error("%.*s: bad variable name", namelen, name); 2561556Srgrimes len = namelen + 2; /* 2 is space for '=' and '\0' */ 2571556Srgrimes if (val == NULL) { 2581556Srgrimes flags |= VUNSET; 2591556Srgrimes } else { 2601556Srgrimes len += strlen(val); 2611556Srgrimes } 262200956Sjilles nameeq = ckmalloc(len); 263200956Sjilles memcpy(nameeq, name, namelen); 264200956Sjilles nameeq[namelen] = '='; 2651556Srgrimes if (val) 266200956Sjilles scopy(val, nameeq + namelen + 1); 267200956Sjilles else 268200956Sjilles nameeq[namelen + 1] = '\0'; 2691556Srgrimes setvareq(nameeq, flags); 2701556Srgrimes} 2711556Srgrimes 272213811Sobrienstatic int 273200956Sjilleslocalevar(const char *s) 27490111Simp{ 275207678Sjilles const char *const *ss; 2761556Srgrimes 27717525Sache if (*s != 'L') 27817525Sache return 0; 27917525Sache if (varequal(s + 1, "ANG")) 28017525Sache return 1; 28117525Sache if (strncmp(s + 1, "C_", 2) != 0) 28217525Sache return 0; 283207678Sjilles if (varequal(s + 3, "ALL")) 284207678Sjilles return 1; 285207678Sjilles for (ss = locale_names; *ss ; ss++) 286207678Sjilles if (varequal(s + 3, *ss + 3)) 28717525Sache return 1; 28817525Sache return 0; 28917525Sache} 2901556Srgrimes 291171268Sscf 2921556Srgrimes/* 293171268Sscf * Sets/unsets an environment variable from a pointer that may actually be a 294171268Sscf * pointer into environ where the string should not be manipulated. 295171268Sscf */ 296213811Sobrienstatic void 297200956Sjilleschange_env(const char *s, int set) 298171268Sscf{ 299171268Sscf char *eqp; 300171268Sscf char *ss; 301171268Sscf 302171268Sscf ss = savestr(s); 303171268Sscf if ((eqp = strchr(ss, '=')) != NULL) 304171268Sscf *eqp = '\0'; 305171268Sscf if (set && eqp != NULL) 306171268Sscf (void) setenv(ss, eqp + 1, 1); 307171268Sscf else 308171268Sscf (void) unsetenv(ss); 309171268Sscf ckfree(ss); 310171268Sscf 311171268Sscf return; 312171268Sscf} 313171268Sscf 314171268Sscf 315171268Sscf/* 3161556Srgrimes * Same as setvar except that the variable and value are passed in 3171556Srgrimes * the first argument as name=value. Since the first argument will 3181556Srgrimes * be actually stored in the table, it should not be a string that 3191556Srgrimes * will go away. 3201556Srgrimes */ 3211556Srgrimes 3221556Srgrimesvoid 32390111Simpsetvareq(char *s, int flags) 32417987Speter{ 3251556Srgrimes struct var *vp, **vpp; 326221668Sjilles int nlen; 3271556Srgrimes 32845263Scracauer if (aflag) 32945263Scracauer flags |= VEXPORT; 330223024Sjilles if (forcelocal && !(flags & (VNOSET | VNOLOCAL))) 331223024Sjilles mklocal(s); 332221668Sjilles vp = find_var(s, &vpp, &nlen); 333221668Sjilles if (vp != NULL) { 334221668Sjilles if (vp->flags & VREADONLY) 335221668Sjilles error("%.*s: is read only", vp->name_len, s); 336221668Sjilles if (flags & VNOSET) 337221668Sjilles return; 338221668Sjilles INTOFF; 33920425Ssteve 340221668Sjilles if (vp->func && (flags & VNOFUNC) == 0) 341221668Sjilles (*vp->func)(s + vp->name_len + 1); 34220425Ssteve 343221668Sjilles if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0) 344221668Sjilles ckfree(vp->text); 34520425Ssteve 346221668Sjilles vp->flags &= ~(VTEXTFIXED|VSTACK|VUNSET); 347221668Sjilles vp->flags |= flags; 348221668Sjilles vp->text = s; 34920425Ssteve 350221668Sjilles /* 351221668Sjilles * We could roll this to a function, to handle it as 352221668Sjilles * a regular variable function callback, but why bother? 353221668Sjilles * 354221668Sjilles * Note: this assumes iflag is not set to 1 initially. 355221668Sjilles * As part of init(), this is called before arguments 356221668Sjilles * are looked at. 357221668Sjilles */ 358221668Sjilles if ((vp == &vmpath || (vp == &vmail && ! mpathset())) && 359221668Sjilles iflag == 1) 360221668Sjilles chkmail(1); 361221668Sjilles if ((vp->flags & VEXPORT) && localevar(s)) { 362221668Sjilles change_env(s, 1); 363221668Sjilles (void) setlocale(LC_ALL, ""); 364221668Sjilles updatecharset(); 3651556Srgrimes } 366221668Sjilles INTON; 367221668Sjilles return; 3681556Srgrimes } 3691556Srgrimes /* not found */ 370216870Sjilles if (flags & VNOSET) 371216870Sjilles return; 3721556Srgrimes vp = ckmalloc(sizeof (*vp)); 3731556Srgrimes vp->flags = flags; 3741556Srgrimes vp->text = s; 375221668Sjilles vp->name_len = nlen; 3761556Srgrimes vp->next = *vpp; 37720425Ssteve vp->func = NULL; 37817525Sache INTOFF; 3791556Srgrimes *vpp = vp; 38017525Sache if ((vp->flags & VEXPORT) && localevar(s)) { 381171268Sscf change_env(s, 1); 38217525Sache (void) setlocale(LC_ALL, ""); 383221559Sjilles updatecharset(); 38417525Sache } 38517525Sache INTON; 3861556Srgrimes} 3871556Srgrimes 3881556Srgrimes 3891556Srgrimes 3901556Srgrimes/* 3911556Srgrimes * Process a linked list of variable assignments. 3921556Srgrimes */ 3931556Srgrimes 3941556Srgrimesvoid 395216870Sjilleslistsetvar(struct strlist *list, int flags) 39690111Simp{ 3971556Srgrimes struct strlist *lp; 3981556Srgrimes 3991556Srgrimes INTOFF; 4001556Srgrimes for (lp = list ; lp ; lp = lp->next) { 401216870Sjilles setvareq(savestr(lp->text), flags); 4021556Srgrimes } 4031556Srgrimes INTON; 4041556Srgrimes} 4051556Srgrimes 4061556Srgrimes 4071556Srgrimes 4081556Srgrimes/* 4091556Srgrimes * Find the value of a variable. Returns NULL if not set. 4101556Srgrimes */ 4111556Srgrimes 4121556Srgrimeschar * 413200956Sjilleslookupvar(const char *name) 41490111Simp{ 4151556Srgrimes struct var *v; 4161556Srgrimes 417221668Sjilles v = find_var(name, NULL, NULL); 418221668Sjilles if (v == NULL || v->flags & VUNSET) 419221668Sjilles return NULL; 420221668Sjilles return v->text + v->name_len + 1; 4211556Srgrimes} 4221556Srgrimes 4231556Srgrimes 4241556Srgrimes 4251556Srgrimes/* 4261556Srgrimes * Search the environment of a builtin command. If the second argument 4271556Srgrimes * is nonzero, return the value of a variable even if it hasn't been 4281556Srgrimes * exported. 4291556Srgrimes */ 4301556Srgrimes 4311556Srgrimeschar * 432200956Sjillesbltinlookup(const char *name, int doall) 43317987Speter{ 4341556Srgrimes struct strlist *sp; 4351556Srgrimes struct var *v; 436212467Sjilles char *result; 4371556Srgrimes 438212467Sjilles result = NULL; 4391556Srgrimes for (sp = cmdenviron ; sp ; sp = sp->next) { 4401556Srgrimes if (varequal(sp->text, name)) 441212467Sjilles result = strchr(sp->text, '=') + 1; 4421556Srgrimes } 443212467Sjilles if (result != NULL) 444212467Sjilles return result; 445221668Sjilles 446221668Sjilles v = find_var(name, NULL, NULL); 447221668Sjilles if (v == NULL || v->flags & VUNSET || 448221668Sjilles (!doall && (v->flags & VEXPORT) == 0)) 449221668Sjilles return NULL; 450221668Sjilles return v->text + v->name_len + 1; 4511556Srgrimes} 4521556Srgrimes 4531556Srgrimes 454207678Sjilles/* 455207678Sjilles * Set up locale for a builtin (LANG/LC_* assignments). 456207678Sjilles */ 457207678Sjillesvoid 458207678Sjillesbltinsetlocale(void) 459207678Sjilles{ 460207678Sjilles struct strlist *lp; 461207678Sjilles int act = 0; 462207678Sjilles char *loc, *locdef; 463207678Sjilles int i; 4641556Srgrimes 465207678Sjilles for (lp = cmdenviron ; lp ; lp = lp->next) { 466207678Sjilles if (localevar(lp->text)) { 467207678Sjilles act = 1; 468207678Sjilles break; 469207678Sjilles } 470207678Sjilles } 471207678Sjilles if (!act) 472207678Sjilles return; 473207678Sjilles loc = bltinlookup("LC_ALL", 0); 474207678Sjilles INTOFF; 475207678Sjilles if (loc != NULL) { 476207678Sjilles setlocale(LC_ALL, loc); 477207678Sjilles INTON; 478221559Sjilles updatecharset(); 479207678Sjilles return; 480207678Sjilles } 481207678Sjilles locdef = bltinlookup("LANG", 0); 482207678Sjilles for (i = 0; locale_names[i] != NULL; i++) { 483207678Sjilles loc = bltinlookup(locale_names[i], 0); 484207678Sjilles if (loc == NULL) 485207678Sjilles loc = locdef; 486207678Sjilles if (loc != NULL) 487207678Sjilles setlocale(locale_categories[i], loc); 488207678Sjilles } 489207678Sjilles INTON; 490221559Sjilles updatecharset(); 491207678Sjilles} 492207678Sjilles 4931556Srgrimes/* 494207678Sjilles * Undo the effect of bltinlocaleset(). 495207678Sjilles */ 496207678Sjillesvoid 497207678Sjillesbltinunsetlocale(void) 498207678Sjilles{ 499207678Sjilles struct strlist *lp; 500207678Sjilles 501207678Sjilles INTOFF; 502207678Sjilles for (lp = cmdenviron ; lp ; lp = lp->next) { 503207678Sjilles if (localevar(lp->text)) { 504207678Sjilles setlocale(LC_ALL, ""); 505221559Sjilles updatecharset(); 506207678Sjilles return; 507207678Sjilles } 508207678Sjilles } 509207678Sjilles INTON; 510207678Sjilles} 511207678Sjilles 512221559Sjilles/* 513221559Sjilles * Update the localeisutf8 flag. 514221559Sjilles */ 515221559Sjillesvoid 516221559Sjillesupdatecharset(void) 517221559Sjilles{ 518221559Sjilles char *charset; 519207678Sjilles 520221559Sjilles charset = nl_langinfo(CODESET); 521221559Sjilles localeisutf8 = !strcmp(charset, "UTF-8"); 522221559Sjilles} 523221559Sjilles 524221669Sjillesvoid 525221669Sjillesinitcharset(void) 526221669Sjilles{ 527221669Sjilles updatecharset(); 528221669Sjilles initial_localeisutf8 = localeisutf8; 529221669Sjilles} 530221669Sjilles 531207678Sjilles/* 5321556Srgrimes * Generate a list of exported variables. This routine is used to construct 5331556Srgrimes * the third argument to execve when executing a program. 5341556Srgrimes */ 5351556Srgrimes 5361556Srgrimeschar ** 53790111Simpenvironment(void) 53890111Simp{ 5391556Srgrimes int nenv; 5401556Srgrimes struct var **vpp; 5411556Srgrimes struct var *vp; 5421556Srgrimes char **env, **ep; 5431556Srgrimes 5441556Srgrimes nenv = 0; 5451556Srgrimes for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) { 5461556Srgrimes for (vp = *vpp ; vp ; vp = vp->next) 5471556Srgrimes if (vp->flags & VEXPORT) 5481556Srgrimes nenv++; 5491556Srgrimes } 5501556Srgrimes ep = env = stalloc((nenv + 1) * sizeof *env); 5511556Srgrimes for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) { 5521556Srgrimes for (vp = *vpp ; vp ; vp = vp->next) 5531556Srgrimes if (vp->flags & VEXPORT) 5541556Srgrimes *ep++ = vp->text; 5551556Srgrimes } 5561556Srgrimes *ep = NULL; 5571556Srgrimes return env; 5581556Srgrimes} 5591556Srgrimes 5601556Srgrimes 561213811Sobrienstatic int 562158145Sstefanfvar_compare(const void *a, const void *b) 563158145Sstefanf{ 564158145Sstefanf const char *const *sa, *const *sb; 5651556Srgrimes 566158145Sstefanf sa = a; 567158145Sstefanf sb = b; 568158145Sstefanf /* 569158145Sstefanf * This compares two var=value strings which creates a different 570158145Sstefanf * order from what you would probably expect. POSIX is somewhat 571158145Sstefanf * ambiguous on what should be sorted exactly. 572158145Sstefanf */ 573158145Sstefanf return strcoll(*sa, *sb); 574158145Sstefanf} 575158145Sstefanf 576158145Sstefanf 5771556Srgrimes/* 578217847Sjilles * Command to list all variables which are set. This is invoked from the 579217847Sjilles * set command when it is called without any options or operands. 5801556Srgrimes */ 5811556Srgrimes 5821556Srgrimesint 58390111Simpshowvarscmd(int argc __unused, char **argv __unused) 58417987Speter{ 5851556Srgrimes struct var **vpp; 5861556Srgrimes struct var *vp; 58797915Stjr const char *s; 588158145Sstefanf const char **vars; 589158145Sstefanf int i, n; 5901556Srgrimes 591158145Sstefanf /* 592158145Sstefanf * POSIX requires us to sort the variables. 593158145Sstefanf */ 594158145Sstefanf n = 0; 595158145Sstefanf for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) { 596158145Sstefanf for (vp = *vpp; vp; vp = vp->next) { 597158145Sstefanf if (!(vp->flags & VUNSET)) 598158145Sstefanf n++; 5991556Srgrimes } 6001556Srgrimes } 601158145Sstefanf 602158145Sstefanf INTON; 603158145Sstefanf vars = ckmalloc(n * sizeof(*vars)); 604158145Sstefanf i = 0; 605158145Sstefanf for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) { 606158145Sstefanf for (vp = *vpp; vp; vp = vp->next) { 607158145Sstefanf if (!(vp->flags & VUNSET)) 608158145Sstefanf vars[i++] = vp->text; 609158145Sstefanf } 610158145Sstefanf } 611158145Sstefanf 612158145Sstefanf qsort(vars, n, sizeof(*vars), var_compare); 613158145Sstefanf for (i = 0; i < n; i++) { 614215567Sjilles s = strchr(vars[i], '='); 615215567Sjilles s++; 616215567Sjilles outbin(vars[i], s - vars[i], out1); 617215567Sjilles out1qstr(s); 618158145Sstefanf out1c('\n'); 619158145Sstefanf } 620158145Sstefanf ckfree(vars); 621158145Sstefanf INTOFF; 622158145Sstefanf 6231556Srgrimes return 0; 6241556Srgrimes} 6251556Srgrimes 6261556Srgrimes 6271556Srgrimes 6281556Srgrimes/* 6291556Srgrimes * The export and readonly commands. 6301556Srgrimes */ 6311556Srgrimes 6321556Srgrimesint 63390111Simpexportcmd(int argc, char **argv) 63417987Speter{ 6351556Srgrimes struct var **vpp; 6361556Srgrimes struct var *vp; 6371556Srgrimes char *name; 6381556Srgrimes char *p; 63997914Stjr char *cmdname; 64097914Stjr int ch, values; 6411556Srgrimes int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT; 6421556Srgrimes 64397914Stjr cmdname = argv[0]; 64497914Stjr optreset = optind = 1; 645100663Stjr opterr = 0; 64697914Stjr values = 0; 64797914Stjr while ((ch = getopt(argc, argv, "p")) != -1) { 64897914Stjr switch (ch) { 64997914Stjr case 'p': 65097914Stjr values = 1; 65197914Stjr break; 65297914Stjr case '?': 65397914Stjr default: 65497914Stjr error("unknown option: -%c", optopt); 65597914Stjr } 65697914Stjr } 65797914Stjr argc -= optind; 65897914Stjr argv += optind; 65997914Stjr 660149919Sstefanf if (values && argc != 0) 661149919Sstefanf error("-p requires no arguments"); 66297914Stjr if (argc != 0) { 663149919Sstefanf while ((name = *argv++) != NULL) { 6641556Srgrimes if ((p = strchr(name, '=')) != NULL) { 6651556Srgrimes p++; 6661556Srgrimes } else { 667221668Sjilles vp = find_var(name, NULL, NULL); 668221668Sjilles if (vp != NULL) { 669221668Sjilles vp->flags |= flag; 670221668Sjilles if ((vp->flags & VEXPORT) && localevar(vp->text)) { 671221668Sjilles change_env(vp->text, 1); 672221668Sjilles (void) setlocale(LC_ALL, ""); 673221668Sjilles updatecharset(); 6741556Srgrimes } 675221668Sjilles continue; 6761556Srgrimes } 6771556Srgrimes } 6781556Srgrimes setvar(name, p, flag); 6791556Srgrimes } 6801556Srgrimes } else { 6811556Srgrimes for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) { 6821556Srgrimes for (vp = *vpp ; vp ; vp = vp->next) { 6831556Srgrimes if (vp->flags & flag) { 68497914Stjr if (values) { 68597914Stjr out1str(cmdname); 68697914Stjr out1c(' '); 68797914Stjr } 68897914Stjr if (values && !(vp->flags & VUNSET)) { 689221975Sjilles outbin(vp->text, 690221975Sjilles vp->name_len + 1, out1); 691221975Sjilles out1qstr(vp->text + 692221975Sjilles vp->name_len + 1); 693215567Sjilles } else 694221975Sjilles outbin(vp->text, vp->name_len, 695215567Sjilles out1); 6961556Srgrimes out1c('\n'); 6971556Srgrimes } 6981556Srgrimes } 6991556Srgrimes } 7001556Srgrimes } 7011556Srgrimes return 0; 7021556Srgrimes} 7031556Srgrimes 7041556Srgrimes 7051556Srgrimes/* 7061556Srgrimes * The "local" command. 7071556Srgrimes */ 7081556Srgrimes 70917987Speterint 71090111Simplocalcmd(int argc __unused, char **argv __unused) 71117987Speter{ 7121556Srgrimes char *name; 7131556Srgrimes 7141556Srgrimes if (! in_function()) 7151556Srgrimes error("Not in a function"); 7161556Srgrimes while ((name = *argptr++) != NULL) { 7171556Srgrimes mklocal(name); 7181556Srgrimes } 7191556Srgrimes return 0; 7201556Srgrimes} 7211556Srgrimes 7221556Srgrimes 7231556Srgrimes/* 7241556Srgrimes * Make a variable a local variable. When a variable is made local, it's 7251556Srgrimes * value and flags are saved in a localvar structure. The saved values 7261556Srgrimes * will be restored when the shell function returns. We handle the name 7271556Srgrimes * "-" as a special case. 7281556Srgrimes */ 7291556Srgrimes 7301556Srgrimesvoid 73190111Simpmklocal(char *name) 73290111Simp{ 7331556Srgrimes struct localvar *lvp; 7341556Srgrimes struct var **vpp; 7351556Srgrimes struct var *vp; 7361556Srgrimes 7371556Srgrimes INTOFF; 7381556Srgrimes lvp = ckmalloc(sizeof (struct localvar)); 7391556Srgrimes if (name[0] == '-' && name[1] == '\0') { 7401556Srgrimes lvp->text = ckmalloc(sizeof optlist); 74117987Speter memcpy(lvp->text, optlist, sizeof optlist); 7421556Srgrimes vp = NULL; 7431556Srgrimes } else { 744221668Sjilles vp = find_var(name, &vpp, NULL); 7451556Srgrimes if (vp == NULL) { 7461556Srgrimes if (strchr(name, '=')) 747223024Sjilles setvareq(savestr(name), VSTRFIXED | VNOLOCAL); 7481556Srgrimes else 749223024Sjilles setvar(name, NULL, VSTRFIXED | VNOLOCAL); 7501556Srgrimes vp = *vpp; /* the new variable */ 7511556Srgrimes lvp->text = NULL; 7521556Srgrimes lvp->flags = VUNSET; 7531556Srgrimes } else { 7541556Srgrimes lvp->text = vp->text; 7551556Srgrimes lvp->flags = vp->flags; 7561556Srgrimes vp->flags |= VSTRFIXED|VTEXTFIXED; 757221668Sjilles if (name[vp->name_len] == '=') 758223024Sjilles setvareq(savestr(name), VNOLOCAL); 7591556Srgrimes } 7601556Srgrimes } 7611556Srgrimes lvp->vp = vp; 7621556Srgrimes lvp->next = localvars; 7631556Srgrimes localvars = lvp; 7641556Srgrimes INTON; 7651556Srgrimes} 7661556Srgrimes 7671556Srgrimes 7681556Srgrimes/* 7691556Srgrimes * Called after a function returns. 7701556Srgrimes */ 7711556Srgrimes 7721556Srgrimesvoid 77390111Simppoplocalvars(void) 77490111Simp{ 7751556Srgrimes struct localvar *lvp; 7761556Srgrimes struct var *vp; 7771556Srgrimes 7781556Srgrimes while ((lvp = localvars) != NULL) { 7791556Srgrimes localvars = lvp->next; 7801556Srgrimes vp = lvp->vp; 7811556Srgrimes if (vp == NULL) { /* $- saved */ 78217987Speter memcpy(optlist, lvp->text, sizeof optlist); 7831556Srgrimes ckfree(lvp->text); 784215266Sjilles optschanged(); 7851556Srgrimes } else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) { 7861556Srgrimes (void)unsetvar(vp->text); 7871556Srgrimes } else { 7881556Srgrimes if ((vp->flags & VTEXTFIXED) == 0) 7891556Srgrimes ckfree(vp->text); 7901556Srgrimes vp->flags = lvp->flags; 7911556Srgrimes vp->text = lvp->text; 7921556Srgrimes } 7931556Srgrimes ckfree(lvp); 7941556Srgrimes } 7951556Srgrimes} 7961556Srgrimes 7971556Srgrimes 79817987Speterint 79990111Simpsetvarcmd(int argc, char **argv) 80017987Speter{ 8011556Srgrimes if (argc <= 2) 8021556Srgrimes return unsetcmd(argc, argv); 8031556Srgrimes else if (argc == 3) 8041556Srgrimes setvar(argv[1], argv[2], 0); 8051556Srgrimes else 806214538Sjilles error("too many arguments"); 8071556Srgrimes return 0; 8081556Srgrimes} 8091556Srgrimes 8101556Srgrimes 8111556Srgrimes/* 812217847Sjilles * The unset builtin command. 8131556Srgrimes */ 8141556Srgrimes 81517987Speterint 81690111Simpunsetcmd(int argc __unused, char **argv __unused) 81717987Speter{ 8181556Srgrimes char **ap; 8191556Srgrimes int i; 8201556Srgrimes int flg_func = 0; 8211556Srgrimes int flg_var = 0; 8221556Srgrimes int ret = 0; 8231556Srgrimes 8241556Srgrimes while ((i = nextopt("vf")) != '\0') { 8251556Srgrimes if (i == 'f') 8261556Srgrimes flg_func = 1; 8271556Srgrimes else 8281556Srgrimes flg_var = 1; 8291556Srgrimes } 8301556Srgrimes if (flg_func == 0 && flg_var == 0) 8311556Srgrimes flg_var = 1; 8328855Srgrimes 8331556Srgrimes for (ap = argptr; *ap ; ap++) { 8341556Srgrimes if (flg_func) 8351556Srgrimes ret |= unsetfunc(*ap); 8361556Srgrimes if (flg_var) 8371556Srgrimes ret |= unsetvar(*ap); 8381556Srgrimes } 8391556Srgrimes return ret; 8401556Srgrimes} 8411556Srgrimes 8421556Srgrimes 8431556Srgrimes/* 8441556Srgrimes * Unset the specified variable. 8451556Srgrimes */ 8461556Srgrimes 84720425Ssteveint 848200956Sjillesunsetvar(const char *s) 84990111Simp{ 8501556Srgrimes struct var **vpp; 8511556Srgrimes struct var *vp; 8521556Srgrimes 853221668Sjilles vp = find_var(s, &vpp, NULL); 854221668Sjilles if (vp == NULL) 855221668Sjilles return (0); 856221668Sjilles if (vp->flags & VREADONLY) 857221668Sjilles return (1); 858221668Sjilles INTOFF; 859221668Sjilles if (vp->text[vp->name_len + 1] != '\0') 860221668Sjilles setvar(s, nullstr, 0); 861221668Sjilles if ((vp->flags & VEXPORT) && localevar(vp->text)) { 862221668Sjilles change_env(s, 0); 863221668Sjilles setlocale(LC_ALL, ""); 864221668Sjilles updatecharset(); 8651556Srgrimes } 866221668Sjilles vp->flags &= ~VEXPORT; 867221668Sjilles vp->flags |= VUNSET; 868221668Sjilles if ((vp->flags & VSTRFIXED) == 0) { 869221668Sjilles if ((vp->flags & VTEXTFIXED) == 0) 870221668Sjilles ckfree(vp->text); 871221668Sjilles *vpp = vp->next; 872221668Sjilles ckfree(vp); 873221668Sjilles } 874221668Sjilles INTON; 875135856Sdes return (0); 8761556Srgrimes} 8771556Srgrimes 8781556Srgrimes 8791556Srgrimes 8801556Srgrimes/* 8811556Srgrimes * Returns true if the two strings specify the same varable. The first 8821556Srgrimes * variable name is terminated by '='; the second may be terminated by 8831556Srgrimes * either '=' or '\0'. 8841556Srgrimes */ 8851556Srgrimes 886213811Sobrienstatic int 887200956Sjillesvarequal(const char *p, const char *q) 88890111Simp{ 8891556Srgrimes while (*p == *q++) { 8901556Srgrimes if (*p++ == '=') 8911556Srgrimes return 1; 8921556Srgrimes } 8931556Srgrimes if (*p == '=' && *(q - 1) == '\0') 8941556Srgrimes return 1; 8951556Srgrimes return 0; 8961556Srgrimes} 897221668Sjilles 898221668Sjilles/* 899221668Sjilles * Search for a variable. 900221668Sjilles * 'name' may be terminated by '=' or a NUL. 901221668Sjilles * vppp is set to the pointer to vp, or the list head if vp isn't found 902221668Sjilles * lenp is set to the number of charactets in 'name' 903221668Sjilles */ 904221668Sjilles 905221668Sjillesstatic struct var * 906221668Sjillesfind_var(const char *name, struct var ***vppp, int *lenp) 907221668Sjilles{ 908221668Sjilles unsigned int hashval; 909221668Sjilles int len; 910221668Sjilles struct var *vp, **vpp; 911221668Sjilles const char *p = name; 912221668Sjilles 913221668Sjilles hashval = 0; 914221668Sjilles while (*p && *p != '=') 915221668Sjilles hashval = 2 * hashval + (unsigned char)*p++; 916221668Sjilles len = p - name; 917221668Sjilles 918221668Sjilles if (lenp) 919221668Sjilles *lenp = len; 920221668Sjilles vpp = &vartab[hashval % VTABSIZE]; 921221668Sjilles if (vppp) 922221668Sjilles *vppp = vpp; 923221668Sjilles 924221668Sjilles for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) { 925221668Sjilles if (vp->name_len != len) 926221668Sjilles continue; 927221668Sjilles if (memcmp(vp->text, name, len) != 0) 928221668Sjilles continue; 929221668Sjilles if (vppp) 930221668Sjilles *vppp = vpp; 931221668Sjilles return vp; 932221668Sjilles } 933221668Sjilles return NULL; 934221668Sjilles} 935