var.c revision 221669
1/*-
2 * Copyright (c) 1991, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Kenneth Almquist.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#ifndef lint
34#if 0
35static char sccsid[] = "@(#)var.c	8.3 (Berkeley) 5/4/95";
36#endif
37#endif /* not lint */
38#include <sys/cdefs.h>
39__FBSDID("$FreeBSD: head/bin/sh/var.c 221669 2011-05-08 17:40:10Z jilles $");
40
41#include <unistd.h>
42#include <stdlib.h>
43#include <paths.h>
44
45/*
46 * Shell variables.
47 */
48
49#include <locale.h>
50#include <langinfo.h>
51
52#include "shell.h"
53#include "output.h"
54#include "expand.h"
55#include "nodes.h"	/* for other headers */
56#include "eval.h"	/* defines cmdenviron */
57#include "exec.h"
58#include "syntax.h"
59#include "options.h"
60#include "mail.h"
61#include "var.h"
62#include "memalloc.h"
63#include "error.h"
64#include "mystring.h"
65#include "parser.h"
66#ifndef NO_HISTORY
67#include "myhistedit.h"
68#endif
69
70
71#define VTABSIZE 39
72
73
74struct varinit {
75	struct var *var;
76	int flags;
77	const char *text;
78	void (*func)(const char *);
79};
80
81
82#ifndef NO_HISTORY
83struct var vhistsize;
84struct var vterm;
85#endif
86struct var vifs;
87struct var vmail;
88struct var vmpath;
89struct var vpath;
90struct var vppid;
91struct var vps1;
92struct var vps2;
93struct var vps4;
94struct var vvers;
95static struct var voptind;
96
97static const struct varinit varinit[] = {
98#ifndef NO_HISTORY
99	{ &vhistsize,	VUNSET,				"HISTSIZE=",
100	  sethistsize },
101#endif
102	{ &vifs,	0,				"IFS= \t\n",
103	  NULL },
104	{ &vmail,	VUNSET,				"MAIL=",
105	  NULL },
106	{ &vmpath,	VUNSET,				"MAILPATH=",
107	  NULL },
108	{ &vpath,	0,				"PATH=" _PATH_DEFPATH,
109	  changepath },
110	{ &vppid,	VUNSET,				"PPID=",
111	  NULL },
112	/*
113	 * vps1 depends on uid
114	 */
115	{ &vps2,	0,				"PS2=> ",
116	  NULL },
117	{ &vps4,	0,				"PS4=+ ",
118	  NULL },
119#ifndef NO_HISTORY
120	{ &vterm,	VUNSET,				"TERM=",
121	  setterm },
122#endif
123	{ &voptind,	0,				"OPTIND=1",
124	  getoptsreset },
125	{ NULL,	0,				NULL,
126	  NULL }
127};
128
129static struct var *vartab[VTABSIZE];
130
131static const char *const locale_names[7] = {
132	"LC_COLLATE", "LC_CTYPE", "LC_MONETARY",
133	"LC_NUMERIC", "LC_TIME", "LC_MESSAGES", NULL
134};
135static const int locale_categories[7] = {
136	LC_COLLATE, LC_CTYPE, LC_MONETARY, LC_NUMERIC, LC_TIME, LC_MESSAGES, 0
137};
138
139static int varequal(const char *, const char *);
140static struct var *find_var(const char *, struct var ***, int *);
141static int localevar(const char *);
142
143/*
144 * Initialize the variable symbol tables and import the environment.
145 */
146
147#ifdef mkinit
148INCLUDE "var.h"
149MKINIT char **environ;
150INIT {
151	char **envp;
152
153	initvar();
154	for (envp = environ ; *envp ; envp++) {
155		if (strchr(*envp, '=')) {
156			setvareq(*envp, VEXPORT|VTEXTFIXED);
157		}
158	}
159}
160#endif
161
162
163/*
164 * This routine initializes the builtin variables.  It is called when the
165 * shell is initialized.
166 */
167
168void
169initvar(void)
170{
171	char ppid[20];
172	const struct varinit *ip;
173	struct var *vp;
174	struct var **vpp;
175
176	for (ip = varinit ; (vp = ip->var) != NULL ; ip++) {
177		if (find_var(ip->text, &vpp, &vp->name_len) != NULL)
178			continue;
179		vp->next = *vpp;
180		*vpp = vp;
181		vp->text = __DECONST(char *, ip->text);
182		vp->flags = ip->flags | VSTRFIXED | VTEXTFIXED;
183		vp->func = ip->func;
184	}
185	/*
186	 * PS1 depends on uid
187	 */
188	if (find_var("PS1", &vpp, &vps1.name_len) == NULL) {
189		vps1.next = *vpp;
190		*vpp = &vps1;
191		vps1.text = __DECONST(char *, geteuid() ? "PS1=$ " : "PS1=# ");
192		vps1.flags = VSTRFIXED|VTEXTFIXED;
193	}
194	if ((vppid.flags & VEXPORT) == 0) {
195		fmtstr(ppid, sizeof(ppid), "%d", (int)getppid());
196		setvarsafe("PPID", ppid, 0);
197	}
198}
199
200/*
201 * Safe version of setvar, returns 1 on success 0 on failure.
202 */
203
204int
205setvarsafe(const char *name, const char *val, int flags)
206{
207	struct jmploc jmploc;
208	struct jmploc *const savehandler = handler;
209	int err = 0;
210	int inton;
211
212	inton = is_int_on();
213	if (setjmp(jmploc.loc))
214		err = 1;
215	else {
216		handler = &jmploc;
217		setvar(name, val, flags);
218	}
219	handler = savehandler;
220	SETINTON(inton);
221	return err;
222}
223
224/*
225 * Set the value of a variable.  The flags argument is stored with the
226 * flags of the variable.  If val is NULL, the variable is unset.
227 */
228
229void
230setvar(const char *name, const char *val, int flags)
231{
232	const char *p;
233	int len;
234	int namelen;
235	char *nameeq;
236	int isbad;
237
238	isbad = 0;
239	p = name;
240	if (! is_name(*p))
241		isbad = 1;
242	p++;
243	for (;;) {
244		if (! is_in_name(*p)) {
245			if (*p == '\0' || *p == '=')
246				break;
247			isbad = 1;
248		}
249		p++;
250	}
251	namelen = p - name;
252	if (isbad)
253		error("%.*s: bad variable name", namelen, name);
254	len = namelen + 2;		/* 2 is space for '=' and '\0' */
255	if (val == NULL) {
256		flags |= VUNSET;
257	} else {
258		len += strlen(val);
259	}
260	nameeq = ckmalloc(len);
261	memcpy(nameeq, name, namelen);
262	nameeq[namelen] = '=';
263	if (val)
264		scopy(val, nameeq + namelen + 1);
265	else
266		nameeq[namelen + 1] = '\0';
267	setvareq(nameeq, flags);
268}
269
270static int
271localevar(const char *s)
272{
273	const char *const *ss;
274
275	if (*s != 'L')
276		return 0;
277	if (varequal(s + 1, "ANG"))
278		return 1;
279	if (strncmp(s + 1, "C_", 2) != 0)
280		return 0;
281	if (varequal(s + 3, "ALL"))
282		return 1;
283	for (ss = locale_names; *ss ; ss++)
284		if (varequal(s + 3, *ss + 3))
285			return 1;
286	return 0;
287}
288
289
290/*
291 * Sets/unsets an environment variable from a pointer that may actually be a
292 * pointer into environ where the string should not be manipulated.
293 */
294static void
295change_env(const char *s, int set)
296{
297	char *eqp;
298	char *ss;
299
300	ss = savestr(s);
301	if ((eqp = strchr(ss, '=')) != NULL)
302		*eqp = '\0';
303	if (set && eqp != NULL)
304		(void) setenv(ss, eqp + 1, 1);
305	else
306		(void) unsetenv(ss);
307	ckfree(ss);
308
309	return;
310}
311
312
313/*
314 * Same as setvar except that the variable and value are passed in
315 * the first argument as name=value.  Since the first argument will
316 * be actually stored in the table, it should not be a string that
317 * will go away.
318 */
319
320void
321setvareq(char *s, int flags)
322{
323	struct var *vp, **vpp;
324	int nlen;
325
326	if (aflag)
327		flags |= VEXPORT;
328	vp = find_var(s, &vpp, &nlen);
329	if (vp != NULL) {
330		if (vp->flags & VREADONLY)
331			error("%.*s: is read only", vp->name_len, s);
332		if (flags & VNOSET)
333			return;
334		INTOFF;
335
336		if (vp->func && (flags & VNOFUNC) == 0)
337			(*vp->func)(s + vp->name_len + 1);
338
339		if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0)
340			ckfree(vp->text);
341
342		vp->flags &= ~(VTEXTFIXED|VSTACK|VUNSET);
343		vp->flags |= flags;
344		vp->text = s;
345
346		/*
347		 * We could roll this to a function, to handle it as
348		 * a regular variable function callback, but why bother?
349		 *
350		 * Note: this assumes iflag is not set to 1 initially.
351		 * As part of init(), this is called before arguments
352		 * are looked at.
353		 */
354		if ((vp == &vmpath || (vp == &vmail && ! mpathset())) &&
355		    iflag == 1)
356			chkmail(1);
357		if ((vp->flags & VEXPORT) && localevar(s)) {
358			change_env(s, 1);
359			(void) setlocale(LC_ALL, "");
360			updatecharset();
361		}
362		INTON;
363		return;
364	}
365	/* not found */
366	if (flags & VNOSET)
367		return;
368	vp = ckmalloc(sizeof (*vp));
369	vp->flags = flags;
370	vp->text = s;
371	vp->name_len = nlen;
372	vp->next = *vpp;
373	vp->func = NULL;
374	INTOFF;
375	*vpp = vp;
376	if ((vp->flags & VEXPORT) && localevar(s)) {
377		change_env(s, 1);
378		(void) setlocale(LC_ALL, "");
379		updatecharset();
380	}
381	INTON;
382}
383
384
385
386/*
387 * Process a linked list of variable assignments.
388 */
389
390void
391listsetvar(struct strlist *list, int flags)
392{
393	struct strlist *lp;
394
395	INTOFF;
396	for (lp = list ; lp ; lp = lp->next) {
397		setvareq(savestr(lp->text), flags);
398	}
399	INTON;
400}
401
402
403
404/*
405 * Find the value of a variable.  Returns NULL if not set.
406 */
407
408char *
409lookupvar(const char *name)
410{
411	struct var *v;
412
413	v = find_var(name, NULL, NULL);
414	if (v == NULL || v->flags & VUNSET)
415		return NULL;
416	return v->text + v->name_len + 1;
417}
418
419
420
421/*
422 * Search the environment of a builtin command.  If the second argument
423 * is nonzero, return the value of a variable even if it hasn't been
424 * exported.
425 */
426
427char *
428bltinlookup(const char *name, int doall)
429{
430	struct strlist *sp;
431	struct var *v;
432	char *result;
433
434	result = NULL;
435	for (sp = cmdenviron ; sp ; sp = sp->next) {
436		if (varequal(sp->text, name))
437			result = strchr(sp->text, '=') + 1;
438	}
439	if (result != NULL)
440		return result;
441
442	v = find_var(name, NULL, NULL);
443	if (v == NULL || v->flags & VUNSET ||
444	    (!doall && (v->flags & VEXPORT) == 0))
445		return NULL;
446	return v->text + v->name_len + 1;
447}
448
449
450/*
451 * Set up locale for a builtin (LANG/LC_* assignments).
452 */
453void
454bltinsetlocale(void)
455{
456	struct strlist *lp;
457	int act = 0;
458	char *loc, *locdef;
459	int i;
460
461	for (lp = cmdenviron ; lp ; lp = lp->next) {
462		if (localevar(lp->text)) {
463			act = 1;
464			break;
465		}
466	}
467	if (!act)
468		return;
469	loc = bltinlookup("LC_ALL", 0);
470	INTOFF;
471	if (loc != NULL) {
472		setlocale(LC_ALL, loc);
473		INTON;
474		updatecharset();
475		return;
476	}
477	locdef = bltinlookup("LANG", 0);
478	for (i = 0; locale_names[i] != NULL; i++) {
479		loc = bltinlookup(locale_names[i], 0);
480		if (loc == NULL)
481			loc = locdef;
482		if (loc != NULL)
483			setlocale(locale_categories[i], loc);
484	}
485	INTON;
486	updatecharset();
487}
488
489/*
490 * Undo the effect of bltinlocaleset().
491 */
492void
493bltinunsetlocale(void)
494{
495	struct strlist *lp;
496
497	INTOFF;
498	for (lp = cmdenviron ; lp ; lp = lp->next) {
499		if (localevar(lp->text)) {
500			setlocale(LC_ALL, "");
501			updatecharset();
502			return;
503		}
504	}
505	INTON;
506}
507
508/*
509 * Update the localeisutf8 flag.
510 */
511void
512updatecharset(void)
513{
514	char *charset;
515
516	charset = nl_langinfo(CODESET);
517	localeisutf8 = !strcmp(charset, "UTF-8");
518}
519
520void
521initcharset(void)
522{
523	updatecharset();
524	initial_localeisutf8 = localeisutf8;
525}
526
527/*
528 * Generate a list of exported variables.  This routine is used to construct
529 * the third argument to execve when executing a program.
530 */
531
532char **
533environment(void)
534{
535	int nenv;
536	struct var **vpp;
537	struct var *vp;
538	char **env, **ep;
539
540	nenv = 0;
541	for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
542		for (vp = *vpp ; vp ; vp = vp->next)
543			if (vp->flags & VEXPORT)
544				nenv++;
545	}
546	ep = env = stalloc((nenv + 1) * sizeof *env);
547	for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
548		for (vp = *vpp ; vp ; vp = vp->next)
549			if (vp->flags & VEXPORT)
550				*ep++ = vp->text;
551	}
552	*ep = NULL;
553	return env;
554}
555
556
557static int
558var_compare(const void *a, const void *b)
559{
560	const char *const *sa, *const *sb;
561
562	sa = a;
563	sb = b;
564	/*
565	 * This compares two var=value strings which creates a different
566	 * order from what you would probably expect.  POSIX is somewhat
567	 * ambiguous on what should be sorted exactly.
568	 */
569	return strcoll(*sa, *sb);
570}
571
572
573/*
574 * Command to list all variables which are set.  This is invoked from the
575 * set command when it is called without any options or operands.
576 */
577
578int
579showvarscmd(int argc __unused, char **argv __unused)
580{
581	struct var **vpp;
582	struct var *vp;
583	const char *s;
584	const char **vars;
585	int i, n;
586
587	/*
588	 * POSIX requires us to sort the variables.
589	 */
590	n = 0;
591	for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) {
592		for (vp = *vpp; vp; vp = vp->next) {
593			if (!(vp->flags & VUNSET))
594				n++;
595		}
596	}
597
598	INTON;
599	vars = ckmalloc(n * sizeof(*vars));
600	i = 0;
601	for (vpp = vartab; vpp < vartab + VTABSIZE; vpp++) {
602		for (vp = *vpp; vp; vp = vp->next) {
603			if (!(vp->flags & VUNSET))
604				vars[i++] = vp->text;
605		}
606	}
607
608	qsort(vars, n, sizeof(*vars), var_compare);
609	for (i = 0; i < n; i++) {
610		s = strchr(vars[i], '=');
611		s++;
612		outbin(vars[i], s - vars[i], out1);
613		out1qstr(s);
614		out1c('\n');
615	}
616	ckfree(vars);
617	INTOFF;
618
619	return 0;
620}
621
622
623
624/*
625 * The export and readonly commands.
626 */
627
628int
629exportcmd(int argc, char **argv)
630{
631	struct var **vpp;
632	struct var *vp;
633	char *name;
634	char *p;
635	char *cmdname;
636	int ch, values;
637	int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT;
638
639	cmdname = argv[0];
640	optreset = optind = 1;
641	opterr = 0;
642	values = 0;
643	while ((ch = getopt(argc, argv, "p")) != -1) {
644		switch (ch) {
645		case 'p':
646			values = 1;
647			break;
648		case '?':
649		default:
650			error("unknown option: -%c", optopt);
651		}
652	}
653	argc -= optind;
654	argv += optind;
655
656	if (values && argc != 0)
657		error("-p requires no arguments");
658	if (argc != 0) {
659		while ((name = *argv++) != NULL) {
660			if ((p = strchr(name, '=')) != NULL) {
661				p++;
662			} else {
663				vp = find_var(name, NULL, NULL);
664				if (vp != NULL) {
665					vp->flags |= flag;
666					if ((vp->flags & VEXPORT) && localevar(vp->text)) {
667						change_env(vp->text, 1);
668						(void) setlocale(LC_ALL, "");
669						updatecharset();
670					}
671					continue;
672				}
673			}
674			setvar(name, p, flag);
675		}
676	} else {
677		for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
678			for (vp = *vpp ; vp ; vp = vp->next) {
679				if (vp->flags & flag) {
680					if (values) {
681						out1str(cmdname);
682						out1c(' ');
683					}
684					p = strchr(vp->text, '=');
685					if (values && !(vp->flags & VUNSET)) {
686						p++;
687						outbin(vp->text, p - vp->text,
688						    out1);
689						out1qstr(p);
690					} else
691						outbin(vp->text, p - vp->text,
692						    out1);
693					out1c('\n');
694				}
695			}
696		}
697	}
698	return 0;
699}
700
701
702/*
703 * The "local" command.
704 */
705
706int
707localcmd(int argc __unused, char **argv __unused)
708{
709	char *name;
710
711	if (! in_function())
712		error("Not in a function");
713	while ((name = *argptr++) != NULL) {
714		mklocal(name);
715	}
716	return 0;
717}
718
719
720/*
721 * Make a variable a local variable.  When a variable is made local, it's
722 * value and flags are saved in a localvar structure.  The saved values
723 * will be restored when the shell function returns.  We handle the name
724 * "-" as a special case.
725 */
726
727void
728mklocal(char *name)
729{
730	struct localvar *lvp;
731	struct var **vpp;
732	struct var *vp;
733
734	INTOFF;
735	lvp = ckmalloc(sizeof (struct localvar));
736	if (name[0] == '-' && name[1] == '\0') {
737		lvp->text = ckmalloc(sizeof optlist);
738		memcpy(lvp->text, optlist, sizeof optlist);
739		vp = NULL;
740	} else {
741		vp = find_var(name, &vpp, NULL);
742		if (vp == NULL) {
743			if (strchr(name, '='))
744				setvareq(savestr(name), VSTRFIXED);
745			else
746				setvar(name, NULL, VSTRFIXED);
747			vp = *vpp;	/* the new variable */
748			lvp->text = NULL;
749			lvp->flags = VUNSET;
750		} else {
751			lvp->text = vp->text;
752			lvp->flags = vp->flags;
753			vp->flags |= VSTRFIXED|VTEXTFIXED;
754			if (name[vp->name_len] == '=')
755				setvareq(savestr(name), 0);
756		}
757	}
758	lvp->vp = vp;
759	lvp->next = localvars;
760	localvars = lvp;
761	INTON;
762}
763
764
765/*
766 * Called after a function returns.
767 */
768
769void
770poplocalvars(void)
771{
772	struct localvar *lvp;
773	struct var *vp;
774
775	while ((lvp = localvars) != NULL) {
776		localvars = lvp->next;
777		vp = lvp->vp;
778		if (vp == NULL) {	/* $- saved */
779			memcpy(optlist, lvp->text, sizeof optlist);
780			ckfree(lvp->text);
781			optschanged();
782		} else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) {
783			(void)unsetvar(vp->text);
784		} else {
785			if ((vp->flags & VTEXTFIXED) == 0)
786				ckfree(vp->text);
787			vp->flags = lvp->flags;
788			vp->text = lvp->text;
789		}
790		ckfree(lvp);
791	}
792}
793
794
795int
796setvarcmd(int argc, char **argv)
797{
798	if (argc <= 2)
799		return unsetcmd(argc, argv);
800	else if (argc == 3)
801		setvar(argv[1], argv[2], 0);
802	else
803		error("too many arguments");
804	return 0;
805}
806
807
808/*
809 * The unset builtin command.
810 */
811
812int
813unsetcmd(int argc __unused, char **argv __unused)
814{
815	char **ap;
816	int i;
817	int flg_func = 0;
818	int flg_var = 0;
819	int ret = 0;
820
821	while ((i = nextopt("vf")) != '\0') {
822		if (i == 'f')
823			flg_func = 1;
824		else
825			flg_var = 1;
826	}
827	if (flg_func == 0 && flg_var == 0)
828		flg_var = 1;
829
830	for (ap = argptr; *ap ; ap++) {
831		if (flg_func)
832			ret |= unsetfunc(*ap);
833		if (flg_var)
834			ret |= unsetvar(*ap);
835	}
836	return ret;
837}
838
839
840/*
841 * Unset the specified variable.
842 */
843
844int
845unsetvar(const char *s)
846{
847	struct var **vpp;
848	struct var *vp;
849
850	vp = find_var(s, &vpp, NULL);
851	if (vp == NULL)
852		return (0);
853	if (vp->flags & VREADONLY)
854		return (1);
855	INTOFF;
856	if (vp->text[vp->name_len + 1] != '\0')
857		setvar(s, nullstr, 0);
858	if ((vp->flags & VEXPORT) && localevar(vp->text)) {
859		change_env(s, 0);
860		setlocale(LC_ALL, "");
861		updatecharset();
862	}
863	vp->flags &= ~VEXPORT;
864	vp->flags |= VUNSET;
865	if ((vp->flags & VSTRFIXED) == 0) {
866		if ((vp->flags & VTEXTFIXED) == 0)
867			ckfree(vp->text);
868		*vpp = vp->next;
869		ckfree(vp);
870	}
871	INTON;
872	return (0);
873}
874
875
876
877/*
878 * Returns true if the two strings specify the same varable.  The first
879 * variable name is terminated by '='; the second may be terminated by
880 * either '=' or '\0'.
881 */
882
883static int
884varequal(const char *p, const char *q)
885{
886	while (*p == *q++) {
887		if (*p++ == '=')
888			return 1;
889	}
890	if (*p == '=' && *(q - 1) == '\0')
891		return 1;
892	return 0;
893}
894
895/*
896 * Search for a variable.
897 * 'name' may be terminated by '=' or a NUL.
898 * vppp is set to the pointer to vp, or the list head if vp isn't found
899 * lenp is set to the number of charactets in 'name'
900 */
901
902static struct var *
903find_var(const char *name, struct var ***vppp, int *lenp)
904{
905	unsigned int hashval;
906	int len;
907	struct var *vp, **vpp;
908	const char *p = name;
909
910	hashval = 0;
911	while (*p && *p != '=')
912		hashval = 2 * hashval + (unsigned char)*p++;
913	len = p - name;
914
915	if (lenp)
916		*lenp = len;
917	vpp = &vartab[hashval % VTABSIZE];
918	if (vppp)
919		*vppp = vpp;
920
921	for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) {
922		if (vp->name_len != len)
923			continue;
924		if (memcmp(vp->text, name, len) != 0)
925			continue;
926		if (vppp)
927			*vppp = vpp;
928		return vp;
929	}
930	return NULL;
931}
932