2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.10 1995-11-16 17:00:44 adam
10 * Revision 1.9 1995/10/27 13:58:09 adam
11 * Makes 'Database unavailable' diagnostic.
13 * Revision 1.8 1995/10/19 14:57:21 adam
14 * New feature: grep lookup saves length of longest prefix match.
16 * Revision 1.7 1995/10/17 18:01:22 adam
17 * Userfunc may return non-zero in which case the the grepping stops
20 * Revision 1.6 1995/10/09 16:18:32 adam
21 * Function dict_lookup_grep got extra client data parameter.
23 * Revision 1.5 1995/09/14 11:52:59 adam
24 * Grep handle function parameter info is const now.
26 * Revision 1.4 1995/01/24 16:01:02 adam
27 * Added -ansi to CFLAGS.
28 * Use new API of dfa module.
30 * Revision 1.3 1994/10/05 12:16:50 adam
31 * Pagesize is a resource now.
33 * Revision 1.2 1994/10/04 12:08:07 adam
34 * Some bug fixes and some optimizations.
36 * Revision 1.1 1994/10/03 17:23:04 adam
37 * First version of dictionary lookup with regular expressions and errors.
49 typedef unsigned MatchWord;
51 #define MAX_LENGTH 1024
54 int n; /* no of MatchWord needed */
55 int range; /* max no. of errors */
56 int fact; /* (range+1)*n */
57 MatchWord *match_mask; /* match_mask */
62 static INLINE void set_bit (MatchContext *mc, MatchWord *m, int ch, int state)
64 int off = state & (WORD_BITS-1);
65 int wno = state / WORD_BITS;
67 m[mc->n * ch + wno] |= 1<<off;
70 static INLINE MatchWord get_bit (MatchContext *mc, MatchWord *m, int ch,
73 int off = state & (WORD_BITS-1);
74 int wno = state / WORD_BITS;
76 return m[mc->n * ch + wno] & (1<<off);
79 static MatchContext *mk_MatchContext (struct DFA *dfa, int range)
81 MatchContext *mc = xmalloc (sizeof(*mc));
84 mc->n = (dfa->no_states+WORD_BITS) / WORD_BITS;
86 mc->fact = (range+1)*mc->n;
87 mc->match_mask = xcalloc (mc->n, sizeof(*mc->match_mask));
89 for (s = 0; s<dfa->no_states; s++)
90 if (dfa->states[s]->rule_no)
91 set_bit (mc, mc->match_mask, 0, s);
95 static void rm_MatchContext (MatchContext **mc)
97 xfree ((*mc)->match_mask);
102 static void mask_shift (MatchContext *mc, MatchWord *Rdst, MatchWord *Rsrc,
103 struct DFA *dfa, int ch)
106 MatchWord *Rsrc_p = Rsrc, mask;
108 for (j = 0; j<mc->n; j++)
113 for (j = 0; j<WORD_BITS/4; j++)
119 struct DFA_state *state = dfa->states[s];
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+1];
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+2];
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+3];
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 if (s >= dfa->no_states)
162 static void shift (MatchContext *mc, MatchWord *Rdst, MatchWord *Rsrc,
166 MatchWord *Rsrc_p = Rsrc, mask;
167 for (j = 0; j<mc->n; j++)
172 for (j = 0; j<WORD_BITS/4; j++)
178 struct DFA_state *state = dfa->states[s];
179 int i = state->tran_no;
181 set_bit (mc, Rdst, 0, state->trans[i].to);
185 struct DFA_state *state = dfa->states[s+1];
186 int i = state->tran_no;
188 set_bit (mc, Rdst, 0, state->trans[i].to);
192 struct DFA_state *state = dfa->states[s+2];
193 int i = state->tran_no;
195 set_bit (mc, Rdst, 0, state->trans[i].to);
199 struct DFA_state *state = dfa->states[s+3];
200 int i = state->tran_no;
202 set_bit (mc, Rdst, 0, state->trans[i].to);
206 if (s >= dfa->no_states)
213 static void or (MatchContext *mc, MatchWord *Rdst,
214 MatchWord *Rsrc1, MatchWord *Rsrc2)
217 for (i = 0; i<mc->n; i++)
218 Rdst[i] = Rsrc1[i] | Rsrc2[i];
221 static INLINE int move (MatchContext *mc, MatchWord *Rj1, MatchWord *Rj,
222 Dict_char ch, struct DFA *dfa, MatchWord *Rtmp)
225 MatchWord *Rtmp_2 = Rtmp + mc->n;
227 mask_shift (mc, Rj1, Rj, dfa, ch);
228 for (d = 1; d <= mc->range; d++)
230 or (mc, Rtmp, Rj, Rj1); /* 2,3 */
232 shift (mc, Rtmp_2, Rtmp, dfa);
234 mask_shift (mc, Rtmp, Rj+mc->n, dfa, ch); /* 1 */
236 or (mc, Rtmp, Rtmp_2, Rtmp); /* 1,2,3*/
240 or (mc, Rj1, Rtmp, Rj); /* 1,2,3,4 */
249 static int dict_grep (Dict dict, Dict_ptr ptr, MatchContext *mc,
250 MatchWord *Rj, int pos, void *client,
251 int (*userfunc)(Dict_char *, const char *, void *),
252 Dict_char *prefix, struct DFA *dfa,
260 dict_bf_readp (dict->dbf, ptr, &p);
262 hi = DICT_nodir(p)-1;
263 indxp = (short*) ((char*) p+DICT_pagesize(dict)-sizeof(short));
269 /* string (Dict_char *) DICT_EOS terminated */
270 /* unsigned char length of information */
271 /* char * information */
274 info = (char*)p + indxp[-lo];
278 MatchWord *Rj0 = Rj + j *mc->fact;
279 MatchWord *Rj1 = Rj + (j+1)*mc->fact;
280 MatchWord *Rj_tmp = Rj + (j+2)*mc->fact;
282 memcpy (&ch, info+j*sizeof(Dict_char), sizeof(Dict_char));
284 if (pos+j > *max_pos)
289 if ((*userfunc)(prefix, info+(j+1)*sizeof(Dict_char),
294 move (mc, Rj1, Rj0, ch, dfa, Rj_tmp);
295 for (d = mc->n; --d >= 0; )
296 if (Rj1[mc->range*mc->n + d])
301 for (d = mc->n; --d >= 0; )
302 if (Rj1[mc->range*mc->n + d] & mc->match_mask[d])
311 MatchWord *Rj1 = Rj+ mc->fact;
312 MatchWord *Rj_tmp = Rj+2*mc->fact;
315 /* Dict_ptr subptr */
316 /* Dict_char sub char */
317 /* unsigned char length of information */
318 /* char * information */
319 info = (char*)p - indxp[-lo];
320 memcpy (&ch, info+sizeof(Dict_ptr), sizeof(Dict_char));
325 move (mc, Rj1, Rj, ch, dfa, Rj_tmp);
326 for (d = mc->n; --d >= 0; )
327 if (Rj1[mc->range*mc->n + d])
332 if (info[sizeof(Dict_ptr)+sizeof(Dict_char)])
334 for (d = mc->n; --d >= 0; )
335 if (Rj1[mc->range*mc->n + d] & mc->match_mask[d])
337 prefix[pos+1] = DICT_EOS;
338 if ((*userfunc)(prefix, info+sizeof(Dict_ptr)+
339 sizeof(Dict_char), client))
344 memcpy (&subptr, info, sizeof(Dict_ptr));
347 if (dict_grep (dict, subptr, mc, Rj1, pos+1,
348 client, userfunc, prefix, dfa, max_pos))
350 dict_bf_readp (dict->dbf, ptr, &p);
351 indxp = (short*) ((char*) p+DICT_pagesize(dict)
361 int dict_lookup_grep (Dict dict, Dict_char *pattern, int range, void *client,
363 int (*userfunc)(Dict_char *name, const char *info,
367 Dict_char prefix[MAX_LENGTH+1];
368 char *this_pattern = pattern;
370 struct DFA *dfa = dfa_init();
373 i = dfa_parse (dfa, &this_pattern);
374 if (i || *this_pattern)
381 mc = mk_MatchContext (dfa, range);
383 Rj = xcalloc ((MAX_LENGTH+1) * mc->n, sizeof(*Rj));
385 set_bit (mc, Rj, 0, 0);
386 for (d = 1; d<=mc->range; d++)
389 memcpy (Rj + mc->n * d, Rj + mc->n * (d-1), mc->n * sizeof(*Rj));
390 for (s = 0; s<dfa->no_states; s++)
392 if (get_bit (mc, Rj, d-1, s))
394 struct DFA_state *state = dfa->states[s];
395 int i = state->tran_no;
397 set_bit (mc, Rj, d, state->trans[i].to);
402 i = dict_grep (dict, 1, mc, Rj, 0, client, userfunc, prefix, dfa,
404 logf (LOG_DEBUG, "max_pos = %d", *max_pos);
407 rm_MatchContext (&mc);