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