1 /* $NetBSD: expand.c,v 1.146 2024/10/21 15:57:45 kre 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.146 2024/10/21 15:57:45 kre 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 #include <wctype.h> 55 #include <wchar.h> 56 57 /* 58 * Routines to expand arguments to commands. We have to deal with 59 * backquotes, shell variables, and file metacharacters. 60 */ 61 62 #include "shell.h" 63 #include "main.h" 64 #include "nodes.h" 65 #include "eval.h" 66 #include "expand.h" 67 #include "syntax.h" 68 #include "arithmetic.h" 69 #include "parser.h" 70 #include "jobs.h" 71 #include "options.h" 72 #include "builtins.h" 73 #include "var.h" 74 #include "input.h" 75 #include "output.h" 76 #include "memalloc.h" 77 #include "error.h" 78 #include "mystring.h" 79 #include "show.h" 80 81 /* 82 * Structure specifying which parts of the string should be searched 83 * for IFS characters. 84 */ 85 86 struct ifsregion { 87 struct ifsregion *next; /* next region in list */ 88 int begoff; /* offset of start of region */ 89 int endoff; /* offset of end of region */ 90 int inquotes; /* search for nul bytes only */ 91 }; 92 93 94 char *expdest; /* output of current string */ 95 struct nodelist *argbackq; /* list of back quote expressions */ 96 struct ifsregion ifsfirst; /* first struct in list of ifs regions */ 97 struct ifsregion *ifslastp; /* last struct in list */ 98 struct arglist exparg; /* holds expanded arg list */ 99 100 static int empty_dollar_at; /* have expanded "$@" to nothing */ 101 102 STATIC const char *argstr(const char *, int); 103 STATIC const char *exptilde(const char *, int); 104 STATIC void expbackq(union node *, int, int); 105 STATIC const char *expari(const char *); 106 STATIC int subevalvar(const char *, const char *, int, int, int); 107 STATIC int subevalvar_trim(const char *, int, int, int, int, int); 108 STATIC const char *evalvar(const char *, int); 109 STATIC int varisset(const char *, int); 110 STATIC void varvalue(const char *, int, int, int); 111 STATIC void recordregion(int, int, int); 112 STATIC void removerecordregions(int); 113 STATIC void ifsbreakup(char *, struct arglist *); 114 STATIC void ifsfree(void); 115 STATIC void expandmeta(struct strlist *, int); 116 STATIC void expmeta(char *, char *); 117 STATIC void addfname(char *); 118 STATIC struct strlist *expsort(struct strlist *); 119 STATIC struct strlist *msort(struct strlist *, int); 120 STATIC int patmatch(const char *, const char *, int); 121 STATIC char *cvtnum(int, char *); 122 static int collate_range_cmp(wchar_t, wchar_t); 123 STATIC void add_args(struct strlist *); 124 STATIC void rmescapes_nl(char *); 125 126 #ifdef DEBUG 127 #define NULLTERM_4_TRACE(p) STACKSTRNUL(p) 128 #else 129 #define NULLTERM_4_TRACE(p) do { /* nothing */ } while (0) 130 #endif 131 132 #define IS_BORING(_ch) \ 133 ((_ch) == CTLQUOTEMARK || (_ch) == CTLQUOTEEND || (_ch) == CTLNONL) 134 #define SKIP_BORING(p) \ 135 do { \ 136 char _ch; \ 137 \ 138 while ((_ch = *(p)), IS_BORING(_ch)) \ 139 (p)++; \ 140 } while (0) 141 142 /* 143 * Expand shell variables and backquotes inside a here document. 144 */ 145 146 char * 147 expandhere(union node *arg) 148 { 149 int len; 150 151 VTRACE(DBG_EXPAND|DBG_REDIR, ("expandhere(%p)\n", arg)); 152 expandarg(arg, NULL, 0); 153 len = rmescapes(stackblock()); 154 VTRACE(DBG_EXPAND|DBG_REDIR, ("expandhere() -> %d\n", len)); 155 return stalloc(len + 1); /* include the \0 */ 156 } 157 158 159 static int 160 collate_range_cmp(wchar_t c1, wchar_t c2) 161 { 162 wchar_t s1[2], s2[2]; 163 164 s1[0] = c1; 165 s1[1] = L'\0'; 166 s2[0] = c2; 167 s2[1] = L'\0'; 168 return (wcscoll(s1, s2)); 169 } 170 171 /* 172 * Perform variable substitution and command substitution on an argument, 173 * placing the resulting list of arguments in arglist. If EXP_FULL is true, 174 * perform splitting and file name expansion. When arglist is NULL, perform 175 * here document expansion. 176 */ 177 178 void 179 expandarg(union node *arg, struct arglist *arglist, int flag) 180 { 181 struct strlist *sp; 182 char *p; 183 184 CTRACE(DBG_EXPAND, ("expandarg(fl=%#x)\n", flag)); 185 if (fflag) /* no filename expandsion */ 186 flag &= ~EXP_GLOB; 187 188 empty_dollar_at = 0; 189 argbackq = arg->narg.backquote; 190 STARTSTACKSTR(expdest); 191 ifsfirst.next = NULL; 192 ifslastp = NULL; 193 line_number = arg->narg.lineno; 194 argstr(arg->narg.text, flag); 195 if (arglist == NULL) { 196 STACKSTRNUL(expdest); 197 CTRACE(DBG_EXPAND, 198 ("expandarg: no arglist, done[%d] (len %d) \"%s\"\n", 199 back_exitstatus, expdest - stackblock(), stackblock())); 200 return; /* here document expanded */ 201 } 202 STPUTC('\0', expdest); 203 CTRACE(DBG_EXPAND, ("expandarg: arglist got (%d) \"%s\"\n", 204 expdest - stackblock() - 1, stackblock())); 205 p = grabstackstr(expdest); 206 exparg.lastp = &exparg.list; 207 /* 208 * TODO - EXP_REDIR 209 */ 210 if (flag & EXP_SPLIT) { 211 ifsbreakup(p, &exparg); 212 *exparg.lastp = NULL; 213 exparg.lastp = &exparg.list; 214 if (flag & EXP_GLOB) 215 expandmeta(exparg.list, flag); 216 else 217 add_args(exparg.list); 218 #if 0 219 } else if (flag & EXP_REDIR) { 220 /* if EXP_REDIR ever happens, it happens here */ 221 /* for now just (below) remove escapes, and leave it alone */ 222 #endif 223 } else { 224 rmescapes(p); /* we might have escaped CTL bytes to remove */ 225 sp = stalloc(sizeof(*sp)); 226 sp->text = p; 227 *exparg.lastp = sp; 228 exparg.lastp = &sp->next; 229 } 230 ifsfree(); 231 *exparg.lastp = NULL; 232 if (exparg.list) { 233 *arglist->lastp = exparg.list; 234 arglist->lastp = exparg.lastp; 235 } 236 } 237 238 239 240 /* 241 * Perform variable and command substitution. 242 * If EXP_GLOB is set, output CTLESC characters to allow for further processing. 243 * If EXP_SPLIT is set, remember location of result for later, 244 * Otherwise treat $@ like $* since no splitting will be performed. 245 */ 246 247 STATIC const char * 248 argstr(const char *p, int flag) 249 { 250 char c; 251 const int quotes = flag & EXP_QNEEDED; /* do CTLESC */ 252 int firsteq = 1; 253 int had_dol_at = 0; 254 int startoff; 255 const char *ifs = NULL; 256 int ifs_split = EXP_IFS_SPLIT; 257 258 if (flag & EXP_IFS_SPLIT) 259 ifs = ifsval(); 260 261 CTRACE(DBG_EXPAND, ("argstr(\"%s\", %#x) quotes=%#x\n", p,flag,quotes)); 262 263 startoff = expdest - stackblock(); 264 if (*p == '~' && (flag & (EXP_TILDE | EXP_VARTILDE))) 265 p = exptilde(p, flag); 266 for (;;) { 267 switch (c = *p++) { 268 case '\0': 269 NULLTERM_4_TRACE(expdest); 270 VTRACE(DBG_EXPAND, ("argstr returning at \"\" " 271 "added \"%s\" to expdest\n", stackblock())); 272 return p - 1; 273 case CTLENDVAR: /* end of expanding yyy in ${xxx-yyy} */ 274 case CTLENDARI: /* end of a $(( )) string */ 275 if (had_dol_at && *p == CTLQUOTEEND) 276 p++; 277 NULLTERM_4_TRACE(expdest); 278 VTRACE(DBG_EXPAND, ("argstr returning at \"%.6s\"..." 279 " after %2.2X; added \"%s\" to expdest\n", 280 p, (c & 0xff), stackblock())); 281 return p; 282 case CTLQUOTEMARK: 283 /* "$@" syntax adherence hack */ 284 if (p[0] == CTLVAR && p[1] & VSQUOTE && 285 p[2] == '@' && p[3] == '=') { 286 had_dol_at = 1; 287 break; 288 } 289 had_dol_at = 0; 290 empty_dollar_at = 0; 291 if ((flag & EXP_SPLIT) != 0) 292 STPUTC(c, expdest); 293 ifs_split = 0; 294 break; 295 case CTLNONL: 296 if (flag & EXP_NL) 297 STPUTC(c, expdest); 298 line_number++; 299 break; 300 case CTLCNL: 301 STPUTC('\n', expdest); /* no line_number++ */ 302 break; 303 case CTLQUOTEEND: 304 if (empty_dollar_at && 305 expdest - stackblock() > startoff && 306 expdest[-1] == CTLQUOTEMARK) 307 expdest--; 308 else if (!had_dol_at && (flag & EXP_SPLIT) != 0) 309 STPUTC(c, expdest); 310 ifs_split = EXP_IFS_SPLIT; 311 had_dol_at = 0; 312 break; 313 case CTLESC: 314 if (quotes || ISCTL(*p)) 315 STPUTC(c, expdest); 316 c = *p++; 317 STPUTC(c, expdest); 318 if (c == '\n') /* should not happen, but ... */ 319 line_number++; 320 break; 321 case CTLVAR: { 322 #ifdef DEBUG 323 unsigned int pos = expdest - stackblock(); 324 NULLTERM_4_TRACE(expdest); 325 #endif 326 p = evalvar(p, (flag & ~EXP_IFS_SPLIT) | (flag & ifs_split)); 327 NULLTERM_4_TRACE(expdest); 328 VTRACE(DBG_EXPAND, ("argstr evalvar " 329 "added %zd \"%s\" to expdest\n", 330 (size_t)(expdest - (stackblock() + pos)), 331 stackblock() + pos)); 332 break; 333 } 334 case CTLBACKQ: 335 case CTLBACKQ|CTLQUOTE: { 336 #ifdef DEBUG 337 unsigned int pos = expdest - stackblock(); 338 #endif 339 expbackq(argbackq->n, c & CTLQUOTE, flag); 340 argbackq = argbackq->next; 341 NULLTERM_4_TRACE(expdest); 342 VTRACE(DBG_EXPAND, ("argstr expbackq added \"%s\" " 343 "to expdest\n", stackblock() + pos)); 344 break; 345 } 346 case CTLARI: { 347 #ifdef DEBUG 348 unsigned int pos = expdest - stackblock(); 349 #endif 350 p = expari(p); 351 NULLTERM_4_TRACE(expdest); 352 VTRACE(DBG_EXPAND, ("argstr expari " 353 "+ \"%s\" to expdest p=\"%.5s...\"\n", 354 stackblock() + pos, p)); 355 break; 356 } 357 case ':': 358 case '=': 359 /* 360 * sort of a hack - expand tildes in variable 361 * assignments (after the first '=' and after ':'s). 362 */ 363 STPUTC(c, expdest); 364 if (flag & EXP_VARTILDE && *p == '~') { 365 if (c == '=') { 366 if (firsteq) 367 firsteq = 0; 368 else 369 break; 370 } 371 p = exptilde(p, flag); 372 } 373 break; 374 default: 375 if (c == '\n') 376 line_number++; 377 STPUTC(c, expdest); 378 if (flag & ifs_split && strchr(ifs, c) != NULL) { 379 /* We need to get the output split here... */ 380 recordregion(expdest - stackblock() - 1, 381 expdest - stackblock(), 0); 382 } 383 break; 384 } 385 } 386 } 387 388 STATIC const char * 389 exptilde(const char *p, int flag) 390 { 391 char c, last; 392 const char *startp = p; 393 struct passwd *pw; 394 const char *home; 395 const int quotes = flag & EXP_QNEEDED; 396 char *user; 397 struct stackmark smark; 398 #ifdef DEBUG 399 unsigned int offs = expdest - stackblock(); 400 #endif 401 402 setstackmark(&smark); 403 (void) grabstackstr(expdest); 404 user = stackblock(); /* we will just borrow top of stack */ 405 406 while ((c = *++p) != '\0') { 407 switch(c) { 408 case CTLESC: /* any of these occurring */ 409 case CTLVAR: /* means ~ expansion */ 410 case CTLBACKQ: /* does not happen at all */ 411 case CTLBACKQ | CTLQUOTE: 412 case CTLARI: /* just leave original unchanged */ 413 case CTLENDARI: 414 case CTLQUOTEMARK: 415 case '\n': 416 popstackmark(&smark); 417 return (startp); 418 case CTLNONL: 419 continue; 420 case ':': 421 if (!posix || flag & EXP_VARTILDE) 422 goto done; 423 break; 424 case CTLENDVAR: 425 case '/': 426 goto done; 427 } 428 STPUTC(c, user); 429 } 430 done: 431 STACKSTRNUL(user); 432 user = stackblock(); /* to start of collected username */ 433 434 CTRACE(DBG_EXPAND, ("exptilde, found \"~%s\"", user)); 435 if (*user == '\0') { 436 home = lookupvar("HOME"); 437 /* 438 * if HOME is unset, results are unspecified... 439 * we used to just leave the ~ unchanged, but 440 * (some) other shells do ... and this seems more useful. 441 */ 442 if (home == NULL && (pw = getpwuid(getuid())) != NULL) 443 home = pw->pw_dir; 444 } else if ((pw = getpwnam(user)) == NULL) { 445 /* 446 * If user does not exist, results are undefined. 447 * so we can abort() here if we want, but let's not! 448 */ 449 home = NULL; 450 } else 451 home = pw->pw_dir; 452 453 VTRACE(DBG_EXPAND, (" ->\"%s\"", home ? home : "<<NULL>>")); 454 popstackmark(&smark); /* now expdest is valid again */ 455 456 /* 457 * Posix XCU 2.6.1: The value of $HOME (for ~) or the initial 458 * working directory from getpwnam() for ~user 459 * Nothing there about "except if a null string". So do what it wants. 460 * In later drafts (to become Issue 8), it is even required that in 461 * this case, (where HOME='') a bare ~ expands to "" (which must not 462 * be reduced to nothing). 463 */ 464 last = '\0'; /* just in case *home == '\0' (already) */ 465 if (home == NULL) { 466 CTRACE(DBG_EXPAND, (": returning unused \"%s\"\n", startp)); 467 return startp; 468 } 469 while ((c = *home++) != '\0') { 470 if ((quotes && NEEDESC(c)) || ISCTL(c)) 471 STPUTC(CTLESC, expdest); 472 STPUTC(c, expdest); 473 last = c; 474 } 475 476 /* 477 * If HOME (or whatver) ended in a '/' (last == '/'), and 478 * the ~prefix was terminated by a '/', then only keep one 479 * of them - since we already took the one from HOME, just 480 * skip over the one that ended the tilde prefix. 481 * 482 * Current (Issue 8) drafts say this is permitted, and recommend 483 * it - a later version of the standard will probably require it. 484 * This is to prevent ~/foo generating //foo when HOME=/ (and 485 * other cases like it, but that's the important one). 486 */ 487 if (last == '/' && *p == '/') 488 p++; 489 490 CTRACE(DBG_EXPAND, (": added %d \"%.*s\" returning \"%s\"\n", 491 expdest - stackblock() - offs, expdest - stackblock() - offs, 492 stackblock() + offs, p)); 493 494 return (p); 495 } 496 497 498 STATIC void 499 removerecordregions(int endoff) 500 { 501 502 VTRACE(DBG_EXPAND, ("removerecordregions(%d):", endoff)); 503 if (ifslastp == NULL) { 504 VTRACE(DBG_EXPAND, (" none\n", endoff)); 505 return; 506 } 507 508 if (ifsfirst.endoff > endoff) { 509 VTRACE(DBG_EXPAND, (" first(%d)", ifsfirst.endoff)); 510 while (ifsfirst.next != NULL) { 511 struct ifsregion *ifsp; 512 INTOFF; 513 ifsp = ifsfirst.next->next; 514 ckfree(ifsfirst.next); 515 ifsfirst.next = ifsp; 516 INTON; 517 } 518 if (ifsfirst.begoff > endoff) 519 ifslastp = NULL; 520 else { 521 VTRACE(DBG_EXPAND,("->(%d,%d)",ifsfirst.begoff,endoff)); 522 ifslastp = &ifsfirst; 523 ifsfirst.endoff = endoff; 524 } 525 VTRACE(DBG_EXPAND, ("\n")); 526 return; 527 } 528 529 ifslastp = &ifsfirst; 530 while (ifslastp->next && ifslastp->next->begoff < endoff) 531 ifslastp=ifslastp->next; 532 VTRACE(DBG_EXPAND, (" found(%d,%d)", ifslastp->begoff,ifslastp->endoff)); 533 while (ifslastp->next != NULL) { 534 struct ifsregion *ifsp; 535 INTOFF; 536 ifsp = ifslastp->next->next; 537 ckfree(ifslastp->next); 538 ifslastp->next = ifsp; 539 INTON; 540 } 541 if (ifslastp->endoff > endoff) 542 ifslastp->endoff = endoff; 543 VTRACE(DBG_EXPAND, ("->(%d,%d)", ifslastp->begoff,ifslastp->endoff)); 544 } 545 546 547 /* 548 * Expand arithmetic expression. 549 * 550 * In this incarnation, we start at the beginning (yes, "Let's start at the 551 * very beginning. A very good place to start.") and collect the expression 552 * until the end - which means expanding anything contained within. 553 * 554 * Fortunately, argstr() just happens to do that for us... 555 */ 556 STATIC const char * 557 expari(const char *p) 558 { 559 char *q, *start; 560 intmax_t result; 561 int adjustment; 562 int begoff; 563 int quoted; 564 struct stackmark smark; 565 566 /* ifsfree(); */ 567 568 /* 569 * SPACE_NEEDED is enough for all possible digits (rounded up) 570 * plus possible "-", and the terminating '\0', hence, plus 2 571 * 572 * The calculation produces the number of bytes needed to 573 * represent the biggest possible value, in octal. We only 574 * generate decimal, which takes (often) less digits (never more) 575 * so this is safe, if occasionally slightly wasteful. 576 */ 577 #define SPACE_NEEDED ((int)((sizeof(intmax_t) * CHAR_BIT + 2) / 3 + 2)) 578 579 quoted = *p++ == '"'; 580 begoff = expdest - stackblock(); 581 VTRACE(DBG_EXPAND, ("expari%s: \"%s\" begoff %d\n", 582 quoted ? "(quoted)" : "", p, begoff)); 583 584 p = argstr(p, EXP_NL); /* expand $(( )) string */ 585 STPUTC('\0', expdest); 586 start = stackblock() + begoff; 587 588 removerecordregions(begoff); /* nothing there is kept */ 589 rmescapes_nl(start); /* convert CRTNONL back into \n's */ 590 591 setstackmark(&smark); 592 q = grabstackstr(expdest); /* keep the expression while eval'ing */ 593 result = arith(start, line_number); 594 popstackmark(&smark); /* return the stack to before grab */ 595 596 start = stackblock() + begoff; /* block may have moved */ 597 adjustment = expdest - start; 598 STADJUST(-adjustment, expdest); /* remove the argstr() result */ 599 600 CHECKSTRSPACE(SPACE_NEEDED, expdest); /* nb: stack block might move */ 601 fmtstr(expdest, SPACE_NEEDED, "%"PRIdMAX, result); 602 603 for (q = expdest; *q++ != '\0'; ) /* find end of what we added */ 604 ; 605 606 if (quoted == 0) /* allow weird splitting */ 607 recordregion(begoff, begoff + q - 1 - expdest, 0); 608 adjustment = q - expdest - 1; 609 STADJUST(adjustment, expdest); /* move expdest to end */ 610 VTRACE(DBG_EXPAND, ("expari: adding %d \"%s\", returning \"%.5s...\"\n", 611 adjustment, stackblock() + begoff, p)); 612 613 return p; 614 } 615 616 617 /* 618 * Expand stuff in backwards quotes (these days, any command substitution). 619 */ 620 621 STATIC void 622 expbackq(union node *cmd, int quoted, int flag) 623 { 624 struct backcmd in; 625 int i; 626 char buf[128]; 627 char *p; 628 char *dest = expdest; /* expdest may be reused by eval, use an alt */ 629 struct ifsregion saveifs, *savelastp; 630 struct nodelist *saveargbackq; 631 char lastc; 632 int startloc = dest - stackblock(); 633 int saveherefd; 634 const int quotes = flag & EXP_QNEEDED; 635 int nnl; 636 struct stackmark smark; 637 638 VTRACE(DBG_EXPAND, ("expbackq( ..., q=%d flag=%#x) have %d\n", 639 quoted, flag, startloc)); 640 INTOFF; 641 saveifs = ifsfirst; 642 savelastp = ifslastp; 643 saveargbackq = argbackq; 644 saveherefd = herefd; 645 herefd = -1; 646 647 setstackmark(&smark); /* preserve the stack */ 648 p = grabstackstr(dest); /* save what we have there currently */ 649 evalbackcmd(cmd, &in); /* evaluate the $( ) tree (using stack) */ 650 popstackmark(&smark); /* and return stack to when we entered */ 651 652 ifsfirst = saveifs; 653 ifslastp = savelastp; 654 argbackq = saveargbackq; 655 herefd = saveherefd; 656 657 p = in.buf; /* now extract the results */ 658 nnl = 0; /* dropping trailing \n's */ 659 for (;;) { 660 if (--in.nleft < 0) { 661 if (in.fd < 0) 662 break; 663 INTON; 664 while ((i = read(in.fd, buf, sizeof buf)) < 0 && errno == EINTR) 665 continue; 666 INTOFF; 667 VTRACE(DBG_EXPAND, ("expbackq: read returns %d\n", i)); 668 if (i <= 0) 669 break; 670 p = buf; 671 in.nleft = i - 1; 672 } 673 lastc = *p++; 674 if (lastc != '\0') { 675 if (lastc == '\n') /* don't save \n yet */ 676 nnl++; /* it might be trailing */ 677 else { 678 /* 679 * We have something other than \n 680 * 681 * Before saving it, we need to insert 682 * any \n's that we have just skipped. 683 */ 684 685 /* XXX 686 * this hack is just because our 687 * CHECKSTRSPACE() is lazy, and only 688 * ever grows the stack once, even 689 * if that does not allocate the space 690 * we requested. ie: safe for small 691 * requests, but not large ones. 692 * FIXME someday... 693 */ 694 if (nnl < 20) { 695 CHECKSTRSPACE(nnl + 2, dest); 696 while (nnl > 0) { 697 nnl--; 698 USTPUTC('\n', dest); 699 } 700 } else { 701 /* The slower, safer, way */ 702 while (nnl > 0) { 703 nnl--; 704 STPUTC('\n', dest); 705 } 706 CHECKSTRSPACE(2, dest); 707 } 708 if ((quotes && quoted && NEEDESC(lastc)) || 709 ISCTL(lastc)) 710 USTPUTC(CTLESC, dest); 711 USTPUTC(lastc, dest); 712 } 713 } 714 } 715 716 if (in.fd >= 0) 717 close(in.fd); 718 if (in.buf) 719 ckfree(in.buf); 720 if (in.jp) 721 back_exitstatus = waitforjob(in.jp); 722 if (quoted == 0) 723 recordregion(startloc, dest - stackblock(), 0); 724 CTRACE(DBG_EXPAND, ("evalbackq: [%d] size=%d: \"%.*s\"\n", 725 back_exitstatus, 726 (int)((dest - stackblock()) - startloc), 727 (int)((dest - stackblock()) - startloc), 728 stackblock() + startloc)); 729 730 expdest = dest; /* all done, expdest is all ours again */ 731 INTON; 732 } 733 734 735 STATIC int 736 subevalvar(const char *p, const char *str, int subtype, int startloc, 737 int varflags) 738 { 739 char *startp; 740 int saveherefd = herefd; 741 struct nodelist *saveargbackq = argbackq; 742 int amount; 743 744 herefd = -1; 745 VTRACE(DBG_EXPAND, ("subevalvar(%d) \"%.20s\" ${%.*s} sloc=%d vf=%x\n", 746 subtype, p, p-str, str, startloc, varflags)); 747 argstr(p, subtype == VSASSIGN ? EXP_VARTILDE : EXP_TILDE); 748 STACKSTRNUL(expdest); 749 herefd = saveherefd; 750 argbackq = saveargbackq; 751 startp = stackblock() + startloc; 752 753 switch (subtype) { 754 case VSASSIGN: 755 setvar(str, startp, 0); 756 amount = startp - expdest; /* remove what argstr added */ 757 STADJUST(amount, expdest); 758 varflags &= ~VSNUL; /*XXX Huh? What's that achieve? */ 759 return 1; /* go back and eval var again */ 760 761 case VSQUESTION: 762 if (*p != CTLENDVAR) { 763 outfmt(&errout, "%s\n", startp); 764 error(NULL); 765 } 766 error("%.*s: parameter %snot set", 767 (int)(p - str - 1), 768 str, (varflags & VSNUL) ? "null or " 769 : nullstr); 770 /* NOTREACHED */ 771 772 default: 773 abort(); 774 } 775 } 776 777 STATIC int 778 subevalvar_trim(const char *p, int strloc, int subtype, int startloc, 779 int varflags, int quotes) 780 { 781 char *startp; 782 char *str; 783 char *loc = NULL; 784 char *q; 785 int c = 0; 786 int saveherefd = herefd; 787 struct nodelist *saveargbackq = argbackq; 788 int amount; 789 790 herefd = -1; 791 switch (subtype) { 792 case VSTRIMLEFT: 793 case VSTRIMLEFTMAX: 794 case VSTRIMRIGHT: 795 case VSTRIMRIGHTMAX: 796 break; 797 default: 798 abort(); 799 break; 800 } 801 802 VTRACE(DBG_EXPAND, 803 ("subevalvar_trim(\"%.9s\", STR@%d, SUBT=%d, start@%d, vf=%x, q=%x)\n", 804 p, strloc, subtype, startloc, varflags, quotes)); 805 806 argstr(p, (varflags & (VSQUOTE|VSPATQ)) == VSQUOTE ? 0 : EXP_CASE); 807 STACKSTRNUL(expdest); 808 herefd = saveherefd; 809 argbackq = saveargbackq; 810 startp = stackblock() + startloc; 811 str = stackblock() + strloc; 812 813 switch (subtype) { 814 815 case VSTRIMLEFT: 816 for (loc = startp; loc < str; loc++) { 817 c = *loc; 818 *loc = '\0'; 819 if (patmatch(str, startp, quotes)) 820 goto recordleft; 821 *loc = c; 822 if (quotes && *loc == CTLESC) 823 loc++; 824 } 825 return 0; 826 827 case VSTRIMLEFTMAX: 828 for (loc = str - 1; loc >= startp;) { 829 c = *loc; 830 *loc = '\0'; 831 if (patmatch(str, startp, quotes)) 832 goto recordleft; 833 *loc = c; 834 loc--; 835 if (quotes && loc > startp && 836 *(loc - 1) == CTLESC) { 837 for (q = startp; q < loc; q++) 838 if (*q == CTLESC) 839 q++; 840 if (q > loc) 841 loc--; 842 } 843 } 844 return 0; 845 846 case VSTRIMRIGHT: 847 for (loc = str - 1; loc >= startp;) { 848 if (patmatch(str, loc, quotes)) 849 goto recordright; 850 loc--; 851 if (quotes && loc > startp && 852 *(loc - 1) == CTLESC) { 853 for (q = startp; q < loc; q++) 854 if (*q == CTLESC) 855 q++; 856 if (q > loc) 857 loc--; 858 } 859 } 860 return 0; 861 862 case VSTRIMRIGHTMAX: 863 for (loc = startp; loc < str - 1; loc++) { 864 if (patmatch(str, loc, quotes)) 865 goto recordright; 866 if (quotes && *loc == CTLESC) 867 loc++; 868 } 869 return 0; 870 871 default: 872 abort(); 873 } 874 875 recordleft: 876 *loc = c; 877 amount = ((str - 1) - (loc - startp)) - expdest; 878 STADJUST(amount, expdest); 879 while (loc != str - 1) 880 *startp++ = *loc++; 881 return 1; 882 883 recordright: 884 amount = loc - expdest; 885 STADJUST(amount, expdest); 886 STPUTC('\0', expdest); 887 STADJUST(-1, expdest); 888 return 1; 889 } 890 891 892 /* 893 * Expand a variable, and return a pointer to the next character in the 894 * input string. 895 */ 896 897 STATIC const char * 898 evalvar(const char *p, int flag) 899 { 900 int subtype; 901 int varflags; 902 const char *var; 903 char *val; 904 int patloc; 905 int c; 906 int set; 907 int special; 908 int startloc; 909 int varlen; 910 int apply_ifs; 911 const int quotes = flag & EXP_QNEEDED; 912 913 varflags = (unsigned char)*p++; 914 subtype = varflags & VSTYPE; 915 var = p; 916 special = subtype != VSUNKNOWN && !is_name(*p); 917 p = strchr(p, '=') + 1; 918 919 CTRACE(DBG_EXPAND, 920 ("evalvar \"%.*s\", flag=%#X quotes=%#X vf=%#X subtype=%X\n", 921 (int)(p - var - 1), var, flag, quotes, varflags, subtype)); 922 923 again: /* jump here after setting a variable with ${var=text} */ 924 if (varflags & VSLINENO) { 925 if (line_num.flags & VUNSET) { 926 set = 0; 927 val = NULL; 928 } else { 929 set = 1; 930 special = p - var; 931 val = NULL; 932 } 933 } else if (special) { 934 set = varisset(var, varflags & VSNUL); 935 val = NULL; 936 if (!set && *var == '@') 937 empty_dollar_at = 1; 938 } else { 939 val = lookupvar(var); 940 if (val == NULL || ((varflags & VSNUL) && val[0] == '\0')) { 941 val = NULL; 942 set = 0; 943 } else 944 set = 1; 945 } 946 947 varlen = 0; 948 startloc = expdest - stackblock(); 949 950 if (!set && uflag && *var != '@' && *var != '*') { 951 switch (subtype) { 952 case VSNORMAL: 953 case VSTRIMLEFT: 954 case VSTRIMLEFTMAX: 955 case VSTRIMRIGHT: 956 case VSTRIMRIGHTMAX: 957 case VSLENGTH: 958 error("%.*s: parameter not set", 959 (int)(p - var - 1), var); 960 /* NOTREACHED */ 961 } 962 } 963 964 #if 0 /* no longer need this $@ evil ... */ 965 if (!set && subtype != VSPLUS && special && *var == '@') 966 if (startloc > 0 && expdest[-1] == CTLQUOTEMARK) 967 expdest--, startloc--; 968 #endif 969 970 if (set && subtype != VSPLUS) { 971 /* insert the value of the variable */ 972 if (special) { 973 if (varflags & VSLINENO) { 974 /* 975 * The LINENO hack (expansion part) 976 */ 977 while (--special > 0) { 978 /* not needed, it is a number... 979 if (quotes && NEEDESC(*var)) 980 STPUTC(CTLESC, expdest); 981 */ 982 STPUTC(*var++, expdest); 983 } 984 } else 985 varvalue(var, varflags&VSQUOTE, subtype, flag); 986 if (subtype == VSLENGTH) { 987 varlen = expdest - stackblock() - startloc; 988 STADJUST(-varlen, expdest); 989 } 990 } else { 991 992 if (subtype == VSLENGTH) { 993 for (; *val; val++) 994 varlen++; 995 } else if (quotes && varflags & VSQUOTE) { 996 /* 997 * If we are going to look for magic in the 998 * value (quotes is set) and the expansion 999 * occurs inside "" (VSQUOTE) then any char 1000 * that has any potential special meaning 1001 * needs to have that meaning suppressed, 1002 * so supply a CTLESC prefix for it. 1003 */ 1004 for (; (c = *val) != '\0'; val++) { 1005 if (NEEDESC(c)) 1006 STPUTC(CTLESC, expdest); 1007 STPUTC(c, expdest); 1008 } 1009 } else { 1010 /* 1011 * We are going to rmescapes() later, 1012 * so make sure that any data char that 1013 * might be mistaken for one of our CTLxxx 1014 * magic chars is protected ... always. 1015 */ 1016 for (; (c = *val) != '\0'; val++) { 1017 if (ISCTL(c)) 1018 STPUTC(CTLESC, expdest); 1019 STPUTC(c, expdest); 1020 } 1021 } 1022 } 1023 } 1024 1025 1026 if (varflags & VSQUOTE) { 1027 if (*var == '@' && shellparam.nparam != 1) 1028 apply_ifs = 1; 1029 else { 1030 /* 1031 * Mark so that we don't apply IFS if we recurse through 1032 * here expanding $bar from "${foo-$bar}". 1033 */ 1034 flag |= EXP_IN_QUOTES; 1035 apply_ifs = 0; 1036 } 1037 } else if (flag & EXP_IN_QUOTES) { 1038 apply_ifs = 0; 1039 } else 1040 apply_ifs = 1; 1041 1042 switch (subtype) { 1043 case VSLENGTH: 1044 expdest = cvtnum(varlen, expdest); 1045 break; 1046 1047 case VSNORMAL: 1048 break; 1049 1050 case VSPLUS: 1051 set = !set; 1052 /* FALLTHROUGH */ 1053 case VSMINUS: 1054 if (!set) { 1055 argstr(p, flag | (apply_ifs ? EXP_IFS_SPLIT : 0)); 1056 /* 1057 * ${x-a b c} doesn't get split, but removing the 1058 * 'apply_ifs = 0' apparently breaks ${1+"$@"}.. 1059 * ${x-'a b' c} should generate 2 args. 1060 */ 1061 if (*p != CTLENDVAR) 1062 /* We should have marked stuff already */ 1063 apply_ifs = 0; 1064 } 1065 break; 1066 1067 case VSTRIMLEFT: 1068 case VSTRIMLEFTMAX: 1069 case VSTRIMRIGHT: 1070 case VSTRIMRIGHTMAX: 1071 if (!set) { 1072 set = 1; /* allow argbackq to be advanced if needed */ 1073 break; 1074 } 1075 /* 1076 * Terminate the string and start recording the pattern 1077 * right after it 1078 */ 1079 STPUTC('\0', expdest); 1080 patloc = expdest - stackblock(); 1081 if (subevalvar_trim(p, patloc, subtype, startloc, varflags, 1082 quotes) == 0) { 1083 int amount = (expdest - stackblock() - patloc) + 1; 1084 STADJUST(-amount, expdest); 1085 } 1086 /* Remove any recorded regions beyond start of variable */ 1087 removerecordregions(startloc); 1088 apply_ifs = 1; 1089 break; 1090 1091 case VSASSIGN: 1092 case VSQUESTION: 1093 if (set) 1094 break; 1095 if (subevalvar(p, var, subtype, startloc, varflags)) { 1096 /* if subevalvar() returns, it always returns 1 */ 1097 1098 varflags &= ~VSNUL; 1099 /* 1100 * Remove any recorded regions beyond 1101 * start of variable 1102 */ 1103 removerecordregions(startloc); 1104 goto again; 1105 } 1106 apply_ifs = 0; /* never executed */ 1107 break; 1108 1109 case VSUNKNOWN: 1110 VTRACE(DBG_EXPAND, 1111 ("evalvar \"%.*s\", unknown [%p %p] \"%.3s\" (%#2x %#2x)\n", 1112 (int)(p - var - 1), var, var, p, p, p[0] & 0xFF, p[1] & 0xFF)); 1113 1114 if ((p - var) <= 1) 1115 error("%d: unknown expansion type", line_number); 1116 else { 1117 if (*p == '#') /* only VSUNKNOWN as a ${#var:...} */ 1118 error("%d: ${#%.*s%c..}: unknown modifier", 1119 line_number, (int)(p - var - 1), 1120 var, p[1]&0xFF); 1121 1122 if (*p == CTLESC) 1123 p++; 1124 error("%d: ${%.*s%c..}: unknown modifier", 1125 line_number, (int)(p - var - 1), var, (*p & 0xFF)); 1126 } 1127 /* NOTREACHED */ 1128 1129 default: 1130 abort(); 1131 } 1132 1133 if (apply_ifs) 1134 recordregion(startloc, expdest - stackblock(), 1135 varflags & VSQUOTE); 1136 1137 if (subtype != VSNORMAL) { /* skip to end of alternative */ 1138 int nesting = 1; 1139 for (;;) { 1140 if ((c = *p++) == CTLESC) 1141 p++; 1142 else if (c == CTLNONL) 1143 ; 1144 else if (c == CTLBACKQ || c == (CTLBACKQ|CTLQUOTE)) { 1145 if (set) 1146 argbackq = argbackq->next; 1147 } else if (c == CTLVAR) { 1148 if ((*p++ & VSTYPE) != VSNORMAL) 1149 nesting++; 1150 } else if (c == CTLENDVAR) { 1151 if (--nesting == 0) 1152 break; 1153 } 1154 } 1155 } 1156 return p; 1157 } 1158 1159 1160 1161 /* 1162 * Test whether a special parameter is set. 1163 */ 1164 1165 STATIC int 1166 varisset(const char *name, int nulok) 1167 { 1168 if (*name == '!') 1169 return backgndpid != -1; 1170 else if (*name == '@' || *name == '*') { 1171 if (*shellparam.p == NULL) 1172 return 0; 1173 1174 if (nulok) { 1175 char **av; 1176 1177 for (av = shellparam.p; *av; av++) 1178 if (**av != '\0') 1179 return 1; 1180 return 0; 1181 } 1182 } else if (is_digit(*name)) { 1183 char *ap; 1184 long num; 1185 1186 /* 1187 * handle overflow sensibly (the *ap tests should never fail) 1188 */ 1189 errno = 0; 1190 num = strtol(name, &ap, 10); 1191 if (errno != 0 || (*ap != '\0' && *ap != '=')) 1192 return 0; 1193 1194 if (num == 0) 1195 ap = arg0; 1196 else if (num > shellparam.nparam) 1197 return 0; 1198 else 1199 ap = shellparam.p[num - 1]; 1200 1201 if (nulok && (ap == NULL || *ap == '\0')) 1202 return 0; 1203 } 1204 return 1; 1205 } 1206 1207 1208 1209 /* 1210 * Add the value of a specialized variable to the stack string. 1211 */ 1212 1213 STATIC void 1214 varvalue(const char *name, int quoted, int subtype, int flag) 1215 { 1216 int num; 1217 char *p; 1218 int i; 1219 int sep; 1220 char **ap; 1221 #ifdef DEBUG 1222 char *start = expdest; 1223 #endif 1224 1225 VTRACE(DBG_EXPAND, ("varvalue(%c%s, sub=%d, fl=%#x)", *name, 1226 quoted ? ", quoted" : "", subtype, flag)); 1227 1228 if (subtype == VSLENGTH) /* no magic required ... */ 1229 flag &= ~(EXP_FULL | EXP_QNEEDED); 1230 1231 #define STRTODEST(p) \ 1232 do {\ 1233 if ((flag & EXP_QNEEDED) && quoted) { \ 1234 while (*p) { \ 1235 if (NEEDESC(*p)) \ 1236 STPUTC(CTLESC, expdest); \ 1237 STPUTC(*p++, expdest); \ 1238 } \ 1239 } else \ 1240 while (*p) { \ 1241 if ((flag & EXP_QNEEDED) && ISCTL(*p)) \ 1242 STPUTC(CTLESC, expdest); \ 1243 STPUTC(*p++, expdest); \ 1244 } \ 1245 } while (0) 1246 1247 1248 switch (*name) { 1249 case '$': 1250 num = rootpid; 1251 break; 1252 case '?': 1253 num = exitstatus; 1254 break; 1255 case '#': 1256 num = shellparam.nparam; 1257 break; 1258 case '!': 1259 num = backgndpid; 1260 break; 1261 case '-': 1262 for (i = 0; i < option_flags; i++) { 1263 if (optlist[optorder[i]].val) 1264 STPUTC(optlist[optorder[i]].letter, expdest); 1265 } 1266 VTRACE(DBG_EXPAND, (": %.*s\n", expdest-start, start)); 1267 return; 1268 case '@': 1269 if (flag & EXP_SPLIT && quoted) { 1270 VTRACE(DBG_EXPAND, (": $@ split (%d)\n", 1271 shellparam.nparam)); 1272 #if 0 1273 /* GROSS HACK */ 1274 if (shellparam.nparam == 0 && 1275 expdest[-1] == CTLQUOTEMARK) 1276 expdest--; 1277 /* KCAH SSORG */ 1278 #endif 1279 if (shellparam.nparam == 0) 1280 empty_dollar_at = 1; 1281 1282 for (ap = shellparam.p ; (p = *ap++) != NULL ; ) { 1283 if (*p == '\0') { 1284 /* retain an explicit null string */ 1285 STPUTC(CTLQUOTEMARK, expdest); 1286 STPUTC(CTLQUOTEEND, expdest); 1287 } else 1288 STRTODEST(p); 1289 if (*ap) 1290 /* A NUL separates args inside "" */ 1291 STPUTC('\0', expdest); 1292 } 1293 return; 1294 } 1295 /* fall through */ 1296 case '*': 1297 sep = ifsval()[0]; 1298 for (ap = shellparam.p ; (p = *ap++) != NULL ; ) { 1299 STRTODEST(p); 1300 if (!*ap) 1301 break; 1302 if (sep) { 1303 if (quoted && (flag & EXP_QNEEDED) && 1304 NEEDESC(sep)) 1305 STPUTC(CTLESC, expdest); 1306 STPUTC(sep, expdest); 1307 } else 1308 if ((flag & (EXP_SPLIT|EXP_IN_QUOTES)) == EXP_SPLIT 1309 && !quoted && **ap != '\0') 1310 STPUTC('\0', expdest); 1311 } 1312 VTRACE(DBG_EXPAND, (": %.*s\n", expdest-start, start)); 1313 return; 1314 default: 1315 if (is_digit(*name)) { 1316 long lnum; 1317 1318 errno = 0; 1319 lnum = strtol(name, &p, 10); 1320 if (errno != 0 || (*p != '\0' && *p != '=')) 1321 return; 1322 1323 if (lnum == 0) 1324 p = arg0; 1325 else if (lnum > 0 && lnum <= shellparam.nparam) 1326 p = shellparam.p[lnum - 1]; 1327 else 1328 return; 1329 STRTODEST(p); 1330 } 1331 VTRACE(DBG_EXPAND, (": %.*s\n", expdest-start, start)); 1332 return; 1333 } 1334 /* 1335 * only the specials with an int value arrive here 1336 */ 1337 VTRACE(DBG_EXPAND, ("(%d)", num)); 1338 expdest = cvtnum(num, expdest); 1339 VTRACE(DBG_EXPAND, (": %.*s\n", expdest-start, start)); 1340 } 1341 1342 1343 1344 /* 1345 * Record the fact that we have to scan this region of the 1346 * string for IFS characters. 1347 */ 1348 1349 STATIC void 1350 recordregion(int start, int end, int inquotes) 1351 { 1352 struct ifsregion *ifsp; 1353 1354 VTRACE(DBG_EXPAND, ("recordregion(%d,%d,%d)\n", start, end, inquotes)); 1355 if (ifslastp == NULL) { 1356 ifsp = &ifsfirst; 1357 } else { 1358 if (ifslastp->endoff == start 1359 && ifslastp->inquotes == inquotes) { 1360 /* extend previous area */ 1361 ifslastp->endoff = end; 1362 return; 1363 } 1364 ifsp = (struct ifsregion *)ckmalloc(sizeof (struct ifsregion)); 1365 ifslastp->next = ifsp; 1366 } 1367 ifslastp = ifsp; 1368 ifslastp->next = NULL; 1369 ifslastp->begoff = start; 1370 ifslastp->endoff = end; 1371 ifslastp->inquotes = inquotes; 1372 } 1373 1374 1375 1376 /* 1377 * Break the argument string into pieces based upon IFS and add the 1378 * strings to the argument list. The regions of the string to be 1379 * searched for IFS characters have been stored by recordregion. 1380 */ 1381 STATIC void 1382 ifsbreakup(char *string, struct arglist *arglist) 1383 { 1384 struct ifsregion *ifsp; 1385 struct strlist *sp; 1386 char *start; 1387 char *p; 1388 char *q; 1389 const char *ifs; 1390 const char *ifsspc; 1391 int had_param_ch = 0; 1392 1393 start = string; 1394 1395 VTRACE(DBG_EXPAND, ("ifsbreakup(\"%s\")", string)); /* misses \0's */ 1396 if (ifslastp == NULL) { 1397 /* Return entire argument, IFS doesn't apply to any of it */ 1398 VTRACE(DBG_EXPAND, ("no regions\n", string)); 1399 sp = stalloc(sizeof(*sp)); 1400 sp->text = start; 1401 *arglist->lastp = sp; 1402 arglist->lastp = &sp->next; 1403 return; 1404 } 1405 1406 ifs = ifsval(); 1407 1408 for (ifsp = &ifsfirst; ifsp != NULL; ifsp = ifsp->next) { 1409 p = string + ifsp->begoff; 1410 VTRACE(DBG_EXPAND, (" !%.*s!(%d)", ifsp->endoff-ifsp->begoff, 1411 p, ifsp->endoff-ifsp->begoff)); 1412 while (p < string + ifsp->endoff) { 1413 had_param_ch = 1; 1414 q = p; 1415 if (IS_BORING(*p)) { 1416 p++; 1417 continue; 1418 } 1419 if (*p == CTLESC) 1420 p++; 1421 if (ifsp->inquotes) { 1422 /* Only NULs (should be from "$@") end args */ 1423 if (*p != 0) { 1424 p++; 1425 continue; 1426 } 1427 ifsspc = NULL; 1428 VTRACE(DBG_EXPAND, (" \\0 nxt:\"%s\" ", p)); 1429 } else { 1430 if (!strchr(ifs, *p)) { 1431 p++; 1432 continue; 1433 } 1434 had_param_ch = 0; 1435 ifsspc = strchr(" \t\n", *p); 1436 1437 /* Ignore IFS whitespace at start */ 1438 if (q == start && ifsspc != NULL) { 1439 p++; 1440 start = p; 1441 continue; 1442 } 1443 } 1444 1445 /* Save this argument... */ 1446 *q = '\0'; 1447 VTRACE(DBG_EXPAND, ("<%s>", start)); 1448 sp = stalloc(sizeof(*sp)); 1449 sp->text = start; 1450 *arglist->lastp = sp; 1451 arglist->lastp = &sp->next; 1452 p++; 1453 1454 if (ifsspc != NULL) { 1455 /* Ignore further trailing IFS whitespace */ 1456 for (; p < string + ifsp->endoff; p++) { 1457 q = p; 1458 if (*p == CTLNONL) 1459 continue; 1460 if (*p == CTLESC) 1461 p++; 1462 if (strchr(ifs, *p) == NULL) { 1463 p = q; 1464 break; 1465 } 1466 if (strchr(" \t\n", *p) == NULL) { 1467 p++; 1468 break; 1469 } 1470 } 1471 } 1472 start = p; 1473 } 1474 } 1475 1476 /* 1477 while (*start == CTLQUOTEEND) 1478 start++; 1479 */ 1480 1481 /* 1482 * Save anything left as an argument. 1483 * Traditionally we have treated 'IFS=':'; set -- x$IFS' as 1484 * generating 2 arguments, the second of which is empty. 1485 * Some recent clarification of the Posix spec say that it 1486 * should only generate one.... 1487 */ 1488 if (had_param_ch || *start != 0) { 1489 VTRACE(DBG_EXPAND, (" T<%s>", start)); 1490 sp = stalloc(sizeof(*sp)); 1491 sp->text = start; 1492 *arglist->lastp = sp; 1493 arglist->lastp = &sp->next; 1494 } 1495 VTRACE(DBG_EXPAND, ("\n")); 1496 } 1497 1498 STATIC void 1499 ifsfree(void) 1500 { 1501 while (ifsfirst.next != NULL) { 1502 struct ifsregion *ifsp; 1503 INTOFF; 1504 ifsp = ifsfirst.next->next; 1505 ckfree(ifsfirst.next); 1506 ifsfirst.next = ifsp; 1507 INTON; 1508 } 1509 ifslastp = NULL; 1510 ifsfirst.next = NULL; 1511 } 1512 1513 1514 1515 /* 1516 * Expand shell metacharacters. At this point, the only control characters 1517 * should be escapes. The results are stored in the list exparg. 1518 */ 1519 1520 char *expdir; 1521 1522 1523 STATIC void 1524 expandmeta(struct strlist *str, int flag) 1525 { 1526 char *p; 1527 struct strlist **savelastp; 1528 struct strlist *sp; 1529 char c; 1530 /* TODO - EXP_REDIR */ 1531 1532 while (str) { 1533 p = str->text; 1534 for (;;) { /* fast check for meta chars */ 1535 if ((c = *p++) == '\0') 1536 goto nometa; 1537 if (c == '*' || c == '?' || c == '[' /* || c == '!' */) 1538 break; 1539 } 1540 savelastp = exparg.lastp; 1541 INTOFF; 1542 if (expdir == NULL) { 1543 int i = strlen(str->text); 1544 expdir = ckmalloc(i < 2048 ? 2048 : i); /* XXX */ 1545 } 1546 1547 expmeta(expdir, str->text); 1548 ckfree(expdir); 1549 expdir = NULL; 1550 INTON; 1551 if (exparg.lastp == savelastp) { 1552 /* 1553 * no matches 1554 */ 1555 nometa: 1556 *exparg.lastp = str; 1557 rmescapes(str->text); 1558 exparg.lastp = &str->next; 1559 } else { 1560 *exparg.lastp = NULL; 1561 *savelastp = sp = expsort(*savelastp); 1562 while (sp->next != NULL) 1563 sp = sp->next; 1564 exparg.lastp = &sp->next; 1565 } 1566 str = str->next; 1567 } 1568 } 1569 1570 STATIC void 1571 add_args(struct strlist *str) 1572 { 1573 while (str) { 1574 *exparg.lastp = str; 1575 rmescapes(str->text); 1576 exparg.lastp = &str->next; 1577 str = str->next; 1578 } 1579 } 1580 1581 1582 /* 1583 * Do metacharacter (i.e. *, ?, [...]) expansion. 1584 */ 1585 1586 STATIC void 1587 expmeta(char *enddir, char *name) 1588 { 1589 char *p; 1590 const char *cp; 1591 char *q; 1592 char *start; 1593 char *endname; 1594 int metaflag; 1595 struct stat statb; 1596 DIR *dirp; 1597 struct dirent *dp; 1598 int atend; 1599 int matchdot; 1600 1601 CTRACE(DBG_EXPAND|DBG_MATCH, ("expmeta(\"%s\")\n", name)); 1602 metaflag = 0; 1603 start = name; 1604 for (p = name ; ; p++) { 1605 if (*p == '*' || *p == '?') 1606 metaflag = 1; 1607 else if (*p == '[') { 1608 q = p + 1; 1609 if (*q == '!' || *q == '^') 1610 q++; 1611 for (;;) { 1612 while (IS_BORING(*q)) 1613 q++; 1614 if (*q == ']') { 1615 q++; 1616 metaflag = 1; 1617 break; 1618 } 1619 if (*q == '[' && q[1] == ':') { 1620 /* 1621 * character class, look for :] ending 1622 * also stop on ']' (end bracket expr) 1623 * or '\0' or '/' (end pattern) 1624 */ 1625 while (*++q != '\0' && *q != ']' && 1626 *q != '/') { 1627 if (*q == CTLESC) { 1628 if (*++q == '\0') 1629 break; 1630 if (*q == '/') 1631 break; 1632 } else if (*q == ':' && 1633 q[1] == ']') 1634 break; 1635 } 1636 if (*q == ':') { 1637 /* 1638 * stopped at ':]' 1639 * still in [...] 1640 * skip ":]" and continue; 1641 */ 1642 q += 2; 1643 continue; 1644 } 1645 1646 /* done at end of pattern, not [...] */ 1647 if (*q == '\0' || *q == '/') 1648 break; 1649 1650 /* found the ']', we have a [...] */ 1651 metaflag = 1; 1652 q++; /* skip ']' */ 1653 break; 1654 } 1655 if (*q == CTLESC) 1656 q++; 1657 /* end of pattern cannot be escaped */ 1658 if (*q == '/' || *q == '\0') 1659 break; 1660 q++; 1661 } 1662 } else if (*p == '\0') 1663 break; 1664 else if (IS_BORING(*p)) 1665 continue; 1666 else if (*p == CTLESC) 1667 p++; 1668 if (*p == '/') { 1669 if (metaflag) 1670 break; 1671 start = p + 1; 1672 } 1673 } 1674 if (metaflag == 0) { /* we've reached the end of the file name */ 1675 if (enddir != expdir) 1676 metaflag++; 1677 for (p = name ; ; p++) { 1678 if (IS_BORING(*p)) 1679 continue; 1680 if (*p == CTLESC) 1681 p++; 1682 *enddir++ = *p; 1683 if (*p == '\0') 1684 break; 1685 } 1686 if (metaflag == 0 || lstat(expdir, &statb) >= 0) 1687 addfname(expdir); 1688 return; 1689 } 1690 endname = p; 1691 if (start != name) { 1692 p = name; 1693 while (p < start) { 1694 while (IS_BORING(*p)) 1695 p++; 1696 if (*p == CTLESC) 1697 p++; 1698 *enddir++ = *p++; 1699 } 1700 } 1701 if (enddir == expdir) { 1702 cp = "."; 1703 } else if (enddir == expdir + 1 && *expdir == '/') { 1704 cp = "/"; 1705 } else { 1706 cp = expdir; 1707 enddir[-1] = '\0'; 1708 } 1709 if ((dirp = opendir(cp)) == NULL) 1710 return; 1711 if (enddir != expdir) 1712 enddir[-1] = '/'; 1713 if (*endname == 0) { 1714 atend = 1; 1715 } else { 1716 atend = 0; 1717 *endname++ = '\0'; 1718 } 1719 matchdot = 0; 1720 p = start; 1721 while (IS_BORING(*p)) 1722 p++; 1723 if (*p == CTLESC) 1724 p++; 1725 if (*p == '.') 1726 matchdot++; 1727 while (! int_pending() && (dp = readdir(dirp)) != NULL) { 1728 if (dp->d_name[0] == '.' && ! matchdot) 1729 continue; 1730 if (patmatch(start, dp->d_name, 0)) { 1731 if (atend) { 1732 scopy(dp->d_name, enddir); 1733 addfname(expdir); 1734 } else { 1735 for (p = enddir, cp = dp->d_name; 1736 (*p++ = *cp++) != '\0';) 1737 continue; 1738 p[-1] = '/'; 1739 expmeta(p, endname); 1740 } 1741 } 1742 } 1743 closedir(dirp); 1744 if (! atend) 1745 endname[-1] = '/'; 1746 } 1747 1748 1749 /* 1750 * Add a file name to the list. 1751 */ 1752 1753 STATIC void 1754 addfname(char *name) 1755 { 1756 char *p; 1757 struct strlist *sp; 1758 1759 p = stalloc(strlen(name) + 1); 1760 scopy(name, p); 1761 sp = stalloc(sizeof(*sp)); 1762 sp->text = p; 1763 *exparg.lastp = sp; 1764 exparg.lastp = &sp->next; 1765 } 1766 1767 1768 /* 1769 * Sort the results of file name expansion. It calculates the number of 1770 * strings to sort and then calls msort (short for merge sort) to do the 1771 * work. 1772 */ 1773 1774 STATIC struct strlist * 1775 expsort(struct strlist *str) 1776 { 1777 int len; 1778 struct strlist *sp; 1779 1780 len = 0; 1781 for (sp = str ; sp ; sp = sp->next) 1782 len++; 1783 return msort(str, len); 1784 } 1785 1786 1787 STATIC struct strlist * 1788 msort(struct strlist *list, int len) 1789 { 1790 struct strlist *p, *q = NULL; 1791 struct strlist **lpp; 1792 int half; 1793 int n; 1794 1795 if (len <= 1) 1796 return list; 1797 half = len >> 1; 1798 p = list; 1799 for (n = half ; --n >= 0 ; ) { 1800 q = p; 1801 p = p->next; 1802 } 1803 q->next = NULL; /* terminate first half of list */ 1804 q = msort(list, half); /* sort first half of list */ 1805 p = msort(p, len - half); /* sort second half */ 1806 lpp = &list; 1807 for (;;) { 1808 if (strcmp(p->text, q->text) < 0) { 1809 *lpp = p; 1810 lpp = &p->next; 1811 if ((p = *lpp) == NULL) { 1812 *lpp = q; 1813 break; 1814 } 1815 } else { 1816 *lpp = q; 1817 lpp = &q->next; 1818 if ((q = *lpp) == NULL) { 1819 *lpp = p; 1820 break; 1821 } 1822 } 1823 } 1824 return list; 1825 } 1826 1827 1828 /* 1829 * See if a character matches a character class, starting at the first colon 1830 * of "[:class:]". 1831 * If a valid character class is recognized, a pointer to the next character 1832 * after the final closing bracket is stored into *end, otherwise a null 1833 * pointer is stored into *end. 1834 */ 1835 static int 1836 match_charclass(const char *p, wchar_t chr, const char **end) 1837 { 1838 char name[20]; 1839 const char *nameend; 1840 wctype_t cclass; 1841 char *q; 1842 1843 *end = NULL; 1844 p++; 1845 q = &name[0]; 1846 nameend = strstr(p, ":]"); 1847 if (nameend == NULL || nameend == p) /* not a valid class */ 1848 return 0; 1849 1850 if (*p == CTLESC) { 1851 if (*++p == CTLESC) 1852 return 0; 1853 if (p == nameend) 1854 return 0; 1855 } 1856 if (!is_alpha(*p)) 1857 return 0; 1858 while (p < nameend) { 1859 if (*p == CTLESC) { 1860 p++; 1861 if (p == nameend) 1862 return 0; 1863 } 1864 if (!is_in_name(*p)) /* '_' is a local extension */ 1865 return 0; 1866 if (q < &name[sizeof name]) 1867 *q++ = *p++; 1868 else 1869 p++; 1870 } 1871 1872 *end = nameend + 2; /* committed to it being a char class */ 1873 1874 if (q < &name[sizeof name]) /* a usable name found */ 1875 *q++ = '\0'; 1876 else /* too long, valid, but no match */ 1877 return 0; 1878 1879 cclass = wctype(name); 1880 /* An unknown class matches nothing but is valid nevertheless. */ 1881 if (cclass == 0) 1882 return 0; 1883 return iswctype(chr, cclass); 1884 } 1885 1886 1887 /* 1888 * Returns true if the pattern matches the string. 1889 */ 1890 1891 STATIC int 1892 patmatch(const char *pattern, const char *string, int squoted) 1893 { 1894 const char *p, *q, *end; 1895 const char *bt_p, *bt_q; 1896 char c; 1897 wchar_t wc, wc2; 1898 1899 VTRACE(DBG_MATCH, ("patmatch(P=\"%s\", W=\"%s\"%s): ", 1900 pattern, string, squoted ? ", SQ" : "")); 1901 p = pattern; 1902 q = string; 1903 bt_p = NULL; 1904 bt_q = NULL; 1905 for (;;) { 1906 switch (c = *p++) { 1907 case '\0': 1908 if (squoted && *q == CTLESC) { 1909 if (q[1] == '\0') 1910 q++; 1911 } 1912 if (*q != '\0') 1913 goto backtrack; 1914 VTRACE(DBG_MATCH, ("match\n")); 1915 return 1; 1916 case CTLESC: 1917 if (squoted && *q == CTLESC) 1918 q++; 1919 if (*p == '\0' && *q == '\0') { 1920 VTRACE(DBG_MATCH, ("match-\\\n")); 1921 return 1; 1922 } 1923 if (*q++ != *p++) 1924 goto backtrack; 1925 break; 1926 case '\\': 1927 if (squoted && *q == CTLESC) 1928 q++; 1929 if (*q++ != *p++) 1930 goto backtrack; 1931 break; 1932 case CTLQUOTEMARK: 1933 case CTLQUOTEEND: 1934 case CTLNONL: 1935 continue; 1936 case '?': 1937 if (squoted && *q == CTLESC) 1938 q++; 1939 if (*q++ == '\0') { 1940 VTRACE(DBG_MATCH, ("?fail\n")); 1941 return 0; 1942 } 1943 break; 1944 case '*': 1945 c = *p; 1946 while (c == CTLQUOTEMARK || c == '*') 1947 c = *++p; 1948 if (c != CTLESC && !IS_BORING(c) && 1949 c != '?' && c != '*' && c != '[') { 1950 while (*q != c) { 1951 if (squoted && *q == CTLESC && 1952 q[1] == c) 1953 break; 1954 if (*q == '\0') { 1955 VTRACE(DBG_MATCH, ("*fail\n")); 1956 return 0; 1957 } 1958 if (squoted && *q == CTLESC) 1959 q++; 1960 q++; 1961 } 1962 } 1963 if (c == CTLESC && p[1] == '\0') { 1964 VTRACE(DBG_MATCH, ("match+\\\n")); 1965 return 1; 1966 } 1967 /* 1968 * First try the shortest match for the '*' that 1969 * could work. We can forget any earlier '*' since 1970 * there is no way having it match more characters 1971 * can help us, given that we are already here. 1972 */ 1973 bt_p = p; 1974 bt_q = q; 1975 break; 1976 case '[': { 1977 const char *savep, *saveq, *endp; 1978 int invert, found; 1979 unsigned char chr; 1980 1981 /* 1982 * First quick check to see if there is a 1983 * possible matching ']' - if not, then this 1984 * is not a char class, and the '[' is just 1985 * a literal '['. 1986 * 1987 * This check will not detect all non classes, but 1988 * that's OK - It just means that we execute the 1989 * harder code sometimes when it it cannot succeed. 1990 */ 1991 endp = p; 1992 if (*endp == '!' || *endp == '^') 1993 endp++; 1994 for (;;) { 1995 while (IS_BORING(*endp)) 1996 endp++; 1997 if (*endp == '\0') 1998 goto dft; /* no matching ] */ 1999 if (*endp++ == ']') 2000 break; 2001 } 2002 /* end shortcut */ 2003 2004 savep = p, saveq = q; 2005 invert = 0; 2006 if (*p == '!' || *p == '^') { 2007 invert++; 2008 p++; 2009 } 2010 found = 0; 2011 if (*q == '\0') { 2012 VTRACE(DBG_MATCH, ("[]fail\n")); 2013 return 0; 2014 } 2015 if (squoted && *q == CTLESC) 2016 q++; 2017 chr = (unsigned char)*q++; 2018 c = *p++; 2019 do { 2020 if (IS_BORING(c)) 2021 continue; 2022 if (c == '\0') { 2023 p = savep, q = saveq; 2024 c = '['; 2025 goto dft; 2026 } 2027 if (c == '[' && *p == ':') { 2028 found |= match_charclass(p, chr, &end); 2029 if (end != NULL) { 2030 p = end; 2031 continue; 2032 } 2033 } 2034 if (c == CTLESC || c == '\\') 2035 c = *p++; 2036 wc = (unsigned char)c; 2037 if (*p == '-' && p[1] != ']') { 2038 p++; 2039 if (*p == CTLESC || *p == '\\') 2040 p++; 2041 wc2 = (unsigned char)*p++; 2042 if ( collate_range_cmp(chr, wc) >= 0 2043 && collate_range_cmp(chr, wc2) <= 0 2044 ) 2045 found = 1; 2046 } else { 2047 if (chr == wc) 2048 found = 1; 2049 } 2050 } while ((c = *p++) != ']'); 2051 if (found == invert) 2052 goto backtrack; 2053 break; 2054 } 2055 dft: default: 2056 if (squoted && *q == CTLESC) 2057 q++; 2058 if (*q++ == c) 2059 break; 2060 backtrack: 2061 /* 2062 * If we have a mismatch (other than hitting the end 2063 * of the string), go back to the last '*' seen and 2064 * have it match one additional character. 2065 */ 2066 if (bt_p == NULL) { 2067 VTRACE(DBG_MATCH, ("BTP fail\n")); 2068 return 0; 2069 } 2070 if (*bt_q == '\0') { 2071 VTRACE(DBG_MATCH, ("BTQ fail\n")); 2072 return 0; 2073 } 2074 bt_q++; 2075 p = bt_p; 2076 q = bt_q; 2077 break; 2078 } 2079 } 2080 } 2081 2082 2083 2084 /* 2085 * Remove any CTLESC or CTLNONL characters from a string. 2086 * 2087 * String is modified in place, and we return the length of the result 2088 */ 2089 2090 int 2091 rmescapes(char *str) 2092 { 2093 char *p, *q; 2094 2095 p = str; 2096 while (!ISCTL(*p)) { 2097 if (*p++ == '\0') 2098 return ((int)(p - str) - 1); 2099 } 2100 q = p; 2101 while (*p) { 2102 if (IS_BORING(*p)) { 2103 p++; 2104 continue; 2105 } 2106 if (*p == CTLCNL) { 2107 p++; 2108 *q++ = '\n'; 2109 continue; 2110 } 2111 if (*p == CTLESC) 2112 p++; 2113 #ifdef DEBUG 2114 else if (ISCTL(*p)) 2115 abort(); 2116 #endif 2117 *q++ = *p++; 2118 } 2119 *q = '\0'; 2120 2121 return ((int)(q - str)); 2122 } 2123 2124 /* 2125 * and a special version for dealing with expressions to be parsed 2126 * by the arithmetic evaluator. That needs to be able to count \n's 2127 * even ones that were \newline elided \n's, so we have to put the 2128 * latter back into the string - just being careful to put them only 2129 * at a place where white space can reasonably occur in the string 2130 * -- then the \n we insert will just be white space, and ignored 2131 * for all purposes except line counting. 2132 */ 2133 2134 void 2135 rmescapes_nl(char *str) 2136 { 2137 char *p, *q; 2138 int nls = 0, holdnl = 0, holdlast; 2139 2140 p = str; 2141 while (!ISCTL(*p)) { 2142 if (*p++ == '\0') 2143 return; 2144 } 2145 if (p > str) /* must reprocess char before stopper (if any) */ 2146 --p; /* so we do not place a \n badly */ 2147 q = p; 2148 while (*p) { 2149 if (*p == CTLQUOTEMARK || *p == CTLQUOTEEND) { 2150 p++; 2151 continue; 2152 } 2153 if (*p == CTLNONL) { 2154 p++; 2155 nls++; 2156 continue; 2157 } 2158 if (*p == CTLCNL) { 2159 p++; 2160 *q++ = '\n'; 2161 continue; 2162 } 2163 if (*p == CTLESC) 2164 p++; 2165 #ifdef DEBUG 2166 else if (ISCTL(*p)) 2167 abort(); 2168 #endif 2169 2170 holdlast = holdnl; 2171 holdnl = is_in_name(*p); /* letters, digits, _ */ 2172 if (q == str || is_space(q[-1]) || (*p != '=' && q[-1] != *p)) { 2173 if (nls > 0 && holdnl != holdlast) { 2174 while (nls > 0) 2175 *q++ = '\n', nls--; 2176 } 2177 } 2178 *q++ = *p++; 2179 } 2180 while (--nls >= 0) 2181 *q++ = '\n'; 2182 *q = '\0'; 2183 } 2184 2185 2186 2187 /* 2188 * See if a pattern matches in a case statement. 2189 */ 2190 2191 int 2192 casematch(union node *pattern, char *val) 2193 { 2194 struct stackmark smark; 2195 int result; 2196 char *p; 2197 2198 CTRACE(DBG_MATCH, ("casematch(P=\"%s\", W=\"%s\")\n", 2199 pattern->narg.text, val)); 2200 setstackmark(&smark); 2201 argbackq = pattern->narg.backquote; 2202 STARTSTACKSTR(expdest); 2203 ifslastp = NULL; 2204 argstr(pattern->narg.text, EXP_TILDE | EXP_CASE); 2205 STPUTC('\0', expdest); 2206 p = grabstackstr(expdest); 2207 result = patmatch(p, val, 0); 2208 popstackmark(&smark); 2209 return result; 2210 } 2211 2212 /* 2213 * Our own itoa(). Assumes result buffer is on the stack 2214 */ 2215 2216 STATIC char * 2217 cvtnum(int num, char *buf) 2218 { 2219 char temp[32]; 2220 int neg = num < 0; 2221 char *p = temp + sizeof temp - 1; 2222 2223 if (neg) 2224 num = -num; 2225 2226 *p = '\0'; 2227 do { 2228 *--p = num % 10 + '0'; 2229 } while ((num /= 10) != 0 && p > temp + 1); 2230 2231 if (neg) 2232 *--p = '-'; 2233 2234 while (*p) 2235 STPUTC(*p++, buf); 2236 return buf; 2237 } 2238 2239 /* 2240 * Do most of the work for wordexp(3). 2241 */ 2242 2243 int 2244 wordexpcmd(int argc, char **argv) 2245 { 2246 size_t len; 2247 int i; 2248 2249 out1fmt("%d", argc - 1); 2250 out1c('\0'); 2251 for (i = 1, len = 0; i < argc; i++) 2252 len += strlen(argv[i]); 2253 out1fmt("%zu", len); 2254 out1c('\0'); 2255 for (i = 1; i < argc; i++) { 2256 out1str(argv[i]); 2257 out1c('\0'); 2258 } 2259 return (0); 2260 } 2261