2 * Copyright (C) 1994-1996, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.17 1996-06-04 10:20:06 adam
8 * Added support for character mapping.
10 * Revision 1.16 1996/05/24 14:46:04 adam
11 * Added dict_grep_cmap function to define user-mapping in grep lookups.
13 * Revision 1.15 1996/03/20 09:35:18 adam
14 * Function dict_lookup_grep got extra parameter, init_pos, which marks
15 * from which position in pattern approximate pattern matching should occur.
17 * Revision 1.14 1996/02/02 13:43:51 adam
18 * The public functions simply use char instead of Dict_char to represent
19 * search strings. Dict_char is used internally only.
21 * Revision 1.13 1996/01/08 09:09:30 adam
22 * Function dfa_parse got 'const' string argument.
24 * Revision 1.12 1995/12/11 09:04:48 adam
25 * Bug fix: the lookup/scan/lookgrep didn't handle empty dictionary.
27 * Revision 1.11 1995/12/06 14:43:02 adam
28 * New function: dict_delete.
30 * Revision 1.10 1995/11/16 17:00:44 adam
33 * Revision 1.9 1995/10/27 13:58:09 adam
34 * Makes 'Database unavailable' diagnostic.
36 * Revision 1.8 1995/10/19 14:57:21 adam
37 * New feature: grep lookup saves length of longest prefix match.
39 * Revision 1.7 1995/10/17 18:01:22 adam
40 * Userfunc may return non-zero in which case the the grepping stops
43 * Revision 1.6 1995/10/09 16:18:32 adam
44 * Function dict_lookup_grep got extra client data parameter.
46 * Revision 1.5 1995/09/14 11:52:59 adam
47 * Grep handle function parameter info is const now.
49 * Revision 1.4 1995/01/24 16:01:02 adam
50 * Added -ansi to CFLAGS.
51 * Use new API of dfa module.
53 * Revision 1.3 1994/10/05 12:16:50 adam
54 * Pagesize is a resource now.
56 * Revision 1.2 1994/10/04 12:08:07 adam
57 * Some bug fixes and some optimizations.
59 * Revision 1.1 1994/10/03 17:23:04 adam
60 * First version of dictionary lookup with regular expressions and errors.
72 typedef unsigned MatchWord;
74 #define MAX_LENGTH 1024
77 int n; /* no of MatchWord needed */
78 int range; /* max no. of errors */
79 int fact; /* (range+1)*n */
80 MatchWord *match_mask; /* match_mask */
85 static INLINE void set_bit (MatchContext *mc, MatchWord *m, int ch, int state)
87 int off = state & (WORD_BITS-1);
88 int wno = state / WORD_BITS;
90 m[mc->n * ch + wno] |= 1<<off;
93 static INLINE MatchWord get_bit (MatchContext *mc, MatchWord *m, int ch,
96 int off = state & (WORD_BITS-1);
97 int wno = state / WORD_BITS;
99 return m[mc->n * ch + wno] & (1<<off);
102 static MatchContext *mk_MatchContext (struct DFA *dfa, int range)
104 MatchContext *mc = xmalloc (sizeof(*mc));
107 mc->n = (dfa->no_states+WORD_BITS) / WORD_BITS;
109 mc->fact = (range+1)*mc->n;
110 mc->match_mask = xcalloc (mc->n, sizeof(*mc->match_mask));
112 for (s = 0; s<dfa->no_states; s++)
113 if (dfa->states[s]->rule_no)
114 set_bit (mc, mc->match_mask, 0, s);
118 static void rm_MatchContext (MatchContext **mc)
120 xfree ((*mc)->match_mask);
125 static void mask_shift (MatchContext *mc, MatchWord *Rdst, MatchWord *Rsrc,
126 struct DFA *dfa, int ch)
129 MatchWord *Rsrc_p = Rsrc, mask;
131 for (j = 0; j<mc->n; j++)
136 for (j = 0; j<WORD_BITS/4; j++)
142 struct DFA_state *state = dfa->states[s];
143 int i = state->tran_no;
145 if (ch >= state->trans[i].ch[0] &&
146 ch <= state->trans[i].ch[1])
147 set_bit (mc, Rdst, 0, state->trans[i].to);
151 struct DFA_state *state = dfa->states[s+1];
152 int i = state->tran_no;
154 if (ch >= state->trans[i].ch[0] &&
155 ch <= state->trans[i].ch[1])
156 set_bit (mc, Rdst, 0, state->trans[i].to);
160 struct DFA_state *state = dfa->states[s+2];
161 int i = state->tran_no;
163 if (ch >= state->trans[i].ch[0] &&
164 ch <= state->trans[i].ch[1])
165 set_bit (mc, Rdst, 0, state->trans[i].to);
169 struct DFA_state *state = dfa->states[s+3];
170 int i = state->tran_no;
172 if (ch >= state->trans[i].ch[0] &&
173 ch <= state->trans[i].ch[1])
174 set_bit (mc, Rdst, 0, state->trans[i].to);
178 if (s >= dfa->no_states)
185 static void shift (MatchContext *mc, MatchWord *Rdst, MatchWord *Rsrc,
189 MatchWord *Rsrc_p = Rsrc, mask;
190 for (j = 0; j<mc->n; j++)
195 for (j = 0; j<WORD_BITS/4; j++)
201 struct DFA_state *state = dfa->states[s];
202 int i = state->tran_no;
204 set_bit (mc, Rdst, 0, state->trans[i].to);
208 struct DFA_state *state = dfa->states[s+1];
209 int i = state->tran_no;
211 set_bit (mc, Rdst, 0, state->trans[i].to);
215 struct DFA_state *state = dfa->states[s+2];
216 int i = state->tran_no;
218 set_bit (mc, Rdst, 0, state->trans[i].to);
222 struct DFA_state *state = dfa->states[s+3];
223 int i = state->tran_no;
225 set_bit (mc, Rdst, 0, state->trans[i].to);
229 if (s >= dfa->no_states)
236 static void or (MatchContext *mc, MatchWord *Rdst,
237 MatchWord *Rsrc1, MatchWord *Rsrc2)
240 for (i = 0; i<mc->n; i++)
241 Rdst[i] = Rsrc1[i] | Rsrc2[i];
244 static INLINE int move (MatchContext *mc, MatchWord *Rj1, MatchWord *Rj,
245 Dict_char ch, struct DFA *dfa, MatchWord *Rtmp,
249 MatchWord *Rtmp_2 = Rtmp + mc->n;
251 mask_shift (mc, Rj1, Rj, dfa, ch);
252 for (d = 1; d <= mc->range; d++)
254 or (mc, Rtmp, Rj, Rj1); /* 2,3 */
256 shift (mc, Rtmp_2, Rtmp, dfa);
258 mask_shift (mc, Rtmp, Rj+mc->n, dfa, ch); /* 1 */
260 or (mc, Rtmp, Rtmp_2, Rtmp); /* 1,2,3*/
264 or (mc, Rj1, Rtmp, Rj); /* 1,2,3,4 */
273 static int dict_grep (Dict dict, Dict_ptr ptr, MatchContext *mc,
274 MatchWord *Rj, int pos, void *client,
275 int (*userfunc)(char *, const char *, void *),
276 Dict_char *prefix, struct DFA *dfa,
277 int *max_pos, int init_pos)
284 dict_bf_readp (dict->dbf, ptr, &p);
286 hi = DICT_nodir(p)-1;
287 indxp = (short*) ((char*) p+DICT_pagesize(dict)-sizeof(short));
293 /* string (Dict_char *) DICT_EOS terminated */
294 /* unsigned char length of information */
295 /* char * information */
298 info = (char*)p + indxp[-lo];
302 MatchWord *Rj0 = Rj + j *mc->fact;
303 MatchWord *Rj1 = Rj + (j+1)*mc->fact;
304 MatchWord *Rj_tmp = Rj + (j+2)*mc->fact;
307 memcpy (&ch, info+j*sizeof(Dict_char), sizeof(Dict_char));
309 if (pos+j > *max_pos)
314 if ((*userfunc)((char*) prefix,
315 info+(j+1)*sizeof(Dict_char), client))
319 if (pos+j >= init_pos)
323 move (mc, Rj1, Rj0, ch, dfa, Rj_tmp, range);
324 for (d = mc->n; --d >= 0; )
325 if (Rj1[range*mc->n + d])
330 for (d = mc->n; --d >= 0; )
331 if (Rj1[range*mc->n + d] & mc->match_mask[d])
340 MatchWord *Rj1 = Rj+ mc->fact;
341 MatchWord *Rj_tmp = Rj+2*mc->fact;
345 /* Dict_ptr subptr */
346 /* Dict_char sub char */
347 /* unsigned char length of information */
348 /* char * information */
349 info = (char*)p - indxp[-lo];
350 memcpy (&ch, info+sizeof(Dict_ptr), sizeof(Dict_char));
359 move (mc, Rj1, Rj, ch, dfa, Rj_tmp, range);
360 for (d = mc->n; --d >= 0; )
361 if (Rj1[range*mc->n + d])
366 if (info[sizeof(Dict_ptr)+sizeof(Dict_char)])
368 for (d = mc->n; --d >= 0; )
369 if (Rj1[range*mc->n + d] & mc->match_mask[d])
371 prefix[pos+1] = DICT_EOS;
372 if ((*userfunc)((char*) prefix,
373 info+sizeof(Dict_ptr)+
374 sizeof(Dict_char), client))
379 memcpy (&subptr, info, sizeof(Dict_ptr));
382 if (dict_grep (dict, subptr, mc, Rj1, pos+1,
383 client, userfunc, prefix, dfa, max_pos,
386 dict_bf_readp (dict->dbf, ptr, &p);
387 indxp = (short*) ((char*) p+DICT_pagesize(dict)
397 int dict_lookup_grep (Dict dict, const char *pattern, int range, void *client,
398 int *max_pos, int init_pos,
399 int (*userfunc)(char *name, const char *info,
403 Dict_char prefix[MAX_LENGTH+1];
404 const char *this_pattern = pattern;
406 struct DFA *dfa = dfa_init();
409 logf (LOG_DEBUG, "dict_lookup_grep '%s' range=%d", pattern, range);
411 dfa_set_cmap (dfa, dict->grep_cmap);
413 i = dfa_parse (dfa, &this_pattern);
414 if (i || *this_pattern)
421 mc = mk_MatchContext (dfa, range);
423 Rj = xcalloc ((MAX_LENGTH+1) * mc->n, sizeof(*Rj));
425 set_bit (mc, Rj, 0, 0);
426 for (d = 1; d<=mc->range; d++)
429 memcpy (Rj + mc->n * d, Rj + mc->n * (d-1), mc->n * sizeof(*Rj));
430 for (s = 0; s<dfa->no_states; s++)
432 if (get_bit (mc, Rj, d-1, s))
434 struct DFA_state *state = dfa->states[s];
435 int i = state->tran_no;
437 set_bit (mc, Rj, d, state->trans[i].to);
442 if (dict->head.last > 1)
443 i = dict_grep (dict, 1, mc, Rj, 0, client, userfunc, prefix,
444 dfa, max_pos, init_pos);
447 logf (LOG_DEBUG, "max_pos = %d", *max_pos);
450 rm_MatchContext (&mc);
454 void dict_grep_cmap (Dict dict, char **(*cmap)(const char **from, int len))
456 dict->grep_cmap = cmap;