|
|
1.1 root 1: #include <setjmp.h>
2: #include <stdio.h>
3: #include <ctype.h>
4: #include <libc.h>
5: #include "mgr.h"
6:
7: /*
8: * Compressed regular expressions. A single lists represents all regular
9: * expressions. Whenever a new one is created, the list is searched for an
10: * equivalent re. If one is found, it is returned and its reference count
11: * incremented. If the regular expression is a simple string, it is not
12: * compiled and strcmp is used to check for equivalence.
13: */
14:
15: Cregexp *relist; /* the list */
16:
17: /*
18: * trap errors from recomp and regexec
19: */
20: jmp_buf rxerr;
21:
22: regerror(s)
23: char *s;
24: {
25: logevent("regex error: %s\n", s);
26: longjmp(rxerr, 1);
27: }
28:
29: /*
30: * compile a regular expression inserting a ^ at the beginning and
31: * a $ at the end.
32: */
33: Cregexp *
34: newre(re)
35: char *re;
36: {
37: char fullre[ARB];
38: register Cregexp *rp;
39: int i;
40:
41: /*
42: * look for a previous version of the same
43: */
44: for(rp=relist; rp; rp=rp->next)
45: if(strcmp(rp->re, re)==0){
46: rp->ref++;
47: return(rp);
48: }
49:
50: /*
51: * alloc a new one
52: */
53: rp = (Cregexp *)malloc(sizeof(Cregexp));
54: if(rp == NULL)
55: return((Cregexp *)0);
56: rp->re = strdup(re);
57: rp->ref = 1;
58: rp->prog = NULL;
59: if(setjmp(rxerr)){
60: freere(rp);
61: return((Cregexp *)0);
62: }
63:
64: /*
65: * add the ^$
66: */
67: if(*re != '^')
68: strcpy(fullre, "^");
69: else
70: fullre[0] = 0;
71: strcat(fullre, re);
72: i = strlen(fullre);
73: if(fullre[i-1] != '$')
74: strcat(fullre, "$");
75:
76: /*
77: * compile the re
78: */
79: rp->prog = regcomp(fullre);
80: if(rp->prog == NULL){
81: freere(rp);
82: return((Cregexp *)0);
83: }
84:
85: /*
86: * thread into list
87: */
88: rp->next = relist;
89: relist = rp;
90: return(rp);
91: }
92:
93: /*
94: * free a regular expression
95: */
96: freere(rp)
97: Cregexp *rp;
98: {
99: /*
100: * This code is left if the need arises. Normally, it is cheaper
101: * in space to not free re's since most are eventually reused.
102: Cregexp *trp;
103:
104: if(--(rp->ref) > 0)
105: return;
106: free(rp->re);
107: if(rp->prog != NULL)
108: free((char *)(rp->prog));
109: if(relist == rp)
110: relist = rp->next;
111: else {
112: for(trp=relist; trp->next!=rp; trp=trp->next)
113: ;
114: trp->next = rp->next;
115: }
116: free((char *)rp);
117: */
118: }
119:
120: /*
121: * execute a regular expression
122: */
123: execre(rp, string, sub, nsub)
124: Cregexp *rp;
125: char *string;
126: regsubexp *sub; /* subexpression elements */
127: int nsub; /* number of elements pointed to by sub */
128: {
129: int rv;
130:
131: if(rp->prog==NULL){
132: rv = strcmp(string, rp->re)==0;
133: if(rv && sub && nsub>1){
134: sub[0].sp = string;
135: sub[0].ep = string+strlen(string);
136: }
137: return(rv);
138: } else
139: return(regexec(rp->prog, string, sub, nsub));
140: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.