]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - bin/sh/expand.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / bin / sh / expand.c
1 /*-
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.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Kenneth Almquist.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
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.
21  *
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
32  * SUCH DAMAGE.
33  */
34
35 #ifndef lint
36 #if 0
37 static char sccsid[] = "@(#)expand.c    8.5 (Berkeley) 5/15/95";
38 #endif
39 #endif /* not lint */
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
42
43 #include <sys/types.h>
44 #include <sys/time.h>
45 #include <sys/stat.h>
46 #include <dirent.h>
47 #include <errno.h>
48 #include <inttypes.h>
49 #include <limits.h>
50 #include <pwd.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <unistd.h>
55 #include <wchar.h>
56 #include <wctype.h>
57
58 /*
59  * Routines to expand arguments to commands.  We have to deal with
60  * backquotes, shell variables, and file metacharacters.
61  */
62
63 #include "shell.h"
64 #include "main.h"
65 #include "nodes.h"
66 #include "eval.h"
67 #include "expand.h"
68 #include "syntax.h"
69 #include "parser.h"
70 #include "jobs.h"
71 #include "options.h"
72 #include "var.h"
73 #include "input.h"
74 #include "output.h"
75 #include "memalloc.h"
76 #include "error.h"
77 #include "mystring.h"
78 #include "arith.h"
79 #include "show.h"
80 #include "builtins.h"
81
82 /*
83  * Structure specifying which parts of the string should be searched
84  * for IFS characters.
85  */
86
87 struct ifsregion {
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 */
92 };
93
94
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 */
100
101 static void argstr(char *, int);
102 static char *exptilde(char *, int);
103 static void expbackq(union node *, int, int);
104 static int subevalvar(char *, char *, int, int, int, int, int);
105 static char *evalvar(char *, int);
106 static int varisset(char *, int);
107 static void varvalue(char *, int, int, int);
108 static void recordregion(int, int, int);
109 static void removerecordregions(int);
110 static void ifsbreakup(char *, struct arglist *);
111 static void expandmeta(struct strlist *, int);
112 static void expmeta(char *, char *);
113 static void addfname(char *);
114 static struct strlist *expsort(struct strlist *);
115 static struct strlist *msort(struct strlist *, int);
116 static int patmatch(const char *, const char *, int);
117 static char *cvtnum(int, char *);
118 static int collate_range_cmp(wchar_t, wchar_t);
119
120 static int
121 collate_range_cmp(wchar_t c1, wchar_t c2)
122 {
123         static wchar_t s1[2], s2[2];
124
125         s1[0] = c1;
126         s2[0] = c2;
127         return (wcscoll(s1, s2));
128 }
129
130 /*
131  * Expand shell variables and backquotes inside a here document.
132  *      union node *arg         the document
133  *      int fd;                 where to write the expanded version
134  */
135
136 void
137 expandhere(union node *arg, int fd)
138 {
139         expandarg(arg, (struct arglist *)NULL, 0);
140         xwrite(fd, stackblock(), expdest - stackblock());
141 }
142
143 static char *
144 stputs_quotes(const char *data, const char *syntax, char *p)
145 {
146         while (*data) {
147                 CHECKSTRSPACE(2, p);
148                 if (syntax[(int)*data] == CCTL)
149                         USTPUTC(CTLESC, p);
150                 USTPUTC(*data++, p);
151         }
152         return (p);
153 }
154 #define STPUTS_QUOTES(data, syntax, p) p = stputs_quotes((data), syntax, p)
155
156 /*
157  * Perform expansions on an argument, placing the resulting list of arguments
158  * in arglist.  Parameter expansion, command substitution and arithmetic
159  * expansion are always performed; additional expansions can be requested
160  * via flag (EXP_*).
161  * The result is left in the stack string.
162  * When arglist is NULL, perform here document expansion.
163  *
164  * Caution: this function uses global state and is not reentrant.
165  * However, a new invocation after an interrupted invocation is safe
166  * and will reset the global state for the new call.
167  */
168 void
169 expandarg(union node *arg, struct arglist *arglist, int flag)
170 {
171         struct strlist *sp;
172         char *p;
173
174         argbackq = arg->narg.backquote;
175         STARTSTACKSTR(expdest);
176         ifsfirst.next = NULL;
177         ifslastp = NULL;
178         argstr(arg->narg.text, flag);
179         if (arglist == NULL) {
180                 STACKSTRNUL(expdest);
181                 return;                 /* here document expanded */
182         }
183         STPUTC('\0', expdest);
184         p = grabstackstr(expdest);
185         exparg.lastp = &exparg.list;
186         /*
187          * TODO - EXP_REDIR
188          */
189         if (flag & EXP_FULL) {
190                 ifsbreakup(p, &exparg);
191                 *exparg.lastp = NULL;
192                 exparg.lastp = &exparg.list;
193                 expandmeta(exparg.list, flag);
194         } else {
195                 if (flag & EXP_REDIR) /*XXX - for now, just remove escapes */
196                         rmescapes(p);
197                 sp = (struct strlist *)stalloc(sizeof (struct strlist));
198                 sp->text = p;
199                 *exparg.lastp = sp;
200                 exparg.lastp = &sp->next;
201         }
202         while (ifsfirst.next != NULL) {
203                 struct ifsregion *ifsp;
204                 INTOFF;
205                 ifsp = ifsfirst.next->next;
206                 ckfree(ifsfirst.next);
207                 ifsfirst.next = ifsp;
208                 INTON;
209         }
210         *exparg.lastp = NULL;
211         if (exparg.list) {
212                 *arglist->lastp = exparg.list;
213                 arglist->lastp = exparg.lastp;
214         }
215 }
216
217
218
219 /*
220  * Perform parameter expansion, command substitution and arithmetic
221  * expansion, and tilde expansion if requested via EXP_TILDE/EXP_VARTILDE.
222  * Processing ends at a CTLENDVAR character as well as '\0'.
223  * This is used to expand word in ${var+word} etc.
224  * If EXP_FULL, EXP_CASE or EXP_REDIR are set, keep and/or generate CTLESC
225  * characters to allow for further processing.
226  * If EXP_FULL is set, also preserve CTLQUOTEMARK characters.
227  */
228 static void
229 argstr(char *p, int flag)
230 {
231         char c;
232         int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR);  /* do CTLESC */
233         int firsteq = 1;
234         int split_lit;
235         int lit_quoted;
236
237         split_lit = flag & EXP_SPLIT_LIT;
238         lit_quoted = flag & EXP_LIT_QUOTED;
239         flag &= ~(EXP_SPLIT_LIT | EXP_LIT_QUOTED);
240         if (*p == '~' && (flag & (EXP_TILDE | EXP_VARTILDE)))
241                 p = exptilde(p, flag);
242         for (;;) {
243                 CHECKSTRSPACE(2, expdest);
244                 switch (c = *p++) {
245                 case '\0':
246                 case CTLENDVAR:
247                         goto breakloop;
248                 case CTLQUOTEMARK:
249                         lit_quoted = 1;
250                         /* "$@" syntax adherence hack */
251                         if (p[0] == CTLVAR && p[2] == '@' && p[3] == '=')
252                                 break;
253                         if ((flag & EXP_FULL) != 0)
254                                 USTPUTC(c, expdest);
255                         break;
256                 case CTLQUOTEEND:
257                         lit_quoted = 0;
258                         break;
259                 case CTLESC:
260                         if (quotes)
261                                 USTPUTC(c, expdest);
262                         c = *p++;
263                         USTPUTC(c, expdest);
264                         if (split_lit && !lit_quoted)
265                                 recordregion(expdest - stackblock() -
266                                     (quotes ? 2 : 1),
267                                     expdest - stackblock(), 0);
268                         break;
269                 case CTLVAR:
270                         p = evalvar(p, flag);
271                         break;
272                 case CTLBACKQ:
273                 case CTLBACKQ|CTLQUOTE:
274                         expbackq(argbackq->n, c & CTLQUOTE, flag);
275                         argbackq = argbackq->next;
276                         break;
277                 case CTLENDARI:
278                         expari(flag);
279                         break;
280                 case ':':
281                 case '=':
282                         /*
283                          * sort of a hack - expand tildes in variable
284                          * assignments (after the first '=' and after ':'s).
285                          */
286                         USTPUTC(c, expdest);
287                         if (split_lit && !lit_quoted)
288                                 recordregion(expdest - stackblock() - 1,
289                                     expdest - stackblock(), 0);
290                         if (flag & EXP_VARTILDE && *p == '~' &&
291                             (c != '=' || firsteq)) {
292                                 if (c == '=')
293                                         firsteq = 0;
294                                 p = exptilde(p, flag);
295                         }
296                         break;
297                 default:
298                         USTPUTC(c, expdest);
299                         if (split_lit && !lit_quoted)
300                                 recordregion(expdest - stackblock() - 1,
301                                     expdest - stackblock(), 0);
302                 }
303         }
304 breakloop:;
305 }
306
307 /*
308  * Perform tilde expansion, placing the result in the stack string and
309  * returning the next position in the input string to process.
310  */
311 static char *
312 exptilde(char *p, int flag)
313 {
314         char c, *startp = p;
315         struct passwd *pw;
316         char *home;
317         int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR);
318
319         while ((c = *p) != '\0') {
320                 switch(c) {
321                 case CTLESC: /* This means CTL* are always considered quoted. */
322                 case CTLVAR:
323                 case CTLBACKQ:
324                 case CTLBACKQ | CTLQUOTE:
325                 case CTLARI:
326                 case CTLENDARI:
327                 case CTLQUOTEMARK:
328                         return (startp);
329                 case ':':
330                         if (flag & EXP_VARTILDE)
331                                 goto done;
332                         break;
333                 case '/':
334                 case CTLENDVAR:
335                         goto done;
336                 }
337                 p++;
338         }
339 done:
340         *p = '\0';
341         if (*(startp+1) == '\0') {
342                 if ((home = lookupvar("HOME")) == NULL)
343                         goto lose;
344         } else {
345                 if ((pw = getpwnam(startp+1)) == NULL)
346                         goto lose;
347                 home = pw->pw_dir;
348         }
349         if (*home == '\0')
350                 goto lose;
351         *p = c;
352         if (quotes)
353                 STPUTS_QUOTES(home, SQSYNTAX, expdest);
354         else
355                 STPUTS(home, expdest);
356         return (p);
357 lose:
358         *p = c;
359         return (startp);
360 }
361
362
363 static void
364 removerecordregions(int endoff)
365 {
366         if (ifslastp == NULL)
367                 return;
368
369         if (ifsfirst.endoff > endoff) {
370                 while (ifsfirst.next != NULL) {
371                         struct ifsregion *ifsp;
372                         INTOFF;
373                         ifsp = ifsfirst.next->next;
374                         ckfree(ifsfirst.next);
375                         ifsfirst.next = ifsp;
376                         INTON;
377                 }
378                 if (ifsfirst.begoff > endoff)
379                         ifslastp = NULL;
380                 else {
381                         ifslastp = &ifsfirst;
382                         ifsfirst.endoff = endoff;
383                 }
384                 return;
385         }
386
387         ifslastp = &ifsfirst;
388         while (ifslastp->next && ifslastp->next->begoff < endoff)
389                 ifslastp=ifslastp->next;
390         while (ifslastp->next != NULL) {
391                 struct ifsregion *ifsp;
392                 INTOFF;
393                 ifsp = ifslastp->next->next;
394                 ckfree(ifslastp->next);
395                 ifslastp->next = ifsp;
396                 INTON;
397         }
398         if (ifslastp->endoff > endoff)
399                 ifslastp->endoff = endoff;
400 }
401
402 /*
403  * Expand arithmetic expression.  Backup to start of expression,
404  * evaluate, place result in (backed up) result, adjust string position.
405  */
406 void
407 expari(int flag)
408 {
409         char *p, *q, *start;
410         arith_t result;
411         int begoff;
412         int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR);
413         int quoted;
414
415         /*
416          * This routine is slightly over-complicated for
417          * efficiency.  First we make sure there is
418          * enough space for the result, which may be bigger
419          * than the expression.  Next we
420          * scan backwards looking for the start of arithmetic.  If the
421          * next previous character is a CTLESC character, then we
422          * have to rescan starting from the beginning since CTLESC
423          * characters have to be processed left to right.
424          */
425         CHECKSTRSPACE(DIGITS(result) - 2, expdest);
426         USTPUTC('\0', expdest);
427         start = stackblock();
428         p = expdest - 2;
429         while (p >= start && *p != CTLARI)
430                 --p;
431         if (p < start || *p != CTLARI)
432                 error("missing CTLARI (shouldn't happen)");
433         if (p > start && *(p - 1) == CTLESC)
434                 for (p = start; *p != CTLARI; p++)
435                         if (*p == CTLESC)
436                                 p++;
437
438         if (p[1] == '"')
439                 quoted=1;
440         else
441                 quoted=0;
442         begoff = p - start;
443         removerecordregions(begoff);
444         if (quotes)
445                 rmescapes(p+2);
446         q = grabstackstr(expdest);
447         result = arith(p+2);
448         ungrabstackstr(q, expdest);
449         fmtstr(p, DIGITS(result), ARITH_FORMAT_STR, result);
450         while (*p++)
451                 ;
452         if (quoted == 0)
453                 recordregion(begoff, p - 1 - start, 0);
454         result = expdest - p + 1;
455         STADJUST(-result, expdest);
456 }
457
458
459 /*
460  * Perform command substitution.
461  */
462 static void
463 expbackq(union node *cmd, int quoted, int flag)
464 {
465         struct backcmd in;
466         int i;
467         char buf[128];
468         char *p;
469         char *dest = expdest;
470         struct ifsregion saveifs, *savelastp;
471         struct nodelist *saveargbackq;
472         char lastc;
473         int startloc = dest - stackblock();
474         char const *syntax = quoted? DQSYNTAX : BASESYNTAX;
475         int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR);
476         int nnl;
477
478         INTOFF;
479         saveifs = ifsfirst;
480         savelastp = ifslastp;
481         saveargbackq = argbackq;
482         p = grabstackstr(dest);
483         evalbackcmd(cmd, &in);
484         ungrabstackstr(p, dest);
485         ifsfirst = saveifs;
486         ifslastp = savelastp;
487         argbackq = saveargbackq;
488
489         p = in.buf;
490         lastc = '\0';
491         nnl = 0;
492         /* Don't copy trailing newlines */
493         for (;;) {
494                 if (--in.nleft < 0) {
495                         if (in.fd < 0)
496                                 break;
497                         while ((i = read(in.fd, buf, sizeof buf)) < 0 && errno == EINTR);
498                         TRACE(("expbackq: read returns %d\n", i));
499                         if (i <= 0)
500                                 break;
501                         p = buf;
502                         in.nleft = i - 1;
503                 }
504                 lastc = *p++;
505                 if (lastc != '\0') {
506                         if (lastc == '\n') {
507                                 nnl++;
508                         } else {
509                                 CHECKSTRSPACE(nnl + 2, dest);
510                                 while (nnl > 0) {
511                                         nnl--;
512                                         USTPUTC('\n', dest);
513                                 }
514                                 if (quotes && syntax[(int)lastc] == CCTL)
515                                         USTPUTC(CTLESC, dest);
516                                 USTPUTC(lastc, dest);
517                         }
518                 }
519         }
520
521         if (in.fd >= 0)
522                 close(in.fd);
523         if (in.buf)
524                 ckfree(in.buf);
525         if (in.jp)
526                 exitstatus = waitforjob(in.jp, (int *)NULL);
527         if (quoted == 0)
528                 recordregion(startloc, dest - stackblock(), 0);
529         TRACE(("expbackq: size=%td: \"%.*s\"\n",
530                 ((dest - stackblock()) - startloc),
531                 (int)((dest - stackblock()) - startloc),
532                 stackblock() + startloc));
533         expdest = dest;
534         INTON;
535 }
536
537
538
539 static int
540 subevalvar(char *p, char *str, int strloc, int subtype, int startloc,
541   int varflags, int quotes)
542 {
543         char *startp;
544         char *loc = NULL;
545         char *q;
546         int c = 0;
547         struct nodelist *saveargbackq = argbackq;
548         int amount;
549
550         argstr(p, (subtype == VSTRIMLEFT || subtype == VSTRIMLEFTMAX ||
551             subtype == VSTRIMRIGHT || subtype == VSTRIMRIGHTMAX ?
552             EXP_CASE : 0) | EXP_TILDE);
553         STACKSTRNUL(expdest);
554         argbackq = saveargbackq;
555         startp = stackblock() + startloc;
556         if (str == NULL)
557             str = stackblock() + strloc;
558
559         switch (subtype) {
560         case VSASSIGN:
561                 setvar(str, startp, 0);
562                 amount = startp - expdest;
563                 STADJUST(amount, expdest);
564                 varflags &= ~VSNUL;
565                 return 1;
566
567         case VSQUESTION:
568                 if (*p != CTLENDVAR) {
569                         outfmt(out2, "%s\n", startp);
570                         error((char *)NULL);
571                 }
572                 error("%.*s: parameter %snot set", (int)(p - str - 1),
573                       str, (varflags & VSNUL) ? "null or "
574                                               : nullstr);
575                 return 0;
576
577         case VSTRIMLEFT:
578                 for (loc = startp; loc < str; loc++) {
579                         c = *loc;
580                         *loc = '\0';
581                         if (patmatch(str, startp, quotes)) {
582                                 *loc = c;
583                                 goto recordleft;
584                         }
585                         *loc = c;
586                         if (quotes && *loc == CTLESC)
587                                 loc++;
588                 }
589                 return 0;
590
591         case VSTRIMLEFTMAX:
592                 for (loc = str - 1; loc >= startp;) {
593                         c = *loc;
594                         *loc = '\0';
595                         if (patmatch(str, startp, quotes)) {
596                                 *loc = c;
597                                 goto recordleft;
598                         }
599                         *loc = c;
600                         loc--;
601                         if (quotes && loc > startp && *(loc - 1) == CTLESC) {
602                                 for (q = startp; q < loc; q++)
603                                         if (*q == CTLESC)
604                                                 q++;
605                                 if (q > loc)
606                                         loc--;
607                         }
608                 }
609                 return 0;
610
611         case VSTRIMRIGHT:
612                 for (loc = str - 1; loc >= startp;) {
613                         if (patmatch(str, loc, quotes)) {
614                                 amount = loc - expdest;
615                                 STADJUST(amount, expdest);
616                                 return 1;
617                         }
618                         loc--;
619                         if (quotes && loc > startp && *(loc - 1) == CTLESC) {
620                                 for (q = startp; q < loc; q++)
621                                         if (*q == CTLESC)
622                                                 q++;
623                                 if (q > loc)
624                                         loc--;
625                         }
626                 }
627                 return 0;
628
629         case VSTRIMRIGHTMAX:
630                 for (loc = startp; loc < str - 1; loc++) {
631                         if (patmatch(str, loc, quotes)) {
632                                 amount = loc - expdest;
633                                 STADJUST(amount, expdest);
634                                 return 1;
635                         }
636                         if (quotes && *loc == CTLESC)
637                                 loc++;
638                 }
639                 return 0;
640
641
642         default:
643                 abort();
644         }
645
646 recordleft:
647         amount = ((str - 1) - (loc - startp)) - expdest;
648         STADJUST(amount, expdest);
649         while (loc != str - 1)
650                 *startp++ = *loc++;
651         return 1;
652 }
653
654
655 /*
656  * Expand a variable, and return a pointer to the next character in the
657  * input string.
658  */
659
660 static char *
661 evalvar(char *p, int flag)
662 {
663         int subtype;
664         int varflags;
665         char *var;
666         char *val;
667         int patloc;
668         int c;
669         int set;
670         int special;
671         int startloc;
672         int varlen;
673         int varlenb;
674         int easy;
675         int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR);
676
677         varflags = (unsigned char)*p++;
678         subtype = varflags & VSTYPE;
679         var = p;
680         special = 0;
681         if (! is_name(*p))
682                 special = 1;
683         p = strchr(p, '=') + 1;
684 again: /* jump here after setting a variable with ${var=text} */
685         if (varflags & VSLINENO) {
686                 set = 1;
687                 special = 0;
688                 val = var;
689                 p[-1] = '\0';   /* temporarily overwrite '=' to have \0
690                                    terminated string */
691         } else if (special) {
692                 set = varisset(var, varflags & VSNUL);
693                 val = NULL;
694         } else {
695                 val = bltinlookup(var, 1);
696                 if (val == NULL || ((varflags & VSNUL) && val[0] == '\0')) {
697                         val = NULL;
698                         set = 0;
699                 } else
700                         set = 1;
701         }
702         varlen = 0;
703         startloc = expdest - stackblock();
704         if (!set && uflag && *var != '@' && *var != '*') {
705                 switch (subtype) {
706                 case VSNORMAL:
707                 case VSTRIMLEFT:
708                 case VSTRIMLEFTMAX:
709                 case VSTRIMRIGHT:
710                 case VSTRIMRIGHTMAX:
711                 case VSLENGTH:
712                         error("%.*s: parameter not set", (int)(p - var - 1),
713                             var);
714                 }
715         }
716         if (set && subtype != VSPLUS) {
717                 /* insert the value of the variable */
718                 if (special) {
719                         varvalue(var, varflags & VSQUOTE, subtype, flag);
720                         if (subtype == VSLENGTH) {
721                                 varlenb = expdest - stackblock() - startloc;
722                                 varlen = varlenb;
723                                 if (localeisutf8) {
724                                         val = stackblock() + startloc;
725                                         for (;val != expdest; val++)
726                                                 if ((*val & 0xC0) == 0x80)
727                                                         varlen--;
728                                 }
729                                 STADJUST(-varlenb, expdest);
730                         }
731                 } else {
732                         char const *syntax = (varflags & VSQUOTE) ? DQSYNTAX
733                                                                   : BASESYNTAX;
734
735                         if (subtype == VSLENGTH) {
736                                 for (;*val; val++)
737                                         if (!localeisutf8 ||
738                                             (*val & 0xC0) != 0x80)
739                                                 varlen++;
740                         }
741                         else {
742                                 if (quotes)
743                                         STPUTS_QUOTES(val, syntax, expdest);
744                                 else
745                                         STPUTS(val, expdest);
746
747                         }
748                 }
749         }
750
751         if (subtype == VSPLUS)
752                 set = ! set;
753
754         easy = ((varflags & VSQUOTE) == 0 ||
755                 (*var == '@' && shellparam.nparam != 1));
756
757
758         switch (subtype) {
759         case VSLENGTH:
760                 expdest = cvtnum(varlen, expdest);
761                 goto record;
762
763         case VSNORMAL:
764                 if (!easy)
765                         break;
766 record:
767                 recordregion(startloc, expdest - stackblock(),
768                     varflags & VSQUOTE || (ifsset() && ifsval()[0] == '\0' &&
769                     (*var == '@' || *var == '*')));
770                 break;
771
772         case VSPLUS:
773         case VSMINUS:
774                 if (!set) {
775                         argstr(p, flag | (flag & EXP_FULL ? EXP_SPLIT_LIT : 0) |
776                             (varflags & VSQUOTE ? EXP_LIT_QUOTED : 0));
777                         break;
778                 }
779                 if (easy)
780                         goto record;
781                 break;
782
783         case VSTRIMLEFT:
784         case VSTRIMLEFTMAX:
785         case VSTRIMRIGHT:
786         case VSTRIMRIGHTMAX:
787                 if (!set)
788                         break;
789                 /*
790                  * Terminate the string and start recording the pattern
791                  * right after it
792                  */
793                 STPUTC('\0', expdest);
794                 patloc = expdest - stackblock();
795                 if (subevalvar(p, NULL, patloc, subtype,
796                     startloc, varflags, quotes) == 0) {
797                         int amount = (expdest - stackblock() - patloc) + 1;
798                         STADJUST(-amount, expdest);
799                 }
800                 /* Remove any recorded regions beyond start of variable */
801                 removerecordregions(startloc);
802                 goto record;
803
804         case VSASSIGN:
805         case VSQUESTION:
806                 if (!set) {
807                         if (subevalvar(p, var, 0, subtype, startloc, varflags,
808                             quotes)) {
809                                 varflags &= ~VSNUL;
810                                 /*
811                                  * Remove any recorded regions beyond
812                                  * start of variable
813                                  */
814                                 removerecordregions(startloc);
815                                 goto again;
816                         }
817                         break;
818                 }
819                 if (easy)
820                         goto record;
821                 break;
822
823         case VSERROR:
824                 c = p - var - 1;
825                 error("${%.*s%s}: Bad substitution", c, var,
826                     (c > 0 && *p != CTLENDVAR) ? "..." : "");
827
828         default:
829                 abort();
830         }
831         p[-1] = '=';    /* recover overwritten '=' */
832
833         if (subtype != VSNORMAL) {      /* skip to end of alternative */
834                 int nesting = 1;
835                 for (;;) {
836                         if ((c = *p++) == CTLESC)
837                                 p++;
838                         else if (c == CTLBACKQ || c == (CTLBACKQ|CTLQUOTE)) {
839                                 if (set)
840                                         argbackq = argbackq->next;
841                         } else if (c == CTLVAR) {
842                                 if ((*p++ & VSTYPE) != VSNORMAL)
843                                         nesting++;
844                         } else if (c == CTLENDVAR) {
845                                 if (--nesting == 0)
846                                         break;
847                         }
848                 }
849         }
850         return p;
851 }
852
853
854
855 /*
856  * Test whether a specialized variable is set.
857  */
858
859 static int
860 varisset(char *name, int nulok)
861 {
862
863         if (*name == '!')
864                 return backgndpidset();
865         else if (*name == '@' || *name == '*') {
866                 if (*shellparam.p == NULL)
867                         return 0;
868
869                 if (nulok) {
870                         char **av;
871
872                         for (av = shellparam.p; *av; av++)
873                                 if (**av != '\0')
874                                         return 1;
875                         return 0;
876                 }
877         } else if (is_digit(*name)) {
878                 char *ap;
879                 int num = atoi(name);
880
881                 if (num > shellparam.nparam)
882                         return 0;
883
884                 if (num == 0)
885                         ap = arg0;
886                 else
887                         ap = shellparam.p[num - 1];
888
889                 if (nulok && (ap == NULL || *ap == '\0'))
890                         return 0;
891         }
892         return 1;
893 }
894
895 static void
896 strtodest(const char *p, int flag, int subtype, int quoted)
897 {
898         if (flag & (EXP_FULL | EXP_CASE) && subtype != VSLENGTH)
899                 STPUTS_QUOTES(p, quoted ? DQSYNTAX : BASESYNTAX, expdest);
900         else
901                 STPUTS(p, expdest);
902 }
903
904 /*
905  * Add the value of a specialized variable to the stack string.
906  */
907
908 static void
909 varvalue(char *name, int quoted, int subtype, int flag)
910 {
911         int num;
912         char *p;
913         int i;
914         char sep;
915         char **ap;
916
917         switch (*name) {
918         case '$':
919                 num = rootpid;
920                 goto numvar;
921         case '?':
922                 num = oexitstatus;
923                 goto numvar;
924         case '#':
925                 num = shellparam.nparam;
926                 goto numvar;
927         case '!':
928                 num = backgndpidval();
929 numvar:
930                 expdest = cvtnum(num, expdest);
931                 break;
932         case '-':
933                 for (i = 0 ; i < NOPTS ; i++) {
934                         if (optlist[i].val)
935                                 STPUTC(optlist[i].letter, expdest);
936                 }
937                 break;
938         case '@':
939                 if (flag & EXP_FULL && quoted) {
940                         for (ap = shellparam.p ; (p = *ap++) != NULL ; ) {
941                                 strtodest(p, flag, subtype, quoted);
942                                 if (*ap)
943                                         STPUTC('\0', expdest);
944                         }
945                         break;
946                 }
947                 /* FALLTHROUGH */
948         case '*':
949                 if (ifsset())
950                         sep = ifsval()[0];
951                 else
952                         sep = ' ';
953                 for (ap = shellparam.p ; (p = *ap++) != NULL ; ) {
954                         strtodest(p, flag, subtype, quoted);
955                         if (!*ap)
956                                 break;
957                         if (sep || (flag & EXP_FULL && !quoted && **ap != '\0'))
958                                 STPUTC(sep, expdest);
959                 }
960                 break;
961         case '0':
962                 p = arg0;
963                 strtodest(p, flag, subtype, quoted);
964                 break;
965         default:
966                 if (is_digit(*name)) {
967                         num = atoi(name);
968                         if (num > 0 && num <= shellparam.nparam) {
969                                 p = shellparam.p[num - 1];
970                                 strtodest(p, flag, subtype, quoted);
971                         }
972                 }
973                 break;
974         }
975 }
976
977
978
979 /*
980  * Record the fact that we have to scan this region of the
981  * string for IFS characters.
982  */
983
984 static void
985 recordregion(int start, int end, int inquotes)
986 {
987         struct ifsregion *ifsp;
988
989         if (ifslastp == NULL) {
990                 ifsp = &ifsfirst;
991         } else {
992                 if (ifslastp->endoff == start
993                     && ifslastp->inquotes == inquotes) {
994                         /* extend previous area */
995                         ifslastp->endoff = end;
996                         return;
997                 }
998                 ifsp = (struct ifsregion *)ckmalloc(sizeof (struct ifsregion));
999                 ifslastp->next = ifsp;
1000         }
1001         ifslastp = ifsp;
1002         ifslastp->next = NULL;
1003         ifslastp->begoff = start;
1004         ifslastp->endoff = end;
1005         ifslastp->inquotes = inquotes;
1006 }
1007
1008
1009
1010 /*
1011  * Break the argument string into pieces based upon IFS and add the
1012  * strings to the argument list.  The regions of the string to be
1013  * searched for IFS characters have been stored by recordregion.
1014  * CTLESC characters are preserved but have little effect in this pass
1015  * other than escaping CTL* characters.  In particular, they do not escape
1016  * IFS characters: that should be done with the ifsregion mechanism.
1017  * CTLQUOTEMARK characters are used to preserve empty quoted strings.
1018  * This pass treats them as a regular character, making the string non-empty.
1019  * Later, they are removed along with the other CTL* characters.
1020  */
1021 static void
1022 ifsbreakup(char *string, struct arglist *arglist)
1023 {
1024         struct ifsregion *ifsp;
1025         struct strlist *sp;
1026         char *start;
1027         char *p;
1028         char *q;
1029         const char *ifs;
1030         const char *ifsspc;
1031         int had_param_ch = 0;
1032
1033         start = string;
1034
1035         if (ifslastp == NULL) {
1036                 /* Return entire argument, IFS doesn't apply to any of it */
1037                 sp = (struct strlist *)stalloc(sizeof *sp);
1038                 sp->text = start;
1039                 *arglist->lastp = sp;
1040                 arglist->lastp = &sp->next;
1041                 return;
1042         }
1043
1044         ifs = ifsset() ? ifsval() : " \t\n";
1045
1046         for (ifsp = &ifsfirst; ifsp != NULL; ifsp = ifsp->next) {
1047                 p = string + ifsp->begoff;
1048                 while (p < string + ifsp->endoff) {
1049                         q = p;
1050                         if (*p == CTLESC)
1051                                 p++;
1052                         if (ifsp->inquotes) {
1053                                 /* Only NULs (should be from "$@") end args */
1054                                 had_param_ch = 1;
1055                                 if (*p != 0) {
1056                                         p++;
1057                                         continue;
1058                                 }
1059                                 ifsspc = NULL;
1060                         } else {
1061                                 if (!strchr(ifs, *p)) {
1062                                         had_param_ch = 1;
1063                                         p++;
1064                                         continue;
1065                                 }
1066                                 ifsspc = strchr(" \t\n", *p);
1067
1068                                 /* Ignore IFS whitespace at start */
1069                                 if (q == start && ifsspc != NULL) {
1070                                         p++;
1071                                         start = p;
1072                                         continue;
1073                                 }
1074                                 had_param_ch = 0;
1075                         }
1076
1077                         /* Save this argument... */
1078                         *q = '\0';
1079                         sp = (struct strlist *)stalloc(sizeof *sp);
1080                         sp->text = start;
1081                         *arglist->lastp = sp;
1082                         arglist->lastp = &sp->next;
1083                         p++;
1084
1085                         if (ifsspc != NULL) {
1086                                 /* Ignore further trailing IFS whitespace */
1087                                 for (; p < string + ifsp->endoff; p++) {
1088                                         q = p;
1089                                         if (*p == CTLESC)
1090                                                 p++;
1091                                         if (strchr(ifs, *p) == NULL) {
1092                                                 p = q;
1093                                                 break;
1094                                         }
1095                                         if (strchr(" \t\n", *p) == NULL) {
1096                                                 p++;
1097                                                 break;
1098                                         }
1099                                 }
1100                         }
1101                         start = p;
1102                 }
1103         }
1104
1105         /*
1106          * Save anything left as an argument.
1107          * Traditionally we have treated 'IFS=':'; set -- x$IFS' as
1108          * generating 2 arguments, the second of which is empty.
1109          * Some recent clarification of the Posix spec say that it
1110          * should only generate one....
1111          */
1112         if (had_param_ch || *start != 0) {
1113                 sp = (struct strlist *)stalloc(sizeof *sp);
1114                 sp->text = start;
1115                 *arglist->lastp = sp;
1116                 arglist->lastp = &sp->next;
1117         }
1118 }
1119
1120
1121 static char expdir[PATH_MAX];
1122 #define expdir_end (expdir + sizeof(expdir))
1123
1124 /*
1125  * Perform pathname generation and remove control characters.
1126  * At this point, the only control characters should be CTLESC and CTLQUOTEMARK.
1127  * The results are stored in the list exparg.
1128  */
1129 static void
1130 expandmeta(struct strlist *str, int flag __unused)
1131 {
1132         char *p;
1133         struct strlist **savelastp;
1134         struct strlist *sp;
1135         char c;
1136         /* TODO - EXP_REDIR */
1137
1138         while (str) {
1139                 if (fflag)
1140                         goto nometa;
1141                 p = str->text;
1142                 for (;;) {                      /* fast check for meta chars */
1143                         if ((c = *p++) == '\0')
1144                                 goto nometa;
1145                         if (c == '*' || c == '?' || c == '[')
1146                                 break;
1147                 }
1148                 savelastp = exparg.lastp;
1149                 INTOFF;
1150                 expmeta(expdir, str->text);
1151                 INTON;
1152                 if (exparg.lastp == savelastp) {
1153                         /*
1154                          * no matches
1155                          */
1156 nometa:
1157                         *exparg.lastp = str;
1158                         rmescapes(str->text);
1159                         exparg.lastp = &str->next;
1160                 } else {
1161                         *exparg.lastp = NULL;
1162                         *savelastp = sp = expsort(*savelastp);
1163                         while (sp->next != NULL)
1164                                 sp = sp->next;
1165                         exparg.lastp = &sp->next;
1166                 }
1167                 str = str->next;
1168         }
1169 }
1170
1171
1172 /*
1173  * Do metacharacter (i.e. *, ?, [...]) expansion.
1174  */
1175
1176 static void
1177 expmeta(char *enddir, char *name)
1178 {
1179         char *p;
1180         char *q;
1181         char *start;
1182         char *endname;
1183         int metaflag;
1184         struct stat statb;
1185         DIR *dirp;
1186         struct dirent *dp;
1187         int atend;
1188         int matchdot;
1189         int esc;
1190
1191         metaflag = 0;
1192         start = name;
1193         for (p = name; esc = 0, *p; p += esc + 1) {
1194                 if (*p == '*' || *p == '?')
1195                         metaflag = 1;
1196                 else if (*p == '[') {
1197                         q = p + 1;
1198                         if (*q == '!' || *q == '^')
1199                                 q++;
1200                         for (;;) {
1201                                 while (*q == CTLQUOTEMARK)
1202                                         q++;
1203                                 if (*q == CTLESC)
1204                                         q++;
1205                                 if (*q == '/' || *q == '\0')
1206                                         break;
1207                                 if (*++q == ']') {
1208                                         metaflag = 1;
1209                                         break;
1210                                 }
1211                         }
1212                 } else if (*p == '\0')
1213                         break;
1214                 else if (*p == CTLQUOTEMARK)
1215                         continue;
1216                 else {
1217                         if (*p == CTLESC)
1218                                 esc++;
1219                         if (p[esc] == '/') {
1220                                 if (metaflag)
1221                                         break;
1222                                 start = p + esc + 1;
1223                         }
1224                 }
1225         }
1226         if (metaflag == 0) {    /* we've reached the end of the file name */
1227                 if (enddir != expdir)
1228                         metaflag++;
1229                 for (p = name ; ; p++) {
1230                         if (*p == CTLQUOTEMARK)
1231                                 continue;
1232                         if (*p == CTLESC)
1233                                 p++;
1234                         *enddir++ = *p;
1235                         if (*p == '\0')
1236                                 break;
1237                         if (enddir == expdir_end)
1238                                 return;
1239                 }
1240                 if (metaflag == 0 || lstat(expdir, &statb) >= 0)
1241                         addfname(expdir);
1242                 return;
1243         }
1244         endname = p;
1245         if (start != name) {
1246                 p = name;
1247                 while (p < start) {
1248                         while (*p == CTLQUOTEMARK)
1249                                 p++;
1250                         if (*p == CTLESC)
1251                                 p++;
1252                         *enddir++ = *p++;
1253                         if (enddir == expdir_end)
1254                                 return;
1255                 }
1256         }
1257         if (enddir == expdir) {
1258                 p = ".";
1259         } else if (enddir == expdir + 1 && *expdir == '/') {
1260                 p = "/";
1261         } else {
1262                 p = expdir;
1263                 enddir[-1] = '\0';
1264         }
1265         if ((dirp = opendir(p)) == NULL)
1266                 return;
1267         if (enddir != expdir)
1268                 enddir[-1] = '/';
1269         if (*endname == 0) {
1270                 atend = 1;
1271         } else {
1272                 atend = 0;
1273                 *endname = '\0';
1274                 endname += esc + 1;
1275         }
1276         matchdot = 0;
1277         p = start;
1278         while (*p == CTLQUOTEMARK)
1279                 p++;
1280         if (*p == CTLESC)
1281                 p++;
1282         if (*p == '.')
1283                 matchdot++;
1284         while (! int_pending() && (dp = readdir(dirp)) != NULL) {
1285                 if (dp->d_name[0] == '.' && ! matchdot)
1286                         continue;
1287                 if (patmatch(start, dp->d_name, 0)) {
1288                         if (enddir + dp->d_namlen + 1 > expdir_end)
1289                                 continue;
1290                         memcpy(enddir, dp->d_name, dp->d_namlen + 1);
1291                         if (atend)
1292                                 addfname(expdir);
1293                         else {
1294                                 if (enddir + dp->d_namlen + 2 > expdir_end)
1295                                         continue;
1296                                 enddir[dp->d_namlen] = '/';
1297                                 enddir[dp->d_namlen + 1] = '\0';
1298                                 expmeta(enddir + dp->d_namlen + 1, endname);
1299                         }
1300                 }
1301         }
1302         closedir(dirp);
1303         if (! atend)
1304                 endname[-esc - 1] = esc ? CTLESC : '/';
1305 }
1306
1307
1308 /*
1309  * Add a file name to the list.
1310  */
1311
1312 static void
1313 addfname(char *name)
1314 {
1315         char *p;
1316         struct strlist *sp;
1317
1318         p = stalloc(strlen(name) + 1);
1319         scopy(name, p);
1320         sp = (struct strlist *)stalloc(sizeof *sp);
1321         sp->text = p;
1322         *exparg.lastp = sp;
1323         exparg.lastp = &sp->next;
1324 }
1325
1326
1327 /*
1328  * Sort the results of file name expansion.  It calculates the number of
1329  * strings to sort and then calls msort (short for merge sort) to do the
1330  * work.
1331  */
1332
1333 static struct strlist *
1334 expsort(struct strlist *str)
1335 {
1336         int len;
1337         struct strlist *sp;
1338
1339         len = 0;
1340         for (sp = str ; sp ; sp = sp->next)
1341                 len++;
1342         return msort(str, len);
1343 }
1344
1345
1346 static struct strlist *
1347 msort(struct strlist *list, int len)
1348 {
1349         struct strlist *p, *q = NULL;
1350         struct strlist **lpp;
1351         int half;
1352         int n;
1353
1354         if (len <= 1)
1355                 return list;
1356         half = len >> 1;
1357         p = list;
1358         for (n = half ; --n >= 0 ; ) {
1359                 q = p;
1360                 p = p->next;
1361         }
1362         q->next = NULL;                 /* terminate first half of list */
1363         q = msort(list, half);          /* sort first half of list */
1364         p = msort(p, len - half);               /* sort second half */
1365         lpp = &list;
1366         for (;;) {
1367                 if (strcmp(p->text, q->text) < 0) {
1368                         *lpp = p;
1369                         lpp = &p->next;
1370                         if ((p = *lpp) == NULL) {
1371                                 *lpp = q;
1372                                 break;
1373                         }
1374                 } else {
1375                         *lpp = q;
1376                         lpp = &q->next;
1377                         if ((q = *lpp) == NULL) {
1378                                 *lpp = p;
1379                                 break;
1380                         }
1381                 }
1382         }
1383         return list;
1384 }
1385
1386
1387
1388 static wchar_t
1389 get_wc(const char **p)
1390 {
1391         wchar_t c;
1392         int chrlen;
1393
1394         chrlen = mbtowc(&c, *p, 4);
1395         if (chrlen == 0)
1396                 return 0;
1397         else if (chrlen == -1)
1398                 c = 0;
1399         else
1400                 *p += chrlen;
1401         return c;
1402 }
1403
1404
1405 /*
1406  * See if a character matches a character class, starting at the first colon
1407  * of "[:class:]".
1408  * If a valid character class is recognized, a pointer to the next character
1409  * after the final closing bracket is stored into *end, otherwise a null
1410  * pointer is stored into *end.
1411  */
1412 static int
1413 match_charclass(const char *p, wchar_t chr, const char **end)
1414 {
1415         char name[20];
1416         const char *nameend;
1417         wctype_t cclass;
1418
1419         *end = NULL;
1420         p++;
1421         nameend = strstr(p, ":]");
1422         if (nameend == NULL || nameend - p >= sizeof(name) || nameend == p)
1423                 return 0;
1424         memcpy(name, p, nameend - p);
1425         name[nameend - p] = '\0';
1426         *end = nameend + 2;
1427         cclass = wctype(name);
1428         /* An unknown class matches nothing but is valid nevertheless. */
1429         if (cclass == 0)
1430                 return 0;
1431         return iswctype(chr, cclass);
1432 }
1433
1434
1435 /*
1436  * Returns true if the pattern matches the string.
1437  */
1438
1439 static int
1440 patmatch(const char *pattern, const char *string, int squoted)
1441 {
1442         const char *p, *q, *end;
1443         const char *bt_p, *bt_q;
1444         char c;
1445         wchar_t wc, wc2;
1446
1447         p = pattern;
1448         q = string;
1449         bt_p = NULL;
1450         bt_q = NULL;
1451         for (;;) {
1452                 switch (c = *p++) {
1453                 case '\0':
1454                         if (*q != '\0')
1455                                 goto backtrack;
1456                         return 1;
1457                 case CTLESC:
1458                         if (squoted && *q == CTLESC)
1459                                 q++;
1460                         if (*q++ != *p++)
1461                                 goto backtrack;
1462                         break;
1463                 case CTLQUOTEMARK:
1464                         continue;
1465                 case '?':
1466                         if (squoted && *q == CTLESC)
1467                                 q++;
1468                         if (*q == '\0')
1469                                 return 0;
1470                         if (localeisutf8) {
1471                                 wc = get_wc(&q);
1472                                 /*
1473                                  * A '?' does not match invalid UTF-8 but a
1474                                  * '*' does, so backtrack.
1475                                  */
1476                                 if (wc == 0)
1477                                         goto backtrack;
1478                         } else
1479                                 wc = (unsigned char)*q++;
1480                         break;
1481                 case '*':
1482                         c = *p;
1483                         while (c == CTLQUOTEMARK || c == '*')
1484                                 c = *++p;
1485                         /*
1486                          * If the pattern ends here, we know the string
1487                          * matches without needing to look at the rest of it.
1488                          */
1489                         if (c == '\0')
1490                                 return 1;
1491                         /*
1492                          * First try the shortest match for the '*' that
1493                          * could work. We can forget any earlier '*' since
1494                          * there is no way having it match more characters
1495                          * can help us, given that we are already here.
1496                          */
1497                         bt_p = p;
1498                         bt_q = q;
1499                         break;
1500                 case '[': {
1501                         const char *endp;
1502                         int invert, found;
1503                         wchar_t chr;
1504
1505                         endp = p;
1506                         if (*endp == '!' || *endp == '^')
1507                                 endp++;
1508                         for (;;) {
1509                                 while (*endp == CTLQUOTEMARK)
1510                                         endp++;
1511                                 if (*endp == 0)
1512                                         goto dft;               /* no matching ] */
1513                                 if (*endp == CTLESC)
1514                                         endp++;
1515                                 if (*++endp == ']')
1516                                         break;
1517                         }
1518                         invert = 0;
1519                         if (*p == '!' || *p == '^') {
1520                                 invert++;
1521                                 p++;
1522                         }
1523                         found = 0;
1524                         if (squoted && *q == CTLESC)
1525                                 q++;
1526                         if (*q == '\0')
1527                                 return 0;
1528                         if (localeisutf8) {
1529                                 chr = get_wc(&q);
1530                                 if (chr == 0)
1531                                         goto backtrack;
1532                         } else
1533                                 chr = (unsigned char)*q++;
1534                         c = *p++;
1535                         do {
1536                                 if (c == CTLQUOTEMARK)
1537                                         continue;
1538                                 if (c == '[' && *p == ':') {
1539                                         found |= match_charclass(p, chr, &end);
1540                                         if (end != NULL)
1541                                                 p = end;
1542                                 }
1543                                 if (c == CTLESC)
1544                                         c = *p++;
1545                                 if (localeisutf8 && c & 0x80) {
1546                                         p--;
1547                                         wc = get_wc(&p);
1548                                         if (wc == 0) /* bad utf-8 */
1549                                                 return 0;
1550                                 } else
1551                                         wc = (unsigned char)c;
1552                                 if (*p == '-' && p[1] != ']') {
1553                                         p++;
1554                                         while (*p == CTLQUOTEMARK)
1555                                                 p++;
1556                                         if (*p == CTLESC)
1557                                                 p++;
1558                                         if (localeisutf8) {
1559                                                 wc2 = get_wc(&p);
1560                                                 if (wc2 == 0) /* bad utf-8 */
1561                                                         return 0;
1562                                         } else
1563                                                 wc2 = (unsigned char)*p++;
1564                                         if (   collate_range_cmp(chr, wc) >= 0
1565                                             && collate_range_cmp(chr, wc2) <= 0
1566                                            )
1567                                                 found = 1;
1568                                 } else {
1569                                         if (chr == wc)
1570                                                 found = 1;
1571                                 }
1572                         } while ((c = *p++) != ']');
1573                         if (found == invert)
1574                                 goto backtrack;
1575                         break;
1576                 }
1577 dft:            default:
1578                         if (squoted && *q == CTLESC)
1579                                 q++;
1580                         if (*q == '\0')
1581                                 return 0;
1582                         if (*q++ == c)
1583                                 break;
1584 backtrack:
1585                         /*
1586                          * If we have a mismatch (other than hitting the end
1587                          * of the string), go back to the last '*' seen and
1588                          * have it match one additional character.
1589                          */
1590                         if (bt_p == NULL)
1591                                 return 0;
1592                         if (squoted && *bt_q == CTLESC)
1593                                 bt_q++;
1594                         if (*bt_q == '\0')
1595                                 return 0;
1596                         bt_q++;
1597                         p = bt_p;
1598                         q = bt_q;
1599                         break;
1600                 }
1601         }
1602 }
1603
1604
1605
1606 /*
1607  * Remove any CTLESC and CTLQUOTEMARK characters from a string.
1608  */
1609
1610 void
1611 rmescapes(char *str)
1612 {
1613         char *p, *q;
1614
1615         p = str;
1616         while (*p != CTLESC && *p != CTLQUOTEMARK && *p != CTLQUOTEEND) {
1617                 if (*p++ == '\0')
1618                         return;
1619         }
1620         q = p;
1621         while (*p) {
1622                 if (*p == CTLQUOTEMARK || *p == CTLQUOTEEND) {
1623                         p++;
1624                         continue;
1625                 }
1626                 if (*p == CTLESC)
1627                         p++;
1628                 *q++ = *p++;
1629         }
1630         *q = '\0';
1631 }
1632
1633
1634
1635 /*
1636  * See if a pattern matches in a case statement.
1637  */
1638
1639 int
1640 casematch(union node *pattern, const char *val)
1641 {
1642         struct stackmark smark;
1643         int result;
1644         char *p;
1645
1646         setstackmark(&smark);
1647         argbackq = pattern->narg.backquote;
1648         STARTSTACKSTR(expdest);
1649         ifslastp = NULL;
1650         argstr(pattern->narg.text, EXP_TILDE | EXP_CASE);
1651         STPUTC('\0', expdest);
1652         p = grabstackstr(expdest);
1653         result = patmatch(p, val, 0);
1654         popstackmark(&smark);
1655         return result;
1656 }
1657
1658 /*
1659  * Our own itoa().
1660  */
1661
1662 static char *
1663 cvtnum(int num, char *buf)
1664 {
1665         char temp[32];
1666         int neg = num < 0;
1667         char *p = temp + 31;
1668
1669         temp[31] = '\0';
1670
1671         do {
1672                 *--p = num % 10 + '0';
1673         } while ((num /= 10) != 0);
1674
1675         if (neg)
1676                 *--p = '-';
1677
1678         STPUTS(p, buf);
1679         return buf;
1680 }
1681
1682 /*
1683  * Do most of the work for wordexp(3).
1684  */
1685
1686 int
1687 wordexpcmd(int argc, char **argv)
1688 {
1689         size_t len;
1690         int i;
1691
1692         out1fmt("%08x", argc - 1);
1693         for (i = 1, len = 0; i < argc; i++)
1694                 len += strlen(argv[i]);
1695         out1fmt("%08x", (int)len);
1696         for (i = 1; i < argc; i++)
1697                 outbin(argv[i], strlen(argv[i]) + 1, out1);
1698         return (0);
1699 }