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