2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.7 1995-10-17 18:01:22 adam
8 * Userfunc may return non-zero in which case the the grepping stops
11 * Revision 1.6 1995/10/09 16:18:32 adam
12 * Function dict_lookup_grep got extra client data parameter.
14 * Revision 1.5 1995/09/14 11:52:59 adam
15 * Grep handle function parameter info is const now.
17 * Revision 1.4 1995/01/24 16:01:02 adam
18 * Added -ansi to CFLAGS.
19 * Use new API of dfa module.
21 * Revision 1.3 1994/10/05 12:16:50 adam
22 * Pagesize is a resource now.
24 * Revision 1.2 1994/10/04 12:08:07 adam
25 * Some bug fixes and some optimizations.
27 * Revision 1.1 1994/10/03 17:23:04 adam
28 * First version of dictionary lookup with regular expressions and errors.
40 typedef unsigned MatchWord;
42 #define MAX_LENGTH 1024
45 int n; /* no of MatchWord needed */
46 int range; /* max no. of errors */
47 int fact; /* (range+1)*n */
48 MatchWord *match_mask; /* match_mask */
53 static INLINE void set_bit (MatchContext *mc, MatchWord *m, int ch, int state)
55 int off = state & (WORD_BITS-1);
56 int wno = state / WORD_BITS;
58 m[mc->n * ch + wno] |= 1<<off;
61 static INLINE MatchWord get_bit (MatchContext *mc, MatchWord *m, int ch,
64 int off = state & (WORD_BITS-1);
65 int wno = state / WORD_BITS;
67 return m[mc->n * ch + wno] & (1<<off);
70 static MatchContext *mk_MatchContext (struct DFA *dfa, int range)
72 MatchContext *mc = xmalloc (sizeof(*mc));
75 mc->n = (dfa->no_states+WORD_BITS) / WORD_BITS;
77 mc->fact = (range+1)*mc->n;
78 mc->match_mask = xcalloc (mc->n, sizeof(*mc->match_mask));
80 for (s = 0; s<dfa->no_states; s++)
81 if (dfa->states[s]->rule_no)
82 set_bit (mc, mc->match_mask, 0, s);
86 static void rm_MatchContext (MatchContext **mc)
88 xfree ((*mc)->match_mask);
93 static void mask_shift (MatchContext *mc, MatchWord *Rdst, MatchWord *Rsrc,
94 struct DFA *dfa, int ch)
97 MatchWord *Rsrc_p = Rsrc, mask;
99 for (j = 0; j<mc->n; j++)
104 for (j = 0; j<WORD_BITS/4; j++)
110 struct DFA_state *state = dfa->states[s];
111 int i = state->tran_no;
113 if (ch >= state->trans[i].ch[0] &&
114 ch <= state->trans[i].ch[1])
115 set_bit (mc, Rdst, 0, state->trans[i].to);
119 struct DFA_state *state = dfa->states[s+1];
120 int i = state->tran_no;
122 if (ch >= state->trans[i].ch[0] &&
123 ch <= state->trans[i].ch[1])
124 set_bit (mc, Rdst, 0, state->trans[i].to);
128 struct DFA_state *state = dfa->states[s+2];
129 int i = state->tran_no;
131 if (ch >= state->trans[i].ch[0] &&
132 ch <= state->trans[i].ch[1])
133 set_bit (mc, Rdst, 0, state->trans[i].to);
137 struct DFA_state *state = dfa->states[s+3];
138 int i = state->tran_no;
140 if (ch >= state->trans[i].ch[0] &&
141 ch <= state->trans[i].ch[1])
142 set_bit (mc, Rdst, 0, state->trans[i].to);
146 if (s >= dfa->no_states)
153 static void shift (MatchContext *mc, MatchWord *Rdst, MatchWord *Rsrc,
157 MatchWord *Rsrc_p = Rsrc, mask;
158 for (j = 0; j<mc->n; j++)
163 for (j = 0; j<WORD_BITS/4; j++)
169 struct DFA_state *state = dfa->states[s];
170 int i = state->tran_no;
172 set_bit (mc, Rdst, 0, state->trans[i].to);
176 struct DFA_state *state = dfa->states[s+1];
177 int i = state->tran_no;
179 set_bit (mc, Rdst, 0, state->trans[i].to);
183 struct DFA_state *state = dfa->states[s+2];
184 int i = state->tran_no;
186 set_bit (mc, Rdst, 0, state->trans[i].to);
190 struct DFA_state *state = dfa->states[s+3];
191 int i = state->tran_no;
193 set_bit (mc, Rdst, 0, state->trans[i].to);
197 if (s >= dfa->no_states)
204 static void or (MatchContext *mc, MatchWord *Rdst,
205 MatchWord *Rsrc1, MatchWord *Rsrc2)
208 for (i = 0; i<mc->n; i++)
209 Rdst[i] = Rsrc1[i] | Rsrc2[i];
212 static INLINE int move (MatchContext *mc, MatchWord *Rj1, MatchWord *Rj,
213 Dict_char ch, struct DFA *dfa, MatchWord *Rtmp)
216 MatchWord *Rtmp_2 = Rtmp + mc->n;
218 mask_shift (mc, Rj1, Rj, dfa, ch);
219 for (d = 1; d <= mc->range; d++)
221 or (mc, Rtmp, Rj, Rj1); /* 2,3 */
223 shift (mc, Rtmp_2, Rtmp, dfa);
225 mask_shift (mc, Rtmp, Rj+mc->n, dfa, ch); /* 1 */
227 or (mc, Rtmp, Rtmp_2, Rtmp); /* 1,2,3*/
231 or (mc, Rj1, Rtmp, Rj); /* 1,2,3,4 */
240 static int dict_grep (Dict dict, Dict_ptr ptr, MatchContext *mc,
241 MatchWord *Rj, int pos, void *client,
242 int (*userfunc)(Dict_char *, const char *, void *),
243 Dict_char *prefix, struct DFA *dfa)
250 dict_bf_readp (dict->dbf, ptr, &p);
252 hi = DICT_nodir(p)-1;
253 indxp = (short*) ((char*) p+DICT_pagesize(dict)-sizeof(short));
259 /* string (Dict_char *) DICT_EOS terminated */
260 /* unsigned char length of information */
261 /* char * information */
264 info = (char*)p + indxp[-lo];
268 MatchWord *Rj0 = Rj + j *mc->fact;
269 MatchWord *Rj1 = Rj + (j+1)*mc->fact;
270 MatchWord *Rj_tmp = Rj + (j+2)*mc->fact;
272 memcpy (&ch, info+j*sizeof(Dict_char), sizeof(Dict_char));
277 if ((*userfunc)(prefix, info+(j+1)*sizeof(Dict_char),
282 move (mc, Rj1, Rj0, ch, dfa, Rj_tmp);
283 for (d = mc->n; --d >= 0; )
284 if (Rj1[mc->range*mc->n + d])
289 for (d = mc->n; --d >= 0; )
290 if (Rj1[mc->range*mc->n + d] & mc->match_mask[d])
299 MatchWord *Rj1 = Rj+ mc->fact;
300 MatchWord *Rj_tmp = Rj+2*mc->fact;
303 /* Dict_ptr subptr */
304 /* Dict_char sub char */
305 /* unsigned char length of information */
306 /* char * information */
307 info = (char*)p - indxp[-lo];
308 memcpy (&ch, info+sizeof(Dict_ptr), sizeof(Dict_char));
311 move (mc, Rj1, Rj, ch, dfa, Rj_tmp);
312 for (d = mc->n; --d >= 0; )
313 if (Rj1[mc->range*mc->n + d])
318 if (info[sizeof(Dict_ptr)+sizeof(Dict_char)])
320 for (d = mc->n; --d >= 0; )
321 if (Rj1[mc->range*mc->n + d] & mc->match_mask[d])
323 prefix[pos+1] = DICT_EOS;
324 if ((*userfunc)(prefix, info+sizeof(Dict_ptr)+
325 sizeof(Dict_char), client))
330 memcpy (&subptr, info, sizeof(Dict_ptr));
333 if (dict_grep (dict, subptr, mc, Rj1, pos+1,
334 client, userfunc, prefix, dfa))
336 dict_bf_readp (dict->dbf, ptr, &p);
337 indxp = (short*) ((char*) p+DICT_pagesize(dict)
347 int dict_lookup_grep (Dict dict, Dict_char *pattern, int range, void *client,
348 int (*userfunc)(Dict_char *name, const char *info,
352 Dict_char prefix[MAX_LENGTH+1];
353 char *this_pattern = pattern;
355 struct DFA *dfa = dfa_init();
358 i = dfa_parse (dfa, &this_pattern);
359 if (i || *this_pattern)
366 mc = mk_MatchContext (dfa, range);
368 Rj = xcalloc ((MAX_LENGTH+1) * mc->n, sizeof(*Rj));
370 set_bit (mc, Rj, 0, 0);
371 for (d = 1; d<=mc->range; d++)
374 memcpy (Rj + mc->n * d, Rj + mc->n * (d-1), mc->n * sizeof(*Rj));
375 for (s = 0; s<dfa->no_states; s++)
377 if (get_bit (mc, Rj, d-1, s))
379 struct DFA_state *state = dfa->states[s];
380 int i = state->tran_no;
382 set_bit (mc, Rj, d, state->trans[i].to);
386 i = dict_grep (dict, 1, mc, Rj, 0, client, userfunc, prefix, dfa);
390 rm_MatchContext (&mc);