1 /* $NetBSD: expand.c,v 1.82 2009/01/18 00:30:54 lukem 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.82 2009/01/18 00:30:54 lukem Exp $");
44 #include <sys/types.h>
55 * Routines to expand arguments to commands. We have to deal with
56 * backquotes, shell variables, and file metacharacters.
77 * Structure specifying which parts of the string should be searched
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 inquotes
; /* search for nul bytes only */
89 char *expdest
; /* output of current string */
90 struct nodelist
*argbackq
; /* list of back quote expressions */
91 struct ifsregion ifsfirst
; /* first struct in list of ifs regions */
92 struct ifsregion
*ifslastp
; /* last struct in list */
93 struct arglist exparg
; /* holds expanded arg list */
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 ifsfree(void);
106 STATIC
void expandmeta(struct strlist
*, int);
107 STATIC
void expmeta(char *, char *);
108 STATIC
void addfname(char *);
109 STATIC
struct strlist
*expsort(struct strlist
*);
110 STATIC
struct strlist
*msort(struct strlist
*, int);
111 STATIC
int pmatch(char *, char *, int);
112 STATIC
char *cvtnum(int, char *);
115 * Expand shell variables and backquotes inside a here document.
119 expandhere(union node
*arg
, int fd
)
122 expandarg(arg
, (struct arglist
*)NULL
, 0);
123 xwrite(fd
, stackblock(), expdest
- stackblock());
128 * Perform variable substitution and command substitution on an argument,
129 * placing the resulting list of arguments in arglist. If EXP_FULL is true,
130 * perform splitting and file name expansion. When arglist is NULL, perform
131 * here document expansion.
135 expandarg(union node
*arg
, struct arglist
*arglist
, int flag
)
140 argbackq
= arg
->narg
.backquote
;
141 STARTSTACKSTR(expdest
);
142 ifsfirst
.next
= NULL
;
144 argstr(arg
->narg
.text
, flag
);
145 if (arglist
== NULL
) {
146 return; /* here document expanded */
148 STPUTC('\0', expdest
);
149 p
= grabstackstr(expdest
);
150 exparg
.lastp
= &exparg
.list
;
154 if (flag
& EXP_FULL
) {
155 ifsbreakup(p
, &exparg
);
156 *exparg
.lastp
= NULL
;
157 exparg
.lastp
= &exparg
.list
;
158 expandmeta(exparg
.list
, flag
);
160 if (flag
& EXP_REDIR
) /*XXX - for now, just remove escapes */
162 sp
= (struct strlist
*)stalloc(sizeof (struct strlist
));
165 exparg
.lastp
= &sp
->next
;
168 *exparg
.lastp
= NULL
;
170 *arglist
->lastp
= exparg
.list
;
171 arglist
->lastp
= exparg
.lastp
;
178 * Perform variable and command substitution.
179 * If EXP_FULL is set, output CTLESC characters to allow for further processing.
180 * Otherwise treat $@ like $* since no splitting will be performed.
184 argstr(char *p
, int flag
)
187 int quotes
= flag
& (EXP_FULL
| EXP_CASE
); /* do CTLESC */
189 const char *ifs
= NULL
;
190 int ifs_split
= EXP_IFS_SPLIT
;
192 if (flag
& EXP_IFS_SPLIT
)
193 ifs
= ifsset() ? ifsval() : " \t\n";
195 if (*p
== '~' && (flag
& (EXP_TILDE
| EXP_VARTILDE
)))
196 p
= exptilde(p
, flag
);
200 case CTLENDVAR
: /* end of expanding yyy in ${xxx-yyy} */
203 /* "$@" syntax adherence hack */
204 if (p
[0] == CTLVAR
&& p
[2] == '@' && p
[3] == '=')
206 if ((flag
& EXP_FULL
) != 0)
211 ifs_split
= EXP_IFS_SPLIT
;
220 p
= evalvar(p
, (flag
& ~EXP_IFS_SPLIT
) | (flag
& ifs_split
));
223 case CTLBACKQ
|CTLQUOTE
:
224 expbackq(argbackq
->n
, c
& CTLQUOTE
, flag
);
225 argbackq
= argbackq
->next
;
233 * sort of a hack - expand tildes in variable
234 * assignments (after the first '=' and after ':'s).
237 if (flag
& EXP_VARTILDE
&& *p
== '~') {
244 p
= exptilde(p
, flag
);
249 if (flag
& EXP_IFS_SPLIT
& ifs_split
&& strchr(ifs
, c
) != NULL
) {
250 /* We need to get the output split here... */
251 recordregion(expdest
- stackblock() - 1,
252 expdest
- stackblock(), 0);
260 exptilde(char *p
, int flag
)
265 int quotes
= flag
& (EXP_FULL
| EXP_CASE
);
267 while ((c
= *p
) != '\0') {
274 if (flag
& EXP_VARTILDE
)
284 if (*(startp
+1) == '\0') {
285 if ((home
= lookupvar("HOME")) == NULL
)
288 if ((pw
= getpwnam(startp
+1)) == NULL
)
295 while ((c
= *home
++) != '\0') {
296 if (quotes
&& SQSYNTAX
[(int)c
] == CCTL
)
297 STPUTC(CTLESC
, expdest
);
308 removerecordregions(int endoff
)
310 if (ifslastp
== NULL
)
313 if (ifsfirst
.endoff
> endoff
) {
314 while (ifsfirst
.next
!= NULL
) {
315 struct ifsregion
*ifsp
;
317 ifsp
= ifsfirst
.next
->next
;
318 ckfree(ifsfirst
.next
);
319 ifsfirst
.next
= ifsp
;
322 if (ifsfirst
.begoff
> endoff
)
325 ifslastp
= &ifsfirst
;
326 ifsfirst
.endoff
= endoff
;
331 ifslastp
= &ifsfirst
;
332 while (ifslastp
->next
&& ifslastp
->next
->begoff
< endoff
)
333 ifslastp
=ifslastp
->next
;
334 while (ifslastp
->next
!= NULL
) {
335 struct ifsregion
*ifsp
;
337 ifsp
= ifslastp
->next
->next
;
338 ckfree(ifslastp
->next
);
339 ifslastp
->next
= ifsp
;
342 if (ifslastp
->endoff
> endoff
)
343 ifslastp
->endoff
= endoff
;
348 * Expand arithmetic expression. Backup to start of expression,
349 * evaluate, place result in (backed up) result, adjust string position.
358 int quotes
= flag
& (EXP_FULL
| EXP_CASE
);
364 * This routine is slightly over-complicated for
365 * efficiency. First we make sure there is
366 * enough space for the result, which may be bigger
367 * than the expression if we add exponentation. Next we
368 * scan backwards looking for the start of arithmetic. If the
369 * next previous character is a CTLESC character, then we
370 * have to rescan starting from the beginning since CTLESC
371 * characters have to be processed left to right.
373 /* SPACE_NEEDED is enough for all digits, plus possible "-", plus 2 (why?) */
374 #define SPACE_NEEDED ((sizeof(intmax_t) * CHAR_BIT + 2) / 3 + 1 + 2)
375 CHECKSTRSPACE((int)(SPACE_NEEDED
- 2), expdest
);
376 USTPUTC('\0', expdest
);
377 start
= stackblock();
379 while (*p
!= CTLARI
&& p
>= start
)
382 error("missing CTLARI (shouldn't happen)");
383 if (p
> start
&& *(p
-1) == CTLESC
)
384 for (p
= start
; *p
!= CTLARI
; p
++)
393 removerecordregions(begoff
);
397 fmtstr(p
, SPACE_NEEDED
, "%"PRIdMAX
, result
);
403 recordregion(begoff
, p
- 1 - start
, 0);
404 adjustment
= expdest
- p
+ 1;
405 STADJUST(-adjustment
, expdest
);
410 * Expand stuff in backwards quotes.
414 expbackq(union node
*cmd
, int quoted
, int flag
)
420 char *dest
= expdest
;
421 struct ifsregion saveifs
, *savelastp
;
422 struct nodelist
*saveargbackq
;
424 int startloc
= dest
- stackblock();
425 char const *syntax
= quoted
? DQSYNTAX
: BASESYNTAX
;
427 int quotes
= flag
& (EXP_FULL
| EXP_CASE
);
431 savelastp
= ifslastp
;
432 saveargbackq
= argbackq
;
435 p
= grabstackstr(dest
);
436 evalbackcmd(cmd
, &in
);
437 ungrabstackstr(p
, dest
);
439 ifslastp
= savelastp
;
440 argbackq
= saveargbackq
;
446 if (--in
.nleft
< 0) {
449 while ((i
= read(in
.fd
, buf
, sizeof buf
)) < 0 && errno
== EINTR
);
450 TRACE(("expbackq: read returns %d\n", i
));
458 if (quotes
&& syntax
[(int)lastc
] == CCTL
)
459 STPUTC(CTLESC
, dest
);
464 /* Eat all trailing newlines */
465 p
= stackblock() + startloc
;
466 while (dest
> p
&& dest
[-1] == '\n')
474 back_exitstatus
= waitforjob(in
.jp
);
476 recordregion(startloc
, dest
- stackblock(), 0);
477 TRACE(("evalbackq: size=%d: \"%.*s\"\n",
478 (int)((dest
- stackblock()) - startloc
),
479 (int)((dest
- stackblock()) - startloc
),
480 stackblock() + startloc
));
488 subevalvar(char *p
, char *str
, int strloc
, int subtype
, int startloc
, int varflags
)
494 int saveherefd
= herefd
;
495 struct nodelist
*saveargbackq
= argbackq
;
504 how
= (varflags
& VSQUOTE
) ? 0 : EXP_CASE
;
511 STACKSTRNUL(expdest
);
513 argbackq
= saveargbackq
;
514 startp
= stackblock() + startloc
;
516 str
= stackblock() + strloc
;
520 setvar(str
, startp
, 0);
521 amount
= startp
- expdest
;
522 STADJUST(amount
, expdest
);
527 if (*p
!= CTLENDVAR
) {
528 outfmt(&errout
, "%s\n", startp
);
531 error("%.*s: parameter %snot set",
533 str
, (varflags
& VSNUL
) ? "null or "
538 for (loc
= startp
; loc
< str
; loc
++) {
541 if (patmatch(str
, startp
, varflags
& VSQUOTE
))
544 if ((varflags
& VSQUOTE
) && *loc
== CTLESC
)
550 for (loc
= str
- 1; loc
>= startp
;) {
553 if (patmatch(str
, startp
, varflags
& VSQUOTE
))
557 if ((varflags
& VSQUOTE
) && loc
> startp
&&
558 *(loc
- 1) == CTLESC
) {
559 for (q
= startp
; q
< loc
; q
++)
569 for (loc
= str
- 1; loc
>= startp
;) {
570 if (patmatch(str
, loc
, varflags
& VSQUOTE
))
573 if ((varflags
& VSQUOTE
) && loc
> startp
&&
574 *(loc
- 1) == CTLESC
) {
575 for (q
= startp
; q
< loc
; q
++)
585 for (loc
= startp
; loc
< str
- 1; loc
++) {
586 if (patmatch(str
, loc
, varflags
& VSQUOTE
))
588 if ((varflags
& VSQUOTE
) && *loc
== CTLESC
)
599 amount
= ((str
- 1) - (loc
- startp
)) - expdest
;
600 STADJUST(amount
, expdest
);
601 while (loc
!= str
- 1)
606 amount
= loc
- expdest
;
607 STADJUST(amount
, expdest
);
608 STPUTC('\0', expdest
);
609 STADJUST(-1, expdest
);
615 * Expand a variable, and return a pointer to the next character in the
620 evalvar(char *p
, int flag
)
633 int quotes
= flag
& (EXP_FULL
| EXP_CASE
);
635 varflags
= (unsigned char)*p
++;
636 subtype
= varflags
& VSTYPE
;
638 special
= !is_name(*p
);
639 p
= strchr(p
, '=') + 1;
641 again
: /* jump here after setting a variable with ${var=text} */
643 set
= varisset(var
, varflags
& VSNUL
);
646 val
= lookupvar(var
);
647 if (val
== NULL
|| ((varflags
& VSNUL
) && val
[0] == '\0')) {
655 startloc
= expdest
- stackblock();
665 error("%.*s: parameter not set",
666 (int)(p
- var
- 1), var
);
671 if (set
&& subtype
!= VSPLUS
) {
672 /* insert the value of the variable */
674 varvalue(var
, varflags
& VSQUOTE
, subtype
, flag
);
675 if (subtype
== VSLENGTH
) {
676 varlen
= expdest
- stackblock() - startloc
;
677 STADJUST(-varlen
, expdest
);
680 char const *syntax
= (varflags
& VSQUOTE
) ? DQSYNTAX
683 if (subtype
== VSLENGTH
) {
688 if (quotes
&& syntax
[(int)*val
] == CCTL
)
689 STPUTC(CTLESC
, expdest
);
690 STPUTC(*val
++, expdest
);
698 apply_ifs
= ((varflags
& VSQUOTE
) == 0 ||
699 (*var
== '@' && shellparam
.nparam
!= 1));
703 expdest
= cvtnum(varlen
, expdest
);
714 argstr(p
, flag
| (apply_ifs
? EXP_IFS_SPLIT
: 0));
716 * ${x-a b c} doesn't get split, but removing the
717 * 'apply_ifs = 0' apparently breaks ${1+"$@"}..
718 * ${x-'a b' c} should generate 2 args.
720 /* We should have marked stuff already */
732 * Terminate the string and start recording the pattern
735 STPUTC('\0', expdest
);
736 patloc
= expdest
- stackblock();
737 if (subevalvar(p
, NULL
, patloc
, subtype
,
738 startloc
, varflags
) == 0) {
739 int amount
= (expdest
- stackblock() - patloc
) + 1;
740 STADJUST(-amount
, expdest
);
742 /* Remove any recorded regions beyond start of variable */
743 removerecordregions(startloc
);
751 if (subevalvar(p
, var
, 0, subtype
, startloc
, varflags
)) {
754 * Remove any recorded regions beyond
757 removerecordregions(startloc
);
768 recordregion(startloc
, expdest
- stackblock(),
771 if (subtype
!= VSNORMAL
) { /* skip to end of alternative */
774 if ((c
= *p
++) == CTLESC
)
776 else if (c
== CTLBACKQ
|| c
== (CTLBACKQ
|CTLQUOTE
)) {
778 argbackq
= argbackq
->next
;
779 } else if (c
== CTLVAR
) {
780 if ((*p
++ & VSTYPE
) != VSNORMAL
)
782 } else if (c
== CTLENDVAR
) {
794 * Test whether a specialized variable is set.
798 varisset(char *name
, int nulok
)
801 return backgndpid
!= -1;
802 else if (*name
== '@' || *name
== '*') {
803 if (*shellparam
.p
== NULL
)
809 for (av
= shellparam
.p
; *av
; av
++)
814 } else if (is_digit(*name
)) {
816 int num
= atoi(name
);
818 if (num
> shellparam
.nparam
)
824 ap
= shellparam
.p
[num
- 1];
826 if (nulok
&& (ap
== NULL
|| *ap
== '\0'))
835 * Add the value of a specialized variable to the stack string.
839 varvalue(char *name
, int quoted
, int subtype
, int flag
)
848 #define STRTODEST(p) \
850 if (flag & (EXP_FULL | EXP_CASE) && subtype != VSLENGTH) { \
851 syntax = quoted? DQSYNTAX : BASESYNTAX; \
853 if (syntax[(int)*p] == CCTL) \
854 STPUTC(CTLESC, expdest); \
855 STPUTC(*p++, expdest); \
859 STPUTC(*p++, expdest); \
871 num
= shellparam
.nparam
;
876 expdest
= cvtnum(num
, expdest
);
879 for (i
= 0; optlist
[i
].name
; i
++) {
880 if (optlist
[i
].val
&& optlist
[i
].letter
)
881 STPUTC(optlist
[i
].letter
, expdest
);
885 if (flag
& EXP_FULL
&& quoted
) {
886 for (ap
= shellparam
.p
; (p
= *ap
++) != NULL
; ) {
889 /* A NUL separates args inside "" */
890 STPUTC('\0', expdest
);
900 for (ap
= shellparam
.p
; (p
= *ap
++) != NULL
; ) {
903 STPUTC(sep
, expdest
);
911 if (is_digit(*name
)) {
913 if (num
> 0 && num
<= shellparam
.nparam
) {
914 p
= shellparam
.p
[num
- 1];
925 * Record the fact that we have to scan this region of the
926 * string for IFS characters.
930 recordregion(int start
, int end
, int inquotes
)
932 struct ifsregion
*ifsp
;
934 if (ifslastp
== NULL
) {
937 if (ifslastp
->endoff
== start
938 && ifslastp
->inquotes
== inquotes
) {
939 /* extend previous area */
940 ifslastp
->endoff
= end
;
943 ifsp
= (struct ifsregion
*)ckmalloc(sizeof (struct ifsregion
));
944 ifslastp
->next
= ifsp
;
947 ifslastp
->next
= NULL
;
948 ifslastp
->begoff
= start
;
949 ifslastp
->endoff
= end
;
950 ifslastp
->inquotes
= inquotes
;
956 * Break the argument string into pieces based upon IFS and add the
957 * strings to the argument list. The regions of the string to be
958 * searched for IFS characters have been stored by recordregion.
961 ifsbreakup(char *string
, struct arglist
*arglist
)
963 struct ifsregion
*ifsp
;
970 int had_param_ch
= 0;
974 if (ifslastp
== NULL
) {
975 /* Return entire argument, IFS doesn't apply to any of it */
976 sp
= (struct strlist
*)stalloc(sizeof *sp
);
978 *arglist
->lastp
= sp
;
979 arglist
->lastp
= &sp
->next
;
983 ifs
= ifsset() ? ifsval() : " \t\n";
985 for (ifsp
= &ifsfirst
; ifsp
!= NULL
; ifsp
= ifsp
->next
) {
986 p
= string
+ ifsp
->begoff
;
987 while (p
< string
+ ifsp
->endoff
) {
992 if (ifsp
->inquotes
) {
993 /* Only NULs (should be from "$@") end args */
1000 if (!strchr(ifs
, *p
)) {
1005 ifsspc
= strchr(" \t\n", *p
);
1007 /* Ignore IFS whitespace at start */
1008 if (q
== start
&& ifsspc
!= NULL
) {
1015 /* Save this argument... */
1017 sp
= (struct strlist
*)stalloc(sizeof *sp
);
1019 *arglist
->lastp
= sp
;
1020 arglist
->lastp
= &sp
->next
;
1023 if (ifsspc
!= NULL
) {
1024 /* Ignore further trailing IFS whitespace */
1025 for (; p
< string
+ ifsp
->endoff
; p
++) {
1029 if (strchr(ifs
, *p
) == NULL
) {
1033 if (strchr(" \t\n", *p
) == NULL
) {
1044 * Save anything left as an argument.
1045 * Traditionally we have treated 'IFS=':'; set -- x$IFS' as
1046 * generating 2 arguments, the second of which is empty.
1047 * Some recent clarification of the Posix spec say that it
1048 * should only generate one....
1050 if (had_param_ch
|| *start
!= 0) {
1051 sp
= (struct strlist
*)stalloc(sizeof *sp
);
1053 *arglist
->lastp
= sp
;
1054 arglist
->lastp
= &sp
->next
;
1061 while (ifsfirst
.next
!= NULL
) {
1062 struct ifsregion
*ifsp
;
1064 ifsp
= ifsfirst
.next
->next
;
1065 ckfree(ifsfirst
.next
);
1066 ifsfirst
.next
= ifsp
;
1070 ifsfirst
.next
= NULL
;
1076 * Expand shell metacharacters. At this point, the only control characters
1077 * should be escapes. The results are stored in the list exparg.
1084 expandmeta(struct strlist
*str
, int flag
)
1087 struct strlist
**savelastp
;
1090 /* TODO - EXP_REDIR */
1096 for (;;) { /* fast check for meta chars */
1097 if ((c
= *p
++) == '\0')
1099 if (c
== '*' || c
== '?' || c
== '[' || c
== '!')
1102 savelastp
= exparg
.lastp
;
1104 if (expdir
== NULL
) {
1105 int i
= strlen(str
->text
);
1106 expdir
= ckmalloc(i
< 2048 ? 2048 : i
); /* XXX */
1109 expmeta(expdir
, str
->text
);
1113 if (exparg
.lastp
== savelastp
) {
1118 *exparg
.lastp
= str
;
1119 rmescapes(str
->text
);
1120 exparg
.lastp
= &str
->next
;
1122 *exparg
.lastp
= NULL
;
1123 *savelastp
= sp
= expsort(*savelastp
);
1124 while (sp
->next
!= NULL
)
1126 exparg
.lastp
= &sp
->next
;
1134 * Do metacharacter (i.e. *, ?, [...]) expansion.
1138 expmeta(char *enddir
, char *name
)
1154 for (p
= name
; ; p
++) {
1155 if (*p
== '*' || *p
== '?')
1157 else if (*p
== '[') {
1162 while (*q
== CTLQUOTEMARK
)
1166 if (*q
== '/' || *q
== '\0')
1173 } else if (*p
== '!' && p
[1] == '!' && (p
== name
|| p
[-1] == '/')) {
1175 } else if (*p
== '\0')
1177 else if (*p
== CTLQUOTEMARK
)
1179 else if (*p
== CTLESC
)
1187 if (metaflag
== 0) { /* we've reached the end of the file name */
1188 if (enddir
!= expdir
)
1190 for (p
= name
; ; p
++) {
1191 if (*p
== CTLQUOTEMARK
)
1199 if (metaflag
== 0 || lstat(expdir
, &statb
) >= 0)
1204 if (start
!= name
) {
1207 while (*p
== CTLQUOTEMARK
)
1214 if (enddir
== expdir
) {
1216 } else if (enddir
== expdir
+ 1 && *expdir
== '/') {
1222 if ((dirp
= opendir(cp
)) == NULL
)
1224 if (enddir
!= expdir
)
1226 if (*endname
== 0) {
1234 while (*p
== CTLQUOTEMARK
)
1240 while (! int_pending() && (dp
= readdir(dirp
)) != NULL
) {
1241 if (dp
->d_name
[0] == '.' && ! matchdot
)
1243 if (patmatch(start
, dp
->d_name
, 0)) {
1245 scopy(dp
->d_name
, enddir
);
1248 for (p
= enddir
, cp
= dp
->d_name
;
1249 (*p
++ = *cp
++) != '\0';)
1252 expmeta(p
, endname
);
1263 * Add a file name to the list.
1267 addfname(char *name
)
1272 p
= stalloc(strlen(name
) + 1);
1274 sp
= (struct strlist
*)stalloc(sizeof *sp
);
1277 exparg
.lastp
= &sp
->next
;
1282 * Sort the results of file name expansion. It calculates the number of
1283 * strings to sort and then calls msort (short for merge sort) to do the
1287 STATIC
struct strlist
*
1288 expsort(struct strlist
*str
)
1294 for (sp
= str
; sp
; sp
= sp
->next
)
1296 return msort(str
, len
);
1300 STATIC
struct strlist
*
1301 msort(struct strlist
*list
, int len
)
1303 struct strlist
*p
, *q
= NULL
;
1304 struct strlist
**lpp
;
1312 for (n
= half
; --n
>= 0 ; ) {
1316 q
->next
= NULL
; /* terminate first half of list */
1317 q
= msort(list
, half
); /* sort first half of list */
1318 p
= msort(p
, len
- half
); /* sort second half */
1321 if (strcmp(p
->text
, q
->text
) < 0) {
1324 if ((p
= *lpp
) == NULL
) {
1331 if ((q
= *lpp
) == NULL
) {
1343 * Returns true if the pattern matches the string.
1347 patmatch(char *pattern
, char *string
, int squoted
)
1350 if (pattern
[0] == '!' && pattern
[1] == '!')
1351 return 1 - pmatch(pattern
+ 2, string
);
1354 return pmatch(pattern
, string
, squoted
);
1359 pmatch(char *pattern
, char *string
, int squoted
)
1371 if (squoted
&& *q
== CTLESC
)
1379 if (squoted
&& *q
== CTLESC
)
1386 while (c
== CTLQUOTEMARK
|| c
== '*')
1388 if (c
!= CTLESC
&& c
!= CTLQUOTEMARK
&&
1389 c
!= '?' && c
!= '*' && c
!= '[') {
1391 if (squoted
&& *q
== CTLESC
&&
1396 if (squoted
&& *q
== CTLESC
)
1402 if (pmatch(p
, q
, squoted
))
1404 if (squoted
&& *q
== CTLESC
)
1406 } while (*q
++ != '\0');
1417 while (*endp
== CTLQUOTEMARK
)
1420 goto dft
; /* no matching ] */
1421 if (*endp
== CTLESC
)
1433 if (squoted
&& chr
== CTLESC
)
1439 if (c
== CTLQUOTEMARK
)
1443 if (*p
== '-' && p
[1] != ']') {
1445 while (*p
== CTLQUOTEMARK
)
1449 if (chr
>= c
&& chr
<= *p
)
1456 } while ((c
= *p
++) != ']');
1457 if (found
== invert
)
1462 if (squoted
&& *q
== CTLESC
)
1478 * Remove any CTLESC characters from a string.
1482 rmescapes(char *str
)
1487 while (*p
!= CTLESC
&& *p
!= CTLQUOTEMARK
) {
1493 if (*p
== CTLQUOTEMARK
) {
1507 * See if a pattern matches in a case statement.
1511 casematch(union node
*pattern
, char *val
)
1513 struct stackmark smark
;
1517 setstackmark(&smark
);
1518 argbackq
= pattern
->narg
.backquote
;
1519 STARTSTACKSTR(expdest
);
1521 argstr(pattern
->narg
.text
, EXP_TILDE
| EXP_CASE
);
1522 STPUTC('\0', expdest
);
1523 p
= grabstackstr(expdest
);
1524 result
= patmatch(p
, val
, 0);
1525 popstackmark(&smark
);
1534 cvtnum(int num
, char *buf
)
1538 char *p
= temp
+ 31;
1543 *--p
= num
% 10 + '0';
1544 } while ((num
/= 10) != 0);
1555 * Do most of the work for wordexp(3).
1559 wordexpcmd(int argc
, char **argv
)
1564 out1fmt("%d", argc
- 1);
1566 for (i
= 1, len
= 0; i
< argc
; i
++)
1567 len
+= strlen(argv
[i
]);
1568 out1fmt("%zu", len
);
1570 for (i
= 1; i
< argc
; i
++) {