Home | History | Annotate | Line # | Download | only in sh
var.c revision 1.13
      1 /*	$NetBSD: var.c,v 1.13 1995/05/11 21:30:39 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1991, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Kenneth Almquist.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the University of
     21  *	California, Berkeley and its contributors.
     22  * 4. Neither the name of the University nor the names of its contributors
     23  *    may be used to endorse or promote products derived from this software
     24  *    without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  */
     38 
     39 #ifndef lint
     40 #if 0
     41 static char sccsid[] = "@(#)var.c	8.3 (Berkeley) 5/4/95";
     42 #else
     43 static char rcsid[] = "$NetBSD: var.c,v 1.13 1995/05/11 21:30:39 christos Exp $";
     44 #endif
     45 #endif /* not lint */
     46 
     47 #include <unistd.h>
     48 #include <stdlib.h>
     49 
     50 /*
     51  * Shell variables.
     52  */
     53 
     54 #include "shell.h"
     55 #include "output.h"
     56 #include "expand.h"
     57 #include "nodes.h"	/* for other headers */
     58 #include "eval.h"	/* defines cmdenviron */
     59 #include "exec.h"
     60 #include "syntax.h"
     61 #include "options.h"
     62 #include "mail.h"
     63 #include "var.h"
     64 #include "memalloc.h"
     65 #include "error.h"
     66 #include "mystring.h"
     67 #ifndef NO_HISTORY
     68 #include "myhistedit.h"
     69 #endif
     70 
     71 
     72 #define VTABSIZE 39
     73 
     74 
     75 struct varinit {
     76 	struct var *var;
     77 	int flags;
     78 	char *text;
     79 };
     80 
     81 
     82 #if ATTY
     83 struct var vatty;
     84 #endif
     85 #ifndef NO_HISTORY
     86 struct var vhistsize;
     87 #endif
     88 struct var vifs;
     89 struct var vmail;
     90 struct var vmpath;
     91 struct var vpath;
     92 struct var vps1;
     93 struct var vps2;
     94 struct var vvers;
     95 #if ATTY
     96 struct var vterm;
     97 #endif
     98 
     99 const struct varinit varinit[] = {
    100 #if ATTY
    101 	{&vatty,	VSTRFIXED|VTEXTFIXED|VUNSET,	"ATTY="},
    102 #endif
    103 #ifndef NO_HISTORY
    104 	{&vhistsize,	VSTRFIXED|VTEXTFIXED|VUNSET,	"HISTSIZE="},
    105 #endif
    106 	{&vifs,	VSTRFIXED|VTEXTFIXED,		"IFS= \t\n"},
    107 	{&vmail,	VSTRFIXED|VTEXTFIXED|VUNSET,	"MAIL="},
    108 	{&vmpath,	VSTRFIXED|VTEXTFIXED|VUNSET,	"MAILPATH="},
    109 	{&vpath,	VSTRFIXED|VTEXTFIXED,		"PATH=/bin:/usr/bin"},
    110 	/*
    111 	 * vps1 depends on uid
    112 	 */
    113 	{&vps2,	VSTRFIXED|VTEXTFIXED,		"PS2=> "},
    114 #if ATTY
    115 	{&vterm,	VSTRFIXED|VTEXTFIXED|VUNSET,	"TERM="},
    116 #endif
    117 	{NULL,	0,				NULL}
    118 };
    119 
    120 struct var *vartab[VTABSIZE];
    121 
    122 STATIC int unsetvar __P((char *));
    123 STATIC struct var **hashvar __P((char *));
    124 STATIC int varequal __P((char *, char *));
    125 
    126 /*
    127  * Initialize the varable symbol tables and import the environment
    128  */
    129 
    130 #ifdef mkinit
    131 INCLUDE "var.h"
    132 INIT {
    133 	char **envp;
    134 	extern char **environ;
    135 
    136 	initvar();
    137 	for (envp = environ ; *envp ; envp++) {
    138 		if (strchr(*envp, '=')) {
    139 			setvareq(*envp, VEXPORT|VTEXTFIXED);
    140 		}
    141 	}
    142 }
    143 #endif
    144 
    145 
    146 /*
    147  * This routine initializes the builtin variables.  It is called when the
    148  * shell is initialized and again when a shell procedure is spawned.
    149  */
    150 
    151 void
    152 initvar() {
    153 	const struct varinit *ip;
    154 	struct var *vp;
    155 	struct var **vpp;
    156 
    157 	for (ip = varinit ; (vp = ip->var) != NULL ; ip++) {
    158 		if ((vp->flags & VEXPORT) == 0) {
    159 			vpp = hashvar(ip->text);
    160 			vp->next = *vpp;
    161 			*vpp = vp;
    162 			vp->text = ip->text;
    163 			vp->flags = ip->flags;
    164 		}
    165 	}
    166 	/*
    167 	 * PS1 depends on uid
    168 	 */
    169 	if ((vps1.flags & VEXPORT) == 0) {
    170 		vpp = hashvar("PS1=");
    171 		vps1.next = *vpp;
    172 		*vpp = &vps1;
    173 		vps1.text = geteuid() ? "PS1=$ " : "PS1=# ";
    174 		vps1.flags = VSTRFIXED|VTEXTFIXED;
    175 	}
    176 }
    177 
    178 /*
    179  * Set the value of a variable.  The flags argument is ored with the
    180  * flags of the variable.  If val is NULL, the variable is unset.
    181  */
    182 
    183 void
    184 setvar(name, val, flags)
    185 	char *name, *val;
    186 	int flags;
    187 {
    188 	char *p, *q;
    189 	int len;
    190 	int namelen;
    191 	char *nameeq;
    192 	int isbad;
    193 
    194 	isbad = 0;
    195 	p = name;
    196 	if (! is_name(*p++))
    197 		isbad = 1;
    198 	for (;;) {
    199 		if (! is_in_name(*p)) {
    200 			if (*p == '\0' || *p == '=')
    201 				break;
    202 			isbad = 1;
    203 		}
    204 		p++;
    205 	}
    206 	namelen = p - name;
    207 	if (isbad)
    208 		error("%.*s: bad variable name", namelen, name);
    209 	len = namelen + 2;		/* 2 is space for '=' and '\0' */
    210 	if (val == NULL) {
    211 		flags |= VUNSET;
    212 	} else {
    213 		len += strlen(val);
    214 	}
    215 	p = nameeq = ckmalloc(len);
    216 	q = name;
    217 	while (--namelen >= 0)
    218 		*p++ = *q++;
    219 	*p++ = '=';
    220 	*p = '\0';
    221 	if (val)
    222 		scopy(val, p);
    223 	setvareq(nameeq, flags);
    224 }
    225 
    226 
    227 
    228 /*
    229  * Same as setvar except that the variable and value are passed in
    230  * the first argument as name=value.  Since the first argument will
    231  * be actually stored in the table, it should not be a string that
    232  * will go away.
    233  */
    234 
    235 void
    236 setvareq(s, flags)
    237 	char *s;
    238 	int flags;
    239 {
    240 	struct var *vp, **vpp;
    241 
    242 	vpp = hashvar(s);
    243 	for (vp = *vpp ; vp ; vp = vp->next) {
    244 		if (varequal(s, vp->text)) {
    245 			if (vp->flags & VREADONLY) {
    246 				int len = strchr(s, '=') - s;
    247 				error("%.*s: is read only", len, s);
    248 			}
    249 			INTOFF;
    250 			if (vp == &vpath)
    251 				changepath(s + 5);	/* 5 = strlen("PATH=") */
    252 			if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0)
    253 				ckfree(vp->text);
    254 			vp->flags &=~ (VTEXTFIXED|VSTACK|VUNSET);
    255 			vp->flags |= flags;
    256 			vp->text = s;
    257 			if (vp == &vmpath || (vp == &vmail && ! mpathset()))
    258 				chkmail(1);
    259 #ifndef NO_HISTORY
    260 			if (vp == &vhistsize)
    261 				sethistsize();
    262 #endif
    263 			INTON;
    264 			return;
    265 		}
    266 	}
    267 	/* not found */
    268 	vp = ckmalloc(sizeof (*vp));
    269 	vp->flags = flags;
    270 	vp->text = s;
    271 	vp->next = *vpp;
    272 	*vpp = vp;
    273 }
    274 
    275 
    276 
    277 /*
    278  * Process a linked list of variable assignments.
    279  */
    280 
    281 void
    282 listsetvar(list)
    283 	struct strlist *list;
    284 	{
    285 	struct strlist *lp;
    286 
    287 	INTOFF;
    288 	for (lp = list ; lp ; lp = lp->next) {
    289 		setvareq(savestr(lp->text), 0);
    290 	}
    291 	INTON;
    292 }
    293 
    294 
    295 
    296 /*
    297  * Find the value of a variable.  Returns NULL if not set.
    298  */
    299 
    300 char *
    301 lookupvar(name)
    302 	char *name;
    303 	{
    304 	struct var *v;
    305 
    306 	for (v = *hashvar(name) ; v ; v = v->next) {
    307 		if (varequal(v->text, name)) {
    308 			if (v->flags & VUNSET)
    309 				return NULL;
    310 			return strchr(v->text, '=') + 1;
    311 		}
    312 	}
    313 	return NULL;
    314 }
    315 
    316 
    317 
    318 /*
    319  * Search the environment of a builtin command.  If the second argument
    320  * is nonzero, return the value of a variable even if it hasn't been
    321  * exported.
    322  */
    323 
    324 char *
    325 bltinlookup(name, doall)
    326 	char *name;
    327 	int doall;
    328 {
    329 	struct strlist *sp;
    330 	struct var *v;
    331 
    332 	for (sp = cmdenviron ; sp ; sp = sp->next) {
    333 		if (varequal(sp->text, name))
    334 			return strchr(sp->text, '=') + 1;
    335 	}
    336 	for (v = *hashvar(name) ; v ; v = v->next) {
    337 		if (varequal(v->text, name)) {
    338 			if ((v->flags & VUNSET)
    339 			 || (!doall && (v->flags & VEXPORT) == 0))
    340 				return NULL;
    341 			return strchr(v->text, '=') + 1;
    342 		}
    343 	}
    344 	return NULL;
    345 }
    346 
    347 
    348 
    349 /*
    350  * Generate a list of exported variables.  This routine is used to construct
    351  * the third argument to execve when executing a program.
    352  */
    353 
    354 char **
    355 environment() {
    356 	int nenv;
    357 	struct var **vpp;
    358 	struct var *vp;
    359 	char **env, **ep;
    360 
    361 	nenv = 0;
    362 	for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
    363 		for (vp = *vpp ; vp ; vp = vp->next)
    364 			if (vp->flags & VEXPORT)
    365 				nenv++;
    366 	}
    367 	ep = env = stalloc((nenv + 1) * sizeof *env);
    368 	for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
    369 		for (vp = *vpp ; vp ; vp = vp->next)
    370 			if (vp->flags & VEXPORT)
    371 				*ep++ = vp->text;
    372 	}
    373 	*ep = NULL;
    374 	return env;
    375 }
    376 
    377 
    378 /*
    379  * Called when a shell procedure is invoked to clear out nonexported
    380  * variables.  It is also necessary to reallocate variables of with
    381  * VSTACK set since these are currently allocated on the stack.
    382  */
    383 
    384 #ifdef mkinit
    385 MKINIT void shprocvar();
    386 
    387 SHELLPROC {
    388 	shprocvar();
    389 }
    390 #endif
    391 
    392 void
    393 shprocvar() {
    394 	struct var **vpp;
    395 	struct var *vp, **prev;
    396 
    397 	for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
    398 		for (prev = vpp ; (vp = *prev) != NULL ; ) {
    399 			if ((vp->flags & VEXPORT) == 0) {
    400 				*prev = vp->next;
    401 				if ((vp->flags & VTEXTFIXED) == 0)
    402 					ckfree(vp->text);
    403 				if ((vp->flags & VSTRFIXED) == 0)
    404 					ckfree(vp);
    405 			} else {
    406 				if (vp->flags & VSTACK) {
    407 					vp->text = savestr(vp->text);
    408 					vp->flags &=~ VSTACK;
    409 				}
    410 				prev = &vp->next;
    411 			}
    412 		}
    413 	}
    414 	initvar();
    415 }
    416 
    417 
    418 
    419 /*
    420  * Command to list all variables which are set.  Currently this command
    421  * is invoked from the set command when the set command is called without
    422  * any variables.
    423  */
    424 
    425 int
    426 showvarscmd(argc, argv)
    427 	int argc;
    428 	char **argv;
    429 {
    430 	struct var **vpp;
    431 	struct var *vp;
    432 
    433 	for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
    434 		for (vp = *vpp ; vp ; vp = vp->next) {
    435 			if ((vp->flags & VUNSET) == 0)
    436 				out1fmt("%s\n", vp->text);
    437 		}
    438 	}
    439 	return 0;
    440 }
    441 
    442 
    443 
    444 /*
    445  * The export and readonly commands.
    446  */
    447 
    448 int
    449 exportcmd(argc, argv)
    450 	int argc;
    451 	char **argv;
    452 {
    453 	struct var **vpp;
    454 	struct var *vp;
    455 	char *name;
    456 	char *p;
    457 	int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT;
    458 
    459 	listsetvar(cmdenviron);
    460 	if (argc > 1) {
    461 		while ((name = *argptr++) != NULL) {
    462 			if ((p = strchr(name, '=')) != NULL) {
    463 				p++;
    464 			} else {
    465 				vpp = hashvar(name);
    466 				for (vp = *vpp ; vp ; vp = vp->next) {
    467 					if (varequal(vp->text, name)) {
    468 						vp->flags |= flag;
    469 						goto found;
    470 					}
    471 				}
    472 			}
    473 			setvar(name, p, flag);
    474 found:;
    475 		}
    476 	} else {
    477 		for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
    478 			for (vp = *vpp ; vp ; vp = vp->next) {
    479 				if (vp->flags & flag) {
    480 					for (p = vp->text ; *p != '=' ; p++)
    481 						out1c(*p);
    482 					out1c('\n');
    483 				}
    484 			}
    485 		}
    486 	}
    487 	return 0;
    488 }
    489 
    490 
    491 /*
    492  * The "local" command.
    493  */
    494 
    495 int
    496 localcmd(argc, argv)
    497 	int argc;
    498 	char **argv;
    499 {
    500 	char *name;
    501 
    502 	if (! in_function())
    503 		error("Not in a function");
    504 	while ((name = *argptr++) != NULL) {
    505 		mklocal(name);
    506 	}
    507 	return 0;
    508 }
    509 
    510 
    511 /*
    512  * Make a variable a local variable.  When a variable is made local, it's
    513  * value and flags are saved in a localvar structure.  The saved values
    514  * will be restored when the shell function returns.  We handle the name
    515  * "-" as a special case.
    516  */
    517 
    518 void
    519 mklocal(name)
    520 	char *name;
    521 	{
    522 	struct localvar *lvp;
    523 	struct var **vpp;
    524 	struct var *vp;
    525 
    526 	INTOFF;
    527 	lvp = ckmalloc(sizeof (struct localvar));
    528 	if (name[0] == '-' && name[1] == '\0') {
    529 		lvp->text = ckmalloc(sizeof optlist);
    530 		memcpy(lvp->text, optlist, sizeof optlist);
    531 		vp = NULL;
    532 	} else {
    533 		vpp = hashvar(name);
    534 		for (vp = *vpp ; vp && ! varequal(vp->text, name) ; vp = vp->next);
    535 		if (vp == NULL) {
    536 			if (strchr(name, '='))
    537 				setvareq(savestr(name), VSTRFIXED);
    538 			else
    539 				setvar(name, NULL, VSTRFIXED);
    540 			vp = *vpp;	/* the new variable */
    541 			lvp->text = NULL;
    542 			lvp->flags = VUNSET;
    543 		} else {
    544 			lvp->text = vp->text;
    545 			lvp->flags = vp->flags;
    546 			vp->flags |= VSTRFIXED|VTEXTFIXED;
    547 			if (strchr(name, '='))
    548 				setvareq(savestr(name), 0);
    549 		}
    550 	}
    551 	lvp->vp = vp;
    552 	lvp->next = localvars;
    553 	localvars = lvp;
    554 	INTON;
    555 }
    556 
    557 
    558 /*
    559  * Called after a function returns.
    560  */
    561 
    562 void
    563 poplocalvars() {
    564 	struct localvar *lvp;
    565 	struct var *vp;
    566 
    567 	while ((lvp = localvars) != NULL) {
    568 		localvars = lvp->next;
    569 		vp = lvp->vp;
    570 		if (vp == NULL) {	/* $- saved */
    571 			memcpy(optlist, lvp->text, sizeof optlist);
    572 			ckfree(lvp->text);
    573 		} else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) {
    574 			(void)unsetvar(vp->text);
    575 		} else {
    576 			if ((vp->flags & VTEXTFIXED) == 0)
    577 				ckfree(vp->text);
    578 			vp->flags = lvp->flags;
    579 			vp->text = lvp->text;
    580 		}
    581 		ckfree(lvp);
    582 	}
    583 }
    584 
    585 
    586 int
    587 setvarcmd(argc, argv)
    588 	int argc;
    589 	char **argv;
    590 {
    591 	if (argc <= 2)
    592 		return unsetcmd(argc, argv);
    593 	else if (argc == 3)
    594 		setvar(argv[1], argv[2], 0);
    595 	else
    596 		error("List assignment not implemented");
    597 	return 0;
    598 }
    599 
    600 
    601 /*
    602  * The unset builtin command.  We unset the function before we unset the
    603  * variable to allow a function to be unset when there is a readonly variable
    604  * with the same name.
    605  */
    606 
    607 int
    608 unsetcmd(argc, argv)
    609 	int argc;
    610 	char **argv;
    611 {
    612 	char **ap;
    613 	int i;
    614 	int flg_func = 0;
    615 	int flg_var = 0;
    616 	int ret = 0;
    617 
    618 	while ((i = nextopt("vf")) != '\0') {
    619 		if (i == 'f')
    620 			flg_func = 1;
    621 		else
    622 			flg_var = 1;
    623 	}
    624 	if (flg_func == 0 && flg_var == 0)
    625 		flg_var = 1;
    626 
    627 	for (ap = argptr; *ap ; ap++) {
    628 		if (flg_func)
    629 			ret |= unsetfunc(*ap);
    630 		if (flg_var)
    631 			ret |= unsetvar(*ap);
    632 	}
    633 	return ret;
    634 }
    635 
    636 
    637 /*
    638  * Unset the specified variable.
    639  */
    640 
    641 STATIC int
    642 unsetvar(s)
    643 	char *s;
    644 	{
    645 	struct var **vpp;
    646 	struct var *vp;
    647 
    648 	vpp = hashvar(s);
    649 	for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) {
    650 		if (varequal(vp->text, s)) {
    651 			if (vp->flags & VREADONLY)
    652 				return (1);
    653 			INTOFF;
    654 			if (*(strchr(vp->text, '=') + 1) != '\0')
    655 				setvar(s, nullstr, 0);
    656 			vp->flags &=~ VEXPORT;
    657 			vp->flags |= VUNSET;
    658 			if ((vp->flags & VSTRFIXED) == 0) {
    659 				if ((vp->flags & VTEXTFIXED) == 0)
    660 					ckfree(vp->text);
    661 				*vpp = vp->next;
    662 				ckfree(vp);
    663 			}
    664 			INTON;
    665 			return (0);
    666 		}
    667 	}
    668 
    669 	return (1);
    670 }
    671 
    672 
    673 
    674 /*
    675  * Find the appropriate entry in the hash table from the name.
    676  */
    677 
    678 STATIC struct var **
    679 hashvar(p)
    680 	register char *p;
    681 	{
    682 	unsigned int hashval;
    683 
    684 	hashval = *p << 4;
    685 	while (*p && *p != '=')
    686 		hashval += *p++;
    687 	return &vartab[hashval % VTABSIZE];
    688 }
    689 
    690 
    691 
    692 /*
    693  * Returns true if the two strings specify the same varable.  The first
    694  * variable name is terminated by '='; the second may be terminated by
    695  * either '=' or '\0'.
    696  */
    697 
    698 STATIC int
    699 varequal(p, q)
    700 	register char *p, *q;
    701 	{
    702 	while (*p == *q++) {
    703 		if (*p++ == '=')
    704 			return 1;
    705 	}
    706 	if (*p == '=' && *(q - 1) == '\0')
    707 		return 1;
    708 	return 0;
    709 }
    710