source: trunk/ash/var.c@ 3416

Last change on this file since 3416 was 3284, checked in by bird, 18 years ago

fixed unterminated LIBPATHSTRICT value.

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