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