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