expand.c revision 214524
11556Srgrimes/*- 21556Srgrimes * Copyright (c) 1991, 1993 31556Srgrimes * The Regents of the University of California. All rights reserved. 4207944Sjilles * Copyright (c) 1997-2005 5207944Sjilles * Herbert Xu <herbert@gondor.apana.org.au>. All rights reserved. 61556Srgrimes * 71556Srgrimes * This code is derived from software contributed to Berkeley by 81556Srgrimes * Kenneth Almquist. 91556Srgrimes * 101556Srgrimes * Redistribution and use in source and binary forms, with or without 111556Srgrimes * modification, are permitted provided that the following conditions 121556Srgrimes * are met: 131556Srgrimes * 1. Redistributions of source code must retain the above copyright 141556Srgrimes * notice, this list of conditions and the following disclaimer. 151556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 161556Srgrimes * notice, this list of conditions and the following disclaimer in the 171556Srgrimes * documentation and/or other materials provided with the distribution. 181556Srgrimes * 4. Neither the name of the University nor the names of its contributors 191556Srgrimes * may be used to endorse or promote products derived from this software 201556Srgrimes * without specific prior written permission. 211556Srgrimes * 221556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 231556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 241556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 251556Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 261556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 271556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 281556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 291556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 301556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 311556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 321556Srgrimes * SUCH DAMAGE. 331556Srgrimes */ 341556Srgrimes 351556Srgrimes#ifndef lint 3636150Scharnier#if 0 3736150Scharnierstatic char sccsid[] = "@(#)expand.c 8.5 (Berkeley) 5/15/95"; 3836150Scharnier#endif 391556Srgrimes#endif /* not lint */ 4099110Sobrien#include <sys/cdefs.h> 4199110Sobrien__FBSDID("$FreeBSD: head/bin/sh/expand.c 214524 2010-10-29 19:34:57Z jilles $"); 421556Srgrimes 4317987Speter#include <sys/types.h> 4417987Speter#include <sys/time.h> 4517987Speter#include <sys/stat.h> 46213775Sjhb#include <dirent.h> 4717987Speter#include <errno.h> 48213775Sjhb#include <inttypes.h> 49213775Sjhb#include <limits.h> 5017987Speter#include <pwd.h> 51213775Sjhb#include <stdio.h> 5217987Speter#include <stdlib.h> 53108286Stjr#include <string.h> 54213775Sjhb#include <unistd.h> 5517987Speter 561556Srgrimes/* 571556Srgrimes * Routines to expand arguments to commands. We have to deal with 581556Srgrimes * backquotes, shell variables, and file metacharacters. 591556Srgrimes */ 601556Srgrimes 611556Srgrimes#include "shell.h" 621556Srgrimes#include "main.h" 631556Srgrimes#include "nodes.h" 641556Srgrimes#include "eval.h" 651556Srgrimes#include "expand.h" 661556Srgrimes#include "syntax.h" 671556Srgrimes#include "parser.h" 681556Srgrimes#include "jobs.h" 691556Srgrimes#include "options.h" 701556Srgrimes#include "var.h" 711556Srgrimes#include "input.h" 721556Srgrimes#include "output.h" 731556Srgrimes#include "memalloc.h" 741556Srgrimes#include "error.h" 751556Srgrimes#include "mystring.h" 7617987Speter#include "arith.h" 7717987Speter#include "show.h" 781556Srgrimes 791556Srgrimes/* 801556Srgrimes * Structure specifying which parts of the string should be searched 811556Srgrimes * for IFS characters. 821556Srgrimes */ 831556Srgrimes 841556Srgrimesstruct ifsregion { 851556Srgrimes struct ifsregion *next; /* next region in list */ 861556Srgrimes int begoff; /* offset of start of region */ 871556Srgrimes int endoff; /* offset of end of region */ 88194975Sjilles int inquotes; /* search for nul bytes only */ 891556Srgrimes}; 901556Srgrimes 911556Srgrimes 92213760Sobrienstatic char *expdest; /* output of current string */ 93213760Sobrienstatic struct nodelist *argbackq; /* list of back quote expressions */ 94213760Sobrienstatic struct ifsregion ifsfirst; /* first struct in list of ifs regions */ 95213760Sobrienstatic struct ifsregion *ifslastp; /* last struct in list */ 96213760Sobrienstatic struct arglist exparg; /* holds expanded arg list */ 971556Srgrimes 98213811Sobrienstatic void argstr(char *, int); 99213811Sobrienstatic char *exptilde(char *, int); 100213811Sobrienstatic void expbackq(union node *, int, int); 101214524Sjillesstatic int subevalvar(char *, char *, int, int, int, int, int); 102213811Sobrienstatic char *evalvar(char *, int); 103213811Sobrienstatic int varisset(char *, int); 104213811Sobrienstatic void varvalue(char *, int, int, int); 105213811Sobrienstatic void recordregion(int, int, int); 106213811Sobrienstatic void removerecordregions(int); 107213811Sobrienstatic void ifsbreakup(char *, struct arglist *); 108213811Sobrienstatic void expandmeta(struct strlist *, int); 109213811Sobrienstatic void expmeta(char *, char *); 110213811Sobrienstatic void addfname(char *); 111213811Sobrienstatic struct strlist *expsort(struct strlist *); 112213811Sobrienstatic struct strlist *msort(struct strlist *, int); 113213811Sobrienstatic char *cvtnum(int, char *); 114213811Sobrienstatic int collate_range_cmp(int, int); 1151556Srgrimes 116213811Sobrienstatic int 117118374Sachecollate_range_cmp(int c1, int c2) 11819281Sache{ 11919281Sache static char s1[2], s2[2]; 12019281Sache 12119281Sache s1[0] = c1; 12219281Sache s2[0] = c2; 123118374Sache return (strcoll(s1, s2)); 12419281Sache} 12519281Sache 1261556Srgrimes/* 1271556Srgrimes * Expand shell variables and backquotes inside a here document. 12890111Simp * union node *arg the document 12990111Simp * int fd; where to write the expanded version 1301556Srgrimes */ 1311556Srgrimes 1321556Srgrimesvoid 13390111Simpexpandhere(union node *arg, int fd) 13490111Simp{ 1351556Srgrimes herefd = fd; 1361556Srgrimes expandarg(arg, (struct arglist *)NULL, 0); 13739137Stegge xwrite(fd, stackblock(), expdest - stackblock()); 1381556Srgrimes} 1391556Srgrimes 1401556Srgrimes 1411556Srgrimes/* 142212243Sjilles * Perform expansions on an argument, placing the resulting list of arguments 143212243Sjilles * in arglist. Parameter expansion, command substitution and arithmetic 144212243Sjilles * expansion are always performed; additional expansions can be requested 145212243Sjilles * via flag (EXP_*). 146212243Sjilles * The result is left in the stack string. 147212243Sjilles * When arglist is NULL, perform here document expansion. A partial result 148212243Sjilles * may be written to herefd, which is then not included in the stack string. 149212243Sjilles * 150212243Sjilles * Caution: this function uses global state and is not reentrant. 151212243Sjilles * However, a new invocation after an interrupted invocation is safe 152212243Sjilles * and will reset the global state for the new call. 1531556Srgrimes */ 1541556Srgrimesvoid 15590111Simpexpandarg(union node *arg, struct arglist *arglist, int flag) 15617987Speter{ 1571556Srgrimes struct strlist *sp; 1581556Srgrimes char *p; 1591556Srgrimes 1601556Srgrimes argbackq = arg->narg.backquote; 1611556Srgrimes STARTSTACKSTR(expdest); 1621556Srgrimes ifsfirst.next = NULL; 1631556Srgrimes ifslastp = NULL; 1641556Srgrimes argstr(arg->narg.text, flag); 1651556Srgrimes if (arglist == NULL) { 1661556Srgrimes return; /* here document expanded */ 1671556Srgrimes } 1681556Srgrimes STPUTC('\0', expdest); 1691556Srgrimes p = grabstackstr(expdest); 1701556Srgrimes exparg.lastp = &exparg.list; 1711556Srgrimes /* 1721556Srgrimes * TODO - EXP_REDIR 1731556Srgrimes */ 1741556Srgrimes if (flag & EXP_FULL) { 1751556Srgrimes ifsbreakup(p, &exparg); 1761556Srgrimes *exparg.lastp = NULL; 1771556Srgrimes exparg.lastp = &exparg.list; 1781556Srgrimes expandmeta(exparg.list, flag); 1791556Srgrimes } else { 1801556Srgrimes if (flag & EXP_REDIR) /*XXX - for now, just remove escapes */ 1811556Srgrimes rmescapes(p); 1821556Srgrimes sp = (struct strlist *)stalloc(sizeof (struct strlist)); 1831556Srgrimes sp->text = p; 1841556Srgrimes *exparg.lastp = sp; 1851556Srgrimes exparg.lastp = &sp->next; 1861556Srgrimes } 1871556Srgrimes while (ifsfirst.next != NULL) { 1881556Srgrimes struct ifsregion *ifsp; 1891556Srgrimes INTOFF; 1901556Srgrimes ifsp = ifsfirst.next->next; 1911556Srgrimes ckfree(ifsfirst.next); 1921556Srgrimes ifsfirst.next = ifsp; 1931556Srgrimes INTON; 1941556Srgrimes } 1951556Srgrimes *exparg.lastp = NULL; 1961556Srgrimes if (exparg.list) { 1971556Srgrimes *arglist->lastp = exparg.list; 1981556Srgrimes arglist->lastp = exparg.lastp; 1991556Srgrimes } 2001556Srgrimes} 2011556Srgrimes 2021556Srgrimes 2031556Srgrimes 2041556Srgrimes/* 205212243Sjilles * Perform parameter expansion, command substitution and arithmetic 206212243Sjilles * expansion, and tilde expansion if requested via EXP_TILDE/EXP_VARTILDE. 207212243Sjilles * Processing ends at a CTLENDVAR character as well as '\0'. 208212243Sjilles * This is used to expand word in ${var+word} etc. 209212243Sjilles * If EXP_FULL, EXP_CASE or EXP_REDIR are set, keep and/or generate CTLESC 210212243Sjilles * characters to allow for further processing. 211212243Sjilles * If EXP_FULL is set, also preserve CTLQUOTEMARK characters. 2121556Srgrimes */ 213213811Sobrienstatic void 21490111Simpargstr(char *p, int flag) 21517987Speter{ 21625233Ssteve char c; 217104672Stjr int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR); /* do CTLESC */ 2181556Srgrimes int firsteq = 1; 219214512Sjilles int split_lit; 220214512Sjilles int lit_quoted; 2211556Srgrimes 222214512Sjilles split_lit = flag & EXP_SPLIT_LIT; 223214512Sjilles lit_quoted = flag & EXP_LIT_QUOTED; 224214512Sjilles flag &= ~(EXP_SPLIT_LIT | EXP_LIT_QUOTED); 2251556Srgrimes if (*p == '~' && (flag & (EXP_TILDE | EXP_VARTILDE))) 2261556Srgrimes p = exptilde(p, flag); 2271556Srgrimes for (;;) { 2281556Srgrimes switch (c = *p++) { 2291556Srgrimes case '\0': 230212243Sjilles case CTLENDVAR: 2311556Srgrimes goto breakloop; 23238887Stegge case CTLQUOTEMARK: 233214512Sjilles lit_quoted = 1; 23438887Stegge /* "$@" syntax adherence hack */ 23538887Stegge if (p[0] == CTLVAR && p[2] == '@' && p[3] == '=') 23638887Stegge break; 23739137Stegge if ((flag & EXP_FULL) != 0) 23839137Stegge STPUTC(c, expdest); 23938887Stegge break; 240214512Sjilles case CTLQUOTEEND: 241214512Sjilles lit_quoted = 0; 242214512Sjilles break; 2431556Srgrimes case CTLESC: 2441556Srgrimes if (quotes) 2451556Srgrimes STPUTC(c, expdest); 2461556Srgrimes c = *p++; 2471556Srgrimes STPUTC(c, expdest); 248214512Sjilles if (split_lit && !lit_quoted) 249214512Sjilles recordregion(expdest - stackblock() - 250214512Sjilles (quotes ? 2 : 1), 251214512Sjilles expdest - stackblock(), 0); 2521556Srgrimes break; 2531556Srgrimes case CTLVAR: 2541556Srgrimes p = evalvar(p, flag); 2551556Srgrimes break; 2561556Srgrimes case CTLBACKQ: 2571556Srgrimes case CTLBACKQ|CTLQUOTE: 2581556Srgrimes expbackq(argbackq->n, c & CTLQUOTE, flag); 2591556Srgrimes argbackq = argbackq->next; 2601556Srgrimes break; 2611556Srgrimes case CTLENDARI: 2621556Srgrimes expari(flag); 2631556Srgrimes break; 2641556Srgrimes case ':': 2651556Srgrimes case '=': 2661556Srgrimes /* 2671556Srgrimes * sort of a hack - expand tildes in variable 2681556Srgrimes * assignments (after the first '=' and after ':'s). 2691556Srgrimes */ 2701556Srgrimes STPUTC(c, expdest); 271214512Sjilles if (split_lit && !lit_quoted) 272214512Sjilles recordregion(expdest - stackblock() - 1, 273214512Sjilles expdest - stackblock(), 0); 274214512Sjilles if (flag & EXP_VARTILDE && *p == '~' && 275214512Sjilles (c != '=' || firsteq)) { 276214512Sjilles if (c == '=') 277214512Sjilles firsteq = 0; 2781556Srgrimes p = exptilde(p, flag); 2791556Srgrimes } 2801556Srgrimes break; 2811556Srgrimes default: 2821556Srgrimes STPUTC(c, expdest); 283214512Sjilles if (split_lit && !lit_quoted) 284214512Sjilles recordregion(expdest - stackblock() - 1, 285214512Sjilles expdest - stackblock(), 0); 2861556Srgrimes } 2871556Srgrimes } 2881556Srgrimesbreakloop:; 2891556Srgrimes} 2901556Srgrimes 291212243Sjilles/* 292212243Sjilles * Perform tilde expansion, placing the result in the stack string and 293212243Sjilles * returning the next position in the input string to process. 294212243Sjilles */ 295213811Sobrienstatic char * 29690111Simpexptilde(char *p, int flag) 29717987Speter{ 2981556Srgrimes char c, *startp = p; 2991556Srgrimes struct passwd *pw; 3001556Srgrimes char *home; 301108935Stjr int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR); 3021556Srgrimes 30317987Speter while ((c = *p) != '\0') { 3041556Srgrimes switch(c) { 305200988Sjilles case CTLESC: /* This means CTL* are always considered quoted. */ 306200988Sjilles case CTLVAR: 307200988Sjilles case CTLBACKQ: 308200988Sjilles case CTLBACKQ | CTLQUOTE: 309200988Sjilles case CTLARI: 310200988Sjilles case CTLENDARI: 31139137Stegge case CTLQUOTEMARK: 31239137Stegge return (startp); 3131556Srgrimes case ':': 3141556Srgrimes if (flag & EXP_VARTILDE) 3151556Srgrimes goto done; 3161556Srgrimes break; 3171556Srgrimes case '/': 318206150Sjilles case CTLENDVAR: 3191556Srgrimes goto done; 3201556Srgrimes } 3211556Srgrimes p++; 3221556Srgrimes } 3231556Srgrimesdone: 3241556Srgrimes *p = '\0'; 3251556Srgrimes if (*(startp+1) == '\0') { 3261556Srgrimes if ((home = lookupvar("HOME")) == NULL) 3271556Srgrimes goto lose; 3281556Srgrimes } else { 3291556Srgrimes if ((pw = getpwnam(startp+1)) == NULL) 3301556Srgrimes goto lose; 3311556Srgrimes home = pw->pw_dir; 3321556Srgrimes } 3331556Srgrimes if (*home == '\0') 3341556Srgrimes goto lose; 3351556Srgrimes *p = c; 33617987Speter while ((c = *home++) != '\0') { 33783675Stegge if (quotes && SQSYNTAX[(int)c] == CCTL) 3381556Srgrimes STPUTC(CTLESC, expdest); 3391556Srgrimes STPUTC(c, expdest); 3401556Srgrimes } 3411556Srgrimes return (p); 3421556Srgrimeslose: 3431556Srgrimes *p = c; 3441556Srgrimes return (startp); 3451556Srgrimes} 3461556Srgrimes 3471556Srgrimes 348213811Sobrienstatic void 34990111Simpremoverecordregions(int endoff) 35038887Stegge{ 35138887Stegge if (ifslastp == NULL) 35238887Stegge return; 35338887Stegge 35438887Stegge if (ifsfirst.endoff > endoff) { 35538887Stegge while (ifsfirst.next != NULL) { 35638887Stegge struct ifsregion *ifsp; 35738887Stegge INTOFF; 35838887Stegge ifsp = ifsfirst.next->next; 35938887Stegge ckfree(ifsfirst.next); 36038887Stegge ifsfirst.next = ifsp; 36138887Stegge INTON; 36238887Stegge } 36338887Stegge if (ifsfirst.begoff > endoff) 36438887Stegge ifslastp = NULL; 36538887Stegge else { 36638887Stegge ifslastp = &ifsfirst; 36738887Stegge ifsfirst.endoff = endoff; 36838887Stegge } 36938887Stegge return; 37038887Stegge } 371155301Sschweikh 37238887Stegge ifslastp = &ifsfirst; 37338887Stegge while (ifslastp->next && ifslastp->next->begoff < endoff) 37438887Stegge ifslastp=ifslastp->next; 37538887Stegge while (ifslastp->next != NULL) { 37638887Stegge struct ifsregion *ifsp; 37738887Stegge INTOFF; 37838887Stegge ifsp = ifslastp->next->next; 37938887Stegge ckfree(ifslastp->next); 38038887Stegge ifslastp->next = ifsp; 38138887Stegge INTON; 38238887Stegge } 38338887Stegge if (ifslastp->endoff > endoff) 38438887Stegge ifslastp->endoff = endoff; 38538887Stegge} 38638887Stegge 3871556Srgrimes/* 3881556Srgrimes * Expand arithmetic expression. Backup to start of expression, 3891556Srgrimes * evaluate, place result in (backed up) result, adjust string position. 3901556Srgrimes */ 3911556Srgrimesvoid 39290111Simpexpari(int flag) 39317987Speter{ 394207206Sjilles char *p, *q, *start; 395178631Sstefanf arith_t result; 39638887Stegge int begoff; 397108935Stjr int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR); 39838887Stegge int quoted; 3991556Srgrimes 4001556Srgrimes /* 40146684Skris * This routine is slightly over-complicated for 4021556Srgrimes * efficiency. First we make sure there is 4031556Srgrimes * enough space for the result, which may be bigger 404212243Sjilles * than the expression. Next we 4051556Srgrimes * scan backwards looking for the start of arithmetic. If the 4061556Srgrimes * next previous character is a CTLESC character, then we 4071556Srgrimes * have to rescan starting from the beginning since CTLESC 4088855Srgrimes * characters have to be processed left to right. 4091556Srgrimes */ 410178631Sstefanf CHECKSTRSPACE(DIGITS(result) - 2, expdest); 4118855Srgrimes USTPUTC('\0', expdest); 4121556Srgrimes start = stackblock(); 41383676Stegge p = expdest - 2; 41483676Stegge while (p >= start && *p != CTLARI) 4151556Srgrimes --p; 41683676Stegge if (p < start || *p != CTLARI) 4171556Srgrimes error("missing CTLARI (shouldn't happen)"); 41883676Stegge if (p > start && *(p - 1) == CTLESC) 4191556Srgrimes for (p = start; *p != CTLARI; p++) 4201556Srgrimes if (*p == CTLESC) 4211556Srgrimes p++; 42238887Stegge 42338887Stegge if (p[1] == '"') 42438887Stegge quoted=1; 42538887Stegge else 42638887Stegge quoted=0; 42738887Stegge begoff = p - start; 42838887Stegge removerecordregions(begoff); 4291556Srgrimes if (quotes) 43038887Stegge rmescapes(p+2); 431207206Sjilles q = grabstackstr(expdest); 43238887Stegge result = arith(p+2); 433207206Sjilles ungrabstackstr(q, expdest); 434178631Sstefanf fmtstr(p, DIGITS(result), ARITH_FORMAT_STR, result); 4351556Srgrimes while (*p++) 4361556Srgrimes ; 43738887Stegge if (quoted == 0) 43838887Stegge recordregion(begoff, p - 1 - start, 0); 4391556Srgrimes result = expdest - p + 1; 4401556Srgrimes STADJUST(-result, expdest); 4411556Srgrimes} 4421556Srgrimes 4431556Srgrimes 4441556Srgrimes/* 445212243Sjilles * Perform command substitution. 4461556Srgrimes */ 447213811Sobrienstatic void 44890111Simpexpbackq(union node *cmd, int quoted, int flag) 44917987Speter{ 4501556Srgrimes struct backcmd in; 4511556Srgrimes int i; 4521556Srgrimes char buf[128]; 4531556Srgrimes char *p; 4541556Srgrimes char *dest = expdest; 4551556Srgrimes struct ifsregion saveifs, *savelastp; 4561556Srgrimes struct nodelist *saveargbackq; 4571556Srgrimes char lastc; 4581556Srgrimes int startloc = dest - stackblock(); 4591556Srgrimes char const *syntax = quoted? DQSYNTAX : BASESYNTAX; 4601556Srgrimes int saveherefd; 461108935Stjr int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR); 462115424Sfenner int nnl; 4631556Srgrimes 4641556Srgrimes INTOFF; 4651556Srgrimes saveifs = ifsfirst; 4661556Srgrimes savelastp = ifslastp; 4671556Srgrimes saveargbackq = argbackq; 4688855Srgrimes saveherefd = herefd; 4691556Srgrimes herefd = -1; 4701556Srgrimes p = grabstackstr(dest); 4711556Srgrimes evalbackcmd(cmd, &in); 4721556Srgrimes ungrabstackstr(p, dest); 4731556Srgrimes ifsfirst = saveifs; 4741556Srgrimes ifslastp = savelastp; 4751556Srgrimes argbackq = saveargbackq; 4761556Srgrimes herefd = saveherefd; 4771556Srgrimes 4781556Srgrimes p = in.buf; 4791556Srgrimes lastc = '\0'; 480115424Sfenner nnl = 0; 481115424Sfenner /* Don't copy trailing newlines */ 4821556Srgrimes for (;;) { 4831556Srgrimes if (--in.nleft < 0) { 4841556Srgrimes if (in.fd < 0) 4851556Srgrimes break; 4861556Srgrimes while ((i = read(in.fd, buf, sizeof buf)) < 0 && errno == EINTR); 4871556Srgrimes TRACE(("expbackq: read returns %d\n", i)); 4881556Srgrimes if (i <= 0) 4891556Srgrimes break; 4901556Srgrimes p = buf; 4911556Srgrimes in.nleft = i - 1; 4921556Srgrimes } 4931556Srgrimes lastc = *p++; 4941556Srgrimes if (lastc != '\0') { 49583675Stegge if (quotes && syntax[(int)lastc] == CCTL) 4961556Srgrimes STPUTC(CTLESC, dest); 497115424Sfenner if (lastc == '\n') { 498115424Sfenner nnl++; 499115424Sfenner } else { 500115424Sfenner while (nnl > 0) { 501115424Sfenner nnl--; 502115424Sfenner STPUTC('\n', dest); 503115424Sfenner } 504115424Sfenner STPUTC(lastc, dest); 505115424Sfenner } 5061556Srgrimes } 5071556Srgrimes } 50817987Speter 5091556Srgrimes if (in.fd >= 0) 5101556Srgrimes close(in.fd); 5111556Srgrimes if (in.buf) 5121556Srgrimes ckfree(in.buf); 5131556Srgrimes if (in.jp) 51445916Scracauer exitstatus = waitforjob(in.jp, (int *)NULL); 5151556Srgrimes if (quoted == 0) 5161556Srgrimes recordregion(startloc, dest - stackblock(), 0); 517213775Sjhb TRACE(("expbackq: size=%td: \"%.*s\"\n", 518213775Sjhb ((dest - stackblock()) - startloc), 519213775Sjhb (int)((dest - stackblock()) - startloc), 5201556Srgrimes stackblock() + startloc)); 5211556Srgrimes expdest = dest; 5221556Srgrimes INTON; 5231556Srgrimes} 5241556Srgrimes 5251556Srgrimes 5261556Srgrimes 527213811Sobrienstatic int 52890111Simpsubevalvar(char *p, char *str, int strloc, int subtype, int startloc, 529214524Sjilles int varflags, int quotes) 53017987Speter{ 53117987Speter char *startp; 53217987Speter char *loc = NULL; 53345514Stegge char *q; 53417987Speter int c = 0; 53517987Speter int saveherefd = herefd; 53617987Speter struct nodelist *saveargbackq = argbackq; 53720425Ssteve int amount; 53820425Ssteve 53917987Speter herefd = -1; 540206150Sjilles argstr(p, (subtype == VSTRIMLEFT || subtype == VSTRIMLEFTMAX || 541206147Sjilles subtype == VSTRIMRIGHT || subtype == VSTRIMRIGHTMAX ? 542206150Sjilles EXP_CASE : 0) | EXP_TILDE); 54317987Speter STACKSTRNUL(expdest); 54417987Speter herefd = saveherefd; 54517987Speter argbackq = saveargbackq; 54617987Speter startp = stackblock() + startloc; 54720425Ssteve if (str == NULL) 54820425Ssteve str = stackblock() + strloc; 54917987Speter 55017987Speter switch (subtype) { 55117987Speter case VSASSIGN: 55217987Speter setvar(str, startp, 0); 55320425Ssteve amount = startp - expdest; 55420425Ssteve STADJUST(amount, expdest); 55517987Speter varflags &= ~VSNUL; 55617987Speter if (c != 0) 55717987Speter *loc = c; 55817987Speter return 1; 55917987Speter 56017987Speter case VSQUESTION: 56117987Speter if (*p != CTLENDVAR) { 562201366Sjilles outfmt(out2, "%s\n", startp); 56317987Speter error((char *)NULL); 56417987Speter } 565112254Sru error("%.*s: parameter %snot set", (int)(p - str - 1), 56617987Speter str, (varflags & VSNUL) ? "null or " 56717987Speter : nullstr); 56817987Speter return 0; 56917987Speter 57017987Speter case VSTRIMLEFT: 57125233Ssteve for (loc = startp; loc < str; loc++) { 57217987Speter c = *loc; 57317987Speter *loc = '\0'; 574214524Sjilles if (patmatch(str, startp, quotes)) { 57517987Speter *loc = c; 57617987Speter goto recordleft; 57717987Speter } 57817987Speter *loc = c; 579214524Sjilles if (quotes && *loc == CTLESC) 58045514Stegge loc++; 58117987Speter } 58217987Speter return 0; 58317987Speter 58417987Speter case VSTRIMLEFTMAX: 58545514Stegge for (loc = str - 1; loc >= startp;) { 58617987Speter c = *loc; 58717987Speter *loc = '\0'; 588214524Sjilles if (patmatch(str, startp, quotes)) { 58917987Speter *loc = c; 59017987Speter goto recordleft; 59117987Speter } 59217987Speter *loc = c; 59345514Stegge loc--; 594214524Sjilles if (quotes && loc > startp && *(loc - 1) == CTLESC) { 59545514Stegge for (q = startp; q < loc; q++) 59645514Stegge if (*q == CTLESC) 59745514Stegge q++; 59845514Stegge if (q > loc) 59945514Stegge loc--; 60045514Stegge } 60117987Speter } 60217987Speter return 0; 60317987Speter 60417987Speter case VSTRIMRIGHT: 60545514Stegge for (loc = str - 1; loc >= startp;) { 606214524Sjilles if (patmatch(str, loc, quotes)) { 60720425Ssteve amount = loc - expdest; 60820425Ssteve STADJUST(amount, expdest); 60917987Speter return 1; 61017987Speter } 61145514Stegge loc--; 612214524Sjilles if (quotes && loc > startp && *(loc - 1) == CTLESC) { 61345514Stegge for (q = startp; q < loc; q++) 61445514Stegge if (*q == CTLESC) 61545514Stegge q++; 61645514Stegge if (q > loc) 61745514Stegge loc--; 61845514Stegge } 61917987Speter } 62017987Speter return 0; 62117987Speter 62217987Speter case VSTRIMRIGHTMAX: 62317987Speter for (loc = startp; loc < str - 1; loc++) { 624214524Sjilles if (patmatch(str, loc, quotes)) { 62520425Ssteve amount = loc - expdest; 62620425Ssteve STADJUST(amount, expdest); 62717987Speter return 1; 62817987Speter } 629214524Sjilles if (quotes && *loc == CTLESC) 63045514Stegge loc++; 63117987Speter } 63217987Speter return 0; 63317987Speter 63417987Speter 63517987Speter default: 63617987Speter abort(); 63717987Speter } 63817987Speter 63917987Speterrecordleft: 64020425Ssteve amount = ((str - 1) - (loc - startp)) - expdest; 64120425Ssteve STADJUST(amount, expdest); 64217987Speter while (loc != str - 1) 64317987Speter *startp++ = *loc++; 64417987Speter return 1; 64517987Speter} 64617987Speter 64717987Speter 6481556Srgrimes/* 6491556Srgrimes * Expand a variable, and return a pointer to the next character in the 6501556Srgrimes * input string. 6511556Srgrimes */ 6521556Srgrimes 653213811Sobrienstatic char * 65490111Simpevalvar(char *p, int flag) 65517987Speter{ 6561556Srgrimes int subtype; 6571556Srgrimes int varflags; 6581556Srgrimes char *var; 6591556Srgrimes char *val; 66045644Stegge int patloc; 6611556Srgrimes int c; 6621556Srgrimes int set; 6631556Srgrimes int special; 6641556Srgrimes int startloc; 66517987Speter int varlen; 66617987Speter int easy; 667108935Stjr int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR); 6681556Srgrimes 669164081Sstefanf varflags = (unsigned char)*p++; 6701556Srgrimes subtype = varflags & VSTYPE; 6711556Srgrimes var = p; 6721556Srgrimes special = 0; 6731556Srgrimes if (! is_name(*p)) 6741556Srgrimes special = 1; 6751556Srgrimes p = strchr(p, '=') + 1; 6761556Srgrimesagain: /* jump here after setting a variable with ${var=text} */ 677179022Sstefanf if (varflags & VSLINENO) { 678179022Sstefanf set = 1; 679179022Sstefanf special = 0; 680179022Sstefanf val = var; 681179022Sstefanf p[-1] = '\0'; /* temporarily overwrite '=' to have \0 682179022Sstefanf terminated string */ 683179022Sstefanf } else if (special) { 68425233Ssteve set = varisset(var, varflags & VSNUL); 6851556Srgrimes val = NULL; 6861556Srgrimes } else { 68760592Scracauer val = bltinlookup(var, 1); 68817987Speter if (val == NULL || ((varflags & VSNUL) && val[0] == '\0')) { 6891556Srgrimes val = NULL; 6901556Srgrimes set = 0; 6911556Srgrimes } else 6921556Srgrimes set = 1; 6931556Srgrimes } 69417987Speter varlen = 0; 6951556Srgrimes startloc = expdest - stackblock(); 696198454Sjilles if (!set && uflag && *var != '@' && *var != '*') { 69796939Stjr switch (subtype) { 69896939Stjr case VSNORMAL: 69996939Stjr case VSTRIMLEFT: 70096939Stjr case VSTRIMLEFTMAX: 70196939Stjr case VSTRIMRIGHT: 70296939Stjr case VSTRIMRIGHTMAX: 70396939Stjr case VSLENGTH: 704112254Sru error("%.*s: parameter not set", (int)(p - var - 1), 705112254Sru var); 70696939Stjr } 70796939Stjr } 7081556Srgrimes if (set && subtype != VSPLUS) { 7091556Srgrimes /* insert the value of the variable */ 7101556Srgrimes if (special) { 711164081Sstefanf varvalue(var, varflags & VSQUOTE, subtype, flag); 71217987Speter if (subtype == VSLENGTH) { 71325233Ssteve varlen = expdest - stackblock() - startloc; 71425233Ssteve STADJUST(-varlen, expdest); 71517987Speter } 7161556Srgrimes } else { 71720425Ssteve char const *syntax = (varflags & VSQUOTE) ? DQSYNTAX 71817987Speter : BASESYNTAX; 7191556Srgrimes 72017987Speter if (subtype == VSLENGTH) { 72117987Speter for (;*val; val++) 72217987Speter varlen++; 7231556Srgrimes } 72417987Speter else { 72517987Speter while (*val) { 72683675Stegge if (quotes && 72754132Scracauer syntax[(int)*val] == CCTL) 72817987Speter STPUTC(CTLESC, expdest); 72917987Speter STPUTC(*val++, expdest); 73017987Speter } 73117987Speter 73217987Speter } 7331556Srgrimes } 7341556Srgrimes } 73520425Ssteve 7361556Srgrimes if (subtype == VSPLUS) 7371556Srgrimes set = ! set; 73817987Speter 73920425Ssteve easy = ((varflags & VSQUOTE) == 0 || 74017987Speter (*var == '@' && shellparam.nparam != 1)); 74117987Speter 74217987Speter 74317987Speter switch (subtype) { 74417987Speter case VSLENGTH: 74517987Speter expdest = cvtnum(varlen, expdest); 74617987Speter goto record; 74717987Speter 74817987Speter case VSNORMAL: 74917987Speter if (!easy) 75017987Speter break; 75117987Speterrecord: 75220425Ssteve recordregion(startloc, expdest - stackblock(), 75317987Speter varflags & VSQUOTE); 75417987Speter break; 75517987Speter 75617987Speter case VSPLUS: 75717987Speter case VSMINUS: 75817987Speter if (!set) { 759214512Sjilles argstr(p, flag | (flag & EXP_FULL ? EXP_SPLIT_LIT : 0) | 760214512Sjilles (varflags & VSQUOTE ? EXP_LIT_QUOTED : 0)); 76117987Speter break; 76217987Speter } 76317987Speter if (easy) 76417987Speter goto record; 76517987Speter break; 76617987Speter 76717987Speter case VSTRIMLEFT: 76817987Speter case VSTRIMLEFTMAX: 76917987Speter case VSTRIMRIGHT: 77017987Speter case VSTRIMRIGHTMAX: 77117987Speter if (!set) 77217987Speter break; 77317987Speter /* 77417987Speter * Terminate the string and start recording the pattern 77517987Speter * right after it 77617987Speter */ 77717987Speter STPUTC('\0', expdest); 77845644Stegge patloc = expdest - stackblock(); 77945644Stegge if (subevalvar(p, NULL, patloc, subtype, 780214524Sjilles startloc, varflags, quotes) == 0) { 78145644Stegge int amount = (expdest - stackblock() - patloc) + 1; 78225233Ssteve STADJUST(-amount, expdest); 78325233Ssteve } 78438887Stegge /* Remove any recorded regions beyond start of variable */ 78538887Stegge removerecordregions(startloc); 78638887Stegge goto record; 78717987Speter 78817987Speter case VSASSIGN: 78917987Speter case VSQUESTION: 79017987Speter if (!set) { 791214524Sjilles if (subevalvar(p, var, 0, subtype, startloc, varflags, 792214524Sjilles quotes)) { 79320425Ssteve varflags &= ~VSNUL; 794155301Sschweikh /* 795155301Sschweikh * Remove any recorded regions beyond 796155301Sschweikh * start of variable 79738887Stegge */ 79838887Stegge removerecordregions(startloc); 7991556Srgrimes goto again; 80020425Ssteve } 80117987Speter break; 8021556Srgrimes } 80317987Speter if (easy) 80417987Speter goto record; 80517987Speter break; 80617987Speter 807164003Sstefanf case VSERROR: 808164003Sstefanf c = p - var - 1; 809164003Sstefanf error("${%.*s%s}: Bad substitution", c, var, 810164003Sstefanf (c > 0 && *p != CTLENDVAR) ? "..." : ""); 811164003Sstefanf 81217987Speter default: 81317987Speter abort(); 8141556Srgrimes } 815179022Sstefanf p[-1] = '='; /* recover overwritten '=' */ 81617987Speter 8171556Srgrimes if (subtype != VSNORMAL) { /* skip to end of alternative */ 8181556Srgrimes int nesting = 1; 8191556Srgrimes for (;;) { 8201556Srgrimes if ((c = *p++) == CTLESC) 8211556Srgrimes p++; 8221556Srgrimes else if (c == CTLBACKQ || c == (CTLBACKQ|CTLQUOTE)) { 8231556Srgrimes if (set) 8241556Srgrimes argbackq = argbackq->next; 8251556Srgrimes } else if (c == CTLVAR) { 8261556Srgrimes if ((*p++ & VSTYPE) != VSNORMAL) 8271556Srgrimes nesting++; 8281556Srgrimes } else if (c == CTLENDVAR) { 8291556Srgrimes if (--nesting == 0) 8301556Srgrimes break; 8311556Srgrimes } 8321556Srgrimes } 8331556Srgrimes } 8341556Srgrimes return p; 8351556Srgrimes} 8361556Srgrimes 8371556Srgrimes 8381556Srgrimes 8391556Srgrimes/* 8401556Srgrimes * Test whether a specialized variable is set. 8411556Srgrimes */ 8421556Srgrimes 843213811Sobrienstatic int 84490111Simpvarisset(char *name, int nulok) 84525233Ssteve{ 8461556Srgrimes 84725233Ssteve if (*name == '!') 848209600Sjilles return backgndpidset(); 84925233Ssteve else if (*name == '@' || *name == '*') { 8501556Srgrimes if (*shellparam.p == NULL) 8511556Srgrimes return 0; 85225233Ssteve 85325233Ssteve if (nulok) { 85425233Ssteve char **av; 85525233Ssteve 85625233Ssteve for (av = shellparam.p; *av; av++) 85725233Ssteve if (**av != '\0') 85825233Ssteve return 1; 85925233Ssteve return 0; 86025233Ssteve } 86118202Speter } else if (is_digit(*name)) { 86225233Ssteve char *ap; 86320425Ssteve int num = atoi(name); 86425233Ssteve 86525233Ssteve if (num > shellparam.nparam) 86625233Ssteve return 0; 86725233Ssteve 86825233Ssteve if (num == 0) 86925233Ssteve ap = arg0; 87025233Ssteve else 87125233Ssteve ap = shellparam.p[num - 1]; 87225233Ssteve 87325233Ssteve if (nulok && (ap == NULL || *ap == '\0')) 87425233Ssteve return 0; 8751556Srgrimes } 8761556Srgrimes return 1; 8771556Srgrimes} 8781556Srgrimes 8791556Srgrimes 8801556Srgrimes 8811556Srgrimes/* 8821556Srgrimes * Add the value of a specialized variable to the stack string. 8831556Srgrimes */ 8841556Srgrimes 885213811Sobrienstatic void 886164081Sstefanfvarvalue(char *name, int quoted, int subtype, int flag) 88717987Speter{ 8881556Srgrimes int num; 8891556Srgrimes char *p; 8901556Srgrimes int i; 8911556Srgrimes char sep; 8921556Srgrimes char **ap; 8931556Srgrimes char const *syntax; 8941556Srgrimes 8951556Srgrimes#define STRTODEST(p) \ 8961556Srgrimes do {\ 897164081Sstefanf if (flag & (EXP_FULL | EXP_CASE) && subtype != VSLENGTH) { \ 8981556Srgrimes syntax = quoted? DQSYNTAX : BASESYNTAX; \ 8991556Srgrimes while (*p) { \ 90083675Stegge if (syntax[(int)*p] == CCTL) \ 9011556Srgrimes STPUTC(CTLESC, expdest); \ 9021556Srgrimes STPUTC(*p++, expdest); \ 9031556Srgrimes } \ 9041556Srgrimes } else \ 9051556Srgrimes while (*p) \ 9061556Srgrimes STPUTC(*p++, expdest); \ 9071556Srgrimes } while (0) 9081556Srgrimes 9091556Srgrimes 91018202Speter switch (*name) { 9111556Srgrimes case '$': 9121556Srgrimes num = rootpid; 9131556Srgrimes goto numvar; 9141556Srgrimes case '?': 91517987Speter num = oexitstatus; 9161556Srgrimes goto numvar; 9171556Srgrimes case '#': 9181556Srgrimes num = shellparam.nparam; 9191556Srgrimes goto numvar; 9201556Srgrimes case '!': 921209600Sjilles num = backgndpidval(); 9221556Srgrimesnumvar: 92317987Speter expdest = cvtnum(num, expdest); 9241556Srgrimes break; 9251556Srgrimes case '-': 9261556Srgrimes for (i = 0 ; i < NOPTS ; i++) { 9271556Srgrimes if (optlist[i].val) 9281556Srgrimes STPUTC(optlist[i].letter, expdest); 9291556Srgrimes } 9301556Srgrimes break; 9311556Srgrimes case '@': 932164081Sstefanf if (flag & EXP_FULL && quoted) { 93338887Stegge for (ap = shellparam.p ; (p = *ap++) != NULL ; ) { 93438887Stegge STRTODEST(p); 93538887Stegge if (*ap) 93638887Stegge STPUTC('\0', expdest); 93738887Stegge } 93838887Stegge break; 9391556Srgrimes } 940102410Scharnier /* FALLTHROUGH */ 9411556Srgrimes case '*': 942149825Srse if (ifsset()) 94338887Stegge sep = ifsval()[0]; 94438887Stegge else 94538887Stegge sep = ' '; 9461556Srgrimes for (ap = shellparam.p ; (p = *ap++) != NULL ; ) { 9471556Srgrimes STRTODEST(p); 94838887Stegge if (*ap && sep) 9491556Srgrimes STPUTC(sep, expdest); 9501556Srgrimes } 9511556Srgrimes break; 9521556Srgrimes case '0': 9531556Srgrimes p = arg0; 9541556Srgrimes STRTODEST(p); 9551556Srgrimes break; 9561556Srgrimes default: 95718202Speter if (is_digit(*name)) { 95818202Speter num = atoi(name); 95918202Speter if (num > 0 && num <= shellparam.nparam) { 96018202Speter p = shellparam.p[num - 1]; 96118202Speter STRTODEST(p); 96218202Speter } 9631556Srgrimes } 9641556Srgrimes break; 9651556Srgrimes } 9661556Srgrimes} 9671556Srgrimes 9681556Srgrimes 9691556Srgrimes 9701556Srgrimes/* 9711556Srgrimes * Record the the fact that we have to scan this region of the 9721556Srgrimes * string for IFS characters. 9731556Srgrimes */ 9741556Srgrimes 975213811Sobrienstatic void 976194975Sjillesrecordregion(int start, int end, int inquotes) 97717987Speter{ 97825233Ssteve struct ifsregion *ifsp; 9791556Srgrimes 9801556Srgrimes if (ifslastp == NULL) { 9811556Srgrimes ifsp = &ifsfirst; 9821556Srgrimes } else { 983194975Sjilles if (ifslastp->endoff == start 984194975Sjilles && ifslastp->inquotes == inquotes) { 985194975Sjilles /* extend previous area */ 986194975Sjilles ifslastp->endoff = end; 987194975Sjilles return; 988194975Sjilles } 9891556Srgrimes ifsp = (struct ifsregion *)ckmalloc(sizeof (struct ifsregion)); 9901556Srgrimes ifslastp->next = ifsp; 9911556Srgrimes } 9921556Srgrimes ifslastp = ifsp; 9931556Srgrimes ifslastp->next = NULL; 9941556Srgrimes ifslastp->begoff = start; 9951556Srgrimes ifslastp->endoff = end; 996194975Sjilles ifslastp->inquotes = inquotes; 9971556Srgrimes} 9981556Srgrimes 9991556Srgrimes 10001556Srgrimes 10011556Srgrimes/* 10021556Srgrimes * Break the argument string into pieces based upon IFS and add the 10031556Srgrimes * strings to the argument list. The regions of the string to be 10041556Srgrimes * searched for IFS characters have been stored by recordregion. 1005212243Sjilles * CTLESC characters are preserved but have little effect in this pass 1006212243Sjilles * other than escaping CTL* characters. In particular, they do not escape 1007212243Sjilles * IFS characters: that should be done with the ifsregion mechanism. 1008212243Sjilles * CTLQUOTEMARK characters are used to preserve empty quoted strings. 1009212243Sjilles * This pass treats them as a regular character, making the string non-empty. 1010212243Sjilles * Later, they are removed along with the other CTL* characters. 10111556Srgrimes */ 1012213811Sobrienstatic void 101390111Simpifsbreakup(char *string, struct arglist *arglist) 101490111Simp{ 10151556Srgrimes struct ifsregion *ifsp; 10161556Srgrimes struct strlist *sp; 10171556Srgrimes char *start; 101825233Ssteve char *p; 10191556Srgrimes char *q; 1020201053Sjilles const char *ifs; 1021194975Sjilles const char *ifsspc; 1022194975Sjilles int had_param_ch = 0; 10231556Srgrimes 1024194975Sjilles start = string; 102517987Speter 1026194975Sjilles if (ifslastp == NULL) { 1027194975Sjilles /* Return entire argument, IFS doesn't apply to any of it */ 1028194975Sjilles sp = (struct strlist *)stalloc(sizeof *sp); 1029194975Sjilles sp->text = start; 1030194975Sjilles *arglist->lastp = sp; 1031194975Sjilles arglist->lastp = &sp->next; 1032194975Sjilles return; 1033194975Sjilles } 1034194975Sjilles 1035194975Sjilles ifs = ifsset() ? ifsval() : " \t\n"; 1036194975Sjilles 1037194975Sjilles for (ifsp = &ifsfirst; ifsp != NULL; ifsp = ifsp->next) { 1038194975Sjilles p = string + ifsp->begoff; 1039194975Sjilles while (p < string + ifsp->endoff) { 1040194975Sjilles q = p; 1041194975Sjilles if (*p == CTLESC) 1042194975Sjilles p++; 1043194975Sjilles if (ifsp->inquotes) { 1044194975Sjilles /* Only NULs (should be from "$@") end args */ 1045194977Sjilles had_param_ch = 1; 1046194975Sjilles if (*p != 0) { 10471556Srgrimes p++; 1048194975Sjilles continue; 1049194975Sjilles } 1050194975Sjilles ifsspc = NULL; 1051194975Sjilles } else { 1052194975Sjilles if (!strchr(ifs, *p)) { 1053194977Sjilles had_param_ch = 1; 105438887Stegge p++; 1055194975Sjilles continue; 1056194975Sjilles } 1057194975Sjilles ifsspc = strchr(" \t\n", *p); 1058194975Sjilles 1059194975Sjilles /* Ignore IFS whitespace at start */ 1060194975Sjilles if (q == start && ifsspc != NULL) { 1061194975Sjilles p++; 10621556Srgrimes start = p; 1063194975Sjilles continue; 1064194975Sjilles } 1065194977Sjilles had_param_ch = 0; 10661556Srgrimes } 1067194975Sjilles 1068194975Sjilles /* Save this argument... */ 1069194975Sjilles *q = '\0'; 10701556Srgrimes sp = (struct strlist *)stalloc(sizeof *sp); 10711556Srgrimes sp->text = start; 10721556Srgrimes *arglist->lastp = sp; 10731556Srgrimes arglist->lastp = &sp->next; 1074194975Sjilles p++; 1075194975Sjilles 1076194975Sjilles if (ifsspc != NULL) { 1077194975Sjilles /* Ignore further trailing IFS whitespace */ 1078194975Sjilles for (; p < string + ifsp->endoff; p++) { 1079194975Sjilles q = p; 1080194975Sjilles if (*p == CTLESC) 1081194975Sjilles p++; 1082194975Sjilles if (strchr(ifs, *p) == NULL) { 1083194975Sjilles p = q; 1084194975Sjilles break; 1085194975Sjilles } 1086194975Sjilles if (strchr(" \t\n", *p) == NULL) { 1087194975Sjilles p++; 1088194975Sjilles break; 1089194975Sjilles } 1090194975Sjilles } 1091194975Sjilles } 1092194975Sjilles start = p; 10931556Srgrimes } 1094194975Sjilles } 1095194975Sjilles 1096194975Sjilles /* 1097194975Sjilles * Save anything left as an argument. 1098194975Sjilles * Traditionally we have treated 'IFS=':'; set -- x$IFS' as 1099194975Sjilles * generating 2 arguments, the second of which is empty. 1100194975Sjilles * Some recent clarification of the Posix spec say that it 1101194975Sjilles * should only generate one.... 1102194975Sjilles */ 1103194975Sjilles if (had_param_ch || *start != 0) { 11041556Srgrimes sp = (struct strlist *)stalloc(sizeof *sp); 11051556Srgrimes sp->text = start; 11061556Srgrimes *arglist->lastp = sp; 11071556Srgrimes arglist->lastp = &sp->next; 11081556Srgrimes } 11091556Srgrimes} 11101556Srgrimes 11111556Srgrimes 1112213760Sobrienstatic char expdir[PATH_MAX]; 1113212243Sjilles#define expdir_end (expdir + sizeof(expdir)) 11141556Srgrimes 11151556Srgrimes/* 1116212243Sjilles * Perform pathname generation and remove control characters. 1117212243Sjilles * At this point, the only control characters should be CTLESC and CTLQUOTEMARK. 1118212243Sjilles * The results are stored in the list exparg. 11191556Srgrimes */ 1120213811Sobrienstatic void 112190111Simpexpandmeta(struct strlist *str, int flag __unused) 112217987Speter{ 11231556Srgrimes char *p; 11241556Srgrimes struct strlist **savelastp; 11251556Srgrimes struct strlist *sp; 11261556Srgrimes char c; 11271556Srgrimes /* TODO - EXP_REDIR */ 11281556Srgrimes 11291556Srgrimes while (str) { 11301556Srgrimes if (fflag) 11311556Srgrimes goto nometa; 11321556Srgrimes p = str->text; 11331556Srgrimes for (;;) { /* fast check for meta chars */ 11341556Srgrimes if ((c = *p++) == '\0') 11351556Srgrimes goto nometa; 1136211646Sjilles if (c == '*' || c == '?' || c == '[') 11371556Srgrimes break; 11381556Srgrimes } 11391556Srgrimes savelastp = exparg.lastp; 11401556Srgrimes INTOFF; 11411556Srgrimes expmeta(expdir, str->text); 11421556Srgrimes INTON; 11431556Srgrimes if (exparg.lastp == savelastp) { 11448855Srgrimes /* 11458855Srgrimes * no matches 11461556Srgrimes */ 11471556Srgrimesnometa: 11481556Srgrimes *exparg.lastp = str; 11491556Srgrimes rmescapes(str->text); 11501556Srgrimes exparg.lastp = &str->next; 11511556Srgrimes } else { 11521556Srgrimes *exparg.lastp = NULL; 11531556Srgrimes *savelastp = sp = expsort(*savelastp); 11541556Srgrimes while (sp->next != NULL) 11551556Srgrimes sp = sp->next; 11561556Srgrimes exparg.lastp = &sp->next; 11571556Srgrimes } 11581556Srgrimes str = str->next; 11591556Srgrimes } 11601556Srgrimes} 11611556Srgrimes 11621556Srgrimes 11631556Srgrimes/* 11641556Srgrimes * Do metacharacter (i.e. *, ?, [...]) expansion. 11651556Srgrimes */ 11661556Srgrimes 1167213811Sobrienstatic void 116890111Simpexpmeta(char *enddir, char *name) 116990111Simp{ 117025233Ssteve char *p; 11711556Srgrimes char *q; 11721556Srgrimes char *start; 11731556Srgrimes char *endname; 11741556Srgrimes int metaflag; 11751556Srgrimes struct stat statb; 11761556Srgrimes DIR *dirp; 11771556Srgrimes struct dirent *dp; 11781556Srgrimes int atend; 11791556Srgrimes int matchdot; 1180207944Sjilles int esc; 11811556Srgrimes 11821556Srgrimes metaflag = 0; 11831556Srgrimes start = name; 1184207944Sjilles for (p = name; esc = 0, *p; p += esc + 1) { 11851556Srgrimes if (*p == '*' || *p == '?') 11861556Srgrimes metaflag = 1; 11871556Srgrimes else if (*p == '[') { 11881556Srgrimes q = p + 1; 118926488Sache if (*q == '!' || *q == '^') 11901556Srgrimes q++; 11911556Srgrimes for (;;) { 119238887Stegge while (*q == CTLQUOTEMARK) 119338887Stegge q++; 11941556Srgrimes if (*q == CTLESC) 11951556Srgrimes q++; 11961556Srgrimes if (*q == '/' || *q == '\0') 11971556Srgrimes break; 11981556Srgrimes if (*++q == ']') { 11991556Srgrimes metaflag = 1; 12001556Srgrimes break; 12011556Srgrimes } 12021556Srgrimes } 12031556Srgrimes } else if (*p == '\0') 12041556Srgrimes break; 120538887Stegge else if (*p == CTLQUOTEMARK) 120638887Stegge continue; 1207207944Sjilles else { 1208207944Sjilles if (*p == CTLESC) 1209207944Sjilles esc++; 1210207944Sjilles if (p[esc] == '/') { 1211207944Sjilles if (metaflag) 1212207944Sjilles break; 1213207944Sjilles start = p + esc + 1; 1214207944Sjilles } 12151556Srgrimes } 12161556Srgrimes } 12171556Srgrimes if (metaflag == 0) { /* we've reached the end of the file name */ 12181556Srgrimes if (enddir != expdir) 12191556Srgrimes metaflag++; 12201556Srgrimes for (p = name ; ; p++) { 122138887Stegge if (*p == CTLQUOTEMARK) 122238887Stegge continue; 12231556Srgrimes if (*p == CTLESC) 12241556Srgrimes p++; 12251556Srgrimes *enddir++ = *p; 12261556Srgrimes if (*p == '\0') 12271556Srgrimes break; 1228211155Sjilles if (enddir == expdir_end) 1229211155Sjilles return; 12301556Srgrimes } 1231147812Sdelphij if (metaflag == 0 || lstat(expdir, &statb) >= 0) 12321556Srgrimes addfname(expdir); 12331556Srgrimes return; 12341556Srgrimes } 12351556Srgrimes endname = p; 12361556Srgrimes if (start != name) { 12371556Srgrimes p = name; 12381556Srgrimes while (p < start) { 123938887Stegge while (*p == CTLQUOTEMARK) 124038887Stegge p++; 12411556Srgrimes if (*p == CTLESC) 12421556Srgrimes p++; 12431556Srgrimes *enddir++ = *p++; 1244211155Sjilles if (enddir == expdir_end) 1245211155Sjilles return; 12461556Srgrimes } 12471556Srgrimes } 12481556Srgrimes if (enddir == expdir) { 12491556Srgrimes p = "."; 12501556Srgrimes } else if (enddir == expdir + 1 && *expdir == '/') { 12511556Srgrimes p = "/"; 12521556Srgrimes } else { 12531556Srgrimes p = expdir; 12541556Srgrimes enddir[-1] = '\0'; 12551556Srgrimes } 12561556Srgrimes if ((dirp = opendir(p)) == NULL) 12571556Srgrimes return; 12581556Srgrimes if (enddir != expdir) 12591556Srgrimes enddir[-1] = '/'; 12601556Srgrimes if (*endname == 0) { 12611556Srgrimes atend = 1; 12621556Srgrimes } else { 12631556Srgrimes atend = 0; 1264207944Sjilles *endname = '\0'; 1265207944Sjilles endname += esc + 1; 12661556Srgrimes } 12671556Srgrimes matchdot = 0; 126838887Stegge p = start; 126938887Stegge while (*p == CTLQUOTEMARK) 127038887Stegge p++; 127138887Stegge if (*p == CTLESC) 127238887Stegge p++; 127338887Stegge if (*p == '.') 12741556Srgrimes matchdot++; 12751556Srgrimes while (! int_pending() && (dp = readdir(dirp)) != NULL) { 12761556Srgrimes if (dp->d_name[0] == '.' && ! matchdot) 12771556Srgrimes continue; 127845514Stegge if (patmatch(start, dp->d_name, 0)) { 1279211155Sjilles if (enddir + dp->d_namlen + 1 > expdir_end) 1280211155Sjilles continue; 1281211155Sjilles memcpy(enddir, dp->d_name, dp->d_namlen + 1); 1282211155Sjilles if (atend) 12831556Srgrimes addfname(expdir); 1284211155Sjilles else { 1285211155Sjilles if (enddir + dp->d_namlen + 2 > expdir_end) 128617987Speter continue; 1287211155Sjilles enddir[dp->d_namlen] = '/'; 1288211155Sjilles enddir[dp->d_namlen + 1] = '\0'; 1289211155Sjilles expmeta(enddir + dp->d_namlen + 1, endname); 12901556Srgrimes } 12911556Srgrimes } 12921556Srgrimes } 12931556Srgrimes closedir(dirp); 12941556Srgrimes if (! atend) 1295207944Sjilles endname[-esc - 1] = esc ? CTLESC : '/'; 12961556Srgrimes} 12971556Srgrimes 12981556Srgrimes 12991556Srgrimes/* 13001556Srgrimes * Add a file name to the list. 13011556Srgrimes */ 13021556Srgrimes 1303213811Sobrienstatic void 130490111Simpaddfname(char *name) 130590111Simp{ 13061556Srgrimes char *p; 13071556Srgrimes struct strlist *sp; 13081556Srgrimes 13091556Srgrimes p = stalloc(strlen(name) + 1); 13101556Srgrimes scopy(name, p); 13111556Srgrimes sp = (struct strlist *)stalloc(sizeof *sp); 13121556Srgrimes sp->text = p; 13131556Srgrimes *exparg.lastp = sp; 13141556Srgrimes exparg.lastp = &sp->next; 13151556Srgrimes} 13161556Srgrimes 13171556Srgrimes 13181556Srgrimes/* 13191556Srgrimes * Sort the results of file name expansion. It calculates the number of 13201556Srgrimes * strings to sort and then calls msort (short for merge sort) to do the 13211556Srgrimes * work. 13221556Srgrimes */ 13231556Srgrimes 1324213811Sobrienstatic struct strlist * 132590111Simpexpsort(struct strlist *str) 132690111Simp{ 13271556Srgrimes int len; 13281556Srgrimes struct strlist *sp; 13291556Srgrimes 13301556Srgrimes len = 0; 13311556Srgrimes for (sp = str ; sp ; sp = sp->next) 13321556Srgrimes len++; 13331556Srgrimes return msort(str, len); 13341556Srgrimes} 13351556Srgrimes 13361556Srgrimes 1337213811Sobrienstatic struct strlist * 133890111Simpmsort(struct strlist *list, int len) 133917987Speter{ 134017987Speter struct strlist *p, *q = NULL; 13411556Srgrimes struct strlist **lpp; 13421556Srgrimes int half; 13431556Srgrimes int n; 13441556Srgrimes 13451556Srgrimes if (len <= 1) 13461556Srgrimes return list; 13478855Srgrimes half = len >> 1; 13481556Srgrimes p = list; 13491556Srgrimes for (n = half ; --n >= 0 ; ) { 13501556Srgrimes q = p; 13511556Srgrimes p = p->next; 13521556Srgrimes } 13531556Srgrimes q->next = NULL; /* terminate first half of list */ 13541556Srgrimes q = msort(list, half); /* sort first half of list */ 13551556Srgrimes p = msort(p, len - half); /* sort second half */ 13561556Srgrimes lpp = &list; 13571556Srgrimes for (;;) { 13581556Srgrimes if (strcmp(p->text, q->text) < 0) { 13591556Srgrimes *lpp = p; 13601556Srgrimes lpp = &p->next; 13611556Srgrimes if ((p = *lpp) == NULL) { 13621556Srgrimes *lpp = q; 13631556Srgrimes break; 13641556Srgrimes } 13651556Srgrimes } else { 13661556Srgrimes *lpp = q; 13671556Srgrimes lpp = &q->next; 13681556Srgrimes if ((q = *lpp) == NULL) { 13691556Srgrimes *lpp = p; 13701556Srgrimes break; 13711556Srgrimes } 13721556Srgrimes } 13731556Srgrimes } 13741556Srgrimes return list; 13751556Srgrimes} 13761556Srgrimes 13771556Srgrimes 13781556Srgrimes 13791556Srgrimes/* 13801556Srgrimes * Returns true if the pattern matches the string. 13811556Srgrimes */ 13821556Srgrimes 13831556Srgrimesint 1384200956Sjillespatmatch(const char *pattern, const char *string, int squoted) 138590111Simp{ 1386200956Sjilles const char *p, *q; 138725233Ssteve char c; 13881556Srgrimes 13891556Srgrimes p = pattern; 13901556Srgrimes q = string; 13911556Srgrimes for (;;) { 13921556Srgrimes switch (c = *p++) { 13931556Srgrimes case '\0': 13941556Srgrimes goto breakloop; 13951556Srgrimes case CTLESC: 139645514Stegge if (squoted && *q == CTLESC) 139745514Stegge q++; 13981556Srgrimes if (*q++ != *p++) 13991556Srgrimes return 0; 14001556Srgrimes break; 140138887Stegge case CTLQUOTEMARK: 140238887Stegge continue; 14031556Srgrimes case '?': 140445514Stegge if (squoted && *q == CTLESC) 140545514Stegge q++; 14061556Srgrimes if (*q++ == '\0') 14071556Srgrimes return 0; 14081556Srgrimes break; 14091556Srgrimes case '*': 14101556Srgrimes c = *p; 141138887Stegge while (c == CTLQUOTEMARK || c == '*') 141238887Stegge c = *++p; 141338887Stegge if (c != CTLESC && c != CTLQUOTEMARK && 141438887Stegge c != '?' && c != '*' && c != '[') { 14151556Srgrimes while (*q != c) { 141645514Stegge if (squoted && *q == CTLESC && 141745514Stegge q[1] == c) 141845514Stegge break; 14191556Srgrimes if (*q == '\0') 14201556Srgrimes return 0; 142145514Stegge if (squoted && *q == CTLESC) 142245514Stegge q++; 14231556Srgrimes q++; 14241556Srgrimes } 14251556Srgrimes } 14261556Srgrimes do { 1427211646Sjilles if (patmatch(p, q, squoted)) 14281556Srgrimes return 1; 142945514Stegge if (squoted && *q == CTLESC) 143045514Stegge q++; 14311556Srgrimes } while (*q++ != '\0'); 14321556Srgrimes return 0; 14331556Srgrimes case '[': { 1434200956Sjilles const char *endp; 14351556Srgrimes int invert, found; 14361556Srgrimes char chr; 14371556Srgrimes 14381556Srgrimes endp = p; 143926488Sache if (*endp == '!' || *endp == '^') 14401556Srgrimes endp++; 14411556Srgrimes for (;;) { 144238887Stegge while (*endp == CTLQUOTEMARK) 144338887Stegge endp++; 14441556Srgrimes if (*endp == '\0') 14451556Srgrimes goto dft; /* no matching ] */ 14461556Srgrimes if (*endp == CTLESC) 14471556Srgrimes endp++; 14481556Srgrimes if (*++endp == ']') 14491556Srgrimes break; 14501556Srgrimes } 14511556Srgrimes invert = 0; 145226488Sache if (*p == '!' || *p == '^') { 14531556Srgrimes invert++; 14541556Srgrimes p++; 14551556Srgrimes } 14561556Srgrimes found = 0; 14571556Srgrimes chr = *q++; 145845514Stegge if (squoted && chr == CTLESC) 145945514Stegge chr = *q++; 146017987Speter if (chr == '\0') 146117987Speter return 0; 14621556Srgrimes c = *p++; 14631556Srgrimes do { 146438887Stegge if (c == CTLQUOTEMARK) 146538887Stegge continue; 14661556Srgrimes if (c == CTLESC) 14671556Srgrimes c = *p++; 14681556Srgrimes if (*p == '-' && p[1] != ']') { 14691556Srgrimes p++; 147038887Stegge while (*p == CTLQUOTEMARK) 147138887Stegge p++; 14721556Srgrimes if (*p == CTLESC) 14731556Srgrimes p++; 147417557Sache if ( collate_range_cmp(chr, c) >= 0 147517557Sache && collate_range_cmp(chr, *p) <= 0 147617525Sache ) 14771556Srgrimes found = 1; 14781556Srgrimes p++; 14791556Srgrimes } else { 14801556Srgrimes if (chr == c) 14811556Srgrimes found = 1; 14821556Srgrimes } 14831556Srgrimes } while ((c = *p++) != ']'); 14841556Srgrimes if (found == invert) 14851556Srgrimes return 0; 14861556Srgrimes break; 14871556Srgrimes } 14881556Srgrimesdft: default: 148945514Stegge if (squoted && *q == CTLESC) 149045514Stegge q++; 14911556Srgrimes if (*q++ != c) 14921556Srgrimes return 0; 14931556Srgrimes break; 14941556Srgrimes } 14951556Srgrimes } 14961556Srgrimesbreakloop: 14971556Srgrimes if (*q != '\0') 14981556Srgrimes return 0; 14991556Srgrimes return 1; 15001556Srgrimes} 15011556Srgrimes 15021556Srgrimes 15031556Srgrimes 15041556Srgrimes/* 1505212243Sjilles * Remove any CTLESC and CTLQUOTEMARK characters from a string. 15061556Srgrimes */ 15071556Srgrimes 15081556Srgrimesvoid 150990111Simprmescapes(char *str) 151038887Stegge{ 151125233Ssteve char *p, *q; 15121556Srgrimes 15131556Srgrimes p = str; 1514214512Sjilles while (*p != CTLESC && *p != CTLQUOTEMARK && *p != CTLQUOTEEND) { 15151556Srgrimes if (*p++ == '\0') 15161556Srgrimes return; 15171556Srgrimes } 15181556Srgrimes q = p; 15191556Srgrimes while (*p) { 1520214512Sjilles if (*p == CTLQUOTEMARK || *p == CTLQUOTEEND) { 152138887Stegge p++; 152238887Stegge continue; 152338887Stegge } 15241556Srgrimes if (*p == CTLESC) 15251556Srgrimes p++; 15261556Srgrimes *q++ = *p++; 15271556Srgrimes } 15281556Srgrimes *q = '\0'; 15291556Srgrimes} 15301556Srgrimes 15311556Srgrimes 15321556Srgrimes 15331556Srgrimes/* 15341556Srgrimes * See if a pattern matches in a case statement. 15351556Srgrimes */ 15361556Srgrimes 15371556Srgrimesint 1538200956Sjillescasematch(union node *pattern, const char *val) 153990111Simp{ 15401556Srgrimes struct stackmark smark; 15411556Srgrimes int result; 15421556Srgrimes char *p; 15431556Srgrimes 15441556Srgrimes setstackmark(&smark); 15451556Srgrimes argbackq = pattern->narg.backquote; 15461556Srgrimes STARTSTACKSTR(expdest); 15471556Srgrimes ifslastp = NULL; 15481556Srgrimes argstr(pattern->narg.text, EXP_TILDE | EXP_CASE); 15491556Srgrimes STPUTC('\0', expdest); 15501556Srgrimes p = grabstackstr(expdest); 155145514Stegge result = patmatch(p, val, 0); 15521556Srgrimes popstackmark(&smark); 15531556Srgrimes return result; 15541556Srgrimes} 155517987Speter 155617987Speter/* 155717987Speter * Our own itoa(). 155817987Speter */ 155917987Speter 1560213811Sobrienstatic char * 156190111Simpcvtnum(int num, char *buf) 156290111Simp{ 156317987Speter char temp[32]; 156417987Speter int neg = num < 0; 156517987Speter char *p = temp + 31; 156617987Speter 156717987Speter temp[31] = '\0'; 156817987Speter 156917987Speter do { 157017987Speter *--p = num % 10 + '0'; 157117987Speter } while ((num /= 10) != 0); 157217987Speter 157317987Speter if (neg) 157417987Speter *--p = '-'; 157517987Speter 157617987Speter while (*p) 157717987Speter STPUTC(*p++, buf); 157817987Speter return buf; 157917987Speter} 1580108286Stjr 1581108286Stjr/* 1582108286Stjr * Do most of the work for wordexp(3). 1583108286Stjr */ 1584108286Stjr 1585108286Stjrint 1586108286Stjrwordexpcmd(int argc, char **argv) 1587108286Stjr{ 1588108286Stjr size_t len; 1589108286Stjr int i; 1590108286Stjr 1591108286Stjr out1fmt("%08x", argc - 1); 1592108286Stjr for (i = 1, len = 0; i < argc; i++) 1593108286Stjr len += strlen(argv[i]); 1594108286Stjr out1fmt("%08x", (int)len); 1595108286Stjr for (i = 1; i < argc; i++) { 1596108286Stjr out1str(argv[i]); 1597108286Stjr out1c('\0'); 1598108286Stjr } 1599108286Stjr return (0); 1600108286Stjr} 1601