expand.c revision 39137
11556Srgrimes/*- 21556Srgrimes * Copyright (c) 1991, 1993 31556Srgrimes * The Regents of the University of California. All rights reserved. 41556Srgrimes * 51556Srgrimes * This code is derived from software contributed to Berkeley by 61556Srgrimes * Kenneth Almquist. 71556Srgrimes * 81556Srgrimes * Redistribution and use in source and binary forms, with or without 91556Srgrimes * modification, are permitted provided that the following conditions 101556Srgrimes * are met: 111556Srgrimes * 1. Redistributions of source code must retain the above copyright 121556Srgrimes * notice, this list of conditions and the following disclaimer. 131556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 141556Srgrimes * notice, this list of conditions and the following disclaimer in the 151556Srgrimes * documentation and/or other materials provided with the distribution. 161556Srgrimes * 3. All advertising materials mentioning features or use of this software 171556Srgrimes * must display the following acknowledgement: 181556Srgrimes * This product includes software developed by the University of 191556Srgrimes * California, Berkeley and its contributors. 201556Srgrimes * 4. Neither the name of the University nor the names of its contributors 211556Srgrimes * may be used to endorse or promote products derived from this software 221556Srgrimes * without specific prior written permission. 231556Srgrimes * 241556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 251556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 261556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 271556Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 281556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 291556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 301556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 311556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 321556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 331556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 341556Srgrimes * SUCH DAMAGE. 351556Srgrimes */ 361556Srgrimes 371556Srgrimes#ifndef lint 3836150Scharnier#if 0 3936150Scharnierstatic char sccsid[] = "@(#)expand.c 8.5 (Berkeley) 5/15/95"; 4036150Scharnier#endif 4136150Scharnierstatic const char rcsid[] = 4239137Stegge "$Id: expand.c,v 1.23 1998/09/06 21:13:09 tegge Exp $"; 431556Srgrimes#endif /* not lint */ 441556Srgrimes 4517987Speter#include <sys/types.h> 4617987Speter#include <sys/time.h> 4717987Speter#include <sys/stat.h> 4817987Speter#include <errno.h> 4917987Speter#include <dirent.h> 5017987Speter#include <unistd.h> 5117987Speter#include <pwd.h> 5217987Speter#include <stdlib.h> 5319281Sache#include <limits.h> 5438887Stegge#include <stdio.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 */ 881556Srgrimes int nulonly; /* search for nul bytes only */ 891556Srgrimes}; 901556Srgrimes 911556Srgrimes 921556Srgrimeschar *expdest; /* output of current string */ 931556Srgrimesstruct nodelist *argbackq; /* list of back quote expressions */ 941556Srgrimesstruct ifsregion ifsfirst; /* first struct in list of ifs regions */ 951556Srgrimesstruct ifsregion *ifslastp; /* last struct in list */ 961556Srgrimesstruct arglist exparg; /* holds expanded arg list */ 971556Srgrimes 9817987SpeterSTATIC void argstr __P((char *, int)); 9917987SpeterSTATIC char *exptilde __P((char *, int)); 10017987SpeterSTATIC void expbackq __P((union node *, int, int)); 10120425SsteveSTATIC int subevalvar __P((char *, char *, int, int, int, int)); 10217987SpeterSTATIC char *evalvar __P((char *, int)); 10325233SsteveSTATIC int varisset __P((char *, int)); 10418202SpeterSTATIC void varvalue __P((char *, int, int)); 10517987SpeterSTATIC void recordregion __P((int, int, int)); 10638887SteggeSTATIC void removerecordregions __P((int)); 10717987SpeterSTATIC void ifsbreakup __P((char *, struct arglist *)); 10817987SpeterSTATIC void expandmeta __P((struct strlist *, int)); 10917987SpeterSTATIC void expmeta __P((char *, char *)); 11017987SpeterSTATIC void addfname __P((char *)); 11117987SpeterSTATIC struct strlist *expsort __P((struct strlist *)); 11217987SpeterSTATIC struct strlist *msort __P((struct strlist *, int)); 11317987SpeterSTATIC int pmatch __P((char *, char *)); 11417987SpeterSTATIC char *cvtnum __P((int, char *)); 11519281SacheSTATIC int collate_range_cmp __P((int, int)); 1161556Srgrimes 11719281SacheSTATIC int collate_range_cmp (c1, c2) 11819281Sache int c1, c2; 11919281Sache{ 12019281Sache static char s1[2], s2[2]; 12119281Sache int ret; 12219281Sache 12319281Sache c1 &= UCHAR_MAX; 12419281Sache c2 &= UCHAR_MAX; 12519281Sache if (c1 == c2) 12619281Sache return (0); 12719281Sache s1[0] = c1; 12819281Sache s2[0] = c2; 12919281Sache if ((ret = strcoll(s1, s2)) != 0) 13019281Sache return (ret); 13119281Sache return (c1 - c2); 13219281Sache} 13319281Sache 1341556Srgrimes/* 1351556Srgrimes * Expand shell variables and backquotes inside a here document. 1361556Srgrimes */ 1371556Srgrimes 1381556Srgrimesvoid 1391556Srgrimesexpandhere(arg, fd) 1401556Srgrimes union node *arg; /* the document */ 1411556Srgrimes int fd; /* where to write the expanded version */ 1421556Srgrimes { 1431556Srgrimes herefd = fd; 1441556Srgrimes expandarg(arg, (struct arglist *)NULL, 0); 14539137Stegge xwrite(fd, stackblock(), expdest - stackblock()); 1461556Srgrimes} 1471556Srgrimes 1481556Srgrimes 1491556Srgrimes/* 1501556Srgrimes * Perform variable substitution and command substitution on an argument, 1511556Srgrimes * placing the resulting list of arguments in arglist. If EXP_FULL is true, 1521556Srgrimes * perform splitting and file name expansion. When arglist is NULL, perform 1531556Srgrimes * here document expansion. 1541556Srgrimes */ 1551556Srgrimes 1561556Srgrimesvoid 1571556Srgrimesexpandarg(arg, arglist, flag) 1581556Srgrimes union node *arg; 1591556Srgrimes struct arglist *arglist; 16017987Speter int flag; 16117987Speter{ 1621556Srgrimes struct strlist *sp; 1631556Srgrimes char *p; 1641556Srgrimes 1651556Srgrimes argbackq = arg->narg.backquote; 1661556Srgrimes STARTSTACKSTR(expdest); 1671556Srgrimes ifsfirst.next = NULL; 1681556Srgrimes ifslastp = NULL; 1691556Srgrimes argstr(arg->narg.text, flag); 1701556Srgrimes if (arglist == NULL) { 1711556Srgrimes return; /* here document expanded */ 1721556Srgrimes } 1731556Srgrimes STPUTC('\0', expdest); 1741556Srgrimes p = grabstackstr(expdest); 1751556Srgrimes exparg.lastp = &exparg.list; 1761556Srgrimes /* 1771556Srgrimes * TODO - EXP_REDIR 1781556Srgrimes */ 1791556Srgrimes if (flag & EXP_FULL) { 1801556Srgrimes ifsbreakup(p, &exparg); 1811556Srgrimes *exparg.lastp = NULL; 1821556Srgrimes exparg.lastp = &exparg.list; 1831556Srgrimes expandmeta(exparg.list, flag); 1841556Srgrimes } else { 1851556Srgrimes if (flag & EXP_REDIR) /*XXX - for now, just remove escapes */ 1861556Srgrimes rmescapes(p); 1871556Srgrimes sp = (struct strlist *)stalloc(sizeof (struct strlist)); 1881556Srgrimes sp->text = p; 1891556Srgrimes *exparg.lastp = sp; 1901556Srgrimes exparg.lastp = &sp->next; 1911556Srgrimes } 1921556Srgrimes while (ifsfirst.next != NULL) { 1931556Srgrimes struct ifsregion *ifsp; 1941556Srgrimes INTOFF; 1951556Srgrimes ifsp = ifsfirst.next->next; 1961556Srgrimes ckfree(ifsfirst.next); 1971556Srgrimes ifsfirst.next = ifsp; 1981556Srgrimes INTON; 1991556Srgrimes } 2001556Srgrimes *exparg.lastp = NULL; 2011556Srgrimes if (exparg.list) { 2021556Srgrimes *arglist->lastp = exparg.list; 2031556Srgrimes arglist->lastp = exparg.lastp; 2041556Srgrimes } 2051556Srgrimes} 2061556Srgrimes 2071556Srgrimes 2081556Srgrimes 2091556Srgrimes/* 2101556Srgrimes * Perform variable and command substitution. If EXP_FULL is set, output CTLESC 2111556Srgrimes * characters to allow for further processing. Otherwise treat 2121556Srgrimes * $@ like $* since no splitting will be performed. 2131556Srgrimes */ 2141556Srgrimes 2151556SrgrimesSTATIC void 2161556Srgrimesargstr(p, flag) 21725233Ssteve char *p; 21817987Speter int flag; 21917987Speter{ 22025233Ssteve char c; 2211556Srgrimes int quotes = flag & (EXP_FULL | EXP_CASE); /* do CTLESC */ 2221556Srgrimes int firsteq = 1; 2231556Srgrimes 2241556Srgrimes if (*p == '~' && (flag & (EXP_TILDE | EXP_VARTILDE))) 2251556Srgrimes p = exptilde(p, flag); 2261556Srgrimes for (;;) { 2271556Srgrimes switch (c = *p++) { 2281556Srgrimes case '\0': 2291556Srgrimes case CTLENDVAR: /* ??? */ 2301556Srgrimes goto breakloop; 23138887Stegge case CTLQUOTEMARK: 23238887Stegge /* "$@" syntax adherence hack */ 23338887Stegge if (p[0] == CTLVAR && p[2] == '@' && p[3] == '=') 23438887Stegge break; 23539137Stegge if ((flag & EXP_FULL) != 0) 23639137Stegge STPUTC(c, expdest); 23738887Stegge break; 2381556Srgrimes case CTLESC: 2391556Srgrimes if (quotes) 2401556Srgrimes STPUTC(c, expdest); 2411556Srgrimes c = *p++; 2421556Srgrimes STPUTC(c, expdest); 2431556Srgrimes break; 2441556Srgrimes case CTLVAR: 2451556Srgrimes p = evalvar(p, flag); 2461556Srgrimes break; 2471556Srgrimes case CTLBACKQ: 2481556Srgrimes case CTLBACKQ|CTLQUOTE: 2491556Srgrimes expbackq(argbackq->n, c & CTLQUOTE, flag); 2501556Srgrimes argbackq = argbackq->next; 2511556Srgrimes break; 2521556Srgrimes case CTLENDARI: 2531556Srgrimes expari(flag); 2541556Srgrimes break; 2551556Srgrimes case ':': 2561556Srgrimes case '=': 2571556Srgrimes /* 2581556Srgrimes * sort of a hack - expand tildes in variable 2591556Srgrimes * assignments (after the first '=' and after ':'s). 2601556Srgrimes */ 2611556Srgrimes STPUTC(c, expdest); 2621556Srgrimes if (flag & EXP_VARTILDE && *p == '~') { 2631556Srgrimes if (c == '=') { 2641556Srgrimes if (firsteq) 2651556Srgrimes firsteq = 0; 2661556Srgrimes else 2671556Srgrimes break; 2681556Srgrimes } 2691556Srgrimes p = exptilde(p, flag); 2701556Srgrimes } 2711556Srgrimes break; 2721556Srgrimes default: 2731556Srgrimes STPUTC(c, expdest); 2741556Srgrimes } 2751556Srgrimes } 2761556Srgrimesbreakloop:; 2771556Srgrimes} 2781556Srgrimes 2791556SrgrimesSTATIC char * 2801556Srgrimesexptilde(p, flag) 2811556Srgrimes char *p; 28217987Speter int flag; 28317987Speter{ 2841556Srgrimes char c, *startp = p; 2851556Srgrimes struct passwd *pw; 2861556Srgrimes char *home; 2871556Srgrimes int quotes = flag & (EXP_FULL | EXP_CASE); 2881556Srgrimes 28917987Speter while ((c = *p) != '\0') { 2901556Srgrimes switch(c) { 2911556Srgrimes case CTLESC: 2921556Srgrimes return (startp); 29339137Stegge case CTLQUOTEMARK: 29439137Stegge return (startp); 2951556Srgrimes case ':': 2961556Srgrimes if (flag & EXP_VARTILDE) 2971556Srgrimes goto done; 2981556Srgrimes break; 2991556Srgrimes case '/': 3001556Srgrimes goto done; 3011556Srgrimes } 3021556Srgrimes p++; 3031556Srgrimes } 3041556Srgrimesdone: 3051556Srgrimes *p = '\0'; 3061556Srgrimes if (*(startp+1) == '\0') { 3071556Srgrimes if ((home = lookupvar("HOME")) == NULL) 3081556Srgrimes goto lose; 3091556Srgrimes } else { 3101556Srgrimes if ((pw = getpwnam(startp+1)) == NULL) 3111556Srgrimes goto lose; 3121556Srgrimes home = pw->pw_dir; 3131556Srgrimes } 3141556Srgrimes if (*home == '\0') 3151556Srgrimes goto lose; 3161556Srgrimes *p = c; 31717987Speter while ((c = *home++) != '\0') { 3181556Srgrimes if (quotes && SQSYNTAX[c] == CCTL) 3191556Srgrimes STPUTC(CTLESC, expdest); 3201556Srgrimes STPUTC(c, expdest); 3211556Srgrimes } 3221556Srgrimes return (p); 3231556Srgrimeslose: 3241556Srgrimes *p = c; 3251556Srgrimes return (startp); 3261556Srgrimes} 3271556Srgrimes 3281556Srgrimes 32938887SteggeSTATIC void 33038887Steggeremoverecordregions(endoff) 33138887Stegge int endoff; 33238887Stegge{ 33338887Stegge if (ifslastp == NULL) 33438887Stegge return; 33538887Stegge 33638887Stegge if (ifsfirst.endoff > endoff) { 33738887Stegge while (ifsfirst.next != NULL) { 33838887Stegge struct ifsregion *ifsp; 33938887Stegge INTOFF; 34038887Stegge ifsp = ifsfirst.next->next; 34138887Stegge ckfree(ifsfirst.next); 34238887Stegge ifsfirst.next = ifsp; 34338887Stegge INTON; 34438887Stegge } 34538887Stegge if (ifsfirst.begoff > endoff) 34638887Stegge ifslastp = NULL; 34738887Stegge else { 34838887Stegge ifslastp = &ifsfirst; 34938887Stegge ifsfirst.endoff = endoff; 35038887Stegge } 35138887Stegge return; 35238887Stegge } 35338887Stegge 35438887Stegge ifslastp = &ifsfirst; 35538887Stegge while (ifslastp->next && ifslastp->next->begoff < endoff) 35638887Stegge ifslastp=ifslastp->next; 35738887Stegge while (ifslastp->next != NULL) { 35838887Stegge struct ifsregion *ifsp; 35938887Stegge INTOFF; 36038887Stegge ifsp = ifslastp->next->next; 36138887Stegge ckfree(ifslastp->next); 36238887Stegge ifslastp->next = ifsp; 36338887Stegge INTON; 36438887Stegge } 36538887Stegge if (ifslastp->endoff > endoff) 36638887Stegge ifslastp->endoff = endoff; 36738887Stegge} 36838887Stegge 3691556Srgrimes/* 3701556Srgrimes * Expand arithmetic expression. Backup to start of expression, 3711556Srgrimes * evaluate, place result in (backed up) result, adjust string position. 3721556Srgrimes */ 3731556Srgrimesvoid 3741556Srgrimesexpari(flag) 37517987Speter int flag; 37617987Speter{ 3771556Srgrimes char *p, *start; 3781556Srgrimes int result; 37938887Stegge int begoff; 3801556Srgrimes int quotes = flag & (EXP_FULL | EXP_CASE); 38138887Stegge int quoted; 3821556Srgrimes 38325233Ssteve 3841556Srgrimes /* 3851556Srgrimes * This routine is slightly over-compilcated for 3861556Srgrimes * efficiency. First we make sure there is 3871556Srgrimes * enough space for the result, which may be bigger 3881556Srgrimes * than the expression if we add exponentation. Next we 3891556Srgrimes * scan backwards looking for the start of arithmetic. If the 3901556Srgrimes * next previous character is a CTLESC character, then we 3911556Srgrimes * have to rescan starting from the beginning since CTLESC 3928855Srgrimes * characters have to be processed left to right. 3931556Srgrimes */ 39422777Ssteve#if INT_MAX / 1000000000 >= 10 || INT_MIN / 1000000000 <= -10 39522777Ssteve#error "integers with more than 10 digits are not supported" 39622777Ssteve#endif 39722777Ssteve CHECKSTRSPACE(12 - 2, expdest); 3988855Srgrimes USTPUTC('\0', expdest); 3991556Srgrimes start = stackblock(); 4001556Srgrimes p = expdest; 4011556Srgrimes while (*p != CTLARI && p >= start) 4021556Srgrimes --p; 4031556Srgrimes if (*p != CTLARI) 4041556Srgrimes error("missing CTLARI (shouldn't happen)"); 4051556Srgrimes if (p > start && *(p-1) == CTLESC) 4061556Srgrimes for (p = start; *p != CTLARI; p++) 4071556Srgrimes if (*p == CTLESC) 4081556Srgrimes p++; 40938887Stegge 41038887Stegge if (p[1] == '"') 41138887Stegge quoted=1; 41238887Stegge else 41338887Stegge quoted=0; 41438887Stegge begoff = p - start; 41538887Stegge removerecordregions(begoff); 4161556Srgrimes if (quotes) 41738887Stegge rmescapes(p+2); 41838887Stegge result = arith(p+2); 41922777Ssteve fmtstr(p, 12, "%d", result); 4201556Srgrimes while (*p++) 4211556Srgrimes ; 42238887Stegge if (quoted == 0) 42338887Stegge recordregion(begoff, p - 1 - start, 0); 4241556Srgrimes result = expdest - p + 1; 4251556Srgrimes STADJUST(-result, expdest); 4261556Srgrimes} 4271556Srgrimes 4281556Srgrimes 4291556Srgrimes/* 4301556Srgrimes * Expand stuff in backwards quotes. 4311556Srgrimes */ 4321556Srgrimes 4331556SrgrimesSTATIC void 4341556Srgrimesexpbackq(cmd, quoted, flag) 4351556Srgrimes union node *cmd; 43617987Speter int quoted; 43717987Speter int flag; 43817987Speter{ 4391556Srgrimes struct backcmd in; 4401556Srgrimes int i; 4411556Srgrimes char buf[128]; 4421556Srgrimes char *p; 4431556Srgrimes char *dest = expdest; 4441556Srgrimes struct ifsregion saveifs, *savelastp; 4451556Srgrimes struct nodelist *saveargbackq; 4461556Srgrimes char lastc; 4471556Srgrimes int startloc = dest - stackblock(); 4481556Srgrimes char const *syntax = quoted? DQSYNTAX : BASESYNTAX; 4491556Srgrimes int saveherefd; 4501556Srgrimes int quotes = flag & (EXP_FULL | EXP_CASE); 4511556Srgrimes 4521556Srgrimes INTOFF; 4531556Srgrimes saveifs = ifsfirst; 4541556Srgrimes savelastp = ifslastp; 4551556Srgrimes saveargbackq = argbackq; 4568855Srgrimes saveherefd = herefd; 4571556Srgrimes herefd = -1; 4581556Srgrimes p = grabstackstr(dest); 4591556Srgrimes evalbackcmd(cmd, &in); 4601556Srgrimes ungrabstackstr(p, dest); 4611556Srgrimes ifsfirst = saveifs; 4621556Srgrimes ifslastp = savelastp; 4631556Srgrimes argbackq = saveargbackq; 4641556Srgrimes herefd = saveherefd; 4651556Srgrimes 4661556Srgrimes p = in.buf; 4671556Srgrimes lastc = '\0'; 4681556Srgrimes for (;;) { 4691556Srgrimes if (--in.nleft < 0) { 4701556Srgrimes if (in.fd < 0) 4711556Srgrimes break; 4721556Srgrimes while ((i = read(in.fd, buf, sizeof buf)) < 0 && errno == EINTR); 4731556Srgrimes TRACE(("expbackq: read returns %d\n", i)); 4741556Srgrimes if (i <= 0) 4751556Srgrimes break; 4761556Srgrimes p = buf; 4771556Srgrimes in.nleft = i - 1; 4781556Srgrimes } 4791556Srgrimes lastc = *p++; 4801556Srgrimes if (lastc != '\0') { 4811556Srgrimes if (quotes && syntax[lastc] == CCTL) 4821556Srgrimes STPUTC(CTLESC, dest); 4831556Srgrimes STPUTC(lastc, dest); 4841556Srgrimes } 4851556Srgrimes } 48617987Speter 48717987Speter /* Eat all trailing newlines */ 48817987Speter for (p--; lastc == '\n'; lastc = *--p) 4891556Srgrimes STUNPUTC(dest); 49017987Speter 4911556Srgrimes if (in.fd >= 0) 4921556Srgrimes close(in.fd); 4931556Srgrimes if (in.buf) 4941556Srgrimes ckfree(in.buf); 4951556Srgrimes if (in.jp) 4961556Srgrimes exitstatus = waitforjob(in.jp); 4971556Srgrimes if (quoted == 0) 4981556Srgrimes recordregion(startloc, dest - stackblock(), 0); 4991556Srgrimes TRACE(("evalbackq: size=%d: \"%.*s\"\n", 5001556Srgrimes (dest - stackblock()) - startloc, 5011556Srgrimes (dest - stackblock()) - startloc, 5021556Srgrimes stackblock() + startloc)); 5031556Srgrimes expdest = dest; 5041556Srgrimes INTON; 5051556Srgrimes} 5061556Srgrimes 5071556Srgrimes 5081556Srgrimes 50917987SpeterSTATIC int 51020425Sstevesubevalvar(p, str, strloc, subtype, startloc, varflags) 51117987Speter char *p; 51217987Speter char *str; 51320425Ssteve int strloc; 51417987Speter int subtype; 51517987Speter int startloc; 51617987Speter int varflags; 51717987Speter{ 51817987Speter char *startp; 51917987Speter char *loc = NULL; 52017987Speter int c = 0; 52117987Speter int saveherefd = herefd; 52217987Speter struct nodelist *saveargbackq = argbackq; 52320425Ssteve int amount; 52420425Ssteve 52517987Speter herefd = -1; 52617987Speter argstr(p, 0); 52717987Speter STACKSTRNUL(expdest); 52817987Speter herefd = saveherefd; 52917987Speter argbackq = saveargbackq; 53017987Speter startp = stackblock() + startloc; 53120425Ssteve if (str == NULL) 53220425Ssteve str = stackblock() + strloc; 53317987Speter 53417987Speter switch (subtype) { 53517987Speter case VSASSIGN: 53617987Speter setvar(str, startp, 0); 53720425Ssteve amount = startp - expdest; 53820425Ssteve STADJUST(amount, expdest); 53917987Speter varflags &= ~VSNUL; 54017987Speter if (c != 0) 54117987Speter *loc = c; 54217987Speter return 1; 54317987Speter 54417987Speter case VSQUESTION: 54517987Speter if (*p != CTLENDVAR) { 54617987Speter outfmt(&errout, "%s\n", startp); 54717987Speter error((char *)NULL); 54817987Speter } 54917987Speter error("%.*s: parameter %snot set", p - str - 1, 55017987Speter str, (varflags & VSNUL) ? "null or " 55117987Speter : nullstr); 55217987Speter return 0; 55317987Speter 55417987Speter case VSTRIMLEFT: 55525233Ssteve for (loc = startp; loc < str; loc++) { 55617987Speter c = *loc; 55717987Speter *loc = '\0'; 55817987Speter if (patmatch(str, startp)) { 55917987Speter *loc = c; 56017987Speter goto recordleft; 56117987Speter } 56217987Speter *loc = c; 56317987Speter } 56417987Speter return 0; 56517987Speter 56617987Speter case VSTRIMLEFTMAX: 56717987Speter for (loc = str - 1; loc >= startp; loc--) { 56817987Speter c = *loc; 56917987Speter *loc = '\0'; 57017987Speter if (patmatch(str, startp)) { 57117987Speter *loc = c; 57217987Speter goto recordleft; 57317987Speter } 57417987Speter *loc = c; 57517987Speter } 57617987Speter return 0; 57717987Speter 57817987Speter case VSTRIMRIGHT: 57917987Speter for (loc = str - 1; loc >= startp; loc--) { 58017987Speter if (patmatch(str, loc)) { 58120425Ssteve amount = loc - expdest; 58220425Ssteve STADJUST(amount, expdest); 58317987Speter return 1; 58417987Speter } 58517987Speter } 58617987Speter return 0; 58717987Speter 58817987Speter case VSTRIMRIGHTMAX: 58917987Speter for (loc = startp; loc < str - 1; loc++) { 59017987Speter if (patmatch(str, loc)) { 59120425Ssteve amount = loc - expdest; 59220425Ssteve STADJUST(amount, expdest); 59317987Speter return 1; 59417987Speter } 59517987Speter } 59617987Speter return 0; 59717987Speter 59817987Speter 59917987Speter default: 60017987Speter abort(); 60117987Speter } 60217987Speter 60317987Speterrecordleft: 60420425Ssteve amount = ((str - 1) - (loc - startp)) - expdest; 60520425Ssteve STADJUST(amount, expdest); 60617987Speter while (loc != str - 1) 60717987Speter *startp++ = *loc++; 60817987Speter return 1; 60917987Speter} 61017987Speter 61117987Speter 6121556Srgrimes/* 6131556Srgrimes * Expand a variable, and return a pointer to the next character in the 6141556Srgrimes * input string. 6151556Srgrimes */ 6161556Srgrimes 6171556SrgrimesSTATIC char * 6181556Srgrimesevalvar(p, flag) 6191556Srgrimes char *p; 62017987Speter int flag; 62117987Speter{ 6221556Srgrimes int subtype; 6231556Srgrimes int varflags; 6241556Srgrimes char *var; 6251556Srgrimes char *val; 62617987Speter char *pat; 6271556Srgrimes int c; 6281556Srgrimes int set; 6291556Srgrimes int special; 6301556Srgrimes int startloc; 63117987Speter int varlen; 63217987Speter int easy; 6331556Srgrimes int quotes = flag & (EXP_FULL | EXP_CASE); 6341556Srgrimes 6351556Srgrimes varflags = *p++; 6361556Srgrimes subtype = varflags & VSTYPE; 6371556Srgrimes var = p; 6381556Srgrimes special = 0; 6391556Srgrimes if (! is_name(*p)) 6401556Srgrimes special = 1; 6411556Srgrimes p = strchr(p, '=') + 1; 6421556Srgrimesagain: /* jump here after setting a variable with ${var=text} */ 6431556Srgrimes if (special) { 64425233Ssteve set = varisset(var, varflags & VSNUL); 6451556Srgrimes val = NULL; 6461556Srgrimes } else { 6471556Srgrimes val = lookupvar(var); 64817987Speter if (val == NULL || ((varflags & VSNUL) && val[0] == '\0')) { 6491556Srgrimes val = NULL; 6501556Srgrimes set = 0; 6511556Srgrimes } else 6521556Srgrimes set = 1; 6531556Srgrimes } 65417987Speter varlen = 0; 6551556Srgrimes startloc = expdest - stackblock(); 6561556Srgrimes if (set && subtype != VSPLUS) { 6571556Srgrimes /* insert the value of the variable */ 6581556Srgrimes if (special) { 65918202Speter varvalue(var, varflags & VSQUOTE, flag & EXP_FULL); 66017987Speter if (subtype == VSLENGTH) { 66125233Ssteve varlen = expdest - stackblock() - startloc; 66225233Ssteve STADJUST(-varlen, expdest); 66317987Speter } 6641556Srgrimes } else { 66520425Ssteve char const *syntax = (varflags & VSQUOTE) ? DQSYNTAX 66617987Speter : BASESYNTAX; 6671556Srgrimes 66817987Speter if (subtype == VSLENGTH) { 66917987Speter for (;*val; val++) 67017987Speter varlen++; 6711556Srgrimes } 67217987Speter else { 67317987Speter while (*val) { 67417987Speter if (quotes && syntax[*val] == CCTL) 67517987Speter STPUTC(CTLESC, expdest); 67617987Speter STPUTC(*val++, expdest); 67717987Speter } 67817987Speter 67917987Speter } 6801556Srgrimes } 6811556Srgrimes } 68220425Ssteve 6831556Srgrimes if (subtype == VSPLUS) 6841556Srgrimes set = ! set; 68517987Speter 68620425Ssteve easy = ((varflags & VSQUOTE) == 0 || 68717987Speter (*var == '@' && shellparam.nparam != 1)); 68817987Speter 68917987Speter 69017987Speter switch (subtype) { 69117987Speter case VSLENGTH: 69217987Speter expdest = cvtnum(varlen, expdest); 69317987Speter goto record; 69417987Speter 69517987Speter case VSNORMAL: 69617987Speter if (!easy) 69717987Speter break; 69817987Speterrecord: 69920425Ssteve recordregion(startloc, expdest - stackblock(), 70017987Speter varflags & VSQUOTE); 70117987Speter break; 70217987Speter 70317987Speter case VSPLUS: 70417987Speter case VSMINUS: 70517987Speter if (!set) { 7061556Srgrimes argstr(p, flag); 70717987Speter break; 70817987Speter } 70917987Speter if (easy) 71017987Speter goto record; 71117987Speter break; 71217987Speter 71317987Speter case VSTRIMLEFT: 71417987Speter case VSTRIMLEFTMAX: 71517987Speter case VSTRIMRIGHT: 71617987Speter case VSTRIMRIGHTMAX: 71717987Speter if (!set) 71817987Speter break; 71917987Speter /* 72017987Speter * Terminate the string and start recording the pattern 72117987Speter * right after it 72217987Speter */ 72317987Speter STPUTC('\0', expdest); 72417987Speter pat = expdest; 72520425Ssteve if (subevalvar(p, NULL, expdest - stackblock(), subtype, 72638887Stegge startloc, varflags) == 0) { 72725233Ssteve int amount = (expdest - pat) + 1; 72825233Ssteve STADJUST(-amount, expdest); 72925233Ssteve } 73038887Stegge /* Remove any recorded regions beyond start of variable */ 73138887Stegge removerecordregions(startloc); 73238887Stegge goto record; 73317987Speter 73417987Speter case VSASSIGN: 73517987Speter case VSQUESTION: 73617987Speter if (!set) { 73720425Ssteve if (subevalvar(p, var, 0, subtype, startloc, varflags)) { 73820425Ssteve varflags &= ~VSNUL; 73938887Stegge /* 74038887Stegge * Remove any recorded regions beyond 74138887Stegge * start of variable 74238887Stegge */ 74338887Stegge removerecordregions(startloc); 7441556Srgrimes goto again; 74520425Ssteve } 74617987Speter break; 7471556Srgrimes } 74817987Speter if (easy) 74917987Speter goto record; 75017987Speter break; 75117987Speter 75217987Speter default: 75317987Speter abort(); 7541556Srgrimes } 75517987Speter 7561556Srgrimes if (subtype != VSNORMAL) { /* skip to end of alternative */ 7571556Srgrimes int nesting = 1; 7581556Srgrimes for (;;) { 7591556Srgrimes if ((c = *p++) == CTLESC) 7601556Srgrimes p++; 7611556Srgrimes else if (c == CTLBACKQ || c == (CTLBACKQ|CTLQUOTE)) { 7621556Srgrimes if (set) 7631556Srgrimes argbackq = argbackq->next; 7641556Srgrimes } else if (c == CTLVAR) { 7651556Srgrimes if ((*p++ & VSTYPE) != VSNORMAL) 7661556Srgrimes nesting++; 7671556Srgrimes } else if (c == CTLENDVAR) { 7681556Srgrimes if (--nesting == 0) 7691556Srgrimes break; 7701556Srgrimes } 7711556Srgrimes } 7721556Srgrimes } 7731556Srgrimes return p; 7741556Srgrimes} 7751556Srgrimes 7761556Srgrimes 7771556Srgrimes 7781556Srgrimes/* 7791556Srgrimes * Test whether a specialized variable is set. 7801556Srgrimes */ 7811556Srgrimes 7821556SrgrimesSTATIC int 78325233Sstevevarisset(name, nulok) 78418202Speter char *name; 78525233Ssteve int nulok; 78625233Ssteve{ 7871556Srgrimes 78825233Ssteve if (*name == '!') 78925233Ssteve return backgndpid != -1; 79025233Ssteve else if (*name == '@' || *name == '*') { 7911556Srgrimes if (*shellparam.p == NULL) 7921556Srgrimes return 0; 79325233Ssteve 79425233Ssteve if (nulok) { 79525233Ssteve char **av; 79625233Ssteve 79725233Ssteve for (av = shellparam.p; *av; av++) 79825233Ssteve if (**av != '\0') 79925233Ssteve return 1; 80025233Ssteve return 0; 80125233Ssteve } 80218202Speter } else if (is_digit(*name)) { 80325233Ssteve char *ap; 80420425Ssteve int num = atoi(name); 80525233Ssteve 80625233Ssteve if (num > shellparam.nparam) 80725233Ssteve return 0; 80825233Ssteve 80925233Ssteve if (num == 0) 81025233Ssteve ap = arg0; 81125233Ssteve else 81225233Ssteve ap = shellparam.p[num - 1]; 81325233Ssteve 81425233Ssteve if (nulok && (ap == NULL || *ap == '\0')) 81525233Ssteve return 0; 8161556Srgrimes } 8171556Srgrimes return 1; 8181556Srgrimes} 8191556Srgrimes 8201556Srgrimes 8211556Srgrimes 8221556Srgrimes/* 8231556Srgrimes * Add the value of a specialized variable to the stack string. 8241556Srgrimes */ 8251556Srgrimes 8261556SrgrimesSTATIC void 8271556Srgrimesvarvalue(name, quoted, allow_split) 82818202Speter char *name; 82917987Speter int quoted; 83017987Speter int allow_split; 83117987Speter{ 8321556Srgrimes int num; 8331556Srgrimes char *p; 8341556Srgrimes int i; 83517987Speter extern int oexitstatus; 8361556Srgrimes char sep; 8371556Srgrimes char **ap; 8381556Srgrimes char const *syntax; 8391556Srgrimes 8401556Srgrimes#define STRTODEST(p) \ 8411556Srgrimes do {\ 8421556Srgrimes if (allow_split) { \ 8431556Srgrimes syntax = quoted? DQSYNTAX : BASESYNTAX; \ 8441556Srgrimes while (*p) { \ 8451556Srgrimes if (syntax[*p] == CCTL) \ 8461556Srgrimes STPUTC(CTLESC, expdest); \ 8471556Srgrimes STPUTC(*p++, expdest); \ 8481556Srgrimes } \ 8491556Srgrimes } else \ 8501556Srgrimes while (*p) \ 8511556Srgrimes STPUTC(*p++, expdest); \ 8521556Srgrimes } while (0) 8531556Srgrimes 8541556Srgrimes 85518202Speter switch (*name) { 8561556Srgrimes case '$': 8571556Srgrimes num = rootpid; 8581556Srgrimes goto numvar; 8591556Srgrimes case '?': 86017987Speter num = oexitstatus; 8611556Srgrimes goto numvar; 8621556Srgrimes case '#': 8631556Srgrimes num = shellparam.nparam; 8641556Srgrimes goto numvar; 8651556Srgrimes case '!': 8661556Srgrimes num = backgndpid; 8671556Srgrimesnumvar: 86817987Speter expdest = cvtnum(num, expdest); 8691556Srgrimes break; 8701556Srgrimes case '-': 8711556Srgrimes for (i = 0 ; i < NOPTS ; i++) { 8721556Srgrimes if (optlist[i].val) 8731556Srgrimes STPUTC(optlist[i].letter, expdest); 8741556Srgrimes } 8751556Srgrimes break; 8761556Srgrimes case '@': 87738887Stegge if (allow_split && quoted) { 87838887Stegge for (ap = shellparam.p ; (p = *ap++) != NULL ; ) { 87938887Stegge STRTODEST(p); 88038887Stegge if (*ap) 88138887Stegge STPUTC('\0', expdest); 88238887Stegge } 88338887Stegge break; 8841556Srgrimes } 8858855Srgrimes /* fall through */ 8861556Srgrimes case '*': 88738887Stegge if (ifsset() != 0) 88838887Stegge sep = ifsval()[0]; 88938887Stegge else 89038887Stegge sep = ' '; 8911556Srgrimes for (ap = shellparam.p ; (p = *ap++) != NULL ; ) { 8921556Srgrimes STRTODEST(p); 89338887Stegge if (*ap && sep) 8941556Srgrimes STPUTC(sep, expdest); 8951556Srgrimes } 8961556Srgrimes break; 8971556Srgrimes case '0': 8981556Srgrimes p = arg0; 8991556Srgrimes STRTODEST(p); 9001556Srgrimes break; 9011556Srgrimes default: 90218202Speter if (is_digit(*name)) { 90318202Speter num = atoi(name); 90418202Speter if (num > 0 && num <= shellparam.nparam) { 90518202Speter p = shellparam.p[num - 1]; 90618202Speter STRTODEST(p); 90718202Speter } 9081556Srgrimes } 9091556Srgrimes break; 9101556Srgrimes } 9111556Srgrimes} 9121556Srgrimes 9131556Srgrimes 9141556Srgrimes 9151556Srgrimes/* 9161556Srgrimes * Record the the fact that we have to scan this region of the 9171556Srgrimes * string for IFS characters. 9181556Srgrimes */ 9191556Srgrimes 9201556SrgrimesSTATIC void 92120425Ssteverecordregion(start, end, nulonly) 92217987Speter int start; 92317987Speter int end; 92417987Speter int nulonly; 92517987Speter{ 92625233Ssteve struct ifsregion *ifsp; 9271556Srgrimes 9281556Srgrimes if (ifslastp == NULL) { 9291556Srgrimes ifsp = &ifsfirst; 9301556Srgrimes } else { 9311556Srgrimes ifsp = (struct ifsregion *)ckmalloc(sizeof (struct ifsregion)); 9321556Srgrimes ifslastp->next = ifsp; 9331556Srgrimes } 9341556Srgrimes ifslastp = ifsp; 9351556Srgrimes ifslastp->next = NULL; 9361556Srgrimes ifslastp->begoff = start; 9371556Srgrimes ifslastp->endoff = end; 9381556Srgrimes ifslastp->nulonly = nulonly; 9391556Srgrimes} 9401556Srgrimes 9411556Srgrimes 9421556Srgrimes 9431556Srgrimes/* 9441556Srgrimes * Break the argument string into pieces based upon IFS and add the 9451556Srgrimes * strings to the argument list. The regions of the string to be 9461556Srgrimes * searched for IFS characters have been stored by recordregion. 9471556Srgrimes */ 9481556SrgrimesSTATIC void 9491556Srgrimesifsbreakup(string, arglist) 9501556Srgrimes char *string; 9511556Srgrimes struct arglist *arglist; 9521556Srgrimes { 9531556Srgrimes struct ifsregion *ifsp; 9541556Srgrimes struct strlist *sp; 9551556Srgrimes char *start; 95625233Ssteve char *p; 9571556Srgrimes char *q; 9581556Srgrimes char *ifs; 95917987Speter int ifsspc; 96038887Stegge int nulonly; 9611556Srgrimes 96217987Speter 9631556Srgrimes start = string; 96438887Stegge ifsspc = 0; 96538887Stegge nulonly = 0; 9661556Srgrimes if (ifslastp != NULL) { 9671556Srgrimes ifsp = &ifsfirst; 9681556Srgrimes do { 9691556Srgrimes p = string + ifsp->begoff; 97038887Stegge nulonly = ifsp->nulonly; 97138887Stegge ifs = nulonly ? nullstr : 97238887Stegge ( ifsset() ? ifsval() : " \t\n" ); 97338887Stegge ifsspc = 0; 9741556Srgrimes while (p < string + ifsp->endoff) { 9751556Srgrimes q = p; 9761556Srgrimes if (*p == CTLESC) 9771556Srgrimes p++; 97838887Stegge if (strchr(ifs, *p)) { 97938887Stegge if (!nulonly) 98038887Stegge ifsspc = (strchr(" \t\n", *p) != NULL); 98138887Stegge /* Ignore IFS whitespace at start */ 98238887Stegge if (q == start && ifsspc) { 98338887Stegge p++; 98438887Stegge start = p; 98538887Stegge continue; 9861556Srgrimes } 98738887Stegge *q = '\0'; 98838887Stegge sp = (struct strlist *)stalloc(sizeof *sp); 98938887Stegge sp->text = start; 99038887Stegge *arglist->lastp = sp; 99138887Stegge arglist->lastp = &sp->next; 99238887Stegge p++; 99338887Stegge if (!nulonly) { 9941556Srgrimes for (;;) { 99538887Stegge if (p >= string + ifsp->endoff) { 9961556Srgrimes break; 99738887Stegge } 9981556Srgrimes q = p; 9991556Srgrimes if (*p == CTLESC) 10001556Srgrimes p++; 100138887Stegge if (strchr(ifs, *p) == NULL ) { 10021556Srgrimes p = q; 10031556Srgrimes break; 100438887Stegge } else if (strchr(" \t\n",*p) == NULL) { 100538887Stegge if (ifsspc) { 100638887Stegge p++; 100738887Stegge ifsspc = 0; 100838887Stegge } else { 100938887Stegge p = q; 101038887Stegge break; 101138887Stegge } 101238887Stegge } else 101338887Stegge p++; 10141556Srgrimes } 10151556Srgrimes } 10161556Srgrimes start = p; 101738887Stegge } else 101838887Stegge p++; 10191556Srgrimes } 10201556Srgrimes } while ((ifsp = ifsp->next) != NULL); 102138887Stegge if (*start || (!ifsspc && start > string && 102238887Stegge (nulonly || 1))) { 10231556Srgrimes sp = (struct strlist *)stalloc(sizeof *sp); 10241556Srgrimes sp->text = start; 10251556Srgrimes *arglist->lastp = sp; 10261556Srgrimes arglist->lastp = &sp->next; 10271556Srgrimes } 10281556Srgrimes } else { 10291556Srgrimes sp = (struct strlist *)stalloc(sizeof *sp); 10301556Srgrimes sp->text = start; 10311556Srgrimes *arglist->lastp = sp; 10321556Srgrimes arglist->lastp = &sp->next; 10331556Srgrimes } 10341556Srgrimes} 10351556Srgrimes 10361556Srgrimes 10371556Srgrimes 10381556Srgrimes/* 10391556Srgrimes * Expand shell metacharacters. At this point, the only control characters 10401556Srgrimes * should be escapes. The results are stored in the list exparg. 10411556Srgrimes */ 10421556Srgrimes 10431556Srgrimeschar *expdir; 10441556Srgrimes 10451556Srgrimes 10461556SrgrimesSTATIC void 10471556Srgrimesexpandmeta(str, flag) 10481556Srgrimes struct strlist *str; 104925905Ssteve int flag __unused; 105017987Speter{ 10511556Srgrimes char *p; 10521556Srgrimes struct strlist **savelastp; 10531556Srgrimes struct strlist *sp; 10541556Srgrimes char c; 10551556Srgrimes /* TODO - EXP_REDIR */ 10561556Srgrimes 10571556Srgrimes while (str) { 10581556Srgrimes if (fflag) 10591556Srgrimes goto nometa; 10601556Srgrimes p = str->text; 10611556Srgrimes for (;;) { /* fast check for meta chars */ 10621556Srgrimes if ((c = *p++) == '\0') 10631556Srgrimes goto nometa; 10641556Srgrimes if (c == '*' || c == '?' || c == '[' || c == '!') 10651556Srgrimes break; 10661556Srgrimes } 10671556Srgrimes savelastp = exparg.lastp; 10681556Srgrimes INTOFF; 10691556Srgrimes if (expdir == NULL) { 10701556Srgrimes int i = strlen(str->text); 10711556Srgrimes expdir = ckmalloc(i < 2048 ? 2048 : i); /* XXX */ 10721556Srgrimes } 10731556Srgrimes 10741556Srgrimes expmeta(expdir, str->text); 10751556Srgrimes ckfree(expdir); 10761556Srgrimes expdir = NULL; 10771556Srgrimes INTON; 10781556Srgrimes if (exparg.lastp == savelastp) { 10798855Srgrimes /* 10808855Srgrimes * no matches 10811556Srgrimes */ 10821556Srgrimesnometa: 10831556Srgrimes *exparg.lastp = str; 10841556Srgrimes rmescapes(str->text); 10851556Srgrimes exparg.lastp = &str->next; 10861556Srgrimes } else { 10871556Srgrimes *exparg.lastp = NULL; 10881556Srgrimes *savelastp = sp = expsort(*savelastp); 10891556Srgrimes while (sp->next != NULL) 10901556Srgrimes sp = sp->next; 10911556Srgrimes exparg.lastp = &sp->next; 10921556Srgrimes } 10931556Srgrimes str = str->next; 10941556Srgrimes } 10951556Srgrimes} 10961556Srgrimes 10971556Srgrimes 10981556Srgrimes/* 10991556Srgrimes * Do metacharacter (i.e. *, ?, [...]) expansion. 11001556Srgrimes */ 11011556Srgrimes 11021556SrgrimesSTATIC void 11031556Srgrimesexpmeta(enddir, name) 11041556Srgrimes char *enddir; 11051556Srgrimes char *name; 11061556Srgrimes { 110725233Ssteve char *p; 11081556Srgrimes char *q; 11091556Srgrimes char *start; 11101556Srgrimes char *endname; 11111556Srgrimes int metaflag; 11121556Srgrimes struct stat statb; 11131556Srgrimes DIR *dirp; 11141556Srgrimes struct dirent *dp; 11151556Srgrimes int atend; 11161556Srgrimes int matchdot; 11171556Srgrimes 11181556Srgrimes metaflag = 0; 11191556Srgrimes start = name; 11201556Srgrimes for (p = name ; ; p++) { 11211556Srgrimes if (*p == '*' || *p == '?') 11221556Srgrimes metaflag = 1; 11231556Srgrimes else if (*p == '[') { 11241556Srgrimes q = p + 1; 112526488Sache if (*q == '!' || *q == '^') 11261556Srgrimes q++; 11271556Srgrimes for (;;) { 112838887Stegge while (*q == CTLQUOTEMARK) 112938887Stegge q++; 11301556Srgrimes if (*q == CTLESC) 11311556Srgrimes q++; 11321556Srgrimes if (*q == '/' || *q == '\0') 11331556Srgrimes break; 11341556Srgrimes if (*++q == ']') { 11351556Srgrimes metaflag = 1; 11361556Srgrimes break; 11371556Srgrimes } 11381556Srgrimes } 11391556Srgrimes } else if (*p == '!' && p[1] == '!' && (p == name || p[-1] == '/')) { 11401556Srgrimes metaflag = 1; 11411556Srgrimes } else if (*p == '\0') 11421556Srgrimes break; 114338887Stegge else if (*p == CTLQUOTEMARK) 114438887Stegge continue; 11451556Srgrimes else if (*p == CTLESC) 11461556Srgrimes p++; 11471556Srgrimes if (*p == '/') { 11481556Srgrimes if (metaflag) 11491556Srgrimes break; 11501556Srgrimes start = p + 1; 11511556Srgrimes } 11521556Srgrimes } 11531556Srgrimes if (metaflag == 0) { /* we've reached the end of the file name */ 11541556Srgrimes if (enddir != expdir) 11551556Srgrimes metaflag++; 11561556Srgrimes for (p = name ; ; p++) { 115738887Stegge if (*p == CTLQUOTEMARK) 115838887Stegge continue; 11591556Srgrimes if (*p == CTLESC) 11601556Srgrimes p++; 11611556Srgrimes *enddir++ = *p; 11621556Srgrimes if (*p == '\0') 11631556Srgrimes break; 11641556Srgrimes } 11651556Srgrimes if (metaflag == 0 || stat(expdir, &statb) >= 0) 11661556Srgrimes addfname(expdir); 11671556Srgrimes return; 11681556Srgrimes } 11691556Srgrimes endname = p; 11701556Srgrimes if (start != name) { 11711556Srgrimes p = name; 11721556Srgrimes while (p < start) { 117338887Stegge while (*p == CTLQUOTEMARK) 117438887Stegge p++; 11751556Srgrimes if (*p == CTLESC) 11761556Srgrimes p++; 11771556Srgrimes *enddir++ = *p++; 11781556Srgrimes } 11791556Srgrimes } 11801556Srgrimes if (enddir == expdir) { 11811556Srgrimes p = "."; 11821556Srgrimes } else if (enddir == expdir + 1 && *expdir == '/') { 11831556Srgrimes p = "/"; 11841556Srgrimes } else { 11851556Srgrimes p = expdir; 11861556Srgrimes enddir[-1] = '\0'; 11871556Srgrimes } 11881556Srgrimes if ((dirp = opendir(p)) == NULL) 11891556Srgrimes return; 11901556Srgrimes if (enddir != expdir) 11911556Srgrimes enddir[-1] = '/'; 11921556Srgrimes if (*endname == 0) { 11931556Srgrimes atend = 1; 11941556Srgrimes } else { 11951556Srgrimes atend = 0; 11961556Srgrimes *endname++ = '\0'; 11971556Srgrimes } 11981556Srgrimes matchdot = 0; 119938887Stegge p = start; 120038887Stegge while (*p == CTLQUOTEMARK) 120138887Stegge p++; 120238887Stegge if (*p == CTLESC) 120338887Stegge p++; 120438887Stegge if (*p == '.') 12051556Srgrimes matchdot++; 12061556Srgrimes while (! int_pending() && (dp = readdir(dirp)) != NULL) { 12071556Srgrimes if (dp->d_name[0] == '.' && ! matchdot) 12081556Srgrimes continue; 12091556Srgrimes if (patmatch(start, dp->d_name)) { 12101556Srgrimes if (atend) { 12111556Srgrimes scopy(dp->d_name, enddir); 12121556Srgrimes addfname(expdir); 12131556Srgrimes } else { 12141556Srgrimes char *q; 121517987Speter for (p = enddir, q = dp->d_name; 121617987Speter (*p++ = *q++) != '\0';) 121717987Speter continue; 12181556Srgrimes p[-1] = '/'; 12191556Srgrimes expmeta(p, endname); 12201556Srgrimes } 12211556Srgrimes } 12221556Srgrimes } 12231556Srgrimes closedir(dirp); 12241556Srgrimes if (! atend) 12251556Srgrimes endname[-1] = '/'; 12261556Srgrimes} 12271556Srgrimes 12281556Srgrimes 12291556Srgrimes/* 12301556Srgrimes * Add a file name to the list. 12311556Srgrimes */ 12321556Srgrimes 12331556SrgrimesSTATIC void 12341556Srgrimesaddfname(name) 12351556Srgrimes char *name; 12361556Srgrimes { 12371556Srgrimes char *p; 12381556Srgrimes struct strlist *sp; 12391556Srgrimes 12401556Srgrimes p = stalloc(strlen(name) + 1); 12411556Srgrimes scopy(name, p); 12421556Srgrimes sp = (struct strlist *)stalloc(sizeof *sp); 12431556Srgrimes sp->text = p; 12441556Srgrimes *exparg.lastp = sp; 12451556Srgrimes exparg.lastp = &sp->next; 12461556Srgrimes} 12471556Srgrimes 12481556Srgrimes 12491556Srgrimes/* 12501556Srgrimes * Sort the results of file name expansion. It calculates the number of 12511556Srgrimes * strings to sort and then calls msort (short for merge sort) to do the 12521556Srgrimes * work. 12531556Srgrimes */ 12541556Srgrimes 12551556SrgrimesSTATIC struct strlist * 12561556Srgrimesexpsort(str) 12571556Srgrimes struct strlist *str; 12581556Srgrimes { 12591556Srgrimes int len; 12601556Srgrimes struct strlist *sp; 12611556Srgrimes 12621556Srgrimes len = 0; 12631556Srgrimes for (sp = str ; sp ; sp = sp->next) 12641556Srgrimes len++; 12651556Srgrimes return msort(str, len); 12661556Srgrimes} 12671556Srgrimes 12681556Srgrimes 12691556SrgrimesSTATIC struct strlist * 12701556Srgrimesmsort(list, len) 12711556Srgrimes struct strlist *list; 127217987Speter int len; 127317987Speter{ 127417987Speter struct strlist *p, *q = NULL; 12751556Srgrimes struct strlist **lpp; 12761556Srgrimes int half; 12771556Srgrimes int n; 12781556Srgrimes 12791556Srgrimes if (len <= 1) 12801556Srgrimes return list; 12818855Srgrimes half = len >> 1; 12821556Srgrimes p = list; 12831556Srgrimes for (n = half ; --n >= 0 ; ) { 12841556Srgrimes q = p; 12851556Srgrimes p = p->next; 12861556Srgrimes } 12871556Srgrimes q->next = NULL; /* terminate first half of list */ 12881556Srgrimes q = msort(list, half); /* sort first half of list */ 12891556Srgrimes p = msort(p, len - half); /* sort second half */ 12901556Srgrimes lpp = &list; 12911556Srgrimes for (;;) { 12921556Srgrimes if (strcmp(p->text, q->text) < 0) { 12931556Srgrimes *lpp = p; 12941556Srgrimes lpp = &p->next; 12951556Srgrimes if ((p = *lpp) == NULL) { 12961556Srgrimes *lpp = q; 12971556Srgrimes break; 12981556Srgrimes } 12991556Srgrimes } else { 13001556Srgrimes *lpp = q; 13011556Srgrimes lpp = &q->next; 13021556Srgrimes if ((q = *lpp) == NULL) { 13031556Srgrimes *lpp = p; 13041556Srgrimes break; 13051556Srgrimes } 13061556Srgrimes } 13071556Srgrimes } 13081556Srgrimes return list; 13091556Srgrimes} 13101556Srgrimes 13111556Srgrimes 13121556Srgrimes 13131556Srgrimes/* 13141556Srgrimes * Returns true if the pattern matches the string. 13151556Srgrimes */ 13161556Srgrimes 13171556Srgrimesint 13181556Srgrimespatmatch(pattern, string) 13191556Srgrimes char *pattern; 13201556Srgrimes char *string; 13211556Srgrimes { 13221556Srgrimes#ifdef notdef 13231556Srgrimes if (pattern[0] == '!' && pattern[1] == '!') 13241556Srgrimes return 1 - pmatch(pattern + 2, string); 13251556Srgrimes else 13261556Srgrimes#endif 13271556Srgrimes return pmatch(pattern, string); 13281556Srgrimes} 13291556Srgrimes 133017987Speter 133117525SacheSTATIC int 13321556Srgrimespmatch(pattern, string) 13331556Srgrimes char *pattern; 13341556Srgrimes char *string; 13351556Srgrimes { 133625233Ssteve char *p, *q; 133725233Ssteve char c; 13381556Srgrimes 13391556Srgrimes p = pattern; 13401556Srgrimes q = string; 13411556Srgrimes for (;;) { 13421556Srgrimes switch (c = *p++) { 13431556Srgrimes case '\0': 13441556Srgrimes goto breakloop; 13451556Srgrimes case CTLESC: 13461556Srgrimes if (*q++ != *p++) 13471556Srgrimes return 0; 13481556Srgrimes break; 134938887Stegge case CTLQUOTEMARK: 135038887Stegge continue; 13511556Srgrimes case '?': 13521556Srgrimes if (*q++ == '\0') 13531556Srgrimes return 0; 13541556Srgrimes break; 13551556Srgrimes case '*': 13561556Srgrimes c = *p; 135738887Stegge while (c == CTLQUOTEMARK || c == '*') 135838887Stegge c = *++p; 135938887Stegge if (c != CTLESC && c != CTLQUOTEMARK && 136038887Stegge c != '?' && c != '*' && c != '[') { 13611556Srgrimes while (*q != c) { 13621556Srgrimes if (*q == '\0') 13631556Srgrimes return 0; 13641556Srgrimes q++; 13651556Srgrimes } 13661556Srgrimes } 13671556Srgrimes do { 13681556Srgrimes if (pmatch(p, q)) 13691556Srgrimes return 1; 13701556Srgrimes } while (*q++ != '\0'); 13711556Srgrimes return 0; 13721556Srgrimes case '[': { 13731556Srgrimes char *endp; 13741556Srgrimes int invert, found; 13751556Srgrimes char chr; 13761556Srgrimes 13771556Srgrimes endp = p; 137826488Sache if (*endp == '!' || *endp == '^') 13791556Srgrimes endp++; 13801556Srgrimes for (;;) { 138138887Stegge while (*endp == CTLQUOTEMARK) 138238887Stegge endp++; 13831556Srgrimes if (*endp == '\0') 13841556Srgrimes goto dft; /* no matching ] */ 13851556Srgrimes if (*endp == CTLESC) 13861556Srgrimes endp++; 13871556Srgrimes if (*++endp == ']') 13881556Srgrimes break; 13891556Srgrimes } 13901556Srgrimes invert = 0; 139126488Sache if (*p == '!' || *p == '^') { 13921556Srgrimes invert++; 13931556Srgrimes p++; 13941556Srgrimes } 13951556Srgrimes found = 0; 13961556Srgrimes chr = *q++; 139717987Speter if (chr == '\0') 139817987Speter return 0; 13991556Srgrimes c = *p++; 14001556Srgrimes do { 140138887Stegge if (c == CTLQUOTEMARK) 140238887Stegge continue; 14031556Srgrimes if (c == CTLESC) 14041556Srgrimes c = *p++; 14051556Srgrimes if (*p == '-' && p[1] != ']') { 14061556Srgrimes p++; 140738887Stegge while (*p == CTLQUOTEMARK) 140838887Stegge p++; 14091556Srgrimes if (*p == CTLESC) 14101556Srgrimes p++; 141117557Sache if ( collate_range_cmp(chr, c) >= 0 141217557Sache && collate_range_cmp(chr, *p) <= 0 141317525Sache ) 14141556Srgrimes found = 1; 14151556Srgrimes p++; 14161556Srgrimes } else { 14171556Srgrimes if (chr == c) 14181556Srgrimes found = 1; 14191556Srgrimes } 14201556Srgrimes } while ((c = *p++) != ']'); 14211556Srgrimes if (found == invert) 14221556Srgrimes return 0; 14231556Srgrimes break; 14241556Srgrimes } 14251556Srgrimesdft: default: 14261556Srgrimes if (*q++ != c) 14271556Srgrimes return 0; 14281556Srgrimes break; 14291556Srgrimes } 14301556Srgrimes } 14311556Srgrimesbreakloop: 14321556Srgrimes if (*q != '\0') 14331556Srgrimes return 0; 14341556Srgrimes return 1; 14351556Srgrimes} 14361556Srgrimes 14371556Srgrimes 14381556Srgrimes 14391556Srgrimes/* 14401556Srgrimes * Remove any CTLESC characters from a string. 14411556Srgrimes */ 14421556Srgrimes 14431556Srgrimesvoid 14441556Srgrimesrmescapes(str) 14451556Srgrimes char *str; 144638887Stegge{ 144725233Ssteve char *p, *q; 14481556Srgrimes 14491556Srgrimes p = str; 145038887Stegge while (*p != CTLESC && *p != CTLQUOTEMARK) { 14511556Srgrimes if (*p++ == '\0') 14521556Srgrimes return; 14531556Srgrimes } 14541556Srgrimes q = p; 14551556Srgrimes while (*p) { 145638887Stegge if (*p == CTLQUOTEMARK) { 145738887Stegge p++; 145838887Stegge continue; 145938887Stegge } 14601556Srgrimes if (*p == CTLESC) 14611556Srgrimes p++; 14621556Srgrimes *q++ = *p++; 14631556Srgrimes } 14641556Srgrimes *q = '\0'; 14651556Srgrimes} 14661556Srgrimes 14671556Srgrimes 14681556Srgrimes 14691556Srgrimes/* 14701556Srgrimes * See if a pattern matches in a case statement. 14711556Srgrimes */ 14721556Srgrimes 14731556Srgrimesint 14741556Srgrimescasematch(pattern, val) 14751556Srgrimes union node *pattern; 14761556Srgrimes char *val; 14771556Srgrimes { 14781556Srgrimes struct stackmark smark; 14791556Srgrimes int result; 14801556Srgrimes char *p; 14811556Srgrimes 14821556Srgrimes setstackmark(&smark); 14831556Srgrimes argbackq = pattern->narg.backquote; 14841556Srgrimes STARTSTACKSTR(expdest); 14851556Srgrimes ifslastp = NULL; 14861556Srgrimes argstr(pattern->narg.text, EXP_TILDE | EXP_CASE); 14871556Srgrimes STPUTC('\0', expdest); 14881556Srgrimes p = grabstackstr(expdest); 14891556Srgrimes result = patmatch(p, val); 14901556Srgrimes popstackmark(&smark); 14911556Srgrimes return result; 14921556Srgrimes} 149317987Speter 149417987Speter/* 149517987Speter * Our own itoa(). 149617987Speter */ 149717987Speter 149817987SpeterSTATIC char * 149917987Spetercvtnum(num, buf) 150017987Speter int num; 150117987Speter char *buf; 150217987Speter { 150317987Speter char temp[32]; 150417987Speter int neg = num < 0; 150517987Speter char *p = temp + 31; 150617987Speter 150717987Speter temp[31] = '\0'; 150817987Speter 150917987Speter do { 151017987Speter *--p = num % 10 + '0'; 151117987Speter } while ((num /= 10) != 0); 151217987Speter 151317987Speter if (neg) 151417987Speter *--p = '-'; 151517987Speter 151617987Speter while (*p) 151717987Speter STPUTC(*p++, buf); 151817987Speter return buf; 151917987Speter} 1520