|
|
1.1 root 1: #include "hdr.h"
2: #include "y.tab.h"
3:
4: cgotofn() {
5: register i;
6: count = cntpos = 0;
7: nxtpos = 0;
8: nxtfoll = MAXPOS-1;
9: begout = 0;
10: for (i=1; i<=line; i++) colpos[i] = 0;
11: if (first(line-1)==0) {
12: colpos[line] = 1;
13: cntpos++;
14: begout = 1;
15: }
16: for (i=1; i<=line; i++) tmpstat[i] = begstat[i] = colpos[i];
17: count = begcnt = cntpos-1; /* leave out position 1 */
18: tmpstat[1] = begstat[1] = 0;
19: addstate(1);
20: inxtpos = nxtpos;
21: istat = nxtst(states+1,LEFT);
22: }
23:
24: State *
25: nxtst(ss,c)
26: State *ss;
27: {
28: register i, num, k;
29: int pos, curpos, number, newpos;
30: int s = ss-states;
31: #ifdef DOSTATS
32: ntrans++;
33: #endif
34: num = positions[state[s]];
35: count = begcnt;
36: for (i=3; i<=line; i++) tmpstat[i] = begstat[i];
37: pos = state[s] + 1;
38: for (i=0; i<num; i++) {
39: curpos = positions[pos];
40: if ((k = name[curpos]) >= 0) {
41: if (
42: (k == c)
43: | (k == DOT && c != LEFT && c != RIGHT)
44: | (k == CCL && member(c, right[curpos], 1))
45: | (k == NCCL && member(c, right[curpos], 0) && c != LEFT && c != RIGHT)
46: ) {
47: if (foll[curpos] == 0) {
48: cntpos = 0;
49: for (k=1; k<=line; k++) colpos[k] = 0;
50: #ifdef DOSTATS
51: nfollow++;
52: #endif
53: follow(curpos);
54: addfoll(curpos);
55: }
56: number = positions[foll[curpos]];
57: newpos = foll[curpos] - 1;
58: for (k=0; k<number; k++) {
59: if (tmpstat[positions[newpos]] != 1) {
60: tmpstat[positions[newpos]] = 1;
61: count++;
62: }
63: newpos--;
64: }
65: }
66: }
67: pos++;
68: }
69: if (notin(nstate)) {
70: if (++nstate >= NSTATES) {
71: nxtpos = inxtpos;
72: reinit = 1;
73: nstate = 1;
74: addstate(1);
75: return states+1;
76: }
77: addstate(nstate);
78: return(states[s].gotofn[c] = states+nstate);
79: }
80: else {
81: return(states[s].gotofn[c] = states+xstate);
82: }
83: }
84:
85: first(v) {
86: register b;
87: if (left[v] == 0) {
88: if (colpos[v] != 1) {
89: colpos[v] = 1;
90: cntpos++;
91: }
92: return(1);
93: }
94: else if (right[v] == 0) {
95: if (first(left[v]) == 0) return (0);
96: else if (name[v] == PLUS) return (1);
97: else return (0);
98: }
99: else if (name[v] == CAT) {
100: if (first(left[v]) == 0 && first(right[v]) == 0) return (0);
101: else return (1);
102: }
103: else { /* name[v] == OR */
104: b = first(right[v]);
105: if (first(left[v]) == 0 || b == 0) return (0);
106: else return (1);
107: }
108: }
109:
110: member(symb, set, torf) {
111: register i, num, pos;
112: num = chars[set];
113: pos = set + 1;
114: for (i=0; i<num; i++)
115: if (symb == chars[pos++]) return (torf);
116: return (!torf);
117: }
118:
119: notin(n) {
120: register i, j, pos;
121: for (i=1; i<=n; i++) {
122: if (positions[state[i]] == count) {
123: pos = state[i] + 1;
124: for (j=0; j < count; j++) {
125: if (tmpstat[positions[pos++]] != 1) goto nxt; }
126: xstate = i;
127: return (0);
128: }
129: nxt: ;
130: }
131: return (1);
132: }
133:
134: addstate(n) {
135: register i;
136: if (nxtpos + count >= nxtfoll) {
137: overflo(); }
138: for (i=0; i<NCHARS; i++)
139: states[n].gotofn[i] = 0;
140: state[n] = nxtpos;
141: positions[nxtpos++] = count;
142: for (i=3; i <= line; i++) {
143: if (tmpstat[i] == 1) {
144: positions[nxtpos++] = i;
145: }
146: }
147: if (tmpstat[line] == 1)
148: states[n].out = 1;
149: else
150: states[n].out = 0;
151: }
152:
153: addfoll(n) {
154: register i;
155: if (nxtfoll - cntpos <= nxtpos) {
156: overflo(); }
157: foll[n] = nxtfoll;
158: #ifdef DOSTATS
159: if(nxtfoll > nmaxfoll) nmaxfoll = nxtfoll;
160: #endif
161: positions[nxtfoll--] = cntpos;
162: for (i=3; i <= line; i++) {
163: if (colpos[i] == 1) {
164: positions[nxtfoll--] = i;
165: }
166: }
167: }
168:
169: follow(v) int v; {
170: int p;
171: if (v == line) return;
172: p = parent[v];
173: switch(name[p]) {
174: case STAR:
175: case PLUS: first(v);
176: follow(p);
177: return;
178:
179: case OR:
180: case QUEST: follow(p);
181: return;
182:
183: case CAT: if (v == left[p]) {
184: if (first(right[p]) == 0) {
185: follow(p);
186: return;
187: }
188: }
189: else follow(p);
190: return;
191: case FINAL: if (colpos[line] != 1) {
192: colpos[line] = 1;
193: cntpos++;
194: }
195: return;
196: }
197: }
198:
199: clearg() {
200: register i;
201: reinit = 0;
202: states[1].out = begout;
203: for (i=0; i<NCHARS; i++)
204: states[1].gotofn[i] = 0;
205: nstate = 1;
206: state[1] = 0;
207: istat = nxtst(states+1,LEFT);
208: }
209:
210: execute(file)
211: char *file;
212: {
213: register char *p;
214: register State *cstat, *t;
215: int len;
216: char *nlp;
217: int f;
218:
219: #define READLINE if((p = nlp = Frdline(f)) == 0)\
220: goto done;\
221: else /* Frdline nulls the \n, put it back */\
222: len = FIOLINELEN(f), nlp[len++] = RIGHT
223:
224: if (file) {
225: if ((f = open(file, 0)) < 0) {
226: fprint(2, "%s: can't open %s\n", progname, file);
227: badbotch=1;
228: return;
229: }
230: }
231: else f = 0;
232: Finit(f, (char *)0);
233: Ftie(f, 1); /* link input f with stdout */
234: lnum = 1;
235: tln = 0;
236: READLINE;
237: cstat = istat;
238: if (cstat->out) goto found;
239: for (;;) {
240: if ((t = cstat->gotofn[*(unsigned char *)p]) == 0)
241: cstat = nxtst(cstat, *(unsigned char *)p);
242: else
243: cstat = t;
244: if (cstat->out) {
245: found:
246: if (vflag == 0) {
247: succeed: nsucc = 1;
248: if (cflag) tln++;
249: else if (sflag){
250: if(scanexit) exit(0);
251: } else if (lflag) {
252: Fprint(1, "%s\n", file);
253: close(f);
254: return;
255: }
256: else {
257: if (nfile > 1 && hflag)
258: Fprint(1, "%s:", file);
259: if (bflag)
260: Fprint(1, "%ld:", (FIOSEEK(f)-len)/BLKSIZE);
261: if (nflag)
262: Fprint(1, "%ld:", lnum);
263: Fwrite(1, nlp, len);
264: }
265: }
266: lnum++;
267: READLINE;
268: if (reinit == 1) clearg();
269: if ((cstat = istat)->out)
270: goto found; /* we are a match already */
271: else
272: continue; /* normal pattern matching loop */
273: }
274: if (*p++ == RIGHT) {
275: if (vflag) goto succeed;
276: else {
277: lnum++;
278: READLINE;
279: if (reinit == 1) clearg();
280: if ((cstat = istat)->out)
281: goto found; /* we are a match already */
282: }
283: }
284: }
285: done:
286: #ifdef DOSTATS
287: nbytes += FIOSEEK(f);
288: nlines += lnum-1;
289: #endif
290: close(f);
291: if (cflag) {
292: if (nfile > 1)
293: Fprint(1, "%s:", file);
294: Fprint(1, "%ld\n", tln);
295: }
296: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.