2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.15 1996-03-20 09:35:18 adam
8 * Function dict_lookup_grep got extra parameter, init_pos, which marks
9 * from which position in pattern approximate pattern matching should occur.
11 * Revision 1.14 1996/02/02 13:43:51 adam
12 * The public functions simply use char instead of Dict_char to represent
13 * search strings. Dict_char is used internally only.
15 * Revision 1.13 1996/01/08 09:09:30 adam
16 * Function dfa_parse got 'const' string argument.
18 * Revision 1.12 1995/12/11 09:04:48 adam
19 * Bug fix: the lookup/scan/lookgrep didn't handle empty dictionary.
21 * Revision 1.11 1995/12/06 14:43:02 adam
22 * New function: dict_delete.
24 * Revision 1.10 1995/11/16 17:00:44 adam
27 * Revision 1.9 1995/10/27 13:58:09 adam
28 * Makes 'Database unavailable' diagnostic.
30 * Revision 1.8 1995/10/19 14:57:21 adam
31 * New feature: grep lookup saves length of longest prefix match.
33 * Revision 1.7 1995/10/17 18:01:22 adam
34 * Userfunc may return non-zero in which case the the grepping stops
37 * Revision 1.6 1995/10/09 16:18:32 adam
38 * Function dict_lookup_grep got extra client data parameter.
40 * Revision 1.5 1995/09/14 11:52:59 adam
41 * Grep handle function parameter info is const now.
43 * Revision 1.4 1995/01/24 16:01:02 adam
44 * Added -ansi to CFLAGS.
45 * Use new API of dfa module.
47 * Revision 1.3 1994/10/05 12:16:50 adam
48 * Pagesize is a resource now.
50 * Revision 1.2 1994/10/04 12:08:07 adam
51 * Some bug fixes and some optimizations.
53 * Revision 1.1 1994/10/03 17:23:04 adam
54 * First version of dictionary lookup with regular expressions and errors.
66 typedef unsigned MatchWord;
68 #define MAX_LENGTH 1024
71 int n; /* no of MatchWord needed */
72 int range; /* max no. of errors */
73 int fact; /* (range+1)*n */
74 MatchWord *match_mask; /* match_mask */
79 static INLINE void set_bit (MatchContext *mc, MatchWord *m, int ch, int state)
81 int off = state & (WORD_BITS-1);
82 int wno = state / WORD_BITS;
84 m[mc->n * ch + wno] |= 1<<off;
87 static INLINE MatchWord get_bit (MatchContext *mc, MatchWord *m, int ch,
90 int off = state & (WORD_BITS-1);
91 int wno = state / WORD_BITS;
93 return m[mc->n * ch + wno] & (1<<off);
96 static MatchContext *mk_MatchContext (struct DFA *dfa, int range)
98 MatchContext *mc = xmalloc (sizeof(*mc));
101 mc->n = (dfa->no_states+WORD_BITS) / WORD_BITS;
103 mc->fact = (range+1)*mc->n;
104 mc->match_mask = xcalloc (mc->n, sizeof(*mc->match_mask));
106 for (s = 0; s<dfa->no_states; s++)
107 if (dfa->states[s]->rule_no)
108 set_bit (mc, mc->match_mask, 0, s);
112 static void rm_MatchContext (MatchContext **mc)
114 xfree ((*mc)->match_mask);
119 static void mask_shift (MatchContext *mc, MatchWord *Rdst, MatchWord *Rsrc,
120 struct DFA *dfa, int ch)
123 MatchWord *Rsrc_p = Rsrc, mask;
125 for (j = 0; j<mc->n; j++)
130 for (j = 0; j<WORD_BITS/4; j++)
136 struct DFA_state *state = dfa->states[s];
137 int i = state->tran_no;
139 if (ch >= state->trans[i].ch[0] &&
140 ch <= state->trans[i].ch[1])
141 set_bit (mc, Rdst, 0, state->trans[i].to);
145 struct DFA_state *state = dfa->states[s+1];
146 int i = state->tran_no;
148 if (ch >= state->trans[i].ch[0] &&
149 ch <= state->trans[i].ch[1])
150 set_bit (mc, Rdst, 0, state->trans[i].to);
154 struct DFA_state *state = dfa->states[s+2];
155 int i = state->tran_no;
157 if (ch >= state->trans[i].ch[0] &&
158 ch <= state->trans[i].ch[1])
159 set_bit (mc, Rdst, 0, state->trans[i].to);
163 struct DFA_state *state = dfa->states[s+3];
164 int i = state->tran_no;
166 if (ch >= state->trans[i].ch[0] &&
167 ch <= state->trans[i].ch[1])
168 set_bit (mc, Rdst, 0, state->trans[i].to);
172 if (s >= dfa->no_states)
179 static void shift (MatchContext *mc, MatchWord *Rdst, MatchWord *Rsrc,
183 MatchWord *Rsrc_p = Rsrc, mask;
184 for (j = 0; j<mc->n; j++)
189 for (j = 0; j<WORD_BITS/4; j++)
195 struct DFA_state *state = dfa->states[s];
196 int i = state->tran_no;
198 set_bit (mc, Rdst, 0, state->trans[i].to);
202 struct DFA_state *state = dfa->states[s+1];
203 int i = state->tran_no;
205 set_bit (mc, Rdst, 0, state->trans[i].to);
209 struct DFA_state *state = dfa->states[s+2];
210 int i = state->tran_no;
212 set_bit (mc, Rdst, 0, state->trans[i].to);
216 struct DFA_state *state = dfa->states[s+3];
217 int i = state->tran_no;
219 set_bit (mc, Rdst, 0, state->trans[i].to);
223 if (s >= dfa->no_states)
230 static void or (MatchContext *mc, MatchWord *Rdst,
231 MatchWord *Rsrc1, MatchWord *Rsrc2)
234 for (i = 0; i<mc->n; i++)
235 Rdst[i] = Rsrc1[i] | Rsrc2[i];
238 static INLINE int move (MatchContext *mc, MatchWord *Rj1, MatchWord *Rj,
239 Dict_char ch, struct DFA *dfa, MatchWord *Rtmp,
243 MatchWord *Rtmp_2 = Rtmp + mc->n;
245 mask_shift (mc, Rj1, Rj, dfa, ch);
246 for (d = 1; d <= mc->range; d++)
248 or (mc, Rtmp, Rj, Rj1); /* 2,3 */
250 shift (mc, Rtmp_2, Rtmp, dfa);
252 mask_shift (mc, Rtmp, Rj+mc->n, dfa, ch); /* 1 */
254 or (mc, Rtmp, Rtmp_2, Rtmp); /* 1,2,3*/
258 or (mc, Rj1, Rtmp, Rj); /* 1,2,3,4 */
267 static int dict_grep (Dict dict, Dict_ptr ptr, MatchContext *mc,
268 MatchWord *Rj, int pos, void *client,
269 int (*userfunc)(char *, const char *, void *),
270 Dict_char *prefix, struct DFA *dfa,
271 int *max_pos, int init_pos)
278 dict_bf_readp (dict->dbf, ptr, &p);
280 hi = DICT_nodir(p)-1;
281 indxp = (short*) ((char*) p+DICT_pagesize(dict)-sizeof(short));
287 /* string (Dict_char *) DICT_EOS terminated */
288 /* unsigned char length of information */
289 /* char * information */
292 info = (char*)p + indxp[-lo];
296 MatchWord *Rj0 = Rj + j *mc->fact;
297 MatchWord *Rj1 = Rj + (j+1)*mc->fact;
298 MatchWord *Rj_tmp = Rj + (j+2)*mc->fact;
301 memcpy (&ch, info+j*sizeof(Dict_char), sizeof(Dict_char));
303 if (pos+j > *max_pos)
308 if ((*userfunc)((char*) prefix,
309 info+(j+1)*sizeof(Dict_char), client))
313 if (pos+j >= init_pos)
317 move (mc, Rj1, Rj0, ch, dfa, Rj_tmp, range);
318 for (d = mc->n; --d >= 0; )
319 if (Rj1[range*mc->n + d])
324 for (d = mc->n; --d >= 0; )
325 if (Rj1[range*mc->n + d] & mc->match_mask[d])
334 MatchWord *Rj1 = Rj+ mc->fact;
335 MatchWord *Rj_tmp = Rj+2*mc->fact;
339 /* Dict_ptr subptr */
340 /* Dict_char sub char */
341 /* unsigned char length of information */
342 /* char * information */
343 info = (char*)p - indxp[-lo];
344 memcpy (&ch, info+sizeof(Dict_ptr), sizeof(Dict_char));
353 move (mc, Rj1, Rj, ch, dfa, Rj_tmp, range);
354 for (d = mc->n; --d >= 0; )
355 if (Rj1[range*mc->n + d])
360 if (info[sizeof(Dict_ptr)+sizeof(Dict_char)])
362 for (d = mc->n; --d >= 0; )
363 if (Rj1[range*mc->n + d] & mc->match_mask[d])
365 prefix[pos+1] = DICT_EOS;
366 if ((*userfunc)((char*) prefix,
367 info+sizeof(Dict_ptr)+
368 sizeof(Dict_char), client))
373 memcpy (&subptr, info, sizeof(Dict_ptr));
376 if (dict_grep (dict, subptr, mc, Rj1, pos+1,
377 client, userfunc, prefix, dfa, max_pos,
380 dict_bf_readp (dict->dbf, ptr, &p);
381 indxp = (short*) ((char*) p+DICT_pagesize(dict)
391 int dict_lookup_grep (Dict dict, const char *pattern, int range, void *client,
392 int *max_pos, int init_pos,
393 int (*userfunc)(char *name, const char *info,
397 Dict_char prefix[MAX_LENGTH+1];
398 const char *this_pattern = pattern;
400 struct DFA *dfa = dfa_init();
403 logf (LOG_DEBUG, "dict_lookup_grep '%s' range=%d", pattern, range);
404 i = dfa_parse (dfa, &this_pattern);
405 if (i || *this_pattern)
412 mc = mk_MatchContext (dfa, range);
414 Rj = xcalloc ((MAX_LENGTH+1) * mc->n, sizeof(*Rj));
416 set_bit (mc, Rj, 0, 0);
417 for (d = 1; d<=mc->range; d++)
420 memcpy (Rj + mc->n * d, Rj + mc->n * (d-1), mc->n * sizeof(*Rj));
421 for (s = 0; s<dfa->no_states; s++)
423 if (get_bit (mc, Rj, d-1, s))
425 struct DFA_state *state = dfa->states[s];
426 int i = state->tran_no;
428 set_bit (mc, Rj, d, state->trans[i].to);
433 if (dict->head.last > 1)
434 i = dict_grep (dict, 1, mc, Rj, 0, client, userfunc, prefix,
435 dfa, max_pos, init_pos);
438 logf (LOG_DEBUG, "max_pos = %d", *max_pos);
441 rm_MatchContext (&mc);