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(char *, 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 variable substitution and command substitution on an argument,
143 * placing the resulting list of arguments in arglist. If EXP_FULL is true,
144 * perform splitting and file name expansion. When arglist is NULL, perform
145 * here document expansion.
149 expandarg(union node *arg, struct arglist *arglist, int flag)
154 argbackq = arg->narg.backquote;
155 STARTSTACKSTR(expdest);
156 ifsfirst.next = NULL;
158 argstr(arg->narg.text, flag);
159 if (arglist == NULL) {
160 return; /* here document expanded */
162 STPUTC('\0', expdest);
163 p = grabstackstr(expdest);
164 exparg.lastp = &exparg.list;
168 if (flag & EXP_FULL) {
169 ifsbreakup(p, &exparg);
170 *exparg.lastp = NULL;
171 exparg.lastp = &exparg.list;
172 expandmeta(exparg.list, flag);
174 if (flag & EXP_REDIR) /*XXX - for now, just remove escapes */
176 sp = (struct strlist *)stalloc(sizeof (struct strlist));
179 exparg.lastp = &sp->next;
181 while (ifsfirst.next != NULL) {
182 struct ifsregion *ifsp;
184 ifsp = ifsfirst.next->next;
185 ckfree(ifsfirst.next);
186 ifsfirst.next = ifsp;
189 *exparg.lastp = NULL;
191 *arglist->lastp = exparg.list;
192 arglist->lastp = exparg.lastp;
199 * Perform variable and command substitution. If EXP_FULL is set, output CTLESC
200 * characters to allow for further processing. Otherwise treat
201 * $@ like $* since no splitting will be performed.
205 argstr(char *p, int flag)
208 int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR); /* do CTLESC */
211 if (*p == '~' && (flag & (EXP_TILDE | EXP_VARTILDE)))
212 p = exptilde(p, flag);
216 case CTLENDVAR: /* ??? */
219 /* "$@" syntax adherence hack */
220 if (p[0] == CTLVAR && p[2] == '@' && p[3] == '=')
222 if ((flag & EXP_FULL) != 0)
232 p = evalvar(p, flag);
235 case CTLBACKQ|CTLQUOTE:
236 expbackq(argbackq->n, c & CTLQUOTE, flag);
237 argbackq = argbackq->next;
245 * sort of a hack - expand tildes in variable
246 * assignments (after the first '=' and after ':'s).
249 if (flag & EXP_VARTILDE && *p == '~') {
256 p = exptilde(p, flag);
267 exptilde(char *p, int flag)
272 int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR);
274 while ((c = *p) != '\0') {
281 if (flag & EXP_VARTILDE)
291 if (*(startp+1) == '\0') {
292 if ((home = lookupvar("HOME")) == NULL)
295 if ((pw = getpwnam(startp+1)) == NULL)
302 while ((c = *home++) != '\0') {
303 if (quotes && SQSYNTAX[(int)c] == CCTL)
304 STPUTC(CTLESC, expdest);
315 removerecordregions(int endoff)
317 if (ifslastp == NULL)
320 if (ifsfirst.endoff > endoff) {
321 while (ifsfirst.next != NULL) {
322 struct ifsregion *ifsp;
324 ifsp = ifsfirst.next->next;
325 ckfree(ifsfirst.next);
326 ifsfirst.next = ifsp;
329 if (ifsfirst.begoff > endoff)
332 ifslastp = &ifsfirst;
333 ifsfirst.endoff = endoff;
338 ifslastp = &ifsfirst;
339 while (ifslastp->next && ifslastp->next->begoff < endoff)
340 ifslastp=ifslastp->next;
341 while (ifslastp->next != NULL) {
342 struct ifsregion *ifsp;
344 ifsp = ifslastp->next->next;
345 ckfree(ifslastp->next);
346 ifslastp->next = ifsp;
349 if (ifslastp->endoff > endoff)
350 ifslastp->endoff = endoff;
354 * Expand arithmetic expression. Backup to start of expression,
355 * evaluate, place result in (backed up) result, adjust string position.
363 int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR);
368 * This routine is slightly over-complicated for
369 * efficiency. First we make sure there is
370 * enough space for the result, which may be bigger
371 * than the expression if we add exponentiation. Next we
372 * scan backwards looking for the start of arithmetic. If the
373 * next previous character is a CTLESC character, then we
374 * have to rescan starting from the beginning since CTLESC
375 * characters have to be processed left to right.
377 CHECKSTRSPACE(DIGITS(result) - 2, expdest);
378 USTPUTC('\0', expdest);
379 start = stackblock();
381 while (p >= start && *p != CTLARI)
383 if (p < start || *p != CTLARI)
384 error("missing CTLARI (shouldn't happen)");
385 if (p > start && *(p - 1) == CTLESC)
386 for (p = start; *p != CTLARI; p++)
395 removerecordregions(begoff);
399 fmtstr(p, DIGITS(result), ARITH_FORMAT_STR, result);
403 recordregion(begoff, p - 1 - start, 0);
404 result = expdest - p + 1;
405 STADJUST(-result, expdest);
410 * Expand stuff in backwards quotes.
414 expbackq(union node *cmd, int quoted, int flag)
420 char *dest = expdest;
421 struct ifsregion saveifs, *savelastp;
422 struct nodelist *saveargbackq;
424 int startloc = dest - stackblock();
425 char const *syntax = quoted? DQSYNTAX : BASESYNTAX;
427 int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR);
432 savelastp = ifslastp;
433 saveargbackq = argbackq;
436 p = grabstackstr(dest);
437 evalbackcmd(cmd, &in);
438 ungrabstackstr(p, dest);
440 ifslastp = savelastp;
441 argbackq = saveargbackq;
447 /* Don't copy trailing newlines */
449 if (--in.nleft < 0) {
452 while ((i = read(in.fd, buf, sizeof buf)) < 0 && errno == EINTR);
453 TRACE(("expbackq: read returns %d\n", i));
461 if (quotes && syntax[(int)lastc] == CCTL)
462 STPUTC(CTLESC, dest);
480 exitstatus = waitforjob(in.jp, (int *)NULL);
482 recordregion(startloc, dest - stackblock(), 0);
483 TRACE(("evalbackq: size=%d: \"%.*s\"\n",
484 (dest - stackblock()) - startloc,
485 (dest - stackblock()) - startloc,
486 stackblock() + startloc));
494 subevalvar(char *p, char *str, int strloc, int subtype, int startloc,
501 int saveherefd = herefd;
502 struct nodelist *saveargbackq = argbackq;
507 STACKSTRNUL(expdest);
509 argbackq = saveargbackq;
510 startp = stackblock() + startloc;
512 str = stackblock() + strloc;
516 setvar(str, startp, 0);
517 amount = startp - expdest;
518 STADJUST(amount, expdest);
525 if (*p != CTLENDVAR) {
526 outfmt(&errout, "%s\n", startp);
529 error("%.*s: parameter %snot set", (int)(p - str - 1),
530 str, (varflags & VSNUL) ? "null or "
535 for (loc = startp; loc < str; loc++) {
538 if (patmatch(str, startp, varflags & VSQUOTE)) {
543 if ((varflags & VSQUOTE) && *loc == CTLESC)
549 for (loc = str - 1; loc >= startp;) {
552 if (patmatch(str, startp, varflags & VSQUOTE)) {
558 if ((varflags & VSQUOTE) && loc > startp &&
559 *(loc - 1) == CTLESC) {
560 for (q = startp; q < loc; q++)
570 for (loc = str - 1; loc >= startp;) {
571 if (patmatch(str, loc, varflags & VSQUOTE)) {
572 amount = loc - expdest;
573 STADJUST(amount, expdest);
577 if ((varflags & VSQUOTE) && loc > startp &&
578 *(loc - 1) == CTLESC) {
579 for (q = startp; q < loc; q++)
589 for (loc = startp; loc < str - 1; loc++) {
590 if (patmatch(str, loc, varflags & VSQUOTE)) {
591 amount = loc - expdest;
592 STADJUST(amount, expdest);
595 if ((varflags & VSQUOTE) && *loc == CTLESC)
606 amount = ((str - 1) - (loc - startp)) - expdest;
607 STADJUST(amount, expdest);
608 while (loc != str - 1)
615 * Expand a variable, and return a pointer to the next character in the
620 evalvar(char *p, int flag)
633 int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR);
635 varflags = (unsigned char)*p++;
636 subtype = varflags & VSTYPE;
641 p = strchr(p, '=') + 1;
642 again: /* jump here after setting a variable with ${var=text} */
643 if (varflags & VSLINENO) {
647 p[-1] = '\0'; /* temporarily overwrite '=' to have \0
649 } else if (special) {
650 set = varisset(var, varflags & VSNUL);
653 val = bltinlookup(var, 1);
654 if (val == NULL || ((varflags & VSNUL) && val[0] == '\0')) {
661 startloc = expdest - stackblock();
670 error("%.*s: parameter not set", (int)(p - var - 1),
674 if (set && subtype != VSPLUS) {
675 /* insert the value of the variable */
677 varvalue(var, varflags & VSQUOTE, subtype, flag);
678 if (subtype == VSLENGTH) {
679 varlen = expdest - stackblock() - startloc;
680 STADJUST(-varlen, expdest);
683 char const *syntax = (varflags & VSQUOTE) ? DQSYNTAX
686 if (subtype == VSLENGTH) {
693 syntax[(int)*val] == CCTL)
694 STPUTC(CTLESC, expdest);
695 STPUTC(*val++, expdest);
702 if (subtype == VSPLUS)
705 easy = ((varflags & VSQUOTE) == 0 ||
706 (*var == '@' && shellparam.nparam != 1));
711 expdest = cvtnum(varlen, expdest);
718 recordregion(startloc, expdest - stackblock(),
739 * Terminate the string and start recording the pattern
742 STPUTC('\0', expdest);
743 patloc = expdest - stackblock();
744 if (subevalvar(p, NULL, patloc, subtype,
745 startloc, varflags) == 0) {
746 int amount = (expdest - stackblock() - patloc) + 1;
747 STADJUST(-amount, expdest);
749 /* Remove any recorded regions beyond start of variable */
750 removerecordregions(startloc);
756 if (subevalvar(p, var, 0, subtype, startloc, varflags)) {
759 * Remove any recorded regions beyond
762 removerecordregions(startloc);
773 error("${%.*s%s}: Bad substitution", c, var,
774 (c > 0 && *p != CTLENDVAR) ? "..." : "");
779 p[-1] = '='; /* recover overwritten '=' */
781 if (subtype != VSNORMAL) { /* skip to end of alternative */
784 if ((c = *p++) == CTLESC)
786 else if (c == CTLBACKQ || c == (CTLBACKQ|CTLQUOTE)) {
788 argbackq = argbackq->next;
789 } else if (c == CTLVAR) {
790 if ((*p++ & VSTYPE) != VSNORMAL)
792 } else if (c == CTLENDVAR) {
804 * Test whether a specialized variable is set.
808 varisset(char *name, int nulok)
812 return backgndpid != -1;
813 else if (*name == '@' || *name == '*') {
814 if (*shellparam.p == NULL)
820 for (av = shellparam.p; *av; av++)
825 } else if (is_digit(*name)) {
827 int num = atoi(name);
829 if (num > shellparam.nparam)
835 ap = shellparam.p[num - 1];
837 if (nulok && (ap == NULL || *ap == '\0'))
846 * Add the value of a specialized variable to the stack string.
850 varvalue(char *name, int quoted, int subtype, int flag)
855 extern int oexitstatus;
860 #define STRTODEST(p) \
862 if (flag & (EXP_FULL | EXP_CASE) && subtype != VSLENGTH) { \
863 syntax = quoted? DQSYNTAX : BASESYNTAX; \
865 if (syntax[(int)*p] == CCTL) \
866 STPUTC(CTLESC, expdest); \
867 STPUTC(*p++, expdest); \
871 STPUTC(*p++, expdest); \
883 num = shellparam.nparam;
888 expdest = cvtnum(num, expdest);
891 for (i = 0 ; i < NOPTS ; i++) {
893 STPUTC(optlist[i].letter, expdest);
897 if (flag & EXP_FULL && quoted) {
898 for (ap = shellparam.p ; (p = *ap++) != NULL ; ) {
901 STPUTC('\0', expdest);
911 for (ap = shellparam.p ; (p = *ap++) != NULL ; ) {
914 STPUTC(sep, expdest);
922 if (is_digit(*name)) {
924 if (num > 0 && num <= shellparam.nparam) {
925 p = shellparam.p[num - 1];
936 * Record the the fact that we have to scan this region of the
937 * string for IFS characters.
941 recordregion(int start, int end, int inquotes)
943 struct ifsregion *ifsp;
945 if (ifslastp == NULL) {
948 if (ifslastp->endoff == start
949 && ifslastp->inquotes == inquotes) {
950 /* extend previous area */
951 ifslastp->endoff = end;
954 ifsp = (struct ifsregion *)ckmalloc(sizeof (struct ifsregion));
955 ifslastp->next = ifsp;
958 ifslastp->next = NULL;
959 ifslastp->begoff = start;
960 ifslastp->endoff = end;
961 ifslastp->inquotes = inquotes;
967 * Break the argument string into pieces based upon IFS and add the
968 * strings to the argument list. The regions of the string to be
969 * searched for IFS characters have been stored by recordregion.
972 ifsbreakup(char *string, struct arglist *arglist)
974 struct ifsregion *ifsp;
981 int had_param_ch = 0;
985 if (ifslastp == NULL) {
986 /* Return entire argument, IFS doesn't apply to any of it */
987 sp = (struct strlist *)stalloc(sizeof *sp);
989 *arglist->lastp = sp;
990 arglist->lastp = &sp->next;
994 ifs = ifsset() ? ifsval() : " \t\n";
996 for (ifsp = &ifsfirst; ifsp != NULL; ifsp = ifsp->next) {
997 p = string + ifsp->begoff;
998 while (p < string + ifsp->endoff) {
1002 if (ifsp->inquotes) {
1003 /* Only NULs (should be from "$@") end args */
1011 if (!strchr(ifs, *p)) {
1016 ifsspc = strchr(" \t\n", *p);
1018 /* Ignore IFS whitespace at start */
1019 if (q == start && ifsspc != NULL) {
1027 /* Save this argument... */
1029 sp = (struct strlist *)stalloc(sizeof *sp);
1031 *arglist->lastp = sp;
1032 arglist->lastp = &sp->next;
1035 if (ifsspc != NULL) {
1036 /* Ignore further trailing IFS whitespace */
1037 for (; p < string + ifsp->endoff; p++) {
1041 if (strchr(ifs, *p) == NULL) {
1045 if (strchr(" \t\n", *p) == NULL) {
1056 * Save anything left as an argument.
1057 * Traditionally we have treated 'IFS=':'; set -- x$IFS' as
1058 * generating 2 arguments, the second of which is empty.
1059 * Some recent clarification of the Posix spec say that it
1060 * should only generate one....
1062 if (had_param_ch || *start != 0) {
1063 sp = (struct strlist *)stalloc(sizeof *sp);
1065 *arglist->lastp = sp;
1066 arglist->lastp = &sp->next;
1073 * Expand shell metacharacters. At this point, the only control characters
1074 * should be escapes. The results are stored in the list exparg.
1077 STATIC char *expdir;
1081 expandmeta(struct strlist *str, int flag __unused)
1084 struct strlist **savelastp;
1087 /* TODO - EXP_REDIR */
1093 for (;;) { /* fast check for meta chars */
1094 if ((c = *p++) == '\0')
1096 if (c == '*' || c == '?' || c == '[' || c == '!')
1099 savelastp = exparg.lastp;
1101 if (expdir == NULL) {
1102 int i = strlen(str->text);
1103 expdir = ckmalloc(i < 2048 ? 2048 : i); /* XXX */
1106 expmeta(expdir, str->text);
1110 if (exparg.lastp == savelastp) {
1115 *exparg.lastp = str;
1116 rmescapes(str->text);
1117 exparg.lastp = &str->next;
1119 *exparg.lastp = NULL;
1120 *savelastp = sp = expsort(*savelastp);
1121 while (sp->next != NULL)
1123 exparg.lastp = &sp->next;
1131 * Do metacharacter (i.e. *, ?, [...]) expansion.
1135 expmeta(char *enddir, char *name)
1151 for (p = name; esc = 0, *p; p += esc + 1) {
1152 if (*p == '*' || *p == '?')
1154 else if (*p == '[') {
1156 if (*q == '!' || *q == '^')
1159 while (*q == CTLQUOTEMARK)
1163 if (*q == '/' || *q == '\0')
1170 } else if (*p == '!' && p[1] == '!' && (p == name || p[-1] == '/')) {
1172 } else if (*p == '\0')
1174 else if (*p == CTLQUOTEMARK)
1179 if (p[esc] == '/') {
1182 start = p + esc + 1;
1186 if (metaflag == 0) { /* we've reached the end of the file name */
1187 if (enddir != expdir)
1189 for (p = name ; ; p++) {
1190 if (*p == CTLQUOTEMARK)
1198 if (metaflag == 0 || lstat(expdir, &statb) >= 0)
1203 if (start != name) {
1206 while (*p == CTLQUOTEMARK)
1213 if (enddir == expdir) {
1215 } else if (enddir == expdir + 1 && *expdir == '/') {
1221 if ((dirp = opendir(p)) == NULL)
1223 if (enddir != expdir)
1225 if (*endname == 0) {
1234 while (*p == CTLQUOTEMARK)
1240 while (! int_pending() && (dp = readdir(dirp)) != NULL) {
1241 if (dp->d_name[0] == '.' && ! matchdot)
1243 if (patmatch(start, dp->d_name, 0)) {
1245 scopy(dp->d_name, enddir);
1248 for (p = enddir, q = dp->d_name;
1249 (*p++ = *q++) != '\0';)
1252 expmeta(p, endname);
1258 endname[-esc - 1] = esc ? CTLESC : '/';
1263 * Add a file name to the list.
1267 addfname(char *name)
1272 p = stalloc(strlen(name) + 1);
1274 sp = (struct strlist *)stalloc(sizeof *sp);
1277 exparg.lastp = &sp->next;
1282 * Sort the results of file name expansion. It calculates the number of
1283 * strings to sort and then calls msort (short for merge sort) to do the
1287 STATIC struct strlist *
1288 expsort(struct strlist *str)
1294 for (sp = str ; sp ; sp = sp->next)
1296 return msort(str, len);
1300 STATIC struct strlist *
1301 msort(struct strlist *list, int len)
1303 struct strlist *p, *q = NULL;
1304 struct strlist **lpp;
1312 for (n = half ; --n >= 0 ; ) {
1316 q->next = NULL; /* terminate first half of list */
1317 q = msort(list, half); /* sort first half of list */
1318 p = msort(p, len - half); /* sort second half */
1321 if (strcmp(p->text, q->text) < 0) {
1324 if ((p = *lpp) == NULL) {
1331 if ((q = *lpp) == NULL) {
1343 * Returns true if the pattern matches the string.
1347 patmatch(char *pattern, char *string, int squoted)
1350 if (pattern[0] == '!' && pattern[1] == '!')
1351 return 1 - pmatch(pattern + 2, string);
1354 return pmatch(pattern, string, squoted);
1359 pmatch(char *pattern, char *string, int squoted)
1371 if (squoted && *q == CTLESC)
1379 if (squoted && *q == CTLESC)
1386 while (c == CTLQUOTEMARK || c == '*')
1388 if (c != CTLESC && c != CTLQUOTEMARK &&
1389 c != '?' && c != '*' && c != '[') {
1391 if (squoted && *q == CTLESC &&
1396 if (squoted && *q == CTLESC)
1402 if (pmatch(p, q, squoted))
1404 if (squoted && *q == CTLESC)
1406 } while (*q++ != '\0');
1414 if (*endp == '!' || *endp == '^')
1417 while (*endp == CTLQUOTEMARK)
1420 goto dft; /* no matching ] */
1421 if (*endp == CTLESC)
1427 if (*p == '!' || *p == '^') {
1433 if (squoted && chr == CTLESC)
1439 if (c == CTLQUOTEMARK)
1443 if (*p == '-' && p[1] != ']') {
1445 while (*p == CTLQUOTEMARK)
1449 if ( collate_range_cmp(chr, c) >= 0
1450 && collate_range_cmp(chr, *p) <= 0
1458 } while ((c = *p++) != ']');
1459 if (found == invert)
1464 if (squoted && *q == CTLESC)
1480 * Remove any CTLESC characters from a string.
1484 rmescapes(char *str)
1489 while (*p != CTLESC && *p != CTLQUOTEMARK) {
1495 if (*p == CTLQUOTEMARK) {
1509 * See if a pattern matches in a case statement.
1513 casematch(union node *pattern, char *val)
1515 struct stackmark smark;
1519 setstackmark(&smark);
1520 argbackq = pattern->narg.backquote;
1521 STARTSTACKSTR(expdest);
1523 argstr(pattern->narg.text, EXP_TILDE | EXP_CASE);
1524 STPUTC('\0', expdest);
1525 p = grabstackstr(expdest);
1526 result = patmatch(p, val, 0);
1527 popstackmark(&smark);
1536 cvtnum(int num, char *buf)
1540 char *p = temp + 31;
1545 *--p = num % 10 + '0';
1546 } while ((num /= 10) != 0);
1557 * Do most of the work for wordexp(3).
1561 wordexpcmd(int argc, char **argv)
1566 out1fmt("%08x", argc - 1);
1567 for (i = 1, len = 0; i < argc; i++)
1568 len += strlen(argv[i]);
1569 out1fmt("%08x", (int)len);
1570 for (i = 1; i < argc; i++) {