2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.13 1996-01-08 09:09:30 adam
8 * Function dfa_parse got 'const' string argument.
10 * Revision 1.12 1995/12/11 09:04:48 adam
11 * Bug fix: the lookup/scan/lookgrep didn't handle empty dictionary.
13 * Revision 1.11 1995/12/06 14:43:02 adam
14 * New function: dict_delete.
16 * Revision 1.10 1995/11/16 17:00:44 adam
19 * Revision 1.9 1995/10/27 13:58:09 adam
20 * Makes 'Database unavailable' diagnostic.
22 * Revision 1.8 1995/10/19 14:57:21 adam
23 * New feature: grep lookup saves length of longest prefix match.
25 * Revision 1.7 1995/10/17 18:01:22 adam
26 * Userfunc may return non-zero in which case the the grepping stops
29 * Revision 1.6 1995/10/09 16:18:32 adam
30 * Function dict_lookup_grep got extra client data parameter.
32 * Revision 1.5 1995/09/14 11:52:59 adam
33 * Grep handle function parameter info is const now.
35 * Revision 1.4 1995/01/24 16:01:02 adam
36 * Added -ansi to CFLAGS.
37 * Use new API of dfa module.
39 * Revision 1.3 1994/10/05 12:16:50 adam
40 * Pagesize is a resource now.
42 * Revision 1.2 1994/10/04 12:08:07 adam
43 * Some bug fixes and some optimizations.
45 * Revision 1.1 1994/10/03 17:23:04 adam
46 * First version of dictionary lookup with regular expressions and errors.
58 typedef unsigned MatchWord;
60 #define MAX_LENGTH 1024
63 int n; /* no of MatchWord needed */
64 int range; /* max no. of errors */
65 int fact; /* (range+1)*n */
66 MatchWord *match_mask; /* match_mask */
71 static INLINE void set_bit (MatchContext *mc, MatchWord *m, int ch, int state)
73 int off = state & (WORD_BITS-1);
74 int wno = state / WORD_BITS;
76 m[mc->n * ch + wno] |= 1<<off;
79 static INLINE MatchWord get_bit (MatchContext *mc, MatchWord *m, int ch,
82 int off = state & (WORD_BITS-1);
83 int wno = state / WORD_BITS;
85 return m[mc->n * ch + wno] & (1<<off);
88 static MatchContext *mk_MatchContext (struct DFA *dfa, int range)
90 MatchContext *mc = xmalloc (sizeof(*mc));
93 mc->n = (dfa->no_states+WORD_BITS) / WORD_BITS;
95 mc->fact = (range+1)*mc->n;
96 mc->match_mask = xcalloc (mc->n, sizeof(*mc->match_mask));
98 for (s = 0; s<dfa->no_states; s++)
99 if (dfa->states[s]->rule_no)
100 set_bit (mc, mc->match_mask, 0, s);
104 static void rm_MatchContext (MatchContext **mc)
106 xfree ((*mc)->match_mask);
111 static void mask_shift (MatchContext *mc, MatchWord *Rdst, MatchWord *Rsrc,
112 struct DFA *dfa, int ch)
115 MatchWord *Rsrc_p = Rsrc, mask;
117 for (j = 0; j<mc->n; j++)
122 for (j = 0; j<WORD_BITS/4; j++)
128 struct DFA_state *state = dfa->states[s];
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+1];
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 struct DFA_state *state = dfa->states[s+2];
147 int i = state->tran_no;
149 if (ch >= state->trans[i].ch[0] &&
150 ch <= state->trans[i].ch[1])
151 set_bit (mc, Rdst, 0, state->trans[i].to);
155 struct DFA_state *state = dfa->states[s+3];
156 int i = state->tran_no;
158 if (ch >= state->trans[i].ch[0] &&
159 ch <= state->trans[i].ch[1])
160 set_bit (mc, Rdst, 0, state->trans[i].to);
164 if (s >= dfa->no_states)
171 static void shift (MatchContext *mc, MatchWord *Rdst, MatchWord *Rsrc,
175 MatchWord *Rsrc_p = Rsrc, mask;
176 for (j = 0; j<mc->n; j++)
181 for (j = 0; j<WORD_BITS/4; j++)
187 struct DFA_state *state = dfa->states[s];
188 int i = state->tran_no;
190 set_bit (mc, Rdst, 0, state->trans[i].to);
194 struct DFA_state *state = dfa->states[s+1];
195 int i = state->tran_no;
197 set_bit (mc, Rdst, 0, state->trans[i].to);
201 struct DFA_state *state = dfa->states[s+2];
202 int i = state->tran_no;
204 set_bit (mc, Rdst, 0, state->trans[i].to);
208 struct DFA_state *state = dfa->states[s+3];
209 int i = state->tran_no;
211 set_bit (mc, Rdst, 0, state->trans[i].to);
215 if (s >= dfa->no_states)
222 static void or (MatchContext *mc, MatchWord *Rdst,
223 MatchWord *Rsrc1, MatchWord *Rsrc2)
226 for (i = 0; i<mc->n; i++)
227 Rdst[i] = Rsrc1[i] | Rsrc2[i];
230 static INLINE int move (MatchContext *mc, MatchWord *Rj1, MatchWord *Rj,
231 Dict_char ch, struct DFA *dfa, MatchWord *Rtmp)
234 MatchWord *Rtmp_2 = Rtmp + mc->n;
236 mask_shift (mc, Rj1, Rj, dfa, ch);
237 for (d = 1; d <= mc->range; d++)
239 or (mc, Rtmp, Rj, Rj1); /* 2,3 */
241 shift (mc, Rtmp_2, Rtmp, dfa);
243 mask_shift (mc, Rtmp, Rj+mc->n, dfa, ch); /* 1 */
245 or (mc, Rtmp, Rtmp_2, Rtmp); /* 1,2,3*/
249 or (mc, Rj1, Rtmp, Rj); /* 1,2,3,4 */
258 static int dict_grep (Dict dict, Dict_ptr ptr, MatchContext *mc,
259 MatchWord *Rj, int pos, void *client,
260 int (*userfunc)(Dict_char *, const char *, void *),
261 Dict_char *prefix, struct DFA *dfa,
269 dict_bf_readp (dict->dbf, ptr, &p);
271 hi = DICT_nodir(p)-1;
272 indxp = (short*) ((char*) p+DICT_pagesize(dict)-sizeof(short));
278 /* string (Dict_char *) DICT_EOS terminated */
279 /* unsigned char length of information */
280 /* char * information */
283 info = (char*)p + indxp[-lo];
287 MatchWord *Rj0 = Rj + j *mc->fact;
288 MatchWord *Rj1 = Rj + (j+1)*mc->fact;
289 MatchWord *Rj_tmp = Rj + (j+2)*mc->fact;
291 memcpy (&ch, info+j*sizeof(Dict_char), sizeof(Dict_char));
293 if (pos+j > *max_pos)
298 if ((*userfunc)(prefix, info+(j+1)*sizeof(Dict_char),
303 move (mc, Rj1, Rj0, ch, dfa, Rj_tmp);
304 for (d = mc->n; --d >= 0; )
305 if (Rj1[mc->range*mc->n + d])
310 for (d = mc->n; --d >= 0; )
311 if (Rj1[mc->range*mc->n + d] & mc->match_mask[d])
320 MatchWord *Rj1 = Rj+ mc->fact;
321 MatchWord *Rj_tmp = Rj+2*mc->fact;
324 /* Dict_ptr subptr */
325 /* Dict_char sub char */
326 /* unsigned char length of information */
327 /* char * information */
328 info = (char*)p - indxp[-lo];
329 memcpy (&ch, info+sizeof(Dict_ptr), sizeof(Dict_char));
334 move (mc, Rj1, Rj, ch, dfa, Rj_tmp);
335 for (d = mc->n; --d >= 0; )
336 if (Rj1[mc->range*mc->n + d])
341 if (info[sizeof(Dict_ptr)+sizeof(Dict_char)])
343 for (d = mc->n; --d >= 0; )
344 if (Rj1[mc->range*mc->n + d] & mc->match_mask[d])
346 prefix[pos+1] = DICT_EOS;
347 if ((*userfunc)(prefix, info+sizeof(Dict_ptr)+
348 sizeof(Dict_char), client))
353 memcpy (&subptr, info, sizeof(Dict_ptr));
356 if (dict_grep (dict, subptr, mc, Rj1, pos+1,
357 client, userfunc, prefix, dfa, max_pos))
359 dict_bf_readp (dict->dbf, ptr, &p);
360 indxp = (short*) ((char*) p+DICT_pagesize(dict)
370 int dict_lookup_grep (Dict dict, Dict_char *pattern, int range, void *client,
372 int (*userfunc)(Dict_char *name, const char *info,
376 Dict_char prefix[MAX_LENGTH+1];
377 const char *this_pattern = pattern;
379 struct DFA *dfa = dfa_init();
382 logf (LOG_DEBUG, "dict_lookup_grep '%s' range=%d", pattern, range);
383 i = dfa_parse (dfa, &this_pattern);
384 if (i || *this_pattern)
391 mc = mk_MatchContext (dfa, range);
393 Rj = xcalloc ((MAX_LENGTH+1) * mc->n, sizeof(*Rj));
395 set_bit (mc, Rj, 0, 0);
396 for (d = 1; d<=mc->range; d++)
399 memcpy (Rj + mc->n * d, Rj + mc->n * (d-1), mc->n * sizeof(*Rj));
400 for (s = 0; s<dfa->no_states; s++)
402 if (get_bit (mc, Rj, d-1, s))
404 struct DFA_state *state = dfa->states[s];
405 int i = state->tran_no;
407 set_bit (mc, Rj, d, state->trans[i].to);
412 if (dict->head.last > 1)
413 i = dict_grep (dict, 1, mc, Rj, 0, client, userfunc, prefix,
417 logf (LOG_DEBUG, "max_pos = %d", *max_pos);
420 rm_MatchContext (&mc);