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