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