var.c revision 250422
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 250422 2013-05-09 21:29:16Z eadler $"); 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 149245689Sjillesextern char **environ; 1501556Srgrimes 1511556Srgrimes/* 152245689Sjilles * This routine initializes the builtin variables and imports the environment. 153245689Sjilles * It is called when the shell is initialized. 1541556Srgrimes */ 1551556Srgrimes 1561556Srgrimesvoid 15790111Simpinitvar(void) 15890111Simp{ 15997689Stjr char ppid[20]; 1601556Srgrimes const struct varinit *ip; 1611556Srgrimes struct var *vp; 1621556Srgrimes struct var **vpp; 163245689Sjilles char **envp; 1641556Srgrimes 1651556Srgrimes for (ip = varinit ; (vp = ip->var) != NULL ; ip++) { 166221668Sjilles if (find_var(ip->text, &vpp, &vp->name_len) != NULL) 167221668Sjilles continue; 168221668Sjilles vp->next = *vpp; 169221668Sjilles *vpp = vp; 170221668Sjilles vp->text = __DECONST(char *, ip->text); 171221668Sjilles vp->flags = ip->flags | VSTRFIXED | VTEXTFIXED; 172221668Sjilles vp->func = ip->func; 1731556Srgrimes } 1741556Srgrimes /* 1751556Srgrimes * PS1 depends on uid 1761556Srgrimes */ 177221668Sjilles if (find_var("PS1", &vpp, &vps1.name_len) == NULL) { 1781556Srgrimes vps1.next = *vpp; 1791556Srgrimes *vpp = &vps1; 180201056Sjilles vps1.text = __DECONST(char *, geteuid() ? "PS1=$ " : "PS1=# "); 1811556Srgrimes vps1.flags = VSTRFIXED|VTEXTFIXED; 1821556Srgrimes } 18397689Stjr if ((vppid.flags & VEXPORT) == 0) { 18497689Stjr fmtstr(ppid, sizeof(ppid), "%d", (int)getppid()); 18597689Stjr setvarsafe("PPID", ppid, 0); 18697689Stjr } 187245689Sjilles for (envp = environ ; *envp ; envp++) { 188245689Sjilles if (strchr(*envp, '=')) { 189245689Sjilles setvareq(*envp, VEXPORT|VTEXTFIXED); 190245689Sjilles } 191245689Sjilles } 1921556Srgrimes} 1931556Srgrimes 1941556Srgrimes/* 19520425Ssteve * Safe version of setvar, returns 1 on success 0 on failure. 19620425Ssteve */ 19720425Ssteve 19820425Ssteveint 199200956Sjillessetvarsafe(const char *name, const char *val, int flags) 20020425Ssteve{ 20120425Ssteve struct jmploc jmploc; 202194765Sjilles struct jmploc *const savehandler = handler; 20320425Ssteve int err = 0; 204199660Sjilles int inton; 20520425Ssteve 206199660Sjilles inton = is_int_on(); 20720425Ssteve if (setjmp(jmploc.loc)) 20820425Ssteve err = 1; 20920425Ssteve else { 21020425Ssteve handler = &jmploc; 21120425Ssteve setvar(name, val, flags); 21220425Ssteve } 21320425Ssteve handler = savehandler; 214199660Sjilles SETINTON(inton); 21520425Ssteve return err; 21620425Ssteve} 21720425Ssteve 21820425Ssteve/* 219155302Sschweikh * Set the value of a variable. The flags argument is stored with the 2201556Srgrimes * flags of the variable. If val is NULL, the variable is unset. 2211556Srgrimes */ 2221556Srgrimes 2231556Srgrimesvoid 224200956Sjillessetvar(const char *name, const char *val, int flags) 22517987Speter{ 226200956Sjilles const char *p; 2271556Srgrimes int len; 2281556Srgrimes int namelen; 2291556Srgrimes char *nameeq; 2301556Srgrimes int isbad; 2311556Srgrimes 2321556Srgrimes isbad = 0; 2331556Srgrimes p = name; 23417525Sache if (! is_name(*p)) 2351556Srgrimes isbad = 1; 23617525Sache p++; 2371556Srgrimes for (;;) { 2381556Srgrimes if (! is_in_name(*p)) { 2391556Srgrimes if (*p == '\0' || *p == '=') 2401556Srgrimes break; 2411556Srgrimes isbad = 1; 2421556Srgrimes } 2431556Srgrimes p++; 2441556Srgrimes } 2451556Srgrimes namelen = p - name; 2461556Srgrimes if (isbad) 2471556Srgrimes error("%.*s: bad variable name", namelen, name); 2481556Srgrimes len = namelen + 2; /* 2 is space for '=' and '\0' */ 2491556Srgrimes if (val == NULL) { 2501556Srgrimes flags |= VUNSET; 2511556Srgrimes } else { 2521556Srgrimes len += strlen(val); 2531556Srgrimes } 254200956Sjilles nameeq = ckmalloc(len); 255200956Sjilles memcpy(nameeq, name, namelen); 256200956Sjilles nameeq[namelen] = '='; 2571556Srgrimes if (val) 258200956Sjilles scopy(val, nameeq + namelen + 1); 259200956Sjilles else 260200956Sjilles nameeq[namelen + 1] = '\0'; 2611556Srgrimes setvareq(nameeq, flags); 2621556Srgrimes} 2631556Srgrimes 264213811Sobrienstatic int 265200956Sjilleslocalevar(const char *s) 26690111Simp{ 267207678Sjilles const char *const *ss; 2681556Srgrimes 26917525Sache if (*s != 'L') 27017525Sache return 0; 27117525Sache if (varequal(s + 1, "ANG")) 27217525Sache return 1; 27317525Sache if (strncmp(s + 1, "C_", 2) != 0) 27417525Sache return 0; 275207678Sjilles if (varequal(s + 3, "ALL")) 276207678Sjilles return 1; 277207678Sjilles for (ss = locale_names; *ss ; ss++) 278207678Sjilles if (varequal(s + 3, *ss + 3)) 27917525Sache return 1; 28017525Sache return 0; 28117525Sache} 2821556Srgrimes 283171268Sscf 2841556Srgrimes/* 285171268Sscf * Sets/unsets an environment variable from a pointer that may actually be a 286171268Sscf * pointer into environ where the string should not be manipulated. 287171268Sscf */ 288213811Sobrienstatic void 289200956Sjilleschange_env(const char *s, int set) 290171268Sscf{ 291171268Sscf char *eqp; 292171268Sscf char *ss; 293171268Sscf 294171268Sscf ss = savestr(s); 295171268Sscf if ((eqp = strchr(ss, '=')) != NULL) 296171268Sscf *eqp = '\0'; 297171268Sscf if (set && eqp != NULL) 298171268Sscf (void) setenv(ss, eqp + 1, 1); 299171268Sscf else 300171268Sscf (void) unsetenv(ss); 301171268Sscf ckfree(ss); 302171268Sscf 303171268Sscf return; 304171268Sscf} 305171268Sscf 306171268Sscf 307171268Sscf/* 3081556Srgrimes * Same as setvar except that the variable and value are passed in 3091556Srgrimes * the first argument as name=value. Since the first argument will 3101556Srgrimes * be actually stored in the table, it should not be a string that 3111556Srgrimes * will go away. 3121556Srgrimes */ 3131556Srgrimes 3141556Srgrimesvoid 31590111Simpsetvareq(char *s, int flags) 31617987Speter{ 3171556Srgrimes struct var *vp, **vpp; 318221668Sjilles int nlen; 3191556Srgrimes 32045263Scracauer if (aflag) 32145263Scracauer flags |= VEXPORT; 322223024Sjilles if (forcelocal && !(flags & (VNOSET | VNOLOCAL))) 323223024Sjilles mklocal(s); 324221668Sjilles vp = find_var(s, &vpp, &nlen); 325221668Sjilles if (vp != NULL) { 326221668Sjilles if (vp->flags & VREADONLY) 327221668Sjilles error("%.*s: is read only", vp->name_len, s); 328221668Sjilles if (flags & VNOSET) 329221668Sjilles return; 330221668Sjilles INTOFF; 33120425Ssteve 332221668Sjilles if (vp->func && (flags & VNOFUNC) == 0) 333221668Sjilles (*vp->func)(s + vp->name_len + 1); 33420425Ssteve 335221668Sjilles if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0) 336221668Sjilles ckfree(vp->text); 33720425Ssteve 338221668Sjilles vp->flags &= ~(VTEXTFIXED|VSTACK|VUNSET); 339221668Sjilles vp->flags |= flags; 340221668Sjilles vp->text = s; 34120425Ssteve 342221668Sjilles /* 343221668Sjilles * We could roll this to a function, to handle it as 344221668Sjilles * a regular variable function callback, but why bother? 345221668Sjilles * 346221668Sjilles * Note: this assumes iflag is not set to 1 initially. 347245689Sjilles * As part of initvar(), this is called before arguments 348221668Sjilles * are looked at. 349221668Sjilles */ 350221668Sjilles if ((vp == &vmpath || (vp == &vmail && ! mpathset())) && 351221668Sjilles iflag == 1) 352221668Sjilles chkmail(1); 353221668Sjilles if ((vp->flags & VEXPORT) && localevar(s)) { 354221668Sjilles change_env(s, 1); 355221668Sjilles (void) setlocale(LC_ALL, ""); 356221668Sjilles updatecharset(); 3571556Srgrimes } 358221668Sjilles INTON; 359221668Sjilles return; 3601556Srgrimes } 3611556Srgrimes /* not found */ 362216870Sjilles if (flags & VNOSET) 363216870Sjilles return; 3641556Srgrimes vp = ckmalloc(sizeof (*vp)); 3651556Srgrimes vp->flags = flags; 3661556Srgrimes vp->text = s; 367221668Sjilles vp->name_len = nlen; 3681556Srgrimes vp->next = *vpp; 36920425Ssteve vp->func = NULL; 37017525Sache INTOFF; 3711556Srgrimes *vpp = vp; 37217525Sache if ((vp->flags & VEXPORT) && localevar(s)) { 373171268Sscf change_env(s, 1); 37417525Sache (void) setlocale(LC_ALL, ""); 375221559Sjilles updatecharset(); 37617525Sache } 37717525Sache INTON; 3781556Srgrimes} 3791556Srgrimes 3801556Srgrimes 3811556Srgrimes 3821556Srgrimes/* 3831556Srgrimes * Process a linked list of variable assignments. 3841556Srgrimes */ 3851556Srgrimes 3861556Srgrimesvoid 387216870Sjilleslistsetvar(struct strlist *list, int flags) 38890111Simp{ 3891556Srgrimes struct strlist *lp; 3901556Srgrimes 3911556Srgrimes INTOFF; 3921556Srgrimes for (lp = list ; lp ; lp = lp->next) { 393216870Sjilles setvareq(savestr(lp->text), flags); 3941556Srgrimes } 3951556Srgrimes INTON; 3961556Srgrimes} 3971556Srgrimes 3981556Srgrimes 3991556Srgrimes 4001556Srgrimes/* 4011556Srgrimes * Find the value of a variable. Returns NULL if not set. 4021556Srgrimes */ 4031556Srgrimes 4041556Srgrimeschar * 405200956Sjilleslookupvar(const char *name) 40690111Simp{ 4071556Srgrimes struct var *v; 4081556Srgrimes 409221668Sjilles v = find_var(name, NULL, NULL); 410221668Sjilles if (v == NULL || v->flags & VUNSET) 411221668Sjilles return NULL; 412221668Sjilles return v->text + v->name_len + 1; 4131556Srgrimes} 4141556Srgrimes 4151556Srgrimes 4161556Srgrimes 4171556Srgrimes/* 4181556Srgrimes * Search the environment of a builtin command. If the second argument 4191556Srgrimes * is nonzero, return the value of a variable even if it hasn't been 4201556Srgrimes * exported. 4211556Srgrimes */ 4221556Srgrimes 4231556Srgrimeschar * 424200956Sjillesbltinlookup(const char *name, int doall) 42517987Speter{ 4261556Srgrimes struct strlist *sp; 4271556Srgrimes struct var *v; 428212467Sjilles char *result; 4291556Srgrimes 430212467Sjilles result = NULL; 4311556Srgrimes for (sp = cmdenviron ; sp ; sp = sp->next) { 4321556Srgrimes if (varequal(sp->text, name)) 433212467Sjilles result = strchr(sp->text, '=') + 1; 4341556Srgrimes } 435212467Sjilles if (result != NULL) 436212467Sjilles return result; 437221668Sjilles 438221668Sjilles v = find_var(name, NULL, NULL); 439221668Sjilles if (v == NULL || v->flags & VUNSET || 440221668Sjilles (!doall && (v->flags & VEXPORT) == 0)) 441221668Sjilles return NULL; 442221668Sjilles return v->text + v->name_len + 1; 4431556Srgrimes} 4441556Srgrimes 4451556Srgrimes 446207678Sjilles/* 447207678Sjilles * Set up locale for a builtin (LANG/LC_* assignments). 448207678Sjilles */ 449207678Sjillesvoid 450207678Sjillesbltinsetlocale(void) 451207678Sjilles{ 452207678Sjilles struct strlist *lp; 453207678Sjilles int act = 0; 454207678Sjilles char *loc, *locdef; 455207678Sjilles int i; 4561556Srgrimes 457207678Sjilles for (lp = cmdenviron ; lp ; lp = lp->next) { 458207678Sjilles if (localevar(lp->text)) { 459207678Sjilles act = 1; 460207678Sjilles break; 461207678Sjilles } 462207678Sjilles } 463207678Sjilles if (!act) 464207678Sjilles return; 465207678Sjilles loc = bltinlookup("LC_ALL", 0); 466207678Sjilles INTOFF; 467207678Sjilles if (loc != NULL) { 468207678Sjilles setlocale(LC_ALL, loc); 469207678Sjilles INTON; 470221559Sjilles updatecharset(); 471207678Sjilles return; 472207678Sjilles } 473207678Sjilles locdef = bltinlookup("LANG", 0); 474207678Sjilles for (i = 0; locale_names[i] != NULL; i++) { 475207678Sjilles loc = bltinlookup(locale_names[i], 0); 476207678Sjilles if (loc == NULL) 477207678Sjilles loc = locdef; 478207678Sjilles if (loc != NULL) 479207678Sjilles setlocale(locale_categories[i], loc); 480207678Sjilles } 481207678Sjilles INTON; 482221559Sjilles updatecharset(); 483207678Sjilles} 484207678Sjilles 4851556Srgrimes/* 486207678Sjilles * Undo the effect of bltinlocaleset(). 487207678Sjilles */ 488207678Sjillesvoid 489207678Sjillesbltinunsetlocale(void) 490207678Sjilles{ 491207678Sjilles struct strlist *lp; 492207678Sjilles 493207678Sjilles INTOFF; 494207678Sjilles for (lp = cmdenviron ; lp ; lp = lp->next) { 495207678Sjilles if (localevar(lp->text)) { 496207678Sjilles setlocale(LC_ALL, ""); 497221559Sjilles updatecharset(); 498207678Sjilles return; 499207678Sjilles } 500207678Sjilles } 501207678Sjilles INTON; 502207678Sjilles} 503207678Sjilles 504221559Sjilles/* 505221559Sjilles * Update the localeisutf8 flag. 506221559Sjilles */ 507221559Sjillesvoid 508221559Sjillesupdatecharset(void) 509221559Sjilles{ 510221559Sjilles char *charset; 511207678Sjilles 512221559Sjilles charset = nl_langinfo(CODESET); 513221559Sjilles localeisutf8 = !strcmp(charset, "UTF-8"); 514221559Sjilles} 515221559Sjilles 516221669Sjillesvoid 517221669Sjillesinitcharset(void) 518221669Sjilles{ 519221669Sjilles updatecharset(); 520221669Sjilles initial_localeisutf8 = localeisutf8; 521221669Sjilles} 522221669Sjilles 523207678Sjilles/* 5241556Srgrimes * Generate a list of exported variables. This routine is used to construct 5251556Srgrimes * the third argument to execve when executing a program. 5261556Srgrimes */ 5271556Srgrimes 5281556Srgrimeschar ** 52990111Simpenvironment(void) 53090111Simp{ 5311556Srgrimes int nenv; 5321556Srgrimes struct var **vpp; 5331556Srgrimes struct var *vp; 5341556Srgrimes char **env, **ep; 5351556Srgrimes 5361556Srgrimes nenv = 0; 5371556Srgrimes for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) { 5381556Srgrimes for (vp = *vpp ; vp ; vp = vp->next) 5391556Srgrimes if (vp->flags & VEXPORT) 5401556Srgrimes nenv++; 5411556Srgrimes } 5421556Srgrimes ep = env = stalloc((nenv + 1) * sizeof *env); 5431556Srgrimes for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) { 5441556Srgrimes for (vp = *vpp ; vp ; vp = vp->next) 5451556Srgrimes if (vp->flags & VEXPORT) 5461556Srgrimes *ep++ = vp->text; 5471556Srgrimes } 5481556Srgrimes *ep = NULL; 5491556Srgrimes return env; 5501556Srgrimes} 5511556Srgrimes 5521556Srgrimes 553213811Sobrienstatic int 554158145Sstefanfvar_compare(const void *a, const void *b) 555158145Sstefanf{ 556158145Sstefanf const char *const *sa, *const *sb; 5571556Srgrimes 558158145Sstefanf sa = a; 559158145Sstefanf sb = b; 560158145Sstefanf /* 561158145Sstefanf * This compares two var=value strings which creates a different 562158145Sstefanf * order from what you would probably expect. POSIX is somewhat 563158145Sstefanf * ambiguous on what should be sorted exactly. 564158145Sstefanf */ 565158145Sstefanf return strcoll(*sa, *sb); 566158145Sstefanf} 567158145Sstefanf 568158145Sstefanf 5691556Srgrimes/* 570217847Sjilles * Command to list all variables which are set. This is invoked from the 571217847Sjilles * set command when it is called without any options or operands. 5721556Srgrimes */ 5731556Srgrimes 5741556Srgrimesint 57590111Simpshowvarscmd(int argc __unused, char **argv __unused) 57617987Speter{ 5771556Srgrimes struct var **vpp; 5781556Srgrimes struct var *vp; 57997915Stjr const char *s; 580158145Sstefanf const char **vars; 581158145Sstefanf int i, n; 5821556Srgrimes 583158145Sstefanf /* 584158145Sstefanf * POSIX requires us to sort the variables. 585158145Sstefanf */ 586158145Sstefanf n = 0; 587158145Sstefanf for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) { 588158145Sstefanf for (vp = *vpp; vp; vp = vp->next) { 589158145Sstefanf if (!(vp->flags & VUNSET)) 590158145Sstefanf n++; 5911556Srgrimes } 5921556Srgrimes } 593158145Sstefanf 594231001Sjilles INTOFF; 595158145Sstefanf vars = ckmalloc(n * sizeof(*vars)); 596158145Sstefanf i = 0; 597158145Sstefanf for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) { 598158145Sstefanf for (vp = *vpp; vp; vp = vp->next) { 599158145Sstefanf if (!(vp->flags & VUNSET)) 600158145Sstefanf vars[i++] = vp->text; 601158145Sstefanf } 602158145Sstefanf } 603158145Sstefanf 604158145Sstefanf qsort(vars, n, sizeof(*vars), var_compare); 605158145Sstefanf for (i = 0; i < n; i++) { 606223183Sjilles /* 607223183Sjilles * Skip improper variable names so the output remains usable as 608223183Sjilles * shell input. 609223183Sjilles */ 610223183Sjilles if (!isassignment(vars[i])) 611223183Sjilles continue; 612215567Sjilles s = strchr(vars[i], '='); 613215567Sjilles s++; 614215567Sjilles outbin(vars[i], s - vars[i], out1); 615215567Sjilles out1qstr(s); 616158145Sstefanf out1c('\n'); 617158145Sstefanf } 618158145Sstefanf ckfree(vars); 619231001Sjilles INTON; 620158145Sstefanf 6211556Srgrimes return 0; 6221556Srgrimes} 6231556Srgrimes 6241556Srgrimes 6251556Srgrimes 6261556Srgrimes/* 6271556Srgrimes * The export and readonly commands. 6281556Srgrimes */ 6291556Srgrimes 6301556Srgrimesint 631240541Sjillesexportcmd(int argc __unused, char **argv) 63217987Speter{ 6331556Srgrimes struct var **vpp; 6341556Srgrimes struct var *vp; 635240541Sjilles char **ap; 6361556Srgrimes char *name; 6371556Srgrimes char *p; 63897914Stjr char *cmdname; 63997914Stjr int ch, values; 6401556Srgrimes int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT; 6411556Srgrimes 64297914Stjr cmdname = argv[0]; 64397914Stjr values = 0; 644240541Sjilles while ((ch = nextopt("p")) != '\0') { 64597914Stjr switch (ch) { 64697914Stjr case 'p': 64797914Stjr values = 1; 64897914Stjr break; 64997914Stjr } 65097914Stjr } 65197914Stjr 652240541Sjilles if (values && *argptr != NULL) 653149919Sstefanf error("-p requires no arguments"); 654240541Sjilles if (*argptr != NULL) { 655240541Sjilles for (ap = argptr; (name = *ap) != NULL; ap++) { 6561556Srgrimes if ((p = strchr(name, '=')) != NULL) { 6571556Srgrimes p++; 6581556Srgrimes } else { 659221668Sjilles vp = find_var(name, NULL, NULL); 660221668Sjilles if (vp != NULL) { 661221668Sjilles vp->flags |= flag; 662221668Sjilles if ((vp->flags & VEXPORT) && localevar(vp->text)) { 663221668Sjilles change_env(vp->text, 1); 664221668Sjilles (void) setlocale(LC_ALL, ""); 665221668Sjilles updatecharset(); 6661556Srgrimes } 667221668Sjilles continue; 6681556Srgrimes } 6691556Srgrimes } 6701556Srgrimes setvar(name, p, flag); 6711556Srgrimes } 6721556Srgrimes } else { 6731556Srgrimes for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) { 6741556Srgrimes for (vp = *vpp ; vp ; vp = vp->next) { 6751556Srgrimes if (vp->flags & flag) { 67697914Stjr if (values) { 677223183Sjilles /* 678223183Sjilles * Skip improper variable names 679223183Sjilles * so the output remains usable 680223183Sjilles * as shell input. 681223183Sjilles */ 682223183Sjilles if (!isassignment(vp->text)) 683223183Sjilles continue; 68497914Stjr out1str(cmdname); 68597914Stjr out1c(' '); 68697914Stjr } 68797914Stjr if (values && !(vp->flags & VUNSET)) { 688221975Sjilles outbin(vp->text, 689221975Sjilles vp->name_len + 1, out1); 690221975Sjilles out1qstr(vp->text + 691221975Sjilles vp->name_len + 1); 692215567Sjilles } else 693221975Sjilles outbin(vp->text, vp->name_len, 694215567Sjilles out1); 6951556Srgrimes out1c('\n'); 6961556Srgrimes } 6971556Srgrimes } 6981556Srgrimes } 6991556Srgrimes } 7001556Srgrimes return 0; 7011556Srgrimes} 7021556Srgrimes 7031556Srgrimes 7041556Srgrimes/* 7051556Srgrimes * The "local" command. 7061556Srgrimes */ 7071556Srgrimes 70817987Speterint 70990111Simplocalcmd(int argc __unused, char **argv __unused) 71017987Speter{ 7111556Srgrimes char *name; 7121556Srgrimes 7131556Srgrimes if (! in_function()) 7141556Srgrimes error("Not in a function"); 7151556Srgrimes while ((name = *argptr++) != NULL) { 7161556Srgrimes mklocal(name); 7171556Srgrimes } 7181556Srgrimes return 0; 7191556Srgrimes} 7201556Srgrimes 7211556Srgrimes 7221556Srgrimes/* 7231556Srgrimes * Make a variable a local variable. When a variable is made local, it's 7241556Srgrimes * value and flags are saved in a localvar structure. The saved values 7251556Srgrimes * will be restored when the shell function returns. We handle the name 7261556Srgrimes * "-" as a special case. 7271556Srgrimes */ 7281556Srgrimes 7291556Srgrimesvoid 73090111Simpmklocal(char *name) 73190111Simp{ 7321556Srgrimes struct localvar *lvp; 7331556Srgrimes struct var **vpp; 7341556Srgrimes struct var *vp; 7351556Srgrimes 7361556Srgrimes INTOFF; 7371556Srgrimes lvp = ckmalloc(sizeof (struct localvar)); 7381556Srgrimes if (name[0] == '-' && name[1] == '\0') { 7391556Srgrimes lvp->text = ckmalloc(sizeof optlist); 74017987Speter memcpy(lvp->text, optlist, sizeof optlist); 7411556Srgrimes vp = NULL; 7421556Srgrimes } else { 743221668Sjilles vp = find_var(name, &vpp, NULL); 7441556Srgrimes if (vp == NULL) { 7451556Srgrimes if (strchr(name, '=')) 746223024Sjilles setvareq(savestr(name), VSTRFIXED | VNOLOCAL); 7471556Srgrimes else 748223024Sjilles setvar(name, NULL, VSTRFIXED | VNOLOCAL); 7491556Srgrimes vp = *vpp; /* the new variable */ 7501556Srgrimes lvp->text = NULL; 7511556Srgrimes lvp->flags = VUNSET; 7521556Srgrimes } else { 7531556Srgrimes lvp->text = vp->text; 7541556Srgrimes lvp->flags = vp->flags; 7551556Srgrimes vp->flags |= VSTRFIXED|VTEXTFIXED; 756221668Sjilles if (name[vp->name_len] == '=') 757223024Sjilles setvareq(savestr(name), VNOLOCAL); 7581556Srgrimes } 7591556Srgrimes } 7601556Srgrimes lvp->vp = vp; 7611556Srgrimes lvp->next = localvars; 7621556Srgrimes localvars = lvp; 7631556Srgrimes INTON; 7641556Srgrimes} 7651556Srgrimes 7661556Srgrimes 7671556Srgrimes/* 7681556Srgrimes * Called after a function returns. 7691556Srgrimes */ 7701556Srgrimes 7711556Srgrimesvoid 77290111Simppoplocalvars(void) 77390111Simp{ 7741556Srgrimes struct localvar *lvp; 7751556Srgrimes struct var *vp; 7761556Srgrimes 7771556Srgrimes while ((lvp = localvars) != NULL) { 7781556Srgrimes localvars = lvp->next; 7791556Srgrimes vp = lvp->vp; 7801556Srgrimes if (vp == NULL) { /* $- saved */ 78117987Speter memcpy(optlist, lvp->text, sizeof optlist); 7821556Srgrimes ckfree(lvp->text); 783215266Sjilles optschanged(); 7841556Srgrimes } else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) { 7851556Srgrimes (void)unsetvar(vp->text); 7861556Srgrimes } else { 7871556Srgrimes if ((vp->flags & VTEXTFIXED) == 0) 7881556Srgrimes ckfree(vp->text); 7891556Srgrimes vp->flags = lvp->flags; 7901556Srgrimes vp->text = lvp->text; 7911556Srgrimes } 7921556Srgrimes ckfree(lvp); 7931556Srgrimes } 7941556Srgrimes} 7951556Srgrimes 7961556Srgrimes 79717987Speterint 79890111Simpsetvarcmd(int argc, char **argv) 79917987Speter{ 8001556Srgrimes if (argc <= 2) 8011556Srgrimes return unsetcmd(argc, argv); 8021556Srgrimes else if (argc == 3) 8031556Srgrimes setvar(argv[1], argv[2], 0); 8041556Srgrimes else 805214538Sjilles error("too many arguments"); 8061556Srgrimes return 0; 8071556Srgrimes} 8081556Srgrimes 8091556Srgrimes 8101556Srgrimes/* 811217847Sjilles * The unset builtin command. 8121556Srgrimes */ 8131556Srgrimes 81417987Speterint 81590111Simpunsetcmd(int argc __unused, char **argv __unused) 81617987Speter{ 8171556Srgrimes char **ap; 8181556Srgrimes int i; 8191556Srgrimes int flg_func = 0; 8201556Srgrimes int flg_var = 0; 8211556Srgrimes int ret = 0; 8221556Srgrimes 8231556Srgrimes while ((i = nextopt("vf")) != '\0') { 8241556Srgrimes if (i == 'f') 8251556Srgrimes flg_func = 1; 8261556Srgrimes else 8271556Srgrimes flg_var = 1; 8281556Srgrimes } 8291556Srgrimes if (flg_func == 0 && flg_var == 0) 8301556Srgrimes flg_var = 1; 8318855Srgrimes 8321556Srgrimes for (ap = argptr; *ap ; ap++) { 8331556Srgrimes if (flg_func) 8341556Srgrimes ret |= unsetfunc(*ap); 8351556Srgrimes if (flg_var) 8361556Srgrimes ret |= unsetvar(*ap); 8371556Srgrimes } 8381556Srgrimes return ret; 8391556Srgrimes} 8401556Srgrimes 8411556Srgrimes 8421556Srgrimes/* 8431556Srgrimes * Unset the specified variable. 8441556Srgrimes */ 8451556Srgrimes 84620425Ssteveint 847200956Sjillesunsetvar(const char *s) 84890111Simp{ 8491556Srgrimes struct var **vpp; 8501556Srgrimes struct var *vp; 8511556Srgrimes 852221668Sjilles vp = find_var(s, &vpp, NULL); 853221668Sjilles if (vp == NULL) 854221668Sjilles return (0); 855221668Sjilles if (vp->flags & VREADONLY) 856221668Sjilles return (1); 857221668Sjilles INTOFF; 858221668Sjilles if (vp->text[vp->name_len + 1] != '\0') 859221668Sjilles setvar(s, nullstr, 0); 860221668Sjilles if ((vp->flags & VEXPORT) && localevar(vp->text)) { 861221668Sjilles change_env(s, 0); 862221668Sjilles setlocale(LC_ALL, ""); 863221668Sjilles updatecharset(); 8641556Srgrimes } 865221668Sjilles vp->flags &= ~VEXPORT; 866221668Sjilles vp->flags |= VUNSET; 867221668Sjilles if ((vp->flags & VSTRFIXED) == 0) { 868221668Sjilles if ((vp->flags & VTEXTFIXED) == 0) 869221668Sjilles ckfree(vp->text); 870221668Sjilles *vpp = vp->next; 871221668Sjilles ckfree(vp); 872221668Sjilles } 873221668Sjilles INTON; 874135856Sdes return (0); 8751556Srgrimes} 8761556Srgrimes 8771556Srgrimes 8781556Srgrimes 8791556Srgrimes/* 880250422Seadler * Returns true if the two strings specify the same variable. The first 8811556Srgrimes * variable name is terminated by '='; the second may be terminated by 8821556Srgrimes * either '=' or '\0'. 8831556Srgrimes */ 8841556Srgrimes 885213811Sobrienstatic int 886200956Sjillesvarequal(const char *p, const char *q) 88790111Simp{ 8881556Srgrimes while (*p == *q++) { 8891556Srgrimes if (*p++ == '=') 8901556Srgrimes return 1; 8911556Srgrimes } 8921556Srgrimes if (*p == '=' && *(q - 1) == '\0') 8931556Srgrimes return 1; 8941556Srgrimes return 0; 8951556Srgrimes} 896221668Sjilles 897221668Sjilles/* 898221668Sjilles * Search for a variable. 899221668Sjilles * 'name' may be terminated by '=' or a NUL. 900221668Sjilles * vppp is set to the pointer to vp, or the list head if vp isn't found 901250422Seadler * lenp is set to the number of characters in 'name' 902221668Sjilles */ 903221668Sjilles 904221668Sjillesstatic struct var * 905221668Sjillesfind_var(const char *name, struct var ***vppp, int *lenp) 906221668Sjilles{ 907221668Sjilles unsigned int hashval; 908221668Sjilles int len; 909221668Sjilles struct var *vp, **vpp; 910221668Sjilles const char *p = name; 911221668Sjilles 912221668Sjilles hashval = 0; 913221668Sjilles while (*p && *p != '=') 914221668Sjilles hashval = 2 * hashval + (unsigned char)*p++; 915221668Sjilles len = p - name; 916221668Sjilles 917221668Sjilles if (lenp) 918221668Sjilles *lenp = len; 919221668Sjilles vpp = &vartab[hashval % VTABSIZE]; 920221668Sjilles if (vppp) 921221668Sjilles *vppp = vpp; 922221668Sjilles 923221668Sjilles for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) { 924221668Sjilles if (vp->name_len != len) 925221668Sjilles continue; 926221668Sjilles if (memcmp(vp->text, name, len) != 0) 927221668Sjilles continue; 928221668Sjilles if (vppp) 929221668Sjilles *vppp = vpp; 930221668Sjilles return vp; 931221668Sjilles } 932221668Sjilles return NULL; 933221668Sjilles} 934