var.c revision 218306
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 218306 2011-02-04 22:47:55Z 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; 83208755Sjillesstruct var vterm; 8417987Speter#endif 851556Srgrimesstruct var vifs; 861556Srgrimesstruct var vmail; 871556Srgrimesstruct var vmpath; 881556Srgrimesstruct var vpath; 8997689Stjrstruct var vppid; 901556Srgrimesstruct var vps1; 911556Srgrimesstruct var vps2; 92159632Sstefanfstruct var vps4; 931556Srgrimesstruct var vvers; 94213760Sobrienstatic struct var voptind; 951556Srgrimes 96213760Sobrienstatic const struct varinit varinit[] = { 9717987Speter#ifndef NO_HISTORY 98201056Sjilles { &vhistsize, VUNSET, "HISTSIZE=", 9920425Ssteve sethistsize }, 10017987Speter#endif 101201056Sjilles { &vifs, 0, "IFS= \t\n", 10220425Ssteve NULL }, 103201056Sjilles { &vmail, VUNSET, "MAIL=", 10420425Ssteve NULL }, 105201056Sjilles { &vmpath, VUNSET, "MAILPATH=", 10620425Ssteve NULL }, 107201056Sjilles { &vpath, 0, "PATH=" _PATH_DEFPATH, 10820425Ssteve changepath }, 109201056Sjilles { &vppid, VUNSET, "PPID=", 11097689Stjr NULL }, 1118855Srgrimes /* 1121556Srgrimes * vps1 depends on uid 1131556Srgrimes */ 114201056Sjilles { &vps2, 0, "PS2=> ", 11520425Ssteve NULL }, 116201056Sjilles { &vps4, 0, "PS4=+ ", 117159632Sstefanf NULL }, 118208755Sjilles#ifndef NO_HISTORY 119208755Sjilles { &vterm, VUNSET, "TERM=", 120208755Sjilles setterm }, 121208755Sjilles#endif 122201056Sjilles { &voptind, 0, "OPTIND=1", 12320425Ssteve getoptsreset }, 12420425Ssteve { NULL, 0, NULL, 12520425Ssteve NULL } 1261556Srgrimes}; 1271556Srgrimes 128213760Sobrienstatic struct var *vartab[VTABSIZE]; 1291556Srgrimes 130213760Sobrienstatic const char *const locale_names[7] = { 131207678Sjilles "LC_COLLATE", "LC_CTYPE", "LC_MONETARY", 132207678Sjilles "LC_NUMERIC", "LC_TIME", "LC_MESSAGES", NULL 133207678Sjilles}; 134213760Sobrienstatic const int locale_categories[7] = { 135207678Sjilles LC_COLLATE, LC_CTYPE, LC_MONETARY, LC_NUMERIC, LC_TIME, LC_MESSAGES, 0 136207678Sjilles}; 137207678Sjilles 138213811Sobrienstatic struct var **hashvar(const char *); 139213811Sobrienstatic int varequal(const char *, const char *); 140213811Sobrienstatic int localevar(const char *); 1411556Srgrimes 1421556Srgrimes/* 143155302Sschweikh * Initialize the variable symbol tables and import the environment. 1441556Srgrimes */ 1451556Srgrimes 1461556Srgrimes#ifdef mkinit 1471556SrgrimesINCLUDE "var.h" 148201053SjillesMKINIT char **environ; 1491556SrgrimesINIT { 1501556Srgrimes char **envp; 1511556Srgrimes 1521556Srgrimes initvar(); 1531556Srgrimes for (envp = environ ; *envp ; envp++) { 1541556Srgrimes if (strchr(*envp, '=')) { 1551556Srgrimes setvareq(*envp, VEXPORT|VTEXTFIXED); 1561556Srgrimes } 1571556Srgrimes } 1581556Srgrimes} 1591556Srgrimes#endif 1601556Srgrimes 1611556Srgrimes 1621556Srgrimes/* 1631556Srgrimes * This routine initializes the builtin variables. It is called when the 164218306Sjilles * shell is initialized. 1651556Srgrimes */ 1661556Srgrimes 1671556Srgrimesvoid 16890111Simpinitvar(void) 16990111Simp{ 17097689Stjr char ppid[20]; 1711556Srgrimes const struct varinit *ip; 1721556Srgrimes struct var *vp; 1731556Srgrimes struct var **vpp; 1741556Srgrimes 1751556Srgrimes for (ip = varinit ; (vp = ip->var) != NULL ; ip++) { 1761556Srgrimes if ((vp->flags & VEXPORT) == 0) { 1771556Srgrimes vpp = hashvar(ip->text); 1781556Srgrimes vp->next = *vpp; 1791556Srgrimes *vpp = vp; 180201056Sjilles vp->text = __DECONST(char *, ip->text); 181201056Sjilles vp->flags = ip->flags | VSTRFIXED | VTEXTFIXED; 18220425Ssteve vp->func = ip->func; 1831556Srgrimes } 1841556Srgrimes } 1851556Srgrimes /* 1861556Srgrimes * PS1 depends on uid 1871556Srgrimes */ 1881556Srgrimes if ((vps1.flags & VEXPORT) == 0) { 1891556Srgrimes vpp = hashvar("PS1="); 1901556Srgrimes vps1.next = *vpp; 1911556Srgrimes *vpp = &vps1; 192201056Sjilles vps1.text = __DECONST(char *, geteuid() ? "PS1=$ " : "PS1=# "); 1931556Srgrimes vps1.flags = VSTRFIXED|VTEXTFIXED; 1941556Srgrimes } 19597689Stjr if ((vppid.flags & VEXPORT) == 0) { 19697689Stjr fmtstr(ppid, sizeof(ppid), "%d", (int)getppid()); 19797689Stjr setvarsafe("PPID", ppid, 0); 19897689Stjr } 1991556Srgrimes} 2001556Srgrimes 2011556Srgrimes/* 20220425Ssteve * Safe version of setvar, returns 1 on success 0 on failure. 20320425Ssteve */ 20420425Ssteve 20520425Ssteveint 206200956Sjillessetvarsafe(const char *name, const char *val, int flags) 20720425Ssteve{ 20820425Ssteve struct jmploc jmploc; 209194765Sjilles struct jmploc *const savehandler = handler; 21020425Ssteve int err = 0; 211199660Sjilles int inton; 21220425Ssteve 213199660Sjilles inton = is_int_on(); 21420425Ssteve if (setjmp(jmploc.loc)) 21520425Ssteve err = 1; 21620425Ssteve else { 21720425Ssteve handler = &jmploc; 21820425Ssteve setvar(name, val, flags); 21920425Ssteve } 22020425Ssteve handler = savehandler; 221199660Sjilles SETINTON(inton); 22220425Ssteve return err; 22320425Ssteve} 22420425Ssteve 22520425Ssteve/* 226155302Sschweikh * Set the value of a variable. The flags argument is stored with the 2271556Srgrimes * flags of the variable. If val is NULL, the variable is unset. 2281556Srgrimes */ 2291556Srgrimes 2301556Srgrimesvoid 231200956Sjillessetvar(const char *name, const char *val, int flags) 23217987Speter{ 233200956Sjilles const char *p; 2341556Srgrimes int len; 2351556Srgrimes int namelen; 2361556Srgrimes char *nameeq; 2371556Srgrimes int isbad; 2381556Srgrimes 2391556Srgrimes isbad = 0; 2401556Srgrimes p = name; 24117525Sache if (! is_name(*p)) 2421556Srgrimes isbad = 1; 24317525Sache p++; 2441556Srgrimes for (;;) { 2451556Srgrimes if (! is_in_name(*p)) { 2461556Srgrimes if (*p == '\0' || *p == '=') 2471556Srgrimes break; 2481556Srgrimes isbad = 1; 2491556Srgrimes } 2501556Srgrimes p++; 2511556Srgrimes } 2521556Srgrimes namelen = p - name; 2531556Srgrimes if (isbad) 2541556Srgrimes error("%.*s: bad variable name", namelen, name); 2551556Srgrimes len = namelen + 2; /* 2 is space for '=' and '\0' */ 2561556Srgrimes if (val == NULL) { 2571556Srgrimes flags |= VUNSET; 2581556Srgrimes } else { 2591556Srgrimes len += strlen(val); 2601556Srgrimes } 261200956Sjilles nameeq = ckmalloc(len); 262200956Sjilles memcpy(nameeq, name, namelen); 263200956Sjilles nameeq[namelen] = '='; 2641556Srgrimes if (val) 265200956Sjilles scopy(val, nameeq + namelen + 1); 266200956Sjilles else 267200956Sjilles nameeq[namelen + 1] = '\0'; 2681556Srgrimes setvareq(nameeq, flags); 2691556Srgrimes} 2701556Srgrimes 271213811Sobrienstatic int 272200956Sjilleslocalevar(const char *s) 27390111Simp{ 274207678Sjilles const char *const *ss; 2751556Srgrimes 27617525Sache if (*s != 'L') 27717525Sache return 0; 27817525Sache if (varequal(s + 1, "ANG")) 27917525Sache return 1; 28017525Sache if (strncmp(s + 1, "C_", 2) != 0) 28117525Sache return 0; 282207678Sjilles if (varequal(s + 3, "ALL")) 283207678Sjilles return 1; 284207678Sjilles for (ss = locale_names; *ss ; ss++) 285207678Sjilles if (varequal(s + 3, *ss + 3)) 28617525Sache return 1; 28717525Sache return 0; 28817525Sache} 2891556Srgrimes 290171268Sscf 2911556Srgrimes/* 292171268Sscf * Sets/unsets an environment variable from a pointer that may actually be a 293171268Sscf * pointer into environ where the string should not be manipulated. 294171268Sscf */ 295213811Sobrienstatic void 296200956Sjilleschange_env(const char *s, int set) 297171268Sscf{ 298171268Sscf char *eqp; 299171268Sscf char *ss; 300171268Sscf 301171268Sscf ss = savestr(s); 302171268Sscf if ((eqp = strchr(ss, '=')) != NULL) 303171268Sscf *eqp = '\0'; 304171268Sscf if (set && eqp != NULL) 305171268Sscf (void) setenv(ss, eqp + 1, 1); 306171268Sscf else 307171268Sscf (void) unsetenv(ss); 308171268Sscf ckfree(ss); 309171268Sscf 310171268Sscf return; 311171268Sscf} 312171268Sscf 313171268Sscf 314171268Sscf/* 3151556Srgrimes * Same as setvar except that the variable and value are passed in 3161556Srgrimes * the first argument as name=value. Since the first argument will 3171556Srgrimes * be actually stored in the table, it should not be a string that 3181556Srgrimes * will go away. 3191556Srgrimes */ 3201556Srgrimes 3211556Srgrimesvoid 32290111Simpsetvareq(char *s, int flags) 32317987Speter{ 3241556Srgrimes struct var *vp, **vpp; 32590112Simp int len; 3261556Srgrimes 32745263Scracauer if (aflag) 32845263Scracauer flags |= VEXPORT; 3291556Srgrimes vpp = hashvar(s); 3301556Srgrimes for (vp = *vpp ; vp ; vp = vp->next) { 3311556Srgrimes if (varequal(s, vp->text)) { 3321556Srgrimes if (vp->flags & VREADONLY) { 33390112Simp len = strchr(s, '=') - s; 3341556Srgrimes error("%.*s: is read only", len, s); 3351556Srgrimes } 336216870Sjilles if (flags & VNOSET) 337216870Sjilles return; 3381556Srgrimes INTOFF; 33920425Ssteve 34020425Ssteve if (vp->func && (flags & VNOFUNC) == 0) 34120425Ssteve (*vp->func)(strchr(s, '=') + 1); 34220425Ssteve 3431556Srgrimes if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0) 3441556Srgrimes ckfree(vp->text); 34520425Ssteve 34620425Ssteve vp->flags &= ~(VTEXTFIXED|VSTACK|VUNSET); 3471556Srgrimes vp->flags |= flags; 3481556Srgrimes vp->text = s; 34920425Ssteve 35020425Ssteve /* 35120425Ssteve * We could roll this to a function, to handle it as 35220425Ssteve * a regular variable function callback, but why bother? 353203576Sjilles * 354203576Sjilles * Note: this assumes iflag is not set to 1 initially. 355203576Sjilles * As part of init(), this is called before arguments 356203576Sjilles * are looked at. 35720425Ssteve */ 358203576Sjilles if ((vp == &vmpath || (vp == &vmail && ! mpathset())) && 359203576Sjilles iflag == 1) 3601556Srgrimes chkmail(1); 36117525Sache if ((vp->flags & VEXPORT) && localevar(s)) { 362171268Sscf change_env(s, 1); 36317525Sache (void) setlocale(LC_ALL, ""); 36417525Sache } 3651556Srgrimes INTON; 3661556Srgrimes return; 3671556Srgrimes } 3681556Srgrimes } 3691556Srgrimes /* not found */ 370216870Sjilles if (flags & VNOSET) 371216870Sjilles return; 3721556Srgrimes vp = ckmalloc(sizeof (*vp)); 3731556Srgrimes vp->flags = flags; 3741556Srgrimes vp->text = s; 3751556Srgrimes vp->next = *vpp; 37620425Ssteve vp->func = NULL; 37717525Sache INTOFF; 3781556Srgrimes *vpp = vp; 37917525Sache if ((vp->flags & VEXPORT) && localevar(s)) { 380171268Sscf change_env(s, 1); 38117525Sache (void) setlocale(LC_ALL, ""); 38217525Sache } 38317525Sache INTON; 3841556Srgrimes} 3851556Srgrimes 3861556Srgrimes 3871556Srgrimes 3881556Srgrimes/* 3891556Srgrimes * Process a linked list of variable assignments. 3901556Srgrimes */ 3911556Srgrimes 3921556Srgrimesvoid 393216870Sjilleslistsetvar(struct strlist *list, int flags) 39490111Simp{ 3951556Srgrimes struct strlist *lp; 3961556Srgrimes 3971556Srgrimes INTOFF; 3981556Srgrimes for (lp = list ; lp ; lp = lp->next) { 399216870Sjilles setvareq(savestr(lp->text), flags); 4001556Srgrimes } 4011556Srgrimes INTON; 4021556Srgrimes} 4031556Srgrimes 4041556Srgrimes 4051556Srgrimes 4061556Srgrimes/* 4071556Srgrimes * Find the value of a variable. Returns NULL if not set. 4081556Srgrimes */ 4091556Srgrimes 4101556Srgrimeschar * 411200956Sjilleslookupvar(const char *name) 41290111Simp{ 4131556Srgrimes struct var *v; 4141556Srgrimes 4151556Srgrimes for (v = *hashvar(name) ; v ; v = v->next) { 4161556Srgrimes if (varequal(v->text, name)) { 4171556Srgrimes if (v->flags & VUNSET) 4181556Srgrimes return NULL; 4191556Srgrimes return strchr(v->text, '=') + 1; 4201556Srgrimes } 4211556Srgrimes } 4221556Srgrimes return NULL; 4231556Srgrimes} 4241556Srgrimes 4251556Srgrimes 4261556Srgrimes 4271556Srgrimes/* 4281556Srgrimes * Search the environment of a builtin command. If the second argument 4291556Srgrimes * is nonzero, return the value of a variable even if it hasn't been 4301556Srgrimes * exported. 4311556Srgrimes */ 4321556Srgrimes 4331556Srgrimeschar * 434200956Sjillesbltinlookup(const char *name, int doall) 43517987Speter{ 4361556Srgrimes struct strlist *sp; 4371556Srgrimes struct var *v; 438212467Sjilles char *result; 4391556Srgrimes 440212467Sjilles result = NULL; 4411556Srgrimes for (sp = cmdenviron ; sp ; sp = sp->next) { 4421556Srgrimes if (varequal(sp->text, name)) 443212467Sjilles result = strchr(sp->text, '=') + 1; 4441556Srgrimes } 445212467Sjilles if (result != NULL) 446212467Sjilles return result; 4471556Srgrimes for (v = *hashvar(name) ; v ; v = v->next) { 4481556Srgrimes if (varequal(v->text, name)) { 44917987Speter if ((v->flags & VUNSET) 45017987Speter || (!doall && (v->flags & VEXPORT) == 0)) 4511556Srgrimes return NULL; 4521556Srgrimes return strchr(v->text, '=') + 1; 4531556Srgrimes } 4541556Srgrimes } 4551556Srgrimes return NULL; 4561556Srgrimes} 4571556Srgrimes 4581556Srgrimes 459207678Sjilles/* 460207678Sjilles * Set up locale for a builtin (LANG/LC_* assignments). 461207678Sjilles */ 462207678Sjillesvoid 463207678Sjillesbltinsetlocale(void) 464207678Sjilles{ 465207678Sjilles struct strlist *lp; 466207678Sjilles int act = 0; 467207678Sjilles char *loc, *locdef; 468207678Sjilles int i; 4691556Srgrimes 470207678Sjilles for (lp = cmdenviron ; lp ; lp = lp->next) { 471207678Sjilles if (localevar(lp->text)) { 472207678Sjilles act = 1; 473207678Sjilles break; 474207678Sjilles } 475207678Sjilles } 476207678Sjilles if (!act) 477207678Sjilles return; 478207678Sjilles loc = bltinlookup("LC_ALL", 0); 479207678Sjilles INTOFF; 480207678Sjilles if (loc != NULL) { 481207678Sjilles setlocale(LC_ALL, loc); 482207678Sjilles INTON; 483207678Sjilles return; 484207678Sjilles } 485207678Sjilles locdef = bltinlookup("LANG", 0); 486207678Sjilles for (i = 0; locale_names[i] != NULL; i++) { 487207678Sjilles loc = bltinlookup(locale_names[i], 0); 488207678Sjilles if (loc == NULL) 489207678Sjilles loc = locdef; 490207678Sjilles if (loc != NULL) 491207678Sjilles setlocale(locale_categories[i], loc); 492207678Sjilles } 493207678Sjilles INTON; 494207678Sjilles} 495207678Sjilles 4961556Srgrimes/* 497207678Sjilles * Undo the effect of bltinlocaleset(). 498207678Sjilles */ 499207678Sjillesvoid 500207678Sjillesbltinunsetlocale(void) 501207678Sjilles{ 502207678Sjilles struct strlist *lp; 503207678Sjilles 504207678Sjilles INTOFF; 505207678Sjilles for (lp = cmdenviron ; lp ; lp = lp->next) { 506207678Sjilles if (localevar(lp->text)) { 507207678Sjilles setlocale(LC_ALL, ""); 508207678Sjilles return; 509207678Sjilles } 510207678Sjilles } 511207678Sjilles INTON; 512207678Sjilles} 513207678Sjilles 514207678Sjilles 515207678Sjilles/* 5161556Srgrimes * Generate a list of exported variables. This routine is used to construct 5171556Srgrimes * the third argument to execve when executing a program. 5181556Srgrimes */ 5191556Srgrimes 5201556Srgrimeschar ** 52190111Simpenvironment(void) 52290111Simp{ 5231556Srgrimes int nenv; 5241556Srgrimes struct var **vpp; 5251556Srgrimes struct var *vp; 5261556Srgrimes char **env, **ep; 5271556Srgrimes 5281556Srgrimes nenv = 0; 5291556Srgrimes for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) { 5301556Srgrimes for (vp = *vpp ; vp ; vp = vp->next) 5311556Srgrimes if (vp->flags & VEXPORT) 5321556Srgrimes nenv++; 5331556Srgrimes } 5341556Srgrimes ep = env = stalloc((nenv + 1) * sizeof *env); 5351556Srgrimes for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) { 5361556Srgrimes for (vp = *vpp ; vp ; vp = vp->next) 5371556Srgrimes if (vp->flags & VEXPORT) 5381556Srgrimes *ep++ = vp->text; 5391556Srgrimes } 5401556Srgrimes *ep = NULL; 5411556Srgrimes return env; 5421556Srgrimes} 5431556Srgrimes 5441556Srgrimes 545213811Sobrienstatic int 546158145Sstefanfvar_compare(const void *a, const void *b) 547158145Sstefanf{ 548158145Sstefanf const char *const *sa, *const *sb; 5491556Srgrimes 550158145Sstefanf sa = a; 551158145Sstefanf sb = b; 552158145Sstefanf /* 553158145Sstefanf * This compares two var=value strings which creates a different 554158145Sstefanf * order from what you would probably expect. POSIX is somewhat 555158145Sstefanf * ambiguous on what should be sorted exactly. 556158145Sstefanf */ 557158145Sstefanf return strcoll(*sa, *sb); 558158145Sstefanf} 559158145Sstefanf 560158145Sstefanf 5611556Srgrimes/* 562217847Sjilles * Command to list all variables which are set. This is invoked from the 563217847Sjilles * set command when it is called without any options or operands. 5641556Srgrimes */ 5651556Srgrimes 5661556Srgrimesint 56790111Simpshowvarscmd(int argc __unused, char **argv __unused) 56817987Speter{ 5691556Srgrimes struct var **vpp; 5701556Srgrimes struct var *vp; 57197915Stjr const char *s; 572158145Sstefanf const char **vars; 573158145Sstefanf int i, n; 5741556Srgrimes 575158145Sstefanf /* 576158145Sstefanf * POSIX requires us to sort the variables. 577158145Sstefanf */ 578158145Sstefanf n = 0; 579158145Sstefanf for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) { 580158145Sstefanf for (vp = *vpp; vp; vp = vp->next) { 581158145Sstefanf if (!(vp->flags & VUNSET)) 582158145Sstefanf n++; 5831556Srgrimes } 5841556Srgrimes } 585158145Sstefanf 586158145Sstefanf INTON; 587158145Sstefanf vars = ckmalloc(n * sizeof(*vars)); 588158145Sstefanf i = 0; 589158145Sstefanf for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) { 590158145Sstefanf for (vp = *vpp; vp; vp = vp->next) { 591158145Sstefanf if (!(vp->flags & VUNSET)) 592158145Sstefanf vars[i++] = vp->text; 593158145Sstefanf } 594158145Sstefanf } 595158145Sstefanf 596158145Sstefanf qsort(vars, n, sizeof(*vars), var_compare); 597158145Sstefanf for (i = 0; i < n; i++) { 598215567Sjilles s = strchr(vars[i], '='); 599215567Sjilles s++; 600215567Sjilles outbin(vars[i], s - vars[i], out1); 601215567Sjilles out1qstr(s); 602158145Sstefanf out1c('\n'); 603158145Sstefanf } 604158145Sstefanf ckfree(vars); 605158145Sstefanf INTOFF; 606158145Sstefanf 6071556Srgrimes return 0; 6081556Srgrimes} 6091556Srgrimes 6101556Srgrimes 6111556Srgrimes 6121556Srgrimes/* 6131556Srgrimes * The export and readonly commands. 6141556Srgrimes */ 6151556Srgrimes 6161556Srgrimesint 61790111Simpexportcmd(int argc, char **argv) 61817987Speter{ 6191556Srgrimes struct var **vpp; 6201556Srgrimes struct var *vp; 6211556Srgrimes char *name; 6221556Srgrimes char *p; 62397914Stjr char *cmdname; 62497914Stjr int ch, values; 6251556Srgrimes int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT; 6261556Srgrimes 62797914Stjr cmdname = argv[0]; 62897914Stjr optreset = optind = 1; 629100663Stjr opterr = 0; 63097914Stjr values = 0; 63197914Stjr while ((ch = getopt(argc, argv, "p")) != -1) { 63297914Stjr switch (ch) { 63397914Stjr case 'p': 63497914Stjr values = 1; 63597914Stjr break; 63697914Stjr case '?': 63797914Stjr default: 63897914Stjr error("unknown option: -%c", optopt); 63997914Stjr } 64097914Stjr } 64197914Stjr argc -= optind; 64297914Stjr argv += optind; 64397914Stjr 644149919Sstefanf if (values && argc != 0) 645149919Sstefanf error("-p requires no arguments"); 64697914Stjr if (argc != 0) { 647149919Sstefanf while ((name = *argv++) != NULL) { 6481556Srgrimes if ((p = strchr(name, '=')) != NULL) { 6491556Srgrimes p++; 6501556Srgrimes } else { 6511556Srgrimes vpp = hashvar(name); 6521556Srgrimes for (vp = *vpp ; vp ; vp = vp->next) { 6531556Srgrimes if (varequal(vp->text, name)) { 65490111Simp 6551556Srgrimes vp->flags |= flag; 65617525Sache if ((vp->flags & VEXPORT) && localevar(vp->text)) { 657171268Sscf change_env(vp->text, 1); 65817525Sache (void) setlocale(LC_ALL, ""); 65917525Sache } 6601556Srgrimes goto found; 6611556Srgrimes } 6621556Srgrimes } 6631556Srgrimes } 6641556Srgrimes setvar(name, p, flag); 6651556Srgrimesfound:; 6661556Srgrimes } 6671556Srgrimes } else { 6681556Srgrimes for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) { 6691556Srgrimes for (vp = *vpp ; vp ; vp = vp->next) { 6701556Srgrimes if (vp->flags & flag) { 67197914Stjr if (values) { 67297914Stjr out1str(cmdname); 67397914Stjr out1c(' '); 67497914Stjr } 675215567Sjilles p = strchr(vp->text, '='); 67697914Stjr if (values && !(vp->flags & VUNSET)) { 677215567Sjilles p++; 678215567Sjilles outbin(vp->text, p - vp->text, 679215567Sjilles out1); 680215567Sjilles out1qstr(p); 681215567Sjilles } else 682215567Sjilles outbin(vp->text, p - vp->text, 683215567Sjilles out1); 6841556Srgrimes out1c('\n'); 6851556Srgrimes } 6861556Srgrimes } 6871556Srgrimes } 6881556Srgrimes } 6891556Srgrimes return 0; 6901556Srgrimes} 6911556Srgrimes 6921556Srgrimes 6931556Srgrimes/* 6941556Srgrimes * The "local" command. 6951556Srgrimes */ 6961556Srgrimes 69717987Speterint 69890111Simplocalcmd(int argc __unused, char **argv __unused) 69917987Speter{ 7001556Srgrimes char *name; 7011556Srgrimes 7021556Srgrimes if (! in_function()) 7031556Srgrimes error("Not in a function"); 7041556Srgrimes while ((name = *argptr++) != NULL) { 7051556Srgrimes mklocal(name); 7061556Srgrimes } 7071556Srgrimes return 0; 7081556Srgrimes} 7091556Srgrimes 7101556Srgrimes 7111556Srgrimes/* 7121556Srgrimes * Make a variable a local variable. When a variable is made local, it's 7131556Srgrimes * value and flags are saved in a localvar structure. The saved values 7141556Srgrimes * will be restored when the shell function returns. We handle the name 7151556Srgrimes * "-" as a special case. 7161556Srgrimes */ 7171556Srgrimes 7181556Srgrimesvoid 71990111Simpmklocal(char *name) 72090111Simp{ 7211556Srgrimes struct localvar *lvp; 7221556Srgrimes struct var **vpp; 7231556Srgrimes struct var *vp; 7241556Srgrimes 7251556Srgrimes INTOFF; 7261556Srgrimes lvp = ckmalloc(sizeof (struct localvar)); 7271556Srgrimes if (name[0] == '-' && name[1] == '\0') { 7281556Srgrimes lvp->text = ckmalloc(sizeof optlist); 72917987Speter memcpy(lvp->text, optlist, sizeof optlist); 7301556Srgrimes vp = NULL; 7311556Srgrimes } else { 7321556Srgrimes vpp = hashvar(name); 7331556Srgrimes for (vp = *vpp ; vp && ! varequal(vp->text, name) ; vp = vp->next); 7341556Srgrimes if (vp == NULL) { 7351556Srgrimes if (strchr(name, '=')) 7361556Srgrimes setvareq(savestr(name), VSTRFIXED); 7371556Srgrimes else 7381556Srgrimes setvar(name, NULL, VSTRFIXED); 7391556Srgrimes vp = *vpp; /* the new variable */ 7401556Srgrimes lvp->text = NULL; 7411556Srgrimes lvp->flags = VUNSET; 7421556Srgrimes } else { 7431556Srgrimes lvp->text = vp->text; 7441556Srgrimes lvp->flags = vp->flags; 7451556Srgrimes vp->flags |= VSTRFIXED|VTEXTFIXED; 7461556Srgrimes if (strchr(name, '=')) 7471556Srgrimes setvareq(savestr(name), 0); 7481556Srgrimes } 7491556Srgrimes } 7501556Srgrimes lvp->vp = vp; 7511556Srgrimes lvp->next = localvars; 7521556Srgrimes localvars = lvp; 7531556Srgrimes INTON; 7541556Srgrimes} 7551556Srgrimes 7561556Srgrimes 7571556Srgrimes/* 7581556Srgrimes * Called after a function returns. 7591556Srgrimes */ 7601556Srgrimes 7611556Srgrimesvoid 76290111Simppoplocalvars(void) 76390111Simp{ 7641556Srgrimes struct localvar *lvp; 7651556Srgrimes struct var *vp; 7661556Srgrimes 7671556Srgrimes while ((lvp = localvars) != NULL) { 7681556Srgrimes localvars = lvp->next; 7691556Srgrimes vp = lvp->vp; 7701556Srgrimes if (vp == NULL) { /* $- saved */ 77117987Speter memcpy(optlist, lvp->text, sizeof optlist); 7721556Srgrimes ckfree(lvp->text); 773215266Sjilles optschanged(); 7741556Srgrimes } else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) { 7751556Srgrimes (void)unsetvar(vp->text); 7761556Srgrimes } else { 7771556Srgrimes if ((vp->flags & VTEXTFIXED) == 0) 7781556Srgrimes ckfree(vp->text); 7791556Srgrimes vp->flags = lvp->flags; 7801556Srgrimes vp->text = lvp->text; 7811556Srgrimes } 7821556Srgrimes ckfree(lvp); 7831556Srgrimes } 7841556Srgrimes} 7851556Srgrimes 7861556Srgrimes 78717987Speterint 78890111Simpsetvarcmd(int argc, char **argv) 78917987Speter{ 7901556Srgrimes if (argc <= 2) 7911556Srgrimes return unsetcmd(argc, argv); 7921556Srgrimes else if (argc == 3) 7931556Srgrimes setvar(argv[1], argv[2], 0); 7941556Srgrimes else 795214538Sjilles error("too many arguments"); 7961556Srgrimes return 0; 7971556Srgrimes} 7981556Srgrimes 7991556Srgrimes 8001556Srgrimes/* 801217847Sjilles * The unset builtin command. 8021556Srgrimes */ 8031556Srgrimes 80417987Speterint 80590111Simpunsetcmd(int argc __unused, char **argv __unused) 80617987Speter{ 8071556Srgrimes char **ap; 8081556Srgrimes int i; 8091556Srgrimes int flg_func = 0; 8101556Srgrimes int flg_var = 0; 8111556Srgrimes int ret = 0; 8121556Srgrimes 8131556Srgrimes while ((i = nextopt("vf")) != '\0') { 8141556Srgrimes if (i == 'f') 8151556Srgrimes flg_func = 1; 8161556Srgrimes else 8171556Srgrimes flg_var = 1; 8181556Srgrimes } 8191556Srgrimes if (flg_func == 0 && flg_var == 0) 8201556Srgrimes flg_var = 1; 8218855Srgrimes 8221556Srgrimes for (ap = argptr; *ap ; ap++) { 8231556Srgrimes if (flg_func) 8241556Srgrimes ret |= unsetfunc(*ap); 8251556Srgrimes if (flg_var) 8261556Srgrimes ret |= unsetvar(*ap); 8271556Srgrimes } 8281556Srgrimes return ret; 8291556Srgrimes} 8301556Srgrimes 8311556Srgrimes 8321556Srgrimes/* 8331556Srgrimes * Unset the specified variable. 8341556Srgrimes */ 8351556Srgrimes 83620425Ssteveint 837200956Sjillesunsetvar(const char *s) 83890111Simp{ 8391556Srgrimes struct var **vpp; 8401556Srgrimes struct var *vp; 8411556Srgrimes 8421556Srgrimes vpp = hashvar(s); 8431556Srgrimes for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) { 8441556Srgrimes if (varequal(vp->text, s)) { 8451556Srgrimes if (vp->flags & VREADONLY) 8461556Srgrimes return (1); 8471556Srgrimes INTOFF; 8481556Srgrimes if (*(strchr(vp->text, '=') + 1) != '\0') 8491556Srgrimes setvar(s, nullstr, 0); 85017525Sache if ((vp->flags & VEXPORT) && localevar(vp->text)) { 851171268Sscf change_env(s, 0); 85217525Sache setlocale(LC_ALL, ""); 85317525Sache } 85420425Ssteve vp->flags &= ~VEXPORT; 8551556Srgrimes vp->flags |= VUNSET; 8561556Srgrimes if ((vp->flags & VSTRFIXED) == 0) { 8571556Srgrimes if ((vp->flags & VTEXTFIXED) == 0) 8581556Srgrimes ckfree(vp->text); 8591556Srgrimes *vpp = vp->next; 8601556Srgrimes ckfree(vp); 8611556Srgrimes } 8621556Srgrimes INTON; 8631556Srgrimes return (0); 8641556Srgrimes } 8651556Srgrimes } 8661556Srgrimes 867135856Sdes return (0); 8681556Srgrimes} 8691556Srgrimes 8701556Srgrimes 8711556Srgrimes 8721556Srgrimes/* 8731556Srgrimes * Find the appropriate entry in the hash table from the name. 8741556Srgrimes */ 8751556Srgrimes 876213811Sobrienstatic struct var ** 877200956Sjilleshashvar(const char *p) 87890111Simp{ 8791556Srgrimes unsigned int hashval; 8801556Srgrimes 88120425Ssteve hashval = ((unsigned char) *p) << 4; 8821556Srgrimes while (*p && *p != '=') 88320425Ssteve hashval += (unsigned char) *p++; 8841556Srgrimes return &vartab[hashval % VTABSIZE]; 8851556Srgrimes} 8861556Srgrimes 8871556Srgrimes 8881556Srgrimes 8891556Srgrimes/* 8901556Srgrimes * Returns true if the two strings specify the same varable. The first 8911556Srgrimes * variable name is terminated by '='; the second may be terminated by 8921556Srgrimes * either '=' or '\0'. 8931556Srgrimes */ 8941556Srgrimes 895213811Sobrienstatic int 896200956Sjillesvarequal(const char *p, const char *q) 89790111Simp{ 8981556Srgrimes while (*p == *q++) { 8991556Srgrimes if (*p++ == '=') 9001556Srgrimes return 1; 9011556Srgrimes } 9021556Srgrimes if (*p == '=' && *(q - 1) == '\0') 9031556Srgrimes return 1; 9041556Srgrimes return 0; 9051556Srgrimes} 906