123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- /*
- * This file is part of the UCB release of Plan 9. It is subject to the license
- * terms in the LICENSE file found in the top-level directory of this
- * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
- * part of the UCB release of Plan 9, including this file, may be copied,
- * modified, propagated, or distributed except according to the terms contained
- * in the LICENSE file.
- */
- #include "stdinc.h"
- #include "vac.h"
- #include "dat.h"
- #include "fns.h"
- #include "error.h"
- // Convert globbish pattern to regular expression
- // The wildcards are
- //
- // * any non-slash characters
- // ... any characters including /
- // ? any single character except /
- // [a-z] character class
- // [~a-z] negated character class
- //
- Reprog*
- glob2regexp(char *glob)
- {
- char *s, *p, *w;
- Reprog *re;
- int boe; // beginning of path element
- s = malloc(20*(strlen(glob)+1));
- if(s == nil)
- return nil;
- w = s;
- boe = 1;
- *w++ = '^';
- *w++ = '(';
- for(p=glob; *p; p++){
- if(p[0] == '.' && p[1] == '.' && p[2] == '.'){
- strcpy(w, ".*");
- w += strlen(w);
- p += 3-1;
- boe = 0;
- continue;
- }
- if(p[0] == '*'){
- if(boe)
- strcpy(w, "([^./][^/]*)?");
- else
- strcpy(w, "[^/]*");
- w += strlen(w);
- boe = 0;
- continue;
- }
- if(p[0] == '?'){
- if(boe)
- strcpy(w, "[^./]");
- else
- strcpy(w, "[^/]");
- w += strlen(w);
- boe = 0;
- continue;
- }
- if(p[0] == '['){
- *w++ = '[';
- if(*++p == '~'){
- *w++ = '^';
- p++;
- }
- while(*p != ']'){
- if(*p == '/')
- goto syntax;
- if(*p == '^' || *p == '\\')
- *w++ = '\\';
- *w++ = *p++;
- }
- *w++ = ']';
- boe = 0;
- continue;
- }
- if(strchr("()|^$[]*?+\\.", *p)){
- *w++ = '\\';
- *w++ = *p;
- boe = 0;
- continue;
- }
- if(*p == '/'){
- *w++ = '/';
- boe = 1;
- continue;
- }
- *w++ = *p;
- boe = 0;
- continue;
- }
- *w++ = ')';
- *w++ = '$';
- *w = 0;
-
- re = regcomp(s);
- if(re == nil){
- syntax:
- free(s);
- werrstr("glob syntax error");
- return nil;
- }
- free(s);
- return re;
- }
- typedef struct Pattern Pattern;
- struct Pattern
- {
- Reprog *re;
- int include;
- };
- Pattern *pattern;
- int npattern;
- void
- loadexcludefile(char *file)
- {
- Biobuf *b;
- char *p, *q;
- int n, inc;
- Reprog *re;
- if((b = Bopen(file, OREAD)) == nil)
- sysfatal("open %s: %r", file);
- for(n=1; (p=Brdstr(b, '\n', 1)) != nil; free(p), n++){
- q = p+strlen(p);
- while(q > p && isspace((uchar)*(q-1)))
- *--q = 0;
- switch(p[0]){
- case '\0':
- case '#':
- continue;
- }
-
- inc = 0;
- if(strncmp(p, "include ", 8) == 0){
- inc = 1;
- }else if(strncmp(p, "exclude ", 8) == 0){
- inc = 0;
- }else
- sysfatal("%s:%d: line does not begin with include or exclude", file, n);
- if(strchr(p+8, ' '))
- fprint(2, "%s:%d: warning: space in pattern\n", file, n);
- if((re = glob2regexp(p+8)) == nil)
- sysfatal("%s:%d: bad glob pattern", file, n);
- pattern = vtrealloc(pattern, (npattern+1)*sizeof pattern[0]);
- pattern[npattern].re = re;
- pattern[npattern].include = inc;
- npattern++;
- }
- Bterm(b);
- }
- void
- excludepattern(char *p)
- {
- Reprog *re;
-
- if((re = glob2regexp(p)) == nil)
- sysfatal("bad glob pattern %s", p);
- pattern = vtrealloc(pattern, (npattern+1)*sizeof pattern[0]);
- pattern[npattern].re = re;
- pattern[npattern].include = 0;
- npattern++;
- }
- int
- includefile(char *file)
- {
- Pattern *p, *ep;
-
- for(p=pattern, ep=p+npattern; p<ep; p++)
- if(regexec(p->re, file, nil, 0))
- return p->include;
- return 1;
- }
|