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>
57 * Routines to expand arguments to commands. We have to deal with
58 * backquotes, shell variables, and file metacharacters.
80 * Structure specifying which parts of the string should be searched
85 struct ifsregion *next; /* next region in list */
86 int begoff; /* offset of start of region */
87 int endoff; /* offset of end of region */
88 int inquotes; /* search for nul bytes only */
92 static char *expdest; /* output of current string */
93 static struct nodelist *argbackq; /* list of back quote expressions */
94 static struct ifsregion ifsfirst; /* first struct in list of ifs regions */
95 static struct ifsregion *ifslastp; /* last struct in list */
96 static struct arglist exparg; /* holds expanded arg list */
98 static void argstr(char *, int);
99 static char *exptilde(char *, int);
100 static void expbackq(union node *, int, int);
101 static int subevalvar(char *, char *, int, int, int, int);
102 static char *evalvar(char *, int);
103 static int varisset(char *, int);
104 static void varvalue(char *, int, int, int);
105 static void recordregion(int, int, int);
106 static void removerecordregions(int);
107 static void ifsbreakup(char *, struct arglist *);
108 static void expandmeta(struct strlist *, int);
109 static void expmeta(char *, char *);
110 static void addfname(char *);
111 static struct strlist *expsort(struct strlist *);
112 static struct strlist *msort(struct strlist *, int);
113 static int pmatch(const char *, const char *, int);
114 static char *cvtnum(int, char *);
115 static int collate_range_cmp(int, int);
118 collate_range_cmp(int c1, int c2)
120 static char s1[2], s2[2];
124 return (strcoll(s1, s2));
128 * Expand shell variables and backquotes inside a here document.
129 * union node *arg the document
130 * int fd; where to write the expanded version
134 expandhere(union node *arg, int fd)
137 expandarg(arg, (struct arglist *)NULL, 0);
138 xwrite(fd, stackblock(), expdest - stackblock());
143 * Perform expansions on an argument, placing the resulting list of arguments
144 * in arglist. Parameter expansion, command substitution and arithmetic
145 * expansion are always performed; additional expansions can be requested
147 * The result is left in the stack string.
148 * When arglist is NULL, perform here document expansion. A partial result
149 * may be written to herefd, which is then not included in the stack string.
151 * Caution: this function uses global state and is not reentrant.
152 * However, a new invocation after an interrupted invocation is safe
153 * and will reset the global state for the new call.
156 expandarg(union node *arg, struct arglist *arglist, int flag)
161 argbackq = arg->narg.backquote;
162 STARTSTACKSTR(expdest);
163 ifsfirst.next = NULL;
165 argstr(arg->narg.text, flag);
166 if (arglist == NULL) {
167 return; /* here document expanded */
169 STPUTC('\0', expdest);
170 p = grabstackstr(expdest);
171 exparg.lastp = &exparg.list;
175 if (flag & EXP_FULL) {
176 ifsbreakup(p, &exparg);
177 *exparg.lastp = NULL;
178 exparg.lastp = &exparg.list;
179 expandmeta(exparg.list, flag);
181 if (flag & EXP_REDIR) /*XXX - for now, just remove escapes */
183 sp = (struct strlist *)stalloc(sizeof (struct strlist));
186 exparg.lastp = &sp->next;
188 while (ifsfirst.next != NULL) {
189 struct ifsregion *ifsp;
191 ifsp = ifsfirst.next->next;
192 ckfree(ifsfirst.next);
193 ifsfirst.next = ifsp;
196 *exparg.lastp = NULL;
198 *arglist->lastp = exparg.list;
199 arglist->lastp = exparg.lastp;
206 * Perform parameter expansion, command substitution and arithmetic
207 * expansion, and tilde expansion if requested via EXP_TILDE/EXP_VARTILDE.
208 * Processing ends at a CTLENDVAR character as well as '\0'.
209 * This is used to expand word in ${var+word} etc.
210 * If EXP_FULL, EXP_CASE or EXP_REDIR are set, keep and/or generate CTLESC
211 * characters to allow for further processing.
212 * If EXP_FULL is set, also preserve CTLQUOTEMARK characters.
215 argstr(char *p, int flag)
218 int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR); /* do CTLESC */
221 if (*p == '~' && (flag & (EXP_TILDE | EXP_VARTILDE)))
222 p = exptilde(p, flag);
229 /* "$@" syntax adherence hack */
230 if (p[0] == CTLVAR && p[2] == '@' && p[3] == '=')
232 if ((flag & EXP_FULL) != 0)
242 p = evalvar(p, flag);
245 case CTLBACKQ|CTLQUOTE:
246 expbackq(argbackq->n, c & CTLQUOTE, flag);
247 argbackq = argbackq->next;
255 * sort of a hack - expand tildes in variable
256 * assignments (after the first '=' and after ':'s).
259 if (flag & EXP_VARTILDE && *p == '~') {
266 p = exptilde(p, flag);
277 * Perform tilde expansion, placing the result in the stack string and
278 * returning the next position in the input string to process.
281 exptilde(char *p, int flag)
286 int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR);
288 while ((c = *p) != '\0') {
295 if (flag & EXP_VARTILDE)
305 if (*(startp+1) == '\0') {
306 if ((home = lookupvar("HOME")) == NULL)
309 if ((pw = getpwnam(startp+1)) == NULL)
316 while ((c = *home++) != '\0') {
317 if (quotes && SQSYNTAX[(int)c] == CCTL)
318 STPUTC(CTLESC, expdest);
329 removerecordregions(int endoff)
331 if (ifslastp == NULL)
334 if (ifsfirst.endoff > endoff) {
335 while (ifsfirst.next != NULL) {
336 struct ifsregion *ifsp;
338 ifsp = ifsfirst.next->next;
339 ckfree(ifsfirst.next);
340 ifsfirst.next = ifsp;
343 if (ifsfirst.begoff > endoff)
346 ifslastp = &ifsfirst;
347 ifsfirst.endoff = endoff;
352 ifslastp = &ifsfirst;
353 while (ifslastp->next && ifslastp->next->begoff < endoff)
354 ifslastp=ifslastp->next;
355 while (ifslastp->next != NULL) {
356 struct ifsregion *ifsp;
358 ifsp = ifslastp->next->next;
359 ckfree(ifslastp->next);
360 ifslastp->next = ifsp;
363 if (ifslastp->endoff > endoff)
364 ifslastp->endoff = endoff;
368 * Expand arithmetic expression. Backup to start of expression,
369 * evaluate, place result in (backed up) result, adjust string position.
377 int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR);
381 * This routine is slightly over-complicated for
382 * efficiency. First we make sure there is
383 * enough space for the result, which may be bigger
384 * than the expression. Next we
385 * scan backwards looking for the start of arithmetic. If the
386 * next previous character is a CTLESC character, then we
387 * have to rescan starting from the beginning since CTLESC
388 * characters have to be processed left to right.
390 CHECKSTRSPACE(DIGITS(result) - 2, expdest);
391 USTPUTC('\0', expdest);
392 start = stackblock();
394 while (p >= start && *p != CTLARI)
396 if (p < start || *p != CTLARI)
397 error("missing CTLARI (shouldn't happen)");
398 if (p > start && *(p - 1) == CTLESC)
399 for (p = start; *p != CTLARI; p++)
408 removerecordregions(begoff);
412 fmtstr(p, DIGITS(result), ARITH_FORMAT_STR, result);
416 recordregion(begoff, p - 1 - start, 0);
417 result = expdest - p + 1;
418 STADJUST(-result, expdest);
423 * Perform command substitution.
426 expbackq(union node *cmd, int quoted, int flag)
432 char *dest = expdest;
433 struct ifsregion saveifs, *savelastp;
434 struct nodelist *saveargbackq;
436 int startloc = dest - stackblock();
437 char const *syntax = quoted? DQSYNTAX : BASESYNTAX;
439 int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR);
444 savelastp = ifslastp;
445 saveargbackq = argbackq;
448 p = grabstackstr(dest);
449 evalbackcmd(cmd, &in);
450 ungrabstackstr(p, dest);
452 ifslastp = savelastp;
453 argbackq = saveargbackq;
459 /* Don't copy trailing newlines */
461 if (--in.nleft < 0) {
464 while ((i = read(in.fd, buf, sizeof buf)) < 0 && errno == EINTR);
465 TRACE(("expbackq: read returns %d\n", i));
480 if (quotes && syntax[(int)lastc] == CCTL)
481 STPUTC(CTLESC, dest);
492 exitstatus = waitforjob(in.jp, (int *)NULL);
494 recordregion(startloc, dest - stackblock(), 0);
495 TRACE(("expbackq: size=%td: \"%.*s\"\n",
496 ((dest - stackblock()) - startloc),
497 (int)((dest - stackblock()) - startloc),
498 stackblock() + startloc));
506 subevalvar(char *p, char *str, int strloc, int subtype, int startloc,
513 int saveherefd = herefd;
514 struct nodelist *saveargbackq = argbackq;
519 STACKSTRNUL(expdest);
521 argbackq = saveargbackq;
522 startp = stackblock() + startloc;
524 str = stackblock() + strloc;
528 setvar(str, startp, 0);
529 amount = startp - expdest;
530 STADJUST(amount, expdest);
537 if (*p != CTLENDVAR) {
538 outfmt(&errout, "%s\n", startp);
541 error("%.*s: parameter %snot set", (int)(p - str - 1),
542 str, (varflags & VSNUL) ? "null or "
547 for (loc = startp; loc < str; loc++) {
550 if (patmatch(str, startp, varflags & VSQUOTE)) {
555 if ((varflags & VSQUOTE) && *loc == CTLESC)
561 for (loc = str - 1; loc >= startp;) {
564 if (patmatch(str, startp, varflags & VSQUOTE)) {
570 if ((varflags & VSQUOTE) && loc > startp &&
571 *(loc - 1) == CTLESC) {
572 for (q = startp; q < loc; q++)
582 for (loc = str - 1; loc >= startp;) {
583 if (patmatch(str, loc, varflags & VSQUOTE)) {
584 amount = loc - expdest;
585 STADJUST(amount, expdest);
589 if ((varflags & VSQUOTE) && loc > startp &&
590 *(loc - 1) == CTLESC) {
591 for (q = startp; q < loc; q++)
601 for (loc = startp; loc < str - 1; loc++) {
602 if (patmatch(str, loc, varflags & VSQUOTE)) {
603 amount = loc - expdest;
604 STADJUST(amount, expdest);
607 if ((varflags & VSQUOTE) && *loc == CTLESC)
618 amount = ((str - 1) - (loc - startp)) - expdest;
619 STADJUST(amount, expdest);
620 while (loc != str - 1)
627 * Expand a variable, and return a pointer to the next character in the
632 evalvar(char *p, int flag)
645 int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR);
647 varflags = (unsigned char)*p++;
648 subtype = varflags & VSTYPE;
653 p = strchr(p, '=') + 1;
654 again: /* jump here after setting a variable with ${var=text} */
655 if (varflags & VSLINENO) {
659 p[-1] = '\0'; /* temporarily overwrite '=' to have \0
661 } else if (special) {
662 set = varisset(var, varflags & VSNUL);
665 val = bltinlookup(var, 1);
666 if (val == NULL || ((varflags & VSNUL) && val[0] == '\0')) {
673 startloc = expdest - stackblock();
674 if (!set && uflag && *var != '@' && *var != '*') {
682 error("%.*s: parameter not set", (int)(p - var - 1),
686 if (set && subtype != VSPLUS) {
687 /* insert the value of the variable */
689 varvalue(var, varflags & VSQUOTE, subtype, flag);
690 if (subtype == VSLENGTH) {
691 varlen = expdest - stackblock() - startloc;
692 STADJUST(-varlen, expdest);
695 char const *syntax = (varflags & VSQUOTE) ? DQSYNTAX
698 if (subtype == VSLENGTH) {
705 syntax[(int)*val] == CCTL)
706 STPUTC(CTLESC, expdest);
707 STPUTC(*val++, expdest);
714 if (subtype == VSPLUS)
717 easy = ((varflags & VSQUOTE) == 0 ||
718 (*var == '@' && shellparam.nparam != 1));
723 expdest = cvtnum(varlen, expdest);
730 recordregion(startloc, expdest - stackblock(),
751 * Terminate the string and start recording the pattern
754 STPUTC('\0', expdest);
755 patloc = expdest - stackblock();
756 if (subevalvar(p, NULL, patloc, subtype,
757 startloc, varflags) == 0) {
758 int amount = (expdest - stackblock() - patloc) + 1;
759 STADJUST(-amount, expdest);
761 /* Remove any recorded regions beyond start of variable */
762 removerecordregions(startloc);
768 if (subevalvar(p, var, 0, subtype, startloc, varflags)) {
771 * Remove any recorded regions beyond
774 removerecordregions(startloc);
785 error("${%.*s%s}: Bad substitution", c, var,
786 (c > 0 && *p != CTLENDVAR) ? "..." : "");
791 p[-1] = '='; /* recover overwritten '=' */
793 if (subtype != VSNORMAL) { /* skip to end of alternative */
796 if ((c = *p++) == CTLESC)
798 else if (c == CTLBACKQ || c == (CTLBACKQ|CTLQUOTE)) {
800 argbackq = argbackq->next;
801 } else if (c == CTLVAR) {
802 if ((*p++ & VSTYPE) != VSNORMAL)
804 } else if (c == CTLENDVAR) {
816 * Test whether a specialized variable is set.
820 varisset(char *name, int nulok)
824 return backgndpid != -1;
825 else if (*name == '@' || *name == '*') {
826 if (*shellparam.p == NULL)
832 for (av = shellparam.p; *av; av++)
837 } else if (is_digit(*name)) {
839 int num = atoi(name);
841 if (num > shellparam.nparam)
847 ap = shellparam.p[num - 1];
849 if (nulok && (ap == NULL || *ap == '\0'))
858 * Add the value of a specialized variable to the stack string.
862 varvalue(char *name, int quoted, int subtype, int flag)
871 #define STRTODEST(p) \
873 if (flag & (EXP_FULL | EXP_CASE) && subtype != VSLENGTH) { \
874 syntax = quoted? DQSYNTAX : BASESYNTAX; \
876 if (syntax[(int)*p] == CCTL) \
877 STPUTC(CTLESC, expdest); \
878 STPUTC(*p++, expdest); \
882 STPUTC(*p++, expdest); \
894 num = shellparam.nparam;
899 expdest = cvtnum(num, expdest);
902 for (i = 0 ; i < NOPTS ; i++) {
904 STPUTC(optlist[i].letter, expdest);
908 if (flag & EXP_FULL && quoted) {
909 for (ap = shellparam.p ; (p = *ap++) != NULL ; ) {
912 STPUTC('\0', expdest);
922 for (ap = shellparam.p ; (p = *ap++) != NULL ; ) {
925 STPUTC(sep, expdest);
933 if (is_digit(*name)) {
935 if (num > 0 && num <= shellparam.nparam) {
936 p = shellparam.p[num - 1];
947 * Record the the fact that we have to scan this region of the
948 * string for IFS characters.
952 recordregion(int start, int end, int inquotes)
954 struct ifsregion *ifsp;
956 if (ifslastp == NULL) {
959 if (ifslastp->endoff == start
960 && ifslastp->inquotes == inquotes) {
961 /* extend previous area */
962 ifslastp->endoff = end;
965 ifsp = (struct ifsregion *)ckmalloc(sizeof (struct ifsregion));
966 ifslastp->next = ifsp;
969 ifslastp->next = NULL;
970 ifslastp->begoff = start;
971 ifslastp->endoff = end;
972 ifslastp->inquotes = inquotes;
978 * Break the argument string into pieces based upon IFS and add the
979 * strings to the argument list. The regions of the string to be
980 * searched for IFS characters have been stored by recordregion.
981 * CTLESC characters are preserved but have little effect in this pass
982 * other than escaping CTL* characters. In particular, they do not escape
983 * IFS characters: that should be done with the ifsregion mechanism.
984 * CTLQUOTEMARK characters are used to preserve empty quoted strings.
985 * This pass treats them as a regular character, making the string non-empty.
986 * Later, they are removed along with the other CTL* characters.
989 ifsbreakup(char *string, struct arglist *arglist)
991 struct ifsregion *ifsp;
998 int had_param_ch = 0;
1002 if (ifslastp == NULL) {
1003 /* Return entire argument, IFS doesn't apply to any of it */
1004 sp = (struct strlist *)stalloc(sizeof *sp);
1006 *arglist->lastp = sp;
1007 arglist->lastp = &sp->next;
1011 ifs = ifsset() ? ifsval() : " \t\n";
1013 for (ifsp = &ifsfirst; ifsp != NULL; ifsp = ifsp->next) {
1014 p = string + ifsp->begoff;
1015 while (p < string + ifsp->endoff) {
1019 if (ifsp->inquotes) {
1020 /* Only NULs (should be from "$@") end args */
1028 if (!strchr(ifs, *p)) {
1033 ifsspc = strchr(" \t\n", *p);
1035 /* Ignore IFS whitespace at start */
1036 if (q == start && ifsspc != NULL) {
1044 /* Save this argument... */
1046 sp = (struct strlist *)stalloc(sizeof *sp);
1048 *arglist->lastp = sp;
1049 arglist->lastp = &sp->next;
1052 if (ifsspc != NULL) {
1053 /* Ignore further trailing IFS whitespace */
1054 for (; p < string + ifsp->endoff; p++) {
1058 if (strchr(ifs, *p) == NULL) {
1062 if (strchr(" \t\n", *p) == NULL) {
1073 * Save anything left as an argument.
1074 * Traditionally we have treated 'IFS=':'; set -- x$IFS' as
1075 * generating 2 arguments, the second of which is empty.
1076 * Some recent clarification of the Posix spec say that it
1077 * should only generate one....
1079 if (had_param_ch || *start != 0) {
1080 sp = (struct strlist *)stalloc(sizeof *sp);
1082 *arglist->lastp = sp;
1083 arglist->lastp = &sp->next;
1088 static char expdir[PATH_MAX];
1089 #define expdir_end (expdir + sizeof(expdir))
1092 * Perform pathname generation and remove control characters.
1093 * At this point, the only control characters should be CTLESC and CTLQUOTEMARK.
1094 * The results are stored in the list exparg.
1097 expandmeta(struct strlist *str, int flag __unused)
1100 struct strlist **savelastp;
1103 /* TODO - EXP_REDIR */
1109 for (;;) { /* fast check for meta chars */
1110 if ((c = *p++) == '\0')
1112 if (c == '*' || c == '?' || c == '[' || c == '!')
1115 savelastp = exparg.lastp;
1117 expmeta(expdir, str->text);
1119 if (exparg.lastp == savelastp) {
1124 *exparg.lastp = str;
1125 rmescapes(str->text);
1126 exparg.lastp = &str->next;
1128 *exparg.lastp = NULL;
1129 *savelastp = sp = expsort(*savelastp);
1130 while (sp->next != NULL)
1132 exparg.lastp = &sp->next;
1140 * Do metacharacter (i.e. *, ?, [...]) expansion.
1144 expmeta(char *enddir, char *name)
1160 for (p = name; esc = 0, *p; p += esc + 1) {
1161 if (*p == '*' || *p == '?')
1163 else if (*p == '[') {
1165 if (*q == '!' || *q == '^')
1168 while (*q == CTLQUOTEMARK)
1172 if (*q == '/' || *q == '\0')
1179 } else if (*p == '!' && p[1] == '!' && (p == name || p[-1] == '/')) {
1181 } else if (*p == '\0')
1183 else if (*p == CTLQUOTEMARK)
1188 if (p[esc] == '/') {
1191 start = p + esc + 1;
1195 if (metaflag == 0) { /* we've reached the end of the file name */
1196 if (enddir != expdir)
1198 for (p = name ; ; p++) {
1199 if (*p == CTLQUOTEMARK)
1206 if (enddir == expdir_end)
1209 if (metaflag == 0 || lstat(expdir, &statb) >= 0)
1214 if (start != name) {
1217 while (*p == CTLQUOTEMARK)
1222 if (enddir == expdir_end)
1226 if (enddir == expdir) {
1228 } else if (enddir == expdir + 1 && *expdir == '/') {
1234 if ((dirp = opendir(p)) == NULL)
1236 if (enddir != expdir)
1238 if (*endname == 0) {
1247 while (*p == CTLQUOTEMARK)
1253 while (! int_pending() && (dp = readdir(dirp)) != NULL) {
1254 if (dp->d_name[0] == '.' && ! matchdot)
1256 if (patmatch(start, dp->d_name, 0)) {
1257 if (enddir + dp->d_namlen + 1 > expdir_end)
1259 memcpy(enddir, dp->d_name, dp->d_namlen + 1);
1263 if (enddir + dp->d_namlen + 2 > expdir_end)
1265 enddir[dp->d_namlen] = '/';
1266 enddir[dp->d_namlen + 1] = '\0';
1267 expmeta(enddir + dp->d_namlen + 1, endname);
1273 endname[-esc - 1] = esc ? CTLESC : '/';
1278 * Add a file name to the list.
1282 addfname(char *name)
1287 p = stalloc(strlen(name) + 1);
1289 sp = (struct strlist *)stalloc(sizeof *sp);
1292 exparg.lastp = &sp->next;
1297 * Sort the results of file name expansion. It calculates the number of
1298 * strings to sort and then calls msort (short for merge sort) to do the
1302 static struct strlist *
1303 expsort(struct strlist *str)
1309 for (sp = str ; sp ; sp = sp->next)
1311 return msort(str, len);
1315 static struct strlist *
1316 msort(struct strlist *list, int len)
1318 struct strlist *p, *q = NULL;
1319 struct strlist **lpp;
1327 for (n = half ; --n >= 0 ; ) {
1331 q->next = NULL; /* terminate first half of list */
1332 q = msort(list, half); /* sort first half of list */
1333 p = msort(p, len - half); /* sort second half */
1336 if (strcmp(p->text, q->text) < 0) {
1339 if ((p = *lpp) == NULL) {
1346 if ((q = *lpp) == NULL) {
1358 * Returns true if the pattern matches the string.
1362 patmatch(const char *pattern, const char *string, int squoted)
1365 if (pattern[0] == '!' && pattern[1] == '!')
1366 return 1 - pmatch(pattern + 2, string);
1369 return pmatch(pattern, string, squoted);
1374 pmatch(const char *pattern, const char *string, int squoted)
1386 if (squoted && *q == CTLESC)
1394 if (squoted && *q == CTLESC)
1401 while (c == CTLQUOTEMARK || c == '*')
1403 if (c != CTLESC && c != CTLQUOTEMARK &&
1404 c != '?' && c != '*' && c != '[') {
1406 if (squoted && *q == CTLESC &&
1411 if (squoted && *q == CTLESC)
1417 if (pmatch(p, q, squoted))
1419 if (squoted && *q == CTLESC)
1421 } while (*q++ != '\0');
1429 if (*endp == '!' || *endp == '^')
1432 while (*endp == CTLQUOTEMARK)
1435 goto dft; /* no matching ] */
1436 if (*endp == CTLESC)
1442 if (*p == '!' || *p == '^') {
1448 if (squoted && chr == CTLESC)
1454 if (c == CTLQUOTEMARK)
1458 if (*p == '-' && p[1] != ']') {
1460 while (*p == CTLQUOTEMARK)
1464 if ( collate_range_cmp(chr, c) >= 0
1465 && collate_range_cmp(chr, *p) <= 0
1473 } while ((c = *p++) != ']');
1474 if (found == invert)
1479 if (squoted && *q == CTLESC)
1495 * Remove any CTLESC and CTLQUOTEMARK characters from a string.
1499 rmescapes(char *str)
1504 while (*p != CTLESC && *p != CTLQUOTEMARK) {
1510 if (*p == CTLQUOTEMARK) {
1524 * See if a pattern matches in a case statement.
1528 casematch(union node *pattern, const char *val)
1530 struct stackmark smark;
1534 setstackmark(&smark);
1535 argbackq = pattern->narg.backquote;
1536 STARTSTACKSTR(expdest);
1538 argstr(pattern->narg.text, EXP_TILDE | EXP_CASE);
1539 STPUTC('\0', expdest);
1540 p = grabstackstr(expdest);
1541 result = patmatch(p, val, 0);
1542 popstackmark(&smark);
1551 cvtnum(int num, char *buf)
1555 char *p = temp + 31;
1560 *--p = num % 10 + '0';
1561 } while ((num /= 10) != 0);
1572 * Do most of the work for wordexp(3).
1576 wordexpcmd(int argc, char **argv)
1581 out1fmt("%08x", argc - 1);
1582 for (i = 1, len = 0; i < argc; i++)
1583 len += strlen(argv[i]);
1584 out1fmt("%08x", (int)len);
1585 for (i = 1; i < argc; i++) {