2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 static char sccsid[] = "@(#)expand.c 8.5 (Berkeley) 5/15/95";
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
41 #include <sys/types.h>
54 * Routines to expand arguments to commands. We have to deal with
55 * backquotes, shell variables, and file metacharacters.
77 * Structure specifying which parts of the string should be searched
82 struct ifsregion *next; /* next region in list */
83 int begoff; /* offset of start of region */
84 int endoff; /* offset of end of region */
85 int inquotes; /* search for nul bytes only */
89 STATIC char *expdest; /* output of current string */
90 STATIC struct nodelist *argbackq; /* list of back quote expressions */
91 STATIC struct ifsregion ifsfirst; /* first struct in list of ifs regions */
92 STATIC struct ifsregion *ifslastp; /* last struct in list */
93 STATIC struct arglist exparg; /* holds expanded arg list */
95 STATIC void argstr(char *, int);
96 STATIC char *exptilde(char *, int);
97 STATIC void expbackq(union node *, int, int);
98 STATIC int subevalvar(char *, char *, int, int, int, int);
99 STATIC char *evalvar(char *, int);
100 STATIC int varisset(char *, int);
101 STATIC void varvalue(char *, int, int, int);
102 STATIC void recordregion(int, int, int);
103 STATIC void removerecordregions(int);
104 STATIC void ifsbreakup(char *, struct arglist *);
105 STATIC void expandmeta(struct strlist *, int);
106 STATIC void expmeta(char *, char *);
107 STATIC void addfname(char *);
108 STATIC struct strlist *expsort(struct strlist *);
109 STATIC struct strlist *msort(struct strlist *, int);
110 STATIC int pmatch(char *, char *, int);
111 STATIC char *cvtnum(int, char *);
112 STATIC int collate_range_cmp(int, int);
115 collate_range_cmp(int c1, int c2)
117 static char s1[2], s2[2];
121 return (strcoll(s1, s2));
125 * Expand shell variables and backquotes inside a here document.
126 * union node *arg the document
127 * int fd; where to write the expanded version
131 expandhere(union node *arg, int fd)
134 expandarg(arg, (struct arglist *)NULL, 0);
135 xwrite(fd, stackblock(), expdest - stackblock());
140 * Perform variable substitution and command substitution on an argument,
141 * placing the resulting list of arguments in arglist. If EXP_FULL is true,
142 * perform splitting and file name expansion. When arglist is NULL, perform
143 * here document expansion.
147 expandarg(union node *arg, struct arglist *arglist, int flag)
152 argbackq = arg->narg.backquote;
153 STARTSTACKSTR(expdest);
154 ifsfirst.next = NULL;
156 argstr(arg->narg.text, flag);
157 if (arglist == NULL) {
158 return; /* here document expanded */
160 STPUTC('\0', expdest);
161 p = grabstackstr(expdest);
162 exparg.lastp = &exparg.list;
166 if (flag & EXP_FULL) {
167 ifsbreakup(p, &exparg);
168 *exparg.lastp = NULL;
169 exparg.lastp = &exparg.list;
170 expandmeta(exparg.list, flag);
172 if (flag & EXP_REDIR) /*XXX - for now, just remove escapes */
174 sp = (struct strlist *)stalloc(sizeof (struct strlist));
177 exparg.lastp = &sp->next;
179 while (ifsfirst.next != NULL) {
180 struct ifsregion *ifsp;
182 ifsp = ifsfirst.next->next;
183 ckfree(ifsfirst.next);
184 ifsfirst.next = ifsp;
187 *exparg.lastp = NULL;
189 *arglist->lastp = exparg.list;
190 arglist->lastp = exparg.lastp;
197 * Perform variable and command substitution. If EXP_FULL is set, output CTLESC
198 * characters to allow for further processing. Otherwise treat
199 * $@ like $* since no splitting will be performed.
203 argstr(char *p, int flag)
206 int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR); /* do CTLESC */
209 if (*p == '~' && (flag & (EXP_TILDE | EXP_VARTILDE)))
210 p = exptilde(p, flag);
214 case CTLENDVAR: /* ??? */
217 /* "$@" syntax adherence hack */
218 if (p[0] == CTLVAR && p[2] == '@' && p[3] == '=')
220 if ((flag & EXP_FULL) != 0)
230 p = evalvar(p, flag);
233 case CTLBACKQ|CTLQUOTE:
234 expbackq(argbackq->n, c & CTLQUOTE, flag);
235 argbackq = argbackq->next;
243 * sort of a hack - expand tildes in variable
244 * assignments (after the first '=' and after ':'s).
247 if (flag & EXP_VARTILDE && *p == '~') {
254 p = exptilde(p, flag);
265 exptilde(char *p, int flag)
270 int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR);
272 while ((c = *p) != '\0') {
279 if (flag & EXP_VARTILDE)
289 if (*(startp+1) == '\0') {
290 if ((home = lookupvar("HOME")) == NULL)
293 if ((pw = getpwnam(startp+1)) == NULL)
300 while ((c = *home++) != '\0') {
301 if (quotes && SQSYNTAX[(int)c] == CCTL)
302 STPUTC(CTLESC, expdest);
313 removerecordregions(int endoff)
315 if (ifslastp == NULL)
318 if (ifsfirst.endoff > endoff) {
319 while (ifsfirst.next != NULL) {
320 struct ifsregion *ifsp;
322 ifsp = ifsfirst.next->next;
323 ckfree(ifsfirst.next);
324 ifsfirst.next = ifsp;
327 if (ifsfirst.begoff > endoff)
330 ifslastp = &ifsfirst;
331 ifsfirst.endoff = endoff;
336 ifslastp = &ifsfirst;
337 while (ifslastp->next && ifslastp->next->begoff < endoff)
338 ifslastp=ifslastp->next;
339 while (ifslastp->next != NULL) {
340 struct ifsregion *ifsp;
342 ifsp = ifslastp->next->next;
343 ckfree(ifslastp->next);
344 ifslastp->next = ifsp;
347 if (ifslastp->endoff > endoff)
348 ifslastp->endoff = endoff;
352 * Expand arithmetic expression. Backup to start of expression,
353 * evaluate, place result in (backed up) result, adjust string position.
361 int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR);
366 * This routine is slightly over-complicated for
367 * efficiency. First we make sure there is
368 * enough space for the result, which may be bigger
369 * than the expression if we add exponentiation. Next we
370 * scan backwards looking for the start of arithmetic. If the
371 * next previous character is a CTLESC character, then we
372 * have to rescan starting from the beginning since CTLESC
373 * characters have to be processed left to right.
375 CHECKSTRSPACE(DIGITS(result) - 2, expdest);
376 USTPUTC('\0', expdest);
377 start = stackblock();
379 while (p >= start && *p != CTLARI)
381 if (p < start || *p != CTLARI)
382 error("missing CTLARI (shouldn't happen)");
383 if (p > start && *(p - 1) == CTLESC)
384 for (p = start; *p != CTLARI; p++)
393 removerecordregions(begoff);
397 fmtstr(p, DIGITS(result), ARITH_FORMAT_STR, result);
401 recordregion(begoff, p - 1 - start, 0);
402 result = expdest - p + 1;
403 STADJUST(-result, expdest);
408 * Expand stuff in backwards quotes.
412 expbackq(union node *cmd, int quoted, int flag)
418 char *dest = expdest;
419 struct ifsregion saveifs, *savelastp;
420 struct nodelist *saveargbackq;
422 int startloc = dest - stackblock();
423 char const *syntax = quoted? DQSYNTAX : BASESYNTAX;
425 int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR);
430 savelastp = ifslastp;
431 saveargbackq = argbackq;
434 p = grabstackstr(dest);
435 evalbackcmd(cmd, &in);
436 ungrabstackstr(p, dest);
438 ifslastp = savelastp;
439 argbackq = saveargbackq;
445 /* Don't copy trailing newlines */
447 if (--in.nleft < 0) {
450 while ((i = read(in.fd, buf, sizeof buf)) < 0 && errno == EINTR);
451 TRACE(("expbackq: read returns %d\n", i));
459 if (quotes && syntax[(int)lastc] == CCTL)
460 STPUTC(CTLESC, dest);
478 exitstatus = waitforjob(in.jp, (int *)NULL);
480 recordregion(startloc, dest - stackblock(), 0);
481 TRACE(("evalbackq: size=%d: \"%.*s\"\n",
482 (dest - stackblock()) - startloc,
483 (dest - stackblock()) - startloc,
484 stackblock() + startloc));
492 subevalvar(char *p, char *str, int strloc, int subtype, int startloc,
499 int saveherefd = herefd;
500 struct nodelist *saveargbackq = argbackq;
505 STACKSTRNUL(expdest);
507 argbackq = saveargbackq;
508 startp = stackblock() + startloc;
510 str = stackblock() + strloc;
514 setvar(str, startp, 0);
515 amount = startp - expdest;
516 STADJUST(amount, expdest);
523 if (*p != CTLENDVAR) {
524 outfmt(&errout, "%s\n", startp);
527 error("%.*s: parameter %snot set", (int)(p - str - 1),
528 str, (varflags & VSNUL) ? "null or "
533 for (loc = startp; loc < str; loc++) {
536 if (patmatch(str, startp, varflags & VSQUOTE)) {
541 if ((varflags & VSQUOTE) && *loc == CTLESC)
547 for (loc = str - 1; loc >= startp;) {
550 if (patmatch(str, startp, varflags & VSQUOTE)) {
556 if ((varflags & VSQUOTE) && loc > startp &&
557 *(loc - 1) == CTLESC) {
558 for (q = startp; q < loc; q++)
568 for (loc = str - 1; loc >= startp;) {
569 if (patmatch(str, loc, varflags & VSQUOTE)) {
570 amount = loc - expdest;
571 STADJUST(amount, expdest);
575 if ((varflags & VSQUOTE) && loc > startp &&
576 *(loc - 1) == CTLESC) {
577 for (q = startp; q < loc; q++)
587 for (loc = startp; loc < str - 1; loc++) {
588 if (patmatch(str, loc, varflags & VSQUOTE)) {
589 amount = loc - expdest;
590 STADJUST(amount, expdest);
593 if ((varflags & VSQUOTE) && *loc == CTLESC)
604 amount = ((str - 1) - (loc - startp)) - expdest;
605 STADJUST(amount, expdest);
606 while (loc != str - 1)
613 * Expand a variable, and return a pointer to the next character in the
618 evalvar(char *p, int flag)
631 int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR);
633 varflags = (unsigned char)*p++;
634 subtype = varflags & VSTYPE;
639 p = strchr(p, '=') + 1;
640 again: /* jump here after setting a variable with ${var=text} */
641 if (varflags & VSLINENO) {
645 p[-1] = '\0'; /* temporarily overwrite '=' to have \0
647 } else if (special) {
648 set = varisset(var, varflags & VSNUL);
651 val = bltinlookup(var, 1);
652 if (val == NULL || ((varflags & VSNUL) && val[0] == '\0')) {
659 startloc = expdest - stackblock();
668 error("%.*s: parameter not set", (int)(p - var - 1),
672 if (set && subtype != VSPLUS) {
673 /* insert the value of the variable */
675 varvalue(var, varflags & VSQUOTE, subtype, flag);
676 if (subtype == VSLENGTH) {
677 varlen = expdest - stackblock() - startloc;
678 STADJUST(-varlen, expdest);
681 char const *syntax = (varflags & VSQUOTE) ? DQSYNTAX
684 if (subtype == VSLENGTH) {
691 syntax[(int)*val] == CCTL)
692 STPUTC(CTLESC, expdest);
693 STPUTC(*val++, expdest);
700 if (subtype == VSPLUS)
703 easy = ((varflags & VSQUOTE) == 0 ||
704 (*var == '@' && shellparam.nparam != 1));
709 expdest = cvtnum(varlen, expdest);
716 recordregion(startloc, expdest - stackblock(),
737 * Terminate the string and start recording the pattern
740 STPUTC('\0', expdest);
741 patloc = expdest - stackblock();
742 if (subevalvar(p, NULL, patloc, subtype,
743 startloc, varflags) == 0) {
744 int amount = (expdest - stackblock() - patloc) + 1;
745 STADJUST(-amount, expdest);
747 /* Remove any recorded regions beyond start of variable */
748 removerecordregions(startloc);
754 if (subevalvar(p, var, 0, subtype, startloc, varflags)) {
757 * Remove any recorded regions beyond
760 removerecordregions(startloc);
771 error("${%.*s%s}: Bad substitution", c, var,
772 (c > 0 && *p != CTLENDVAR) ? "..." : "");
777 p[-1] = '='; /* recover overwritten '=' */
779 if (subtype != VSNORMAL) { /* skip to end of alternative */
782 if ((c = *p++) == CTLESC)
784 else if (c == CTLBACKQ || c == (CTLBACKQ|CTLQUOTE)) {
786 argbackq = argbackq->next;
787 } else if (c == CTLVAR) {
788 if ((*p++ & VSTYPE) != VSNORMAL)
790 } else if (c == CTLENDVAR) {
802 * Test whether a specialized variable is set.
806 varisset(char *name, int nulok)
810 return backgndpid != -1;
811 else if (*name == '@' || *name == '*') {
812 if (*shellparam.p == NULL)
818 for (av = shellparam.p; *av; av++)
823 } else if (is_digit(*name)) {
825 int num = atoi(name);
827 if (num > shellparam.nparam)
833 ap = shellparam.p[num - 1];
835 if (nulok && (ap == NULL || *ap == '\0'))
844 * Add the value of a specialized variable to the stack string.
848 varvalue(char *name, int quoted, int subtype, int flag)
853 extern int oexitstatus;
858 #define STRTODEST(p) \
860 if (flag & (EXP_FULL | EXP_CASE) && subtype != VSLENGTH) { \
861 syntax = quoted? DQSYNTAX : BASESYNTAX; \
863 if (syntax[(int)*p] == CCTL) \
864 STPUTC(CTLESC, expdest); \
865 STPUTC(*p++, expdest); \
869 STPUTC(*p++, expdest); \
881 num = shellparam.nparam;
886 expdest = cvtnum(num, expdest);
889 for (i = 0 ; i < NOPTS ; i++) {
891 STPUTC(optlist[i].letter, expdest);
895 if (flag & EXP_FULL && quoted) {
896 for (ap = shellparam.p ; (p = *ap++) != NULL ; ) {
899 STPUTC('\0', expdest);
909 for (ap = shellparam.p ; (p = *ap++) != NULL ; ) {
912 STPUTC(sep, expdest);
920 if (is_digit(*name)) {
922 if (num > 0 && num <= shellparam.nparam) {
923 p = shellparam.p[num - 1];
934 * Record the the fact that we have to scan this region of the
935 * string for IFS characters.
939 recordregion(int start, int end, int inquotes)
941 struct ifsregion *ifsp;
943 if (ifslastp == NULL) {
946 if (ifslastp->endoff == start
947 && ifslastp->inquotes == inquotes) {
948 /* extend previous area */
949 ifslastp->endoff = end;
952 ifsp = (struct ifsregion *)ckmalloc(sizeof (struct ifsregion));
953 ifslastp->next = ifsp;
956 ifslastp->next = NULL;
957 ifslastp->begoff = start;
958 ifslastp->endoff = end;
959 ifslastp->inquotes = inquotes;
965 * Break the argument string into pieces based upon IFS and add the
966 * strings to the argument list. The regions of the string to be
967 * searched for IFS characters have been stored by recordregion.
970 ifsbreakup(char *string, struct arglist *arglist)
972 struct ifsregion *ifsp;
979 int had_param_ch = 0;
983 if (ifslastp == NULL) {
984 /* Return entire argument, IFS doesn't apply to any of it */
985 sp = (struct strlist *)stalloc(sizeof *sp);
987 *arglist->lastp = sp;
988 arglist->lastp = &sp->next;
992 ifs = ifsset() ? ifsval() : " \t\n";
994 for (ifsp = &ifsfirst; ifsp != NULL; ifsp = ifsp->next) {
995 p = string + ifsp->begoff;
996 while (p < string + ifsp->endoff) {
1000 if (ifsp->inquotes) {
1001 /* Only NULs (should be from "$@") end args */
1009 if (!strchr(ifs, *p)) {
1014 ifsspc = strchr(" \t\n", *p);
1016 /* Ignore IFS whitespace at start */
1017 if (q == start && ifsspc != NULL) {
1025 /* Save this argument... */
1027 sp = (struct strlist *)stalloc(sizeof *sp);
1029 *arglist->lastp = sp;
1030 arglist->lastp = &sp->next;
1033 if (ifsspc != NULL) {
1034 /* Ignore further trailing IFS whitespace */
1035 for (; p < string + ifsp->endoff; p++) {
1039 if (strchr(ifs, *p) == NULL) {
1043 if (strchr(" \t\n", *p) == NULL) {
1054 * Save anything left as an argument.
1055 * Traditionally we have treated 'IFS=':'; set -- x$IFS' as
1056 * generating 2 arguments, the second of which is empty.
1057 * Some recent clarification of the Posix spec say that it
1058 * should only generate one....
1060 if (had_param_ch || *start != 0) {
1061 sp = (struct strlist *)stalloc(sizeof *sp);
1063 *arglist->lastp = sp;
1064 arglist->lastp = &sp->next;
1071 * Expand shell metacharacters. At this point, the only control characters
1072 * should be escapes. The results are stored in the list exparg.
1075 STATIC char *expdir;
1079 expandmeta(struct strlist *str, int flag __unused)
1082 struct strlist **savelastp;
1085 /* TODO - EXP_REDIR */
1091 for (;;) { /* fast check for meta chars */
1092 if ((c = *p++) == '\0')
1094 if (c == '*' || c == '?' || c == '[' || c == '!')
1097 savelastp = exparg.lastp;
1099 if (expdir == NULL) {
1100 int i = strlen(str->text);
1101 expdir = ckmalloc(i < 2048 ? 2048 : i); /* XXX */
1104 expmeta(expdir, str->text);
1108 if (exparg.lastp == savelastp) {
1113 *exparg.lastp = str;
1114 rmescapes(str->text);
1115 exparg.lastp = &str->next;
1117 *exparg.lastp = NULL;
1118 *savelastp = sp = expsort(*savelastp);
1119 while (sp->next != NULL)
1121 exparg.lastp = &sp->next;
1129 * Do metacharacter (i.e. *, ?, [...]) expansion.
1133 expmeta(char *enddir, char *name)
1148 for (p = name ; ; p++) {
1149 if (*p == '*' || *p == '?')
1151 else if (*p == '[') {
1153 if (*q == '!' || *q == '^')
1156 while (*q == CTLQUOTEMARK)
1160 if (*q == '/' || *q == '\0')
1167 } else if (*p == '!' && p[1] == '!' && (p == name || p[-1] == '/')) {
1169 } else if (*p == '\0')
1171 else if (*p == CTLQUOTEMARK)
1173 else if (*p == CTLESC)
1181 if (metaflag == 0) { /* we've reached the end of the file name */
1182 if (enddir != expdir)
1184 for (p = name ; ; p++) {
1185 if (*p == CTLQUOTEMARK)
1193 if (metaflag == 0 || lstat(expdir, &statb) >= 0)
1198 if (start != name) {
1201 while (*p == CTLQUOTEMARK)
1208 if (enddir == expdir) {
1210 } else if (enddir == expdir + 1 && *expdir == '/') {
1216 if ((dirp = opendir(p)) == NULL)
1218 if (enddir != expdir)
1220 if (*endname == 0) {
1228 while (*p == CTLQUOTEMARK)
1234 while (! int_pending() && (dp = readdir(dirp)) != NULL) {
1235 if (dp->d_name[0] == '.' && ! matchdot)
1237 if (patmatch(start, dp->d_name, 0)) {
1239 scopy(dp->d_name, enddir);
1242 for (p = enddir, q = dp->d_name;
1243 (*p++ = *q++) != '\0';)
1246 expmeta(p, endname);
1257 * Add a file name to the list.
1261 addfname(char *name)
1266 p = stalloc(strlen(name) + 1);
1268 sp = (struct strlist *)stalloc(sizeof *sp);
1271 exparg.lastp = &sp->next;
1276 * Sort the results of file name expansion. It calculates the number of
1277 * strings to sort and then calls msort (short for merge sort) to do the
1281 STATIC struct strlist *
1282 expsort(struct strlist *str)
1288 for (sp = str ; sp ; sp = sp->next)
1290 return msort(str, len);
1294 STATIC struct strlist *
1295 msort(struct strlist *list, int len)
1297 struct strlist *p, *q = NULL;
1298 struct strlist **lpp;
1306 for (n = half ; --n >= 0 ; ) {
1310 q->next = NULL; /* terminate first half of list */
1311 q = msort(list, half); /* sort first half of list */
1312 p = msort(p, len - half); /* sort second half */
1315 if (strcmp(p->text, q->text) < 0) {
1318 if ((p = *lpp) == NULL) {
1325 if ((q = *lpp) == NULL) {
1337 * Returns true if the pattern matches the string.
1341 patmatch(char *pattern, char *string, int squoted)
1344 if (pattern[0] == '!' && pattern[1] == '!')
1345 return 1 - pmatch(pattern + 2, string);
1348 return pmatch(pattern, string, squoted);
1353 pmatch(char *pattern, char *string, int squoted)
1365 if (squoted && *q == CTLESC)
1373 if (squoted && *q == CTLESC)
1380 while (c == CTLQUOTEMARK || c == '*')
1382 if (c != CTLESC && c != CTLQUOTEMARK &&
1383 c != '?' && c != '*' && c != '[') {
1385 if (squoted && *q == CTLESC &&
1390 if (squoted && *q == CTLESC)
1396 if (pmatch(p, q, squoted))
1398 if (squoted && *q == CTLESC)
1400 } while (*q++ != '\0');
1408 if (*endp == '!' || *endp == '^')
1411 while (*endp == CTLQUOTEMARK)
1414 goto dft; /* no matching ] */
1415 if (*endp == CTLESC)
1421 if (*p == '!' || *p == '^') {
1427 if (squoted && chr == CTLESC)
1433 if (c == CTLQUOTEMARK)
1437 if (*p == '-' && p[1] != ']') {
1439 while (*p == CTLQUOTEMARK)
1443 if ( collate_range_cmp(chr, c) >= 0
1444 && collate_range_cmp(chr, *p) <= 0
1452 } while ((c = *p++) != ']');
1453 if (found == invert)
1458 if (squoted && *q == CTLESC)
1474 * Remove any CTLESC characters from a string.
1478 rmescapes(char *str)
1483 while (*p != CTLESC && *p != CTLQUOTEMARK) {
1489 if (*p == CTLQUOTEMARK) {
1503 * See if a pattern matches in a case statement.
1507 casematch(union node *pattern, char *val)
1509 struct stackmark smark;
1513 setstackmark(&smark);
1514 argbackq = pattern->narg.backquote;
1515 STARTSTACKSTR(expdest);
1517 argstr(pattern->narg.text, EXP_TILDE | EXP_CASE);
1518 STPUTC('\0', expdest);
1519 p = grabstackstr(expdest);
1520 result = patmatch(p, val, 0);
1521 popstackmark(&smark);
1530 cvtnum(int num, char *buf)
1534 char *p = temp + 31;
1539 *--p = num % 10 + '0';
1540 } while ((num /= 10) != 0);
1551 * Do most of the work for wordexp(3).
1555 wordexpcmd(int argc, char **argv)
1560 out1fmt("%08x", argc - 1);
1561 for (i = 1, len = 0; i < argc; i++)
1562 len += strlen(argv[i]);
1563 out1fmt("%08x", (int)len);
1564 for (i = 1; i < argc; i++) {