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