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"
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
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)
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;
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;
30 b2cfc4e2 2003-09-30 devnull match = 0;
31 6d08a0f5 2007-12-07 rsc checkstart = j->startchar;
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;
37 b2cfc4e2 2003-09-30 devnull j->relist[0][0].inst = 0;
38 b2cfc4e2 2003-09-30 devnull j->relist[1][0].inst = 0;
40 b2cfc4e2 2003-09-30 devnull /* Execute machine once for each character, including terminal NUL */
41 b2cfc4e2 2003-09-30 devnull s = j->rstarts;
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;
53 b2cfc4e2 2003-09-30 devnull case BOL:
54 b2cfc4e2 2003-09-30 devnull if(s == bol)
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;
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;
73 b2cfc4e2 2003-09-30 devnull /* Add first instruction to current list */
74 6d08a0f5 2007-12-07 rsc _rrenewemptythread(tl, progp->startinst, ms, s);
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;
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;
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;
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;
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;
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;
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])
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;
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)
130 6d08a0f5 2007-12-07 rsc /* efficiency: advance and re-evaluate */
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);
142 b2cfc4e2 2003-09-30 devnull if(s == j->reol)
144 6d08a0f5 2007-12-07 rsc checkstart = j->startchar && nl->inst==0;
146 b2cfc4e2 2003-09-30 devnull }while(r);
147 b2cfc4e2 2003-09-30 devnull return match;
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
158 6d08a0f5 2007-12-07 rsc Relist relist0[5*LISTSIZE], relist1[5*LISTSIZE];
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;
166 6d08a0f5 2007-12-07 rsc return rregexec1(progp, bol, mp, ms, j);
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 */
175 b2cfc4e2 2003-09-30 devnull Reljunk j;
176 b2cfc4e2 2003-09-30 devnull Relist relist0[LISTSIZE], relist1[LISTSIZE];
180 b2cfc4e2 2003-09-30 devnull * use user-specified starting/ending location if specified
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;
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;
196 b2cfc4e2 2003-09-30 devnull if(progp->startinst->type == BOL)
197 b2cfc4e2 2003-09-30 devnull j.starttype = BOL;
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;
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;