Home | History | Annotate | Line # | Download | only in sh
var.c revision 1.62
      1  1.62       kre /*	$NetBSD: var.c,v 1.62 2017/06/28 13:46:06 kre 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.62       kre __RCSID("$NetBSD: var.c,v 1.62 2017/06/28 13:46:06 kre Exp $");
     41  1.12       cgd #endif
     42   1.1       cgd #endif /* not lint */
     43   1.1       cgd 
     44  1.58       kre #include <stdio.h>
     45  1.13  christos #include <unistd.h>
     46  1.13  christos #include <stdlib.h>
     47  1.42  christos #include <string.h>
     48  1.21      fair #include <paths.h>
     49  1.41  christos #include <limits.h>
     50  1.13  christos 
     51   1.1       cgd /*
     52   1.1       cgd  * Shell variables.
     53   1.1       cgd  */
     54   1.1       cgd 
     55   1.1       cgd #include "shell.h"
     56   1.1       cgd #include "output.h"
     57   1.1       cgd #include "expand.h"
     58   1.1       cgd #include "nodes.h"	/* for other headers */
     59   1.1       cgd #include "eval.h"	/* defines cmdenviron */
     60   1.1       cgd #include "exec.h"
     61   1.1       cgd #include "syntax.h"
     62   1.1       cgd #include "options.h"
     63  1.40  christos #include "builtins.h"
     64   1.1       cgd #include "mail.h"
     65   1.1       cgd #include "var.h"
     66   1.1       cgd #include "memalloc.h"
     67   1.1       cgd #include "error.h"
     68   1.1       cgd #include "mystring.h"
     69  1.15  christos #include "parser.h"
     70  1.32       dsl #include "show.h"
     71  1.17  christos #ifndef SMALL
     72  1.13  christos #include "myhistedit.h"
     73  1.13  christos #endif
     74   1.1       cgd 
     75  1.35       dsl #ifdef SMALL
     76   1.1       cgd #define VTABSIZE 39
     77  1.35       dsl #else
     78  1.35       dsl #define VTABSIZE 517
     79  1.35       dsl #endif
     80   1.1       cgd 
     81   1.1       cgd 
     82   1.1       cgd struct varinit {
     83   1.1       cgd 	struct var *var;
     84   1.1       cgd 	int flags;
     85  1.23  christos 	const char *text;
     86  1.57       kre 	union var_func_union v_u;
     87   1.1       cgd };
     88  1.57       kre #define	func v_u.set_func
     89  1.57       kre #define	rfunc v_u.ref_func
     90  1.57       kre 
     91  1.57       kre char *get_lineno(struct var *);
     92   1.1       cgd 
     93  1.39  dholland struct localvar *localvars;
     94   1.1       cgd 
     95  1.17  christos #ifndef SMALL
     96   1.5       jtc struct var vhistsize;
     97  1.18  christos struct var vterm;
     98  1.61       kre struct var editrc;
     99  1.62       kre struct var ps_lit;
    100   1.7       cgd #endif
    101   1.1       cgd struct var vifs;
    102   1.1       cgd struct var vmail;
    103   1.1       cgd struct var vmpath;
    104   1.1       cgd struct var vpath;
    105   1.1       cgd struct var vps1;
    106   1.1       cgd struct var vps2;
    107  1.32       dsl struct var vps4;
    108  1.49  christos struct var vvers;
    109  1.14  christos struct var voptind;
    110  1.56       kre struct var line_num;
    111   1.1       cgd 
    112  1.57       kre struct var line_num;
    113  1.57       kre int line_number;
    114  1.57       kre int funclinebase = 0;
    115  1.57       kre int funclineabs = 0;
    116  1.57       kre 
    117  1.48  christos char ifs_default[] = " \t\n";
    118  1.48  christos 
    119   1.1       cgd const struct varinit varinit[] = {
    120  1.17  christos #ifndef SMALL
    121  1.14  christos 	{ &vhistsize,	VSTRFIXED|VTEXTFIXED|VUNSET,	"HISTSIZE=",
    122  1.57       kre 	   { .set_func= sethistsize } },
    123   1.7       cgd #endif
    124  1.14  christos 	{ &vifs,	VSTRFIXED|VTEXTFIXED,		"IFS= \t\n",
    125  1.57       kre 	   { NULL } },
    126  1.14  christos 	{ &vmail,	VSTRFIXED|VTEXTFIXED|VUNSET,	"MAIL=",
    127  1.57       kre 	   { NULL } },
    128  1.14  christos 	{ &vmpath,	VSTRFIXED|VTEXTFIXED|VUNSET,	"MAILPATH=",
    129  1.57       kre 	   { NULL } },
    130  1.49  christos 	{ &vvers,	VSTRFIXED|VTEXTFIXED|VNOEXPORT, "NETBSD_SHELL=",
    131  1.57       kre 	   { NULL } },
    132  1.26       cgd 	{ &vpath,	VSTRFIXED|VTEXTFIXED,		"PATH=" _PATH_DEFPATH,
    133  1.57       kre 	   { .set_func= changepath } },
    134  1.15  christos 	/*
    135   1.1       cgd 	 * vps1 depends on uid
    136   1.1       cgd 	 */
    137  1.14  christos 	{ &vps2,	VSTRFIXED|VTEXTFIXED,		"PS2=> ",
    138  1.57       kre 	   { NULL } },
    139  1.32       dsl 	{ &vps4,	VSTRFIXED|VTEXTFIXED,		"PS4=+ ",
    140  1.57       kre 	   { NULL } },
    141  1.18  christos #ifndef SMALL
    142  1.14  christos 	{ &vterm,	VSTRFIXED|VTEXTFIXED|VUNSET,	"TERM=",
    143  1.57       kre 	   { .set_func= setterm } },
    144  1.61       kre 	{ &editrc, 	VSTRFIXED|VTEXTFIXED|VUNSET,	"EDITRC=",
    145  1.61       kre 	   { .set_func= set_editrc } },
    146  1.62       kre 	{ &ps_lit, 	VSTRFIXED|VTEXTFIXED|VUNSET,	"PSlit=",
    147  1.62       kre 	   { .set_func= set_prompt_lit } },
    148   1.1       cgd #endif
    149  1.32       dsl 	{ &voptind,	VSTRFIXED|VTEXTFIXED|VNOFUNC,	"OPTIND=1",
    150  1.57       kre 	   { .set_func= getoptsreset } },
    151  1.57       kre 	{ &line_num,	VSTRFIXED|VTEXTFIXED|VFUNCREF,	"LINENO=1",
    152  1.57       kre 	   { .ref_func= get_lineno } },
    153  1.14  christos 	{ NULL,	0,				NULL,
    154  1.57       kre 	   { NULL } }
    155   1.1       cgd };
    156   1.1       cgd 
    157   1.1       cgd struct var *vartab[VTABSIZE];
    158   1.1       cgd 
    159  1.35       dsl STATIC int strequal(const char *, const char *);
    160  1.35       dsl STATIC struct var *find_var(const char *, struct var ***, int *);
    161   1.1       cgd 
    162   1.1       cgd /*
    163   1.1       cgd  * Initialize the varable symbol tables and import the environment
    164   1.1       cgd  */
    165   1.1       cgd 
    166   1.1       cgd #ifdef mkinit
    167  1.47  christos INCLUDE <stdio.h>
    168  1.47  christos INCLUDE <unistd.h>
    169   1.1       cgd INCLUDE "var.h"
    170  1.49  christos INCLUDE "version.h"
    171  1.27  christos MKINIT char **environ;
    172   1.1       cgd INIT {
    173   1.1       cgd 	char **envp;
    174  1.47  christos 	char buf[64];
    175   1.1       cgd 
    176   1.1       cgd 	initvar();
    177   1.1       cgd 	for (envp = environ ; *envp ; envp++) {
    178   1.1       cgd 		if (strchr(*envp, '=')) {
    179   1.1       cgd 			setvareq(*envp, VEXPORT|VTEXTFIXED);
    180   1.1       cgd 		}
    181   1.1       cgd 	}
    182  1.47  christos 
    183  1.47  christos 	/*
    184  1.53       kre 	 * Set variables which override anything read from environment.
    185  1.53       kre 	 *
    186  1.47  christos 	 * PPID is readonly
    187  1.53       kre 	 * Always default IFS
    188  1.53       kre 	 * NETBSD_SHELL is a constant (readonly), and is never exported
    189  1.57       kre 	 * LINENO is simply magic...
    190  1.47  christos 	 */
    191  1.47  christos 	snprintf(buf, sizeof(buf), "%d", (int)getppid());
    192  1.47  christos 	setvar("PPID", buf, VREADONLY);
    193  1.48  christos 	setvar("IFS", ifs_default, VTEXTFIXED);
    194  1.53       kre 
    195  1.53       kre 	setvar("NETBSD_SHELL", NETBSD_SHELL
    196  1.53       kre #ifdef BUILD_DATE
    197  1.53       kre 		" BUILD:" BUILD_DATE
    198  1.53       kre #endif
    199  1.53       kre #ifdef DEBUG
    200  1.53       kre 		" DEBUG"
    201  1.53       kre #endif
    202  1.53       kre #if !defined(JOBS) || JOBS == 0
    203  1.53       kre 		" -JOBS"
    204  1.53       kre #endif
    205  1.53       kre #ifndef DO_SHAREDVFORK
    206  1.53       kre 		" -VFORK"
    207  1.53       kre #endif
    208  1.53       kre #ifdef SMALL
    209  1.53       kre 		" SMALL"
    210  1.53       kre #endif
    211  1.53       kre #ifdef TINY
    212  1.53       kre 		" TINY"
    213  1.53       kre #endif
    214  1.53       kre #ifdef OLD_TTY_DRIVER
    215  1.53       kre 		" OLD_TTY"
    216  1.53       kre #endif
    217  1.53       kre #ifdef SYSV
    218  1.53       kre 		" SYSV"
    219  1.53       kre #endif
    220  1.53       kre #ifndef BSD
    221  1.53       kre 		" -BSD"
    222  1.53       kre #endif
    223  1.55       kre #ifdef BOGUS_NOT_COMMAND
    224  1.55       kre 		" BOGUS_NOT"
    225  1.55       kre #endif
    226  1.53       kre 		    , VTEXTFIXED|VREADONLY|VNOEXPORT);
    227  1.56       kre 
    228  1.56       kre 	setvar("LINENO", "1", VTEXTFIXED);
    229   1.1       cgd }
    230   1.1       cgd #endif
    231   1.1       cgd 
    232   1.1       cgd 
    233   1.1       cgd /*
    234   1.1       cgd  * This routine initializes the builtin variables.  It is called when the
    235   1.1       cgd  * shell is initialized and again when a shell procedure is spawned.
    236   1.1       cgd  */
    237   1.1       cgd 
    238   1.1       cgd void
    239  1.30  christos initvar(void)
    240  1.30  christos {
    241   1.1       cgd 	const struct varinit *ip;
    242   1.1       cgd 	struct var *vp;
    243   1.1       cgd 	struct var **vpp;
    244   1.1       cgd 
    245   1.1       cgd 	for (ip = varinit ; (vp = ip->var) != NULL ; ip++) {
    246  1.35       dsl 		if (find_var(ip->text, &vpp, &vp->name_len) != NULL)
    247  1.35       dsl 			continue;
    248  1.35       dsl 		vp->next = *vpp;
    249  1.35       dsl 		*vpp = vp;
    250  1.35       dsl 		vp->text = strdup(ip->text);
    251  1.35       dsl 		vp->flags = ip->flags;
    252  1.57       kre 		vp->v_u = ip->v_u;
    253   1.1       cgd 	}
    254   1.1       cgd 	/*
    255   1.1       cgd 	 * PS1 depends on uid
    256   1.1       cgd 	 */
    257  1.35       dsl 	if (find_var("PS1", &vpp, &vps1.name_len) == NULL) {
    258   1.1       cgd 		vps1.next = *vpp;
    259   1.1       cgd 		*vpp = &vps1;
    260   1.1       cgd 		vps1.flags = VSTRFIXED|VTEXTFIXED;
    261  1.44  christos 		vps1.text = NULL;
    262  1.44  christos 		choose_ps1();
    263   1.1       cgd 	}
    264   1.1       cgd }
    265   1.1       cgd 
    266  1.44  christos void
    267  1.44  christos choose_ps1(void)
    268  1.44  christos {
    269  1.44  christos 	free(vps1.text);
    270  1.44  christos 	vps1.text = strdup(geteuid() ? "PS1=$ " : "PS1=# ");
    271  1.44  christos }
    272  1.44  christos 
    273   1.1       cgd /*
    274  1.14  christos  * Safe version of setvar, returns 1 on success 0 on failure.
    275  1.14  christos  */
    276  1.14  christos 
    277  1.14  christos int
    278  1.30  christos setvarsafe(const char *name, const char *val, int flags)
    279  1.14  christos {
    280  1.14  christos 	struct jmploc jmploc;
    281  1.59       kre 	struct jmploc * const savehandler = handler;
    282  1.38  christos 	int volatile err = 0;
    283  1.14  christos 
    284  1.14  christos 	if (setjmp(jmploc.loc))
    285  1.14  christos 		err = 1;
    286  1.14  christos 	else {
    287  1.14  christos 		handler = &jmploc;
    288  1.14  christos 		setvar(name, val, flags);
    289  1.14  christos 	}
    290  1.14  christos 	handler = savehandler;
    291  1.14  christos 	return err;
    292  1.14  christos }
    293  1.14  christos 
    294  1.14  christos /*
    295   1.1       cgd  * Set the value of a variable.  The flags argument is ored with the
    296   1.1       cgd  * flags of the variable.  If val is NULL, the variable is unset.
    297   1.1       cgd  */
    298   1.1       cgd 
    299   1.1       cgd void
    300  1.30  christos setvar(const char *name, const char *val, int flags)
    301  1.10       cgd {
    302  1.23  christos 	const char *p;
    303  1.23  christos 	const char *q;
    304  1.23  christos 	char *d;
    305   1.1       cgd 	int len;
    306   1.1       cgd 	int namelen;
    307   1.1       cgd 	char *nameeq;
    308   1.1       cgd 	int isbad;
    309   1.1       cgd 
    310   1.1       cgd 	isbad = 0;
    311   1.1       cgd 	p = name;
    312  1.15  christos 	if (! is_name(*p))
    313   1.1       cgd 		isbad = 1;
    314  1.15  christos 	p++;
    315   1.1       cgd 	for (;;) {
    316   1.1       cgd 		if (! is_in_name(*p)) {
    317   1.1       cgd 			if (*p == '\0' || *p == '=')
    318   1.1       cgd 				break;
    319   1.1       cgd 			isbad = 1;
    320   1.1       cgd 		}
    321   1.1       cgd 		p++;
    322   1.1       cgd 	}
    323   1.1       cgd 	namelen = p - name;
    324   1.1       cgd 	if (isbad)
    325   1.5       jtc 		error("%.*s: bad variable name", namelen, name);
    326   1.1       cgd 	len = namelen + 2;		/* 2 is space for '=' and '\0' */
    327   1.1       cgd 	if (val == NULL) {
    328   1.1       cgd 		flags |= VUNSET;
    329   1.1       cgd 	} else {
    330   1.1       cgd 		len += strlen(val);
    331   1.1       cgd 	}
    332  1.23  christos 	d = nameeq = ckmalloc(len);
    333   1.1       cgd 	q = name;
    334   1.1       cgd 	while (--namelen >= 0)
    335  1.23  christos 		*d++ = *q++;
    336  1.23  christos 	*d++ = '=';
    337  1.23  christos 	*d = '\0';
    338   1.1       cgd 	if (val)
    339  1.23  christos 		scopy(val, d);
    340   1.1       cgd 	setvareq(nameeq, flags);
    341   1.1       cgd }
    342   1.1       cgd 
    343   1.1       cgd 
    344   1.1       cgd 
    345   1.1       cgd /*
    346   1.1       cgd  * Same as setvar except that the variable and value are passed in
    347   1.1       cgd  * the first argument as name=value.  Since the first argument will
    348   1.1       cgd  * be actually stored in the table, it should not be a string that
    349   1.1       cgd  * will go away.
    350   1.1       cgd  */
    351   1.1       cgd 
    352   1.1       cgd void
    353  1.30  christos setvareq(char *s, int flags)
    354  1.10       cgd {
    355   1.1       cgd 	struct var *vp, **vpp;
    356  1.35       dsl 	int nlen;
    357   1.1       cgd 
    358  1.49  christos 	if (aflag && !(flags & VNOEXPORT))
    359  1.28     bjh21 		flags |= VEXPORT;
    360  1.35       dsl 	vp = find_var(s, &vpp, &nlen);
    361  1.35       dsl 	if (vp != NULL) {
    362  1.60       kre 		if (vp->flags & VREADONLY) {
    363  1.60       kre 			if ((flags & (VTEXTFIXED|VSTACK)) == 0)
    364  1.60       kre 				ckfree(s);
    365  1.60       kre 			if (flags & VNOERROR)
    366  1.60       kre 				return;
    367  1.35       dsl 			error("%.*s: is read only", vp->name_len, s);
    368  1.60       kre 		}
    369  1.60       kre 		if (flags & VNOSET) {
    370  1.60       kre 			if ((flags & (VTEXTFIXED|VSTACK)) == 0)
    371  1.60       kre 				ckfree(s);
    372  1.35       dsl 			return;
    373  1.60       kre 		}
    374  1.57       kre 
    375  1.35       dsl 		INTOFF;
    376  1.14  christos 
    377  1.57       kre 		if (vp->func && !(vp->flags & VFUNCREF) && !(flags & VNOFUNC))
    378  1.35       dsl 			(*vp->func)(s + vp->name_len + 1);
    379  1.14  christos 
    380  1.35       dsl 		if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0)
    381  1.35       dsl 			ckfree(vp->text);
    382  1.14  christos 
    383  1.35       dsl 		vp->flags &= ~(VTEXTFIXED|VSTACK|VUNSET);
    384  1.49  christos 		if (flags & VNOEXPORT)
    385  1.49  christos 			vp->flags &= ~VEXPORT;
    386  1.54       kre 		if (vp->flags & VNOEXPORT)
    387  1.54       kre 			flags &= ~VEXPORT;
    388  1.35       dsl 		vp->flags |= flags & ~VNOFUNC;
    389  1.35       dsl 		vp->text = s;
    390  1.35       dsl 
    391  1.35       dsl 		/*
    392  1.35       dsl 		 * We could roll this to a function, to handle it as
    393  1.35       dsl 		 * a regular variable function callback, but why bother?
    394  1.35       dsl 		 */
    395  1.35       dsl 		if (vp == &vmpath || (vp == &vmail && ! mpathset()))
    396  1.35       dsl 			chkmail(1);
    397  1.57       kre 
    398  1.35       dsl 		INTON;
    399  1.35       dsl 		return;
    400   1.1       cgd 	}
    401   1.1       cgd 	/* not found */
    402  1.60       kre 	if (flags & VNOSET) {
    403  1.60       kre 		if ((flags & (VTEXTFIXED|VSTACK)) == 0)
    404  1.60       kre 			ckfree(s);
    405  1.30  christos 		return;
    406  1.60       kre 	}
    407   1.1       cgd 	vp = ckmalloc(sizeof (*vp));
    408  1.57       kre 	vp->flags = flags & ~(VNOFUNC|VFUNCREF);
    409   1.1       cgd 	vp->text = s;
    410  1.35       dsl 	vp->name_len = nlen;
    411   1.1       cgd 	vp->next = *vpp;
    412  1.14  christos 	vp->func = NULL;
    413   1.1       cgd 	*vpp = vp;
    414   1.1       cgd }
    415   1.1       cgd 
    416   1.1       cgd 
    417   1.1       cgd 
    418   1.1       cgd /*
    419   1.1       cgd  * Process a linked list of variable assignments.
    420   1.1       cgd  */
    421   1.1       cgd 
    422   1.1       cgd void
    423  1.30  christos listsetvar(struct strlist *list, int flags)
    424  1.30  christos {
    425   1.1       cgd 	struct strlist *lp;
    426   1.1       cgd 
    427   1.1       cgd 	INTOFF;
    428   1.1       cgd 	for (lp = list ; lp ; lp = lp->next) {
    429  1.30  christos 		setvareq(savestr(lp->text), flags);
    430   1.1       cgd 	}
    431   1.1       cgd 	INTON;
    432   1.1       cgd }
    433   1.1       cgd 
    434  1.32       dsl void
    435  1.32       dsl listmklocal(struct strlist *list, int flags)
    436  1.32       dsl {
    437  1.32       dsl 	struct strlist *lp;
    438  1.32       dsl 
    439  1.32       dsl 	for (lp = list ; lp ; lp = lp->next)
    440  1.32       dsl 		mklocal(lp->text, flags);
    441  1.32       dsl }
    442   1.1       cgd 
    443   1.1       cgd 
    444   1.1       cgd /*
    445   1.1       cgd  * Find the value of a variable.  Returns NULL if not set.
    446   1.1       cgd  */
    447   1.1       cgd 
    448   1.1       cgd char *
    449  1.30  christos lookupvar(const char *name)
    450  1.30  christos {
    451   1.1       cgd 	struct var *v;
    452   1.1       cgd 
    453  1.35       dsl 	v = find_var(name, NULL, NULL);
    454  1.35       dsl 	if (v == NULL || v->flags & VUNSET)
    455  1.35       dsl 		return NULL;
    456  1.57       kre 	if (v->rfunc && (v->flags & VFUNCREF) != 0)
    457  1.57       kre 		return (*v->rfunc)(v) + v->name_len + 1;
    458  1.35       dsl 	return v->text + v->name_len + 1;
    459   1.1       cgd }
    460   1.1       cgd 
    461   1.1       cgd 
    462   1.1       cgd 
    463   1.1       cgd /*
    464   1.1       cgd  * Search the environment of a builtin command.  If the second argument
    465   1.1       cgd  * is nonzero, return the value of a variable even if it hasn't been
    466   1.1       cgd  * exported.
    467   1.1       cgd  */
    468   1.1       cgd 
    469   1.1       cgd char *
    470  1.30  christos bltinlookup(const char *name, int doall)
    471  1.10       cgd {
    472   1.1       cgd 	struct strlist *sp;
    473   1.1       cgd 	struct var *v;
    474   1.1       cgd 
    475   1.1       cgd 	for (sp = cmdenviron ; sp ; sp = sp->next) {
    476  1.35       dsl 		if (strequal(sp->text, name))
    477   1.1       cgd 			return strchr(sp->text, '=') + 1;
    478   1.1       cgd 	}
    479  1.35       dsl 
    480  1.35       dsl 	v = find_var(name, NULL, NULL);
    481  1.35       dsl 
    482  1.35       dsl 	if (v == NULL || v->flags & VUNSET || (!doall && !(v->flags & VEXPORT)))
    483  1.35       dsl 		return NULL;
    484  1.57       kre 	if (v->rfunc && (v->flags & VFUNCREF) != 0)
    485  1.57       kre 		return (*v->rfunc)(v) + v->name_len + 1;
    486  1.35       dsl 	return v->text + v->name_len + 1;
    487   1.1       cgd }
    488   1.1       cgd 
    489   1.1       cgd 
    490   1.1       cgd 
    491   1.1       cgd /*
    492   1.1       cgd  * Generate a list of exported variables.  This routine is used to construct
    493   1.1       cgd  * the third argument to execve when executing a program.
    494   1.1       cgd  */
    495   1.1       cgd 
    496   1.1       cgd char **
    497  1.30  christos environment(void)
    498  1.30  christos {
    499   1.1       cgd 	int nenv;
    500   1.1       cgd 	struct var **vpp;
    501   1.1       cgd 	struct var *vp;
    502  1.23  christos 	char **env;
    503  1.23  christos 	char **ep;
    504   1.1       cgd 
    505   1.1       cgd 	nenv = 0;
    506   1.1       cgd 	for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
    507   1.1       cgd 		for (vp = *vpp ; vp ; vp = vp->next)
    508  1.51       kre 			if ((vp->flags & (VEXPORT|VUNSET)) == VEXPORT)
    509   1.1       cgd 				nenv++;
    510   1.1       cgd 	}
    511  1.58       kre 	CTRACE(DBG_VARS, ("environment: %d vars to export\n", nenv));
    512   1.1       cgd 	ep = env = stalloc((nenv + 1) * sizeof *env);
    513   1.1       cgd 	for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
    514   1.1       cgd 		for (vp = *vpp ; vp ; vp = vp->next)
    515  1.57       kre 			if ((vp->flags & (VEXPORT|VUNSET)) == VEXPORT) {
    516  1.57       kre 				if (vp->rfunc && (vp->flags & VFUNCREF))
    517  1.57       kre 					*ep++ = (*vp->rfunc)(vp);
    518  1.57       kre 				else
    519  1.57       kre 					*ep++ = vp->text;
    520  1.58       kre 				VTRACE(DBG_VARS, ("environment: %s\n", ep[-1]));
    521  1.57       kre 			}
    522   1.1       cgd 	}
    523   1.1       cgd 	*ep = NULL;
    524   1.1       cgd 	return env;
    525   1.1       cgd }
    526   1.1       cgd 
    527   1.1       cgd 
    528   1.1       cgd /*
    529   1.1       cgd  * Called when a shell procedure is invoked to clear out nonexported
    530   1.1       cgd  * variables.  It is also necessary to reallocate variables of with
    531   1.1       cgd  * VSTACK set since these are currently allocated on the stack.
    532   1.1       cgd  */
    533   1.1       cgd 
    534   1.1       cgd #ifdef mkinit
    535  1.30  christos void shprocvar(void);
    536   1.1       cgd 
    537   1.1       cgd SHELLPROC {
    538   1.1       cgd 	shprocvar();
    539   1.1       cgd }
    540   1.1       cgd #endif
    541   1.1       cgd 
    542   1.1       cgd void
    543  1.30  christos shprocvar(void)
    544  1.30  christos {
    545   1.1       cgd 	struct var **vpp;
    546   1.1       cgd 	struct var *vp, **prev;
    547   1.1       cgd 
    548   1.1       cgd 	for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
    549   1.1       cgd 		for (prev = vpp ; (vp = *prev) != NULL ; ) {
    550   1.1       cgd 			if ((vp->flags & VEXPORT) == 0) {
    551   1.1       cgd 				*prev = vp->next;
    552   1.1       cgd 				if ((vp->flags & VTEXTFIXED) == 0)
    553   1.1       cgd 					ckfree(vp->text);
    554   1.1       cgd 				if ((vp->flags & VSTRFIXED) == 0)
    555   1.1       cgd 					ckfree(vp);
    556   1.1       cgd 			} else {
    557   1.1       cgd 				if (vp->flags & VSTACK) {
    558   1.1       cgd 					vp->text = savestr(vp->text);
    559   1.1       cgd 					vp->flags &=~ VSTACK;
    560   1.1       cgd 				}
    561   1.1       cgd 				prev = &vp->next;
    562   1.1       cgd 			}
    563   1.1       cgd 		}
    564   1.1       cgd 	}
    565   1.1       cgd 	initvar();
    566   1.1       cgd }
    567   1.1       cgd 
    568   1.1       cgd 
    569   1.1       cgd 
    570   1.1       cgd /*
    571   1.1       cgd  * Command to list all variables which are set.  Currently this command
    572   1.1       cgd  * is invoked from the set command when the set command is called without
    573   1.1       cgd  * any variables.
    574   1.1       cgd  */
    575   1.1       cgd 
    576  1.30  christos void
    577  1.30  christos print_quoted(const char *p)
    578  1.30  christos {
    579  1.30  christos 	const char *q;
    580  1.30  christos 
    581  1.50       kre 	if (p[0] == '\0') {
    582  1.50       kre 		out1fmt("''");
    583  1.50       kre 		return;
    584  1.50       kre 	}
    585  1.30  christos 	if (strcspn(p, "|&;<>()$`\\\"' \t\n*?[]#~=%") == strlen(p)) {
    586  1.30  christos 		out1fmt("%s", p);
    587  1.30  christos 		return;
    588  1.30  christos 	}
    589  1.30  christos 	while (*p) {
    590  1.30  christos 		if (*p == '\'') {
    591  1.30  christos 			out1fmt("\\'");
    592  1.30  christos 			p++;
    593  1.30  christos 			continue;
    594  1.30  christos 		}
    595  1.42  christos 		q = strchr(p, '\'');
    596  1.30  christos 		if (!q) {
    597  1.30  christos 			out1fmt("'%s'", p );
    598  1.30  christos 			return;
    599  1.30  christos 		}
    600  1.31       agc 		out1fmt("'%.*s'", (int)(q - p), p );
    601  1.30  christos 		p = q;
    602  1.30  christos 	}
    603  1.30  christos }
    604  1.30  christos 
    605  1.30  christos static int
    606  1.30  christos sort_var(const void *v_v1, const void *v_v2)
    607  1.30  christos {
    608  1.30  christos 	const struct var * const *v1 = v_v1;
    609  1.30  christos 	const struct var * const *v2 = v_v2;
    610  1.52       kre 	char *t1 = (*v1)->text, *t2 = (*v2)->text;
    611  1.30  christos 
    612  1.52       kre 	if (*t1 == *t2) {
    613  1.52       kre 		char *p, *s;
    614  1.52       kre 
    615  1.52       kre 		STARTSTACKSTR(p);
    616  1.52       kre 
    617  1.52       kre 		/*
    618  1.52       kre 		 * note: if lengths are equal, strings must be different
    619  1.52       kre 		 * so we don't care which string we pick for the \0 in
    620  1.52       kre 		 * that case.
    621  1.52       kre 		 */
    622  1.52       kre 		if ((strchr(t1, '=') - t1) <= (strchr(t2, '=') - t2)) {
    623  1.52       kre 			s = t1;
    624  1.52       kre 			t1 = p;
    625  1.52       kre 		} else {
    626  1.52       kre 			s = t2;
    627  1.52       kre 			t2 = p;
    628  1.52       kre 		}
    629  1.52       kre 
    630  1.52       kre 		while (*s && *s != '=') {
    631  1.52       kre 			STPUTC(*s, p);
    632  1.52       kre 			s++;
    633  1.52       kre 		}
    634  1.52       kre 		STPUTC('\0', p);
    635  1.52       kre 	}
    636  1.52       kre 
    637  1.52       kre 	return strcoll(t1, t2);
    638  1.30  christos }
    639  1.30  christos 
    640  1.30  christos /*
    641  1.30  christos  * POSIX requires that 'set' (but not export or readonly) output the
    642  1.30  christos  * variables in lexicographic order - by the locale's collating order (sigh).
    643  1.30  christos  * Maybe we could keep them in an ordered balanced binary tree
    644  1.30  christos  * instead of hashed lists.
    645  1.30  christos  * For now just roll 'em through qsort for printing...
    646  1.30  christos  */
    647  1.30  christos 
    648   1.1       cgd int
    649  1.49  christos showvars(const char *name, int flag, int show_value, const char *xtra)
    650  1.10       cgd {
    651   1.1       cgd 	struct var **vpp;
    652   1.1       cgd 	struct var *vp;
    653  1.30  christos 	const char *p;
    654  1.30  christos 
    655  1.30  christos 	static struct var **list;	/* static in case we are interrupted */
    656  1.30  christos 	static int list_len;
    657  1.30  christos 	int count = 0;
    658  1.30  christos 
    659  1.30  christos 	if (!list) {
    660  1.30  christos 		list_len = 32;
    661  1.30  christos 		list = ckmalloc(list_len * sizeof *list);
    662  1.30  christos 	}
    663   1.1       cgd 
    664   1.1       cgd 	for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
    665   1.1       cgd 		for (vp = *vpp ; vp ; vp = vp->next) {
    666  1.30  christos 			if (flag && !(vp->flags & flag))
    667  1.30  christos 				continue;
    668  1.30  christos 			if (vp->flags & VUNSET && !(show_value & 2))
    669  1.30  christos 				continue;
    670  1.30  christos 			if (count >= list_len) {
    671  1.30  christos 				list = ckrealloc(list,
    672  1.30  christos 					(list_len << 1) * sizeof *list);
    673  1.30  christos 				list_len <<= 1;
    674  1.30  christos 			}
    675  1.30  christos 			list[count++] = vp;
    676   1.1       cgd 		}
    677   1.1       cgd 	}
    678  1.30  christos 
    679  1.30  christos 	qsort(list, count, sizeof *list, sort_var);
    680  1.30  christos 
    681  1.30  christos 	for (vpp = list; count--; vpp++) {
    682  1.30  christos 		vp = *vpp;
    683  1.30  christos 		if (name)
    684  1.30  christos 			out1fmt("%s ", name);
    685  1.49  christos 		if (xtra)
    686  1.49  christos 			out1fmt("%s ", xtra);
    687  1.57       kre 		p = vp->text;
    688  1.57       kre 		if (vp->rfunc && (vp->flags & VFUNCREF) != 0) {
    689  1.57       kre 			p = (*vp->rfunc)(vp);
    690  1.57       kre 			if (p == NULL)
    691  1.57       kre 				p = vp->text;
    692  1.57       kre 		}
    693  1.57       kre 		for ( ; *p != '=' ; p++)
    694  1.30  christos 			out1c(*p);
    695  1.30  christos 		if (!(vp->flags & VUNSET) && show_value) {
    696  1.30  christos 			out1fmt("=");
    697  1.30  christos 			print_quoted(++p);
    698  1.30  christos 		}
    699  1.30  christos 		out1c('\n');
    700  1.30  christos 	}
    701   1.1       cgd 	return 0;
    702   1.1       cgd }
    703   1.1       cgd 
    704   1.1       cgd 
    705   1.1       cgd 
    706   1.1       cgd /*
    707   1.1       cgd  * The export and readonly commands.
    708   1.1       cgd  */
    709   1.1       cgd 
    710   1.1       cgd int
    711  1.30  christos exportcmd(int argc, char **argv)
    712  1.10       cgd {
    713   1.1       cgd 	struct var *vp;
    714   1.1       cgd 	char *name;
    715  1.23  christos 	const char *p;
    716   1.1       cgd 	int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT;
    717  1.49  christos 	int pflg = 0;
    718  1.49  christos 	int nflg = 0;
    719  1.49  christos 	int xflg = 0;
    720  1.49  christos 	int res;
    721  1.49  christos 	int c;
    722  1.51       kre 	int f;
    723  1.49  christos 
    724  1.49  christos 	while ((c = nextopt("npx")) != '\0') {
    725  1.49  christos 		switch (c) {
    726  1.49  christos 		case 'p':
    727  1.49  christos 			if (nflg)
    728  1.49  christos 				return 1;
    729  1.49  christos 			pflg = 3;
    730  1.49  christos 			break;
    731  1.49  christos 		case 'n':
    732  1.49  christos 			if (pflg || xflg || flag == VREADONLY)
    733  1.49  christos 				return 1;
    734  1.49  christos 			nflg = 1;
    735  1.49  christos 			break;
    736  1.49  christos 		case 'x':
    737  1.49  christos 			if (nflg || flag == VREADONLY)
    738  1.49  christos 				return 1;
    739  1.49  christos 			flag = VNOEXPORT;
    740  1.49  christos 			xflg = 1;
    741  1.49  christos 			break;
    742  1.49  christos 		default:
    743  1.49  christos 			return 1;
    744  1.49  christos 		}
    745  1.49  christos 	}
    746   1.1       cgd 
    747  1.49  christos 	if (nflg && *argptr == NULL)
    748  1.49  christos 		return 1;
    749  1.49  christos 
    750  1.49  christos 	if (pflg || *argptr == NULL) {
    751  1.49  christos 		showvars( pflg ? argv[0] : 0, flag, pflg,
    752  1.49  christos 		    pflg && xflg ? "-x" : NULL );
    753  1.30  christos 		return 0;
    754  1.30  christos 	}
    755  1.30  christos 
    756  1.49  christos 	res = 0;
    757  1.30  christos 	while ((name = *argptr++) != NULL) {
    758  1.51       kre 		f = flag;
    759  1.30  christos 		if ((p = strchr(name, '=')) != NULL) {
    760  1.30  christos 			p++;
    761  1.30  christos 		} else {
    762  1.35       dsl 			vp = find_var(name, NULL, NULL);
    763  1.35       dsl 			if (vp != NULL) {
    764  1.49  christos 				if (nflg)
    765  1.49  christos 					vp->flags &= ~flag;
    766  1.49  christos 				else if (flag&VEXPORT && vp->flags&VNOEXPORT)
    767  1.49  christos 					res = 1;
    768  1.49  christos 				else {
    769  1.49  christos 					vp->flags |= flag;
    770  1.49  christos 					if (flag == VNOEXPORT)
    771  1.49  christos 						vp->flags &= ~VEXPORT;
    772  1.49  christos 				}
    773  1.36     enami 				continue;
    774  1.51       kre 			} else
    775  1.51       kre 				f |= VUNSET;
    776   1.1       cgd 		}
    777  1.49  christos 		if (!nflg)
    778  1.51       kre 			setvar(name, p, f);
    779   1.1       cgd 	}
    780  1.49  christos 	return res;
    781   1.1       cgd }
    782   1.1       cgd 
    783   1.1       cgd 
    784   1.1       cgd /*
    785   1.1       cgd  * The "local" command.
    786   1.1       cgd  */
    787   1.1       cgd 
    788  1.10       cgd int
    789  1.30  christos localcmd(int argc, char **argv)
    790  1.10       cgd {
    791   1.1       cgd 	char *name;
    792  1.54       kre 	int c;
    793  1.54       kre 	int flags = 0;		/*XXX perhaps VUNSET from a -o option value */
    794   1.1       cgd 
    795   1.1       cgd 	if (! in_function())
    796   1.1       cgd 		error("Not in a function");
    797  1.54       kre 
    798  1.54       kre 	/* upper case options, as bash stole all the good ones ... */
    799  1.54       kre 	while ((c = nextopt("INx")) != '\0')
    800  1.54       kre 		switch (c) {
    801  1.54       kre 		case 'I':	flags &= ~VUNSET;	break;
    802  1.54       kre 		case 'N':	flags |= VUNSET;	break;
    803  1.54       kre 		case 'x':	flags |= VEXPORT;	break;
    804  1.54       kre 		}
    805  1.54       kre 
    806   1.1       cgd 	while ((name = *argptr++) != NULL) {
    807  1.54       kre 		mklocal(name, flags);
    808   1.1       cgd 	}
    809   1.1       cgd 	return 0;
    810   1.1       cgd }
    811   1.1       cgd 
    812   1.1       cgd 
    813   1.1       cgd /*
    814  1.37       snj  * Make a variable a local variable.  When a variable is made local, its
    815   1.1       cgd  * value and flags are saved in a localvar structure.  The saved values
    816   1.1       cgd  * will be restored when the shell function returns.  We handle the name
    817   1.1       cgd  * "-" as a special case.
    818   1.1       cgd  */
    819   1.1       cgd 
    820   1.1       cgd void
    821  1.32       dsl mklocal(const char *name, int flags)
    822  1.30  christos {
    823   1.1       cgd 	struct localvar *lvp;
    824   1.1       cgd 	struct var **vpp;
    825   1.1       cgd 	struct var *vp;
    826   1.1       cgd 
    827   1.1       cgd 	INTOFF;
    828   1.1       cgd 	lvp = ckmalloc(sizeof (struct localvar));
    829   1.1       cgd 	if (name[0] == '-' && name[1] == '\0') {
    830  1.23  christos 		char *p;
    831  1.30  christos 		p = ckmalloc(sizeof_optlist);
    832  1.30  christos 		lvp->text = memcpy(p, optlist, sizeof_optlist);
    833   1.1       cgd 		vp = NULL;
    834   1.1       cgd 	} else {
    835  1.35       dsl 		vp = find_var(name, &vpp, NULL);
    836   1.1       cgd 		if (vp == NULL) {
    837  1.54       kre 			flags &= ~VNOEXPORT;
    838   1.1       cgd 			if (strchr(name, '='))
    839  1.54       kre 				setvareq(savestr(name),
    840  1.54       kre 				    VSTRFIXED | (flags & ~VUNSET));
    841   1.1       cgd 			else
    842  1.29  christos 				setvar(name, NULL, VSTRFIXED|flags);
    843   1.1       cgd 			vp = *vpp;	/* the new variable */
    844   1.1       cgd 			lvp->text = NULL;
    845   1.1       cgd 			lvp->flags = VUNSET;
    846   1.1       cgd 		} else {
    847   1.1       cgd 			lvp->text = vp->text;
    848   1.1       cgd 			lvp->flags = vp->flags;
    849   1.1       cgd 			vp->flags |= VSTRFIXED|VTEXTFIXED;
    850  1.54       kre 			if (vp->flags & VNOEXPORT)
    851  1.54       kre 				flags &= ~VEXPORT;
    852  1.54       kre 			if (flags & (VNOEXPORT | VUNSET))
    853  1.54       kre 				vp->flags &= ~VEXPORT;
    854  1.54       kre 			flags &= ~VNOEXPORT;
    855  1.35       dsl 			if (name[vp->name_len] == '=')
    856  1.54       kre 				setvareq(savestr(name), flags & ~VUNSET);
    857  1.54       kre 			else if (flags & VUNSET)
    858  1.54       kre 				unsetvar(name, 0);
    859  1.54       kre 			else
    860  1.54       kre 				vp->flags |= flags & (VUNSET|VEXPORT);
    861  1.57       kre 
    862  1.57       kre 			if (vp == &line_num) {
    863  1.57       kre 				if (name[vp->name_len] == '=')
    864  1.57       kre 					funclinebase = funclineabs -1;
    865  1.57       kre 				else
    866  1.57       kre 					funclinebase = 0;
    867  1.57       kre 			}
    868   1.1       cgd 		}
    869   1.1       cgd 	}
    870   1.1       cgd 	lvp->vp = vp;
    871   1.1       cgd 	lvp->next = localvars;
    872   1.1       cgd 	localvars = lvp;
    873   1.1       cgd 	INTON;
    874   1.1       cgd }
    875   1.1       cgd 
    876   1.1       cgd 
    877   1.1       cgd /*
    878   1.1       cgd  * Called after a function returns.
    879   1.1       cgd  */
    880   1.1       cgd 
    881   1.1       cgd void
    882  1.30  christos poplocalvars(void)
    883  1.30  christos {
    884   1.1       cgd 	struct localvar *lvp;
    885   1.1       cgd 	struct var *vp;
    886   1.1       cgd 
    887   1.1       cgd 	while ((lvp = localvars) != NULL) {
    888   1.1       cgd 		localvars = lvp->next;
    889   1.1       cgd 		vp = lvp->vp;
    890  1.32       dsl 		TRACE(("poplocalvar %s", vp ? vp->text : "-"));
    891   1.1       cgd 		if (vp == NULL) {	/* $- saved */
    892  1.30  christos 			memcpy(optlist, lvp->text, sizeof_optlist);
    893   1.1       cgd 			ckfree(lvp->text);
    894  1.57       kre 			optschanged();
    895   1.1       cgd 		} else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) {
    896  1.30  christos 			(void)unsetvar(vp->text, 0);
    897   1.1       cgd 		} else {
    898  1.57       kre 			if (vp->func && (vp->flags & (VNOFUNC|VFUNCREF)) == 0)
    899  1.35       dsl 				(*vp->func)(lvp->text + vp->name_len + 1);
    900   1.1       cgd 			if ((vp->flags & VTEXTFIXED) == 0)
    901   1.1       cgd 				ckfree(vp->text);
    902   1.1       cgd 			vp->flags = lvp->flags;
    903   1.1       cgd 			vp->text = lvp->text;
    904   1.1       cgd 		}
    905   1.1       cgd 		ckfree(lvp);
    906   1.1       cgd 	}
    907   1.1       cgd }
    908   1.1       cgd 
    909   1.1       cgd 
    910  1.10       cgd int
    911  1.30  christos setvarcmd(int argc, char **argv)
    912  1.10       cgd {
    913   1.1       cgd 	if (argc <= 2)
    914   1.1       cgd 		return unsetcmd(argc, argv);
    915   1.1       cgd 	else if (argc == 3)
    916   1.1       cgd 		setvar(argv[1], argv[2], 0);
    917   1.1       cgd 	else
    918   1.1       cgd 		error("List assignment not implemented");
    919   1.1       cgd 	return 0;
    920   1.1       cgd }
    921   1.1       cgd 
    922   1.1       cgd 
    923   1.1       cgd /*
    924   1.1       cgd  * The unset builtin command.  We unset the function before we unset the
    925   1.1       cgd  * variable to allow a function to be unset when there is a readonly variable
    926   1.1       cgd  * with the same name.
    927   1.1       cgd  */
    928   1.1       cgd 
    929  1.10       cgd int
    930  1.30  christos unsetcmd(int argc, char **argv)
    931  1.10       cgd {
    932   1.1       cgd 	char **ap;
    933   1.5       jtc 	int i;
    934   1.5       jtc 	int flg_func = 0;
    935   1.5       jtc 	int flg_var = 0;
    936  1.54       kre 	int flg_x = 0;
    937   1.5       jtc 	int ret = 0;
    938   1.5       jtc 
    939  1.54       kre 	while ((i = nextopt("efvx")) != '\0') {
    940  1.54       kre 		switch (i) {
    941  1.54       kre 		case 'f':
    942   1.5       jtc 			flg_func = 1;
    943  1.54       kre 			break;
    944  1.54       kre 		case 'e':
    945  1.54       kre 		case 'x':
    946  1.54       kre 			flg_x = (2 >> (i == 'e'));
    947  1.54       kre 			/* FALLTHROUGH */
    948  1.54       kre 		case 'v':
    949  1.54       kre 			flg_var = 1;
    950  1.54       kre 			break;
    951  1.54       kre 		}
    952   1.5       jtc 	}
    953  1.54       kre 
    954   1.5       jtc 	if (flg_func == 0 && flg_var == 0)
    955   1.5       jtc 		flg_var = 1;
    956  1.15  christos 
    957   1.5       jtc 	for (ap = argptr; *ap ; ap++) {
    958   1.5       jtc 		if (flg_func)
    959   1.5       jtc 			ret |= unsetfunc(*ap);
    960   1.5       jtc 		if (flg_var)
    961  1.54       kre 			ret |= unsetvar(*ap, flg_x);
    962   1.1       cgd 	}
    963   1.5       jtc 	return ret;
    964   1.1       cgd }
    965   1.1       cgd 
    966   1.1       cgd 
    967   1.1       cgd /*
    968   1.1       cgd  * Unset the specified variable.
    969   1.1       cgd  */
    970   1.1       cgd 
    971  1.14  christos int
    972  1.30  christos unsetvar(const char *s, int unexport)
    973  1.30  christos {
    974   1.1       cgd 	struct var **vpp;
    975   1.1       cgd 	struct var *vp;
    976   1.1       cgd 
    977  1.35       dsl 	vp = find_var(s, &vpp, NULL);
    978  1.35       dsl 	if (vp == NULL)
    979  1.43  christos 		return 0;
    980  1.35       dsl 
    981  1.54       kre 	if (vp->flags & VREADONLY && !(unexport & 1))
    982  1.43  christos 		return 1;
    983  1.35       dsl 
    984  1.35       dsl 	INTOFF;
    985  1.54       kre 	if (unexport & 1) {
    986  1.35       dsl 		vp->flags &= ~VEXPORT;
    987  1.35       dsl 	} else {
    988  1.35       dsl 		if (vp->text[vp->name_len + 1] != '\0')
    989  1.35       dsl 			setvar(s, nullstr, 0);
    990  1.54       kre 		if (!(unexport & 2))
    991  1.54       kre 			vp->flags &= ~VEXPORT;
    992  1.35       dsl 		vp->flags |= VUNSET;
    993  1.54       kre 		if ((vp->flags&(VEXPORT|VSTRFIXED|VREADONLY|VNOEXPORT)) == 0) {
    994  1.35       dsl 			if ((vp->flags & VTEXTFIXED) == 0)
    995  1.35       dsl 				ckfree(vp->text);
    996  1.35       dsl 			*vpp = vp->next;
    997  1.35       dsl 			ckfree(vp);
    998   1.1       cgd 		}
    999   1.1       cgd 	}
   1000  1.35       dsl 	INTON;
   1001  1.35       dsl 	return 0;
   1002   1.1       cgd }
   1003   1.1       cgd 
   1004   1.1       cgd 
   1005   1.1       cgd /*
   1006   1.1       cgd  * Returns true if the two strings specify the same varable.  The first
   1007   1.1       cgd  * variable name is terminated by '='; the second may be terminated by
   1008   1.1       cgd  * either '=' or '\0'.
   1009   1.1       cgd  */
   1010   1.1       cgd 
   1011   1.1       cgd STATIC int
   1012  1.35       dsl strequal(const char *p, const char *q)
   1013  1.30  christos {
   1014   1.1       cgd 	while (*p == *q++) {
   1015   1.1       cgd 		if (*p++ == '=')
   1016   1.1       cgd 			return 1;
   1017   1.1       cgd 	}
   1018   1.1       cgd 	if (*p == '=' && *(q - 1) == '\0')
   1019   1.1       cgd 		return 1;
   1020   1.1       cgd 	return 0;
   1021   1.1       cgd }
   1022  1.35       dsl 
   1023  1.35       dsl /*
   1024  1.35       dsl  * Search for a variable.
   1025  1.35       dsl  * 'name' may be terminated by '=' or a NUL.
   1026  1.35       dsl  * vppp is set to the pointer to vp, or the list head if vp isn't found
   1027  1.35       dsl  * lenp is set to the number of charactets in 'name'
   1028  1.35       dsl  */
   1029  1.35       dsl 
   1030  1.35       dsl STATIC struct var *
   1031  1.35       dsl find_var(const char *name, struct var ***vppp, int *lenp)
   1032  1.35       dsl {
   1033  1.35       dsl 	unsigned int hashval;
   1034  1.35       dsl 	int len;
   1035  1.35       dsl 	struct var *vp, **vpp;
   1036  1.35       dsl 	const char *p = name;
   1037  1.35       dsl 
   1038  1.35       dsl 	hashval = 0;
   1039  1.35       dsl 	while (*p && *p != '=')
   1040  1.35       dsl 		hashval = 2 * hashval + (unsigned char)*p++;
   1041  1.35       dsl 	len = p - name;
   1042  1.35       dsl 
   1043  1.35       dsl 	if (lenp)
   1044  1.35       dsl 		*lenp = len;
   1045  1.35       dsl 	vpp = &vartab[hashval % VTABSIZE];
   1046  1.35       dsl 	if (vppp)
   1047  1.35       dsl 		*vppp = vpp;
   1048  1.35       dsl 
   1049  1.35       dsl 	for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) {
   1050  1.35       dsl 		if (vp->name_len != len)
   1051  1.35       dsl 			continue;
   1052  1.35       dsl 		if (memcmp(vp->text, name, len) != 0)
   1053  1.35       dsl 			continue;
   1054  1.35       dsl 		if (vppp)
   1055  1.35       dsl 			*vppp = vpp;
   1056  1.35       dsl 		return vp;
   1057  1.35       dsl 	}
   1058  1.35       dsl 	return NULL;
   1059  1.35       dsl }
   1060  1.57       kre 
   1061  1.57       kre char *
   1062  1.57       kre get_lineno(struct var *vp)
   1063  1.57       kre {
   1064  1.57       kre 	static char lineno_buf[8 + 14];
   1065  1.57       kre 	int ln = line_number;
   1066  1.57       kre 
   1067  1.57       kre 	if (vp->flags & VUNSET)
   1068  1.57       kre 		return NULL;
   1069  1.57       kre 
   1070  1.57       kre 	ln -= funclinebase;
   1071  1.57       kre 	snprintf(lineno_buf, sizeof(lineno_buf), "LINENO=%d", ln);;
   1072  1.57       kre 	return lineno_buf;
   1073  1.57       kre }
   1074