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>
59 * Routines to expand arguments to commands. We have to deal with
60 * backquotes, shell variables, and file metacharacters.
83 * Structure specifying which parts of the string should be searched
88 struct ifsregion *next; /* next region in list */
89 int begoff; /* offset of start of region */
90 int endoff; /* offset of end of region */
91 int inquotes; /* search for nul bytes only */
95 static char *expdest; /* output of current string */
96 static struct nodelist *argbackq; /* list of back quote expressions */
97 static struct ifsregion ifsfirst; /* first struct in list of ifs regions */
98 static struct ifsregion *ifslastp; /* last struct in list */
99 static struct arglist exparg; /* holds expanded arg list */
101 static char *argstr(char *, int);
102 static char *exptilde(char *, int);
103 static char *expari(char *);
104 static void expbackq(union node *, int, int);
105 static int subevalvar(char *, char *, int, int, int, int, int);
106 static char *evalvar(char *, int);
107 static int varisset(const char *, int);
108 static void strtodest(const char *, int, int, int);
109 static void varvalue(const char *, int, int, int);
110 static void recordregion(int, int, int);
111 static void removerecordregions(int);
112 static void ifsbreakup(char *, struct arglist *);
113 static void expandmeta(struct strlist *);
114 static void expmeta(char *, char *);
115 static void addfname(char *);
116 static struct strlist *expsort(struct strlist *);
117 static struct strlist *msort(struct strlist *, int);
118 static int patmatch(const char *, const char *, int);
119 static char *cvtnum(int, char *);
120 static int collate_range_cmp(wchar_t, wchar_t);
123 collate_range_cmp(wchar_t c1, wchar_t c2)
125 static wchar_t s1[2], s2[2];
129 return (wcscoll(s1, s2));
133 stputs_quotes(const char *data, const char *syntax, char *p)
137 if (syntax[(int)*data] == CCTL)
143 #define STPUTS_QUOTES(data, syntax, p) p = stputs_quotes((data), syntax, p)
146 * Perform expansions on an argument, placing the resulting list of arguments
147 * in arglist. Parameter expansion, command substitution and arithmetic
148 * expansion are always performed; additional expansions can be requested
150 * The result is left in the stack string.
151 * When arglist is NULL, perform here document expansion.
153 * Caution: this function uses global state and is not reentrant.
154 * However, a new invocation after an interrupted invocation is safe
155 * and will reset the global state for the new call.
158 expandarg(union node *arg, struct arglist *arglist, int flag)
163 argbackq = arg->narg.backquote;
164 STARTSTACKSTR(expdest);
165 ifsfirst.next = NULL;
167 argstr(arg->narg.text, flag);
168 if (arglist == NULL) {
169 STACKSTRNUL(expdest);
170 return; /* here document expanded */
172 STPUTC('\0', expdest);
173 p = grabstackstr(expdest);
174 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);
181 sp = (struct strlist *)stalloc(sizeof (struct strlist));
184 exparg.lastp = &sp->next;
186 while (ifsfirst.next != NULL) {
187 struct ifsregion *ifsp;
189 ifsp = ifsfirst.next->next;
190 ckfree(ifsfirst.next);
191 ifsfirst.next = ifsp;
194 *exparg.lastp = NULL;
196 *arglist->lastp = exparg.list;
197 arglist->lastp = exparg.lastp;
204 * Perform parameter expansion, command substitution and arithmetic
205 * expansion, and tilde expansion if requested via EXP_TILDE/EXP_VARTILDE.
206 * Processing ends at a CTLENDVAR or CTLENDARI character as well as '\0'.
207 * This is used to expand word in ${var+word} etc.
208 * If EXP_FULL or EXP_CASE are set, keep and/or generate CTLESC
209 * characters to allow for further processing.
210 * If EXP_FULL is set, also preserve CTLQUOTEMARK characters.
213 argstr(char *p, int flag)
216 int quotes = flag & (EXP_FULL | EXP_CASE); /* do CTLESC */
221 split_lit = flag & EXP_SPLIT_LIT;
222 lit_quoted = flag & EXP_LIT_QUOTED;
223 flag &= ~(EXP_SPLIT_LIT | EXP_LIT_QUOTED);
224 if (*p == '~' && (flag & (EXP_TILDE | EXP_VARTILDE)))
225 p = exptilde(p, flag);
227 CHECKSTRSPACE(2, expdest);
236 /* "$@" syntax adherence hack */
237 if (p[0] == CTLVAR && p[2] == '@' && p[3] == '=')
239 if ((flag & EXP_FULL) != 0)
250 if (split_lit && !lit_quoted)
251 recordregion(expdest - stackblock() -
253 expdest - stackblock(), 0);
256 p = evalvar(p, flag);
259 case CTLBACKQ|CTLQUOTE:
260 expbackq(argbackq->n, c & CTLQUOTE, flag);
261 argbackq = argbackq->next;
269 * sort of a hack - expand tildes in variable
270 * assignments (after the first '=' and after ':'s).
273 if (split_lit && !lit_quoted)
274 recordregion(expdest - stackblock() - 1,
275 expdest - stackblock(), 0);
276 if (flag & EXP_VARTILDE && *p == '~' &&
277 (c != '=' || firsteq)) {
280 p = exptilde(p, flag);
285 if (split_lit && !lit_quoted)
286 recordregion(expdest - stackblock() - 1,
287 expdest - stackblock(), 0);
293 * Perform tilde expansion, placing the result in the stack string and
294 * returning the next position in the input string to process.
297 exptilde(char *p, int flag)
306 case CTLESC: /* This means CTL* are always considered quoted. */
309 case CTLBACKQ | CTLQUOTE:
315 if ((flag & EXP_VARTILDE) == 0)
322 if (*(startp+1) == '\0') {
323 home = lookupvar("HOME");
325 pw = getpwnam(startp+1);
326 home = pw != NULL ? pw->pw_dir : NULL;
329 if (home == NULL || *home == '\0')
331 strtodest(home, flag, VSNORMAL, 1);
340 removerecordregions(int endoff)
342 if (ifslastp == NULL)
345 if (ifsfirst.endoff > endoff) {
346 while (ifsfirst.next != NULL) {
347 struct ifsregion *ifsp;
349 ifsp = ifsfirst.next->next;
350 ckfree(ifsfirst.next);
351 ifsfirst.next = ifsp;
354 if (ifsfirst.begoff > endoff)
357 ifslastp = &ifsfirst;
358 ifsfirst.endoff = endoff;
363 ifslastp = &ifsfirst;
364 while (ifslastp->next && ifslastp->next->begoff < endoff)
365 ifslastp=ifslastp->next;
366 while (ifslastp->next != NULL) {
367 struct ifsregion *ifsp;
369 ifsp = ifslastp->next->next;
370 ckfree(ifslastp->next);
371 ifslastp->next = ifsp;
374 if (ifslastp->endoff > endoff)
375 ifslastp->endoff = endoff;
379 * Expand arithmetic expression.
380 * Note that flag is not required as digits never require CTLESC characters.
391 quoted = *p++ == '"';
392 begoff = expdest - stackblock();
394 removerecordregions(begoff);
395 STPUTC('\0', expdest);
396 start = stackblock() + begoff;
398 q = grabstackstr(expdest);
399 result = arith(start);
400 ungrabstackstr(q, expdest);
402 start = stackblock() + begoff;
403 adj = start - expdest;
404 STADJUST(adj, expdest);
406 CHECKSTRSPACE((int)(DIGITS(result) + 1), expdest);
407 fmtstr(expdest, DIGITS(result), ARITH_FORMAT_STR, result);
408 adj = strlen(expdest);
409 STADJUST(adj, expdest);
411 recordregion(begoff, expdest - stackblock(), 0);
417 * Perform command substitution.
420 expbackq(union node *cmd, int quoted, int flag)
426 char *dest = expdest;
427 struct ifsregion saveifs, *savelastp;
428 struct nodelist *saveargbackq;
430 int startloc = dest - stackblock();
431 char const *syntax = quoted? DQSYNTAX : BASESYNTAX;
432 int quotes = flag & (EXP_FULL | EXP_CASE);
437 savelastp = ifslastp;
438 saveargbackq = argbackq;
439 p = grabstackstr(dest);
440 evalbackcmd(cmd, &in);
441 ungrabstackstr(p, dest);
446 /* Don't copy trailing newlines */
448 if (--in.nleft < 0) {
451 while ((i = read(in.fd, buf, sizeof buf)) < 0 && errno == EINTR);
452 TRACE(("expbackq: read returns %d\n", i));
463 CHECKSTRSPACE(nnl + 2, dest);
468 if (quotes && syntax[(int)lastc] == CCTL)
469 USTPUTC(CTLESC, dest);
470 USTPUTC(lastc, dest);
480 p = grabstackstr(dest);
481 exitstatus = waitforjob(in.jp, (int *)NULL);
482 ungrabstackstr(p, dest);
484 TRACE(("expbackq: size=%td: \"%.*s\"\n",
485 ((dest - stackblock()) - startloc),
486 (int)((dest - stackblock()) - startloc),
487 stackblock() + startloc));
489 ifslastp = savelastp;
491 recordregion(startloc, dest - stackblock(), 0);
492 argbackq = saveargbackq;
500 recordleft(const char *str, const char *loc, char *startp)
504 amount = ((str - 1) - (loc - startp)) - expdest;
505 STADJUST(amount, expdest);
506 while (loc != str - 1)
511 subevalvar(char *p, char *str, int strloc, int subtype, int startloc,
512 int varflags, int quotes)
518 struct nodelist *saveargbackq = argbackq;
521 argstr(p, (subtype == VSTRIMLEFT || subtype == VSTRIMLEFTMAX ||
522 subtype == VSTRIMRIGHT || subtype == VSTRIMRIGHTMAX ?
523 EXP_CASE : 0) | EXP_TILDE);
524 STACKSTRNUL(expdest);
525 argbackq = saveargbackq;
526 startp = stackblock() + startloc;
528 str = stackblock() + strloc;
532 setvar(str, startp, 0);
533 amount = startp - expdest;
534 STADJUST(amount, expdest);
539 if (*p != CTLENDVAR) {
540 outfmt(out2, "%s\n", startp);
543 error("%.*s: parameter %snot set", (int)(p - str - 1),
544 str, (varflags & VSNUL) ? "null or "
549 for (loc = startp; loc < str; loc++) {
552 if (patmatch(str, startp, quotes)) {
554 recordleft(str, loc, startp);
558 if (quotes && *loc == CTLESC)
564 for (loc = str - 1; loc >= startp;) {
567 if (patmatch(str, startp, quotes)) {
569 recordleft(str, loc, startp);
574 if (quotes && loc > startp && *(loc - 1) == CTLESC) {
575 for (q = startp; q < loc; q++)
585 for (loc = str - 1; loc >= startp;) {
586 if (patmatch(str, loc, quotes)) {
587 amount = loc - expdest;
588 STADJUST(amount, expdest);
592 if (quotes && loc > startp && *(loc - 1) == CTLESC) {
593 for (q = startp; q < loc; q++)
603 for (loc = startp; loc < str - 1; loc++) {
604 if (patmatch(str, loc, quotes)) {
605 amount = loc - expdest;
606 STADJUST(amount, expdest);
609 if (quotes && *loc == CTLESC)
622 * Expand a variable, and return a pointer to the next character in the
627 evalvar(char *p, int flag)
641 int quotes = flag & (EXP_FULL | EXP_CASE);
644 varflags = (unsigned char)*p++;
645 subtype = varflags & VSTYPE;
650 p = strchr(p, '=') + 1;
651 again: /* jump here after setting a variable with ${var=text} */
652 if (varflags & VSLINENO) {
656 } else if (special) {
657 set = varisset(var, varflags & VSNUL);
660 val = bltinlookup(var, 1);
661 if (val == NULL || ((varflags & VSNUL) && val[0] == '\0')) {
668 startloc = expdest - stackblock();
669 if (!set && uflag && *var != '@' && *var != '*') {
677 error("%.*s: parameter not set", (int)(p - var - 1),
681 if (set && subtype != VSPLUS) {
682 /* insert the value of the variable */
684 if (varflags & VSLINENO)
685 STPUTBIN(var, p - var - 1, expdest);
687 varvalue(var, varflags & VSQUOTE, subtype, flag);
688 if (subtype == VSLENGTH) {
689 varlenb = expdest - stackblock() - startloc;
692 val = stackblock() + startloc;
693 for (;val != expdest; val++)
694 if ((*val & 0xC0) == 0x80)
697 STADJUST(-varlenb, expdest);
700 if (subtype == VSLENGTH) {
703 (*val & 0xC0) != 0x80)
707 strtodest(val, flag, subtype,
712 if (subtype == VSPLUS)
715 easy = ((varflags & VSQUOTE) == 0 ||
716 (*var == '@' && shellparam.nparam != 1));
721 expdest = cvtnum(varlen, expdest);
732 argstr(p, flag | (flag & EXP_FULL ? EXP_SPLIT_LIT : 0) |
733 (varflags & VSQUOTE ? EXP_LIT_QUOTED : 0));
748 * Terminate the string and start recording the pattern
751 STPUTC('\0', expdest);
752 patloc = expdest - stackblock();
753 if (subevalvar(p, NULL, patloc, subtype,
754 startloc, varflags, quotes) == 0) {
755 int amount = (expdest - stackblock() - patloc) + 1;
756 STADJUST(-amount, expdest);
758 /* Remove any recorded regions beyond start of variable */
759 removerecordregions(startloc);
766 if (subevalvar(p, var, 0, subtype, startloc, varflags,
770 * Remove any recorded regions beyond
773 removerecordregions(startloc);
783 error("${%.*s%s}: Bad substitution", c, var,
784 (c > 0 && *p != CTLENDVAR) ? "..." : "");
791 recordregion(startloc, expdest - stackblock(),
792 varflags & VSQUOTE || (ifsset() && ifsval()[0] == '\0' &&
793 (*var == '@' || *var == '*')));
795 if (subtype != VSNORMAL) { /* skip to end of alternative */
798 if ((c = *p++) == CTLESC)
800 else if (c == CTLBACKQ || c == (CTLBACKQ|CTLQUOTE)) {
802 argbackq = argbackq->next;
803 } else if (c == CTLVAR) {
804 if ((*p++ & VSTYPE) != VSNORMAL)
806 } else if (c == CTLENDVAR) {
818 * Test whether a specialized variable is set.
822 varisset(const char *name, int nulok)
826 return backgndpidset();
827 else if (*name == '@' || *name == '*') {
828 if (*shellparam.p == NULL)
834 for (av = shellparam.p; *av; av++)
839 } else if (is_digit(*name)) {
844 num = strtol(name, NULL, 10);
845 if (errno != 0 || num > shellparam.nparam)
851 ap = shellparam.p[num - 1];
853 if (nulok && (ap == NULL || *ap == '\0'))
860 strtodest(const char *p, int flag, int subtype, int quoted)
862 if (flag & (EXP_FULL | EXP_CASE) && subtype != VSLENGTH)
863 STPUTS_QUOTES(p, quoted ? DQSYNTAX : BASESYNTAX, expdest);
869 * Add the value of a specialized variable to the stack string.
873 varvalue(const char *name, int quoted, int subtype, int flag)
889 num = shellparam.nparam;
892 num = backgndpidval();
894 expdest = cvtnum(num, expdest);
897 for (i = 0 ; i < NOPTS ; i++) {
899 STPUTC(optlist[i].letter, expdest);
903 if (flag & EXP_FULL && quoted) {
904 for (ap = shellparam.p ; (p = *ap++) != NULL ; ) {
905 strtodest(p, flag, subtype, quoted);
907 STPUTC('\0', expdest);
917 for (ap = shellparam.p ; (p = *ap++) != NULL ; ) {
918 strtodest(p, flag, subtype, quoted);
921 if (sep || (flag & EXP_FULL && !quoted && **ap != '\0'))
922 STPUTC(sep, expdest);
927 strtodest(p, flag, subtype, quoted);
930 if (is_digit(*name)) {
932 if (num > 0 && num <= shellparam.nparam) {
933 p = shellparam.p[num - 1];
934 strtodest(p, flag, subtype, quoted);
944 * Record the fact that we have to scan this region of the
945 * string for IFS characters.
949 recordregion(int start, int end, int inquotes)
951 struct ifsregion *ifsp;
954 if (ifslastp == NULL) {
957 if (ifslastp->endoff == start
958 && ifslastp->inquotes == inquotes) {
959 /* extend previous area */
960 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;
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)
1100 struct strlist **savelastp;
1108 for (;;) { /* fast check for meta chars */
1109 if ((c = *p++) == '\0')
1111 if (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)
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 == '\0')
1181 else if (*p == CTLQUOTEMARK)
1186 if (p[esc] == '/') {
1189 start = p + esc + 1;
1193 if (metaflag == 0) { /* we've reached the end of the file name */
1194 if (enddir != expdir)
1196 for (p = name ; ; p++) {
1197 if (*p == CTLQUOTEMARK)
1204 if (enddir == expdir_end)
1207 if (metaflag == 0 || lstat(expdir, &statb) >= 0)
1211 endname = name + (p - name);
1212 if (start != name) {
1215 while (*p == CTLQUOTEMARK)
1220 if (enddir == expdir_end)
1224 if (enddir == expdir) {
1226 } else if (enddir == expdir + 1 && *expdir == '/') {
1232 if ((dirp = opendir(p)) == NULL)
1234 if (enddir != expdir)
1236 if (*endname == 0) {
1245 while (*p == CTLQUOTEMARK)
1251 while (! int_pending() && (dp = readdir(dirp)) != NULL) {
1252 if (dp->d_name[0] == '.' && ! matchdot)
1254 if (patmatch(start, dp->d_name, 0)) {
1255 namlen = dp->d_namlen;
1256 if (enddir + namlen + 1 > expdir_end)
1258 memcpy(enddir, dp->d_name, namlen + 1);
1262 if (dp->d_type != DT_UNKNOWN &&
1263 dp->d_type != DT_DIR &&
1264 dp->d_type != DT_LNK)
1266 if (enddir + namlen + 2 > expdir_end)
1268 enddir[namlen] = '/';
1269 enddir[namlen + 1] = '\0';
1270 expmeta(enddir + namlen + 1, endname);
1276 endname[-esc - 1] = esc ? CTLESC : '/';
1281 * Add a file name to the list.
1285 addfname(char *name)
1290 p = stsavestr(name);
1291 sp = (struct strlist *)stalloc(sizeof *sp);
1294 exparg.lastp = &sp->next;
1299 * Sort the results of file name expansion. It calculates the number of
1300 * strings to sort and then calls msort (short for merge sort) to do the
1304 static struct strlist *
1305 expsort(struct strlist *str)
1311 for (sp = str ; sp ; sp = sp->next)
1313 return msort(str, len);
1317 static struct strlist *
1318 msort(struct strlist *list, int len)
1320 struct strlist *p, *q = NULL;
1321 struct strlist **lpp;
1329 for (n = half ; --n >= 0 ; ) {
1333 q->next = NULL; /* terminate first half of list */
1334 q = msort(list, half); /* sort first half of list */
1335 p = msort(p, len - half); /* sort second half */
1338 if (strcmp(p->text, q->text) < 0) {
1341 if ((p = *lpp) == NULL) {
1348 if ((q = *lpp) == NULL) {
1360 get_wc(const char **p)
1365 chrlen = mbtowc(&c, *p, 4);
1368 else if (chrlen == -1)
1377 * See if a character matches a character class, starting at the first colon
1379 * If a valid character class is recognized, a pointer to the next character
1380 * after the final closing bracket is stored into *end, otherwise a null
1381 * pointer is stored into *end.
1384 match_charclass(const char *p, wchar_t chr, const char **end)
1387 const char *nameend;
1392 nameend = strstr(p, ":]");
1393 if (nameend == NULL || (size_t)(nameend - p) >= sizeof(name) ||
1396 memcpy(name, p, nameend - p);
1397 name[nameend - p] = '\0';
1399 cclass = wctype(name);
1400 /* An unknown class matches nothing but is valid nevertheless. */
1403 return iswctype(chr, cclass);
1408 * Returns true if the pattern matches the string.
1412 patmatch(const char *pattern, const char *string, int squoted)
1414 const char *p, *q, *end;
1415 const char *bt_p, *bt_q;
1430 if (squoted && *q == CTLESC)
1438 if (squoted && *q == CTLESC)
1445 * A '?' does not match invalid UTF-8 but a
1446 * '*' does, so backtrack.
1451 wc = (unsigned char)*q++;
1455 while (c == CTLQUOTEMARK || c == '*')
1458 * If the pattern ends here, we know the string
1459 * matches without needing to look at the rest of it.
1464 * First try the shortest match for the '*' that
1465 * could work. We can forget any earlier '*' since
1466 * there is no way having it match more characters
1467 * can help us, given that we are already here.
1473 const char *savep, *saveq;
1477 savep = p, saveq = q;
1479 if (*p == '!' || *p == '^') {
1484 if (squoted && *q == CTLESC)
1493 chr = (unsigned char)*q++;
1497 p = savep, q = saveq;
1501 if (c == CTLQUOTEMARK)
1503 if (c == '[' && *p == ':') {
1504 found |= match_charclass(p, chr, &end);
1510 if (localeisutf8 && c & 0x80) {
1513 if (wc == 0) /* bad utf-8 */
1516 wc = (unsigned char)c;
1517 if (*p == '-' && p[1] != ']') {
1519 while (*p == CTLQUOTEMARK)
1525 if (wc2 == 0) /* bad utf-8 */
1528 wc2 = (unsigned char)*p++;
1529 if ( collate_range_cmp(chr, wc) >= 0
1530 && collate_range_cmp(chr, wc2) <= 0
1537 } while ((c = *p++) != ']');
1538 if (found == invert)
1543 if (squoted && *q == CTLESC)
1551 * If we have a mismatch (other than hitting the end
1552 * of the string), go back to the last '*' seen and
1553 * have it match one additional character.
1557 if (squoted && *bt_q == CTLESC)
1572 * Remove any CTLESC and CTLQUOTEMARK characters from a string.
1576 rmescapes(char *str)
1581 while (*p != CTLESC && *p != CTLQUOTEMARK && *p != CTLQUOTEEND) {
1587 if (*p == CTLQUOTEMARK || *p == CTLQUOTEEND) {
1601 * See if a pattern matches in a case statement.
1605 casematch(union node *pattern, const char *val)
1607 struct stackmark smark;
1611 setstackmark(&smark);
1612 argbackq = pattern->narg.backquote;
1613 STARTSTACKSTR(expdest);
1615 argstr(pattern->narg.text, EXP_TILDE | EXP_CASE);
1616 STPUTC('\0', expdest);
1617 p = grabstackstr(expdest);
1618 result = patmatch(p, val, 0);
1619 popstackmark(&smark);
1628 cvtnum(int num, char *buf)
1632 char *p = temp + 31;
1637 *--p = num % 10 + '0';
1638 } while ((num /= 10) != 0);
1648 * Do most of the work for wordexp(3).
1652 wordexpcmd(int argc, char **argv)
1657 out1fmt("%08x", argc - 1);
1658 for (i = 1, len = 0; i < argc; i++)
1659 len += strlen(argv[i]);
1660 out1fmt("%08x", (int)len);
1661 for (i = 1; i < argc; i++)
1662 outbin(argv[i], strlen(argv[i]) + 1, out1);
1667 * Do most of the work for wordexp(3), new version.
1671 freebsd_wordexpcmd(int argc __unused, char **argv __unused)
1673 struct arglist arglist;
1674 union node *args, *n;
1681 while ((ch = nextopt("f:p")) != '\0') {
1684 fd = number(shoptarg);
1691 if (*argptr != NULL)
1692 error("wrong number of arguments");
1694 error("missing fd");
1698 args = parsewordexp();
1699 popfile(); /* will also close fd */
1701 for (n = args; n != NULL; n = n->narg.next) {
1702 if (n->narg.backquote != NULL) {
1703 outcslow('C', out1);
1704 error("command substitution disabled");
1707 outcslow(' ', out1);
1708 arglist.lastp = &arglist.list;
1709 for (n = args; n != NULL; n = n->narg.next)
1710 expandarg(n, &arglist, EXP_FULL | EXP_TILDE);
1711 *arglist.lastp = NULL;
1712 for (sp = arglist.list, count = len = 0; sp; sp = sp->next)
1713 count++, len += strlen(sp->text);
1714 out1fmt("%016zx %016zx", count, len);
1715 for (sp = arglist.list; sp; sp = sp->next)
1716 outbin(sp->text, strlen(sp->text) + 1, out1);