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