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