2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.8 1995-10-19 14:57:21 adam
8 * New feature: grep lookup saves length of longest prefix match.
10 * Revision 1.7 1995/10/17 18:01:22 adam
11 * Userfunc may return non-zero in which case the the grepping stops
14 * Revision 1.6 1995/10/09 16:18:32 adam
15 * Function dict_lookup_grep got extra client data parameter.
17 * Revision 1.5 1995/09/14 11:52:59 adam
18 * Grep handle function parameter info is const now.
20 * Revision 1.4 1995/01/24 16:01:02 adam
21 * Added -ansi to CFLAGS.
22 * Use new API of dfa module.
24 * Revision 1.3 1994/10/05 12:16:50 adam
25 * Pagesize is a resource now.
27 * Revision 1.2 1994/10/04 12:08:07 adam
28 * Some bug fixes and some optimizations.
30 * Revision 1.1 1994/10/03 17:23:04 adam
31 * First version of dictionary lookup with regular expressions and errors.
43 typedef unsigned MatchWord;
45 #define MAX_LENGTH 1024
48 int n; /* no of MatchWord needed */
49 int range; /* max no. of errors */
50 int fact; /* (range+1)*n */
51 MatchWord *match_mask; /* match_mask */
56 static INLINE void set_bit (MatchContext *mc, MatchWord *m, int ch, int state)
58 int off = state & (WORD_BITS-1);
59 int wno = state / WORD_BITS;
61 m[mc->n * ch + wno] |= 1<<off;
64 static INLINE MatchWord get_bit (MatchContext *mc, MatchWord *m, int ch,
67 int off = state & (WORD_BITS-1);
68 int wno = state / WORD_BITS;
70 return m[mc->n * ch + wno] & (1<<off);
73 static MatchContext *mk_MatchContext (struct DFA *dfa, int range)
75 MatchContext *mc = xmalloc (sizeof(*mc));
78 mc->n = (dfa->no_states+WORD_BITS) / WORD_BITS;
80 mc->fact = (range+1)*mc->n;
81 mc->match_mask = xcalloc (mc->n, sizeof(*mc->match_mask));
83 for (s = 0; s<dfa->no_states; s++)
84 if (dfa->states[s]->rule_no)
85 set_bit (mc, mc->match_mask, 0, s);
89 static void rm_MatchContext (MatchContext **mc)
91 xfree ((*mc)->match_mask);
96 static void mask_shift (MatchContext *mc, MatchWord *Rdst, MatchWord *Rsrc,
97 struct DFA *dfa, int ch)
100 MatchWord *Rsrc_p = Rsrc, mask;
102 for (j = 0; j<mc->n; j++)
107 for (j = 0; j<WORD_BITS/4; j++)
113 struct DFA_state *state = dfa->states[s];
114 int i = state->tran_no;
116 if (ch >= state->trans[i].ch[0] &&
117 ch <= state->trans[i].ch[1])
118 set_bit (mc, Rdst, 0, state->trans[i].to);
122 struct DFA_state *state = dfa->states[s+1];
123 int i = state->tran_no;
125 if (ch >= state->trans[i].ch[0] &&
126 ch <= state->trans[i].ch[1])
127 set_bit (mc, Rdst, 0, state->trans[i].to);
131 struct DFA_state *state = dfa->states[s+2];
132 int i = state->tran_no;
134 if (ch >= state->trans[i].ch[0] &&
135 ch <= state->trans[i].ch[1])
136 set_bit (mc, Rdst, 0, state->trans[i].to);
140 struct DFA_state *state = dfa->states[s+3];
141 int i = state->tran_no;
143 if (ch >= state->trans[i].ch[0] &&
144 ch <= state->trans[i].ch[1])
145 set_bit (mc, Rdst, 0, state->trans[i].to);
149 if (s >= dfa->no_states)
156 static void shift (MatchContext *mc, MatchWord *Rdst, MatchWord *Rsrc,
160 MatchWord *Rsrc_p = Rsrc, mask;
161 for (j = 0; j<mc->n; j++)
166 for (j = 0; j<WORD_BITS/4; j++)
172 struct DFA_state *state = dfa->states[s];
173 int i = state->tran_no;
175 set_bit (mc, Rdst, 0, state->trans[i].to);
179 struct DFA_state *state = dfa->states[s+1];
180 int i = state->tran_no;
182 set_bit (mc, Rdst, 0, state->trans[i].to);
186 struct DFA_state *state = dfa->states[s+2];
187 int i = state->tran_no;
189 set_bit (mc, Rdst, 0, state->trans[i].to);
193 struct DFA_state *state = dfa->states[s+3];
194 int i = state->tran_no;
196 set_bit (mc, Rdst, 0, state->trans[i].to);
200 if (s >= dfa->no_states)
207 static void or (MatchContext *mc, MatchWord *Rdst,
208 MatchWord *Rsrc1, MatchWord *Rsrc2)
211 for (i = 0; i<mc->n; i++)
212 Rdst[i] = Rsrc1[i] | Rsrc2[i];
215 static INLINE int move (MatchContext *mc, MatchWord *Rj1, MatchWord *Rj,
216 Dict_char ch, struct DFA *dfa, MatchWord *Rtmp)
219 MatchWord *Rtmp_2 = Rtmp + mc->n;
221 mask_shift (mc, Rj1, Rj, dfa, ch);
222 for (d = 1; d <= mc->range; d++)
224 or (mc, Rtmp, Rj, Rj1); /* 2,3 */
226 shift (mc, Rtmp_2, Rtmp, dfa);
228 mask_shift (mc, Rtmp, Rj+mc->n, dfa, ch); /* 1 */
230 or (mc, Rtmp, Rtmp_2, Rtmp); /* 1,2,3*/
234 or (mc, Rj1, Rtmp, Rj); /* 1,2,3,4 */
243 static int dict_grep (Dict dict, Dict_ptr ptr, MatchContext *mc,
244 MatchWord *Rj, int pos, void *client,
245 int (*userfunc)(Dict_char *, const char *, void *),
246 Dict_char *prefix, struct DFA *dfa,
254 dict_bf_readp (dict->dbf, ptr, &p);
256 hi = DICT_nodir(p)-1;
257 indxp = (short*) ((char*) p+DICT_pagesize(dict)-sizeof(short));
263 /* string (Dict_char *) DICT_EOS terminated */
264 /* unsigned char length of information */
265 /* char * information */
268 info = (char*)p + indxp[-lo];
272 MatchWord *Rj0 = Rj + j *mc->fact;
273 MatchWord *Rj1 = Rj + (j+1)*mc->fact;
274 MatchWord *Rj_tmp = Rj + (j+2)*mc->fact;
276 memcpy (&ch, info+j*sizeof(Dict_char), sizeof(Dict_char));
278 if (pos+j > *max_pos)
283 if ((*userfunc)(prefix, info+(j+1)*sizeof(Dict_char),
288 move (mc, Rj1, Rj0, ch, dfa, Rj_tmp);
289 for (d = mc->n; --d >= 0; )
290 if (Rj1[mc->range*mc->n + d])
295 for (d = mc->n; --d >= 0; )
296 if (Rj1[mc->range*mc->n + d] & mc->match_mask[d])
305 MatchWord *Rj1 = Rj+ mc->fact;
306 MatchWord *Rj_tmp = Rj+2*mc->fact;
309 /* Dict_ptr subptr */
310 /* Dict_char sub char */
311 /* unsigned char length of information */
312 /* char * information */
313 info = (char*)p - indxp[-lo];
314 memcpy (&ch, info+sizeof(Dict_ptr), sizeof(Dict_char));
319 move (mc, Rj1, Rj, ch, dfa, Rj_tmp);
320 for (d = mc->n; --d >= 0; )
321 if (Rj1[mc->range*mc->n + d])
326 if (info[sizeof(Dict_ptr)+sizeof(Dict_char)])
328 for (d = mc->n; --d >= 0; )
329 if (Rj1[mc->range*mc->n + d] & mc->match_mask[d])
331 prefix[pos+1] = DICT_EOS;
332 if ((*userfunc)(prefix, info+sizeof(Dict_ptr)+
333 sizeof(Dict_char), client))
338 memcpy (&subptr, info, sizeof(Dict_ptr));
341 if (dict_grep (dict, subptr, mc, Rj1, pos+1,
342 client, userfunc, prefix, dfa, max_pos))
344 dict_bf_readp (dict->dbf, ptr, &p);
345 indxp = (short*) ((char*) p+DICT_pagesize(dict)
355 int dict_lookup_grep (Dict dict, Dict_char *pattern, int range, void *client,
356 int (*userfunc)(Dict_char *name, const char *info,
360 Dict_char prefix[MAX_LENGTH+1];
361 char *this_pattern = pattern;
363 struct DFA *dfa = dfa_init();
366 i = dfa_parse (dfa, &this_pattern);
367 if (i || *this_pattern)
374 mc = mk_MatchContext (dfa, range);
376 Rj = xcalloc ((MAX_LENGTH+1) * mc->n, sizeof(*Rj));
379 set_bit (mc, Rj, 0, 0);
380 for (d = 1; d<=mc->range; d++)
383 memcpy (Rj + mc->n * d, Rj + mc->n * (d-1), mc->n * sizeof(*Rj));
384 for (s = 0; s<dfa->no_states; s++)
386 if (get_bit (mc, Rj, d-1, s))
388 struct DFA_state *state = dfa->states[s];
389 int i = state->tran_no;
391 set_bit (mc, Rj, d, state->trans[i].to);
395 i = dict_grep (dict, 1, mc, Rj, 0, client, userfunc, prefix, dfa,
397 logf (LOG_DEBUG, "max_pos = %d", max_pos);
400 rm_MatchContext (&mc);