1 /* $NetBSD: expand.c,v 1.144 2023/12/29 15:49:23 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.144 2023/12/29 15:49:23 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 = !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 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 default: 1110 abort(); 1111 } 1112 1113 if (apply_ifs) 1114 recordregion(startloc, expdest - stackblock(), 1115 varflags & VSQUOTE); 1116 1117 if (subtype != VSNORMAL) { /* skip to end of alternative */ 1118 int nesting = 1; 1119 for (;;) { 1120 if ((c = *p++) == CTLESC) 1121 p++; 1122 else if (c == CTLNONL) 1123 ; 1124 else if (c == CTLBACKQ || c == (CTLBACKQ|CTLQUOTE)) { 1125 if (set) 1126 argbackq = argbackq->next; 1127 } else if (c == CTLVAR) { 1128 if ((*p++ & VSTYPE) != VSNORMAL) 1129 nesting++; 1130 } else if (c == CTLENDVAR) { 1131 if (--nesting == 0) 1132 break; 1133 } 1134 } 1135 } 1136 return p; 1137 } 1138 1139 1140 1141 /* 1142 * Test whether a special parameter is set. 1143 */ 1144 1145 STATIC int 1146 varisset(const char *name, int nulok) 1147 { 1148 if (*name == '!') 1149 return backgndpid != -1; 1150 else if (*name == '@' || *name == '*') { 1151 if (*shellparam.p == NULL) 1152 return 0; 1153 1154 if (nulok) { 1155 char **av; 1156 1157 for (av = shellparam.p; *av; av++) 1158 if (**av != '\0') 1159 return 1; 1160 return 0; 1161 } 1162 } else if (is_digit(*name)) { 1163 char *ap; 1164 long num; 1165 1166 /* 1167 * handle overflow sensibly (the *ap tests should never fail) 1168 */ 1169 errno = 0; 1170 num = strtol(name, &ap, 10); 1171 if (errno != 0 || (*ap != '\0' && *ap != '=')) 1172 return 0; 1173 1174 if (num == 0) 1175 ap = arg0; 1176 else if (num > shellparam.nparam) 1177 return 0; 1178 else 1179 ap = shellparam.p[num - 1]; 1180 1181 if (nulok && (ap == NULL || *ap == '\0')) 1182 return 0; 1183 } 1184 return 1; 1185 } 1186 1187 1188 1189 /* 1190 * Add the value of a specialized variable to the stack string. 1191 */ 1192 1193 STATIC void 1194 varvalue(const char *name, int quoted, int subtype, int flag) 1195 { 1196 int num; 1197 char *p; 1198 int i; 1199 int sep; 1200 char **ap; 1201 #ifdef DEBUG 1202 char *start = expdest; 1203 #endif 1204 1205 VTRACE(DBG_EXPAND, ("varvalue(%c%s, sub=%d, fl=%#x)", *name, 1206 quoted ? ", quoted" : "", subtype, flag)); 1207 1208 if (subtype == VSLENGTH) /* no magic required ... */ 1209 flag &= ~(EXP_FULL | EXP_QNEEDED); 1210 1211 #define STRTODEST(p) \ 1212 do {\ 1213 if ((flag & EXP_QNEEDED) && quoted) { \ 1214 while (*p) { \ 1215 if (NEEDESC(*p)) \ 1216 STPUTC(CTLESC, expdest); \ 1217 STPUTC(*p++, expdest); \ 1218 } \ 1219 } else \ 1220 while (*p) { \ 1221 if ((flag & EXP_QNEEDED) && ISCTL(*p)) \ 1222 STPUTC(CTLESC, expdest); \ 1223 STPUTC(*p++, expdest); \ 1224 } \ 1225 } while (0) 1226 1227 1228 switch (*name) { 1229 case '$': 1230 num = rootpid; 1231 break; 1232 case '?': 1233 num = exitstatus; 1234 break; 1235 case '#': 1236 num = shellparam.nparam; 1237 break; 1238 case '!': 1239 num = backgndpid; 1240 break; 1241 case '-': 1242 for (i = 0; i < option_flags; i++) { 1243 if (optlist[optorder[i]].val) 1244 STPUTC(optlist[optorder[i]].letter, expdest); 1245 } 1246 VTRACE(DBG_EXPAND, (": %.*s\n", expdest-start, start)); 1247 return; 1248 case '@': 1249 if (flag & EXP_SPLIT && quoted) { 1250 VTRACE(DBG_EXPAND, (": $@ split (%d)\n", 1251 shellparam.nparam)); 1252 #if 0 1253 /* GROSS HACK */ 1254 if (shellparam.nparam == 0 && 1255 expdest[-1] == CTLQUOTEMARK) 1256 expdest--; 1257 /* KCAH SSORG */ 1258 #endif 1259 if (shellparam.nparam == 0) 1260 empty_dollar_at = 1; 1261 1262 for (ap = shellparam.p ; (p = *ap++) != NULL ; ) { 1263 if (*p == '\0') { 1264 /* retain an explicit null string */ 1265 STPUTC(CTLQUOTEMARK, expdest); 1266 STPUTC(CTLQUOTEEND, expdest); 1267 } else 1268 STRTODEST(p); 1269 if (*ap) 1270 /* A NUL separates args inside "" */ 1271 STPUTC('\0', expdest); 1272 } 1273 return; 1274 } 1275 /* fall through */ 1276 case '*': 1277 sep = ifsval()[0]; 1278 for (ap = shellparam.p ; (p = *ap++) != NULL ; ) { 1279 STRTODEST(p); 1280 if (!*ap) 1281 break; 1282 if (sep) { 1283 if (quoted && (flag & EXP_QNEEDED) && 1284 NEEDESC(sep)) 1285 STPUTC(CTLESC, expdest); 1286 STPUTC(sep, expdest); 1287 } else 1288 if ((flag & (EXP_SPLIT|EXP_IN_QUOTES)) == EXP_SPLIT 1289 && !quoted && **ap != '\0') 1290 STPUTC('\0', expdest); 1291 } 1292 VTRACE(DBG_EXPAND, (": %.*s\n", expdest-start, start)); 1293 return; 1294 default: 1295 if (is_digit(*name)) { 1296 long lnum; 1297 1298 errno = 0; 1299 lnum = strtol(name, &p, 10); 1300 if (errno != 0 || (*p != '\0' && *p != '=')) 1301 return; 1302 1303 if (lnum == 0) 1304 p = arg0; 1305 else if (lnum > 0 && lnum <= shellparam.nparam) 1306 p = shellparam.p[lnum - 1]; 1307 else 1308 return; 1309 STRTODEST(p); 1310 } 1311 VTRACE(DBG_EXPAND, (": %.*s\n", expdest-start, start)); 1312 return; 1313 } 1314 /* 1315 * only the specials with an int value arrive here 1316 */ 1317 VTRACE(DBG_EXPAND, ("(%d)", num)); 1318 expdest = cvtnum(num, expdest); 1319 VTRACE(DBG_EXPAND, (": %.*s\n", expdest-start, start)); 1320 } 1321 1322 1323 1324 /* 1325 * Record the fact that we have to scan this region of the 1326 * string for IFS characters. 1327 */ 1328 1329 STATIC void 1330 recordregion(int start, int end, int inquotes) 1331 { 1332 struct ifsregion *ifsp; 1333 1334 VTRACE(DBG_EXPAND, ("recordregion(%d,%d,%d)\n", start, end, inquotes)); 1335 if (ifslastp == NULL) { 1336 ifsp = &ifsfirst; 1337 } else { 1338 if (ifslastp->endoff == start 1339 && ifslastp->inquotes == inquotes) { 1340 /* extend previous area */ 1341 ifslastp->endoff = end; 1342 return; 1343 } 1344 ifsp = (struct ifsregion *)ckmalloc(sizeof (struct ifsregion)); 1345 ifslastp->next = ifsp; 1346 } 1347 ifslastp = ifsp; 1348 ifslastp->next = NULL; 1349 ifslastp->begoff = start; 1350 ifslastp->endoff = end; 1351 ifslastp->inquotes = inquotes; 1352 } 1353 1354 1355 1356 /* 1357 * Break the argument string into pieces based upon IFS and add the 1358 * strings to the argument list. The regions of the string to be 1359 * searched for IFS characters have been stored by recordregion. 1360 */ 1361 STATIC void 1362 ifsbreakup(char *string, struct arglist *arglist) 1363 { 1364 struct ifsregion *ifsp; 1365 struct strlist *sp; 1366 char *start; 1367 char *p; 1368 char *q; 1369 const char *ifs; 1370 const char *ifsspc; 1371 int had_param_ch = 0; 1372 1373 start = string; 1374 1375 VTRACE(DBG_EXPAND, ("ifsbreakup(\"%s\")", string)); /* misses \0's */ 1376 if (ifslastp == NULL) { 1377 /* Return entire argument, IFS doesn't apply to any of it */ 1378 VTRACE(DBG_EXPAND, ("no regions\n", string)); 1379 sp = stalloc(sizeof(*sp)); 1380 sp->text = start; 1381 *arglist->lastp = sp; 1382 arglist->lastp = &sp->next; 1383 return; 1384 } 1385 1386 ifs = ifsval(); 1387 1388 for (ifsp = &ifsfirst; ifsp != NULL; ifsp = ifsp->next) { 1389 p = string + ifsp->begoff; 1390 VTRACE(DBG_EXPAND, (" !%.*s!(%d)", ifsp->endoff-ifsp->begoff, 1391 p, ifsp->endoff-ifsp->begoff)); 1392 while (p < string + ifsp->endoff) { 1393 had_param_ch = 1; 1394 q = p; 1395 if (IS_BORING(*p)) { 1396 p++; 1397 continue; 1398 } 1399 if (*p == CTLESC) 1400 p++; 1401 if (ifsp->inquotes) { 1402 /* Only NULs (should be from "$@") end args */ 1403 if (*p != 0) { 1404 p++; 1405 continue; 1406 } 1407 ifsspc = NULL; 1408 VTRACE(DBG_EXPAND, (" \\0 nxt:\"%s\" ", p)); 1409 } else { 1410 if (!strchr(ifs, *p)) { 1411 p++; 1412 continue; 1413 } 1414 had_param_ch = 0; 1415 ifsspc = strchr(" \t\n", *p); 1416 1417 /* Ignore IFS whitespace at start */ 1418 if (q == start && ifsspc != NULL) { 1419 p++; 1420 start = p; 1421 continue; 1422 } 1423 } 1424 1425 /* Save this argument... */ 1426 *q = '\0'; 1427 VTRACE(DBG_EXPAND, ("<%s>", start)); 1428 sp = stalloc(sizeof(*sp)); 1429 sp->text = start; 1430 *arglist->lastp = sp; 1431 arglist->lastp = &sp->next; 1432 p++; 1433 1434 if (ifsspc != NULL) { 1435 /* Ignore further trailing IFS whitespace */ 1436 for (; p < string + ifsp->endoff; p++) { 1437 q = p; 1438 if (*p == CTLNONL) 1439 continue; 1440 if (*p == CTLESC) 1441 p++; 1442 if (strchr(ifs, *p) == NULL) { 1443 p = q; 1444 break; 1445 } 1446 if (strchr(" \t\n", *p) == NULL) { 1447 p++; 1448 break; 1449 } 1450 } 1451 } 1452 start = p; 1453 } 1454 } 1455 1456 /* 1457 while (*start == CTLQUOTEEND) 1458 start++; 1459 */ 1460 1461 /* 1462 * Save anything left as an argument. 1463 * Traditionally we have treated 'IFS=':'; set -- x$IFS' as 1464 * generating 2 arguments, the second of which is empty. 1465 * Some recent clarification of the Posix spec say that it 1466 * should only generate one.... 1467 */ 1468 if (had_param_ch || *start != 0) { 1469 VTRACE(DBG_EXPAND, (" T<%s>", start)); 1470 sp = stalloc(sizeof(*sp)); 1471 sp->text = start; 1472 *arglist->lastp = sp; 1473 arglist->lastp = &sp->next; 1474 } 1475 VTRACE(DBG_EXPAND, ("\n")); 1476 } 1477 1478 STATIC void 1479 ifsfree(void) 1480 { 1481 while (ifsfirst.next != NULL) { 1482 struct ifsregion *ifsp; 1483 INTOFF; 1484 ifsp = ifsfirst.next->next; 1485 ckfree(ifsfirst.next); 1486 ifsfirst.next = ifsp; 1487 INTON; 1488 } 1489 ifslastp = NULL; 1490 ifsfirst.next = NULL; 1491 } 1492 1493 1494 1495 /* 1496 * Expand shell metacharacters. At this point, the only control characters 1497 * should be escapes. The results are stored in the list exparg. 1498 */ 1499 1500 char *expdir; 1501 1502 1503 STATIC void 1504 expandmeta(struct strlist *str, int flag) 1505 { 1506 char *p; 1507 struct strlist **savelastp; 1508 struct strlist *sp; 1509 char c; 1510 /* TODO - EXP_REDIR */ 1511 1512 while (str) { 1513 p = str->text; 1514 for (;;) { /* fast check for meta chars */ 1515 if ((c = *p++) == '\0') 1516 goto nometa; 1517 if (c == '*' || c == '?' || c == '[' /* || c == '!' */) 1518 break; 1519 } 1520 savelastp = exparg.lastp; 1521 INTOFF; 1522 if (expdir == NULL) { 1523 int i = strlen(str->text); 1524 expdir = ckmalloc(i < 2048 ? 2048 : i); /* XXX */ 1525 } 1526 1527 expmeta(expdir, str->text); 1528 ckfree(expdir); 1529 expdir = NULL; 1530 INTON; 1531 if (exparg.lastp == savelastp) { 1532 /* 1533 * no matches 1534 */ 1535 nometa: 1536 *exparg.lastp = str; 1537 rmescapes(str->text); 1538 exparg.lastp = &str->next; 1539 } else { 1540 *exparg.lastp = NULL; 1541 *savelastp = sp = expsort(*savelastp); 1542 while (sp->next != NULL) 1543 sp = sp->next; 1544 exparg.lastp = &sp->next; 1545 } 1546 str = str->next; 1547 } 1548 } 1549 1550 STATIC void 1551 add_args(struct strlist *str) 1552 { 1553 while (str) { 1554 *exparg.lastp = str; 1555 rmescapes(str->text); 1556 exparg.lastp = &str->next; 1557 str = str->next; 1558 } 1559 } 1560 1561 1562 /* 1563 * Do metacharacter (i.e. *, ?, [...]) expansion. 1564 */ 1565 1566 STATIC void 1567 expmeta(char *enddir, char *name) 1568 { 1569 char *p; 1570 const char *cp; 1571 char *q; 1572 char *start; 1573 char *endname; 1574 int metaflag; 1575 struct stat statb; 1576 DIR *dirp; 1577 struct dirent *dp; 1578 int atend; 1579 int matchdot; 1580 1581 CTRACE(DBG_EXPAND|DBG_MATCH, ("expmeta(\"%s\")\n", name)); 1582 metaflag = 0; 1583 start = name; 1584 for (p = name ; ; p++) { 1585 if (*p == '*' || *p == '?') 1586 metaflag = 1; 1587 else if (*p == '[') { 1588 q = p + 1; 1589 if (*q == '!' || *q == '^') 1590 q++; 1591 for (;;) { 1592 while (IS_BORING(*q)) 1593 q++; 1594 if (*q == ']') { 1595 q++; 1596 metaflag = 1; 1597 break; 1598 } 1599 if (*q == '[' && q[1] == ':') { 1600 /* 1601 * character class, look for :] ending 1602 * also stop on ']' (end bracket expr) 1603 * or '\0' or '/' (end pattern) 1604 */ 1605 while (*++q != '\0' && *q != ']' && 1606 *q != '/') { 1607 if (*q == CTLESC) { 1608 if (*++q == '\0') 1609 break; 1610 if (*q == '/') 1611 break; 1612 } else if (*q == ':' && 1613 q[1] == ']') 1614 break; 1615 } 1616 if (*q == ':') { 1617 /* 1618 * stopped at ':]' 1619 * still in [...] 1620 * skip ":]" and continue; 1621 */ 1622 q += 2; 1623 continue; 1624 } 1625 1626 /* done at end of pattern, not [...] */ 1627 if (*q == '\0' || *q == '/') 1628 break; 1629 1630 /* found the ']', we have a [...] */ 1631 metaflag = 1; 1632 q++; /* skip ']' */ 1633 break; 1634 } 1635 if (*q == CTLESC) 1636 q++; 1637 /* end of pattern cannot be escaped */ 1638 if (*q == '/' || *q == '\0') 1639 break; 1640 q++; 1641 } 1642 } else if (*p == '\0') 1643 break; 1644 else if (IS_BORING(*p)) 1645 continue; 1646 else if (*p == CTLESC) 1647 p++; 1648 if (*p == '/') { 1649 if (metaflag) 1650 break; 1651 start = p + 1; 1652 } 1653 } 1654 if (metaflag == 0) { /* we've reached the end of the file name */ 1655 if (enddir != expdir) 1656 metaflag++; 1657 for (p = name ; ; p++) { 1658 if (IS_BORING(*p)) 1659 continue; 1660 if (*p == CTLESC) 1661 p++; 1662 *enddir++ = *p; 1663 if (*p == '\0') 1664 break; 1665 } 1666 if (metaflag == 0 || lstat(expdir, &statb) >= 0) 1667 addfname(expdir); 1668 return; 1669 } 1670 endname = p; 1671 if (start != name) { 1672 p = name; 1673 while (p < start) { 1674 while (IS_BORING(*p)) 1675 p++; 1676 if (*p == CTLESC) 1677 p++; 1678 *enddir++ = *p++; 1679 } 1680 } 1681 if (enddir == expdir) { 1682 cp = "."; 1683 } else if (enddir == expdir + 1 && *expdir == '/') { 1684 cp = "/"; 1685 } else { 1686 cp = expdir; 1687 enddir[-1] = '\0'; 1688 } 1689 if ((dirp = opendir(cp)) == NULL) 1690 return; 1691 if (enddir != expdir) 1692 enddir[-1] = '/'; 1693 if (*endname == 0) { 1694 atend = 1; 1695 } else { 1696 atend = 0; 1697 *endname++ = '\0'; 1698 } 1699 matchdot = 0; 1700 p = start; 1701 while (IS_BORING(*p)) 1702 p++; 1703 if (*p == CTLESC) 1704 p++; 1705 if (*p == '.') 1706 matchdot++; 1707 while (! int_pending() && (dp = readdir(dirp)) != NULL) { 1708 if (dp->d_name[0] == '.' && ! matchdot) 1709 continue; 1710 if (patmatch(start, dp->d_name, 0)) { 1711 if (atend) { 1712 scopy(dp->d_name, enddir); 1713 addfname(expdir); 1714 } else { 1715 for (p = enddir, cp = dp->d_name; 1716 (*p++ = *cp++) != '\0';) 1717 continue; 1718 p[-1] = '/'; 1719 expmeta(p, endname); 1720 } 1721 } 1722 } 1723 closedir(dirp); 1724 if (! atend) 1725 endname[-1] = '/'; 1726 } 1727 1728 1729 /* 1730 * Add a file name to the list. 1731 */ 1732 1733 STATIC void 1734 addfname(char *name) 1735 { 1736 char *p; 1737 struct strlist *sp; 1738 1739 p = stalloc(strlen(name) + 1); 1740 scopy(name, p); 1741 sp = stalloc(sizeof(*sp)); 1742 sp->text = p; 1743 *exparg.lastp = sp; 1744 exparg.lastp = &sp->next; 1745 } 1746 1747 1748 /* 1749 * Sort the results of file name expansion. It calculates the number of 1750 * strings to sort and then calls msort (short for merge sort) to do the 1751 * work. 1752 */ 1753 1754 STATIC struct strlist * 1755 expsort(struct strlist *str) 1756 { 1757 int len; 1758 struct strlist *sp; 1759 1760 len = 0; 1761 for (sp = str ; sp ; sp = sp->next) 1762 len++; 1763 return msort(str, len); 1764 } 1765 1766 1767 STATIC struct strlist * 1768 msort(struct strlist *list, int len) 1769 { 1770 struct strlist *p, *q = NULL; 1771 struct strlist **lpp; 1772 int half; 1773 int n; 1774 1775 if (len <= 1) 1776 return list; 1777 half = len >> 1; 1778 p = list; 1779 for (n = half ; --n >= 0 ; ) { 1780 q = p; 1781 p = p->next; 1782 } 1783 q->next = NULL; /* terminate first half of list */ 1784 q = msort(list, half); /* sort first half of list */ 1785 p = msort(p, len - half); /* sort second half */ 1786 lpp = &list; 1787 for (;;) { 1788 if (strcmp(p->text, q->text) < 0) { 1789 *lpp = p; 1790 lpp = &p->next; 1791 if ((p = *lpp) == NULL) { 1792 *lpp = q; 1793 break; 1794 } 1795 } else { 1796 *lpp = q; 1797 lpp = &q->next; 1798 if ((q = *lpp) == NULL) { 1799 *lpp = p; 1800 break; 1801 } 1802 } 1803 } 1804 return list; 1805 } 1806 1807 1808 /* 1809 * See if a character matches a character class, starting at the first colon 1810 * of "[:class:]". 1811 * If a valid character class is recognized, a pointer to the next character 1812 * after the final closing bracket is stored into *end, otherwise a null 1813 * pointer is stored into *end. 1814 */ 1815 static int 1816 match_charclass(const char *p, wchar_t chr, const char **end) 1817 { 1818 char name[20]; 1819 char *nameend; 1820 wctype_t cclass; 1821 char *q; 1822 1823 *end = NULL; 1824 p++; 1825 q = &name[0]; 1826 nameend = strstr(p, ":]"); 1827 if (nameend == NULL || nameend == p) /* not a valid class */ 1828 return 0; 1829 1830 if (*p == CTLESC) { 1831 if (*++p == CTLESC) 1832 return 0; 1833 if (p == nameend) 1834 return 0; 1835 } 1836 if (!is_alpha(*p)) 1837 return 0; 1838 while (p < nameend) { 1839 if (*p == CTLESC) { 1840 p++; 1841 if (p == nameend) 1842 return 0; 1843 } 1844 if (!is_in_name(*p)) /* '_' is a local extension */ 1845 return 0; 1846 if (q < &name[sizeof name]) 1847 *q++ = *p++; 1848 else 1849 p++; 1850 } 1851 1852 *end = nameend + 2; /* committed to it being a char class */ 1853 1854 if (q < &name[sizeof name]) /* a usable name found */ 1855 *q++ = '\0'; 1856 else /* too long, valid, but no match */ 1857 return 0; 1858 1859 cclass = wctype(name); 1860 /* An unknown class matches nothing but is valid nevertheless. */ 1861 if (cclass == 0) 1862 return 0; 1863 return iswctype(chr, cclass); 1864 } 1865 1866 1867 /* 1868 * Returns true if the pattern matches the string. 1869 */ 1870 1871 STATIC int 1872 patmatch(const char *pattern, const char *string, int squoted) 1873 { 1874 const char *p, *q, *end; 1875 const char *bt_p, *bt_q; 1876 char c; 1877 wchar_t wc, wc2; 1878 1879 VTRACE(DBG_MATCH, ("patmatch(P=\"%s\", W=\"%s\"%s): ", 1880 pattern, string, squoted ? ", SQ" : "")); 1881 p = pattern; 1882 q = string; 1883 bt_p = NULL; 1884 bt_q = NULL; 1885 for (;;) { 1886 switch (c = *p++) { 1887 case '\0': 1888 if (squoted && *q == CTLESC) { 1889 if (q[1] == '\0') 1890 q++; 1891 } 1892 if (*q != '\0') 1893 goto backtrack; 1894 VTRACE(DBG_MATCH, ("match\n")); 1895 return 1; 1896 case CTLESC: 1897 if (squoted && *q == CTLESC) 1898 q++; 1899 if (*p == '\0' && *q == '\0') { 1900 VTRACE(DBG_MATCH, ("match-\\\n")); 1901 return 1; 1902 } 1903 if (*q++ != *p++) 1904 goto backtrack; 1905 break; 1906 case '\\': 1907 if (squoted && *q == CTLESC) 1908 q++; 1909 if (*q++ != *p++) 1910 goto backtrack; 1911 break; 1912 case CTLQUOTEMARK: 1913 case CTLQUOTEEND: 1914 case CTLNONL: 1915 continue; 1916 case '?': 1917 if (squoted && *q == CTLESC) 1918 q++; 1919 if (*q++ == '\0') { 1920 VTRACE(DBG_MATCH, ("?fail\n")); 1921 return 0; 1922 } 1923 break; 1924 case '*': 1925 c = *p; 1926 while (c == CTLQUOTEMARK || c == '*') 1927 c = *++p; 1928 if (c != CTLESC && !IS_BORING(c) && 1929 c != '?' && c != '*' && c != '[') { 1930 while (*q != c) { 1931 if (squoted && *q == CTLESC && 1932 q[1] == c) 1933 break; 1934 if (*q == '\0') { 1935 VTRACE(DBG_MATCH, ("*fail\n")); 1936 return 0; 1937 } 1938 if (squoted && *q == CTLESC) 1939 q++; 1940 q++; 1941 } 1942 } 1943 if (c == CTLESC && p[1] == '\0') { 1944 VTRACE(DBG_MATCH, ("match+\\\n")); 1945 return 1; 1946 } 1947 /* 1948 * First try the shortest match for the '*' that 1949 * could work. We can forget any earlier '*' since 1950 * there is no way having it match more characters 1951 * can help us, given that we are already here. 1952 */ 1953 bt_p = p; 1954 bt_q = q; 1955 break; 1956 case '[': { 1957 const char *savep, *saveq, *endp; 1958 int invert, found; 1959 unsigned char chr; 1960 1961 /* 1962 * First quick check to see if there is a 1963 * possible matching ']' - if not, then this 1964 * is not a char class, and the '[' is just 1965 * a literal '['. 1966 * 1967 * This check will not detect all non classes, but 1968 * that's OK - It just means that we execute the 1969 * harder code sometimes when it it cannot succeed. 1970 */ 1971 endp = p; 1972 if (*endp == '!' || *endp == '^') 1973 endp++; 1974 for (;;) { 1975 while (IS_BORING(*endp)) 1976 endp++; 1977 if (*endp == '\0') 1978 goto dft; /* no matching ] */ 1979 if (*endp++ == ']') 1980 break; 1981 } 1982 /* end shortcut */ 1983 1984 savep = p, saveq = q; 1985 invert = 0; 1986 if (*p == '!' || *p == '^') { 1987 invert++; 1988 p++; 1989 } 1990 found = 0; 1991 if (*q == '\0') { 1992 VTRACE(DBG_MATCH, ("[]fail\n")); 1993 return 0; 1994 } 1995 if (squoted && *q == CTLESC) 1996 q++; 1997 chr = (unsigned char)*q++; 1998 c = *p++; 1999 do { 2000 if (IS_BORING(c)) 2001 continue; 2002 if (c == '\0') { 2003 p = savep, q = saveq; 2004 c = '['; 2005 goto dft; 2006 } 2007 if (c == '[' && *p == ':') { 2008 found |= match_charclass(p, chr, &end); 2009 if (end != NULL) { 2010 p = end; 2011 continue; 2012 } 2013 } 2014 if (c == CTLESC || c == '\\') 2015 c = *p++; 2016 wc = (unsigned char)c; 2017 if (*p == '-' && p[1] != ']') { 2018 p++; 2019 if (*p == CTLESC || *p == '\\') 2020 p++; 2021 wc2 = (unsigned char)*p++; 2022 if ( collate_range_cmp(chr, wc) >= 0 2023 && collate_range_cmp(chr, wc2) <= 0 2024 ) 2025 found = 1; 2026 } else { 2027 if (chr == wc) 2028 found = 1; 2029 } 2030 } while ((c = *p++) != ']'); 2031 if (found == invert) 2032 goto backtrack; 2033 break; 2034 } 2035 dft: default: 2036 if (squoted && *q == CTLESC) 2037 q++; 2038 if (*q++ == c) 2039 break; 2040 backtrack: 2041 /* 2042 * If we have a mismatch (other than hitting the end 2043 * of the string), go back to the last '*' seen and 2044 * have it match one additional character. 2045 */ 2046 if (bt_p == NULL) { 2047 VTRACE(DBG_MATCH, ("BTP fail\n")); 2048 return 0; 2049 } 2050 if (*bt_q == '\0') { 2051 VTRACE(DBG_MATCH, ("BTQ fail\n")); 2052 return 0; 2053 } 2054 bt_q++; 2055 p = bt_p; 2056 q = bt_q; 2057 break; 2058 } 2059 } 2060 } 2061 2062 2063 2064 /* 2065 * Remove any CTLESC or CTLNONL characters from a string. 2066 * 2067 * String is modified in place, and we return the length of the result 2068 */ 2069 2070 int 2071 rmescapes(char *str) 2072 { 2073 char *p, *q; 2074 2075 p = str; 2076 while (!ISCTL(*p)) { 2077 if (*p++ == '\0') 2078 return ((int)(p - str) - 1); 2079 } 2080 q = p; 2081 while (*p) { 2082 if (IS_BORING(*p)) { 2083 p++; 2084 continue; 2085 } 2086 if (*p == CTLCNL) { 2087 p++; 2088 *q++ = '\n'; 2089 continue; 2090 } 2091 if (*p == CTLESC) 2092 p++; 2093 #ifdef DEBUG 2094 else if (ISCTL(*p)) 2095 abort(); 2096 #endif 2097 *q++ = *p++; 2098 } 2099 *q = '\0'; 2100 2101 return ((int)(q - str)); 2102 } 2103 2104 /* 2105 * and a special version for dealing with expressions to be parsed 2106 * by the arithmetic evaluator. That needs to be able to count \n's 2107 * even ones that were \newline elided \n's, so we have to put the 2108 * latter back into the string - just being careful to put them only 2109 * at a place where white space can reasonably occur in the string 2110 * -- then the \n we insert will just be white space, and ignored 2111 * for all purposes except line counting. 2112 */ 2113 2114 void 2115 rmescapes_nl(char *str) 2116 { 2117 char *p, *q; 2118 int nls = 0, holdnl = 0, holdlast; 2119 2120 p = str; 2121 while (!ISCTL(*p)) { 2122 if (*p++ == '\0') 2123 return; 2124 } 2125 if (p > str) /* must reprocess char before stopper (if any) */ 2126 --p; /* so we do not place a \n badly */ 2127 q = p; 2128 while (*p) { 2129 if (*p == CTLQUOTEMARK || *p == CTLQUOTEEND) { 2130 p++; 2131 continue; 2132 } 2133 if (*p == CTLNONL) { 2134 p++; 2135 nls++; 2136 continue; 2137 } 2138 if (*p == CTLCNL) { 2139 p++; 2140 *q++ = '\n'; 2141 continue; 2142 } 2143 if (*p == CTLESC) 2144 p++; 2145 #ifdef DEBUG 2146 else if (ISCTL(*p)) 2147 abort(); 2148 #endif 2149 2150 holdlast = holdnl; 2151 holdnl = is_in_name(*p); /* letters, digits, _ */ 2152 if (q == str || is_space(q[-1]) || (*p != '=' && q[-1] != *p)) { 2153 if (nls > 0 && holdnl != holdlast) { 2154 while (nls > 0) 2155 *q++ = '\n', nls--; 2156 } 2157 } 2158 *q++ = *p++; 2159 } 2160 while (--nls >= 0) 2161 *q++ = '\n'; 2162 *q = '\0'; 2163 } 2164 2165 2166 2167 /* 2168 * See if a pattern matches in a case statement. 2169 */ 2170 2171 int 2172 casematch(union node *pattern, char *val) 2173 { 2174 struct stackmark smark; 2175 int result; 2176 char *p; 2177 2178 CTRACE(DBG_MATCH, ("casematch(P=\"%s\", W=\"%s\")\n", 2179 pattern->narg.text, val)); 2180 setstackmark(&smark); 2181 argbackq = pattern->narg.backquote; 2182 STARTSTACKSTR(expdest); 2183 ifslastp = NULL; 2184 argstr(pattern->narg.text, EXP_TILDE | EXP_CASE); 2185 STPUTC('\0', expdest); 2186 p = grabstackstr(expdest); 2187 result = patmatch(p, val, 0); 2188 popstackmark(&smark); 2189 return result; 2190 } 2191 2192 /* 2193 * Our own itoa(). Assumes result buffer is on the stack 2194 */ 2195 2196 STATIC char * 2197 cvtnum(int num, char *buf) 2198 { 2199 char temp[32]; 2200 int neg = num < 0; 2201 char *p = temp + sizeof temp - 1; 2202 2203 if (neg) 2204 num = -num; 2205 2206 *p = '\0'; 2207 do { 2208 *--p = num % 10 + '0'; 2209 } while ((num /= 10) != 0 && p > temp + 1); 2210 2211 if (neg) 2212 *--p = '-'; 2213 2214 while (*p) 2215 STPUTC(*p++, buf); 2216 return buf; 2217 } 2218 2219 /* 2220 * Do most of the work for wordexp(3). 2221 */ 2222 2223 int 2224 wordexpcmd(int argc, char **argv) 2225 { 2226 size_t len; 2227 int i; 2228 2229 out1fmt("%d", argc - 1); 2230 out1c('\0'); 2231 for (i = 1, len = 0; i < argc; i++) 2232 len += strlen(argv[i]); 2233 out1fmt("%zu", len); 2234 out1c('\0'); 2235 for (i = 1; i < argc; i++) { 2236 out1str(argv[i]); 2237 out1c('\0'); 2238 } 2239 return (0); 2240 } 2241