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