1 /* $NetBSD: expand.c,v 1.93 2015/08/27 07:46:47 christos Exp $ */
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
38 static char sccsid
[] = "@(#)expand.c 8.5 (Berkeley) 5/15/95";
40 __RCSID("$NetBSD: expand.c,v 1.93 2015/08/27 07:46:47 christos Exp $");
44 #include <sys/types.h>
57 * Routines to expand arguments to commands. We have to deal with
58 * backquotes, shell variables, and file metacharacters.
80 * Structure specifying which parts of the string should be searched
85 struct ifsregion
*next
; /* next region in list */
86 int begoff
; /* offset of start of region */
87 int endoff
; /* offset of end of region */
88 int inquotes
; /* search for nul bytes only */
92 char *expdest
; /* output of current string */
93 struct nodelist
*argbackq
; /* list of back quote expressions */
94 struct ifsregion ifsfirst
; /* first struct in list of ifs regions */
95 struct ifsregion
*ifslastp
; /* last struct in list */
96 struct arglist exparg
; /* holds expanded arg list */
98 STATIC
void argstr(char *, int);
99 STATIC
char *exptilde(char *, int);
100 STATIC
void expbackq(union node
*, int, int);
101 STATIC
int subevalvar(char *, char *, int, int, int, int, int);
102 STATIC
char *evalvar(char *, int);
103 STATIC
int varisset(char *, int);
104 STATIC
void varvalue(char *, int, int, int);
105 STATIC
void recordregion(int, int, int);
106 STATIC
void removerecordregions(int);
107 STATIC
void ifsbreakup(char *, struct arglist
*);
108 STATIC
void ifsfree(void);
109 STATIC
void expandmeta(struct strlist
*, int);
110 STATIC
void expmeta(char *, char *);
111 STATIC
void addfname(char *);
112 STATIC
struct strlist
*expsort(struct strlist
*);
113 STATIC
struct strlist
*msort(struct strlist
*, int);
114 STATIC
int pmatch(char *, char *, int);
115 STATIC
char *cvtnum(int, char *);
118 * Expand shell variables and backquotes inside a here document.
122 expandhere(union node
*arg
, int fd
)
125 expandarg(arg
, NULL
, 0);
126 xwrite(fd
, stackblock(), expdest
- stackblock());
131 * Perform variable substitution and command substitution on an argument,
132 * placing the resulting list of arguments in arglist. If EXP_FULL is true,
133 * perform splitting and file name expansion. When arglist is NULL, perform
134 * here document expansion.
138 expandarg(union node
*arg
, struct arglist
*arglist
, int flag
)
143 argbackq
= arg
->narg
.backquote
;
144 STARTSTACKSTR(expdest
);
145 ifsfirst
.next
= NULL
;
147 argstr(arg
->narg
.text
, flag
);
148 if (arglist
== NULL
) {
149 return; /* here document expanded */
151 STPUTC('\0', expdest
);
152 p
= grabstackstr(expdest
);
153 exparg
.lastp
= &exparg
.list
;
157 if (flag
& EXP_FULL
) {
158 ifsbreakup(p
, &exparg
);
159 *exparg
.lastp
= NULL
;
160 exparg
.lastp
= &exparg
.list
;
161 expandmeta(exparg
.list
, flag
);
163 if (flag
& EXP_REDIR
) /*XXX - for now, just remove escapes */
165 sp
= (struct strlist
*)stalloc(sizeof (struct strlist
));
168 exparg
.lastp
= &sp
->next
;
171 *exparg
.lastp
= NULL
;
173 *arglist
->lastp
= exparg
.list
;
174 arglist
->lastp
= exparg
.lastp
;
181 * Perform variable and command substitution.
182 * If EXP_FULL is set, output CTLESC characters to allow for further processing.
183 * Otherwise treat $@ like $* since no splitting will be performed.
187 argstr(char *p
, int flag
)
190 int quotes
= flag
& (EXP_FULL
| EXP_CASE
); /* do CTLESC */
192 const char *ifs
= NULL
;
193 int ifs_split
= EXP_IFS_SPLIT
;
195 if (flag
& EXP_IFS_SPLIT
)
196 ifs
= ifsset() ? ifsval() : " \t\n";
198 if (*p
== '~' && (flag
& (EXP_TILDE
| EXP_VARTILDE
)))
199 p
= exptilde(p
, flag
);
203 case CTLENDVAR
: /* end of expanding yyy in ${xxx-yyy} */
206 /* "$@" syntax adherence hack */
207 if (p
[0] == CTLVAR
&& p
[2] == '@' && p
[3] == '=')
209 if ((flag
& EXP_FULL
) != 0)
214 ifs_split
= EXP_IFS_SPLIT
;
223 p
= evalvar(p
, (flag
& ~EXP_IFS_SPLIT
) | (flag
& ifs_split
));
226 case CTLBACKQ
|CTLQUOTE
:
227 expbackq(argbackq
->n
, c
& CTLQUOTE
, flag
);
228 argbackq
= argbackq
->next
;
236 * sort of a hack - expand tildes in variable
237 * assignments (after the first '=' and after ':'s).
240 if (flag
& EXP_VARTILDE
&& *p
== '~') {
247 p
= exptilde(p
, flag
);
252 if (flag
& ifs_split
&& strchr(ifs
, c
) != NULL
) {
253 /* We need to get the output split here... */
254 recordregion(expdest
- stackblock() - 1,
255 expdest
- stackblock(), 0);
263 exptilde(char *p
, int flag
)
268 int quotes
= flag
& (EXP_FULL
| EXP_CASE
);
270 while ((c
= *p
) != '\0') {
277 if (flag
& EXP_VARTILDE
)
287 if (*(startp
+1) == '\0') {
288 if ((home
= lookupvar("HOME")) == NULL
)
291 if ((pw
= getpwnam(startp
+1)) == NULL
)
298 while ((c
= *home
++) != '\0') {
299 if (quotes
&& SQSYNTAX
[(int)c
] == CCTL
)
300 STPUTC(CTLESC
, expdest
);
311 removerecordregions(int endoff
)
313 if (ifslastp
== NULL
)
316 if (ifsfirst
.endoff
> endoff
) {
317 while (ifsfirst
.next
!= NULL
) {
318 struct ifsregion
*ifsp
;
320 ifsp
= ifsfirst
.next
->next
;
321 ckfree(ifsfirst
.next
);
322 ifsfirst
.next
= ifsp
;
325 if (ifsfirst
.begoff
> endoff
)
328 ifslastp
= &ifsfirst
;
329 ifsfirst
.endoff
= endoff
;
334 ifslastp
= &ifsfirst
;
335 while (ifslastp
->next
&& ifslastp
->next
->begoff
< endoff
)
336 ifslastp
=ifslastp
->next
;
337 while (ifslastp
->next
!= NULL
) {
338 struct ifsregion
*ifsp
;
340 ifsp
= ifslastp
->next
->next
;
341 ckfree(ifslastp
->next
);
342 ifslastp
->next
= ifsp
;
345 if (ifslastp
->endoff
> endoff
)
346 ifslastp
->endoff
= endoff
;
351 * Expand arithmetic expression. Backup to start of expression,
352 * evaluate, place result in (backed up) result, adjust string position.
361 int quotes
= flag
& (EXP_FULL
| EXP_CASE
);
367 * This routine is slightly over-complicated for
368 * efficiency. First we make sure there is
369 * enough space for the result, which may be bigger
370 * than the expression if we add exponentation. Next we
371 * scan backwards looking for the start of arithmetic. If the
372 * next previous character is a CTLESC character, then we
373 * have to rescan starting from the beginning since CTLESC
374 * characters have to be processed left to right.
376 /* SPACE_NEEDED is enough for all digits, plus possible "-", plus 2 (why?) */
377 #define SPACE_NEEDED ((sizeof(intmax_t) * CHAR_BIT + 2) / 3 + 1 + 2)
378 CHECKSTRSPACE((int)(SPACE_NEEDED
- 2), expdest
);
379 USTPUTC('\0', expdest
);
380 start
= stackblock();
382 while (*p
!= CTLARI
&& p
>= start
)
385 error("missing CTLARI (shouldn't happen)");
386 if (p
> start
&& *(p
-1) == CTLESC
)
387 for (p
= start
; *p
!= CTLARI
; p
++)
396 removerecordregions(begoff
);
400 fmtstr(p
, SPACE_NEEDED
, "%"PRIdMAX
, result
);
406 recordregion(begoff
, p
- 1 - start
, 0);
407 adjustment
= expdest
- p
+ 1;
408 STADJUST(-adjustment
, expdest
);
413 * Expand stuff in backwards quotes.
417 expbackq(union node
*cmd
, int quoted
, int flag
)
423 char *dest
= expdest
;
424 struct ifsregion saveifs
, *savelastp
;
425 struct nodelist
*saveargbackq
;
427 int startloc
= dest
- stackblock();
428 char const *syntax
= quoted
? DQSYNTAX
: BASESYNTAX
;
430 int quotes
= flag
& (EXP_FULL
| EXP_CASE
);
435 savelastp
= ifslastp
;
436 saveargbackq
= argbackq
;
439 p
= grabstackstr(dest
);
440 evalbackcmd(cmd
, &in
);
441 ungrabstackstr(p
, dest
);
443 ifslastp
= savelastp
;
444 argbackq
= saveargbackq
;
451 if (--in
.nleft
< 0) {
454 while ((i
= read(in
.fd
, buf
, sizeof buf
)) < 0 && errno
== EINTR
)
456 TRACE(("expbackq: read returns %d\n", i
));
467 CHECKSTRSPACE(nnl
+ 2, dest
);
472 if (quotes
&& syntax
[(int)lastc
] == CCTL
)
473 USTPUTC(CTLESC
, dest
);
474 USTPUTC(lastc
, dest
);
484 back_exitstatus
= waitforjob(in
.jp
);
486 recordregion(startloc
, dest
- stackblock(), 0);
487 TRACE(("evalbackq: size=%d: \"%.*s\"\n",
488 (int)((dest
- stackblock()) - startloc
),
489 (int)((dest
- stackblock()) - startloc
),
490 stackblock() + startloc
));
498 subevalvar(char *p
, char *str
, int strloc
, int subtype
, int startloc
, int varflags
, int quotes
)
504 int saveherefd
= herefd
;
505 struct nodelist
*saveargbackq
= argbackq
;
514 how
= (varflags
& VSQUOTE
) ? 0 : EXP_CASE
;
521 STACKSTRNUL(expdest
);
523 argbackq
= saveargbackq
;
524 startp
= stackblock() + startloc
;
526 str
= stackblock() + strloc
;
530 setvar(str
, startp
, 0);
531 amount
= startp
- expdest
;
532 STADJUST(amount
, expdest
);
537 if (*p
!= CTLENDVAR
) {
538 outfmt(&errout
, "%s\n", startp
);
541 error("%.*s: parameter %snot set",
543 str
, (varflags
& VSNUL
) ? "null or "
548 for (loc
= startp
; loc
< str
; loc
++) {
551 if (patmatch(str
, startp
, quotes
))
554 if (quotes
&& *loc
== CTLESC
)
560 for (loc
= str
- 1; loc
>= startp
;) {
563 if (patmatch(str
, startp
, quotes
))
567 if (quotes
&& loc
> startp
&&
568 *(loc
- 1) == CTLESC
) {
569 for (q
= startp
; q
< loc
; q
++)
579 for (loc
= str
- 1; loc
>= startp
;) {
580 if (patmatch(str
, loc
, quotes
))
583 if (quotes
&& loc
> startp
&&
584 *(loc
- 1) == CTLESC
) {
585 for (q
= startp
; q
< loc
; q
++)
595 for (loc
= startp
; loc
< str
- 1; loc
++) {
596 if (patmatch(str
, loc
, quotes
))
598 if (quotes
&& *loc
== CTLESC
)
609 amount
= ((str
- 1) - (loc
- startp
)) - expdest
;
610 STADJUST(amount
, expdest
);
611 while (loc
!= str
- 1)
616 amount
= loc
- expdest
;
617 STADJUST(amount
, expdest
);
618 STPUTC('\0', expdest
);
619 STADJUST(-1, expdest
);
625 * Expand a variable, and return a pointer to the next character in the
630 evalvar(char *p
, int flag
)
643 int quotes
= flag
& (EXP_FULL
| EXP_CASE
);
645 varflags
= (unsigned char)*p
++;
646 subtype
= varflags
& VSTYPE
;
648 special
= !is_name(*p
);
649 p
= strchr(p
, '=') + 1;
651 again
: /* jump here after setting a variable with ${var=text} */
652 if (varflags
& VSLINENO
) {
657 } else if (special
) {
658 set
= varisset(var
, varflags
& VSNUL
);
661 val
= lookupvar(var
);
662 if (val
== NULL
|| ((varflags
& VSNUL
) && val
[0] == '\0')) {
670 startloc
= expdest
- stackblock();
672 if (!set
&& uflag
&& *var
!= '@' && *var
!= '*') {
680 error("%.*s: parameter not set",
681 (int)(p
- var
- 1), var
);
686 if (set
&& subtype
!= VSPLUS
) {
687 /* insert the value of the variable */
689 varvalue(var
, varflags
& VSQUOTE
, subtype
, flag
);
690 if (subtype
== VSLENGTH
) {
691 varlen
= expdest
- stackblock() - startloc
;
692 STADJUST(-varlen
, expdest
);
695 char const *syntax
= (varflags
& VSQUOTE
) ? DQSYNTAX
698 if (subtype
== VSLENGTH
) {
703 if (quotes
&& syntax
[(int)*val
] == CCTL
)
704 STPUTC(CTLESC
, expdest
);
705 STPUTC(*val
++, expdest
);
713 if (flag
& EXP_IN_QUOTES
)
715 else if (varflags
& VSQUOTE
) {
716 if (*var
== '@' && shellparam
.nparam
!= 1)
720 * Mark so that we don't apply IFS if we recurse through
721 * here expanding $bar from "${foo-$bar}".
723 flag
|= EXP_IN_QUOTES
;
731 expdest
= cvtnum(varlen
, expdest
);
742 argstr(p
, flag
| (apply_ifs
? EXP_IFS_SPLIT
: 0));
744 * ${x-a b c} doesn't get split, but removing the
745 * 'apply_ifs = 0' apparently breaks ${1+"$@"}..
746 * ${x-'a b' c} should generate 2 args.
748 /* We should have marked stuff already */
760 * Terminate the string and start recording the pattern
763 STPUTC('\0', expdest
);
764 patloc
= expdest
- stackblock();
765 if (subevalvar(p
, NULL
, patloc
, subtype
,
766 startloc
, varflags
, quotes
) == 0) {
767 int amount
= (expdest
- stackblock() - patloc
) + 1;
768 STADJUST(-amount
, expdest
);
770 /* Remove any recorded regions beyond start of variable */
771 removerecordregions(startloc
);
779 if (subevalvar(p
, var
, 0, subtype
, startloc
, varflags
, quotes
)) {
782 * Remove any recorded regions beyond
785 removerecordregions(startloc
);
794 p
[-1] = '='; /* recover overwritten '=' */
797 recordregion(startloc
, expdest
- stackblock(),
800 if (subtype
!= VSNORMAL
) { /* skip to end of alternative */
803 if ((c
= *p
++) == CTLESC
)
805 else if (c
== CTLBACKQ
|| c
== (CTLBACKQ
|CTLQUOTE
)) {
807 argbackq
= argbackq
->next
;
808 } else if (c
== CTLVAR
) {
809 if ((*p
++ & VSTYPE
) != VSNORMAL
)
811 } else if (c
== CTLENDVAR
) {
823 * Test whether a specialized variable is set.
827 varisset(char *name
, int nulok
)
830 return backgndpid
!= -1;
831 else if (*name
== '@' || *name
== '*') {
832 if (*shellparam
.p
== NULL
)
838 for (av
= shellparam
.p
; *av
; av
++)
843 } else if (is_digit(*name
)) {
845 int num
= atoi(name
);
847 if (num
> shellparam
.nparam
)
853 ap
= shellparam
.p
[num
- 1];
855 if (nulok
&& (ap
== NULL
|| *ap
== '\0'))
864 * Add the value of a specialized variable to the stack string.
868 varvalue(char *name
, int quoted
, int subtype
, int flag
)
877 #define STRTODEST(p) \
879 if (flag & (EXP_FULL | EXP_CASE) && subtype != VSLENGTH) { \
880 syntax = quoted? DQSYNTAX : BASESYNTAX; \
882 if (syntax[(int)*p] == CCTL) \
883 STPUTC(CTLESC, expdest); \
884 STPUTC(*p++, expdest); \
888 STPUTC(*p++, expdest); \
900 num
= shellparam
.nparam
;
905 expdest
= cvtnum(num
, expdest
);
908 for (i
= 0; optlist
[i
].name
; i
++) {
909 if (optlist
[i
].val
&& optlist
[i
].letter
)
910 STPUTC(optlist
[i
].letter
, expdest
);
914 if (flag
& EXP_FULL
&& quoted
) {
915 for (ap
= shellparam
.p
; (p
= *ap
++) != NULL
; ) {
918 /* A NUL separates args inside "" */
919 STPUTC('\0', expdest
);
929 for (ap
= shellparam
.p
; (p
= *ap
++) != NULL
; ) {
932 STPUTC(sep
, expdest
);
940 if (is_digit(*name
)) {
942 if (num
> 0 && num
<= shellparam
.nparam
) {
943 p
= shellparam
.p
[num
- 1];
954 * Record the fact that we have to scan this region of the
955 * string for IFS characters.
959 recordregion(int start
, int end
, int inquotes
)
961 struct ifsregion
*ifsp
;
963 if (ifslastp
== NULL
) {
966 if (ifslastp
->endoff
== start
967 && ifslastp
->inquotes
== inquotes
) {
968 /* extend previous area */
969 ifslastp
->endoff
= end
;
972 ifsp
= (struct ifsregion
*)ckmalloc(sizeof (struct ifsregion
));
973 ifslastp
->next
= ifsp
;
976 ifslastp
->next
= NULL
;
977 ifslastp
->begoff
= start
;
978 ifslastp
->endoff
= end
;
979 ifslastp
->inquotes
= inquotes
;
985 * Break the argument string into pieces based upon IFS and add the
986 * strings to the argument list. The regions of the string to be
987 * searched for IFS characters have been stored by recordregion.
990 ifsbreakup(char *string
, struct arglist
*arglist
)
992 struct ifsregion
*ifsp
;
999 int had_param_ch
= 0;
1003 if (ifslastp
== NULL
) {
1004 /* Return entire argument, IFS doesn't apply to any of it */
1005 sp
= (struct strlist
*)stalloc(sizeof *sp
);
1007 *arglist
->lastp
= sp
;
1008 arglist
->lastp
= &sp
->next
;
1012 ifs
= ifsset() ? ifsval() : " \t\n";
1014 for (ifsp
= &ifsfirst
; ifsp
!= NULL
; ifsp
= ifsp
->next
) {
1015 p
= string
+ ifsp
->begoff
;
1016 while (p
< string
+ ifsp
->endoff
) {
1021 if (ifsp
->inquotes
) {
1022 /* Only NULs (should be from "$@") end args */
1029 if (!strchr(ifs
, *p
)) {
1034 ifsspc
= strchr(" \t\n", *p
);
1036 /* Ignore IFS whitespace at start */
1037 if (q
== start
&& ifsspc
!= NULL
) {
1044 /* Save this argument... */
1046 sp
= (struct strlist
*)stalloc(sizeof *sp
);
1048 *arglist
->lastp
= sp
;
1049 arglist
->lastp
= &sp
->next
;
1052 if (ifsspc
!= NULL
) {
1053 /* Ignore further trailing IFS whitespace */
1054 for (; p
< string
+ ifsp
->endoff
; p
++) {
1058 if (strchr(ifs
, *p
) == NULL
) {
1062 if (strchr(" \t\n", *p
) == NULL
) {
1073 * Save anything left as an argument.
1074 * Traditionally we have treated 'IFS=':'; set -- x$IFS' as
1075 * generating 2 arguments, the second of which is empty.
1076 * Some recent clarification of the Posix spec say that it
1077 * should only generate one....
1079 if (had_param_ch
|| *start
!= 0) {
1080 sp
= (struct strlist
*)stalloc(sizeof *sp
);
1082 *arglist
->lastp
= sp
;
1083 arglist
->lastp
= &sp
->next
;
1090 while (ifsfirst
.next
!= NULL
) {
1091 struct ifsregion
*ifsp
;
1093 ifsp
= ifsfirst
.next
->next
;
1094 ckfree(ifsfirst
.next
);
1095 ifsfirst
.next
= ifsp
;
1099 ifsfirst
.next
= NULL
;
1105 * Expand shell metacharacters. At this point, the only control characters
1106 * should be escapes. The results are stored in the list exparg.
1113 expandmeta(struct strlist
*str
, int flag
)
1116 struct strlist
**savelastp
;
1119 /* TODO - EXP_REDIR */
1125 for (;;) { /* fast check for meta chars */
1126 if ((c
= *p
++) == '\0')
1128 if (c
== '*' || c
== '?' || c
== '[' || c
== '!')
1131 savelastp
= exparg
.lastp
;
1133 if (expdir
== NULL
) {
1134 int i
= strlen(str
->text
);
1135 expdir
= ckmalloc(i
< 2048 ? 2048 : i
); /* XXX */
1138 expmeta(expdir
, str
->text
);
1142 if (exparg
.lastp
== savelastp
) {
1147 *exparg
.lastp
= str
;
1148 rmescapes(str
->text
);
1149 exparg
.lastp
= &str
->next
;
1151 *exparg
.lastp
= NULL
;
1152 *savelastp
= sp
= expsort(*savelastp
);
1153 while (sp
->next
!= NULL
)
1155 exparg
.lastp
= &sp
->next
;
1163 * Do metacharacter (i.e. *, ?, [...]) expansion.
1167 expmeta(char *enddir
, char *name
)
1183 for (p
= name
; ; p
++) {
1184 if (*p
== '*' || *p
== '?')
1186 else if (*p
== '[') {
1191 while (*q
== CTLQUOTEMARK
)
1195 if (*q
== '/' || *q
== '\0')
1202 } else if (*p
== '!' && p
[1] == '!' && (p
== name
|| p
[-1] == '/')) {
1204 } else if (*p
== '\0')
1206 else if (*p
== CTLQUOTEMARK
)
1208 else if (*p
== CTLESC
)
1216 if (metaflag
== 0) { /* we've reached the end of the file name */
1217 if (enddir
!= expdir
)
1219 for (p
= name
; ; p
++) {
1220 if (*p
== CTLQUOTEMARK
)
1228 if (metaflag
== 0 || lstat(expdir
, &statb
) >= 0)
1233 if (start
!= name
) {
1236 while (*p
== CTLQUOTEMARK
)
1243 if (enddir
== expdir
) {
1245 } else if (enddir
== expdir
+ 1 && *expdir
== '/') {
1251 if ((dirp
= opendir(cp
)) == NULL
)
1253 if (enddir
!= expdir
)
1255 if (*endname
== 0) {
1263 while (*p
== CTLQUOTEMARK
)
1269 while (! int_pending() && (dp
= readdir(dirp
)) != NULL
) {
1270 if (dp
->d_name
[0] == '.' && ! matchdot
)
1272 if (patmatch(start
, dp
->d_name
, 0)) {
1274 scopy(dp
->d_name
, enddir
);
1277 for (p
= enddir
, cp
= dp
->d_name
;
1278 (*p
++ = *cp
++) != '\0';)
1281 expmeta(p
, endname
);
1292 * Add a file name to the list.
1296 addfname(char *name
)
1301 p
= stalloc(strlen(name
) + 1);
1303 sp
= (struct strlist
*)stalloc(sizeof *sp
);
1306 exparg
.lastp
= &sp
->next
;
1311 * Sort the results of file name expansion. It calculates the number of
1312 * strings to sort and then calls msort (short for merge sort) to do the
1316 STATIC
struct strlist
*
1317 expsort(struct strlist
*str
)
1323 for (sp
= str
; sp
; sp
= sp
->next
)
1325 return msort(str
, len
);
1329 STATIC
struct strlist
*
1330 msort(struct strlist
*list
, int len
)
1332 struct strlist
*p
, *q
= NULL
;
1333 struct strlist
**lpp
;
1341 for (n
= half
; --n
>= 0 ; ) {
1345 q
->next
= NULL
; /* terminate first half of list */
1346 q
= msort(list
, half
); /* sort first half of list */
1347 p
= msort(p
, len
- half
); /* sort second half */
1350 if (strcmp(p
->text
, q
->text
) < 0) {
1353 if ((p
= *lpp
) == NULL
) {
1360 if ((q
= *lpp
) == NULL
) {
1371 * See if a character matches a character class, starting at the first colon
1373 * If a valid character class is recognized, a pointer to the next character
1374 * after the final closing bracket is stored into *end, otherwise a null
1375 * pointer is stored into *end.
1378 match_charclass(char *p
, wchar_t chr
, char **end
)
1386 nameend
= strstr(p
, ":]");
1387 if (nameend
== NULL
|| (size_t)(nameend
- p
) >= sizeof(name
) ||
1390 memcpy(name
, p
, nameend
- p
);
1391 name
[nameend
- p
] = '\0';
1393 cclass
= wctype(name
);
1394 /* An unknown class matches nothing but is valid nevertheless. */
1397 return iswctype(chr
, cclass
);
1403 * Returns true if the pattern matches the string.
1407 patmatch(char *pattern
, char *string
, int squoted
)
1410 if (pattern
[0] == '!' && pattern
[1] == '!')
1411 return 1 - pmatch(pattern
+ 2, string
);
1414 return pmatch(pattern
, string
, squoted
);
1419 pmatch(char *pattern
, char *string
, int squoted
)
1431 if (squoted
&& *q
== CTLESC
)
1439 if (squoted
&& *q
== CTLESC
)
1446 while (c
== CTLQUOTEMARK
|| c
== '*')
1448 if (c
!= CTLESC
&& c
!= CTLQUOTEMARK
&&
1449 c
!= '?' && c
!= '*' && c
!= '[') {
1451 if (squoted
&& *q
== CTLESC
&&
1456 if (squoted
&& *q
== CTLESC
)
1462 if (pmatch(p
, q
, squoted
))
1464 if (squoted
&& *q
== CTLESC
)
1466 } while (*q
++ != '\0');
1477 while (*endp
== CTLQUOTEMARK
)
1480 goto dft
; /* no matching ] */
1481 if (*endp
== CTLESC
)
1493 if (squoted
&& chr
== CTLESC
)
1499 if (c
== CTLQUOTEMARK
)
1501 if (c
== '[' && *p
== ':') {
1502 found
|= match_charclass(p
, chr
, &end
);
1508 if (*p
== '-' && p
[1] != ']') {
1510 while (*p
== CTLQUOTEMARK
)
1514 if (chr
>= c
&& chr
<= *p
)
1521 } while ((c
= *p
++) != ']');
1522 if (found
== invert
)
1527 if (squoted
&& *q
== CTLESC
)
1543 * Remove any CTLESC characters from a string.
1547 rmescapes(char *str
)
1552 while (*p
!= CTLESC
&& *p
!= CTLQUOTEMARK
) {
1558 if (*p
== CTLQUOTEMARK
) {
1572 * See if a pattern matches in a case statement.
1576 casematch(union node
*pattern
, char *val
)
1578 struct stackmark smark
;
1582 setstackmark(&smark
);
1583 argbackq
= pattern
->narg
.backquote
;
1584 STARTSTACKSTR(expdest
);
1586 argstr(pattern
->narg
.text
, EXP_TILDE
| EXP_CASE
);
1587 STPUTC('\0', expdest
);
1588 p
= grabstackstr(expdest
);
1589 result
= patmatch(p
, val
, 0);
1590 popstackmark(&smark
);
1599 cvtnum(int num
, char *buf
)
1603 char *p
= temp
+ 31;
1608 *--p
= num
% 10 + '0';
1609 } while ((num
/= 10) != 0);
1620 * Do most of the work for wordexp(3).
1624 wordexpcmd(int argc
, char **argv
)
1629 out1fmt("%d", argc
- 1);
1631 for (i
= 1, len
= 0; i
< argc
; i
++)
1632 len
+= strlen(argv
[i
]);
1633 out1fmt("%zu", len
);
1635 for (i
= 1; i
< argc
; i
++) {