var.c revision 221669
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 221669 2011-05-08 17:40:10Z 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 97213760Sobrienstatic const struct varinit varinit[] = { 9817987Speter#ifndef NO_HISTORY 99201056Sjilles { &vhistsize, VUNSET, "HISTSIZE=", 10020425Ssteve sethistsize }, 10117987Speter#endif 102201056Sjilles { &vifs, 0, "IFS= \t\n", 10320425Ssteve NULL }, 104201056Sjilles { &vmail, VUNSET, "MAIL=", 10520425Ssteve NULL }, 106201056Sjilles { &vmpath, VUNSET, "MAILPATH=", 10720425Ssteve NULL }, 108201056Sjilles { &vpath, 0, "PATH=" _PATH_DEFPATH, 10920425Ssteve changepath }, 110201056Sjilles { &vppid, VUNSET, "PPID=", 11197689Stjr NULL }, 1128855Srgrimes /* 1131556Srgrimes * vps1 depends on uid 1141556Srgrimes */ 115201056Sjilles { &vps2, 0, "PS2=> ", 11620425Ssteve NULL }, 117201056Sjilles { &vps4, 0, "PS4=+ ", 118159632Sstefanf NULL }, 119208755Sjilles#ifndef NO_HISTORY 120208755Sjilles { &vterm, VUNSET, "TERM=", 121208755Sjilles setterm }, 122208755Sjilles#endif 123201056Sjilles { &voptind, 0, "OPTIND=1", 12420425Ssteve getoptsreset }, 12520425Ssteve { NULL, 0, NULL, 12620425Ssteve NULL } 1271556Srgrimes}; 1281556Srgrimes 129213760Sobrienstatic struct var *vartab[VTABSIZE]; 1301556Srgrimes 131213760Sobrienstatic const char *const locale_names[7] = { 132207678Sjilles "LC_COLLATE", "LC_CTYPE", "LC_MONETARY", 133207678Sjilles "LC_NUMERIC", "LC_TIME", "LC_MESSAGES", NULL 134207678Sjilles}; 135213760Sobrienstatic const int locale_categories[7] = { 136207678Sjilles LC_COLLATE, LC_CTYPE, LC_MONETARY, LC_NUMERIC, LC_TIME, LC_MESSAGES, 0 137207678Sjilles}; 138207678Sjilles 139213811Sobrienstatic int varequal(const char *, const char *); 140221668Sjillesstatic struct var *find_var(const char *, struct var ***, int *); 141213811Sobrienstatic int localevar(const char *); 1421556Srgrimes 1431556Srgrimes/* 144155302Sschweikh * Initialize the variable symbol tables and import the environment. 1451556Srgrimes */ 1461556Srgrimes 1471556Srgrimes#ifdef mkinit 1481556SrgrimesINCLUDE "var.h" 149201053SjillesMKINIT char **environ; 1501556SrgrimesINIT { 1511556Srgrimes char **envp; 1521556Srgrimes 1531556Srgrimes initvar(); 1541556Srgrimes for (envp = environ ; *envp ; envp++) { 1551556Srgrimes if (strchr(*envp, '=')) { 1561556Srgrimes setvareq(*envp, VEXPORT|VTEXTFIXED); 1571556Srgrimes } 1581556Srgrimes } 1591556Srgrimes} 1601556Srgrimes#endif 1611556Srgrimes 1621556Srgrimes 1631556Srgrimes/* 1641556Srgrimes * This routine initializes the builtin variables. It is called when the 165218306Sjilles * shell is initialized. 1661556Srgrimes */ 1671556Srgrimes 1681556Srgrimesvoid 16990111Simpinitvar(void) 17090111Simp{ 17197689Stjr char ppid[20]; 1721556Srgrimes const struct varinit *ip; 1731556Srgrimes struct var *vp; 1741556Srgrimes struct var **vpp; 1751556Srgrimes 1761556Srgrimes for (ip = varinit ; (vp = ip->var) != NULL ; ip++) { 177221668Sjilles if (find_var(ip->text, &vpp, &vp->name_len) != NULL) 178221668Sjilles continue; 179221668Sjilles vp->next = *vpp; 180221668Sjilles *vpp = vp; 181221668Sjilles vp->text = __DECONST(char *, ip->text); 182221668Sjilles vp->flags = ip->flags | VSTRFIXED | VTEXTFIXED; 183221668Sjilles vp->func = ip->func; 1841556Srgrimes } 1851556Srgrimes /* 1861556Srgrimes * PS1 depends on uid 1871556Srgrimes */ 188221668Sjilles if (find_var("PS1", &vpp, &vps1.name_len) == NULL) { 1891556Srgrimes vps1.next = *vpp; 1901556Srgrimes *vpp = &vps1; 191201056Sjilles vps1.text = __DECONST(char *, geteuid() ? "PS1=$ " : "PS1=# "); 1921556Srgrimes vps1.flags = VSTRFIXED|VTEXTFIXED; 1931556Srgrimes } 19497689Stjr if ((vppid.flags & VEXPORT) == 0) { 19597689Stjr fmtstr(ppid, sizeof(ppid), "%d", (int)getppid()); 19697689Stjr setvarsafe("PPID", ppid, 0); 19797689Stjr } 1981556Srgrimes} 1991556Srgrimes 2001556Srgrimes/* 20120425Ssteve * Safe version of setvar, returns 1 on success 0 on failure. 20220425Ssteve */ 20320425Ssteve 20420425Ssteveint 205200956Sjillessetvarsafe(const char *name, const char *val, int flags) 20620425Ssteve{ 20720425Ssteve struct jmploc jmploc; 208194765Sjilles struct jmploc *const savehandler = handler; 20920425Ssteve int err = 0; 210199660Sjilles int inton; 21120425Ssteve 212199660Sjilles inton = is_int_on(); 21320425Ssteve if (setjmp(jmploc.loc)) 21420425Ssteve err = 1; 21520425Ssteve else { 21620425Ssteve handler = &jmploc; 21720425Ssteve setvar(name, val, flags); 21820425Ssteve } 21920425Ssteve handler = savehandler; 220199660Sjilles SETINTON(inton); 22120425Ssteve return err; 22220425Ssteve} 22320425Ssteve 22420425Ssteve/* 225155302Sschweikh * Set the value of a variable. The flags argument is stored with the 2261556Srgrimes * flags of the variable. If val is NULL, the variable is unset. 2271556Srgrimes */ 2281556Srgrimes 2291556Srgrimesvoid 230200956Sjillessetvar(const char *name, const char *val, int flags) 23117987Speter{ 232200956Sjilles const char *p; 2331556Srgrimes int len; 2341556Srgrimes int namelen; 2351556Srgrimes char *nameeq; 2361556Srgrimes int isbad; 2371556Srgrimes 2381556Srgrimes isbad = 0; 2391556Srgrimes p = name; 24017525Sache if (! is_name(*p)) 2411556Srgrimes isbad = 1; 24217525Sache p++; 2431556Srgrimes for (;;) { 2441556Srgrimes if (! is_in_name(*p)) { 2451556Srgrimes if (*p == '\0' || *p == '=') 2461556Srgrimes break; 2471556Srgrimes isbad = 1; 2481556Srgrimes } 2491556Srgrimes p++; 2501556Srgrimes } 2511556Srgrimes namelen = p - name; 2521556Srgrimes if (isbad) 2531556Srgrimes error("%.*s: bad variable name", namelen, name); 2541556Srgrimes len = namelen + 2; /* 2 is space for '=' and '\0' */ 2551556Srgrimes if (val == NULL) { 2561556Srgrimes flags |= VUNSET; 2571556Srgrimes } else { 2581556Srgrimes len += strlen(val); 2591556Srgrimes } 260200956Sjilles nameeq = ckmalloc(len); 261200956Sjilles memcpy(nameeq, name, namelen); 262200956Sjilles nameeq[namelen] = '='; 2631556Srgrimes if (val) 264200956Sjilles scopy(val, nameeq + namelen + 1); 265200956Sjilles else 266200956Sjilles nameeq[namelen + 1] = '\0'; 2671556Srgrimes setvareq(nameeq, flags); 2681556Srgrimes} 2691556Srgrimes 270213811Sobrienstatic int 271200956Sjilleslocalevar(const char *s) 27290111Simp{ 273207678Sjilles const char *const *ss; 2741556Srgrimes 27517525Sache if (*s != 'L') 27617525Sache return 0; 27717525Sache if (varequal(s + 1, "ANG")) 27817525Sache return 1; 27917525Sache if (strncmp(s + 1, "C_", 2) != 0) 28017525Sache return 0; 281207678Sjilles if (varequal(s + 3, "ALL")) 282207678Sjilles return 1; 283207678Sjilles for (ss = locale_names; *ss ; ss++) 284207678Sjilles if (varequal(s + 3, *ss + 3)) 28517525Sache return 1; 28617525Sache return 0; 28717525Sache} 2881556Srgrimes 289171268Sscf 2901556Srgrimes/* 291171268Sscf * Sets/unsets an environment variable from a pointer that may actually be a 292171268Sscf * pointer into environ where the string should not be manipulated. 293171268Sscf */ 294213811Sobrienstatic void 295200956Sjilleschange_env(const char *s, int set) 296171268Sscf{ 297171268Sscf char *eqp; 298171268Sscf char *ss; 299171268Sscf 300171268Sscf ss = savestr(s); 301171268Sscf if ((eqp = strchr(ss, '=')) != NULL) 302171268Sscf *eqp = '\0'; 303171268Sscf if (set && eqp != NULL) 304171268Sscf (void) setenv(ss, eqp + 1, 1); 305171268Sscf else 306171268Sscf (void) unsetenv(ss); 307171268Sscf ckfree(ss); 308171268Sscf 309171268Sscf return; 310171268Sscf} 311171268Sscf 312171268Sscf 313171268Sscf/* 3141556Srgrimes * Same as setvar except that the variable and value are passed in 3151556Srgrimes * the first argument as name=value. Since the first argument will 3161556Srgrimes * be actually stored in the table, it should not be a string that 3171556Srgrimes * will go away. 3181556Srgrimes */ 3191556Srgrimes 3201556Srgrimesvoid 32190111Simpsetvareq(char *s, int flags) 32217987Speter{ 3231556Srgrimes struct var *vp, **vpp; 324221668Sjilles int nlen; 3251556Srgrimes 32645263Scracauer if (aflag) 32745263Scracauer flags |= VEXPORT; 328221668Sjilles vp = find_var(s, &vpp, &nlen); 329221668Sjilles if (vp != NULL) { 330221668Sjilles if (vp->flags & VREADONLY) 331221668Sjilles error("%.*s: is read only", vp->name_len, s); 332221668Sjilles if (flags & VNOSET) 333221668Sjilles return; 334221668Sjilles INTOFF; 33520425Ssteve 336221668Sjilles if (vp->func && (flags & VNOFUNC) == 0) 337221668Sjilles (*vp->func)(s + vp->name_len + 1); 33820425Ssteve 339221668Sjilles if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0) 340221668Sjilles ckfree(vp->text); 34120425Ssteve 342221668Sjilles vp->flags &= ~(VTEXTFIXED|VSTACK|VUNSET); 343221668Sjilles vp->flags |= flags; 344221668Sjilles vp->text = s; 34520425Ssteve 346221668Sjilles /* 347221668Sjilles * We could roll this to a function, to handle it as 348221668Sjilles * a regular variable function callback, but why bother? 349221668Sjilles * 350221668Sjilles * Note: this assumes iflag is not set to 1 initially. 351221668Sjilles * As part of init(), this is called before arguments 352221668Sjilles * are looked at. 353221668Sjilles */ 354221668Sjilles if ((vp == &vmpath || (vp == &vmail && ! mpathset())) && 355221668Sjilles iflag == 1) 356221668Sjilles chkmail(1); 357221668Sjilles if ((vp->flags & VEXPORT) && localevar(s)) { 358221668Sjilles change_env(s, 1); 359221668Sjilles (void) setlocale(LC_ALL, ""); 360221668Sjilles updatecharset(); 3611556Srgrimes } 362221668Sjilles INTON; 363221668Sjilles return; 3641556Srgrimes } 3651556Srgrimes /* not found */ 366216870Sjilles if (flags & VNOSET) 367216870Sjilles return; 3681556Srgrimes vp = ckmalloc(sizeof (*vp)); 3691556Srgrimes vp->flags = flags; 3701556Srgrimes vp->text = s; 371221668Sjilles vp->name_len = nlen; 3721556Srgrimes vp->next = *vpp; 37320425Ssteve vp->func = NULL; 37417525Sache INTOFF; 3751556Srgrimes *vpp = vp; 37617525Sache if ((vp->flags & VEXPORT) && localevar(s)) { 377171268Sscf change_env(s, 1); 37817525Sache (void) setlocale(LC_ALL, ""); 379221559Sjilles updatecharset(); 38017525Sache } 38117525Sache INTON; 3821556Srgrimes} 3831556Srgrimes 3841556Srgrimes 3851556Srgrimes 3861556Srgrimes/* 3871556Srgrimes * Process a linked list of variable assignments. 3881556Srgrimes */ 3891556Srgrimes 3901556Srgrimesvoid 391216870Sjilleslistsetvar(struct strlist *list, int flags) 39290111Simp{ 3931556Srgrimes struct strlist *lp; 3941556Srgrimes 3951556Srgrimes INTOFF; 3961556Srgrimes for (lp = list ; lp ; lp = lp->next) { 397216870Sjilles setvareq(savestr(lp->text), flags); 3981556Srgrimes } 3991556Srgrimes INTON; 4001556Srgrimes} 4011556Srgrimes 4021556Srgrimes 4031556Srgrimes 4041556Srgrimes/* 4051556Srgrimes * Find the value of a variable. Returns NULL if not set. 4061556Srgrimes */ 4071556Srgrimes 4081556Srgrimeschar * 409200956Sjilleslookupvar(const char *name) 41090111Simp{ 4111556Srgrimes struct var *v; 4121556Srgrimes 413221668Sjilles v = find_var(name, NULL, NULL); 414221668Sjilles if (v == NULL || v->flags & VUNSET) 415221668Sjilles return NULL; 416221668Sjilles return v->text + v->name_len + 1; 4171556Srgrimes} 4181556Srgrimes 4191556Srgrimes 4201556Srgrimes 4211556Srgrimes/* 4221556Srgrimes * Search the environment of a builtin command. If the second argument 4231556Srgrimes * is nonzero, return the value of a variable even if it hasn't been 4241556Srgrimes * exported. 4251556Srgrimes */ 4261556Srgrimes 4271556Srgrimeschar * 428200956Sjillesbltinlookup(const char *name, int doall) 42917987Speter{ 4301556Srgrimes struct strlist *sp; 4311556Srgrimes struct var *v; 432212467Sjilles char *result; 4331556Srgrimes 434212467Sjilles result = NULL; 4351556Srgrimes for (sp = cmdenviron ; sp ; sp = sp->next) { 4361556Srgrimes if (varequal(sp->text, name)) 437212467Sjilles result = strchr(sp->text, '=') + 1; 4381556Srgrimes } 439212467Sjilles if (result != NULL) 440212467Sjilles return result; 441221668Sjilles 442221668Sjilles v = find_var(name, NULL, NULL); 443221668Sjilles if (v == NULL || v->flags & VUNSET || 444221668Sjilles (!doall && (v->flags & VEXPORT) == 0)) 445221668Sjilles return NULL; 446221668Sjilles return v->text + v->name_len + 1; 4471556Srgrimes} 4481556Srgrimes 4491556Srgrimes 450207678Sjilles/* 451207678Sjilles * Set up locale for a builtin (LANG/LC_* assignments). 452207678Sjilles */ 453207678Sjillesvoid 454207678Sjillesbltinsetlocale(void) 455207678Sjilles{ 456207678Sjilles struct strlist *lp; 457207678Sjilles int act = 0; 458207678Sjilles char *loc, *locdef; 459207678Sjilles int i; 4601556Srgrimes 461207678Sjilles for (lp = cmdenviron ; lp ; lp = lp->next) { 462207678Sjilles if (localevar(lp->text)) { 463207678Sjilles act = 1; 464207678Sjilles break; 465207678Sjilles } 466207678Sjilles } 467207678Sjilles if (!act) 468207678Sjilles return; 469207678Sjilles loc = bltinlookup("LC_ALL", 0); 470207678Sjilles INTOFF; 471207678Sjilles if (loc != NULL) { 472207678Sjilles setlocale(LC_ALL, loc); 473207678Sjilles INTON; 474221559Sjilles updatecharset(); 475207678Sjilles return; 476207678Sjilles } 477207678Sjilles locdef = bltinlookup("LANG", 0); 478207678Sjilles for (i = 0; locale_names[i] != NULL; i++) { 479207678Sjilles loc = bltinlookup(locale_names[i], 0); 480207678Sjilles if (loc == NULL) 481207678Sjilles loc = locdef; 482207678Sjilles if (loc != NULL) 483207678Sjilles setlocale(locale_categories[i], loc); 484207678Sjilles } 485207678Sjilles INTON; 486221559Sjilles updatecharset(); 487207678Sjilles} 488207678Sjilles 4891556Srgrimes/* 490207678Sjilles * Undo the effect of bltinlocaleset(). 491207678Sjilles */ 492207678Sjillesvoid 493207678Sjillesbltinunsetlocale(void) 494207678Sjilles{ 495207678Sjilles struct strlist *lp; 496207678Sjilles 497207678Sjilles INTOFF; 498207678Sjilles for (lp = cmdenviron ; lp ; lp = lp->next) { 499207678Sjilles if (localevar(lp->text)) { 500207678Sjilles setlocale(LC_ALL, ""); 501221559Sjilles updatecharset(); 502207678Sjilles return; 503207678Sjilles } 504207678Sjilles } 505207678Sjilles INTON; 506207678Sjilles} 507207678Sjilles 508221559Sjilles/* 509221559Sjilles * Update the localeisutf8 flag. 510221559Sjilles */ 511221559Sjillesvoid 512221559Sjillesupdatecharset(void) 513221559Sjilles{ 514221559Sjilles char *charset; 515207678Sjilles 516221559Sjilles charset = nl_langinfo(CODESET); 517221559Sjilles localeisutf8 = !strcmp(charset, "UTF-8"); 518221559Sjilles} 519221559Sjilles 520221669Sjillesvoid 521221669Sjillesinitcharset(void) 522221669Sjilles{ 523221669Sjilles updatecharset(); 524221669Sjilles initial_localeisutf8 = localeisutf8; 525221669Sjilles} 526221669Sjilles 527207678Sjilles/* 5281556Srgrimes * Generate a list of exported variables. This routine is used to construct 5291556Srgrimes * the third argument to execve when executing a program. 5301556Srgrimes */ 5311556Srgrimes 5321556Srgrimeschar ** 53390111Simpenvironment(void) 53490111Simp{ 5351556Srgrimes int nenv; 5361556Srgrimes struct var **vpp; 5371556Srgrimes struct var *vp; 5381556Srgrimes char **env, **ep; 5391556Srgrimes 5401556Srgrimes nenv = 0; 5411556Srgrimes for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) { 5421556Srgrimes for (vp = *vpp ; vp ; vp = vp->next) 5431556Srgrimes if (vp->flags & VEXPORT) 5441556Srgrimes nenv++; 5451556Srgrimes } 5461556Srgrimes ep = env = stalloc((nenv + 1) * sizeof *env); 5471556Srgrimes for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) { 5481556Srgrimes for (vp = *vpp ; vp ; vp = vp->next) 5491556Srgrimes if (vp->flags & VEXPORT) 5501556Srgrimes *ep++ = vp->text; 5511556Srgrimes } 5521556Srgrimes *ep = NULL; 5531556Srgrimes return env; 5541556Srgrimes} 5551556Srgrimes 5561556Srgrimes 557213811Sobrienstatic int 558158145Sstefanfvar_compare(const void *a, const void *b) 559158145Sstefanf{ 560158145Sstefanf const char *const *sa, *const *sb; 5611556Srgrimes 562158145Sstefanf sa = a; 563158145Sstefanf sb = b; 564158145Sstefanf /* 565158145Sstefanf * This compares two var=value strings which creates a different 566158145Sstefanf * order from what you would probably expect. POSIX is somewhat 567158145Sstefanf * ambiguous on what should be sorted exactly. 568158145Sstefanf */ 569158145Sstefanf return strcoll(*sa, *sb); 570158145Sstefanf} 571158145Sstefanf 572158145Sstefanf 5731556Srgrimes/* 574217847Sjilles * Command to list all variables which are set. This is invoked from the 575217847Sjilles * set command when it is called without any options or operands. 5761556Srgrimes */ 5771556Srgrimes 5781556Srgrimesint 57990111Simpshowvarscmd(int argc __unused, char **argv __unused) 58017987Speter{ 5811556Srgrimes struct var **vpp; 5821556Srgrimes struct var *vp; 58397915Stjr const char *s; 584158145Sstefanf const char **vars; 585158145Sstefanf int i, n; 5861556Srgrimes 587158145Sstefanf /* 588158145Sstefanf * POSIX requires us to sort the variables. 589158145Sstefanf */ 590158145Sstefanf n = 0; 591158145Sstefanf for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) { 592158145Sstefanf for (vp = *vpp; vp; vp = vp->next) { 593158145Sstefanf if (!(vp->flags & VUNSET)) 594158145Sstefanf n++; 5951556Srgrimes } 5961556Srgrimes } 597158145Sstefanf 598158145Sstefanf INTON; 599158145Sstefanf vars = ckmalloc(n * sizeof(*vars)); 600158145Sstefanf i = 0; 601158145Sstefanf for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) { 602158145Sstefanf for (vp = *vpp; vp; vp = vp->next) { 603158145Sstefanf if (!(vp->flags & VUNSET)) 604158145Sstefanf vars[i++] = vp->text; 605158145Sstefanf } 606158145Sstefanf } 607158145Sstefanf 608158145Sstefanf qsort(vars, n, sizeof(*vars), var_compare); 609158145Sstefanf for (i = 0; i < n; i++) { 610215567Sjilles s = strchr(vars[i], '='); 611215567Sjilles s++; 612215567Sjilles outbin(vars[i], s - vars[i], out1); 613215567Sjilles out1qstr(s); 614158145Sstefanf out1c('\n'); 615158145Sstefanf } 616158145Sstefanf ckfree(vars); 617158145Sstefanf INTOFF; 618158145Sstefanf 6191556Srgrimes return 0; 6201556Srgrimes} 6211556Srgrimes 6221556Srgrimes 6231556Srgrimes 6241556Srgrimes/* 6251556Srgrimes * The export and readonly commands. 6261556Srgrimes */ 6271556Srgrimes 6281556Srgrimesint 62990111Simpexportcmd(int argc, char **argv) 63017987Speter{ 6311556Srgrimes struct var **vpp; 6321556Srgrimes struct var *vp; 6331556Srgrimes char *name; 6341556Srgrimes char *p; 63597914Stjr char *cmdname; 63697914Stjr int ch, values; 6371556Srgrimes int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT; 6381556Srgrimes 63997914Stjr cmdname = argv[0]; 64097914Stjr optreset = optind = 1; 641100663Stjr opterr = 0; 64297914Stjr values = 0; 64397914Stjr while ((ch = getopt(argc, argv, "p")) != -1) { 64497914Stjr switch (ch) { 64597914Stjr case 'p': 64697914Stjr values = 1; 64797914Stjr break; 64897914Stjr case '?': 64997914Stjr default: 65097914Stjr error("unknown option: -%c", optopt); 65197914Stjr } 65297914Stjr } 65397914Stjr argc -= optind; 65497914Stjr argv += optind; 65597914Stjr 656149919Sstefanf if (values && argc != 0) 657149919Sstefanf error("-p requires no arguments"); 65897914Stjr if (argc != 0) { 659149919Sstefanf while ((name = *argv++) != NULL) { 6601556Srgrimes if ((p = strchr(name, '=')) != NULL) { 6611556Srgrimes p++; 6621556Srgrimes } else { 663221668Sjilles vp = find_var(name, NULL, NULL); 664221668Sjilles if (vp != NULL) { 665221668Sjilles vp->flags |= flag; 666221668Sjilles if ((vp->flags & VEXPORT) && localevar(vp->text)) { 667221668Sjilles change_env(vp->text, 1); 668221668Sjilles (void) setlocale(LC_ALL, ""); 669221668Sjilles updatecharset(); 6701556Srgrimes } 671221668Sjilles continue; 6721556Srgrimes } 6731556Srgrimes } 6741556Srgrimes setvar(name, p, flag); 6751556Srgrimes } 6761556Srgrimes } else { 6771556Srgrimes for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) { 6781556Srgrimes for (vp = *vpp ; vp ; vp = vp->next) { 6791556Srgrimes if (vp->flags & flag) { 68097914Stjr if (values) { 68197914Stjr out1str(cmdname); 68297914Stjr out1c(' '); 68397914Stjr } 684215567Sjilles p = strchr(vp->text, '='); 68597914Stjr if (values && !(vp->flags & VUNSET)) { 686215567Sjilles p++; 687215567Sjilles outbin(vp->text, p - vp->text, 688215567Sjilles out1); 689215567Sjilles out1qstr(p); 690215567Sjilles } else 691215567Sjilles outbin(vp->text, p - vp->text, 692215567Sjilles out1); 6931556Srgrimes out1c('\n'); 6941556Srgrimes } 6951556Srgrimes } 6961556Srgrimes } 6971556Srgrimes } 6981556Srgrimes return 0; 6991556Srgrimes} 7001556Srgrimes 7011556Srgrimes 7021556Srgrimes/* 7031556Srgrimes * The "local" command. 7041556Srgrimes */ 7051556Srgrimes 70617987Speterint 70790111Simplocalcmd(int argc __unused, char **argv __unused) 70817987Speter{ 7091556Srgrimes char *name; 7101556Srgrimes 7111556Srgrimes if (! in_function()) 7121556Srgrimes error("Not in a function"); 7131556Srgrimes while ((name = *argptr++) != NULL) { 7141556Srgrimes mklocal(name); 7151556Srgrimes } 7161556Srgrimes return 0; 7171556Srgrimes} 7181556Srgrimes 7191556Srgrimes 7201556Srgrimes/* 7211556Srgrimes * Make a variable a local variable. When a variable is made local, it's 7221556Srgrimes * value and flags are saved in a localvar structure. The saved values 7231556Srgrimes * will be restored when the shell function returns. We handle the name 7241556Srgrimes * "-" as a special case. 7251556Srgrimes */ 7261556Srgrimes 7271556Srgrimesvoid 72890111Simpmklocal(char *name) 72990111Simp{ 7301556Srgrimes struct localvar *lvp; 7311556Srgrimes struct var **vpp; 7321556Srgrimes struct var *vp; 7331556Srgrimes 7341556Srgrimes INTOFF; 7351556Srgrimes lvp = ckmalloc(sizeof (struct localvar)); 7361556Srgrimes if (name[0] == '-' && name[1] == '\0') { 7371556Srgrimes lvp->text = ckmalloc(sizeof optlist); 73817987Speter memcpy(lvp->text, optlist, sizeof optlist); 7391556Srgrimes vp = NULL; 7401556Srgrimes } else { 741221668Sjilles vp = find_var(name, &vpp, NULL); 7421556Srgrimes if (vp == NULL) { 7431556Srgrimes if (strchr(name, '=')) 7441556Srgrimes setvareq(savestr(name), VSTRFIXED); 7451556Srgrimes else 7461556Srgrimes setvar(name, NULL, VSTRFIXED); 7471556Srgrimes vp = *vpp; /* the new variable */ 7481556Srgrimes lvp->text = NULL; 7491556Srgrimes lvp->flags = VUNSET; 7501556Srgrimes } else { 7511556Srgrimes lvp->text = vp->text; 7521556Srgrimes lvp->flags = vp->flags; 7531556Srgrimes vp->flags |= VSTRFIXED|VTEXTFIXED; 754221668Sjilles if (name[vp->name_len] == '=') 7551556Srgrimes setvareq(savestr(name), 0); 7561556Srgrimes } 7571556Srgrimes } 7581556Srgrimes lvp->vp = vp; 7591556Srgrimes lvp->next = localvars; 7601556Srgrimes localvars = lvp; 7611556Srgrimes INTON; 7621556Srgrimes} 7631556Srgrimes 7641556Srgrimes 7651556Srgrimes/* 7661556Srgrimes * Called after a function returns. 7671556Srgrimes */ 7681556Srgrimes 7691556Srgrimesvoid 77090111Simppoplocalvars(void) 77190111Simp{ 7721556Srgrimes struct localvar *lvp; 7731556Srgrimes struct var *vp; 7741556Srgrimes 7751556Srgrimes while ((lvp = localvars) != NULL) { 7761556Srgrimes localvars = lvp->next; 7771556Srgrimes vp = lvp->vp; 7781556Srgrimes if (vp == NULL) { /* $- saved */ 77917987Speter memcpy(optlist, lvp->text, sizeof optlist); 7801556Srgrimes ckfree(lvp->text); 781215266Sjilles optschanged(); 7821556Srgrimes } else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) { 7831556Srgrimes (void)unsetvar(vp->text); 7841556Srgrimes } else { 7851556Srgrimes if ((vp->flags & VTEXTFIXED) == 0) 7861556Srgrimes ckfree(vp->text); 7871556Srgrimes vp->flags = lvp->flags; 7881556Srgrimes vp->text = lvp->text; 7891556Srgrimes } 7901556Srgrimes ckfree(lvp); 7911556Srgrimes } 7921556Srgrimes} 7931556Srgrimes 7941556Srgrimes 79517987Speterint 79690111Simpsetvarcmd(int argc, char **argv) 79717987Speter{ 7981556Srgrimes if (argc <= 2) 7991556Srgrimes return unsetcmd(argc, argv); 8001556Srgrimes else if (argc == 3) 8011556Srgrimes setvar(argv[1], argv[2], 0); 8021556Srgrimes else 803214538Sjilles error("too many arguments"); 8041556Srgrimes return 0; 8051556Srgrimes} 8061556Srgrimes 8071556Srgrimes 8081556Srgrimes/* 809217847Sjilles * The unset builtin command. 8101556Srgrimes */ 8111556Srgrimes 81217987Speterint 81390111Simpunsetcmd(int argc __unused, char **argv __unused) 81417987Speter{ 8151556Srgrimes char **ap; 8161556Srgrimes int i; 8171556Srgrimes int flg_func = 0; 8181556Srgrimes int flg_var = 0; 8191556Srgrimes int ret = 0; 8201556Srgrimes 8211556Srgrimes while ((i = nextopt("vf")) != '\0') { 8221556Srgrimes if (i == 'f') 8231556Srgrimes flg_func = 1; 8241556Srgrimes else 8251556Srgrimes flg_var = 1; 8261556Srgrimes } 8271556Srgrimes if (flg_func == 0 && flg_var == 0) 8281556Srgrimes flg_var = 1; 8298855Srgrimes 8301556Srgrimes for (ap = argptr; *ap ; ap++) { 8311556Srgrimes if (flg_func) 8321556Srgrimes ret |= unsetfunc(*ap); 8331556Srgrimes if (flg_var) 8341556Srgrimes ret |= unsetvar(*ap); 8351556Srgrimes } 8361556Srgrimes return ret; 8371556Srgrimes} 8381556Srgrimes 8391556Srgrimes 8401556Srgrimes/* 8411556Srgrimes * Unset the specified variable. 8421556Srgrimes */ 8431556Srgrimes 84420425Ssteveint 845200956Sjillesunsetvar(const char *s) 84690111Simp{ 8471556Srgrimes struct var **vpp; 8481556Srgrimes struct var *vp; 8491556Srgrimes 850221668Sjilles vp = find_var(s, &vpp, NULL); 851221668Sjilles if (vp == NULL) 852221668Sjilles return (0); 853221668Sjilles if (vp->flags & VREADONLY) 854221668Sjilles return (1); 855221668Sjilles INTOFF; 856221668Sjilles if (vp->text[vp->name_len + 1] != '\0') 857221668Sjilles setvar(s, nullstr, 0); 858221668Sjilles if ((vp->flags & VEXPORT) && localevar(vp->text)) { 859221668Sjilles change_env(s, 0); 860221668Sjilles setlocale(LC_ALL, ""); 861221668Sjilles updatecharset(); 8621556Srgrimes } 863221668Sjilles vp->flags &= ~VEXPORT; 864221668Sjilles vp->flags |= VUNSET; 865221668Sjilles if ((vp->flags & VSTRFIXED) == 0) { 866221668Sjilles if ((vp->flags & VTEXTFIXED) == 0) 867221668Sjilles ckfree(vp->text); 868221668Sjilles *vpp = vp->next; 869221668Sjilles ckfree(vp); 870221668Sjilles } 871221668Sjilles INTON; 872135856Sdes return (0); 8731556Srgrimes} 8741556Srgrimes 8751556Srgrimes 8761556Srgrimes 8771556Srgrimes/* 8781556Srgrimes * Returns true if the two strings specify the same varable. The first 8791556Srgrimes * variable name is terminated by '='; the second may be terminated by 8801556Srgrimes * either '=' or '\0'. 8811556Srgrimes */ 8821556Srgrimes 883213811Sobrienstatic int 884200956Sjillesvarequal(const char *p, const char *q) 88590111Simp{ 8861556Srgrimes while (*p == *q++) { 8871556Srgrimes if (*p++ == '=') 8881556Srgrimes return 1; 8891556Srgrimes } 8901556Srgrimes if (*p == '=' && *(q - 1) == '\0') 8911556Srgrimes return 1; 8921556Srgrimes return 0; 8931556Srgrimes} 894221668Sjilles 895221668Sjilles/* 896221668Sjilles * Search for a variable. 897221668Sjilles * 'name' may be terminated by '=' or a NUL. 898221668Sjilles * vppp is set to the pointer to vp, or the list head if vp isn't found 899221668Sjilles * lenp is set to the number of charactets in 'name' 900221668Sjilles */ 901221668Sjilles 902221668Sjillesstatic struct var * 903221668Sjillesfind_var(const char *name, struct var ***vppp, int *lenp) 904221668Sjilles{ 905221668Sjilles unsigned int hashval; 906221668Sjilles int len; 907221668Sjilles struct var *vp, **vpp; 908221668Sjilles const char *p = name; 909221668Sjilles 910221668Sjilles hashval = 0; 911221668Sjilles while (*p && *p != '=') 912221668Sjilles hashval = 2 * hashval + (unsigned char)*p++; 913221668Sjilles len = p - name; 914221668Sjilles 915221668Sjilles if (lenp) 916221668Sjilles *lenp = len; 917221668Sjilles vpp = &vartab[hashval % VTABSIZE]; 918221668Sjilles if (vppp) 919221668Sjilles *vppp = vpp; 920221668Sjilles 921221668Sjilles for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) { 922221668Sjilles if (vp->name_len != len) 923221668Sjilles continue; 924221668Sjilles if (memcmp(vp->text, name, len) != 0) 925221668Sjilles continue; 926221668Sjilles if (vppp) 927221668Sjilles *vppp = vpp; 928221668Sjilles return vp; 929221668Sjilles } 930221668Sjilles return NULL; 931221668Sjilles} 932