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