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