1 /* $NetBSD: var.c,v 1.44 2015/05/26 21:35:15 christos 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 38 static char sccsid[] = "@(#)var.c 8.3 (Berkeley) 5/4/95"; 39 #else 40 __RCSID("$NetBSD: var.c,v 1.44 2015/05/26 21:35:15 christos Exp $"); 41 #endif 42 #endif /* not lint */ 43 44 #include <unistd.h> 45 #include <stdlib.h> 46 #include <string.h> 47 #include <paths.h> 48 #include <limits.h> 49 50 /* 51 * Shell variables. 52 */ 53 54 #include "shell.h" 55 #include "output.h" 56 #include "expand.h" 57 #include "nodes.h" /* for other headers */ 58 #include "eval.h" /* defines cmdenviron */ 59 #include "exec.h" 60 #include "syntax.h" 61 #include "options.h" 62 #include "builtins.h" 63 #include "mail.h" 64 #include "var.h" 65 #include "memalloc.h" 66 #include "error.h" 67 #include "mystring.h" 68 #include "parser.h" 69 #include "show.h" 70 #ifndef SMALL 71 #include "myhistedit.h" 72 #endif 73 74 #ifdef SMALL 75 #define VTABSIZE 39 76 #else 77 #define VTABSIZE 517 78 #endif 79 80 81 struct varinit { 82 struct var *var; 83 int flags; 84 const char *text; 85 void (*func)(const char *); 86 }; 87 88 struct localvar *localvars; 89 90 #if ATTY 91 struct var vatty; 92 #endif 93 #ifndef SMALL 94 struct var vhistsize; 95 struct var vterm; 96 #endif 97 struct var vifs; 98 struct var vmail; 99 struct var vmpath; 100 struct var vpath; 101 struct var vps1; 102 struct var vps2; 103 struct var vps4; 104 struct var vvers; 105 struct var voptind; 106 107 const struct varinit varinit[] = { 108 #if ATTY 109 { &vatty, VSTRFIXED|VTEXTFIXED|VUNSET, "ATTY=", 110 NULL }, 111 #endif 112 #ifndef SMALL 113 { &vhistsize, VSTRFIXED|VTEXTFIXED|VUNSET, "HISTSIZE=", 114 sethistsize }, 115 #endif 116 { &vifs, VSTRFIXED|VTEXTFIXED, "IFS= \t\n", 117 NULL }, 118 { &vmail, VSTRFIXED|VTEXTFIXED|VUNSET, "MAIL=", 119 NULL }, 120 { &vmpath, VSTRFIXED|VTEXTFIXED|VUNSET, "MAILPATH=", 121 NULL }, 122 { &vpath, VSTRFIXED|VTEXTFIXED, "PATH=" _PATH_DEFPATH, 123 changepath }, 124 /* 125 * vps1 depends on uid 126 */ 127 { &vps2, VSTRFIXED|VTEXTFIXED, "PS2=> ", 128 NULL }, 129 { &vps4, VSTRFIXED|VTEXTFIXED, "PS4=+ ", 130 NULL }, 131 #ifndef SMALL 132 { &vterm, VSTRFIXED|VTEXTFIXED|VUNSET, "TERM=", 133 setterm }, 134 #endif 135 { &voptind, VSTRFIXED|VTEXTFIXED|VNOFUNC, "OPTIND=1", 136 getoptsreset }, 137 { NULL, 0, NULL, 138 NULL } 139 }; 140 141 struct var *vartab[VTABSIZE]; 142 143 STATIC int strequal(const char *, const char *); 144 STATIC struct var *find_var(const char *, struct var ***, int *); 145 146 /* 147 * Initialize the varable symbol tables and import the environment 148 */ 149 150 #ifdef mkinit 151 INCLUDE "var.h" 152 MKINIT char **environ; 153 INIT { 154 char **envp; 155 156 initvar(); 157 for (envp = environ ; *envp ; envp++) { 158 if (strchr(*envp, '=')) { 159 setvareq(*envp, VEXPORT|VTEXTFIXED); 160 } 161 } 162 } 163 #endif 164 165 166 /* 167 * This routine initializes the builtin variables. It is called when the 168 * shell is initialized and again when a shell procedure is spawned. 169 */ 170 171 void 172 initvar(void) 173 { 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 = strdup(ip->text); 184 vp->flags = ip->flags; 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.flags = VSTRFIXED|VTEXTFIXED; 194 vps1.text = NULL; 195 choose_ps1(); 196 } 197 } 198 199 void 200 choose_ps1(void) 201 { 202 free(vps1.text); 203 vps1.text = strdup(geteuid() ? "PS1=$ " : "PS1=# "); 204 } 205 206 /* 207 * Safe version of setvar, returns 1 on success 0 on failure. 208 */ 209 210 int 211 setvarsafe(const char *name, const char *val, int flags) 212 { 213 struct jmploc jmploc; 214 struct jmploc *volatile savehandler = handler; 215 int volatile err = 0; 216 217 if (setjmp(jmploc.loc)) 218 err = 1; 219 else { 220 handler = &jmploc; 221 setvar(name, val, flags); 222 } 223 handler = savehandler; 224 return err; 225 } 226 227 /* 228 * Set the value of a variable. The flags argument is ored with the 229 * flags of the variable. If val is NULL, the variable is unset. 230 */ 231 232 void 233 setvar(const char *name, const char *val, int flags) 234 { 235 const char *p; 236 const char *q; 237 char *d; 238 int len; 239 int namelen; 240 char *nameeq; 241 int isbad; 242 243 isbad = 0; 244 p = name; 245 if (! is_name(*p)) 246 isbad = 1; 247 p++; 248 for (;;) { 249 if (! is_in_name(*p)) { 250 if (*p == '\0' || *p == '=') 251 break; 252 isbad = 1; 253 } 254 p++; 255 } 256 namelen = p - name; 257 if (isbad) 258 error("%.*s: bad variable name", namelen, name); 259 len = namelen + 2; /* 2 is space for '=' and '\0' */ 260 if (val == NULL) { 261 flags |= VUNSET; 262 } else { 263 len += strlen(val); 264 } 265 d = nameeq = ckmalloc(len); 266 q = name; 267 while (--namelen >= 0) 268 *d++ = *q++; 269 *d++ = '='; 270 *d = '\0'; 271 if (val) 272 scopy(val, d); 273 setvareq(nameeq, flags); 274 } 275 276 277 278 /* 279 * Same as setvar except that the variable and value are passed in 280 * the first argument as name=value. Since the first argument will 281 * be actually stored in the table, it should not be a string that 282 * will go away. 283 */ 284 285 void 286 setvareq(char *s, int flags) 287 { 288 struct var *vp, **vpp; 289 int nlen; 290 291 if (aflag) 292 flags |= VEXPORT; 293 vp = find_var(s, &vpp, &nlen); 294 if (vp != NULL) { 295 if (vp->flags & VREADONLY) 296 error("%.*s: is read only", vp->name_len, s); 297 if (flags & VNOSET) 298 return; 299 INTOFF; 300 301 if (vp->func && (flags & VNOFUNC) == 0) 302 (*vp->func)(s + vp->name_len + 1); 303 304 if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0) 305 ckfree(vp->text); 306 307 vp->flags &= ~(VTEXTFIXED|VSTACK|VUNSET); 308 vp->flags |= flags & ~VNOFUNC; 309 vp->text = s; 310 311 /* 312 * We could roll this to a function, to handle it as 313 * a regular variable function callback, but why bother? 314 */ 315 if (vp == &vmpath || (vp == &vmail && ! mpathset())) 316 chkmail(1); 317 INTON; 318 return; 319 } 320 /* not found */ 321 if (flags & VNOSET) 322 return; 323 vp = ckmalloc(sizeof (*vp)); 324 vp->flags = flags & ~VNOFUNC; 325 vp->text = s; 326 vp->name_len = nlen; 327 vp->next = *vpp; 328 vp->func = NULL; 329 *vpp = vp; 330 } 331 332 333 334 /* 335 * Process a linked list of variable assignments. 336 */ 337 338 void 339 listsetvar(struct strlist *list, int flags) 340 { 341 struct strlist *lp; 342 343 INTOFF; 344 for (lp = list ; lp ; lp = lp->next) { 345 setvareq(savestr(lp->text), flags); 346 } 347 INTON; 348 } 349 350 void 351 listmklocal(struct strlist *list, int flags) 352 { 353 struct strlist *lp; 354 355 for (lp = list ; lp ; lp = lp->next) 356 mklocal(lp->text, flags); 357 } 358 359 360 /* 361 * Find the value of a variable. Returns NULL if not set. 362 */ 363 364 char * 365 lookupvar(const char *name) 366 { 367 struct var *v; 368 369 v = find_var(name, NULL, NULL); 370 if (v == NULL || v->flags & VUNSET) 371 return NULL; 372 return v->text + v->name_len + 1; 373 } 374 375 376 377 /* 378 * Search the environment of a builtin command. If the second argument 379 * is nonzero, return the value of a variable even if it hasn't been 380 * exported. 381 */ 382 383 char * 384 bltinlookup(const char *name, int doall) 385 { 386 struct strlist *sp; 387 struct var *v; 388 389 for (sp = cmdenviron ; sp ; sp = sp->next) { 390 if (strequal(sp->text, name)) 391 return strchr(sp->text, '=') + 1; 392 } 393 394 v = find_var(name, NULL, NULL); 395 396 if (v == NULL || v->flags & VUNSET || (!doall && !(v->flags & VEXPORT))) 397 return NULL; 398 return v->text + v->name_len + 1; 399 } 400 401 402 403 /* 404 * Generate a list of exported variables. This routine is used to construct 405 * the third argument to execve when executing a program. 406 */ 407 408 char ** 409 environment(void) 410 { 411 int nenv; 412 struct var **vpp; 413 struct var *vp; 414 char **env; 415 char **ep; 416 417 nenv = 0; 418 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) { 419 for (vp = *vpp ; vp ; vp = vp->next) 420 if (vp->flags & VEXPORT) 421 nenv++; 422 } 423 ep = env = stalloc((nenv + 1) * sizeof *env); 424 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) { 425 for (vp = *vpp ; vp ; vp = vp->next) 426 if (vp->flags & VEXPORT) 427 *ep++ = vp->text; 428 } 429 *ep = NULL; 430 return env; 431 } 432 433 434 /* 435 * Called when a shell procedure is invoked to clear out nonexported 436 * variables. It is also necessary to reallocate variables of with 437 * VSTACK set since these are currently allocated on the stack. 438 */ 439 440 #ifdef mkinit 441 void shprocvar(void); 442 443 SHELLPROC { 444 shprocvar(); 445 } 446 #endif 447 448 void 449 shprocvar(void) 450 { 451 struct var **vpp; 452 struct var *vp, **prev; 453 454 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) { 455 for (prev = vpp ; (vp = *prev) != NULL ; ) { 456 if ((vp->flags & VEXPORT) == 0) { 457 *prev = vp->next; 458 if ((vp->flags & VTEXTFIXED) == 0) 459 ckfree(vp->text); 460 if ((vp->flags & VSTRFIXED) == 0) 461 ckfree(vp); 462 } else { 463 if (vp->flags & VSTACK) { 464 vp->text = savestr(vp->text); 465 vp->flags &=~ VSTACK; 466 } 467 prev = &vp->next; 468 } 469 } 470 } 471 initvar(); 472 } 473 474 475 476 /* 477 * Command to list all variables which are set. Currently this command 478 * is invoked from the set command when the set command is called without 479 * any variables. 480 */ 481 482 void 483 print_quoted(const char *p) 484 { 485 const char *q; 486 487 if (strcspn(p, "|&;<>()$`\\\"' \t\n*?[]#~=%") == strlen(p)) { 488 out1fmt("%s", p); 489 return; 490 } 491 while (*p) { 492 if (*p == '\'') { 493 out1fmt("\\'"); 494 p++; 495 continue; 496 } 497 q = strchr(p, '\''); 498 if (!q) { 499 out1fmt("'%s'", p ); 500 return; 501 } 502 out1fmt("'%.*s'", (int)(q - p), p ); 503 p = q; 504 } 505 } 506 507 static int 508 sort_var(const void *v_v1, const void *v_v2) 509 { 510 const struct var * const *v1 = v_v1; 511 const struct var * const *v2 = v_v2; 512 513 /* XXX Will anyone notice we include the '=' of the shorter name? */ 514 return strcoll((*v1)->text, (*v2)->text); 515 } 516 517 /* 518 * POSIX requires that 'set' (but not export or readonly) output the 519 * variables in lexicographic order - by the locale's collating order (sigh). 520 * Maybe we could keep them in an ordered balanced binary tree 521 * instead of hashed lists. 522 * For now just roll 'em through qsort for printing... 523 */ 524 525 int 526 showvars(const char *name, int flag, int show_value) 527 { 528 struct var **vpp; 529 struct var *vp; 530 const char *p; 531 532 static struct var **list; /* static in case we are interrupted */ 533 static int list_len; 534 int count = 0; 535 536 if (!list) { 537 list_len = 32; 538 list = ckmalloc(list_len * sizeof *list); 539 } 540 541 for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) { 542 for (vp = *vpp ; vp ; vp = vp->next) { 543 if (flag && !(vp->flags & flag)) 544 continue; 545 if (vp->flags & VUNSET && !(show_value & 2)) 546 continue; 547 if (count >= list_len) { 548 list = ckrealloc(list, 549 (list_len << 1) * sizeof *list); 550 list_len <<= 1; 551 } 552 list[count++] = vp; 553 } 554 } 555 556 qsort(list, count, sizeof *list, sort_var); 557 558 for (vpp = list; count--; vpp++) { 559 vp = *vpp; 560 if (name) 561 out1fmt("%s ", name); 562 for (p = vp->text ; *p != '=' ; p++) 563 out1c(*p); 564 if (!(vp->flags & VUNSET) && show_value) { 565 out1fmt("="); 566 print_quoted(++p); 567 } 568 out1c('\n'); 569 } 570 return 0; 571 } 572 573 574 575 /* 576 * The export and readonly commands. 577 */ 578 579 int 580 exportcmd(int argc, char **argv) 581 { 582 struct var *vp; 583 char *name; 584 const char *p; 585 int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT; 586 int pflg; 587 588 pflg = nextopt("p") == 'p' ? 3 : 0; 589 if (argc <= 1 || pflg) { 590 showvars( pflg ? argv[0] : 0, flag, pflg ); 591 return 0; 592 } 593 594 while ((name = *argptr++) != NULL) { 595 if ((p = strchr(name, '=')) != NULL) { 596 p++; 597 } else { 598 vp = find_var(name, NULL, NULL); 599 if (vp != NULL) { 600 vp->flags |= flag; 601 continue; 602 } 603 } 604 setvar(name, p, flag); 605 } 606 return 0; 607 } 608 609 610 /* 611 * The "local" command. 612 */ 613 614 int 615 localcmd(int argc, char **argv) 616 { 617 char *name; 618 619 if (! in_function()) 620 error("Not in a function"); 621 while ((name = *argptr++) != NULL) { 622 mklocal(name, 0); 623 } 624 return 0; 625 } 626 627 628 /* 629 * Make a variable a local variable. When a variable is made local, its 630 * value and flags are saved in a localvar structure. The saved values 631 * will be restored when the shell function returns. We handle the name 632 * "-" as a special case. 633 */ 634 635 void 636 mklocal(const char *name, int flags) 637 { 638 struct localvar *lvp; 639 struct var **vpp; 640 struct var *vp; 641 642 INTOFF; 643 lvp = ckmalloc(sizeof (struct localvar)); 644 if (name[0] == '-' && name[1] == '\0') { 645 char *p; 646 p = ckmalloc(sizeof_optlist); 647 lvp->text = memcpy(p, optlist, sizeof_optlist); 648 vp = NULL; 649 } else { 650 vp = find_var(name, &vpp, NULL); 651 if (vp == NULL) { 652 if (strchr(name, '=')) 653 setvareq(savestr(name), VSTRFIXED|flags); 654 else 655 setvar(name, NULL, VSTRFIXED|flags); 656 vp = *vpp; /* the new variable */ 657 lvp->text = NULL; 658 lvp->flags = VUNSET; 659 } else { 660 lvp->text = vp->text; 661 lvp->flags = vp->flags; 662 vp->flags |= VSTRFIXED|VTEXTFIXED; 663 if (name[vp->name_len] == '=') 664 setvareq(savestr(name), flags); 665 } 666 } 667 lvp->vp = vp; 668 lvp->next = localvars; 669 localvars = lvp; 670 INTON; 671 } 672 673 674 /* 675 * Called after a function returns. 676 */ 677 678 void 679 poplocalvars(void) 680 { 681 struct localvar *lvp; 682 struct var *vp; 683 684 while ((lvp = localvars) != NULL) { 685 localvars = lvp->next; 686 vp = lvp->vp; 687 TRACE(("poplocalvar %s", vp ? vp->text : "-")); 688 if (vp == NULL) { /* $- saved */ 689 memcpy(optlist, lvp->text, sizeof_optlist); 690 ckfree(lvp->text); 691 } else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) { 692 (void)unsetvar(vp->text, 0); 693 } else { 694 if (vp->func && (vp->flags & VNOFUNC) == 0) 695 (*vp->func)(lvp->text + vp->name_len + 1); 696 if ((vp->flags & VTEXTFIXED) == 0) 697 ckfree(vp->text); 698 vp->flags = lvp->flags; 699 vp->text = lvp->text; 700 } 701 ckfree(lvp); 702 } 703 } 704 705 706 int 707 setvarcmd(int argc, char **argv) 708 { 709 if (argc <= 2) 710 return unsetcmd(argc, argv); 711 else if (argc == 3) 712 setvar(argv[1], argv[2], 0); 713 else 714 error("List assignment not implemented"); 715 return 0; 716 } 717 718 719 /* 720 * The unset builtin command. We unset the function before we unset the 721 * variable to allow a function to be unset when there is a readonly variable 722 * with the same name. 723 */ 724 725 int 726 unsetcmd(int argc, char **argv) 727 { 728 char **ap; 729 int i; 730 int flg_func = 0; 731 int flg_var = 0; 732 int ret = 0; 733 734 while ((i = nextopt("evf")) != '\0') { 735 if (i == 'f') 736 flg_func = 1; 737 else 738 flg_var = i; 739 } 740 if (flg_func == 0 && flg_var == 0) 741 flg_var = 1; 742 743 for (ap = argptr; *ap ; ap++) { 744 if (flg_func) 745 ret |= unsetfunc(*ap); 746 if (flg_var) 747 ret |= unsetvar(*ap, flg_var == 'e'); 748 } 749 return ret; 750 } 751 752 753 /* 754 * Unset the specified variable. 755 */ 756 757 int 758 unsetvar(const char *s, int unexport) 759 { 760 struct var **vpp; 761 struct var *vp; 762 763 vp = find_var(s, &vpp, NULL); 764 if (vp == NULL) 765 return 0; 766 767 if (vp->flags & VREADONLY) 768 return 1; 769 770 INTOFF; 771 if (unexport) { 772 vp->flags &= ~VEXPORT; 773 } else { 774 if (vp->text[vp->name_len + 1] != '\0') 775 setvar(s, nullstr, 0); 776 vp->flags &= ~VEXPORT; 777 vp->flags |= VUNSET; 778 if ((vp->flags & VSTRFIXED) == 0) { 779 if ((vp->flags & VTEXTFIXED) == 0) 780 ckfree(vp->text); 781 *vpp = vp->next; 782 ckfree(vp); 783 } 784 } 785 INTON; 786 return 0; 787 } 788 789 790 /* 791 * Returns true if the two strings specify the same varable. The first 792 * variable name is terminated by '='; the second may be terminated by 793 * either '=' or '\0'. 794 */ 795 796 STATIC int 797 strequal(const char *p, const char *q) 798 { 799 while (*p == *q++) { 800 if (*p++ == '=') 801 return 1; 802 } 803 if (*p == '=' && *(q - 1) == '\0') 804 return 1; 805 return 0; 806 } 807 808 /* 809 * Search for a variable. 810 * 'name' may be terminated by '=' or a NUL. 811 * vppp is set to the pointer to vp, or the list head if vp isn't found 812 * lenp is set to the number of charactets in 'name' 813 */ 814 815 STATIC struct var * 816 find_var(const char *name, struct var ***vppp, int *lenp) 817 { 818 unsigned int hashval; 819 int len; 820 struct var *vp, **vpp; 821 const char *p = name; 822 823 hashval = 0; 824 while (*p && *p != '=') 825 hashval = 2 * hashval + (unsigned char)*p++; 826 len = p - name; 827 828 if (lenp) 829 *lenp = len; 830 vpp = &vartab[hashval % VTABSIZE]; 831 if (vppp) 832 *vppp = vpp; 833 834 for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) { 835 if (vp->name_len != len) 836 continue; 837 if (memcmp(vp->text, name, len) != 0) 838 continue; 839 if (vppp) 840 *vppp = vpp; 841 return vp; 842 } 843 return NULL; 844 } 845