Blame


1 b2cfc4e2 2003-09-30 devnull #include "lib9.h"
2 b2cfc4e2 2003-09-30 devnull #include "regexp9.h"
3 b2cfc4e2 2003-09-30 devnull #include "regcomp.h"
4 b2cfc4e2 2003-09-30 devnull
5 b2cfc4e2 2003-09-30 devnull /*
6 b2cfc4e2 2003-09-30 devnull * return 0 if no match
7 b2cfc4e2 2003-09-30 devnull * >0 if a match
8 b2cfc4e2 2003-09-30 devnull * <0 if we ran out of _relist space
9 b2cfc4e2 2003-09-30 devnull */
10 b2cfc4e2 2003-09-30 devnull static int
11 b2cfc4e2 2003-09-30 devnull rregexec1(Reprog *progp, /* program to run */
12 6d08a0f5 2007-12-07 rsc Rune *bol, /* string to run machine on */
13 6d08a0f5 2007-12-07 rsc Resub *mp, /* subexpression elements */
14 6d08a0f5 2007-12-07 rsc int ms, /* number of elements at mp */
15 b2cfc4e2 2003-09-30 devnull Reljunk *j)
16 b2cfc4e2 2003-09-30 devnull {
17 b2cfc4e2 2003-09-30 devnull int flag=0;
18 b2cfc4e2 2003-09-30 devnull Reinst *inst;
19 b2cfc4e2 2003-09-30 devnull Relist *tlp;
20 b2cfc4e2 2003-09-30 devnull Rune *s;
21 b2cfc4e2 2003-09-30 devnull int i, checkstart;
22 b2cfc4e2 2003-09-30 devnull Rune r, *rp, *ep;
23 b2cfc4e2 2003-09-30 devnull Relist* tl; /* This list, next list */
24 b2cfc4e2 2003-09-30 devnull Relist* nl;
25 b2cfc4e2 2003-09-30 devnull Relist* tle; /* ends of this and next list */
26 b2cfc4e2 2003-09-30 devnull Relist* nle;
27 b2cfc4e2 2003-09-30 devnull int match;
28 da7f7882 2007-05-18 devnull Rune *p;
29 b2cfc4e2 2003-09-30 devnull
30 b2cfc4e2 2003-09-30 devnull match = 0;
31 6d08a0f5 2007-12-07 rsc checkstart = j->startchar;
32 b2cfc4e2 2003-09-30 devnull if(mp)
33 b2cfc4e2 2003-09-30 devnull for(i=0; i<ms; i++) {
34 b2cfc4e2 2003-09-30 devnull mp[i].s.rsp = 0;
35 b2cfc4e2 2003-09-30 devnull mp[i].e.rep = 0;
36 b2cfc4e2 2003-09-30 devnull }
37 b2cfc4e2 2003-09-30 devnull j->relist[0][0].inst = 0;
38 b2cfc4e2 2003-09-30 devnull j->relist[1][0].inst = 0;
39 b2cfc4e2 2003-09-30 devnull
40 b2cfc4e2 2003-09-30 devnull /* Execute machine once for each character, including terminal NUL */
41 b2cfc4e2 2003-09-30 devnull s = j->rstarts;
42 b2cfc4e2 2003-09-30 devnull do{
43 b2cfc4e2 2003-09-30 devnull
44 b2cfc4e2 2003-09-30 devnull /* fast check for first char */
45 b2cfc4e2 2003-09-30 devnull if(checkstart) {
46 b2cfc4e2 2003-09-30 devnull switch(j->starttype) {
47 b2cfc4e2 2003-09-30 devnull case RUNE:
48 da7f7882 2007-05-18 devnull p = runestrchr(s, j->startchar);
49 6d08a0f5 2007-12-07 rsc if(p == 0 || p == j->reol)
50 da7f7882 2007-05-18 devnull return match;
51 da7f7882 2007-05-18 devnull s = p;
52 b2cfc4e2 2003-09-30 devnull break;
53 b2cfc4e2 2003-09-30 devnull case BOL:
54 b2cfc4e2 2003-09-30 devnull if(s == bol)
55 b2cfc4e2 2003-09-30 devnull break;
56 da7f7882 2007-05-18 devnull p = runestrchr(s, '\n');
57 6d08a0f5 2007-12-07 rsc if(p == 0 || s == j->reol)
58 da7f7882 2007-05-18 devnull return match;
59 da7f7882 2007-05-18 devnull s = p+1;
60 b2cfc4e2 2003-09-30 devnull break;
61 b2cfc4e2 2003-09-30 devnull }
62 b2cfc4e2 2003-09-30 devnull }
63 b2cfc4e2 2003-09-30 devnull
64 b2cfc4e2 2003-09-30 devnull r = *s;
65 b2cfc4e2 2003-09-30 devnull
66 b2cfc4e2 2003-09-30 devnull /* switch run lists */
67 b2cfc4e2 2003-09-30 devnull tl = j->relist[flag];
68 b2cfc4e2 2003-09-30 devnull tle = j->reliste[flag];
69 b2cfc4e2 2003-09-30 devnull nl = j->relist[flag^=1];
70 b2cfc4e2 2003-09-30 devnull nle = j->reliste[flag];
71 b2cfc4e2 2003-09-30 devnull nl->inst = 0;
72 b2cfc4e2 2003-09-30 devnull
73 b2cfc4e2 2003-09-30 devnull /* Add first instruction to current list */
74 6d08a0f5 2007-12-07 rsc _rrenewemptythread(tl, progp->startinst, ms, s);
75 b2cfc4e2 2003-09-30 devnull
76 b2cfc4e2 2003-09-30 devnull /* Execute machine until current list is empty */
77 b2cfc4e2 2003-09-30 devnull for(tlp=tl; tlp->inst; tlp++){
78 6d08a0f5 2007-12-07 rsc for(inst=tlp->inst; ; inst = inst->u2.next){
79 b2cfc4e2 2003-09-30 devnull switch(inst->type){
80 b2cfc4e2 2003-09-30 devnull case RUNE: /* regular character */
81 b2cfc4e2 2003-09-30 devnull if(inst->u1.r == r)
82 6d08a0f5 2007-12-07 rsc if(_renewthread(nl, inst->u2.next, ms, &tlp->se)==nle)
83 b2cfc4e2 2003-09-30 devnull return -1;
84 b2cfc4e2 2003-09-30 devnull break;
85 b2cfc4e2 2003-09-30 devnull case LBRA:
86 b2cfc4e2 2003-09-30 devnull tlp->se.m[inst->u1.subid].s.rsp = s;
87 b2cfc4e2 2003-09-30 devnull continue;
88 b2cfc4e2 2003-09-30 devnull case RBRA:
89 b2cfc4e2 2003-09-30 devnull tlp->se.m[inst->u1.subid].e.rep = s;
90 b2cfc4e2 2003-09-30 devnull continue;
91 b2cfc4e2 2003-09-30 devnull case ANY:
92 b2cfc4e2 2003-09-30 devnull if(r != '\n')
93 6d08a0f5 2007-12-07 rsc if(_renewthread(nl, inst->u2.next, ms, &tlp->se)==nle)
94 b2cfc4e2 2003-09-30 devnull return -1;
95 b2cfc4e2 2003-09-30 devnull break;
96 b2cfc4e2 2003-09-30 devnull case ANYNL:
97 6d08a0f5 2007-12-07 rsc if(_renewthread(nl, inst->u2.next, ms, &tlp->se)==nle)
98 b2cfc4e2 2003-09-30 devnull return -1;
99 b2cfc4e2 2003-09-30 devnull break;
100 b2cfc4e2 2003-09-30 devnull case BOL:
101 b2cfc4e2 2003-09-30 devnull if(s == bol || *(s-1) == '\n')
102 b2cfc4e2 2003-09-30 devnull continue;
103 b2cfc4e2 2003-09-30 devnull break;
104 b2cfc4e2 2003-09-30 devnull case EOL:
105 b2cfc4e2 2003-09-30 devnull if(s == j->reol || r == 0 || r == '\n')
106 b2cfc4e2 2003-09-30 devnull continue;
107 b2cfc4e2 2003-09-30 devnull break;
108 b2cfc4e2 2003-09-30 devnull case CCLASS:
109 b2cfc4e2 2003-09-30 devnull ep = inst->u1.cp->end;
110 b2cfc4e2 2003-09-30 devnull for(rp = inst->u1.cp->spans; rp < ep; rp += 2)
111 b2cfc4e2 2003-09-30 devnull if(r >= rp[0] && r <= rp[1]){
112 6d08a0f5 2007-12-07 rsc if(_renewthread(nl, inst->u2.next, ms, &tlp->se)==nle)
113 b2cfc4e2 2003-09-30 devnull return -1;
114 b2cfc4e2 2003-09-30 devnull break;
115 b2cfc4e2 2003-09-30 devnull }
116 b2cfc4e2 2003-09-30 devnull break;
117 b2cfc4e2 2003-09-30 devnull case NCCLASS:
118 b2cfc4e2 2003-09-30 devnull ep = inst->u1.cp->end;
119 b2cfc4e2 2003-09-30 devnull for(rp = inst->u1.cp->spans; rp < ep; rp += 2)
120 b2cfc4e2 2003-09-30 devnull if(r >= rp[0] && r <= rp[1])
121 b2cfc4e2 2003-09-30 devnull break;
122 b2cfc4e2 2003-09-30 devnull if(rp == ep)
123 6d08a0f5 2007-12-07 rsc if(_renewthread(nl, inst->u2.next, ms, &tlp->se)==nle)
124 b2cfc4e2 2003-09-30 devnull return -1;
125 b2cfc4e2 2003-09-30 devnull break;
126 b2cfc4e2 2003-09-30 devnull case OR:
127 6d08a0f5 2007-12-07 rsc /* evaluate right choice later */
128 27589754 2008-01-10 rsc if(_renewthread(tlp, inst->u1.right, ms, &tlp->se) == tle)
129 6d08a0f5 2007-12-07 rsc return -1;
130 6d08a0f5 2007-12-07 rsc /* efficiency: advance and re-evaluate */
131 6d08a0f5 2007-12-07 rsc continue;
132 b2cfc4e2 2003-09-30 devnull case END: /* Match! */
133 b2cfc4e2 2003-09-30 devnull match = 1;
134 b2cfc4e2 2003-09-30 devnull tlp->se.m[0].e.rep = s;
135 b2cfc4e2 2003-09-30 devnull if(mp != 0)
136 b2cfc4e2 2003-09-30 devnull _renewmatch(mp, ms, &tlp->se);
137 b2cfc4e2 2003-09-30 devnull break;
138 b2cfc4e2 2003-09-30 devnull }
139 b2cfc4e2 2003-09-30 devnull break;
140 b2cfc4e2 2003-09-30 devnull }
141 b2cfc4e2 2003-09-30 devnull }
142 b2cfc4e2 2003-09-30 devnull if(s == j->reol)
143 b2cfc4e2 2003-09-30 devnull break;
144 6d08a0f5 2007-12-07 rsc checkstart = j->startchar && nl->inst==0;
145 b2cfc4e2 2003-09-30 devnull s++;
146 b2cfc4e2 2003-09-30 devnull }while(r);
147 b2cfc4e2 2003-09-30 devnull return match;
148 b2cfc4e2 2003-09-30 devnull }
149 b2cfc4e2 2003-09-30 devnull
150 b2cfc4e2 2003-09-30 devnull static int
151 b2cfc4e2 2003-09-30 devnull rregexec2(Reprog *progp, /* program to run */
152 b2cfc4e2 2003-09-30 devnull Rune *bol, /* string to run machine on */
153 b2cfc4e2 2003-09-30 devnull Resub *mp, /* subexpression elements */
154 b2cfc4e2 2003-09-30 devnull int ms, /* number of elements at mp */
155 b2cfc4e2 2003-09-30 devnull Reljunk *j
156 b2cfc4e2 2003-09-30 devnull )
157 b2cfc4e2 2003-09-30 devnull {
158 6d08a0f5 2007-12-07 rsc Relist relist0[5*LISTSIZE], relist1[5*LISTSIZE];
159 b2cfc4e2 2003-09-30 devnull
160 6d08a0f5 2007-12-07 rsc /* mark space */
161 b2cfc4e2 2003-09-30 devnull j->relist[0] = relist0;
162 b2cfc4e2 2003-09-30 devnull j->relist[1] = relist1;
163 6d08a0f5 2007-12-07 rsc j->reliste[0] = relist0 + nelem(relist0) - 2;
164 6d08a0f5 2007-12-07 rsc j->reliste[1] = relist1 + nelem(relist1) - 2;
165 b2cfc4e2 2003-09-30 devnull
166 6d08a0f5 2007-12-07 rsc return rregexec1(progp, bol, mp, ms, j);
167 b2cfc4e2 2003-09-30 devnull }
168 b2cfc4e2 2003-09-30 devnull
169 b2cfc4e2 2003-09-30 devnull extern int
170 b2cfc4e2 2003-09-30 devnull rregexec(Reprog *progp, /* program to run */
171 b2cfc4e2 2003-09-30 devnull Rune *bol, /* string to run machine on */
172 b2cfc4e2 2003-09-30 devnull Resub *mp, /* subexpression elements */
173 b2cfc4e2 2003-09-30 devnull int ms) /* number of elements at mp */
174 b2cfc4e2 2003-09-30 devnull {
175 b2cfc4e2 2003-09-30 devnull Reljunk j;
176 b2cfc4e2 2003-09-30 devnull Relist relist0[LISTSIZE], relist1[LISTSIZE];
177 b2cfc4e2 2003-09-30 devnull int rv;
178 b2cfc4e2 2003-09-30 devnull
179 b2cfc4e2 2003-09-30 devnull /*
180 b2cfc4e2 2003-09-30 devnull * use user-specified starting/ending location if specified
181 b2cfc4e2 2003-09-30 devnull */
182 b2cfc4e2 2003-09-30 devnull j.rstarts = bol;
183 b2cfc4e2 2003-09-30 devnull j.reol = 0;
184 b2cfc4e2 2003-09-30 devnull if(mp && ms>0){
185 b2cfc4e2 2003-09-30 devnull if(mp->s.sp)
186 b2cfc4e2 2003-09-30 devnull j.rstarts = mp->s.rsp;
187 b2cfc4e2 2003-09-30 devnull if(mp->e.ep)
188 b2cfc4e2 2003-09-30 devnull j.reol = mp->e.rep;
189 b2cfc4e2 2003-09-30 devnull }
190 b2cfc4e2 2003-09-30 devnull j.starttype = 0;
191 b2cfc4e2 2003-09-30 devnull j.startchar = 0;
192 62390091 2004-03-05 devnull if(progp->startinst->type == RUNE && progp->startinst->u1.r < Runeself) {
193 b2cfc4e2 2003-09-30 devnull j.starttype = RUNE;
194 b2cfc4e2 2003-09-30 devnull j.startchar = progp->startinst->u1.r;
195 b2cfc4e2 2003-09-30 devnull }
196 b2cfc4e2 2003-09-30 devnull if(progp->startinst->type == BOL)
197 b2cfc4e2 2003-09-30 devnull j.starttype = BOL;
198 b2cfc4e2 2003-09-30 devnull
199 b2cfc4e2 2003-09-30 devnull /* mark space */
200 b2cfc4e2 2003-09-30 devnull j.relist[0] = relist0;
201 b2cfc4e2 2003-09-30 devnull j.relist[1] = relist1;
202 6d08a0f5 2007-12-07 rsc j.reliste[0] = relist0 + nelem(relist0) - 2;
203 6d08a0f5 2007-12-07 rsc j.reliste[1] = relist1 + nelem(relist1) - 2;
204 b2cfc4e2 2003-09-30 devnull
205 b2cfc4e2 2003-09-30 devnull rv = rregexec1(progp, bol, mp, ms, &j);
206 b2cfc4e2 2003-09-30 devnull if(rv >= 0)
207 b2cfc4e2 2003-09-30 devnull return rv;
208 b2cfc4e2 2003-09-30 devnull rv = rregexec2(progp, bol, mp, ms, &j);
209 b2cfc4e2 2003-09-30 devnull if(rv >= 0)
210 b2cfc4e2 2003-09-30 devnull return rv;
211 b2cfc4e2 2003-09-30 devnull return -1;
212 b2cfc4e2 2003-09-30 devnull }