var.c revision 201056
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 201056 2009-12-27 18:32:44Z jilles $"); 401556Srgrimes 4117987Speter#include <unistd.h> 4217987Speter#include <stdlib.h> 43114763Sobrien#include <paths.h> 4417987Speter 451556Srgrimes/* 461556Srgrimes * Shell variables. 471556Srgrimes */ 481556Srgrimes 4917525Sache#include <locale.h> 5017525Sache 511556Srgrimes#include "shell.h" 521556Srgrimes#include "output.h" 531556Srgrimes#include "expand.h" 541556Srgrimes#include "nodes.h" /* for other headers */ 551556Srgrimes#include "eval.h" /* defines cmdenviron */ 561556Srgrimes#include "exec.h" 571556Srgrimes#include "syntax.h" 581556Srgrimes#include "options.h" 591556Srgrimes#include "mail.h" 601556Srgrimes#include "var.h" 611556Srgrimes#include "memalloc.h" 621556Srgrimes#include "error.h" 631556Srgrimes#include "mystring.h" 6420425Ssteve#include "parser.h" 6517987Speter#ifndef NO_HISTORY 6617987Speter#include "myhistedit.h" 6717987Speter#endif 681556Srgrimes 691556Srgrimes 701556Srgrimes#define VTABSIZE 39 711556Srgrimes 721556Srgrimes 731556Srgrimesstruct varinit { 741556Srgrimes struct var *var; 751556Srgrimes int flags; 76201056Sjilles const char *text; 7790111Simp void (*func)(const char *); 781556Srgrimes}; 791556Srgrimes 801556Srgrimes 8117987Speter#ifndef NO_HISTORY 821556Srgrimesstruct var vhistsize; 8317987Speter#endif 841556Srgrimesstruct var vifs; 851556Srgrimesstruct var vmail; 861556Srgrimesstruct var vmpath; 871556Srgrimesstruct var vpath; 8897689Stjrstruct var vppid; 891556Srgrimesstruct var vps1; 901556Srgrimesstruct var vps2; 91159632Sstefanfstruct var vps4; 921556Srgrimesstruct var vvers; 93117261SddsSTATIC struct var voptind; 941556Srgrimes 95117261SddsSTATIC const struct varinit varinit[] = { 9617987Speter#ifndef NO_HISTORY 97201056Sjilles { &vhistsize, VUNSET, "HISTSIZE=", 9820425Ssteve sethistsize }, 9917987Speter#endif 100201056Sjilles { &vifs, 0, "IFS= \t\n", 10120425Ssteve NULL }, 102201056Sjilles { &vmail, VUNSET, "MAIL=", 10320425Ssteve NULL }, 104201056Sjilles { &vmpath, VUNSET, "MAILPATH=", 10520425Ssteve NULL }, 106201056Sjilles { &vpath, 0, "PATH=" _PATH_DEFPATH, 10720425Ssteve changepath }, 108201056Sjilles { &vppid, VUNSET, "PPID=", 10997689Stjr NULL }, 1108855Srgrimes /* 1111556Srgrimes * vps1 depends on uid 1121556Srgrimes */ 113201056Sjilles { &vps2, 0, "PS2=> ", 11420425Ssteve NULL }, 115201056Sjilles { &vps4, 0, "PS4=+ ", 116159632Sstefanf NULL }, 117201056Sjilles { &voptind, 0, "OPTIND=1", 11820425Ssteve getoptsreset }, 11920425Ssteve { NULL, 0, NULL, 12020425Ssteve NULL } 1211556Srgrimes}; 1221556Srgrimes 123117261SddsSTATIC struct var *vartab[VTABSIZE]; 1241556Srgrimes 125200956SjillesSTATIC struct var **hashvar(const char *); 126200956SjillesSTATIC int varequal(const char *, const char *); 127200956SjillesSTATIC int localevar(const char *); 1281556Srgrimes 1291556Srgrimes/* 130155302Sschweikh * Initialize the variable symbol tables and import the environment. 1311556Srgrimes */ 1321556Srgrimes 1331556Srgrimes#ifdef mkinit 1341556SrgrimesINCLUDE "var.h" 135201053SjillesMKINIT char **environ; 1361556SrgrimesINIT { 1371556Srgrimes char **envp; 1381556Srgrimes 1391556Srgrimes initvar(); 1401556Srgrimes for (envp = environ ; *envp ; envp++) { 1411556Srgrimes if (strchr(*envp, '=')) { 1421556Srgrimes setvareq(*envp, VEXPORT|VTEXTFIXED); 1431556Srgrimes } 1441556Srgrimes } 1451556Srgrimes} 1461556Srgrimes#endif 1471556Srgrimes 1481556Srgrimes 1491556Srgrimes/* 1501556Srgrimes * This routine initializes the builtin variables. It is called when the 1511556Srgrimes * shell is initialized and again when a shell procedure is spawned. 1521556Srgrimes */ 1531556Srgrimes 1541556Srgrimesvoid 15590111Simpinitvar(void) 15690111Simp{ 15797689Stjr char ppid[20]; 1581556Srgrimes const struct varinit *ip; 1591556Srgrimes struct var *vp; 1601556Srgrimes struct var **vpp; 1611556Srgrimes 1621556Srgrimes for (ip = varinit ; (vp = ip->var) != NULL ; ip++) { 1631556Srgrimes if ((vp->flags & VEXPORT) == 0) { 1641556Srgrimes vpp = hashvar(ip->text); 1651556Srgrimes vp->next = *vpp; 1661556Srgrimes *vpp = vp; 167201056Sjilles vp->text = __DECONST(char *, ip->text); 168201056Sjilles vp->flags = ip->flags | VSTRFIXED | VTEXTFIXED; 16920425Ssteve vp->func = ip->func; 1701556Srgrimes } 1711556Srgrimes } 1721556Srgrimes /* 1731556Srgrimes * PS1 depends on uid 1741556Srgrimes */ 1751556Srgrimes if ((vps1.flags & VEXPORT) == 0) { 1761556Srgrimes vpp = hashvar("PS1="); 1771556Srgrimes vps1.next = *vpp; 1781556Srgrimes *vpp = &vps1; 179201056Sjilles vps1.text = __DECONST(char *, geteuid() ? "PS1=$ " : "PS1=# "); 1801556Srgrimes vps1.flags = VSTRFIXED|VTEXTFIXED; 1811556Srgrimes } 18297689Stjr if ((vppid.flags & VEXPORT) == 0) { 18397689Stjr fmtstr(ppid, sizeof(ppid), "%d", (int)getppid()); 18497689Stjr setvarsafe("PPID", ppid, 0); 18597689Stjr } 1861556Srgrimes} 1871556Srgrimes 1881556Srgrimes/* 18920425Ssteve * Safe version of setvar, returns 1 on success 0 on failure. 19020425Ssteve */ 19120425Ssteve 19220425Ssteveint 193200956Sjillessetvarsafe(const char *name, const char *val, int flags) 19420425Ssteve{ 19520425Ssteve struct jmploc jmploc; 196194765Sjilles struct jmploc *const savehandler = handler; 19720425Ssteve int err = 0; 198199660Sjilles int inton; 19920425Ssteve 200199660Sjilles inton = is_int_on(); 20120425Ssteve if (setjmp(jmploc.loc)) 20220425Ssteve err = 1; 20320425Ssteve else { 20420425Ssteve handler = &jmploc; 20520425Ssteve setvar(name, val, flags); 20620425Ssteve } 20720425Ssteve handler = savehandler; 208199660Sjilles SETINTON(inton); 20920425Ssteve return err; 21020425Ssteve} 21120425Ssteve 21220425Ssteve/* 213155302Sschweikh * Set the value of a variable. The flags argument is stored with the 2141556Srgrimes * flags of the variable. If val is NULL, the variable is unset. 2151556Srgrimes */ 2161556Srgrimes 2171556Srgrimesvoid 218200956Sjillessetvar(const char *name, const char *val, int flags) 21917987Speter{ 220200956Sjilles const char *p; 2211556Srgrimes int len; 2221556Srgrimes int namelen; 2231556Srgrimes char *nameeq; 2241556Srgrimes int isbad; 2251556Srgrimes 2261556Srgrimes isbad = 0; 2271556Srgrimes p = name; 22817525Sache if (! is_name(*p)) 2291556Srgrimes isbad = 1; 23017525Sache p++; 2311556Srgrimes for (;;) { 2321556Srgrimes if (! is_in_name(*p)) { 2331556Srgrimes if (*p == '\0' || *p == '=') 2341556Srgrimes break; 2351556Srgrimes isbad = 1; 2361556Srgrimes } 2371556Srgrimes p++; 2381556Srgrimes } 2391556Srgrimes namelen = p - name; 2401556Srgrimes if (isbad) 2411556Srgrimes error("%.*s: bad variable name", namelen, name); 2421556Srgrimes len = namelen + 2; /* 2 is space for '=' and '\0' */ 2431556Srgrimes if (val == NULL) { 2441556Srgrimes flags |= VUNSET; 2451556Srgrimes } else { 2461556Srgrimes len += strlen(val); 2471556Srgrimes } 248200956Sjilles nameeq = ckmalloc(len); 249200956Sjilles memcpy(nameeq, name, namelen); 250200956Sjilles nameeq[namelen] = '='; 2511556Srgrimes if (val) 252200956Sjilles scopy(val, nameeq + namelen + 1); 253200956Sjilles else 254200956Sjilles nameeq[namelen + 1] = '\0'; 2551556Srgrimes setvareq(nameeq, flags); 2561556Srgrimes} 2571556Srgrimes 25817525SacheSTATIC int 259200956Sjilleslocalevar(const char *s) 26090111Simp{ 261201053Sjilles static const char *lnames[7] = { 26217525Sache "ALL", "COLLATE", "CTYPE", "MONETARY", 26317525Sache "NUMERIC", "TIME", NULL 26417525Sache }; 265201053Sjilles const char **ss; 2661556Srgrimes 26717525Sache if (*s != 'L') 26817525Sache return 0; 26917525Sache if (varequal(s + 1, "ANG")) 27017525Sache return 1; 27117525Sache if (strncmp(s + 1, "C_", 2) != 0) 27217525Sache return 0; 27317525Sache for (ss = lnames; *ss ; ss++) 27417525Sache if (varequal(s + 3, *ss)) 27517525Sache return 1; 27617525Sache return 0; 27717525Sache} 2781556Srgrimes 279171268Sscf 2801556Srgrimes/* 281171268Sscf * Sets/unsets an environment variable from a pointer that may actually be a 282171268Sscf * pointer into environ where the string should not be manipulated. 283171268Sscf */ 284171268Sscfstatic void 285200956Sjilleschange_env(const char *s, int set) 286171268Sscf{ 287171268Sscf char *eqp; 288171268Sscf char *ss; 289171268Sscf 290171268Sscf ss = savestr(s); 291171268Sscf if ((eqp = strchr(ss, '=')) != NULL) 292171268Sscf *eqp = '\0'; 293171268Sscf if (set && eqp != NULL) 294171268Sscf (void) setenv(ss, eqp + 1, 1); 295171268Sscf else 296171268Sscf (void) unsetenv(ss); 297171268Sscf ckfree(ss); 298171268Sscf 299171268Sscf return; 300171268Sscf} 301171268Sscf 302171268Sscf 303171268Sscf/* 3041556Srgrimes * Same as setvar except that the variable and value are passed in 3051556Srgrimes * the first argument as name=value. Since the first argument will 3061556Srgrimes * be actually stored in the table, it should not be a string that 3071556Srgrimes * will go away. 3081556Srgrimes */ 3091556Srgrimes 3101556Srgrimesvoid 31190111Simpsetvareq(char *s, int flags) 31217987Speter{ 3131556Srgrimes struct var *vp, **vpp; 31490112Simp int len; 3151556Srgrimes 31645263Scracauer if (aflag) 31745263Scracauer flags |= VEXPORT; 3181556Srgrimes vpp = hashvar(s); 3191556Srgrimes for (vp = *vpp ; vp ; vp = vp->next) { 3201556Srgrimes if (varequal(s, vp->text)) { 3211556Srgrimes if (vp->flags & VREADONLY) { 32290112Simp len = strchr(s, '=') - s; 3231556Srgrimes error("%.*s: is read only", len, s); 3241556Srgrimes } 3251556Srgrimes INTOFF; 32620425Ssteve 32720425Ssteve if (vp->func && (flags & VNOFUNC) == 0) 32820425Ssteve (*vp->func)(strchr(s, '=') + 1); 32920425Ssteve 3301556Srgrimes if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0) 3311556Srgrimes ckfree(vp->text); 33220425Ssteve 33320425Ssteve vp->flags &= ~(VTEXTFIXED|VSTACK|VUNSET); 3341556Srgrimes vp->flags |= flags; 3351556Srgrimes vp->text = s; 33620425Ssteve 33720425Ssteve /* 33820425Ssteve * We could roll this to a function, to handle it as 33920425Ssteve * a regular variable function callback, but why bother? 34020425Ssteve */ 3411556Srgrimes if (vp == &vmpath || (vp == &vmail && ! mpathset())) 3421556Srgrimes chkmail(1); 34317525Sache if ((vp->flags & VEXPORT) && localevar(s)) { 344171268Sscf change_env(s, 1); 34517525Sache (void) setlocale(LC_ALL, ""); 34617525Sache } 3471556Srgrimes INTON; 3481556Srgrimes return; 3491556Srgrimes } 3501556Srgrimes } 3511556Srgrimes /* not found */ 3521556Srgrimes vp = ckmalloc(sizeof (*vp)); 3531556Srgrimes vp->flags = flags; 3541556Srgrimes vp->text = s; 3551556Srgrimes vp->next = *vpp; 35620425Ssteve vp->func = NULL; 35717525Sache INTOFF; 3581556Srgrimes *vpp = vp; 35917525Sache if ((vp->flags & VEXPORT) && localevar(s)) { 360171268Sscf change_env(s, 1); 36117525Sache (void) setlocale(LC_ALL, ""); 36217525Sache } 36317525Sache INTON; 3641556Srgrimes} 3651556Srgrimes 3661556Srgrimes 3671556Srgrimes 3681556Srgrimes/* 3691556Srgrimes * Process a linked list of variable assignments. 3701556Srgrimes */ 3711556Srgrimes 3721556Srgrimesvoid 37390111Simplistsetvar(struct strlist *list) 37490111Simp{ 3751556Srgrimes struct strlist *lp; 3761556Srgrimes 3771556Srgrimes INTOFF; 3781556Srgrimes for (lp = list ; lp ; lp = lp->next) { 3791556Srgrimes setvareq(savestr(lp->text), 0); 3801556Srgrimes } 3811556Srgrimes INTON; 3821556Srgrimes} 3831556Srgrimes 3841556Srgrimes 3851556Srgrimes 3861556Srgrimes/* 3871556Srgrimes * Find the value of a variable. Returns NULL if not set. 3881556Srgrimes */ 3891556Srgrimes 3901556Srgrimeschar * 391200956Sjilleslookupvar(const char *name) 39290111Simp{ 3931556Srgrimes struct var *v; 3941556Srgrimes 3951556Srgrimes for (v = *hashvar(name) ; v ; v = v->next) { 3961556Srgrimes if (varequal(v->text, name)) { 3971556Srgrimes if (v->flags & VUNSET) 3981556Srgrimes return NULL; 3991556Srgrimes return strchr(v->text, '=') + 1; 4001556Srgrimes } 4011556Srgrimes } 4021556Srgrimes return NULL; 4031556Srgrimes} 4041556Srgrimes 4051556Srgrimes 4061556Srgrimes 4071556Srgrimes/* 4081556Srgrimes * Search the environment of a builtin command. If the second argument 4091556Srgrimes * is nonzero, return the value of a variable even if it hasn't been 4101556Srgrimes * exported. 4111556Srgrimes */ 4121556Srgrimes 4131556Srgrimeschar * 414200956Sjillesbltinlookup(const char *name, int doall) 41517987Speter{ 4161556Srgrimes struct strlist *sp; 4171556Srgrimes struct var *v; 4181556Srgrimes 4191556Srgrimes for (sp = cmdenviron ; sp ; sp = sp->next) { 4201556Srgrimes if (varequal(sp->text, name)) 4211556Srgrimes return strchr(sp->text, '=') + 1; 4221556Srgrimes } 4231556Srgrimes for (v = *hashvar(name) ; v ; v = v->next) { 4241556Srgrimes if (varequal(v->text, name)) { 42517987Speter if ((v->flags & VUNSET) 42617987Speter || (!doall && (v->flags & VEXPORT) == 0)) 4271556Srgrimes return NULL; 4281556Srgrimes return strchr(v->text, '=') + 1; 4291556Srgrimes } 4301556Srgrimes } 4311556Srgrimes return NULL; 4321556Srgrimes} 4331556Srgrimes 4341556Srgrimes 4351556Srgrimes 4361556Srgrimes/* 4371556Srgrimes * Generate a list of exported variables. This routine is used to construct 4381556Srgrimes * the third argument to execve when executing a program. 4391556Srgrimes */ 4401556Srgrimes 4411556Srgrimeschar ** 44290111Simpenvironment(void) 44390111Simp{ 4441556Srgrimes int nenv; 4451556Srgrimes struct var **vpp; 4461556Srgrimes struct var *vp; 4471556Srgrimes char **env, **ep; 4481556Srgrimes 4491556Srgrimes nenv = 0; 4501556Srgrimes for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) { 4511556Srgrimes for (vp = *vpp ; vp ; vp = vp->next) 4521556Srgrimes if (vp->flags & VEXPORT) 4531556Srgrimes nenv++; 4541556Srgrimes } 4551556Srgrimes ep = env = stalloc((nenv + 1) * sizeof *env); 4561556Srgrimes for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) { 4571556Srgrimes for (vp = *vpp ; vp ; vp = vp->next) 4581556Srgrimes if (vp->flags & VEXPORT) 4591556Srgrimes *ep++ = vp->text; 4601556Srgrimes } 4611556Srgrimes *ep = NULL; 4621556Srgrimes return env; 4631556Srgrimes} 4641556Srgrimes 4651556Srgrimes 4661556Srgrimes/* 4671556Srgrimes * Called when a shell procedure is invoked to clear out nonexported 4681556Srgrimes * variables. It is also necessary to reallocate variables of with 4691556Srgrimes * VSTACK set since these are currently allocated on the stack. 4701556Srgrimes */ 4711556Srgrimes 472149016SstefanfMKINIT void shprocvar(void); 4731556Srgrimes 474201053Sjilles#ifdef mkinit 4751556SrgrimesSHELLPROC { 4761556Srgrimes shprocvar(); 4771556Srgrimes} 4781556Srgrimes#endif 4791556Srgrimes 4801556Srgrimesvoid 48190111Simpshprocvar(void) 48290111Simp{ 4831556Srgrimes struct var **vpp; 4841556Srgrimes struct var *vp, **prev; 4851556Srgrimes 4861556Srgrimes for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) { 4871556Srgrimes for (prev = vpp ; (vp = *prev) != NULL ; ) { 4881556Srgrimes if ((vp->flags & VEXPORT) == 0) { 4891556Srgrimes *prev = vp->next; 4901556Srgrimes if ((vp->flags & VTEXTFIXED) == 0) 4911556Srgrimes ckfree(vp->text); 4921556Srgrimes if ((vp->flags & VSTRFIXED) == 0) 4931556Srgrimes ckfree(vp); 4941556Srgrimes } else { 4951556Srgrimes if (vp->flags & VSTACK) { 4961556Srgrimes vp->text = savestr(vp->text); 4971556Srgrimes vp->flags &=~ VSTACK; 4981556Srgrimes } 4991556Srgrimes prev = &vp->next; 5001556Srgrimes } 5011556Srgrimes } 5021556Srgrimes } 5031556Srgrimes initvar(); 5041556Srgrimes} 5051556Srgrimes 5061556Srgrimes 507158145Sstefanfstatic int 508158145Sstefanfvar_compare(const void *a, const void *b) 509158145Sstefanf{ 510158145Sstefanf const char *const *sa, *const *sb; 5111556Srgrimes 512158145Sstefanf sa = a; 513158145Sstefanf sb = b; 514158145Sstefanf /* 515158145Sstefanf * This compares two var=value strings which creates a different 516158145Sstefanf * order from what you would probably expect. POSIX is somewhat 517158145Sstefanf * ambiguous on what should be sorted exactly. 518158145Sstefanf */ 519158145Sstefanf return strcoll(*sa, *sb); 520158145Sstefanf} 521158145Sstefanf 522158145Sstefanf 5231556Srgrimes/* 5241556Srgrimes * Command to list all variables which are set. Currently this command 5251556Srgrimes * is invoked from the set command when the set command is called without 5261556Srgrimes * any variables. 5271556Srgrimes */ 5281556Srgrimes 5291556Srgrimesint 53090111Simpshowvarscmd(int argc __unused, char **argv __unused) 53117987Speter{ 5321556Srgrimes struct var **vpp; 5331556Srgrimes struct var *vp; 53497915Stjr const char *s; 535158145Sstefanf const char **vars; 536158145Sstefanf int i, n; 5371556Srgrimes 538158145Sstefanf /* 539158145Sstefanf * POSIX requires us to sort the variables. 540158145Sstefanf */ 541158145Sstefanf n = 0; 542158145Sstefanf for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) { 543158145Sstefanf for (vp = *vpp; vp; vp = vp->next) { 544158145Sstefanf if (!(vp->flags & VUNSET)) 545158145Sstefanf n++; 5461556Srgrimes } 5471556Srgrimes } 548158145Sstefanf 549158145Sstefanf INTON; 550158145Sstefanf vars = ckmalloc(n * sizeof(*vars)); 551158145Sstefanf i = 0; 552158145Sstefanf for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) { 553158145Sstefanf for (vp = *vpp; vp; vp = vp->next) { 554158145Sstefanf if (!(vp->flags & VUNSET)) 555158145Sstefanf vars[i++] = vp->text; 556158145Sstefanf } 557158145Sstefanf } 558158145Sstefanf 559158145Sstefanf qsort(vars, n, sizeof(*vars), var_compare); 560158145Sstefanf for (i = 0; i < n; i++) { 561158145Sstefanf for (s = vars[i]; *s != '='; s++) 562158145Sstefanf out1c(*s); 563158145Sstefanf out1c('='); 564158145Sstefanf out1qstr(s + 1); 565158145Sstefanf out1c('\n'); 566158145Sstefanf } 567158145Sstefanf ckfree(vars); 568158145Sstefanf INTOFF; 569158145Sstefanf 5701556Srgrimes return 0; 5711556Srgrimes} 5721556Srgrimes 5731556Srgrimes 5741556Srgrimes 5751556Srgrimes/* 5761556Srgrimes * The export and readonly commands. 5771556Srgrimes */ 5781556Srgrimes 5791556Srgrimesint 58090111Simpexportcmd(int argc, char **argv) 58117987Speter{ 5821556Srgrimes struct var **vpp; 5831556Srgrimes struct var *vp; 5841556Srgrimes char *name; 5851556Srgrimes char *p; 58697914Stjr char *cmdname; 58797914Stjr int ch, values; 5881556Srgrimes int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT; 5891556Srgrimes 59097914Stjr cmdname = argv[0]; 59197914Stjr optreset = optind = 1; 592100663Stjr opterr = 0; 59397914Stjr values = 0; 59497914Stjr while ((ch = getopt(argc, argv, "p")) != -1) { 59597914Stjr switch (ch) { 59697914Stjr case 'p': 59797914Stjr values = 1; 59897914Stjr break; 59997914Stjr case '?': 60097914Stjr default: 60197914Stjr error("unknown option: -%c", optopt); 60297914Stjr } 60397914Stjr } 60497914Stjr argc -= optind; 60597914Stjr argv += optind; 60697914Stjr 607149919Sstefanf if (values && argc != 0) 608149919Sstefanf error("-p requires no arguments"); 60997914Stjr if (argc != 0) { 610149919Sstefanf while ((name = *argv++) != NULL) { 6111556Srgrimes if ((p = strchr(name, '=')) != NULL) { 6121556Srgrimes p++; 6131556Srgrimes } else { 6141556Srgrimes vpp = hashvar(name); 6151556Srgrimes for (vp = *vpp ; vp ; vp = vp->next) { 6161556Srgrimes if (varequal(vp->text, name)) { 61790111Simp 6181556Srgrimes vp->flags |= flag; 61917525Sache if ((vp->flags & VEXPORT) && localevar(vp->text)) { 620171268Sscf change_env(vp->text, 1); 62117525Sache (void) setlocale(LC_ALL, ""); 62217525Sache } 6231556Srgrimes goto found; 6241556Srgrimes } 6251556Srgrimes } 6261556Srgrimes } 6271556Srgrimes setvar(name, p, flag); 6281556Srgrimesfound:; 6291556Srgrimes } 6301556Srgrimes } else { 6311556Srgrimes for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) { 6321556Srgrimes for (vp = *vpp ; vp ; vp = vp->next) { 6331556Srgrimes if (vp->flags & flag) { 63497914Stjr if (values) { 63597914Stjr out1str(cmdname); 63697914Stjr out1c(' '); 63797914Stjr } 6381556Srgrimes for (p = vp->text ; *p != '=' ; p++) 6391556Srgrimes out1c(*p); 64097914Stjr if (values && !(vp->flags & VUNSET)) { 64197914Stjr out1c('='); 64297914Stjr out1qstr(p + 1); 64397914Stjr } 6441556Srgrimes out1c('\n'); 6451556Srgrimes } 6461556Srgrimes } 6471556Srgrimes } 6481556Srgrimes } 6491556Srgrimes return 0; 6501556Srgrimes} 6511556Srgrimes 6521556Srgrimes 6531556Srgrimes/* 6541556Srgrimes * The "local" command. 6551556Srgrimes */ 6561556Srgrimes 65717987Speterint 65890111Simplocalcmd(int argc __unused, char **argv __unused) 65917987Speter{ 6601556Srgrimes char *name; 6611556Srgrimes 6621556Srgrimes if (! in_function()) 6631556Srgrimes error("Not in a function"); 6641556Srgrimes while ((name = *argptr++) != NULL) { 6651556Srgrimes mklocal(name); 6661556Srgrimes } 6671556Srgrimes return 0; 6681556Srgrimes} 6691556Srgrimes 6701556Srgrimes 6711556Srgrimes/* 6721556Srgrimes * Make a variable a local variable. When a variable is made local, it's 6731556Srgrimes * value and flags are saved in a localvar structure. The saved values 6741556Srgrimes * will be restored when the shell function returns. We handle the name 6751556Srgrimes * "-" as a special case. 6761556Srgrimes */ 6771556Srgrimes 6781556Srgrimesvoid 67990111Simpmklocal(char *name) 68090111Simp{ 6811556Srgrimes struct localvar *lvp; 6821556Srgrimes struct var **vpp; 6831556Srgrimes struct var *vp; 6841556Srgrimes 6851556Srgrimes INTOFF; 6861556Srgrimes lvp = ckmalloc(sizeof (struct localvar)); 6871556Srgrimes if (name[0] == '-' && name[1] == '\0') { 6881556Srgrimes lvp->text = ckmalloc(sizeof optlist); 68917987Speter memcpy(lvp->text, optlist, sizeof optlist); 6901556Srgrimes vp = NULL; 6911556Srgrimes } else { 6921556Srgrimes vpp = hashvar(name); 6931556Srgrimes for (vp = *vpp ; vp && ! varequal(vp->text, name) ; vp = vp->next); 6941556Srgrimes if (vp == NULL) { 6951556Srgrimes if (strchr(name, '=')) 6961556Srgrimes setvareq(savestr(name), VSTRFIXED); 6971556Srgrimes else 6981556Srgrimes setvar(name, NULL, VSTRFIXED); 6991556Srgrimes vp = *vpp; /* the new variable */ 7001556Srgrimes lvp->text = NULL; 7011556Srgrimes lvp->flags = VUNSET; 7021556Srgrimes } else { 7031556Srgrimes lvp->text = vp->text; 7041556Srgrimes lvp->flags = vp->flags; 7051556Srgrimes vp->flags |= VSTRFIXED|VTEXTFIXED; 7061556Srgrimes if (strchr(name, '=')) 7071556Srgrimes setvareq(savestr(name), 0); 7081556Srgrimes } 7091556Srgrimes } 7101556Srgrimes lvp->vp = vp; 7111556Srgrimes lvp->next = localvars; 7121556Srgrimes localvars = lvp; 7131556Srgrimes INTON; 7141556Srgrimes} 7151556Srgrimes 7161556Srgrimes 7171556Srgrimes/* 7181556Srgrimes * Called after a function returns. 7191556Srgrimes */ 7201556Srgrimes 7211556Srgrimesvoid 72290111Simppoplocalvars(void) 72390111Simp{ 7241556Srgrimes struct localvar *lvp; 7251556Srgrimes struct var *vp; 7261556Srgrimes 7271556Srgrimes while ((lvp = localvars) != NULL) { 7281556Srgrimes localvars = lvp->next; 7291556Srgrimes vp = lvp->vp; 7301556Srgrimes if (vp == NULL) { /* $- saved */ 73117987Speter memcpy(optlist, lvp->text, sizeof optlist); 7321556Srgrimes ckfree(lvp->text); 7331556Srgrimes } else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) { 7341556Srgrimes (void)unsetvar(vp->text); 7351556Srgrimes } else { 7361556Srgrimes if ((vp->flags & VTEXTFIXED) == 0) 7371556Srgrimes ckfree(vp->text); 7381556Srgrimes vp->flags = lvp->flags; 7391556Srgrimes vp->text = lvp->text; 7401556Srgrimes } 7411556Srgrimes ckfree(lvp); 7421556Srgrimes } 7431556Srgrimes} 7441556Srgrimes 7451556Srgrimes 74617987Speterint 74790111Simpsetvarcmd(int argc, char **argv) 74817987Speter{ 7491556Srgrimes if (argc <= 2) 7501556Srgrimes return unsetcmd(argc, argv); 7511556Srgrimes else if (argc == 3) 7521556Srgrimes setvar(argv[1], argv[2], 0); 7531556Srgrimes else 7541556Srgrimes error("List assignment not implemented"); 7551556Srgrimes return 0; 7561556Srgrimes} 7571556Srgrimes 7581556Srgrimes 7591556Srgrimes/* 7601556Srgrimes * The unset builtin command. We unset the function before we unset the 7611556Srgrimes * variable to allow a function to be unset when there is a readonly variable 7621556Srgrimes * with the same name. 7631556Srgrimes */ 7641556Srgrimes 76517987Speterint 76690111Simpunsetcmd(int argc __unused, char **argv __unused) 76717987Speter{ 7681556Srgrimes char **ap; 7691556Srgrimes int i; 7701556Srgrimes int flg_func = 0; 7711556Srgrimes int flg_var = 0; 7721556Srgrimes int ret = 0; 7731556Srgrimes 7741556Srgrimes while ((i = nextopt("vf")) != '\0') { 7751556Srgrimes if (i == 'f') 7761556Srgrimes flg_func = 1; 7771556Srgrimes else 7781556Srgrimes flg_var = 1; 7791556Srgrimes } 7801556Srgrimes if (flg_func == 0 && flg_var == 0) 7811556Srgrimes flg_var = 1; 7828855Srgrimes 7831556Srgrimes for (ap = argptr; *ap ; ap++) { 7841556Srgrimes if (flg_func) 7851556Srgrimes ret |= unsetfunc(*ap); 7861556Srgrimes if (flg_var) 7871556Srgrimes ret |= unsetvar(*ap); 7881556Srgrimes } 7891556Srgrimes return ret; 7901556Srgrimes} 7911556Srgrimes 7921556Srgrimes 7931556Srgrimes/* 7941556Srgrimes * Unset the specified variable. 7951556Srgrimes */ 7961556Srgrimes 79720425Ssteveint 798200956Sjillesunsetvar(const char *s) 79990111Simp{ 8001556Srgrimes struct var **vpp; 8011556Srgrimes struct var *vp; 8021556Srgrimes 8031556Srgrimes vpp = hashvar(s); 8041556Srgrimes for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) { 8051556Srgrimes if (varequal(vp->text, s)) { 8061556Srgrimes if (vp->flags & VREADONLY) 8071556Srgrimes return (1); 8081556Srgrimes INTOFF; 8091556Srgrimes if (*(strchr(vp->text, '=') + 1) != '\0') 8101556Srgrimes setvar(s, nullstr, 0); 81117525Sache if ((vp->flags & VEXPORT) && localevar(vp->text)) { 812171268Sscf change_env(s, 0); 81317525Sache setlocale(LC_ALL, ""); 81417525Sache } 81520425Ssteve vp->flags &= ~VEXPORT; 8161556Srgrimes vp->flags |= VUNSET; 8171556Srgrimes if ((vp->flags & VSTRFIXED) == 0) { 8181556Srgrimes if ((vp->flags & VTEXTFIXED) == 0) 8191556Srgrimes ckfree(vp->text); 8201556Srgrimes *vpp = vp->next; 8211556Srgrimes ckfree(vp); 8221556Srgrimes } 8231556Srgrimes INTON; 8241556Srgrimes return (0); 8251556Srgrimes } 8261556Srgrimes } 8271556Srgrimes 828135856Sdes return (0); 8291556Srgrimes} 8301556Srgrimes 8311556Srgrimes 8321556Srgrimes 8331556Srgrimes/* 8341556Srgrimes * Find the appropriate entry in the hash table from the name. 8351556Srgrimes */ 8361556Srgrimes 8371556SrgrimesSTATIC struct var ** 838200956Sjilleshashvar(const char *p) 83990111Simp{ 8401556Srgrimes unsigned int hashval; 8411556Srgrimes 84220425Ssteve hashval = ((unsigned char) *p) << 4; 8431556Srgrimes while (*p && *p != '=') 84420425Ssteve hashval += (unsigned char) *p++; 8451556Srgrimes return &vartab[hashval % VTABSIZE]; 8461556Srgrimes} 8471556Srgrimes 8481556Srgrimes 8491556Srgrimes 8501556Srgrimes/* 8511556Srgrimes * Returns true if the two strings specify the same varable. The first 8521556Srgrimes * variable name is terminated by '='; the second may be terminated by 8531556Srgrimes * either '=' or '\0'. 8541556Srgrimes */ 8551556Srgrimes 8561556SrgrimesSTATIC int 857200956Sjillesvarequal(const char *p, const char *q) 85890111Simp{ 8591556Srgrimes while (*p == *q++) { 8601556Srgrimes if (*p++ == '=') 8611556Srgrimes return 1; 8621556Srgrimes } 8631556Srgrimes if (*p == '=' && *(q - 1) == '\0') 8641556Srgrimes return 1; 8651556Srgrimes return 0; 8661556Srgrimes} 867