1 /* $Header: /src/pub/tcsh/sh.glob.c,v 3.55 2004/03/21 16:48:14 christos Exp $ */
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. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 RCSID("$Id: sh.glob.c,v 3.55 2004/03/21 16:48:14 christos Exp $")
43 static int pargsiz, gargsiz;
48 #define G_NONE 0 /* No globbing needed */
49 #define G_GLOB 1 /* string contains *?[] characters */
50 #define G_CSH 2 /* string contains ~`{ characters */
52 #define GLOBSPACE 100 /* Alloc increment */
53 #define LONGBSIZE 10240 /* Backquote expansion buffer size */
68 * globbing is now done in two stages. In the first pass we expand
69 * csh globbing idioms ~`{ and then we proceed doing the normal
70 * globbing if needed ?*[
72 * Csh type globbing is handled in globexpand() and the rest is
73 * handled in glob() which is part of the 4.4BSD libc.
76 static Char *globtilde __P((Char **, Char *));
77 static Char *handleone __P((Char *, Char **, int));
78 static Char **libglob __P((Char **));
79 static Char **globexpand __P((Char **));
80 static int globbrace __P((Char *, Char *, Char ***));
81 static void expbrace __P((Char ***, Char ***, int));
82 static void pword __P((int));
83 static void psave __P((int));
84 static void backeval __P((Char *, bool));
90 Char gbuf[BUFSIZE], *gstart, *b, *u, *e;
98 for (b = gstart, e = &gbuf[BUFSIZE - 1];
99 *s && *s != '/' && *s != ':' && b < e;
103 if (gethdir(gstart)) {
104 if (adrof(STRnonomatch))
108 stderror(ERR_UNKUSER, short2str(gstart));
110 stderror(ERR_NOHOME);
112 b = &gstart[Strlen(gstart)];
114 slash = gstart[0] == '/' && gstart[1] == '\0';
122 if (slash && gstart[1] == '/')
125 return (Strsave(gstart));
136 * kfk - 17 Jan 1984 - stack hack allows user to get at arbitrary dir names
137 * in stack. PWP: let =foobar pass through (for X windows)
139 if (old[1] == '-' && (old[2] == '\0' || old[2] == '/')) {
144 else if (Isdigit(old[1])) {
147 for (b = &old[2]; Isdigit(*b); b++)
148 dig = dig * 10 + (*b - '0');
149 if (*b != '\0' && *b != '/')
150 /* =<number>foobar */
157 if (!getstakd(new, dig))
160 /* Copy the rest of the string */
161 for (d = &new[Strlen(new)];
162 d < &new[BUFSIZE - 1] && (*d++ = *b++) != '\0';)
174 Char *pm, *pe, *lm, *pl;
177 int size = GLOBSPACE;
179 nv = vl = (Char **) xmalloc((size_t) (sizeof(Char *) * size));
183 /* copy part up to the brace */
184 for (lm = gbuf, p = s; *p != LBRC; *lm++ = *p++)
187 /* check for balanced braces */
188 for (i = 0, pe = ++p; *pe; pe++)
190 if (Ismbyte1(*pe) && *(pe + 1) != EOS)
193 #endif /* DSPMBYTE */
195 /* Ignore everything between [] */
196 for (++pe; *pe != RBRK && *pe != EOS; pe++)
198 if (Ismbyte1(*pe) && *(pe + 1) != EOS)
201 #endif /* DSPMBYTE */
208 else if (*pe == LBRC)
210 else if (*pe == RBRC) {
216 if (i != 0 || *pe == '\0') {
221 for (i = 0, pl = pm = p; pm <= pe; pm++)
223 if (Ismbyte1(*pm) && pm + 1 <= pe)
226 #endif /* DSPMBYTE */
229 for (++pm; *pm != RBRK && *pm != EOS; pm++)
231 if (Ismbyte1(*pm) && *(pm + 1) != EOS)
234 #endif /* DSPMBYTE */
258 (void) Strcpy(lm, pl);
259 (void) Strcat(gbuf, pe + 1);
261 *vl++ = Strsave(gbuf);
264 if (vl == &nv[size]) {
266 nv = (Char **) xrealloc((ptr_t) nv,
267 (size_t) (size * sizeof(Char *)));
268 vl = &nv[size - GLOBSPACE];
282 expbrace(nvp, elp, size)
286 Char **vl, **el, **nv, *s;
292 for (el = vl; *el; el++)
295 for (s = *vl; s; s = *++vl) {
299 /* leave {} untouched for find */
300 if (s[0] == '{' && (s[1] == '\0' || (s[1] == '}' && s[2] == '\0')))
302 if ((b = Strchr(s, '{')) != NULL) {
306 #if defined (DSPMBYTE)
307 if (b != s && Ismbyte2(*b) && Ismbyte1(*(b-1))) {
308 /* The "{" is the 2nd byte of a MB character */
311 #endif /* DSPMBYTE */
312 if ((len = globbrace(s, b, &bl)) < 0) {
314 stderror(ERR_MISSING, -len);
322 if (&el[len] >= &nv[size]) {
324 l = (int) (&el[len] - &nv[size]);
325 size += GLOBSPACE > l ? GLOBSPACE : l;
328 nv = (Char **) xrealloc((ptr_t) nv,
329 (size_t) (size * sizeof(Char *)));
342 for (bp = el; bp != vp; bp--)
352 for (bp = bl + 1; *bp; *vp++ = *bp++)
368 Char **nv, **vl, **el;
369 int size = GLOBSPACE;
372 nv = vl = (Char **) xmalloc((size_t) (sizeof(Char *) * size));
376 * Step 1: expand backquotes.
378 while ((s = *v++) != '\0') {
379 if (Strchr(s, '`')) {
382 (void) dobackp(s, 0);
383 for (i = 0; i < pargc; i++) {
385 if (vl == &nv[size]) {
387 nv = (Char **) xrealloc((ptr_t) nv,
388 (size_t) (size * sizeof(Char *)));
389 vl = &nv[size - GLOBSPACE];
392 xfree((ptr_t) pargv);
397 if (vl == &nv[size]) {
399 nv = (Char **) xrealloc((ptr_t) nv,
400 (size_t) (size * sizeof(Char *)));
401 vl = &nv[size - GLOBSPACE];
411 * Step 2: expand braces
414 expbrace(&nv, &el, size);
421 for (s = *vl; s; s = *++vl)
423 Char gp[BUFSIZE], *ns;
425 *vl = globtilde(nv, s);
428 if ((ns = globequal(gp, s)) == NULL) {
429 if (!adrof(STRnonomatch)) {
436 /* Expansion succeeded */
447 * Step 4: expand .. if the variable symlinks==expand is set
449 if (symlinks == SYM_EXPAND) {
450 for (s = *vl; s; s = *++vl) {
451 *vl = dnormalize(s, 1);
461 handleone(str, vl, action)
472 setname(short2str(str));
474 stderror(ERR_NAME | ERR_AMBIG);
478 for (t = vlp; (p = *t++) != '\0'; chars++)
481 str = (Char *)xmalloc((size_t)(chars * sizeof(Char)));
482 for (t = vlp, strp = str; (p = *t++) != '\0'; chars++) {
484 *strp++ = *p++ & TRIM;
491 str = Strsave(strip(*vlp));
504 int gflgs = GLOB_QUOTE | GLOB_NOMAGIC | GLOB_ALTNOT;
507 int nonomatch = adrof(STRnonomatch) != 0, magic = 0, match = 0;
517 gflgs |= GLOB_NOCHECK;
520 ptr = short2qstr(*vl);
521 switch (glob(ptr, gflgs, 0, &globv)) {
525 stderror(ERR_NAME | ERR_GLOB);
534 if (globv.gl_flags & GLOB_MAGCHAR) {
535 match |= (globv.gl_matchc != 0);
538 gflgs |= GLOB_APPEND;
541 vl = (globv.gl_pathc == 0 || (magic && !match && !nonomatch)) ?
542 NULL : blk2short(globv.gl_pathv);
553 Char *v[2], **vl, **vo;
556 noglob = adrof(STRnoglob) != 0;
563 return (strip(Strsave(str)));
567 * Expand back-quote, tilde and brace
570 if (noglob || (gflg & G_GLOB) == 0) {
573 return (Strsave(STRNULL));
576 return (handleone(str, vo, action));
584 else if (noglob || (gflg & G_GLOB) == 0)
585 return (strip(Strsave(str)));
590 if ((gflg & G_CSH) && vl != vo)
593 setname(short2str(str));
594 stderror(ERR_NAME | ERR_NOMATCH);
598 return (Strsave(STRNULL));
601 return (handleone(str, vl, action));
618 gargc = blklen(gargv);
622 noglob = adrof(STRnoglob) != 0;
626 * Expand back-quote, tilde and brace
628 vl = vo = globexpand(v);
630 vl = vo = saveblk(v);
632 if (!noglob && (gflg & G_GLOB)) {
640 gargc = vl ? blklen(vl) : 0;
648 gargv = (Char **) xmalloc((size_t) (sizeof(Char *) * gargsiz));
656 void (*f) __P((int));
660 while ((p = *t++) != '\0')
671 while ((p = *t++) != '\0')
680 register Char *p, *c;
682 while ((p = *t++) != '\0') {
683 if (*p == '~' || *p == '=')
685 else if (*p == '{' &&
686 (p[1] == '\0' || (p[1] == '}' && p[2] == '\0')))
689 * The following line used to be *(c = p++), but hp broke their
690 * optimizer in 9.01, so we break the assignment into two pieces
691 * The careful reader here will note that *most* compiler workarounds
692 * in tcsh are either for apollo/DomainOS or hpux. Is it a coincidence?
694 while ( *(c = p) != '\0') {
700 * We do want to expand echo `echo '*'`, so we don't\
701 * use this piece of code anymore.
703 while (*p && *p != '`')
705 if (*p) /* Quoted chars */
710 if (*p) /* The matching ` */
720 else if (symlinks == SYM_EXPAND &&
721 *p && ISDOTDOT(c) && (c == *(t-1) || *(c-1) == '/') )
728 * Command substitute cp. If literal, then this is a substitution from a
729 * << redirection, and so we should not crunch blanks and tabs, separating
730 * words only at newlines.
737 register Char *lp, *rp;
738 Char *ep, word[LONGBSIZE];
747 pargv = (Char **) xmalloc((size_t) (sizeof(Char *) * pargsiz));
749 pargcp = pargs = word;
751 pnleft = LONGBSIZE - 4;
753 #if defined(DSPMBYTE)
754 for (lp = cp;; lp++) { /* } */
756 (lp-1 < cp || !Ismbyte2(*lp) || !Ismbyte1(*(lp-1)))) {
760 for (lp = cp; *lp != '`'; lp++) {
761 #endif /* DSPMBYTE */
770 for (rp = lp; *rp && *rp != '`'; rp++)
777 oops: stderror(ERR_UNMATCHED, '`');
780 backeval(ep, literal);
787 backeval(cp, literal)
791 register int icnt, c;
793 struct command faket;
796 Char *fakecom[2], ibuf[BUFSIZE];
801 quoted = (literal || (cp[0] & QUOTE)) ? QUOTE : 0;
802 faket.t_dtyp = NODE_COMMAND;
803 faket.t_dflg = F_BACKQ;
807 faket.t_dcom = fakecom;
808 fakecom[0] = STRfakecom1;
812 * We do the psave job to temporarily change the current job so that the
813 * following fork is considered a separate job. This is so that when
814 * backquotes are used in a builtin function that calls glob the "current
815 * job" is not corrupted. We only need one level of pushed jobs as long as
816 * we are sure to fork here.
821 * It would be nicer if we could integrate this redirection more with the
822 * routines in sh.sem.c by doing a fake execute on a builtin function that
826 if (pfork(&faket, -1) == 0) {
829 (void) close(pvec[0]);
830 (void) dmove(pvec[1], 1);
831 (void) dmove(SHDIAG, 2);
835 * Bugfix for nested backquotes by Michael Greim <greim@sbsvax.UUCP>,
836 * posted to comp.bugs.4bsd 12 Sep. 1989.
838 if (pargv) /* mg, 21.dec.88 */
839 blkfree(pargv), pargv = 0, pargsiz = 0;
842 for (arginp = cp; *cp; cp++) {
844 if (*cp == '\n' || *cp == '\r')
849 * In the child ``forget'' everything about current aliases or
860 t = syntax(paraml.next, ¶ml, 0);
864 t->t_dflg |= F_NOFORK;
866 (void) sigignore(SIGTSTP);
869 (void) sigignore(SIGTTIN);
872 (void) sigignore(SIGTTOU);
874 execute(t, -1, NULL, NULL, TRUE);
878 (void) close(pvec[1]);
890 icnt = read(pvec[0], tibuf, BUFSIZE);
891 while (icnt == -1 && errno == EINTR);
896 for (i = 0; i < icnt; i++)
897 ip[i] = (unsigned char) tibuf[i];
908 #endif /* WINNT_NATIVE */
911 * Continue around the loop one more time, so that we can eat
912 * the last newline without terminating this word.
917 if (!quoted && (c == ' ' || c == '\t'))
923 * Unless at end-of-file, we will form a new word here if there were
924 * characters in the word, or in any case when we take text literally.
925 * If we didn't make empty words here when literal was set then we
926 * would lose blank lines.
928 if (c != -1 && (cnt || literal))
932 (void) close(pvec[0]);
942 stderror(ERR_WTOOLONG);
943 *pargcp++ = (Char) c;
951 if (pargc == pargsiz - 1) {
952 pargsiz += GLOBSPACE;
953 pargv = (Char **) xrealloc((ptr_t) pargv,
954 (size_t) (pargsiz * sizeof(Char *)));
956 pargv[pargc++] = Strsave(pargs);
963 Gmatch(string, pattern)
964 Char *string, *pattern;
966 return Gnmatch(string, pattern, NULL);
970 Gnmatch(string, pattern, endstr)
971 Char *string, *pattern, **endstr;
973 Char **blk, **p, *tstring = string;
974 int gpol = 1, gres = 0;
976 if (*pattern == '^') {
981 blk = (Char **) xmalloc((size_t) (GLOBSPACE * sizeof(Char *)));
982 blk[0] = Strsave(pattern);
985 expbrace(&blk, NULL, GLOBSPACE);
988 /* Exact matches only */
989 for (p = blk; *p; p++)
990 gres |= t_pmatch(string, *p, &tstring, 0) == 2 ? 1 : 0;
992 /* partial matches */
993 int minc = 0x7fffffff;
994 for (p = blk; *p; p++)
995 if (t_pmatch(string, *p, &tstring, 0) != 0) {
996 int t = (int) (tstring - string);
998 if (minc == -1 || minc > t)
1001 *endstr = string + minc;
1005 return(gres == gpol);
1009 * Return 2 on exact match,
1010 * Return 1 on substring match.
1011 * Return 0 on no match.
1012 * *estr will point to the end of the longest exact or substring match.
1015 t_pmatch(string, pattern, estr, cs)
1016 Char *string, *pattern, **estr;
1019 Char stringc, patternc;
1020 int match, negate_range;
1021 Char rangec, *oestr, *pestr;
1024 stringc = *string & TRIM;
1026 * apollo compiler bug: switch (patternc = *pattern++) dies
1028 patternc = *pattern++;
1032 return (stringc == 0 ? 2 : 1);
1040 while (*string) string++;
1048 switch(t_pmatch(string, pattern, estr, cs)) {
1057 abort(); /* Cannot happen */
1074 if ((negate_range = (*pattern == '^')) != 0)
1076 while ((rangec = *pattern++) != '\0') {
1081 if (rangec == '-' && *(pattern-2) != '[' && *pattern != ']') {
1082 match = (globcharcoll(stringc, *pattern & TRIM, cs) <= 0 &&
1083 globcharcoll(*(pattern-2) & TRIM, stringc, cs) <= 0);
1087 match = (stringc == (rangec & TRIM));
1090 stderror(ERR_NAME | ERR_MISSING, ']');
1091 if (match == negate_range)
1096 if ((patternc & TRIM) != stringc)
1108 register Char *p, *q;
1115 n = (int) ((p - s1) + (q - s2) - 1);
1116 if (++gargc >= gargsiz) {
1117 gargsiz += GLOBSPACE;
1118 gargv = (Char **) xrealloc((ptr_t) gargv,
1119 (size_t) (gargsiz * sizeof(Char *)));
1122 p = gargv[gargc - 1] = (Char *) xmalloc((size_t) (n * sizeof(Char)));
1123 for (q = s1; (*p++ = *q++) != '\0';)
1125 for (p--, q = s2; (*p++ = *q++) != '\0';)
1129 #if defined(FILEC) && defined(TIOCSTI)
1132 register Char **a, **b;
1134 if (!a) /* check for NULL */
1139 if (!*a) /* check for NULL */
1140 return (*b ? 1 : 0);
1144 return (int) collate(*a, *b);