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 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 */
222 split_lit = flag & EXP_SPLIT_LIT;
223 lit_quoted = flag & EXP_LIT_QUOTED;
224 flag &= ~(EXP_SPLIT_LIT | EXP_LIT_QUOTED);
225 if (*p == '~' && (flag & (EXP_TILDE | EXP_VARTILDE)))
226 p = exptilde(p, flag);
234 /* "$@" syntax adherence hack */
235 if (p[0] == CTLVAR && p[2] == '@' && p[3] == '=')
237 if ((flag & EXP_FULL) != 0)
248 if (split_lit && !lit_quoted)
249 recordregion(expdest - stackblock() -
251 expdest - stackblock(), 0);
254 p = evalvar(p, flag);
257 case CTLBACKQ|CTLQUOTE:
258 expbackq(argbackq->n, c & CTLQUOTE, flag);
259 argbackq = argbackq->next;
267 * sort of a hack - expand tildes in variable
268 * assignments (after the first '=' and after ':'s).
271 if (split_lit && !lit_quoted)
272 recordregion(expdest - stackblock() - 1,
273 expdest - stackblock(), 0);
274 if (flag & EXP_VARTILDE && *p == '~' &&
275 (c != '=' || firsteq)) {
278 p = exptilde(p, flag);
283 if (split_lit && !lit_quoted)
284 recordregion(expdest - stackblock() - 1,
285 expdest - stackblock(), 0);
292 * Perform tilde expansion, placing the result in the stack string and
293 * returning the next position in the input string to process.
296 exptilde(char *p, int flag)
301 int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR);
303 while ((c = *p) != '\0') {
305 case CTLESC: /* This means CTL* are always considered quoted. */
308 case CTLBACKQ | CTLQUOTE:
314 if (flag & EXP_VARTILDE)
325 if (*(startp+1) == '\0') {
326 if ((home = lookupvar("HOME")) == NULL)
329 if ((pw = getpwnam(startp+1)) == NULL)
336 while ((c = *home++) != '\0') {
337 if (quotes && SQSYNTAX[(int)c] == CCTL)
338 STPUTC(CTLESC, expdest);
349 removerecordregions(int endoff)
351 if (ifslastp == NULL)
354 if (ifsfirst.endoff > endoff) {
355 while (ifsfirst.next != NULL) {
356 struct ifsregion *ifsp;
358 ifsp = ifsfirst.next->next;
359 ckfree(ifsfirst.next);
360 ifsfirst.next = ifsp;
363 if (ifsfirst.begoff > endoff)
366 ifslastp = &ifsfirst;
367 ifsfirst.endoff = endoff;
372 ifslastp = &ifsfirst;
373 while (ifslastp->next && ifslastp->next->begoff < endoff)
374 ifslastp=ifslastp->next;
375 while (ifslastp->next != NULL) {
376 struct ifsregion *ifsp;
378 ifsp = ifslastp->next->next;
379 ckfree(ifslastp->next);
380 ifslastp->next = ifsp;
383 if (ifslastp->endoff > endoff)
384 ifslastp->endoff = endoff;
388 * Expand arithmetic expression. Backup to start of expression,
389 * evaluate, place result in (backed up) result, adjust string position.
397 int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR);
401 * This routine is slightly over-complicated for
402 * efficiency. First we make sure there is
403 * enough space for the result, which may be bigger
404 * than the expression. Next we
405 * scan backwards looking for the start of arithmetic. If the
406 * next previous character is a CTLESC character, then we
407 * have to rescan starting from the beginning since CTLESC
408 * characters have to be processed left to right.
410 CHECKSTRSPACE(DIGITS(result) - 2, expdest);
411 USTPUTC('\0', expdest);
412 start = stackblock();
414 while (p >= start && *p != CTLARI)
416 if (p < start || *p != CTLARI)
417 error("missing CTLARI (shouldn't happen)");
418 if (p > start && *(p - 1) == CTLESC)
419 for (p = start; *p != CTLARI; p++)
428 removerecordregions(begoff);
431 q = grabstackstr(expdest);
433 ungrabstackstr(q, expdest);
434 fmtstr(p, DIGITS(result), ARITH_FORMAT_STR, result);
438 recordregion(begoff, p - 1 - start, 0);
439 result = expdest - p + 1;
440 STADJUST(-result, expdest);
445 * Perform command substitution.
448 expbackq(union node *cmd, int quoted, int flag)
454 char *dest = expdest;
455 struct ifsregion saveifs, *savelastp;
456 struct nodelist *saveargbackq;
458 int startloc = dest - stackblock();
459 char const *syntax = quoted? DQSYNTAX : BASESYNTAX;
461 int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR);
466 savelastp = ifslastp;
467 saveargbackq = argbackq;
470 p = grabstackstr(dest);
471 evalbackcmd(cmd, &in);
472 ungrabstackstr(p, dest);
474 ifslastp = savelastp;
475 argbackq = saveargbackq;
481 /* Don't copy trailing newlines */
483 if (--in.nleft < 0) {
486 while ((i = read(in.fd, buf, sizeof buf)) < 0 && errno == EINTR);
487 TRACE(("expbackq: read returns %d\n", i));
495 if (quotes && syntax[(int)lastc] == CCTL)
496 STPUTC(CTLESC, dest);
514 exitstatus = waitforjob(in.jp, (int *)NULL);
516 recordregion(startloc, dest - stackblock(), 0);
517 TRACE(("expbackq: size=%td: \"%.*s\"\n",
518 ((dest - stackblock()) - startloc),
519 (int)((dest - stackblock()) - startloc),
520 stackblock() + startloc));
528 subevalvar(char *p, char *str, int strloc, int subtype, int startloc,
535 int saveherefd = herefd;
536 struct nodelist *saveargbackq = argbackq;
540 argstr(p, (subtype == VSTRIMLEFT || subtype == VSTRIMLEFTMAX ||
541 subtype == VSTRIMRIGHT || subtype == VSTRIMRIGHTMAX ?
542 EXP_CASE : 0) | EXP_TILDE);
543 STACKSTRNUL(expdest);
545 argbackq = saveargbackq;
546 startp = stackblock() + startloc;
548 str = stackblock() + strloc;
552 setvar(str, startp, 0);
553 amount = startp - expdest;
554 STADJUST(amount, expdest);
561 if (*p != CTLENDVAR) {
562 outfmt(out2, "%s\n", startp);
565 error("%.*s: parameter %snot set", (int)(p - str - 1),
566 str, (varflags & VSNUL) ? "null or "
571 for (loc = startp; loc < str; loc++) {
574 if (patmatch(str, startp, varflags & VSQUOTE)) {
579 if ((varflags & VSQUOTE) && *loc == CTLESC)
585 for (loc = str - 1; loc >= startp;) {
588 if (patmatch(str, startp, varflags & VSQUOTE)) {
594 if ((varflags & VSQUOTE) && loc > startp &&
595 *(loc - 1) == CTLESC) {
596 for (q = startp; q < loc; q++)
606 for (loc = str - 1; loc >= startp;) {
607 if (patmatch(str, loc, varflags & VSQUOTE)) {
608 amount = loc - expdest;
609 STADJUST(amount, expdest);
613 if ((varflags & VSQUOTE) && loc > startp &&
614 *(loc - 1) == CTLESC) {
615 for (q = startp; q < loc; q++)
625 for (loc = startp; loc < str - 1; loc++) {
626 if (patmatch(str, loc, varflags & VSQUOTE)) {
627 amount = loc - expdest;
628 STADJUST(amount, expdest);
631 if ((varflags & VSQUOTE) && *loc == CTLESC)
642 amount = ((str - 1) - (loc - startp)) - expdest;
643 STADJUST(amount, expdest);
644 while (loc != str - 1)
651 * Expand a variable, and return a pointer to the next character in the
656 evalvar(char *p, int flag)
669 int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR);
671 varflags = (unsigned char)*p++;
672 subtype = varflags & VSTYPE;
677 p = strchr(p, '=') + 1;
678 again: /* jump here after setting a variable with ${var=text} */
679 if (varflags & VSLINENO) {
683 p[-1] = '\0'; /* temporarily overwrite '=' to have \0
685 } else if (special) {
686 set = varisset(var, varflags & VSNUL);
689 val = bltinlookup(var, 1);
690 if (val == NULL || ((varflags & VSNUL) && val[0] == '\0')) {
697 startloc = expdest - stackblock();
698 if (!set && uflag && *var != '@' && *var != '*') {
706 error("%.*s: parameter not set", (int)(p - var - 1),
710 if (set && subtype != VSPLUS) {
711 /* insert the value of the variable */
713 varvalue(var, varflags & VSQUOTE, subtype, flag);
714 if (subtype == VSLENGTH) {
715 varlen = expdest - stackblock() - startloc;
716 STADJUST(-varlen, expdest);
719 char const *syntax = (varflags & VSQUOTE) ? DQSYNTAX
722 if (subtype == VSLENGTH) {
729 syntax[(int)*val] == CCTL)
730 STPUTC(CTLESC, expdest);
731 STPUTC(*val++, expdest);
738 if (subtype == VSPLUS)
741 easy = ((varflags & VSQUOTE) == 0 ||
742 (*var == '@' && shellparam.nparam != 1));
747 expdest = cvtnum(varlen, expdest);
754 recordregion(startloc, expdest - stackblock(),
761 argstr(p, flag | (flag & EXP_FULL ? EXP_SPLIT_LIT : 0) |
762 (varflags & VSQUOTE ? EXP_LIT_QUOTED : 0));
776 * Terminate the string and start recording the pattern
779 STPUTC('\0', expdest);
780 patloc = expdest - stackblock();
781 if (subevalvar(p, NULL, patloc, subtype,
782 startloc, varflags) == 0) {
783 int amount = (expdest - stackblock() - patloc) + 1;
784 STADJUST(-amount, expdest);
786 /* Remove any recorded regions beyond start of variable */
787 removerecordregions(startloc);
793 if (subevalvar(p, var, 0, subtype, startloc, varflags)) {
796 * Remove any recorded regions beyond
799 removerecordregions(startloc);
810 error("${%.*s%s}: Bad substitution", c, var,
811 (c > 0 && *p != CTLENDVAR) ? "..." : "");
816 p[-1] = '='; /* recover overwritten '=' */
818 if (subtype != VSNORMAL) { /* skip to end of alternative */
821 if ((c = *p++) == CTLESC)
823 else if (c == CTLBACKQ || c == (CTLBACKQ|CTLQUOTE)) {
825 argbackq = argbackq->next;
826 } else if (c == CTLVAR) {
827 if ((*p++ & VSTYPE) != VSNORMAL)
829 } else if (c == CTLENDVAR) {
841 * Test whether a specialized variable is set.
845 varisset(char *name, int nulok)
849 return backgndpidset();
850 else if (*name == '@' || *name == '*') {
851 if (*shellparam.p == NULL)
857 for (av = shellparam.p; *av; av++)
862 } else if (is_digit(*name)) {
864 int num = atoi(name);
866 if (num > shellparam.nparam)
872 ap = shellparam.p[num - 1];
874 if (nulok && (ap == NULL || *ap == '\0'))
883 * Add the value of a specialized variable to the stack string.
887 varvalue(char *name, int quoted, int subtype, int flag)
896 #define STRTODEST(p) \
898 if (flag & (EXP_FULL | EXP_CASE) && subtype != VSLENGTH) { \
899 syntax = quoted? DQSYNTAX : BASESYNTAX; \
901 if (syntax[(int)*p] == CCTL) \
902 STPUTC(CTLESC, expdest); \
903 STPUTC(*p++, expdest); \
907 STPUTC(*p++, expdest); \
919 num = shellparam.nparam;
922 num = backgndpidval();
924 expdest = cvtnum(num, expdest);
927 for (i = 0 ; i < NOPTS ; i++) {
929 STPUTC(optlist[i].letter, expdest);
933 if (flag & EXP_FULL && quoted) {
934 for (ap = shellparam.p ; (p = *ap++) != NULL ; ) {
937 STPUTC('\0', expdest);
947 for (ap = shellparam.p ; (p = *ap++) != NULL ; ) {
950 STPUTC(sep, expdest);
958 if (is_digit(*name)) {
960 if (num > 0 && num <= shellparam.nparam) {
961 p = shellparam.p[num - 1];
972 * Record the the fact that we have to scan this region of the
973 * string for IFS characters.
977 recordregion(int start, int end, int inquotes)
979 struct ifsregion *ifsp;
981 if (ifslastp == NULL) {
984 if (ifslastp->endoff == start
985 && ifslastp->inquotes == inquotes) {
986 /* extend previous area */
987 ifslastp->endoff = end;
990 ifsp = (struct ifsregion *)ckmalloc(sizeof (struct ifsregion));
991 ifslastp->next = ifsp;
994 ifslastp->next = NULL;
995 ifslastp->begoff = start;
996 ifslastp->endoff = end;
997 ifslastp->inquotes = inquotes;
1003 * Break the argument string into pieces based upon IFS and add the
1004 * strings to the argument list. The regions of the string to be
1005 * searched for IFS characters have been stored by recordregion.
1006 * CTLESC characters are preserved but have little effect in this pass
1007 * other than escaping CTL* characters. In particular, they do not escape
1008 * IFS characters: that should be done with the ifsregion mechanism.
1009 * CTLQUOTEMARK characters are used to preserve empty quoted strings.
1010 * This pass treats them as a regular character, making the string non-empty.
1011 * Later, they are removed along with the other CTL* characters.
1014 ifsbreakup(char *string, struct arglist *arglist)
1016 struct ifsregion *ifsp;
1023 int had_param_ch = 0;
1027 if (ifslastp == NULL) {
1028 /* Return entire argument, IFS doesn't apply to any of it */
1029 sp = (struct strlist *)stalloc(sizeof *sp);
1031 *arglist->lastp = sp;
1032 arglist->lastp = &sp->next;
1036 ifs = ifsset() ? ifsval() : " \t\n";
1038 for (ifsp = &ifsfirst; ifsp != NULL; ifsp = ifsp->next) {
1039 p = string + ifsp->begoff;
1040 while (p < string + ifsp->endoff) {
1044 if (ifsp->inquotes) {
1045 /* Only NULs (should be from "$@") end args */
1053 if (!strchr(ifs, *p)) {
1058 ifsspc = strchr(" \t\n", *p);
1060 /* Ignore IFS whitespace at start */
1061 if (q == start && ifsspc != NULL) {
1069 /* Save this argument... */
1071 sp = (struct strlist *)stalloc(sizeof *sp);
1073 *arglist->lastp = sp;
1074 arglist->lastp = &sp->next;
1077 if (ifsspc != NULL) {
1078 /* Ignore further trailing IFS whitespace */
1079 for (; p < string + ifsp->endoff; p++) {
1083 if (strchr(ifs, *p) == NULL) {
1087 if (strchr(" \t\n", *p) == NULL) {
1098 * Save anything left as an argument.
1099 * Traditionally we have treated 'IFS=':'; set -- x$IFS' as
1100 * generating 2 arguments, the second of which is empty.
1101 * Some recent clarification of the Posix spec say that it
1102 * should only generate one....
1104 if (had_param_ch || *start != 0) {
1105 sp = (struct strlist *)stalloc(sizeof *sp);
1107 *arglist->lastp = sp;
1108 arglist->lastp = &sp->next;
1113 static char expdir[PATH_MAX];
1114 #define expdir_end (expdir + sizeof(expdir))
1117 * Perform pathname generation and remove control characters.
1118 * At this point, the only control characters should be CTLESC and CTLQUOTEMARK.
1119 * The results are stored in the list exparg.
1122 expandmeta(struct strlist *str, int flag __unused)
1125 struct strlist **savelastp;
1128 /* TODO - EXP_REDIR */
1134 for (;;) { /* fast check for meta chars */
1135 if ((c = *p++) == '\0')
1137 if (c == '*' || c == '?' || c == '[')
1140 savelastp = exparg.lastp;
1142 expmeta(expdir, str->text);
1144 if (exparg.lastp == savelastp) {
1149 *exparg.lastp = str;
1150 rmescapes(str->text);
1151 exparg.lastp = &str->next;
1153 *exparg.lastp = NULL;
1154 *savelastp = sp = expsort(*savelastp);
1155 while (sp->next != NULL)
1157 exparg.lastp = &sp->next;
1165 * Do metacharacter (i.e. *, ?, [...]) expansion.
1169 expmeta(char *enddir, char *name)
1185 for (p = name; esc = 0, *p; p += esc + 1) {
1186 if (*p == '*' || *p == '?')
1188 else if (*p == '[') {
1190 if (*q == '!' || *q == '^')
1193 while (*q == CTLQUOTEMARK)
1197 if (*q == '/' || *q == '\0')
1204 } else if (*p == '\0')
1206 else if (*p == CTLQUOTEMARK)
1211 if (p[esc] == '/') {
1214 start = p + esc + 1;
1218 if (metaflag == 0) { /* we've reached the end of the file name */
1219 if (enddir != expdir)
1221 for (p = name ; ; p++) {
1222 if (*p == CTLQUOTEMARK)
1229 if (enddir == expdir_end)
1232 if (metaflag == 0 || lstat(expdir, &statb) >= 0)
1237 if (start != name) {
1240 while (*p == CTLQUOTEMARK)
1245 if (enddir == expdir_end)
1249 if (enddir == expdir) {
1251 } else if (enddir == expdir + 1 && *expdir == '/') {
1257 if ((dirp = opendir(p)) == NULL)
1259 if (enddir != expdir)
1261 if (*endname == 0) {
1270 while (*p == CTLQUOTEMARK)
1276 while (! int_pending() && (dp = readdir(dirp)) != NULL) {
1277 if (dp->d_name[0] == '.' && ! matchdot)
1279 if (patmatch(start, dp->d_name, 0)) {
1280 if (enddir + dp->d_namlen + 1 > expdir_end)
1282 memcpy(enddir, dp->d_name, dp->d_namlen + 1);
1286 if (enddir + dp->d_namlen + 2 > expdir_end)
1288 enddir[dp->d_namlen] = '/';
1289 enddir[dp->d_namlen + 1] = '\0';
1290 expmeta(enddir + dp->d_namlen + 1, endname);
1296 endname[-esc - 1] = esc ? CTLESC : '/';
1301 * Add a file name to the list.
1305 addfname(char *name)
1310 p = stalloc(strlen(name) + 1);
1312 sp = (struct strlist *)stalloc(sizeof *sp);
1315 exparg.lastp = &sp->next;
1320 * Sort the results of file name expansion. It calculates the number of
1321 * strings to sort and then calls msort (short for merge sort) to do the
1325 static struct strlist *
1326 expsort(struct strlist *str)
1332 for (sp = str ; sp ; sp = sp->next)
1334 return msort(str, len);
1338 static struct strlist *
1339 msort(struct strlist *list, int len)
1341 struct strlist *p, *q = NULL;
1342 struct strlist **lpp;
1350 for (n = half ; --n >= 0 ; ) {
1354 q->next = NULL; /* terminate first half of list */
1355 q = msort(list, half); /* sort first half of list */
1356 p = msort(p, len - half); /* sort second half */
1359 if (strcmp(p->text, q->text) < 0) {
1362 if ((p = *lpp) == NULL) {
1369 if ((q = *lpp) == NULL) {
1381 * Returns true if the pattern matches the string.
1385 patmatch(const char *pattern, const char *string, int squoted)
1397 if (squoted && *q == CTLESC)
1405 if (squoted && *q == CTLESC)
1412 while (c == CTLQUOTEMARK || c == '*')
1414 if (c != CTLESC && c != CTLQUOTEMARK &&
1415 c != '?' && c != '*' && c != '[') {
1417 if (squoted && *q == CTLESC &&
1422 if (squoted && *q == CTLESC)
1428 if (patmatch(p, q, squoted))
1430 if (squoted && *q == CTLESC)
1432 } while (*q++ != '\0');
1440 if (*endp == '!' || *endp == '^')
1443 while (*endp == CTLQUOTEMARK)
1446 goto dft; /* no matching ] */
1447 if (*endp == CTLESC)
1453 if (*p == '!' || *p == '^') {
1459 if (squoted && chr == CTLESC)
1465 if (c == CTLQUOTEMARK)
1469 if (*p == '-' && p[1] != ']') {
1471 while (*p == CTLQUOTEMARK)
1475 if ( collate_range_cmp(chr, c) >= 0
1476 && collate_range_cmp(chr, *p) <= 0
1484 } while ((c = *p++) != ']');
1485 if (found == invert)
1490 if (squoted && *q == CTLESC)
1506 * Remove any CTLESC and CTLQUOTEMARK characters from a string.
1510 rmescapes(char *str)
1515 while (*p != CTLESC && *p != CTLQUOTEMARK && *p != CTLQUOTEEND) {
1521 if (*p == CTLQUOTEMARK || *p == CTLQUOTEEND) {
1535 * See if a pattern matches in a case statement.
1539 casematch(union node *pattern, const char *val)
1541 struct stackmark smark;
1545 setstackmark(&smark);
1546 argbackq = pattern->narg.backquote;
1547 STARTSTACKSTR(expdest);
1549 argstr(pattern->narg.text, EXP_TILDE | EXP_CASE);
1550 STPUTC('\0', expdest);
1551 p = grabstackstr(expdest);
1552 result = patmatch(p, val, 0);
1553 popstackmark(&smark);
1562 cvtnum(int num, char *buf)
1566 char *p = temp + 31;
1571 *--p = num % 10 + '0';
1572 } while ((num /= 10) != 0);
1583 * Do most of the work for wordexp(3).
1587 wordexpcmd(int argc, char **argv)
1592 out1fmt("%08x", argc - 1);
1593 for (i = 1, len = 0; i < argc; i++)
1594 len += strlen(argv[i]);
1595 out1fmt("%08x", (int)len);
1596 for (i = 1; i < argc; i++) {