2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1997-2005
5 * Herbert Xu <herbert@gondor.apana.org.au>. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 static char sccsid[] = "@(#)expand.c 8.5 (Berkeley) 5/15/95";
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
43 #include <sys/types.h>
56 * Routines to expand arguments to commands. We have to deal with
57 * backquotes, shell variables, and file metacharacters.
79 * Structure specifying which parts of the string should be searched
84 struct ifsregion *next; /* next region in list */
85 int begoff; /* offset of start of region */
86 int endoff; /* offset of end of region */
87 int inquotes; /* search for nul bytes only */
91 static char *expdest; /* output of current string */
92 static struct nodelist *argbackq; /* list of back quote expressions */
93 static struct ifsregion ifsfirst; /* first struct in list of ifs regions */
94 static struct ifsregion *ifslastp; /* last struct in list */
95 static struct arglist exparg; /* holds expanded arg list */
97 static void argstr(char *, int);
98 static char *exptilde(char *, int);
99 static void expbackq(union node *, int, int);
100 static int subevalvar(char *, char *, int, int, int, int);
101 static char *evalvar(char *, int);
102 static int varisset(char *, int);
103 static void varvalue(char *, int, int, int);
104 static void recordregion(int, int, int);
105 static void removerecordregions(int);
106 static void ifsbreakup(char *, struct arglist *);
107 static void expandmeta(struct strlist *, int);
108 static void expmeta(char *, char *);
109 static void addfname(char *);
110 static struct strlist *expsort(struct strlist *);
111 static struct strlist *msort(struct strlist *, int);
112 static int pmatch(const char *, const char *, int);
113 static char *cvtnum(int, char *);
114 static int collate_range_cmp(int, int);
117 collate_range_cmp(int c1, int c2)
119 static char s1[2], s2[2];
123 return (strcoll(s1, s2));
127 * Expand shell variables and backquotes inside a here document.
128 * union node *arg the document
129 * int fd; where to write the expanded version
133 expandhere(union node *arg, int fd)
136 expandarg(arg, (struct arglist *)NULL, 0);
137 xwrite(fd, stackblock(), expdest - stackblock());
142 * Perform expansions on an argument, placing the resulting list of arguments
143 * in arglist. Parameter expansion, command substitution and arithmetic
144 * expansion are always performed; additional expansions can be requested
146 * The result is left in the stack string.
147 * When arglist is NULL, perform here document expansion. A partial result
148 * may be written to herefd, which is then not included in the stack string.
150 * Caution: this function uses global state and is not reentrant.
151 * However, a new invocation after an interrupted invocation is safe
152 * and will reset the global state for the new call.
155 expandarg(union node *arg, struct arglist *arglist, int flag)
160 argbackq = arg->narg.backquote;
161 STARTSTACKSTR(expdest);
162 ifsfirst.next = NULL;
164 argstr(arg->narg.text, flag);
165 if (arglist == NULL) {
166 return; /* here document expanded */
168 STPUTC('\0', expdest);
169 p = grabstackstr(expdest);
170 exparg.lastp = &exparg.list;
174 if (flag & EXP_FULL) {
175 ifsbreakup(p, &exparg);
176 *exparg.lastp = NULL;
177 exparg.lastp = &exparg.list;
178 expandmeta(exparg.list, flag);
180 if (flag & EXP_REDIR) /*XXX - for now, just remove escapes */
182 sp = (struct strlist *)stalloc(sizeof (struct strlist));
185 exparg.lastp = &sp->next;
187 while (ifsfirst.next != NULL) {
188 struct ifsregion *ifsp;
190 ifsp = ifsfirst.next->next;
191 ckfree(ifsfirst.next);
192 ifsfirst.next = ifsp;
195 *exparg.lastp = NULL;
197 *arglist->lastp = exparg.list;
198 arglist->lastp = exparg.lastp;
205 * Perform parameter expansion, command substitution and arithmetic
206 * expansion, and tilde expansion if requested via EXP_TILDE/EXP_VARTILDE.
207 * Processing ends at a CTLENDVAR character as well as '\0'.
208 * This is used to expand word in ${var+word} etc.
209 * If EXP_FULL, EXP_CASE or EXP_REDIR are set, keep and/or generate CTLESC
210 * characters to allow for further processing.
211 * If EXP_FULL is set, also preserve CTLQUOTEMARK characters.
214 argstr(char *p, int flag)
217 int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR); /* do CTLESC */
220 if (*p == '~' && (flag & (EXP_TILDE | EXP_VARTILDE)))
221 p = exptilde(p, flag);
228 /* "$@" syntax adherence hack */
229 if (p[0] == CTLVAR && p[2] == '@' && p[3] == '=')
231 if ((flag & EXP_FULL) != 0)
241 p = evalvar(p, flag);
244 case CTLBACKQ|CTLQUOTE:
245 expbackq(argbackq->n, c & CTLQUOTE, flag);
246 argbackq = argbackq->next;
254 * sort of a hack - expand tildes in variable
255 * assignments (after the first '=' and after ':'s).
258 if (flag & EXP_VARTILDE && *p == '~') {
265 p = exptilde(p, flag);
276 * Perform tilde expansion, placing the result in the stack string and
277 * returning the next position in the input string to process.
280 exptilde(char *p, int flag)
285 int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR);
287 while ((c = *p) != '\0') {
294 if (flag & EXP_VARTILDE)
304 if (*(startp+1) == '\0') {
305 if ((home = lookupvar("HOME")) == NULL)
308 if ((pw = getpwnam(startp+1)) == NULL)
315 while ((c = *home++) != '\0') {
316 if (quotes && SQSYNTAX[(int)c] == CCTL)
317 STPUTC(CTLESC, expdest);
328 removerecordregions(int endoff)
330 if (ifslastp == NULL)
333 if (ifsfirst.endoff > endoff) {
334 while (ifsfirst.next != NULL) {
335 struct ifsregion *ifsp;
337 ifsp = ifsfirst.next->next;
338 ckfree(ifsfirst.next);
339 ifsfirst.next = ifsp;
342 if (ifsfirst.begoff > endoff)
345 ifslastp = &ifsfirst;
346 ifsfirst.endoff = endoff;
351 ifslastp = &ifsfirst;
352 while (ifslastp->next && ifslastp->next->begoff < endoff)
353 ifslastp=ifslastp->next;
354 while (ifslastp->next != NULL) {
355 struct ifsregion *ifsp;
357 ifsp = ifslastp->next->next;
358 ckfree(ifslastp->next);
359 ifslastp->next = ifsp;
362 if (ifslastp->endoff > endoff)
363 ifslastp->endoff = endoff;
367 * Expand arithmetic expression. Backup to start of expression,
368 * evaluate, place result in (backed up) result, adjust string position.
376 int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR);
380 * This routine is slightly over-complicated for
381 * efficiency. First we make sure there is
382 * enough space for the result, which may be bigger
383 * than the expression. Next we
384 * scan backwards looking for the start of arithmetic. If the
385 * next previous character is a CTLESC character, then we
386 * have to rescan starting from the beginning since CTLESC
387 * characters have to be processed left to right.
389 CHECKSTRSPACE(DIGITS(result) - 2, expdest);
390 USTPUTC('\0', expdest);
391 start = stackblock();
393 while (p >= start && *p != CTLARI)
395 if (p < start || *p != CTLARI)
396 error("missing CTLARI (shouldn't happen)");
397 if (p > start && *(p - 1) == CTLESC)
398 for (p = start; *p != CTLARI; p++)
407 removerecordregions(begoff);
411 fmtstr(p, DIGITS(result), ARITH_FORMAT_STR, result);
415 recordregion(begoff, p - 1 - start, 0);
416 result = expdest - p + 1;
417 STADJUST(-result, expdest);
422 * Perform command substitution.
425 expbackq(union node *cmd, int quoted, int flag)
431 char *dest = expdest;
432 struct ifsregion saveifs, *savelastp;
433 struct nodelist *saveargbackq;
435 int startloc = dest - stackblock();
436 char const *syntax = quoted? DQSYNTAX : BASESYNTAX;
438 int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR);
443 savelastp = ifslastp;
444 saveargbackq = argbackq;
447 p = grabstackstr(dest);
448 evalbackcmd(cmd, &in);
449 ungrabstackstr(p, dest);
451 ifslastp = savelastp;
452 argbackq = saveargbackq;
458 /* Don't copy trailing newlines */
460 if (--in.nleft < 0) {
463 while ((i = read(in.fd, buf, sizeof buf)) < 0 && errno == EINTR);
464 TRACE(("expbackq: read returns %d\n", i));
472 if (quotes && syntax[(int)lastc] == CCTL)
473 STPUTC(CTLESC, dest);
491 exitstatus = waitforjob(in.jp, (int *)NULL);
493 recordregion(startloc, dest - stackblock(), 0);
494 TRACE(("evalbackq: size=%d: \"%.*s\"\n",
495 (dest - stackblock()) - startloc,
496 (dest - stackblock()) - startloc,
497 stackblock() + startloc));
505 subevalvar(char *p, char *str, int strloc, int subtype, int startloc,
512 int saveherefd = herefd;
513 struct nodelist *saveargbackq = argbackq;
518 STACKSTRNUL(expdest);
520 argbackq = saveargbackq;
521 startp = stackblock() + startloc;
523 str = stackblock() + strloc;
527 setvar(str, startp, 0);
528 amount = startp - expdest;
529 STADJUST(amount, expdest);
536 if (*p != CTLENDVAR) {
537 outfmt(&errout, "%s\n", startp);
540 error("%.*s: parameter %snot set", (int)(p - str - 1),
541 str, (varflags & VSNUL) ? "null or "
546 for (loc = startp; loc < str; loc++) {
549 if (patmatch(str, startp, varflags & VSQUOTE)) {
554 if ((varflags & VSQUOTE) && *loc == CTLESC)
560 for (loc = str - 1; loc >= startp;) {
563 if (patmatch(str, startp, varflags & VSQUOTE)) {
569 if ((varflags & VSQUOTE) && loc > startp &&
570 *(loc - 1) == CTLESC) {
571 for (q = startp; q < loc; q++)
581 for (loc = str - 1; loc >= startp;) {
582 if (patmatch(str, loc, varflags & VSQUOTE)) {
583 amount = loc - expdest;
584 STADJUST(amount, expdest);
588 if ((varflags & VSQUOTE) && loc > startp &&
589 *(loc - 1) == CTLESC) {
590 for (q = startp; q < loc; q++)
600 for (loc = startp; loc < str - 1; loc++) {
601 if (patmatch(str, loc, varflags & VSQUOTE)) {
602 amount = loc - expdest;
603 STADJUST(amount, expdest);
606 if ((varflags & VSQUOTE) && *loc == CTLESC)
617 amount = ((str - 1) - (loc - startp)) - expdest;
618 STADJUST(amount, expdest);
619 while (loc != str - 1)
626 * Expand a variable, and return a pointer to the next character in the
631 evalvar(char *p, int flag)
644 int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR);
646 varflags = (unsigned char)*p++;
647 subtype = varflags & VSTYPE;
652 p = strchr(p, '=') + 1;
653 again: /* jump here after setting a variable with ${var=text} */
654 if (varflags & VSLINENO) {
658 p[-1] = '\0'; /* temporarily overwrite '=' to have \0
660 } else if (special) {
661 set = varisset(var, varflags & VSNUL);
664 val = bltinlookup(var, 1);
665 if (val == NULL || ((varflags & VSNUL) && val[0] == '\0')) {
672 startloc = expdest - stackblock();
681 error("%.*s: parameter not set", (int)(p - var - 1),
685 if (set && subtype != VSPLUS) {
686 /* insert the value of the variable */
688 varvalue(var, varflags & VSQUOTE, subtype, flag);
689 if (subtype == VSLENGTH) {
690 varlen = expdest - stackblock() - startloc;
691 STADJUST(-varlen, expdest);
694 char const *syntax = (varflags & VSQUOTE) ? DQSYNTAX
697 if (subtype == VSLENGTH) {
704 syntax[(int)*val] == CCTL)
705 STPUTC(CTLESC, expdest);
706 STPUTC(*val++, expdest);
713 if (subtype == VSPLUS)
716 easy = ((varflags & VSQUOTE) == 0 ||
717 (*var == '@' && shellparam.nparam != 1));
722 expdest = cvtnum(varlen, expdest);
729 recordregion(startloc, expdest - stackblock(),
750 * Terminate the string and start recording the pattern
753 STPUTC('\0', expdest);
754 patloc = expdest - stackblock();
755 if (subevalvar(p, NULL, patloc, subtype,
756 startloc, varflags) == 0) {
757 int amount = (expdest - stackblock() - patloc) + 1;
758 STADJUST(-amount, expdest);
760 /* Remove any recorded regions beyond start of variable */
761 removerecordregions(startloc);
767 if (subevalvar(p, var, 0, subtype, startloc, varflags)) {
770 * Remove any recorded regions beyond
773 removerecordregions(startloc);
784 error("${%.*s%s}: Bad substitution", c, var,
785 (c > 0 && *p != CTLENDVAR) ? "..." : "");
790 p[-1] = '='; /* recover overwritten '=' */
792 if (subtype != VSNORMAL) { /* skip to end of alternative */
795 if ((c = *p++) == CTLESC)
797 else if (c == CTLBACKQ || c == (CTLBACKQ|CTLQUOTE)) {
799 argbackq = argbackq->next;
800 } else if (c == CTLVAR) {
801 if ((*p++ & VSTYPE) != VSNORMAL)
803 } else if (c == CTLENDVAR) {
815 * Test whether a specialized variable is set.
819 varisset(char *name, int nulok)
823 return backgndpid != -1;
824 else if (*name == '@' || *name == '*') {
825 if (*shellparam.p == NULL)
831 for (av = shellparam.p; *av; av++)
836 } else if (is_digit(*name)) {
838 int num = atoi(name);
840 if (num > shellparam.nparam)
846 ap = shellparam.p[num - 1];
848 if (nulok && (ap == NULL || *ap == '\0'))
857 * Add the value of a specialized variable to the stack string.
861 varvalue(char *name, int quoted, int subtype, int flag)
870 #define STRTODEST(p) \
872 if (flag & (EXP_FULL | EXP_CASE) && subtype != VSLENGTH) { \
873 syntax = quoted? DQSYNTAX : BASESYNTAX; \
875 if (syntax[(int)*p] == CCTL) \
876 STPUTC(CTLESC, expdest); \
877 STPUTC(*p++, expdest); \
881 STPUTC(*p++, expdest); \
893 num = shellparam.nparam;
898 expdest = cvtnum(num, expdest);
901 for (i = 0 ; i < NOPTS ; i++) {
903 STPUTC(optlist[i].letter, expdest);
907 if (flag & EXP_FULL && quoted) {
908 for (ap = shellparam.p ; (p = *ap++) != NULL ; ) {
911 STPUTC('\0', expdest);
921 for (ap = shellparam.p ; (p = *ap++) != NULL ; ) {
924 STPUTC(sep, expdest);
932 if (is_digit(*name)) {
934 if (num > 0 && num <= shellparam.nparam) {
935 p = shellparam.p[num - 1];
946 * Record the the fact that we have to scan this region of the
947 * string for IFS characters.
951 recordregion(int start, int end, int inquotes)
953 struct ifsregion *ifsp;
955 if (ifslastp == NULL) {
958 if (ifslastp->endoff == start
959 && ifslastp->inquotes == inquotes) {
960 /* extend previous area */
961 ifslastp->endoff = end;
964 ifsp = (struct ifsregion *)ckmalloc(sizeof (struct ifsregion));
965 ifslastp->next = ifsp;
968 ifslastp->next = NULL;
969 ifslastp->begoff = start;
970 ifslastp->endoff = end;
971 ifslastp->inquotes = inquotes;
977 * Break the argument string into pieces based upon IFS and add the
978 * strings to the argument list. The regions of the string to be
979 * searched for IFS characters have been stored by recordregion.
980 * CTLESC characters are preserved but have little effect in this pass
981 * other than escaping CTL* characters. In particular, they do not escape
982 * IFS characters: that should be done with the ifsregion mechanism.
983 * CTLQUOTEMARK characters are used to preserve empty quoted strings.
984 * This pass treats them as a regular character, making the string non-empty.
985 * Later, they are removed along with the other CTL* characters.
988 ifsbreakup(char *string, struct arglist *arglist)
990 struct ifsregion *ifsp;
997 int had_param_ch = 0;
1001 if (ifslastp == NULL) {
1002 /* Return entire argument, IFS doesn't apply to any of it */
1003 sp = (struct strlist *)stalloc(sizeof *sp);
1005 *arglist->lastp = sp;
1006 arglist->lastp = &sp->next;
1010 ifs = ifsset() ? ifsval() : " \t\n";
1012 for (ifsp = &ifsfirst; ifsp != NULL; ifsp = ifsp->next) {
1013 p = string + ifsp->begoff;
1014 while (p < string + ifsp->endoff) {
1018 if (ifsp->inquotes) {
1019 /* Only NULs (should be from "$@") end args */
1027 if (!strchr(ifs, *p)) {
1032 ifsspc = strchr(" \t\n", *p);
1034 /* Ignore IFS whitespace at start */
1035 if (q == start && ifsspc != NULL) {
1043 /* Save this argument... */
1045 sp = (struct strlist *)stalloc(sizeof *sp);
1047 *arglist->lastp = sp;
1048 arglist->lastp = &sp->next;
1051 if (ifsspc != NULL) {
1052 /* Ignore further trailing IFS whitespace */
1053 for (; p < string + ifsp->endoff; p++) {
1057 if (strchr(ifs, *p) == NULL) {
1061 if (strchr(" \t\n", *p) == NULL) {
1072 * Save anything left as an argument.
1073 * Traditionally we have treated 'IFS=':'; set -- x$IFS' as
1074 * generating 2 arguments, the second of which is empty.
1075 * Some recent clarification of the Posix spec say that it
1076 * should only generate one....
1078 if (had_param_ch || *start != 0) {
1079 sp = (struct strlist *)stalloc(sizeof *sp);
1081 *arglist->lastp = sp;
1082 arglist->lastp = &sp->next;
1087 static char expdir[PATH_MAX];
1088 #define expdir_end (expdir + sizeof(expdir))
1091 * Perform pathname generation and remove control characters.
1092 * At this point, the only control characters should be CTLESC and CTLQUOTEMARK.
1093 * The results are stored in the list exparg.
1096 expandmeta(struct strlist *str, int flag __unused)
1099 struct strlist **savelastp;
1102 /* TODO - EXP_REDIR */
1108 for (;;) { /* fast check for meta chars */
1109 if ((c = *p++) == '\0')
1111 if (c == '*' || c == '?' || c == '[' || c == '!')
1114 savelastp = exparg.lastp;
1116 expmeta(expdir, str->text);
1118 if (exparg.lastp == savelastp) {
1123 *exparg.lastp = str;
1124 rmescapes(str->text);
1125 exparg.lastp = &str->next;
1127 *exparg.lastp = NULL;
1128 *savelastp = sp = expsort(*savelastp);
1129 while (sp->next != NULL)
1131 exparg.lastp = &sp->next;
1139 * Do metacharacter (i.e. *, ?, [...]) expansion.
1143 expmeta(char *enddir, char *name)
1159 for (p = name; esc = 0, *p; p += esc + 1) {
1160 if (*p == '*' || *p == '?')
1162 else if (*p == '[') {
1164 if (*q == '!' || *q == '^')
1167 while (*q == CTLQUOTEMARK)
1171 if (*q == '/' || *q == '\0')
1178 } else if (*p == '!' && p[1] == '!' && (p == name || p[-1] == '/')) {
1180 } else if (*p == '\0')
1182 else if (*p == CTLQUOTEMARK)
1187 if (p[esc] == '/') {
1190 start = p + esc + 1;
1194 if (metaflag == 0) { /* we've reached the end of the file name */
1195 if (enddir != expdir)
1197 for (p = name ; ; p++) {
1198 if (*p == CTLQUOTEMARK)
1205 if (enddir == expdir_end)
1208 if (metaflag == 0 || lstat(expdir, &statb) >= 0)
1213 if (start != name) {
1216 while (*p == CTLQUOTEMARK)
1221 if (enddir == expdir_end)
1225 if (enddir == expdir) {
1227 } else if (enddir == expdir + 1 && *expdir == '/') {
1233 if ((dirp = opendir(p)) == NULL)
1235 if (enddir != expdir)
1237 if (*endname == 0) {
1246 while (*p == CTLQUOTEMARK)
1252 while (! int_pending() && (dp = readdir(dirp)) != NULL) {
1253 if (dp->d_name[0] == '.' && ! matchdot)
1255 if (patmatch(start, dp->d_name, 0)) {
1256 if (enddir + dp->d_namlen + 1 > expdir_end)
1258 memcpy(enddir, dp->d_name, dp->d_namlen + 1);
1262 if (enddir + dp->d_namlen + 2 > expdir_end)
1264 enddir[dp->d_namlen] = '/';
1265 enddir[dp->d_namlen + 1] = '\0';
1266 expmeta(enddir + dp->d_namlen + 1, endname);
1272 endname[-esc - 1] = esc ? CTLESC : '/';
1277 * Add a file name to the list.
1281 addfname(char *name)
1286 p = stalloc(strlen(name) + 1);
1288 sp = (struct strlist *)stalloc(sizeof *sp);
1291 exparg.lastp = &sp->next;
1296 * Sort the results of file name expansion. It calculates the number of
1297 * strings to sort and then calls msort (short for merge sort) to do the
1301 static struct strlist *
1302 expsort(struct strlist *str)
1308 for (sp = str ; sp ; sp = sp->next)
1310 return msort(str, len);
1314 static struct strlist *
1315 msort(struct strlist *list, int len)
1317 struct strlist *p, *q = NULL;
1318 struct strlist **lpp;
1326 for (n = half ; --n >= 0 ; ) {
1330 q->next = NULL; /* terminate first half of list */
1331 q = msort(list, half); /* sort first half of list */
1332 p = msort(p, len - half); /* sort second half */
1335 if (strcmp(p->text, q->text) < 0) {
1338 if ((p = *lpp) == NULL) {
1345 if ((q = *lpp) == NULL) {
1357 * Returns true if the pattern matches the string.
1361 patmatch(const char *pattern, const char *string, int squoted)
1364 if (pattern[0] == '!' && pattern[1] == '!')
1365 return 1 - pmatch(pattern + 2, string);
1368 return pmatch(pattern, string, squoted);
1373 pmatch(const char *pattern, const char *string, int squoted)
1385 if (squoted && *q == CTLESC)
1393 if (squoted && *q == CTLESC)
1400 while (c == CTLQUOTEMARK || c == '*')
1402 if (c != CTLESC && c != CTLQUOTEMARK &&
1403 c != '?' && c != '*' && c != '[') {
1405 if (squoted && *q == CTLESC &&
1410 if (squoted && *q == CTLESC)
1416 if (pmatch(p, q, squoted))
1418 if (squoted && *q == CTLESC)
1420 } while (*q++ != '\0');
1428 if (*endp == '!' || *endp == '^')
1431 while (*endp == CTLQUOTEMARK)
1434 goto dft; /* no matching ] */
1435 if (*endp == CTLESC)
1441 if (*p == '!' || *p == '^') {
1447 if (squoted && chr == CTLESC)
1453 if (c == CTLQUOTEMARK)
1457 if (*p == '-' && p[1] != ']') {
1459 while (*p == CTLQUOTEMARK)
1463 if ( collate_range_cmp(chr, c) >= 0
1464 && collate_range_cmp(chr, *p) <= 0
1472 } while ((c = *p++) != ']');
1473 if (found == invert)
1478 if (squoted && *q == CTLESC)
1494 * Remove any CTLESC and CTLQUOTEMARK characters from a string.
1498 rmescapes(char *str)
1503 while (*p != CTLESC && *p != CTLQUOTEMARK) {
1509 if (*p == CTLQUOTEMARK) {
1523 * See if a pattern matches in a case statement.
1527 casematch(union node *pattern, const char *val)
1529 struct stackmark smark;
1533 setstackmark(&smark);
1534 argbackq = pattern->narg.backquote;
1535 STARTSTACKSTR(expdest);
1537 argstr(pattern->narg.text, EXP_TILDE | EXP_CASE);
1538 STPUTC('\0', expdest);
1539 p = grabstackstr(expdest);
1540 result = patmatch(p, val, 0);
1541 popstackmark(&smark);
1550 cvtnum(int num, char *buf)
1554 char *p = temp + 31;
1559 *--p = num % 10 + '0';
1560 } while ((num /= 10) != 0);
1571 * Do most of the work for wordexp(3).
1575 wordexpcmd(int argc, char **argv)
1580 out1fmt("%08x", argc - 1);
1581 for (i = 1, len = 0; i < argc; i++)
1582 len += strlen(argv[i]);
1583 out1fmt("%08x", (int)len);
1584 for (i = 1; i < argc; i++) {