3
* sh.glob.c: Regular expression expansion
6
* Copyright (c) 1980, 1991 The Regents of the University of California.
9
* Redistribution and use in source and binary forms, with or without
10
* modification, are permitted provided that the following conditions
12
* 1. Redistributions of source code must retain the above copyright
13
* notice, this list of conditions and the following disclaimer.
14
* 2. Redistributions in binary form must reproduce the above copyright
15
* notice, this list of conditions and the following disclaimer in the
16
* documentation and/or other materials provided with the distribution.
17
* 3. All advertising materials mentioning features or use of this software
18
* must display the following acknowledgement:
19
* This product includes software developed by the University of
20
* California, Berkeley and its contributors.
21
* 4. Neither the name of the University nor the names of its contributors
22
* may be used to endorse or promote products derived from this software
23
* without specific prior written permission.
25
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46
static int pargsiz, gargsiz;
51
#define G_NONE 0 /* No globbing needed */
52
#define G_GLOB 1 /* string contains *?[] characters */
53
#define G_CSH 2 /* string contains ~`{ characters */
55
#define GLOBSPACE 100 /* Alloc increment */
56
#define LONGBSIZE 10240 /* Backquote expansion buffer size */
71
* globbing is now done in two stages. In the first pass we expand
72
* csh globbing idioms ~`{ and then we proceed doing the normal
73
* globbing if needed ?*[
75
* Csh type globbing is handled in globexpand() and the rest is
76
* handled in glob() which is part of the 4.4BSD libc.
79
static Char *globtilde __P((Char **, Char *));
80
static Char *handleone __P((Char *, Char **, int));
81
static Char **libglob __P((Char **));
82
static Char **globexpand __P((Char **));
83
static int globbrace __P((Char *, Char *, Char ***));
84
static void expbrace __P((Char ***, Char ***, int));
85
static int pmatch __P((Char *, Char *, Char **));
86
static void pword __P((int));
87
static void psave __P((int));
88
static void backeval __P((Char *, bool));
94
Char gbuf[BUFSIZE], *gstart, *b, *u, *e;
102
for (b = gstart, e = &gbuf[BUFSIZE - 1];
103
*s && *s != '/' && *s != ':' && b < e;
107
if (gethdir(gstart)) {
108
if (adrof(STRnonomatch))
112
stderror(ERR_UNKUSER, short2str(gstart));
114
stderror(ERR_NOHOME);
116
b = &gstart[Strlen(gstart)];
118
slash = gstart[0] == '/' && gstart[1] == '\0';
126
if (slash && gstart[1] == '/')
129
return (Strsave(gstart));
140
* kfk - 17 Jan 1984 - stack hack allows user to get at arbitrary dir names
141
* in stack. PWP: let =foobar pass through (for X windows)
143
if (old[1] == '-' && (old[2] == '\0' || old[2] == '/')) {
148
else if (Isdigit(old[1])) {
151
for (b = &old[2]; Isdigit(*b); b++)
152
dig = dig * 10 + (*b - '0');
153
if (*b != '\0' && *b != '/')
154
/* =<number>foobar */
161
if (!getstakd(new, dig))
164
/* Copy the rest of the string */
165
for (d = &new[Strlen(new)];
166
d < &new[BUFSIZE - 1] && (*d++ = *b++) != '\0';)
178
Char *pm, *pe, *lm, *pl;
181
int size = GLOBSPACE;
183
nv = vl = (Char **) xmalloc((size_t) (sizeof(Char *) * size));
187
/* copy part up to the brace */
188
for (lm = gbuf, p = s; *p != LBRC; *lm++ = *p++)
191
/* check for balanced braces */
192
for (i = 0, pe = ++p; *pe; pe++)
194
/* Ignore everything between [] */
195
for (++pe; *pe != RBRK && *pe != EOS; pe++)
202
else if (*pe == LBRC)
204
else if (*pe == RBRC) {
210
if (i != 0 || *pe == '\0') {
215
for (i = 0, pl = pm = p; pm <= pe; pm++)
218
for (++pm; *pm != RBRK && *pm != EOS; pm++)
242
(void) Strcpy(lm, pl);
243
(void) Strcat(gbuf, pe + 1);
245
*vl++ = Strsave(gbuf);
248
if (vl == &nv[size]) {
250
nv = (Char **) xrealloc((ptr_t) nv,
251
(size_t) (size * sizeof(Char *)));
252
vl = &nv[size - GLOBSPACE];
266
expbrace(nvp, elp, size)
270
Char **vl, **el, **nv, *s;
276
for (el = vl; *el; el++)
279
for (s = *vl; s; s = *++vl) {
283
/* leave {} untouched for find */
284
if (s[0] == '{' && (s[1] == '\0' || (s[1] == '}' && s[2] == '\0')))
286
if ((b = Strchr(s, '{')) != NULL) {
290
if ((len = globbrace(s, b, &bl)) < 0) {
292
stderror(ERR_MISSING, -len);
300
if (&el[len] >= &nv[size]) {
302
l = (int) (&el[len] - &nv[size]);
303
size += GLOBSPACE > l ? GLOBSPACE : l;
306
nv = (Char **) xrealloc((ptr_t) nv,
307
(size_t) (size * sizeof(Char *)));
320
for (bp = el; bp != vp; bp--)
330
for (bp = bl + 1; *bp; *vp++ = *bp++)
346
Char **nv, **vl, **el;
347
int size = GLOBSPACE;
350
nv = vl = (Char **) xmalloc((size_t) (sizeof(Char *) * size));
354
* Step 1: expand backquotes.
356
while ((s = *v++) != '\0') {
357
if (Strchr(s, '`')) {
360
(void) dobackp(s, 0);
361
for (i = 0; i < pargc; i++) {
363
if (vl == &nv[size]) {
365
nv = (Char **) xrealloc((ptr_t) nv,
366
(size_t) (size * sizeof(Char *)));
367
vl = &nv[size - GLOBSPACE];
370
xfree((ptr_t) pargv);
375
if (vl == &nv[size]) {
377
nv = (Char **) xrealloc((ptr_t) nv,
378
(size_t) (size * sizeof(Char *)));
379
vl = &nv[size - GLOBSPACE];
389
* Step 2: expand braces
392
expbrace(&nv, &el, size);
399
for (s = *vl; s; s = *++vl)
401
Char gp[BUFSIZE], *ns;
403
*vl = globtilde(nv, s);
406
if ((ns = globequal(gp, s)) == NULL) {
407
if (!adrof(STRnonomatch)) {
414
/* Expansion succeeded */
425
* Step 4: expand .. if the variable symlinks==expand is set
427
if ( symlinks == SYM_EXPAND )
428
for (s = *vl; s; s = *++vl) {
429
*vl = dnormalize(s, 1);
438
handleone(str, vl, action)
449
setname(short2str(str));
451
stderror(ERR_NAME | ERR_AMBIG);
455
for (t = vlp; (p = *t++) != '\0'; chars++)
458
str = (Char *)xmalloc((size_t)(chars * sizeof(Char)));
459
for (t = vlp, strp = str; (p = *t++) != '\0'; chars++) {
461
*strp++ = *p++ & TRIM;
468
str = Strsave(strip(*vlp));
481
int gflgs = GLOB_QUOTE | GLOB_NOMAGIC | GLOB_ALTNOT;
484
int nonomatch = adrof(STRnonomatch) != 0, magic = 0, match = 0;
494
gflgs |= GLOB_NOCHECK;
497
ptr = short2qstr(*vl);
498
switch (glob(ptr, gflgs, 0, &globv)) {
502
stderror(ERR_NAME | ERR_GLOB);
511
if (globv.gl_flags & GLOB_MAGCHAR) {
512
match |= (globv.gl_matchc != 0);
515
gflgs |= GLOB_APPEND;
518
vl = (globv.gl_pathc == 0 || (magic && !match && !nonomatch)) ?
519
NULL : blk2short(globv.gl_pathv);
530
Char *v[2], **vl, **vo;
533
noglob = adrof(STRnoglob) != 0;
540
return (strip(Strsave(str)));
544
* Expand back-quote, tilde and brace
547
if (noglob || (gflg & G_GLOB) == 0) {
550
return (Strsave(STRNULL));
553
return (handleone(str, vo, action));
561
else if (noglob || (gflg & G_GLOB) == 0)
562
return (strip(Strsave(str)));
567
if ((gflg & G_CSH) && vl != vo)
570
setname(short2str(str));
571
stderror(ERR_NAME | ERR_NOMATCH);
575
return (Strsave(STRNULL));
578
return (handleone(str, vl, action));
595
gargc = blklen(gargv);
599
noglob = adrof(STRnoglob) != 0;
603
* Expand back-quote, tilde and brace
605
vl = vo = globexpand(v);
607
vl = vo = saveblk(v);
609
if (!noglob && (gflg & G_GLOB)) {
617
gargc = vl ? blklen(vl) : 0;
625
gargv = (Char **) xmalloc((size_t) (sizeof(Char *) * gargsiz));
633
void (*f) __P((int));
637
while ((p = *t++) != '\0')
648
while ((p = *t++) != '\0')
657
register Char *p, *c;
659
while ((p = *t++) != '\0') {
660
if (*p == '~' || *p == '=')
662
else if (*p == '{' &&
663
(p[1] == '\0' || (p[1] == '}' && p[2] == '\0')))
666
* The following line used to be *(c = p++), but hp broke their
667
* optimizer in 9.01, so we break the assignment into two pieces
668
* The careful reader here will note that *most* compiler workarounds
669
* in tcsh are either for apollo/DomainOS or hpux. Is it a coincidence?
671
while ( *(c = p) != '\0') {
677
* We do want to expand echo `echo '*'`, so we don't\
678
* use this piece of code anymore.
680
while (*p && *p != '`')
682
if (*p) /* Quoted chars */
687
if (*p) /* The matching ` */
697
else if (symlinks == SYM_EXPAND &&
698
*p && ISDOTDOT(c) && (c == *(t-1) || *(c-1) == '/') )
705
* Command substitute cp. If literal, then this is a substitution from a
706
* << redirection, and so we should not crunch blanks and tabs, separating
707
* words only at newlines.
714
register Char *lp, *rp;
715
Char *ep, word[LONGBSIZE];
724
pargv = (Char **) xmalloc((size_t) (sizeof(Char *) * pargsiz));
726
pargcp = pargs = word;
728
pnleft = LONGBSIZE - 4;
730
for (lp = cp; *lp != '`'; lp++) {
739
for (rp = lp; *rp && *rp != '`'; rp++)
746
oops: stderror(ERR_UNMATCHED, '`');
749
backeval(ep, literal);
756
backeval(cp, literal)
760
register int icnt, c;
762
struct command faket;
765
Char *fakecom[2], ibuf[BUFSIZE];
770
quoted = (literal || (cp[0] & QUOTE)) ? QUOTE : 0;
771
faket.t_dtyp = NODE_COMMAND;
772
faket.t_dflg = F_BACKQ;
776
faket.t_dcom = fakecom;
777
fakecom[0] = STRfakecom1;
781
* We do the psave job to temporarily change the current job so that the
782
* following fork is considered a separate job. This is so that when
783
* backquotes are used in a builtin function that calls glob the "current
784
* job" is not corrupted. We only need one level of pushed jobs as long as
785
* we are sure to fork here.
790
* It would be nicer if we could integrate this redirection more with the
791
* routines in sh.sem.c by doing a fake execute on a builtin function that
795
if (pfork(&faket, -1) == 0) {
798
(void) close(pvec[0]);
799
(void) dmove(pvec[1], 1);
800
(void) dmove(SHDIAG, 2);
803
* Bugfix for nested backquotes by Michael Greim <greim@sbsvax.UUCP>,
804
* posted to comp.bugs.4bsd 12 Sep. 1989.
806
if (pargv) /* mg, 21.dec.88 */
807
blkfree(pargv), pargv = 0, pargsiz = 0;
814
* In the child ``forget'' everything about current aliases or
825
t = syntax(paraml.next, ¶ml, 0);
829
t->t_dflg |= F_NOFORK;
831
(void) sigignore(SIGTSTP);
834
(void) sigignore(SIGTTIN);
837
(void) sigignore(SIGTTOU);
839
execute(t, -1, NULL, NULL);
843
(void) close(pvec[1]);
855
icnt = read(pvec[0], tibuf, BUFSIZE);
856
while (icnt == -1 && errno == EINTR);
861
for (i = 0; i < icnt; i++)
862
ip[i] = (unsigned char) tibuf[i];
876
* Continue around the loop one more time, so that we can eat
877
* the last newline without terminating this word.
882
if (!quoted && (c == ' ' || c == '\t'))
888
* Unless at end-of-file, we will form a new word here if there were
889
* characters in the word, or in any case when we take text literally.
890
* If we didn't make empty words here when literal was set then we
891
* would lose blank lines.
893
if (c != -1 && (cnt || literal))
897
(void) close(pvec[0]);
907
stderror(ERR_WTOOLONG);
908
*pargcp++ = (Char) c;
916
if (pargc == pargsiz - 1) {
917
pargsiz += GLOBSPACE;
918
pargv = (Char **) xrealloc((ptr_t) pargv,
919
(size_t) (pargsiz * sizeof(Char *)));
921
pargv[pargc++] = Strsave(pargs);
928
Gmatch(string, pattern)
929
Char *string, *pattern;
931
return Gnmatch(string, pattern, NULL);
935
Gnmatch(string, pattern, endstr)
936
Char *string, *pattern, **endstr;
938
Char **blk, **p, *tstring = string;
939
int gpol = 1, gres = 0;
941
if (*pattern == '^') {
946
blk = (Char **) xmalloc((size_t) (GLOBSPACE * sizeof(Char *)));
947
blk[0] = Strsave(pattern);
950
expbrace(&blk, NULL, GLOBSPACE);
953
/* Exact matches only */
954
for (p = blk; *p; p++)
955
gres |= pmatch(string, *p, &tstring) == 2 ? 1 : 0;
957
/* partial matches */
958
int minc = 0x7fffffff;
959
for (p = blk; *p; p++)
960
if (pmatch(string, *p, &tstring) != 0) {
961
int t = (int) (tstring - string);
963
if (minc == -1 || minc > t)
966
*endstr = string + minc;
970
return(gres == gpol);
974
* Return 2 on exact match,
975
* Return 1 on substring match.
976
* Return 0 on no match.
977
* *estr will point to the end of the longest exact or substring match.
980
pmatch(string, pattern, estr)
981
register Char *string, *pattern, **estr;
983
register Char stringc, patternc;
984
int match, negate_range;
985
Char rangec, *oestr, *pestr;
988
stringc = *string & TRIM;
990
* apollo compiler bug: switch (patternc = *pattern++) dies
992
patternc = *pattern++;
996
return (stringc == 0 ? 2 : 1);
1004
while (*string) string++;
1012
switch(pmatch(string, pattern, estr)) {
1021
abort(); /* Cannot happen */
1038
if ((negate_range = (*pattern == '^')) != 0)
1040
while ((rangec = *pattern++) != '\0') {
1045
if (rangec == '-' && *(pattern-2) != '[' && *pattern != ']') {
1046
match = (globcharcoll(stringc, *pattern & TRIM) <= 0 &&
1047
globcharcoll(*(pattern-2) & TRIM, stringc) <= 0);
1051
match = (stringc == (rangec & TRIM));
1054
stderror(ERR_NAME | ERR_MISSING, ']');
1055
if (match == negate_range)
1060
if ((patternc & TRIM) != stringc)
1072
register Char *p, *q;
1079
n = (int) ((p - s1) + (q - s2) - 1);
1080
if (++gargc >= gargsiz) {
1081
gargsiz += GLOBSPACE;
1082
gargv = (Char **) xrealloc((ptr_t) gargv,
1083
(size_t) (gargsiz * sizeof(Char *)));
1086
p = gargv[gargc - 1] = (Char *) xmalloc((size_t) (n * sizeof(Char)));
1087
for (q = s1; (*p++ = *q++) != '\0';)
1089
for (p--, q = s2; (*p++ = *q++) != '\0';)
1096
register Char **a, **b;
1098
if (!a) /* check for NULL */
1103
if (!*a) /* check for NULL */
1104
return (*b ? 1 : 0);
1108
return (int) collate(*a, *b);