2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.14 1996-02-02 13:43:51 adam
8 * The public functions simply use char instead of Dict_char to represent
9 * search strings. Dict_char is used internally only.
11 * Revision 1.13 1996/01/08 09:09:30 adam
12 * Function dfa_parse got 'const' string argument.
14 * Revision 1.12 1995/12/11 09:04:48 adam
15 * Bug fix: the lookup/scan/lookgrep didn't handle empty dictionary.
17 * Revision 1.11 1995/12/06 14:43:02 adam
18 * New function: dict_delete.
20 * Revision 1.10 1995/11/16 17:00:44 adam
23 * Revision 1.9 1995/10/27 13:58:09 adam
24 * Makes 'Database unavailable' diagnostic.
26 * Revision 1.8 1995/10/19 14:57:21 adam
27 * New feature: grep lookup saves length of longest prefix match.
29 * Revision 1.7 1995/10/17 18:01:22 adam
30 * Userfunc may return non-zero in which case the the grepping stops
33 * Revision 1.6 1995/10/09 16:18:32 adam
34 * Function dict_lookup_grep got extra client data parameter.
36 * Revision 1.5 1995/09/14 11:52:59 adam
37 * Grep handle function parameter info is const now.
39 * Revision 1.4 1995/01/24 16:01:02 adam
40 * Added -ansi to CFLAGS.
41 * Use new API of dfa module.
43 * Revision 1.3 1994/10/05 12:16:50 adam
44 * Pagesize is a resource now.
46 * Revision 1.2 1994/10/04 12:08:07 adam
47 * Some bug fixes and some optimizations.
49 * Revision 1.1 1994/10/03 17:23:04 adam
50 * First version of dictionary lookup with regular expressions and errors.
62 typedef unsigned MatchWord;
64 #define MAX_LENGTH 1024
67 int n; /* no of MatchWord needed */
68 int range; /* max no. of errors */
69 int fact; /* (range+1)*n */
70 MatchWord *match_mask; /* match_mask */
75 static INLINE void set_bit (MatchContext *mc, MatchWord *m, int ch, int state)
77 int off = state & (WORD_BITS-1);
78 int wno = state / WORD_BITS;
80 m[mc->n * ch + wno] |= 1<<off;
83 static INLINE MatchWord get_bit (MatchContext *mc, MatchWord *m, int ch,
86 int off = state & (WORD_BITS-1);
87 int wno = state / WORD_BITS;
89 return m[mc->n * ch + wno] & (1<<off);
92 static MatchContext *mk_MatchContext (struct DFA *dfa, int range)
94 MatchContext *mc = xmalloc (sizeof(*mc));
97 mc->n = (dfa->no_states+WORD_BITS) / WORD_BITS;
99 mc->fact = (range+1)*mc->n;
100 mc->match_mask = xcalloc (mc->n, sizeof(*mc->match_mask));
102 for (s = 0; s<dfa->no_states; s++)
103 if (dfa->states[s]->rule_no)
104 set_bit (mc, mc->match_mask, 0, s);
108 static void rm_MatchContext (MatchContext **mc)
110 xfree ((*mc)->match_mask);
115 static void mask_shift (MatchContext *mc, MatchWord *Rdst, MatchWord *Rsrc,
116 struct DFA *dfa, int ch)
119 MatchWord *Rsrc_p = Rsrc, mask;
121 for (j = 0; j<mc->n; j++)
126 for (j = 0; j<WORD_BITS/4; j++)
132 struct DFA_state *state = dfa->states[s];
133 int i = state->tran_no;
135 if (ch >= state->trans[i].ch[0] &&
136 ch <= state->trans[i].ch[1])
137 set_bit (mc, Rdst, 0, state->trans[i].to);
141 struct DFA_state *state = dfa->states[s+1];
142 int i = state->tran_no;
144 if (ch >= state->trans[i].ch[0] &&
145 ch <= state->trans[i].ch[1])
146 set_bit (mc, Rdst, 0, state->trans[i].to);
150 struct DFA_state *state = dfa->states[s+2];
151 int i = state->tran_no;
153 if (ch >= state->trans[i].ch[0] &&
154 ch <= state->trans[i].ch[1])
155 set_bit (mc, Rdst, 0, state->trans[i].to);
159 struct DFA_state *state = dfa->states[s+3];
160 int i = state->tran_no;
162 if (ch >= state->trans[i].ch[0] &&
163 ch <= state->trans[i].ch[1])
164 set_bit (mc, Rdst, 0, state->trans[i].to);
168 if (s >= dfa->no_states)
175 static void shift (MatchContext *mc, MatchWord *Rdst, MatchWord *Rsrc,
179 MatchWord *Rsrc_p = Rsrc, mask;
180 for (j = 0; j<mc->n; j++)
185 for (j = 0; j<WORD_BITS/4; j++)
191 struct DFA_state *state = dfa->states[s];
192 int i = state->tran_no;
194 set_bit (mc, Rdst, 0, state->trans[i].to);
198 struct DFA_state *state = dfa->states[s+1];
199 int i = state->tran_no;
201 set_bit (mc, Rdst, 0, state->trans[i].to);
205 struct DFA_state *state = dfa->states[s+2];
206 int i = state->tran_no;
208 set_bit (mc, Rdst, 0, state->trans[i].to);
212 struct DFA_state *state = dfa->states[s+3];
213 int i = state->tran_no;
215 set_bit (mc, Rdst, 0, state->trans[i].to);
219 if (s >= dfa->no_states)
226 static void or (MatchContext *mc, MatchWord *Rdst,
227 MatchWord *Rsrc1, MatchWord *Rsrc2)
230 for (i = 0; i<mc->n; i++)
231 Rdst[i] = Rsrc1[i] | Rsrc2[i];
234 static INLINE int move (MatchContext *mc, MatchWord *Rj1, MatchWord *Rj,
235 Dict_char ch, struct DFA *dfa, MatchWord *Rtmp)
238 MatchWord *Rtmp_2 = Rtmp + mc->n;
240 mask_shift (mc, Rj1, Rj, dfa, ch);
241 for (d = 1; d <= mc->range; d++)
243 or (mc, Rtmp, Rj, Rj1); /* 2,3 */
245 shift (mc, Rtmp_2, Rtmp, dfa);
247 mask_shift (mc, Rtmp, Rj+mc->n, dfa, ch); /* 1 */
249 or (mc, Rtmp, Rtmp_2, Rtmp); /* 1,2,3*/
253 or (mc, Rj1, Rtmp, Rj); /* 1,2,3,4 */
262 static int dict_grep (Dict dict, Dict_ptr ptr, MatchContext *mc,
263 MatchWord *Rj, int pos, void *client,
264 int (*userfunc)(char *, const char *, void *),
265 Dict_char *prefix, struct DFA *dfa,
273 dict_bf_readp (dict->dbf, ptr, &p);
275 hi = DICT_nodir(p)-1;
276 indxp = (short*) ((char*) p+DICT_pagesize(dict)-sizeof(short));
282 /* string (Dict_char *) DICT_EOS terminated */
283 /* unsigned char length of information */
284 /* char * information */
287 info = (char*)p + indxp[-lo];
291 MatchWord *Rj0 = Rj + j *mc->fact;
292 MatchWord *Rj1 = Rj + (j+1)*mc->fact;
293 MatchWord *Rj_tmp = Rj + (j+2)*mc->fact;
295 memcpy (&ch, info+j*sizeof(Dict_char), sizeof(Dict_char));
297 if (pos+j > *max_pos)
302 if ((*userfunc)((char*) prefix,
303 info+(j+1)*sizeof(Dict_char), client))
307 move (mc, Rj1, Rj0, ch, dfa, Rj_tmp);
308 for (d = mc->n; --d >= 0; )
309 if (Rj1[mc->range*mc->n + d])
314 for (d = mc->n; --d >= 0; )
315 if (Rj1[mc->range*mc->n + d] & mc->match_mask[d])
324 MatchWord *Rj1 = Rj+ mc->fact;
325 MatchWord *Rj_tmp = Rj+2*mc->fact;
328 /* Dict_ptr subptr */
329 /* Dict_char sub char */
330 /* unsigned char length of information */
331 /* char * information */
332 info = (char*)p - indxp[-lo];
333 memcpy (&ch, info+sizeof(Dict_ptr), sizeof(Dict_char));
338 move (mc, Rj1, Rj, ch, dfa, Rj_tmp);
339 for (d = mc->n; --d >= 0; )
340 if (Rj1[mc->range*mc->n + d])
345 if (info[sizeof(Dict_ptr)+sizeof(Dict_char)])
347 for (d = mc->n; --d >= 0; )
348 if (Rj1[mc->range*mc->n + d] & mc->match_mask[d])
350 prefix[pos+1] = DICT_EOS;
351 if ((*userfunc)((char*) prefix,
352 info+sizeof(Dict_ptr)+
353 sizeof(Dict_char), client))
358 memcpy (&subptr, info, sizeof(Dict_ptr));
361 if (dict_grep (dict, subptr, mc, Rj1, pos+1,
362 client, userfunc, prefix, dfa, max_pos))
364 dict_bf_readp (dict->dbf, ptr, &p);
365 indxp = (short*) ((char*) p+DICT_pagesize(dict)
375 int dict_lookup_grep (Dict dict, const char *pattern, int range, void *client,
377 int (*userfunc)(char *name, const char *info,
381 Dict_char prefix[MAX_LENGTH+1];
382 const char *this_pattern = pattern;
384 struct DFA *dfa = dfa_init();
387 logf (LOG_DEBUG, "dict_lookup_grep '%s' range=%d", pattern, range);
388 i = dfa_parse (dfa, &this_pattern);
389 if (i || *this_pattern)
396 mc = mk_MatchContext (dfa, range);
398 Rj = xcalloc ((MAX_LENGTH+1) * mc->n, sizeof(*Rj));
400 set_bit (mc, Rj, 0, 0);
401 for (d = 1; d<=mc->range; d++)
404 memcpy (Rj + mc->n * d, Rj + mc->n * (d-1), mc->n * sizeof(*Rj));
405 for (s = 0; s<dfa->no_states; s++)
407 if (get_bit (mc, Rj, d-1, s))
409 struct DFA_state *state = dfa->states[s];
410 int i = state->tran_no;
412 set_bit (mc, Rj, d, state->trans[i].to);
417 if (dict->head.last > 1)
418 i = dict_grep (dict, 1, mc, Rj, 0, client, userfunc, prefix,
422 logf (LOG_DEBUG, "max_pos = %d", *max_pos);
425 rm_MatchContext (&mc);