2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.16 1996-05-24 14:46:04 adam
8 * Added dict_grep_cmap function to define user-mapping in grep lookups.
10 * Revision 1.15 1996/03/20 09:35:18 adam
11 * Function dict_lookup_grep got extra parameter, init_pos, which marks
12 * from which position in pattern approximate pattern matching should occur.
14 * Revision 1.14 1996/02/02 13:43:51 adam
15 * The public functions simply use char instead of Dict_char to represent
16 * search strings. Dict_char is used internally only.
18 * Revision 1.13 1996/01/08 09:09:30 adam
19 * Function dfa_parse got 'const' string argument.
21 * Revision 1.12 1995/12/11 09:04:48 adam
22 * Bug fix: the lookup/scan/lookgrep didn't handle empty dictionary.
24 * Revision 1.11 1995/12/06 14:43:02 adam
25 * New function: dict_delete.
27 * Revision 1.10 1995/11/16 17:00:44 adam
30 * Revision 1.9 1995/10/27 13:58:09 adam
31 * Makes 'Database unavailable' diagnostic.
33 * Revision 1.8 1995/10/19 14:57:21 adam
34 * New feature: grep lookup saves length of longest prefix match.
36 * Revision 1.7 1995/10/17 18:01:22 adam
37 * Userfunc may return non-zero in which case the the grepping stops
40 * Revision 1.6 1995/10/09 16:18:32 adam
41 * Function dict_lookup_grep got extra client data parameter.
43 * Revision 1.5 1995/09/14 11:52:59 adam
44 * Grep handle function parameter info is const now.
46 * Revision 1.4 1995/01/24 16:01:02 adam
47 * Added -ansi to CFLAGS.
48 * Use new API of dfa module.
50 * Revision 1.3 1994/10/05 12:16:50 adam
51 * Pagesize is a resource now.
53 * Revision 1.2 1994/10/04 12:08:07 adam
54 * Some bug fixes and some optimizations.
56 * Revision 1.1 1994/10/03 17:23:04 adam
57 * First version of dictionary lookup with regular expressions and errors.
69 typedef unsigned MatchWord;
71 #define MAX_LENGTH 1024
74 int n; /* no of MatchWord needed */
75 int range; /* max no. of errors */
76 int fact; /* (range+1)*n */
77 MatchWord *match_mask; /* match_mask */
82 static INLINE void set_bit (MatchContext *mc, MatchWord *m, int ch, int state)
84 int off = state & (WORD_BITS-1);
85 int wno = state / WORD_BITS;
87 m[mc->n * ch + wno] |= 1<<off;
90 static INLINE MatchWord get_bit (MatchContext *mc, MatchWord *m, int ch,
93 int off = state & (WORD_BITS-1);
94 int wno = state / WORD_BITS;
96 return m[mc->n * ch + wno] & (1<<off);
99 static MatchContext *mk_MatchContext (struct DFA *dfa, int range)
101 MatchContext *mc = xmalloc (sizeof(*mc));
104 mc->n = (dfa->no_states+WORD_BITS) / WORD_BITS;
106 mc->fact = (range+1)*mc->n;
107 mc->match_mask = xcalloc (mc->n, sizeof(*mc->match_mask));
109 for (s = 0; s<dfa->no_states; s++)
110 if (dfa->states[s]->rule_no)
111 set_bit (mc, mc->match_mask, 0, s);
115 static void rm_MatchContext (MatchContext **mc)
117 xfree ((*mc)->match_mask);
122 static void mask_shift (MatchContext *mc, MatchWord *Rdst, MatchWord *Rsrc,
123 struct DFA *dfa, int ch)
126 MatchWord *Rsrc_p = Rsrc, mask;
128 for (j = 0; j<mc->n; j++)
133 for (j = 0; j<WORD_BITS/4; j++)
139 struct DFA_state *state = dfa->states[s];
140 int i = state->tran_no;
142 if (ch >= state->trans[i].ch[0] &&
143 ch <= state->trans[i].ch[1])
144 set_bit (mc, Rdst, 0, state->trans[i].to);
148 struct DFA_state *state = dfa->states[s+1];
149 int i = state->tran_no;
151 if (ch >= state->trans[i].ch[0] &&
152 ch <= state->trans[i].ch[1])
153 set_bit (mc, Rdst, 0, state->trans[i].to);
157 struct DFA_state *state = dfa->states[s+2];
158 int i = state->tran_no;
160 if (ch >= state->trans[i].ch[0] &&
161 ch <= state->trans[i].ch[1])
162 set_bit (mc, Rdst, 0, state->trans[i].to);
166 struct DFA_state *state = dfa->states[s+3];
167 int i = state->tran_no;
169 if (ch >= state->trans[i].ch[0] &&
170 ch <= state->trans[i].ch[1])
171 set_bit (mc, Rdst, 0, state->trans[i].to);
175 if (s >= dfa->no_states)
182 static void shift (MatchContext *mc, MatchWord *Rdst, MatchWord *Rsrc,
186 MatchWord *Rsrc_p = Rsrc, mask;
187 for (j = 0; j<mc->n; j++)
192 for (j = 0; j<WORD_BITS/4; j++)
198 struct DFA_state *state = dfa->states[s];
199 int i = state->tran_no;
201 set_bit (mc, Rdst, 0, state->trans[i].to);
205 struct DFA_state *state = dfa->states[s+1];
206 int i = state->tran_no;
208 set_bit (mc, Rdst, 0, state->trans[i].to);
212 struct DFA_state *state = dfa->states[s+2];
213 int i = state->tran_no;
215 set_bit (mc, Rdst, 0, state->trans[i].to);
219 struct DFA_state *state = dfa->states[s+3];
220 int i = state->tran_no;
222 set_bit (mc, Rdst, 0, state->trans[i].to);
226 if (s >= dfa->no_states)
233 static void or (MatchContext *mc, MatchWord *Rdst,
234 MatchWord *Rsrc1, MatchWord *Rsrc2)
237 for (i = 0; i<mc->n; i++)
238 Rdst[i] = Rsrc1[i] | Rsrc2[i];
241 static INLINE int move (MatchContext *mc, MatchWord *Rj1, MatchWord *Rj,
242 Dict_char ch, struct DFA *dfa, MatchWord *Rtmp,
246 MatchWord *Rtmp_2 = Rtmp + mc->n;
248 mask_shift (mc, Rj1, Rj, dfa, ch);
249 for (d = 1; d <= mc->range; d++)
251 or (mc, Rtmp, Rj, Rj1); /* 2,3 */
253 shift (mc, Rtmp_2, Rtmp, dfa);
255 mask_shift (mc, Rtmp, Rj+mc->n, dfa, ch); /* 1 */
257 or (mc, Rtmp, Rtmp_2, Rtmp); /* 1,2,3*/
261 or (mc, Rj1, Rtmp, Rj); /* 1,2,3,4 */
270 static int dict_grep (Dict dict, Dict_ptr ptr, MatchContext *mc,
271 MatchWord *Rj, int pos, void *client,
272 int (*userfunc)(char *, const char *, void *),
273 Dict_char *prefix, struct DFA *dfa,
274 int *max_pos, int init_pos)
281 dict_bf_readp (dict->dbf, ptr, &p);
283 hi = DICT_nodir(p)-1;
284 indxp = (short*) ((char*) p+DICT_pagesize(dict)-sizeof(short));
290 /* string (Dict_char *) DICT_EOS terminated */
291 /* unsigned char length of information */
292 /* char * information */
295 info = (char*)p + indxp[-lo];
299 MatchWord *Rj0 = Rj + j *mc->fact;
300 MatchWord *Rj1 = Rj + (j+1)*mc->fact;
301 MatchWord *Rj_tmp = Rj + (j+2)*mc->fact;
304 memcpy (&ch, info+j*sizeof(Dict_char), sizeof(Dict_char));
306 if (pos+j > *max_pos)
311 if ((*userfunc)((char*) prefix,
312 info+(j+1)*sizeof(Dict_char), client))
316 if (pos+j >= init_pos)
320 move (mc, Rj1, Rj0, ch, dfa, Rj_tmp, range);
321 for (d = mc->n; --d >= 0; )
322 if (Rj1[range*mc->n + d])
327 for (d = mc->n; --d >= 0; )
328 if (Rj1[range*mc->n + d] & mc->match_mask[d])
337 MatchWord *Rj1 = Rj+ mc->fact;
338 MatchWord *Rj_tmp = Rj+2*mc->fact;
342 /* Dict_ptr subptr */
343 /* Dict_char sub char */
344 /* unsigned char length of information */
345 /* char * information */
346 info = (char*)p - indxp[-lo];
347 memcpy (&ch, info+sizeof(Dict_ptr), sizeof(Dict_char));
356 move (mc, Rj1, Rj, ch, dfa, Rj_tmp, range);
357 for (d = mc->n; --d >= 0; )
358 if (Rj1[range*mc->n + d])
363 if (info[sizeof(Dict_ptr)+sizeof(Dict_char)])
365 for (d = mc->n; --d >= 0; )
366 if (Rj1[range*mc->n + d] & mc->match_mask[d])
368 prefix[pos+1] = DICT_EOS;
369 if ((*userfunc)((char*) prefix,
370 info+sizeof(Dict_ptr)+
371 sizeof(Dict_char), client))
376 memcpy (&subptr, info, sizeof(Dict_ptr));
379 if (dict_grep (dict, subptr, mc, Rj1, pos+1,
380 client, userfunc, prefix, dfa, max_pos,
383 dict_bf_readp (dict->dbf, ptr, &p);
384 indxp = (short*) ((char*) p+DICT_pagesize(dict)
394 int dict_lookup_grep (Dict dict, const char *pattern, int range, void *client,
395 int *max_pos, int init_pos,
396 int (*userfunc)(char *name, const char *info,
400 Dict_char prefix[MAX_LENGTH+1];
401 const char *this_pattern = pattern;
403 struct DFA *dfa = dfa_init();
406 logf (LOG_DEBUG, "dict_lookup_grep '%s' range=%d", pattern, range);
407 i = dfa_parse (dfa, &this_pattern);
408 if (i || *this_pattern)
415 mc = mk_MatchContext (dfa, range);
417 Rj = xcalloc ((MAX_LENGTH+1) * mc->n, sizeof(*Rj));
419 set_bit (mc, Rj, 0, 0);
420 for (d = 1; d<=mc->range; d++)
423 memcpy (Rj + mc->n * d, Rj + mc->n * (d-1), mc->n * sizeof(*Rj));
424 for (s = 0; s<dfa->no_states; s++)
426 if (get_bit (mc, Rj, d-1, s))
428 struct DFA_state *state = dfa->states[s];
429 int i = state->tran_no;
431 set_bit (mc, Rj, d, state->trans[i].to);
436 if (dict->head.last > 1)
437 i = dict_grep (dict, 1, mc, Rj, 0, client, userfunc, prefix,
438 dfa, max_pos, init_pos);
441 logf (LOG_DEBUG, "max_pos = %d", *max_pos);
444 rm_MatchContext (&mc);
448 void dict_grep_cmap (Dict dict, char **(*cmap)(const char **from))
450 dict->grep_cmap = cmap;