1 /* $NetBSD: expand.c,v 1.90 2013/10/06 21:05:50 ast 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[] = "@(#)expand.c 8.5 (Berkeley) 5/15/95"; 39 #else 40 __RCSID("$NetBSD: expand.c,v 1.90 2013/10/06 21:05:50 ast Exp $"); 41 #endif 42 #endif /* not lint */ 43 44 #include <sys/types.h> 45 #include <sys/time.h> 46 #include <sys/stat.h> 47 #include <errno.h> 48 #include <dirent.h> 49 #include <unistd.h> 50 #include <pwd.h> 51 #include <limits.h> 52 #include <stdlib.h> 53 #include <stdio.h> 54 55 /* 56 * Routines to expand arguments to commands. We have to deal with 57 * backquotes, shell variables, and file metacharacters. 58 */ 59 60 #include "shell.h" 61 #include "main.h" 62 #include "nodes.h" 63 #include "eval.h" 64 #include "expand.h" 65 #include "syntax.h" 66 #include "parser.h" 67 #include "jobs.h" 68 #include "options.h" 69 #include "builtins.h" 70 #include "var.h" 71 #include "input.h" 72 #include "output.h" 73 #include "memalloc.h" 74 #include "error.h" 75 #include "mystring.h" 76 #include "show.h" 77 78 /* 79 * Structure specifying which parts of the string should be searched 80 * for IFS characters. 81 */ 82 83 struct ifsregion { 84 struct ifsregion *next; /* next region in list */ 85 int begoff; /* offset of start of region */ 86 int endoff; /* offset of end of region */ 87 int inquotes; /* search for nul bytes only */ 88 }; 89 90 91 char *expdest; /* output of current string */ 92 struct nodelist *argbackq; /* list of back quote expressions */ 93 struct ifsregion ifsfirst; /* first struct in list of ifs regions */ 94 struct ifsregion *ifslastp; /* last struct in list */ 95 struct arglist exparg; /* holds expanded arg list */ 96 97 STATIC void argstr(char *, int); 98 STATIC char *exptilde(char *, int); 99 STATIC void expbackq(union node *, int, int); 100 STATIC int subevalvar(char *, char *, int, int, int, int); 101 STATIC char *evalvar(char *, int); 102 STATIC int varisset(char *, int); 103 STATIC void varvalue(char *, int, int, int); 104 STATIC void recordregion(int, int, int); 105 STATIC void removerecordregions(int); 106 STATIC void ifsbreakup(char *, struct arglist *); 107 STATIC void ifsfree(void); 108 STATIC void expandmeta(struct strlist *, int); 109 STATIC void expmeta(char *, char *); 110 STATIC void addfname(char *); 111 STATIC struct strlist *expsort(struct strlist *); 112 STATIC struct strlist *msort(struct strlist *, int); 113 STATIC int pmatch(char *, char *, int); 114 STATIC char *cvtnum(int, char *); 115 116 /* 117 * Expand shell variables and backquotes inside a here document. 118 */ 119 120 void 121 expandhere(union node *arg, int fd) 122 { 123 herefd = fd; 124 expandarg(arg, NULL, 0); 125 xwrite(fd, stackblock(), expdest - stackblock()); 126 } 127 128 129 /* 130 * Perform variable substitution and command substitution on an argument, 131 * placing the resulting list of arguments in arglist. If EXP_FULL is true, 132 * perform splitting and file name expansion. When arglist is NULL, perform 133 * here document expansion. 134 */ 135 136 void 137 expandarg(union node *arg, struct arglist *arglist, int flag) 138 { 139 struct strlist *sp; 140 char *p; 141 142 argbackq = arg->narg.backquote; 143 STARTSTACKSTR(expdest); 144 ifsfirst.next = NULL; 145 ifslastp = NULL; 146 argstr(arg->narg.text, flag); 147 if (arglist == NULL) { 148 return; /* here document expanded */ 149 } 150 STPUTC('\0', expdest); 151 p = grabstackstr(expdest); 152 exparg.lastp = &exparg.list; 153 /* 154 * TODO - EXP_REDIR 155 */ 156 if (flag & EXP_FULL) { 157 ifsbreakup(p, &exparg); 158 *exparg.lastp = NULL; 159 exparg.lastp = &exparg.list; 160 expandmeta(exparg.list, flag); 161 } else { 162 if (flag & EXP_REDIR) /*XXX - for now, just remove escapes */ 163 rmescapes(p); 164 sp = (struct strlist *)stalloc(sizeof (struct strlist)); 165 sp->text = p; 166 *exparg.lastp = sp; 167 exparg.lastp = &sp->next; 168 } 169 ifsfree(); 170 *exparg.lastp = NULL; 171 if (exparg.list) { 172 *arglist->lastp = exparg.list; 173 arglist->lastp = exparg.lastp; 174 } 175 } 176 177 178 179 /* 180 * Perform variable and command substitution. 181 * If EXP_FULL is set, output CTLESC characters to allow for further processing. 182 * Otherwise treat $@ like $* since no splitting will be performed. 183 */ 184 185 STATIC void 186 argstr(char *p, int flag) 187 { 188 char c; 189 int quotes = flag & (EXP_FULL | EXP_CASE); /* do CTLESC */ 190 int firsteq = 1; 191 const char *ifs = NULL; 192 int ifs_split = EXP_IFS_SPLIT; 193 194 if (flag & EXP_IFS_SPLIT) 195 ifs = ifsset() ? ifsval() : " \t\n"; 196 197 if (*p == '~' && (flag & (EXP_TILDE | EXP_VARTILDE))) 198 p = exptilde(p, flag); 199 for (;;) { 200 switch (c = *p++) { 201 case '\0': 202 case CTLENDVAR: /* end of expanding yyy in ${xxx-yyy} */ 203 return; 204 case CTLQUOTEMARK: 205 /* "$@" syntax adherence hack */ 206 if (p[0] == CTLVAR && p[2] == '@' && p[3] == '=') 207 break; 208 if ((flag & EXP_FULL) != 0) 209 STPUTC(c, expdest); 210 ifs_split = 0; 211 break; 212 case CTLQUOTEEND: 213 ifs_split = EXP_IFS_SPLIT; 214 break; 215 case CTLESC: 216 if (quotes) 217 STPUTC(c, expdest); 218 c = *p++; 219 STPUTC(c, expdest); 220 break; 221 case CTLVAR: 222 p = evalvar(p, (flag & ~EXP_IFS_SPLIT) | (flag & ifs_split)); 223 break; 224 case CTLBACKQ: 225 case CTLBACKQ|CTLQUOTE: 226 expbackq(argbackq->n, c & CTLQUOTE, flag); 227 argbackq = argbackq->next; 228 break; 229 case CTLENDARI: 230 expari(flag); 231 break; 232 case ':': 233 case '=': 234 /* 235 * sort of a hack - expand tildes in variable 236 * assignments (after the first '=' and after ':'s). 237 */ 238 STPUTC(c, expdest); 239 if (flag & EXP_VARTILDE && *p == '~') { 240 if (c == '=') { 241 if (firsteq) 242 firsteq = 0; 243 else 244 break; 245 } 246 p = exptilde(p, flag); 247 } 248 break; 249 default: 250 STPUTC(c, expdest); 251 if (flag & ifs_split && strchr(ifs, c) != NULL) { 252 /* We need to get the output split here... */ 253 recordregion(expdest - stackblock() - 1, 254 expdest - stackblock(), 0); 255 } 256 break; 257 } 258 } 259 } 260 261 STATIC char * 262 exptilde(char *p, int flag) 263 { 264 char c, *startp = p; 265 struct passwd *pw; 266 const char *home; 267 int quotes = flag & (EXP_FULL | EXP_CASE); 268 269 while ((c = *p) != '\0') { 270 switch(c) { 271 case CTLESC: 272 return (startp); 273 case CTLQUOTEMARK: 274 return (startp); 275 case ':': 276 if (flag & EXP_VARTILDE) 277 goto done; 278 break; 279 case '/': 280 goto done; 281 } 282 p++; 283 } 284 done: 285 *p = '\0'; 286 if (*(startp+1) == '\0') { 287 if ((home = lookupvar("HOME")) == NULL) 288 goto lose; 289 } else { 290 if ((pw = getpwnam(startp+1)) == NULL) 291 goto lose; 292 home = pw->pw_dir; 293 } 294 if (*home == '\0') 295 goto lose; 296 *p = c; 297 while ((c = *home++) != '\0') { 298 if (quotes && SQSYNTAX[(int)c] == CCTL) 299 STPUTC(CTLESC, expdest); 300 STPUTC(c, expdest); 301 } 302 return (p); 303 lose: 304 *p = c; 305 return (startp); 306 } 307 308 309 STATIC void 310 removerecordregions(int endoff) 311 { 312 if (ifslastp == NULL) 313 return; 314 315 if (ifsfirst.endoff > endoff) { 316 while (ifsfirst.next != NULL) { 317 struct ifsregion *ifsp; 318 INTOFF; 319 ifsp = ifsfirst.next->next; 320 ckfree(ifsfirst.next); 321 ifsfirst.next = ifsp; 322 INTON; 323 } 324 if (ifsfirst.begoff > endoff) 325 ifslastp = NULL; 326 else { 327 ifslastp = &ifsfirst; 328 ifsfirst.endoff = endoff; 329 } 330 return; 331 } 332 333 ifslastp = &ifsfirst; 334 while (ifslastp->next && ifslastp->next->begoff < endoff) 335 ifslastp=ifslastp->next; 336 while (ifslastp->next != NULL) { 337 struct ifsregion *ifsp; 338 INTOFF; 339 ifsp = ifslastp->next->next; 340 ckfree(ifslastp->next); 341 ifslastp->next = ifsp; 342 INTON; 343 } 344 if (ifslastp->endoff > endoff) 345 ifslastp->endoff = endoff; 346 } 347 348 349 /* 350 * Expand arithmetic expression. Backup to start of expression, 351 * evaluate, place result in (backed up) result, adjust string position. 352 */ 353 void 354 expari(int flag) 355 { 356 char *p, *start; 357 intmax_t result; 358 int adjustment; 359 int begoff; 360 int quotes = flag & (EXP_FULL | EXP_CASE); 361 int quoted; 362 363 /* ifsfree(); */ 364 365 /* 366 * This routine is slightly over-complicated for 367 * efficiency. First we make sure there is 368 * enough space for the result, which may be bigger 369 * than the expression if we add exponentation. Next we 370 * scan backwards looking for the start of arithmetic. If the 371 * next previous character is a CTLESC character, then we 372 * have to rescan starting from the beginning since CTLESC 373 * characters have to be processed left to right. 374 */ 375 /* SPACE_NEEDED is enough for all digits, plus possible "-", plus 2 (why?) */ 376 #define SPACE_NEEDED ((sizeof(intmax_t) * CHAR_BIT + 2) / 3 + 1 + 2) 377 CHECKSTRSPACE((int)(SPACE_NEEDED - 2), expdest); 378 USTPUTC('\0', expdest); 379 start = stackblock(); 380 p = expdest - 1; 381 while (*p != CTLARI && p >= start) 382 --p; 383 if (*p != CTLARI) 384 error("missing CTLARI (shouldn't happen)"); 385 if (p > start && *(p-1) == CTLESC) 386 for (p = start; *p != CTLARI; p++) 387 if (*p == CTLESC) 388 p++; 389 390 if (p[1] == '"') 391 quoted=1; 392 else 393 quoted=0; 394 begoff = p - start; 395 removerecordregions(begoff); 396 if (quotes) 397 rmescapes(p+2); 398 result = arith(p+2); 399 fmtstr(p, SPACE_NEEDED, "%"PRIdMAX, result); 400 401 while (*p++) 402 ; 403 404 if (quoted == 0) 405 recordregion(begoff, p - 1 - start, 0); 406 adjustment = expdest - p + 1; 407 STADJUST(-adjustment, expdest); 408 } 409 410 411 /* 412 * Expand stuff in backwards quotes. 413 */ 414 415 STATIC void 416 expbackq(union node *cmd, int quoted, int flag) 417 { 418 struct backcmd in; 419 int i; 420 char buf[128]; 421 char *p; 422 char *dest = expdest; 423 struct ifsregion saveifs, *savelastp; 424 struct nodelist *saveargbackq; 425 char lastc; 426 int startloc = dest - stackblock(); 427 char const *syntax = quoted? DQSYNTAX : BASESYNTAX; 428 int saveherefd; 429 int quotes = flag & (EXP_FULL | EXP_CASE); 430 int nnl; 431 432 INTOFF; 433 saveifs = ifsfirst; 434 savelastp = ifslastp; 435 saveargbackq = argbackq; 436 saveherefd = herefd; 437 herefd = -1; 438 p = grabstackstr(dest); 439 evalbackcmd(cmd, &in); 440 ungrabstackstr(p, dest); 441 ifsfirst = saveifs; 442 ifslastp = savelastp; 443 argbackq = saveargbackq; 444 herefd = saveherefd; 445 446 p = in.buf; 447 lastc = '\0'; 448 nnl = 0; 449 for (;;) { 450 if (--in.nleft < 0) { 451 if (in.fd < 0) 452 break; 453 while ((i = read(in.fd, buf, sizeof buf)) < 0 && errno == EINTR); 454 TRACE(("expbackq: read returns %d\n", i)); 455 if (i <= 0) 456 break; 457 p = buf; 458 in.nleft = i - 1; 459 } 460 lastc = *p++; 461 if (lastc != '\0') { 462 if (lastc == '\n') 463 nnl++; 464 else { 465 CHECKSTRSPACE(nnl + 2, dest); 466 while (nnl > 0) { 467 nnl--; 468 USTPUTC('\n', dest); 469 } 470 if (quotes && syntax[(int)lastc] == CCTL) 471 USTPUTC(CTLESC, dest); 472 USTPUTC(lastc, dest); 473 } 474 } 475 } 476 477 if (in.fd >= 0) 478 close(in.fd); 479 if (in.buf) 480 ckfree(in.buf); 481 if (in.jp) 482 back_exitstatus = waitforjob(in.jp); 483 if (quoted == 0) 484 recordregion(startloc, dest - stackblock(), 0); 485 TRACE(("evalbackq: size=%d: \"%.*s\"\n", 486 (int)((dest - stackblock()) - startloc), 487 (int)((dest - stackblock()) - startloc), 488 stackblock() + startloc)); 489 expdest = dest; 490 INTON; 491 } 492 493 494 495 STATIC int 496 subevalvar(char *p, char *str, int strloc, int subtype, int startloc, int varflags) 497 { 498 char *startp; 499 char *loc = NULL; 500 char *q; 501 int c = 0; 502 int saveherefd = herefd; 503 struct nodelist *saveargbackq = argbackq; 504 int amount, how; 505 506 herefd = -1; 507 switch (subtype) { 508 case VSTRIMLEFT: 509 case VSTRIMLEFTMAX: 510 case VSTRIMRIGHT: 511 case VSTRIMRIGHTMAX: 512 how = (varflags & VSQUOTE) ? 0 : EXP_CASE; 513 break; 514 default: 515 how = 0; 516 break; 517 } 518 argstr(p, how); 519 STACKSTRNUL(expdest); 520 herefd = saveherefd; 521 argbackq = saveargbackq; 522 startp = stackblock() + startloc; 523 if (str == NULL) 524 str = stackblock() + strloc; 525 526 switch (subtype) { 527 case VSASSIGN: 528 setvar(str, startp, 0); 529 amount = startp - expdest; 530 STADJUST(amount, expdest); 531 varflags &= ~VSNUL; 532 return 1; 533 534 case VSQUESTION: 535 if (*p != CTLENDVAR) { 536 outfmt(&errout, "%s\n", startp); 537 error(NULL); 538 } 539 error("%.*s: parameter %snot set", 540 (int)(p - str - 1), 541 str, (varflags & VSNUL) ? "null or " 542 : nullstr); 543 /* NOTREACHED */ 544 545 case VSTRIMLEFT: 546 for (loc = startp; loc < str; loc++) { 547 c = *loc; 548 *loc = '\0'; 549 if (patmatch(str, startp, varflags & VSQUOTE)) 550 goto recordleft; 551 *loc = c; 552 if ((varflags & VSQUOTE) && *loc == CTLESC) 553 loc++; 554 } 555 return 0; 556 557 case VSTRIMLEFTMAX: 558 for (loc = str - 1; loc >= startp;) { 559 c = *loc; 560 *loc = '\0'; 561 if (patmatch(str, startp, varflags & VSQUOTE)) 562 goto recordleft; 563 *loc = c; 564 loc--; 565 if ((varflags & VSQUOTE) && loc > startp && 566 *(loc - 1) == CTLESC) { 567 for (q = startp; q < loc; q++) 568 if (*q == CTLESC) 569 q++; 570 if (q > loc) 571 loc--; 572 } 573 } 574 return 0; 575 576 case VSTRIMRIGHT: 577 for (loc = str - 1; loc >= startp;) { 578 if (patmatch(str, loc, varflags & VSQUOTE)) 579 goto recordright; 580 loc--; 581 if ((varflags & VSQUOTE) && loc > startp && 582 *(loc - 1) == CTLESC) { 583 for (q = startp; q < loc; q++) 584 if (*q == CTLESC) 585 q++; 586 if (q > loc) 587 loc--; 588 } 589 } 590 return 0; 591 592 case VSTRIMRIGHTMAX: 593 for (loc = startp; loc < str - 1; loc++) { 594 if (patmatch(str, loc, varflags & VSQUOTE)) 595 goto recordright; 596 if ((varflags & VSQUOTE) && *loc == CTLESC) 597 loc++; 598 } 599 return 0; 600 601 default: 602 abort(); 603 } 604 605 recordleft: 606 *loc = c; 607 amount = ((str - 1) - (loc - startp)) - expdest; 608 STADJUST(amount, expdest); 609 while (loc != str - 1) 610 *startp++ = *loc++; 611 return 1; 612 613 recordright: 614 amount = loc - expdest; 615 STADJUST(amount, expdest); 616 STPUTC('\0', expdest); 617 STADJUST(-1, expdest); 618 return 1; 619 } 620 621 622 /* 623 * Expand a variable, and return a pointer to the next character in the 624 * input string. 625 */ 626 627 STATIC char * 628 evalvar(char *p, int flag) 629 { 630 int subtype; 631 int varflags; 632 char *var; 633 char *val; 634 int patloc; 635 int c; 636 int set; 637 int special; 638 int startloc; 639 int varlen; 640 int apply_ifs; 641 int quotes = flag & (EXP_FULL | EXP_CASE); 642 643 varflags = (unsigned char)*p++; 644 subtype = varflags & VSTYPE; 645 var = p; 646 special = !is_name(*p); 647 p = strchr(p, '=') + 1; 648 649 again: /* jump here after setting a variable with ${var=text} */ 650 if (varflags & VSLINENO) { 651 set = 1; 652 special = 0; 653 val = var; 654 p[-1] = '\0'; 655 } else if (special) { 656 set = varisset(var, varflags & VSNUL); 657 val = NULL; 658 } else { 659 val = lookupvar(var); 660 if (val == NULL || ((varflags & VSNUL) && val[0] == '\0')) { 661 val = NULL; 662 set = 0; 663 } else 664 set = 1; 665 } 666 667 varlen = 0; 668 startloc = expdest - stackblock(); 669 670 if (!set && uflag && *var != '@' && *var != '*') { 671 switch (subtype) { 672 case VSNORMAL: 673 case VSTRIMLEFT: 674 case VSTRIMLEFTMAX: 675 case VSTRIMRIGHT: 676 case VSTRIMRIGHTMAX: 677 case VSLENGTH: 678 error("%.*s: parameter not set", 679 (int)(p - var - 1), var); 680 /* NOTREACHED */ 681 } 682 } 683 684 if (set && subtype != VSPLUS) { 685 /* insert the value of the variable */ 686 if (special) { 687 varvalue(var, varflags & VSQUOTE, subtype, flag); 688 if (subtype == VSLENGTH) { 689 varlen = expdest - stackblock() - startloc; 690 STADJUST(-varlen, expdest); 691 } 692 } else { 693 char const *syntax = (varflags & VSQUOTE) ? DQSYNTAX 694 : BASESYNTAX; 695 696 if (subtype == VSLENGTH) { 697 for (;*val; val++) 698 varlen++; 699 } else { 700 while (*val) { 701 if (quotes && syntax[(int)*val] == CCTL) 702 STPUTC(CTLESC, expdest); 703 STPUTC(*val++, expdest); 704 } 705 706 } 707 } 708 } 709 710 711 if (flag & EXP_IN_QUOTES) 712 apply_ifs = 0; 713 else if (varflags & VSQUOTE) { 714 if (*var == '@' && shellparam.nparam != 1) 715 apply_ifs = 1; 716 else { 717 /* 718 * Mark so that we don't apply IFS if we recurse through 719 * here expanding $bar from "${foo-$bar}". 720 */ 721 flag |= EXP_IN_QUOTES; 722 apply_ifs = 0; 723 } 724 } else 725 apply_ifs = 1; 726 727 switch (subtype) { 728 case VSLENGTH: 729 expdest = cvtnum(varlen, expdest); 730 break; 731 732 case VSNORMAL: 733 break; 734 735 case VSPLUS: 736 set = !set; 737 /* FALLTHROUGH */ 738 case VSMINUS: 739 if (!set) { 740 argstr(p, flag | (apply_ifs ? EXP_IFS_SPLIT : 0)); 741 /* 742 * ${x-a b c} doesn't get split, but removing the 743 * 'apply_ifs = 0' apparently breaks ${1+"$@"}.. 744 * ${x-'a b' c} should generate 2 args. 745 */ 746 /* We should have marked stuff already */ 747 apply_ifs = 0; 748 } 749 break; 750 751 case VSTRIMLEFT: 752 case VSTRIMLEFTMAX: 753 case VSTRIMRIGHT: 754 case VSTRIMRIGHTMAX: 755 if (!set) 756 break; 757 /* 758 * Terminate the string and start recording the pattern 759 * right after it 760 */ 761 STPUTC('\0', expdest); 762 patloc = expdest - stackblock(); 763 if (subevalvar(p, NULL, patloc, subtype, 764 startloc, varflags) == 0) { 765 int amount = (expdest - stackblock() - patloc) + 1; 766 STADJUST(-amount, expdest); 767 } 768 /* Remove any recorded regions beyond start of variable */ 769 removerecordregions(startloc); 770 apply_ifs = 1; 771 break; 772 773 case VSASSIGN: 774 case VSQUESTION: 775 if (set) 776 break; 777 if (subevalvar(p, var, 0, subtype, startloc, varflags)) { 778 varflags &= ~VSNUL; 779 /* 780 * Remove any recorded regions beyond 781 * start of variable 782 */ 783 removerecordregions(startloc); 784 goto again; 785 } 786 apply_ifs = 0; 787 break; 788 789 default: 790 abort(); 791 } 792 p[-1] = '='; /* recover overwritten '=' */ 793 794 if (apply_ifs) 795 recordregion(startloc, expdest - stackblock(), 796 varflags & VSQUOTE); 797 798 if (subtype != VSNORMAL) { /* skip to end of alternative */ 799 int nesting = 1; 800 for (;;) { 801 if ((c = *p++) == CTLESC) 802 p++; 803 else if (c == CTLBACKQ || c == (CTLBACKQ|CTLQUOTE)) { 804 if (set) 805 argbackq = argbackq->next; 806 } else if (c == CTLVAR) { 807 if ((*p++ & VSTYPE) != VSNORMAL) 808 nesting++; 809 } else if (c == CTLENDVAR) { 810 if (--nesting == 0) 811 break; 812 } 813 } 814 } 815 return p; 816 } 817 818 819 820 /* 821 * Test whether a specialized variable is set. 822 */ 823 824 STATIC int 825 varisset(char *name, int nulok) 826 { 827 if (*name == '!') 828 return backgndpid != -1; 829 else if (*name == '@' || *name == '*') { 830 if (*shellparam.p == NULL) 831 return 0; 832 833 if (nulok) { 834 char **av; 835 836 for (av = shellparam.p; *av; av++) 837 if (**av != '\0') 838 return 1; 839 return 0; 840 } 841 } else if (is_digit(*name)) { 842 char *ap; 843 int num = atoi(name); 844 845 if (num > shellparam.nparam) 846 return 0; 847 848 if (num == 0) 849 ap = arg0; 850 else 851 ap = shellparam.p[num - 1]; 852 853 if (nulok && (ap == NULL || *ap == '\0')) 854 return 0; 855 } 856 return 1; 857 } 858 859 860 861 /* 862 * Add the value of a specialized variable to the stack string. 863 */ 864 865 STATIC void 866 varvalue(char *name, int quoted, int subtype, int flag) 867 { 868 int num; 869 char *p; 870 int i; 871 char sep; 872 char **ap; 873 char const *syntax; 874 875 #define STRTODEST(p) \ 876 do {\ 877 if (flag & (EXP_FULL | EXP_CASE) && subtype != VSLENGTH) { \ 878 syntax = quoted? DQSYNTAX : BASESYNTAX; \ 879 while (*p) { \ 880 if (syntax[(int)*p] == CCTL) \ 881 STPUTC(CTLESC, expdest); \ 882 STPUTC(*p++, expdest); \ 883 } \ 884 } else \ 885 while (*p) \ 886 STPUTC(*p++, expdest); \ 887 } while (0) 888 889 890 switch (*name) { 891 case '$': 892 num = rootpid; 893 goto numvar; 894 case '?': 895 num = exitstatus; 896 goto numvar; 897 case '#': 898 num = shellparam.nparam; 899 goto numvar; 900 case '!': 901 num = backgndpid; 902 numvar: 903 expdest = cvtnum(num, expdest); 904 break; 905 case '-': 906 for (i = 0; optlist[i].name; i++) { 907 if (optlist[i].val && optlist[i].letter) 908 STPUTC(optlist[i].letter, expdest); 909 } 910 break; 911 case '@': 912 if (flag & EXP_FULL && quoted) { 913 for (ap = shellparam.p ; (p = *ap++) != NULL ; ) { 914 STRTODEST(p); 915 if (*ap) 916 /* A NUL separates args inside "" */ 917 STPUTC('\0', expdest); 918 } 919 break; 920 } 921 /* fall through */ 922 case '*': 923 if (ifsset() != 0) 924 sep = ifsval()[0]; 925 else 926 sep = ' '; 927 for (ap = shellparam.p ; (p = *ap++) != NULL ; ) { 928 STRTODEST(p); 929 if (*ap && sep) 930 STPUTC(sep, expdest); 931 } 932 break; 933 case '0': 934 p = arg0; 935 STRTODEST(p); 936 break; 937 default: 938 if (is_digit(*name)) { 939 num = atoi(name); 940 if (num > 0 && num <= shellparam.nparam) { 941 p = shellparam.p[num - 1]; 942 STRTODEST(p); 943 } 944 } 945 break; 946 } 947 } 948 949 950 951 /* 952 * Record the fact that we have to scan this region of the 953 * string for IFS characters. 954 */ 955 956 STATIC void 957 recordregion(int start, int end, int inquotes) 958 { 959 struct ifsregion *ifsp; 960 961 if (ifslastp == NULL) { 962 ifsp = &ifsfirst; 963 } else { 964 if (ifslastp->endoff == start 965 && ifslastp->inquotes == inquotes) { 966 /* extend previous area */ 967 ifslastp->endoff = end; 968 return; 969 } 970 ifsp = (struct ifsregion *)ckmalloc(sizeof (struct ifsregion)); 971 ifslastp->next = ifsp; 972 } 973 ifslastp = ifsp; 974 ifslastp->next = NULL; 975 ifslastp->begoff = start; 976 ifslastp->endoff = end; 977 ifslastp->inquotes = inquotes; 978 } 979 980 981 982 /* 983 * Break the argument string into pieces based upon IFS and add the 984 * strings to the argument list. The regions of the string to be 985 * searched for IFS characters have been stored by recordregion. 986 */ 987 STATIC void 988 ifsbreakup(char *string, struct arglist *arglist) 989 { 990 struct ifsregion *ifsp; 991 struct strlist *sp; 992 char *start; 993 char *p; 994 char *q; 995 const char *ifs; 996 const char *ifsspc; 997 int had_param_ch = 0; 998 999 start = string; 1000 1001 if (ifslastp == NULL) { 1002 /* Return entire argument, IFS doesn't apply to any of it */ 1003 sp = (struct strlist *)stalloc(sizeof *sp); 1004 sp->text = start; 1005 *arglist->lastp = sp; 1006 arglist->lastp = &sp->next; 1007 return; 1008 } 1009 1010 ifs = ifsset() ? ifsval() : " \t\n"; 1011 1012 for (ifsp = &ifsfirst; ifsp != NULL; ifsp = ifsp->next) { 1013 p = string + ifsp->begoff; 1014 while (p < string + ifsp->endoff) { 1015 had_param_ch = 1; 1016 q = p; 1017 if (*p == CTLESC) 1018 p++; 1019 if (ifsp->inquotes) { 1020 /* Only NULs (should be from "$@") end args */ 1021 if (*p != 0) { 1022 p++; 1023 continue; 1024 } 1025 ifsspc = NULL; 1026 } else { 1027 if (!strchr(ifs, *p)) { 1028 p++; 1029 continue; 1030 } 1031 had_param_ch = 0; 1032 ifsspc = strchr(" \t\n", *p); 1033 1034 /* Ignore IFS whitespace at start */ 1035 if (q == start && ifsspc != NULL) { 1036 p++; 1037 start = p; 1038 continue; 1039 } 1040 } 1041 1042 /* Save this argument... */ 1043 *q = '\0'; 1044 sp = (struct strlist *)stalloc(sizeof *sp); 1045 sp->text = start; 1046 *arglist->lastp = sp; 1047 arglist->lastp = &sp->next; 1048 p++; 1049 1050 if (ifsspc != NULL) { 1051 /* Ignore further trailing IFS whitespace */ 1052 for (; p < string + ifsp->endoff; p++) { 1053 q = p; 1054 if (*p == CTLESC) 1055 p++; 1056 if (strchr(ifs, *p) == NULL) { 1057 p = q; 1058 break; 1059 } 1060 if (strchr(" \t\n", *p) == NULL) { 1061 p++; 1062 break; 1063 } 1064 } 1065 } 1066 start = p; 1067 } 1068 } 1069 1070 /* 1071 * Save anything left as an argument. 1072 * Traditionally we have treated 'IFS=':'; set -- x$IFS' as 1073 * generating 2 arguments, the second of which is empty. 1074 * Some recent clarification of the Posix spec say that it 1075 * should only generate one.... 1076 */ 1077 if (had_param_ch || *start != 0) { 1078 sp = (struct strlist *)stalloc(sizeof *sp); 1079 sp->text = start; 1080 *arglist->lastp = sp; 1081 arglist->lastp = &sp->next; 1082 } 1083 } 1084 1085 STATIC void 1086 ifsfree(void) 1087 { 1088 while (ifsfirst.next != NULL) { 1089 struct ifsregion *ifsp; 1090 INTOFF; 1091 ifsp = ifsfirst.next->next; 1092 ckfree(ifsfirst.next); 1093 ifsfirst.next = ifsp; 1094 INTON; 1095 } 1096 ifslastp = NULL; 1097 ifsfirst.next = NULL; 1098 } 1099 1100 1101 1102 /* 1103 * Expand shell metacharacters. At this point, the only control characters 1104 * should be escapes. The results are stored in the list exparg. 1105 */ 1106 1107 char *expdir; 1108 1109 1110 STATIC void 1111 expandmeta(struct strlist *str, int flag) 1112 { 1113 char *p; 1114 struct strlist **savelastp; 1115 struct strlist *sp; 1116 char c; 1117 /* TODO - EXP_REDIR */ 1118 1119 while (str) { 1120 if (fflag) 1121 goto nometa; 1122 p = str->text; 1123 for (;;) { /* fast check for meta chars */ 1124 if ((c = *p++) == '\0') 1125 goto nometa; 1126 if (c == '*' || c == '?' || c == '[' || c == '!') 1127 break; 1128 } 1129 savelastp = exparg.lastp; 1130 INTOFF; 1131 if (expdir == NULL) { 1132 int i = strlen(str->text); 1133 expdir = ckmalloc(i < 2048 ? 2048 : i); /* XXX */ 1134 } 1135 1136 expmeta(expdir, str->text); 1137 ckfree(expdir); 1138 expdir = NULL; 1139 INTON; 1140 if (exparg.lastp == savelastp) { 1141 /* 1142 * no matches 1143 */ 1144 nometa: 1145 *exparg.lastp = str; 1146 rmescapes(str->text); 1147 exparg.lastp = &str->next; 1148 } else { 1149 *exparg.lastp = NULL; 1150 *savelastp = sp = expsort(*savelastp); 1151 while (sp->next != NULL) 1152 sp = sp->next; 1153 exparg.lastp = &sp->next; 1154 } 1155 str = str->next; 1156 } 1157 } 1158 1159 1160 /* 1161 * Do metacharacter (i.e. *, ?, [...]) expansion. 1162 */ 1163 1164 STATIC void 1165 expmeta(char *enddir, char *name) 1166 { 1167 char *p; 1168 const char *cp; 1169 char *q; 1170 char *start; 1171 char *endname; 1172 int metaflag; 1173 struct stat statb; 1174 DIR *dirp; 1175 struct dirent *dp; 1176 int atend; 1177 int matchdot; 1178 1179 metaflag = 0; 1180 start = name; 1181 for (p = name ; ; p++) { 1182 if (*p == '*' || *p == '?') 1183 metaflag = 1; 1184 else if (*p == '[') { 1185 q = p + 1; 1186 if (*q == '!') 1187 q++; 1188 for (;;) { 1189 while (*q == CTLQUOTEMARK) 1190 q++; 1191 if (*q == CTLESC) 1192 q++; 1193 if (*q == '/' || *q == '\0') 1194 break; 1195 if (*++q == ']') { 1196 metaflag = 1; 1197 break; 1198 } 1199 } 1200 } else if (*p == '!' && p[1] == '!' && (p == name || p[-1] == '/')) { 1201 metaflag = 1; 1202 } else if (*p == '\0') 1203 break; 1204 else if (*p == CTLQUOTEMARK) 1205 continue; 1206 else if (*p == CTLESC) 1207 p++; 1208 if (*p == '/') { 1209 if (metaflag) 1210 break; 1211 start = p + 1; 1212 } 1213 } 1214 if (metaflag == 0) { /* we've reached the end of the file name */ 1215 if (enddir != expdir) 1216 metaflag++; 1217 for (p = name ; ; p++) { 1218 if (*p == CTLQUOTEMARK) 1219 continue; 1220 if (*p == CTLESC) 1221 p++; 1222 *enddir++ = *p; 1223 if (*p == '\0') 1224 break; 1225 } 1226 if (metaflag == 0 || lstat(expdir, &statb) >= 0) 1227 addfname(expdir); 1228 return; 1229 } 1230 endname = p; 1231 if (start != name) { 1232 p = name; 1233 while (p < start) { 1234 while (*p == CTLQUOTEMARK) 1235 p++; 1236 if (*p == CTLESC) 1237 p++; 1238 *enddir++ = *p++; 1239 } 1240 } 1241 if (enddir == expdir) { 1242 cp = "."; 1243 } else if (enddir == expdir + 1 && *expdir == '/') { 1244 cp = "/"; 1245 } else { 1246 cp = expdir; 1247 enddir[-1] = '\0'; 1248 } 1249 if ((dirp = opendir(cp)) == NULL) 1250 return; 1251 if (enddir != expdir) 1252 enddir[-1] = '/'; 1253 if (*endname == 0) { 1254 atend = 1; 1255 } else { 1256 atend = 0; 1257 *endname++ = '\0'; 1258 } 1259 matchdot = 0; 1260 p = start; 1261 while (*p == CTLQUOTEMARK) 1262 p++; 1263 if (*p == CTLESC) 1264 p++; 1265 if (*p == '.') 1266 matchdot++; 1267 while (! int_pending() && (dp = readdir(dirp)) != NULL) { 1268 if (dp->d_name[0] == '.' && ! matchdot) 1269 continue; 1270 if (patmatch(start, dp->d_name, 0)) { 1271 if (atend) { 1272 scopy(dp->d_name, enddir); 1273 addfname(expdir); 1274 } else { 1275 for (p = enddir, cp = dp->d_name; 1276 (*p++ = *cp++) != '\0';) 1277 continue; 1278 p[-1] = '/'; 1279 expmeta(p, endname); 1280 } 1281 } 1282 } 1283 closedir(dirp); 1284 if (! atend) 1285 endname[-1] = '/'; 1286 } 1287 1288 1289 /* 1290 * Add a file name to the list. 1291 */ 1292 1293 STATIC void 1294 addfname(char *name) 1295 { 1296 char *p; 1297 struct strlist *sp; 1298 1299 p = stalloc(strlen(name) + 1); 1300 scopy(name, p); 1301 sp = (struct strlist *)stalloc(sizeof *sp); 1302 sp->text = p; 1303 *exparg.lastp = sp; 1304 exparg.lastp = &sp->next; 1305 } 1306 1307 1308 /* 1309 * Sort the results of file name expansion. It calculates the number of 1310 * strings to sort and then calls msort (short for merge sort) to do the 1311 * work. 1312 */ 1313 1314 STATIC struct strlist * 1315 expsort(struct strlist *str) 1316 { 1317 int len; 1318 struct strlist *sp; 1319 1320 len = 0; 1321 for (sp = str ; sp ; sp = sp->next) 1322 len++; 1323 return msort(str, len); 1324 } 1325 1326 1327 STATIC struct strlist * 1328 msort(struct strlist *list, int len) 1329 { 1330 struct strlist *p, *q = NULL; 1331 struct strlist **lpp; 1332 int half; 1333 int n; 1334 1335 if (len <= 1) 1336 return list; 1337 half = len >> 1; 1338 p = list; 1339 for (n = half ; --n >= 0 ; ) { 1340 q = p; 1341 p = p->next; 1342 } 1343 q->next = NULL; /* terminate first half of list */ 1344 q = msort(list, half); /* sort first half of list */ 1345 p = msort(p, len - half); /* sort second half */ 1346 lpp = &list; 1347 for (;;) { 1348 if (strcmp(p->text, q->text) < 0) { 1349 *lpp = p; 1350 lpp = &p->next; 1351 if ((p = *lpp) == NULL) { 1352 *lpp = q; 1353 break; 1354 } 1355 } else { 1356 *lpp = q; 1357 lpp = &q->next; 1358 if ((q = *lpp) == NULL) { 1359 *lpp = p; 1360 break; 1361 } 1362 } 1363 } 1364 return list; 1365 } 1366 1367 1368 1369 /* 1370 * Returns true if the pattern matches the string. 1371 */ 1372 1373 int 1374 patmatch(char *pattern, char *string, int squoted) 1375 { 1376 #ifdef notdef 1377 if (pattern[0] == '!' && pattern[1] == '!') 1378 return 1 - pmatch(pattern + 2, string); 1379 else 1380 #endif 1381 return pmatch(pattern, string, squoted); 1382 } 1383 1384 1385 STATIC int 1386 pmatch(char *pattern, char *string, int squoted) 1387 { 1388 char *p, *q; 1389 char c; 1390 1391 p = pattern; 1392 q = string; 1393 for (;;) { 1394 switch (c = *p++) { 1395 case '\0': 1396 goto breakloop; 1397 case CTLESC: 1398 if (squoted && *q == CTLESC) 1399 q++; 1400 if (*q++ != *p++) 1401 return 0; 1402 break; 1403 case CTLQUOTEMARK: 1404 continue; 1405 case '?': 1406 if (squoted && *q == CTLESC) 1407 q++; 1408 if (*q++ == '\0') 1409 return 0; 1410 break; 1411 case '*': 1412 c = *p; 1413 while (c == CTLQUOTEMARK || c == '*') 1414 c = *++p; 1415 if (c != CTLESC && c != CTLQUOTEMARK && 1416 c != '?' && c != '*' && c != '[') { 1417 while (*q != c) { 1418 if (squoted && *q == CTLESC && 1419 q[1] == c) 1420 break; 1421 if (*q == '\0') 1422 return 0; 1423 if (squoted && *q == CTLESC) 1424 q++; 1425 q++; 1426 } 1427 } 1428 do { 1429 if (pmatch(p, q, squoted)) 1430 return 1; 1431 if (squoted && *q == CTLESC) 1432 q++; 1433 } while (*q++ != '\0'); 1434 return 0; 1435 case '[': { 1436 char *endp; 1437 int invert, found; 1438 char chr; 1439 1440 endp = p; 1441 if (*endp == '!') 1442 endp++; 1443 for (;;) { 1444 while (*endp == CTLQUOTEMARK) 1445 endp++; 1446 if (*endp == '\0') 1447 goto dft; /* no matching ] */ 1448 if (*endp == CTLESC) 1449 endp++; 1450 if (*++endp == ']') 1451 break; 1452 } 1453 invert = 0; 1454 if (*p == '!') { 1455 invert++; 1456 p++; 1457 } 1458 found = 0; 1459 chr = *q++; 1460 if (squoted && chr == CTLESC) 1461 chr = *q++; 1462 if (chr == '\0') 1463 return 0; 1464 c = *p++; 1465 do { 1466 if (c == CTLQUOTEMARK) 1467 continue; 1468 if (c == CTLESC) 1469 c = *p++; 1470 if (*p == '-' && p[1] != ']') { 1471 p++; 1472 while (*p == CTLQUOTEMARK) 1473 p++; 1474 if (*p == CTLESC) 1475 p++; 1476 if (chr >= c && chr <= *p) 1477 found = 1; 1478 p++; 1479 } else { 1480 if (chr == c) 1481 found = 1; 1482 } 1483 } while ((c = *p++) != ']'); 1484 if (found == invert) 1485 return 0; 1486 break; 1487 } 1488 dft: default: 1489 if (squoted && *q == CTLESC) 1490 q++; 1491 if (*q++ != c) 1492 return 0; 1493 break; 1494 } 1495 } 1496 breakloop: 1497 if (*q != '\0') 1498 return 0; 1499 return 1; 1500 } 1501 1502 1503 1504 /* 1505 * Remove any CTLESC characters from a string. 1506 */ 1507 1508 void 1509 rmescapes(char *str) 1510 { 1511 char *p, *q; 1512 1513 p = str; 1514 while (*p != CTLESC && *p != CTLQUOTEMARK) { 1515 if (*p++ == '\0') 1516 return; 1517 } 1518 q = p; 1519 while (*p) { 1520 if (*p == CTLQUOTEMARK) { 1521 p++; 1522 continue; 1523 } 1524 if (*p == CTLESC) 1525 p++; 1526 *q++ = *p++; 1527 } 1528 *q = '\0'; 1529 } 1530 1531 1532 1533 /* 1534 * See if a pattern matches in a case statement. 1535 */ 1536 1537 int 1538 casematch(union node *pattern, char *val) 1539 { 1540 struct stackmark smark; 1541 int result; 1542 char *p; 1543 1544 setstackmark(&smark); 1545 argbackq = pattern->narg.backquote; 1546 STARTSTACKSTR(expdest); 1547 ifslastp = NULL; 1548 argstr(pattern->narg.text, EXP_TILDE | EXP_CASE); 1549 STPUTC('\0', expdest); 1550 p = grabstackstr(expdest); 1551 result = patmatch(p, val, 0); 1552 popstackmark(&smark); 1553 return result; 1554 } 1555 1556 /* 1557 * Our own itoa(). 1558 */ 1559 1560 STATIC char * 1561 cvtnum(int num, char *buf) 1562 { 1563 char temp[32]; 1564 int neg = num < 0; 1565 char *p = temp + 31; 1566 1567 temp[31] = '\0'; 1568 1569 do { 1570 *--p = num % 10 + '0'; 1571 } while ((num /= 10) != 0); 1572 1573 if (neg) 1574 *--p = '-'; 1575 1576 while (*p) 1577 STPUTC(*p++, buf); 1578 return buf; 1579 } 1580 1581 /* 1582 * Do most of the work for wordexp(3). 1583 */ 1584 1585 int 1586 wordexpcmd(int argc, char **argv) 1587 { 1588 size_t len; 1589 int i; 1590 1591 out1fmt("%d", argc - 1); 1592 out1c('\0'); 1593 for (i = 1, len = 0; i < argc; i++) 1594 len += strlen(argv[i]); 1595 out1fmt("%zu", len); 1596 out1c('\0'); 1597 for (i = 1; i < argc; i++) { 1598 out1str(argv[i]); 1599 out1c('\0'); 1600 } 1601 return (0); 1602 } 1603