2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.9 1995-10-27 13:58:09 adam
8 * Makes 'Database unavailable' diagnostic.
10 * Revision 1.8 1995/10/19 14:57:21 adam
11 * New feature: grep lookup saves length of longest prefix match.
13 * Revision 1.7 1995/10/17 18:01:22 adam
14 * Userfunc may return non-zero in which case the the grepping stops
17 * Revision 1.6 1995/10/09 16:18:32 adam
18 * Function dict_lookup_grep got extra client data parameter.
20 * Revision 1.5 1995/09/14 11:52:59 adam
21 * Grep handle function parameter info is const now.
23 * Revision 1.4 1995/01/24 16:01:02 adam
24 * Added -ansi to CFLAGS.
25 * Use new API of dfa module.
27 * Revision 1.3 1994/10/05 12:16:50 adam
28 * Pagesize is a resource now.
30 * Revision 1.2 1994/10/04 12:08:07 adam
31 * Some bug fixes and some optimizations.
33 * Revision 1.1 1994/10/03 17:23:04 adam
34 * First version of dictionary lookup with regular expressions and errors.
46 typedef unsigned MatchWord;
48 #define MAX_LENGTH 1024
51 int n; /* no of MatchWord needed */
52 int range; /* max no. of errors */
53 int fact; /* (range+1)*n */
54 MatchWord *match_mask; /* match_mask */
59 static INLINE void set_bit (MatchContext *mc, MatchWord *m, int ch, int state)
61 int off = state & (WORD_BITS-1);
62 int wno = state / WORD_BITS;
64 m[mc->n * ch + wno] |= 1<<off;
67 static INLINE MatchWord get_bit (MatchContext *mc, MatchWord *m, int ch,
70 int off = state & (WORD_BITS-1);
71 int wno = state / WORD_BITS;
73 return m[mc->n * ch + wno] & (1<<off);
76 static MatchContext *mk_MatchContext (struct DFA *dfa, int range)
78 MatchContext *mc = xmalloc (sizeof(*mc));
81 mc->n = (dfa->no_states+WORD_BITS) / WORD_BITS;
83 mc->fact = (range+1)*mc->n;
84 mc->match_mask = xcalloc (mc->n, sizeof(*mc->match_mask));
86 for (s = 0; s<dfa->no_states; s++)
87 if (dfa->states[s]->rule_no)
88 set_bit (mc, mc->match_mask, 0, s);
92 static void rm_MatchContext (MatchContext **mc)
94 xfree ((*mc)->match_mask);
99 static void mask_shift (MatchContext *mc, MatchWord *Rdst, MatchWord *Rsrc,
100 struct DFA *dfa, int ch)
103 MatchWord *Rsrc_p = Rsrc, mask;
105 for (j = 0; j<mc->n; j++)
110 for (j = 0; j<WORD_BITS/4; j++)
116 struct DFA_state *state = dfa->states[s];
117 int i = state->tran_no;
119 if (ch >= state->trans[i].ch[0] &&
120 ch <= state->trans[i].ch[1])
121 set_bit (mc, Rdst, 0, state->trans[i].to);
125 struct DFA_state *state = dfa->states[s+1];
126 int i = state->tran_no;
128 if (ch >= state->trans[i].ch[0] &&
129 ch <= state->trans[i].ch[1])
130 set_bit (mc, Rdst, 0, state->trans[i].to);
134 struct DFA_state *state = dfa->states[s+2];
135 int i = state->tran_no;
137 if (ch >= state->trans[i].ch[0] &&
138 ch <= state->trans[i].ch[1])
139 set_bit (mc, Rdst, 0, state->trans[i].to);
143 struct DFA_state *state = dfa->states[s+3];
144 int i = state->tran_no;
146 if (ch >= state->trans[i].ch[0] &&
147 ch <= state->trans[i].ch[1])
148 set_bit (mc, Rdst, 0, state->trans[i].to);
152 if (s >= dfa->no_states)
159 static void shift (MatchContext *mc, MatchWord *Rdst, MatchWord *Rsrc,
163 MatchWord *Rsrc_p = Rsrc, mask;
164 for (j = 0; j<mc->n; j++)
169 for (j = 0; j<WORD_BITS/4; j++)
175 struct DFA_state *state = dfa->states[s];
176 int i = state->tran_no;
178 set_bit (mc, Rdst, 0, state->trans[i].to);
182 struct DFA_state *state = dfa->states[s+1];
183 int i = state->tran_no;
185 set_bit (mc, Rdst, 0, state->trans[i].to);
189 struct DFA_state *state = dfa->states[s+2];
190 int i = state->tran_no;
192 set_bit (mc, Rdst, 0, state->trans[i].to);
196 struct DFA_state *state = dfa->states[s+3];
197 int i = state->tran_no;
199 set_bit (mc, Rdst, 0, state->trans[i].to);
203 if (s >= dfa->no_states)
210 static void or (MatchContext *mc, MatchWord *Rdst,
211 MatchWord *Rsrc1, MatchWord *Rsrc2)
214 for (i = 0; i<mc->n; i++)
215 Rdst[i] = Rsrc1[i] | Rsrc2[i];
218 static INLINE int move (MatchContext *mc, MatchWord *Rj1, MatchWord *Rj,
219 Dict_char ch, struct DFA *dfa, MatchWord *Rtmp)
222 MatchWord *Rtmp_2 = Rtmp + mc->n;
224 mask_shift (mc, Rj1, Rj, dfa, ch);
225 for (d = 1; d <= mc->range; d++)
227 or (mc, Rtmp, Rj, Rj1); /* 2,3 */
229 shift (mc, Rtmp_2, Rtmp, dfa);
231 mask_shift (mc, Rtmp, Rj+mc->n, dfa, ch); /* 1 */
233 or (mc, Rtmp, Rtmp_2, Rtmp); /* 1,2,3*/
237 or (mc, Rj1, Rtmp, Rj); /* 1,2,3,4 */
246 static int dict_grep (Dict dict, Dict_ptr ptr, MatchContext *mc,
247 MatchWord *Rj, int pos, void *client,
248 int (*userfunc)(Dict_char *, const char *, void *),
249 Dict_char *prefix, struct DFA *dfa,
257 dict_bf_readp (dict->dbf, ptr, &p);
259 hi = DICT_nodir(p)-1;
260 indxp = (short*) ((char*) p+DICT_pagesize(dict)-sizeof(short));
266 /* string (Dict_char *) DICT_EOS terminated */
267 /* unsigned char length of information */
268 /* char * information */
271 info = (char*)p + indxp[-lo];
275 MatchWord *Rj0 = Rj + j *mc->fact;
276 MatchWord *Rj1 = Rj + (j+1)*mc->fact;
277 MatchWord *Rj_tmp = Rj + (j+2)*mc->fact;
279 memcpy (&ch, info+j*sizeof(Dict_char), sizeof(Dict_char));
281 if (pos+j > *max_pos)
286 if ((*userfunc)(prefix, info+(j+1)*sizeof(Dict_char),
291 move (mc, Rj1, Rj0, ch, dfa, Rj_tmp);
292 for (d = mc->n; --d >= 0; )
293 if (Rj1[mc->range*mc->n + d])
298 for (d = mc->n; --d >= 0; )
299 if (Rj1[mc->range*mc->n + d] & mc->match_mask[d])
308 MatchWord *Rj1 = Rj+ mc->fact;
309 MatchWord *Rj_tmp = Rj+2*mc->fact;
312 /* Dict_ptr subptr */
313 /* Dict_char sub char */
314 /* unsigned char length of information */
315 /* char * information */
316 info = (char*)p - indxp[-lo];
317 memcpy (&ch, info+sizeof(Dict_ptr), sizeof(Dict_char));
322 move (mc, Rj1, Rj, ch, dfa, Rj_tmp);
323 for (d = mc->n; --d >= 0; )
324 if (Rj1[mc->range*mc->n + d])
329 if (info[sizeof(Dict_ptr)+sizeof(Dict_char)])
331 for (d = mc->n; --d >= 0; )
332 if (Rj1[mc->range*mc->n + d] & mc->match_mask[d])
334 prefix[pos+1] = DICT_EOS;
335 if ((*userfunc)(prefix, info+sizeof(Dict_ptr)+
336 sizeof(Dict_char), client))
341 memcpy (&subptr, info, sizeof(Dict_ptr));
344 if (dict_grep (dict, subptr, mc, Rj1, pos+1,
345 client, userfunc, prefix, dfa, max_pos))
347 dict_bf_readp (dict->dbf, ptr, &p);
348 indxp = (short*) ((char*) p+DICT_pagesize(dict)
358 int dict_lookup_grep (Dict dict, Dict_char *pattern, int range, void *client,
360 int (*userfunc)(Dict_char *name, const char *info,
364 Dict_char prefix[MAX_LENGTH+1];
365 char *this_pattern = pattern;
367 struct DFA *dfa = dfa_init();
370 i = dfa_parse (dfa, &this_pattern);
371 if (i || *this_pattern)
378 mc = mk_MatchContext (dfa, range);
380 Rj = xcalloc ((MAX_LENGTH+1) * mc->n, sizeof(*Rj));
382 set_bit (mc, Rj, 0, 0);
383 for (d = 1; d<=mc->range; d++)
386 memcpy (Rj + mc->n * d, Rj + mc->n * (d-1), mc->n * sizeof(*Rj));
387 for (s = 0; s<dfa->no_states; s++)
389 if (get_bit (mc, Rj, d-1, s))
391 struct DFA_state *state = dfa->states[s];
392 int i = state->tran_no;
394 set_bit (mc, Rj, d, state->trans[i].to);
399 i = dict_grep (dict, 1, mc, Rj, 0, client, userfunc, prefix, dfa,
401 logf (LOG_DEBUG, "max_pos = %d", max_pos);
404 rm_MatchContext (&mc);