2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
36 * @(#)expand.c 8.5 (Berkeley) 5/15/95
37 * $FreeBSD: src/bin/sh/expand.c,v 1.51 2006/11/07 22:46:13 stefanf Exp $
38 * $DragonFly: src/bin/sh/expand.c,v 1.9 2007/01/07 16:58:30 pavalos Exp $
41 #include <sys/types.h>
54 * Routines to expand arguments to commands. We have to deal with
55 * 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 nulonly
; /* search for nul bytes only */
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 */
95 STATIC
void argstr(char *, int);
96 STATIC
char *exptilde(char *, int);
97 STATIC
void expbackq(union node
*, int, int);
98 STATIC
int subevalvar(char *, char *, int, int, int, int);
99 STATIC
char *evalvar(char *, int);
100 STATIC
int varisset(char *, int);
101 STATIC
void varvalue(char *, int, int, int);
102 STATIC
void recordregion(int, int, int);
103 STATIC
void removerecordregions(int);
104 STATIC
void ifsbreakup(char *, struct arglist
*);
105 STATIC
void expandmeta(struct strlist
*, int);
106 STATIC
void expmeta(char *, char *);
107 STATIC
void addfname(char *);
108 STATIC
struct strlist
*expsort(struct strlist
*);
109 STATIC
struct strlist
*msort(struct strlist
*, int);
110 STATIC
int pmatch(char *, char *, int);
111 STATIC
char *cvtnum(int, char *);
112 STATIC
int collate_range_cmp(int, int);
115 collate_range_cmp(int c1
, int c2
)
117 static char s1
[2], s2
[2];
121 return (strcoll(s1
, s2
));
124 extern int oexitstatus
;
127 * Expand shell variables and backquotes inside a here document.
128 * union node *arg the document
129 * int fd; where to write the expanded version
133 expandhere(union node
*arg
, int fd
)
136 expandarg(arg
, NULL
, 0);
137 xwrite(fd
, stackblock(), expdest
- stackblock());
142 * Perform variable substitution and command substitution on an argument,
143 * placing the resulting list of arguments in arglist. If EXP_FULL is true,
144 * perform splitting and file name expansion. When arglist is NULL, perform
145 * here document expansion.
149 expandarg(union node
*arg
, struct arglist
*arglist
, int flag
)
154 argbackq
= arg
->narg
.backquote
;
155 STARTSTACKSTR(expdest
);
156 ifsfirst
.next
= NULL
;
158 argstr(arg
->narg
.text
, flag
);
159 if (arglist
== NULL
) {
160 return; /* here document expanded */
162 STPUTC('\0', expdest
);
163 p
= grabstackstr(expdest
);
164 exparg
.lastp
= &exparg
.list
;
168 if (flag
& EXP_FULL
) {
169 ifsbreakup(p
, &exparg
);
170 *exparg
.lastp
= NULL
;
171 exparg
.lastp
= &exparg
.list
;
172 expandmeta(exparg
.list
, flag
);
174 if (flag
& EXP_REDIR
) /*XXX - for now, just remove escapes */
176 sp
= (struct strlist
*)stalloc(sizeof (struct strlist
));
179 exparg
.lastp
= &sp
->next
;
181 while (ifsfirst
.next
!= NULL
) {
182 struct ifsregion
*ifsp
;
184 ifsp
= ifsfirst
.next
->next
;
185 ckfree(ifsfirst
.next
);
186 ifsfirst
.next
= ifsp
;
189 *exparg
.lastp
= NULL
;
191 *arglist
->lastp
= exparg
.list
;
192 arglist
->lastp
= exparg
.lastp
;
199 * Perform variable and command substitution. If EXP_FULL is set, output CTLESC
200 * characters to allow for further processing. Otherwise treat
201 * $@ like $* since no splitting will be performed.
205 argstr(char *p
, int flag
)
208 int quotes
= flag
& (EXP_FULL
| EXP_CASE
| EXP_REDIR
); /* do CTLESC */
211 if (*p
== '~' && (flag
& (EXP_TILDE
| EXP_VARTILDE
)))
212 p
= exptilde(p
, flag
);
216 case CTLENDVAR
: /* ??? */
219 /* "$@" syntax adherence hack */
220 if (p
[0] == CTLVAR
&& p
[2] == '@' && p
[3] == '=')
222 if ((flag
& EXP_FULL
) != 0)
232 p
= evalvar(p
, flag
);
235 case CTLBACKQ
|CTLQUOTE
:
236 expbackq(argbackq
->n
, c
& CTLQUOTE
, flag
);
237 argbackq
= argbackq
->next
;
245 * sort of a hack - expand tildes in variable
246 * assignments (after the first '=' and after ':'s).
249 if (flag
& EXP_VARTILDE
&& *p
== '~') {
256 p
= exptilde(p
, flag
);
267 exptilde(char *p
, int flag
)
272 int quotes
= flag
& (EXP_FULL
| EXP_CASE
| EXP_REDIR
);
274 while ((c
= *p
) != '\0') {
281 if (flag
& EXP_VARTILDE
)
291 if (*(startp
+1) == '\0') {
292 if ((home
= lookupvar("HOME")) == NULL
)
295 if ((pw
= getpwnam(startp
+1)) == NULL
)
302 while ((c
= *home
++) != '\0') {
303 if (quotes
&& SQSYNTAX
[(int)c
] == CCTL
)
304 STPUTC(CTLESC
, expdest
);
315 removerecordregions(int endoff
)
317 if (ifslastp
== NULL
)
320 if (ifsfirst
.endoff
> endoff
) {
321 while (ifsfirst
.next
!= NULL
) {
322 struct ifsregion
*ifsp
;
324 ifsp
= ifsfirst
.next
->next
;
325 ckfree(ifsfirst
.next
);
326 ifsfirst
.next
= ifsp
;
329 if (ifsfirst
.begoff
> endoff
)
332 ifslastp
= &ifsfirst
;
333 ifsfirst
.endoff
= endoff
;
338 ifslastp
= &ifsfirst
;
339 while (ifslastp
->next
&& ifslastp
->next
->begoff
< endoff
)
340 ifslastp
=ifslastp
->next
;
341 while (ifslastp
->next
!= NULL
) {
342 struct ifsregion
*ifsp
;
344 ifsp
= ifslastp
->next
->next
;
345 ckfree(ifslastp
->next
);
346 ifslastp
->next
= ifsp
;
349 if (ifslastp
->endoff
> endoff
)
350 ifslastp
->endoff
= endoff
;
354 * Expand arithmetic expression. Backup to start of expression,
355 * evaluate, place result in (backed up) result, adjust string position.
363 int quotes
= flag
& (EXP_FULL
| EXP_CASE
| EXP_REDIR
);
368 * This routine is slightly over-complicated for
369 * efficiency. First we make sure there is
370 * enough space for the result, which may be bigger
371 * than the expression if we add exponentiation. Next we
372 * scan backwards looking for the start of arithmetic. If the
373 * next previous character is a CTLESC character, then we
374 * have to rescan starting from the beginning since CTLESC
375 * characters have to be processed left to right.
377 #if INT_MAX / 1000000000 >= 10 || INT_MIN / 1000000000 <= -10
378 #error "integers with more than 10 digits are not supported"
380 CHECKSTRSPACE(12 - 2, expdest
);
381 USTPUTC('\0', expdest
);
382 start
= stackblock();
384 while (p
>= start
&& *p
!= CTLARI
)
386 if (p
< start
|| *p
!= CTLARI
)
387 error("missing CTLARI (shouldn't happen)");
388 if (p
> start
&& *(p
- 1) == CTLESC
)
389 for (p
= start
; *p
!= CTLARI
; p
++)
398 removerecordregions(begoff
);
402 fmtstr(p
, 12, "%d", result
);
406 recordregion(begoff
, p
- 1 - start
, 0);
407 result
= expdest
- p
+ 1;
408 STADJUST(-result
, 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
| EXP_REDIR
);
435 savelastp
= ifslastp
;
436 saveargbackq
= argbackq
;
439 p
= grabstackstr(dest
);
440 evalbackcmd(cmd
, &in
);
441 ungrabstackstr(p
, dest
);
443 ifslastp
= savelastp
;
444 argbackq
= saveargbackq
;
450 /* Don't copy trailing newlines */
452 if (--in
.nleft
< 0) {
455 while ((i
= read(in
.fd
, buf
, sizeof buf
)) < 0 && errno
== EINTR
);
456 TRACE(("expbackq: read returns %d\n", i
));
464 if (quotes
&& syntax
[(int)lastc
] == CCTL
)
465 STPUTC(CTLESC
, dest
);
483 exitstatus
= waitforjob(in
.jp
, NULL
);
485 recordregion(startloc
, dest
- stackblock(), 0);
486 TRACE(("evalbackq: size=%d: \"%.*s\"\n",
487 (dest
- stackblock()) - startloc
,
488 (dest
- stackblock()) - startloc
,
489 stackblock() + startloc
));
497 subevalvar(char *p
, char *str
, int strloc
, int subtype
, int startloc
,
504 int saveherefd
= herefd
;
505 struct nodelist
*saveargbackq
= argbackq
;
510 STACKSTRNUL(expdest
);
512 argbackq
= saveargbackq
;
513 startp
= stackblock() + startloc
;
515 str
= stackblock() + strloc
;
519 setvar(str
, startp
, 0);
520 amount
= startp
- expdest
;
521 STADJUST(amount
, expdest
);
528 if (*p
!= CTLENDVAR
) {
529 outfmt(&errout
, "%s\n", startp
);
532 error("%.*s: parameter %snot set", (int)(p
- str
- 1),
533 str
, (varflags
& VSNUL
) ? "null or "
538 for (loc
= startp
; loc
< str
; loc
++) {
541 if (patmatch(str
, startp
, varflags
& VSQUOTE
)) {
546 if ((varflags
& VSQUOTE
) && *loc
== CTLESC
)
552 for (loc
= str
- 1; loc
>= startp
;) {
555 if (patmatch(str
, startp
, varflags
& VSQUOTE
)) {
561 if ((varflags
& VSQUOTE
) && loc
> startp
&&
562 *(loc
- 1) == CTLESC
) {
563 for (q
= startp
; q
< loc
; q
++)
573 for (loc
= str
- 1; loc
>= startp
;) {
574 if (patmatch(str
, loc
, varflags
& VSQUOTE
)) {
575 amount
= loc
- expdest
;
576 STADJUST(amount
, expdest
);
580 if ((varflags
& VSQUOTE
) && loc
> startp
&&
581 *(loc
- 1) == CTLESC
) {
582 for (q
= startp
; q
< loc
; q
++)
592 for (loc
= startp
; loc
< str
- 1; loc
++) {
593 if (patmatch(str
, loc
, varflags
& VSQUOTE
)) {
594 amount
= loc
- expdest
;
595 STADJUST(amount
, expdest
);
598 if ((varflags
& VSQUOTE
) && *loc
== CTLESC
)
609 amount
= ((str
- 1) - (loc
- startp
)) - expdest
;
610 STADJUST(amount
, expdest
);
611 while (loc
!= str
- 1)
618 * Expand a variable, and return a pointer to the next character in the
623 evalvar(char *p
, int flag
)
636 int quotes
= flag
& (EXP_FULL
| EXP_CASE
| EXP_REDIR
);
638 varflags
= (unsigned char)*p
++;
639 subtype
= varflags
& VSTYPE
;
644 p
= strchr(p
, '=') + 1;
645 again
: /* jump here after setting a variable with ${var=text} */
646 if (varflags
& VSLINENO
) {
650 p
[-1] = '\0'; /* temporarily overwrite '=' to have \0
652 } else if (special
) {
653 set
= varisset(var
, varflags
& VSNUL
);
656 val
= bltinlookup(var
, 1);
657 if (val
== NULL
|| ((varflags
& VSNUL
) && val
[0] == '\0')) {
664 startloc
= expdest
- stackblock();
673 error("%.*s: parameter not set", (int)(p
- var
- 1),
677 if (set
&& subtype
!= VSPLUS
) {
678 /* insert the value of the variable */
680 varvalue(var
, varflags
& VSQUOTE
, subtype
, flag
);
681 if (subtype
== VSLENGTH
) {
682 varlen
= expdest
- stackblock() - startloc
;
683 STADJUST(-varlen
, expdest
);
686 char const *syntax
= (varflags
& VSQUOTE
) ? DQSYNTAX
689 if (subtype
== VSLENGTH
) {
696 syntax
[(int)*val
] == CCTL
)
697 STPUTC(CTLESC
, expdest
);
698 STPUTC(*val
++, expdest
);
705 if (subtype
== VSPLUS
)
708 easy
= ((varflags
& VSQUOTE
) == 0 ||
709 (*var
== '@' && shellparam
.nparam
!= 1));
714 expdest
= cvtnum(varlen
, expdest
);
721 recordregion(startloc
, expdest
- stackblock(),
742 * Terminate the string and start recording the pattern
745 STPUTC('\0', expdest
);
746 patloc
= expdest
- stackblock();
747 if (subevalvar(p
, NULL
, patloc
, subtype
,
748 startloc
, varflags
) == 0) {
749 int amount
= (expdest
- stackblock() - patloc
) + 1;
750 STADJUST(-amount
, expdest
);
752 /* Remove any recorded regions beyond start of variable */
753 removerecordregions(startloc
);
759 if (subevalvar(p
, var
, 0, subtype
, startloc
, varflags
)) {
762 * Remove any recorded regions beyond
765 removerecordregions(startloc
);
776 error("${%.*s%s}: Bad substitution", c
, var
,
777 (c
> 0 && *p
!= CTLENDVAR
) ? "..." : "");
782 p
[-1] = '='; /* recover overwritten '=' */
784 if (subtype
!= VSNORMAL
) { /* skip to end of alternative */
787 if ((c
= *p
++) == CTLESC
)
789 else if (c
== CTLBACKQ
|| c
== (CTLBACKQ
|CTLQUOTE
)) {
791 argbackq
= argbackq
->next
;
792 } else if (c
== CTLVAR
) {
793 if ((*p
++ & VSTYPE
) != VSNORMAL
)
795 } else if (c
== CTLENDVAR
) {
807 * Test whether a specialized variable is set.
811 varisset(char *name
, int nulok
)
815 return backgndpid
!= -1;
816 else if (*name
== '@' || *name
== '*') {
817 if (*shellparam
.p
== NULL
)
823 for (av
= shellparam
.p
; *av
; av
++)
828 } else if (is_digit(*name
)) {
830 int num
= atoi(name
);
832 if (num
> shellparam
.nparam
)
838 ap
= shellparam
.p
[num
- 1];
840 if (nulok
&& (ap
== NULL
|| *ap
== '\0'))
849 * Add the value of a specialized variable to the stack string.
853 varvalue(char *name
, int quoted
, int subtype
, int flag
)
862 #define STRTODEST(p) \
864 if (flag & (EXP_FULL | EXP_CASE) && subtype != VSLENGTH) { \
865 syntax = quoted? DQSYNTAX : BASESYNTAX; \
867 if (syntax[(int)*p] == CCTL) \
868 STPUTC(CTLESC, expdest); \
869 STPUTC(*p++, expdest); \
873 STPUTC(*p++, expdest); \
885 num
= shellparam
.nparam
;
890 expdest
= cvtnum(num
, expdest
);
893 for (i
= 0 ; i
< NOPTS
; i
++) {
895 STPUTC(optlist
[i
].letter
, expdest
);
899 if (flag
& EXP_FULL
&& quoted
) {
900 for (ap
= shellparam
.p
; (p
= *ap
++) != NULL
; ) {
903 STPUTC('\0', expdest
);
913 for (ap
= shellparam
.p
; (p
= *ap
++) != NULL
; ) {
916 STPUTC(sep
, expdest
);
924 if (is_digit(*name
)) {
926 if (num
> 0 && num
<= shellparam
.nparam
) {
927 p
= shellparam
.p
[num
- 1];
938 * Record the the fact that we have to scan this region of the
939 * string for IFS characters.
943 recordregion(int start
, int end
, int nulonly
)
945 struct ifsregion
*ifsp
;
947 if (ifslastp
== NULL
) {
950 ifsp
= (struct ifsregion
*)ckmalloc(sizeof (struct ifsregion
));
951 ifslastp
->next
= ifsp
;
954 ifslastp
->next
= NULL
;
955 ifslastp
->begoff
= start
;
956 ifslastp
->endoff
= end
;
957 ifslastp
->nulonly
= nulonly
;
963 * Break the argument string into pieces based upon IFS and add the
964 * strings to the argument list. The regions of the string to be
965 * searched for IFS characters have been stored by recordregion.
968 ifsbreakup(char *string
, struct arglist
*arglist
)
970 struct ifsregion
*ifsp
;
983 if (ifslastp
!= NULL
) {
986 p
= string
+ ifsp
->begoff
;
987 nulonly
= ifsp
->nulonly
;
988 ifs
= nulonly
? nullstr
:
989 ( ifsset() ? ifsval() : " \t\n" );
991 while (p
< string
+ ifsp
->endoff
) {
995 if (strchr(ifs
, *p
)) {
997 ifsspc
= (strchr(" \t\n", *p
) != NULL
);
998 /* Ignore IFS whitespace at start */
999 if (q
== start
&& ifsspc
) {
1005 sp
= (struct strlist
*)stalloc(sizeof *sp
);
1007 *arglist
->lastp
= sp
;
1008 arglist
->lastp
= &sp
->next
;
1012 if (p
>= string
+ ifsp
->endoff
) {
1018 if (strchr(ifs
, *p
) == NULL
) {
1021 } else if (strchr(" \t\n",*p
) == NULL
) {
1037 } while ((ifsp
= ifsp
->next
) != NULL
);
1038 if (*start
|| (!ifsspc
&& start
> string
)) {
1039 sp
= (struct strlist
*)stalloc(sizeof *sp
);
1041 *arglist
->lastp
= sp
;
1042 arglist
->lastp
= &sp
->next
;
1045 sp
= (struct strlist
*)stalloc(sizeof *sp
);
1047 *arglist
->lastp
= sp
;
1048 arglist
->lastp
= &sp
->next
;
1055 * Expand shell metacharacters. At this point, the only control characters
1056 * should be escapes. The results are stored in the list exparg.
1059 STATIC
char *expdir
;
1063 expandmeta(struct strlist
*str
, int flag __unused
)
1066 struct strlist
**savelastp
;
1069 /* TODO - EXP_REDIR */
1075 for (;;) { /* fast check for meta chars */
1076 if ((c
= *p
++) == '\0')
1078 if (c
== '*' || c
== '?' || c
== '[' || c
== '!')
1081 savelastp
= exparg
.lastp
;
1083 if (expdir
== NULL
) {
1084 int i
= strlen(str
->text
);
1085 expdir
= ckmalloc(i
< 2048 ? 2048 : i
); /* XXX */
1088 expmeta(expdir
, str
->text
);
1092 if (exparg
.lastp
== savelastp
) {
1097 *exparg
.lastp
= str
;
1098 rmescapes(str
->text
);
1099 exparg
.lastp
= &str
->next
;
1101 *exparg
.lastp
= NULL
;
1102 *savelastp
= sp
= expsort(*savelastp
);
1103 while (sp
->next
!= NULL
)
1105 exparg
.lastp
= &sp
->next
;
1113 * Do metacharacter (i.e. *, ?, [...]) expansion.
1117 expmeta(char *enddir
, char *name
)
1132 for (p
= name
; ; p
++) {
1133 if (*p
== '*' || *p
== '?')
1135 else if (*p
== '[') {
1137 if (*q
== '!' || *q
== '^')
1140 while (*q
== CTLQUOTEMARK
)
1144 if (*q
== '/' || *q
== '\0')
1151 } else if (*p
== '!' && p
[1] == '!' && (p
== name
|| p
[-1] == '/')) {
1153 } else if (*p
== '\0')
1155 else if (*p
== CTLQUOTEMARK
)
1157 else if (*p
== CTLESC
)
1165 if (metaflag
== 0) { /* we've reached the end of the file name */
1166 if (enddir
!= expdir
)
1168 for (p
= name
; ; p
++) {
1169 if (*p
== CTLQUOTEMARK
)
1177 if (metaflag
== 0 || lstat(expdir
, &statb
) >= 0)
1182 if (start
!= name
) {
1185 while (*p
== CTLQUOTEMARK
)
1192 if (enddir
== expdir
) {
1194 } else if (enddir
== expdir
+ 1 && *expdir
== '/') {
1200 if ((dirp
= opendir(q
)) == NULL
)
1202 if (enddir
!= expdir
)
1204 if (*endname
== 0) {
1212 while (*p
== CTLQUOTEMARK
)
1218 while (! int_pending() && (dp
= readdir(dirp
)) != NULL
) {
1219 if (dp
->d_name
[0] == '.' && ! matchdot
)
1221 if (patmatch(start
, dp
->d_name
, 0)) {
1223 scopy(dp
->d_name
, enddir
);
1227 for (t
= enddir
, q
= dp
->d_name
;
1228 (*t
++ = *q
++) != '\0';)
1231 expmeta(t
, endname
);
1242 * Add a file name to the list.
1246 addfname(char *name
)
1251 p
= stalloc(strlen(name
) + 1);
1253 sp
= (struct strlist
*)stalloc(sizeof *sp
);
1256 exparg
.lastp
= &sp
->next
;
1261 * Sort the results of file name expansion. It calculates the number of
1262 * strings to sort and then calls msort (short for merge sort) to do the
1266 STATIC
struct strlist
*
1267 expsort(struct strlist
*str
)
1273 for (sp
= str
; sp
; sp
= sp
->next
)
1275 return msort(str
, len
);
1279 STATIC
struct strlist
*
1280 msort(struct strlist
*list
, int len
)
1282 struct strlist
*p
, *q
= NULL
;
1283 struct strlist
**lpp
;
1291 for (n
= half
; --n
>= 0 ; ) {
1295 q
->next
= NULL
; /* terminate first half of list */
1296 q
= msort(list
, half
); /* sort first half of list */
1297 p
= msort(p
, len
- half
); /* sort second half */
1300 if (strcmp(p
->text
, q
->text
) < 0) {
1303 if ((p
= *lpp
) == NULL
) {
1310 if ((q
= *lpp
) == NULL
) {
1322 * Returns true if the pattern matches the string.
1326 patmatch(char *pattern
, char *string
, int squoted
)
1329 if (pattern
[0] == '!' && pattern
[1] == '!')
1330 return 1 - pmatch(pattern
+ 2, string
);
1333 return pmatch(pattern
, string
, squoted
);
1338 pmatch(char *pattern
, char *string
, int squoted
)
1350 if (squoted
&& *q
== CTLESC
)
1358 if (squoted
&& *q
== CTLESC
)
1365 while (c
== CTLQUOTEMARK
|| c
== '*')
1367 if (c
!= CTLESC
&& c
!= CTLQUOTEMARK
&&
1368 c
!= '?' && c
!= '*' && c
!= '[') {
1370 if (squoted
&& *q
== CTLESC
&&
1375 if (squoted
&& *q
== CTLESC
)
1381 if (pmatch(p
, q
, squoted
))
1383 if (squoted
&& *q
== CTLESC
)
1385 } while (*q
++ != '\0');
1393 if (*endp
== '!' || *endp
== '^')
1396 while (*endp
== CTLQUOTEMARK
)
1399 goto dft
; /* no matching ] */
1400 if (*endp
== CTLESC
)
1406 if (*p
== '!' || *p
== '^') {
1412 if (squoted
&& chr
== CTLESC
)
1418 if (c
== CTLQUOTEMARK
)
1422 if (*p
== '-' && p
[1] != ']') {
1424 while (*p
== CTLQUOTEMARK
)
1428 if ( collate_range_cmp(chr
, c
) >= 0
1429 && collate_range_cmp(chr
, *p
) <= 0
1437 } while ((c
= *p
++) != ']');
1438 if (found
== invert
)
1443 if (squoted
&& *q
== CTLESC
)
1459 * Remove any CTLESC characters from a string.
1463 rmescapes(char *str
)
1468 while (*p
!= CTLESC
&& *p
!= CTLQUOTEMARK
) {
1474 if (*p
== CTLQUOTEMARK
) {
1488 * See if a pattern matches in a case statement.
1492 casematch(union node
*pattern
, char *val
)
1494 struct stackmark smark
;
1498 setstackmark(&smark
);
1499 argbackq
= pattern
->narg
.backquote
;
1500 STARTSTACKSTR(expdest
);
1502 argstr(pattern
->narg
.text
, EXP_TILDE
| EXP_CASE
);
1503 STPUTC('\0', expdest
);
1504 p
= grabstackstr(expdest
);
1505 result
= patmatch(p
, val
, 0);
1506 popstackmark(&smark
);
1515 cvtnum(int num
, char *buf
)
1519 char *p
= temp
+ 31;
1524 *--p
= num
% 10 + '0';
1525 } while ((num
/= 10) != 0);
1536 * Do most of the work for wordexp(3).
1540 wordexpcmd(int argc
, char **argv
)
1545 out1fmt("%08x", argc
- 1);
1546 for (i
= 1, len
= 0; i
< argc
; i
++)
1547 len
+= strlen(argv
[i
]);
1548 out1fmt("%08x", (int)len
);
1549 for (i
= 1; i
< argc
; i
++) {