1 /* $Id: lookgrep.c,v 1.25 2002-08-02 19:26:55 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
33 typedef unsigned MatchWord;
35 #define MAX_LENGTH 1024
38 int n; /* no of MatchWord needed */
39 int range; /* max no. of errors */
40 int fact; /* (range+1)*n */
41 MatchWord *match_mask; /* match_mask */
46 static INLINE void set_bit (MatchContext *mc, MatchWord *m, int ch, int state)
48 int off = state & (WORD_BITS-1);
49 int wno = state / WORD_BITS;
51 m[mc->n * ch + wno] |= 1<<off;
54 static INLINE MatchWord get_bit (MatchContext *mc, MatchWord *m, int ch,
57 int off = state & (WORD_BITS-1);
58 int wno = state / WORD_BITS;
60 return m[mc->n * ch + wno] & (1<<off);
63 static MatchContext *mk_MatchContext (struct DFA *dfa, int range)
65 MatchContext *mc = (MatchContext *) xmalloc (sizeof(*mc));
68 mc->n = (dfa->no_states+WORD_BITS) / WORD_BITS;
70 mc->fact = (range+1)*mc->n;
71 mc->match_mask = (MatchWord *) xcalloc (mc->n, sizeof(*mc->match_mask));
73 for (s = 0; s<dfa->no_states; s++)
74 if (dfa->states[s]->rule_no)
75 set_bit (mc, mc->match_mask, 0, s);
79 static void rm_MatchContext (MatchContext **mc)
81 xfree ((*mc)->match_mask);
86 static void mask_shift (MatchContext *mc, MatchWord *Rdst, MatchWord *Rsrc,
87 struct DFA *dfa, int ch)
90 MatchWord *Rsrc_p = Rsrc, mask;
92 for (j = 0; j<mc->n; j++)
97 for (j = 0; j<WORD_BITS/4; j++)
103 struct DFA_state *state = dfa->states[s];
104 int i = state->tran_no;
106 if (ch >= state->trans[i].ch[0] &&
107 ch <= state->trans[i].ch[1])
108 set_bit (mc, Rdst, 0, state->trans[i].to);
112 struct DFA_state *state = dfa->states[s+1];
113 int i = state->tran_no;
115 if (ch >= state->trans[i].ch[0] &&
116 ch <= state->trans[i].ch[1])
117 set_bit (mc, Rdst, 0, state->trans[i].to);
121 struct DFA_state *state = dfa->states[s+2];
122 int i = state->tran_no;
124 if (ch >= state->trans[i].ch[0] &&
125 ch <= state->trans[i].ch[1])
126 set_bit (mc, Rdst, 0, state->trans[i].to);
130 struct DFA_state *state = dfa->states[s+3];
131 int i = state->tran_no;
133 if (ch >= state->trans[i].ch[0] &&
134 ch <= state->trans[i].ch[1])
135 set_bit (mc, Rdst, 0, state->trans[i].to);
139 if (s >= dfa->no_states)
146 static void shift (MatchContext *mc, MatchWord *Rdst, MatchWord *Rsrc,
150 MatchWord *Rsrc_p = Rsrc, mask;
151 for (j = 0; j<mc->n; j++)
156 for (j = 0; j<WORD_BITS/4; j++)
162 struct DFA_state *state = dfa->states[s];
163 int i = state->tran_no;
165 set_bit (mc, Rdst, 0, state->trans[i].to);
169 struct DFA_state *state = dfa->states[s+1];
170 int i = state->tran_no;
172 set_bit (mc, Rdst, 0, state->trans[i].to);
176 struct DFA_state *state = dfa->states[s+2];
177 int i = state->tran_no;
179 set_bit (mc, Rdst, 0, state->trans[i].to);
183 struct DFA_state *state = dfa->states[s+3];
184 int i = state->tran_no;
186 set_bit (mc, Rdst, 0, state->trans[i].to);
190 if (s >= dfa->no_states)
197 static void or (MatchContext *mc, MatchWord *Rdst,
198 MatchWord *Rsrc1, MatchWord *Rsrc2)
201 for (i = 0; i<mc->n; i++)
202 Rdst[i] = Rsrc1[i] | Rsrc2[i];
205 static INLINE int move (MatchContext *mc, MatchWord *Rj1, MatchWord *Rj,
206 Dict_char ch, struct DFA *dfa, MatchWord *Rtmp,
210 MatchWord *Rtmp_2 = Rtmp + mc->n;
212 mask_shift (mc, Rj1, Rj, dfa, ch);
213 for (d = 1; d <= mc->range; d++)
215 or (mc, Rtmp, Rj, Rj1); /* 2,3 */
217 shift (mc, Rtmp_2, Rtmp, dfa);
219 mask_shift (mc, Rtmp, Rj+mc->n, dfa, ch); /* 1 */
221 or (mc, Rtmp, Rtmp_2, Rtmp); /* 1,2,3*/
225 or (mc, Rj1, Rtmp, Rj); /* 1,2,3,4 */
234 static int dict_grep (Dict dict, Dict_ptr ptr, MatchContext *mc,
235 MatchWord *Rj, int pos, void *client,
236 int (*userfunc)(char *, const char *, void *),
237 Dict_char *prefix, struct DFA *dfa,
238 int *max_pos, int init_pos)
245 dict_bf_readp (dict->dbf, ptr, &p);
247 hi = DICT_nodir(p)-1;
248 indxp = (short*) ((char*) p+DICT_bsize(p)-sizeof(short));
254 /* string (Dict_char *) DICT_EOS terminated */
255 /* unsigned char length of information */
256 /* char * information */
259 info = (char*)p + indxp[-lo];
263 MatchWord *Rj0 = Rj + j *mc->fact;
264 MatchWord *Rj1 = Rj + (j+1)*mc->fact;
265 MatchWord *Rj_tmp = Rj + (j+2)*mc->fact;
268 memcpy (&ch, info+j*sizeof(Dict_char), sizeof(Dict_char));
270 if (pos+j > *max_pos)
275 if ((*userfunc)((char*) prefix,
276 info+(j+1)*sizeof(Dict_char), client))
280 if (pos+j >= init_pos)
284 move (mc, Rj1, Rj0, ch, dfa, Rj_tmp, range);
285 for (d = mc->n; --d >= 0; )
286 if (Rj1[range*mc->n + d])
291 for (d = mc->n; --d >= 0; )
292 if (Rj1[range*mc->n + d] & mc->match_mask[d])
301 MatchWord *Rj1 = Rj+ mc->fact;
302 MatchWord *Rj_tmp = Rj+2*mc->fact;
306 /* Dict_ptr subptr */
307 /* Dict_char sub char */
308 /* unsigned char length of information */
309 /* char * information */
310 info = (char*)p - indxp[-lo];
311 memcpy (&ch, info+sizeof(Dict_ptr), sizeof(Dict_char));
320 move (mc, Rj1, Rj, ch, dfa, Rj_tmp, range);
321 for (d = mc->n; --d >= 0; )
322 if (Rj1[range*mc->n + d])
327 if (info[sizeof(Dict_ptr)+sizeof(Dict_char)])
329 for (d = mc->n; --d >= 0; )
330 if (Rj1[range*mc->n + d] & mc->match_mask[d])
332 prefix[pos+1] = DICT_EOS;
333 if ((*userfunc)((char*) prefix,
334 info+sizeof(Dict_ptr)+
335 sizeof(Dict_char), client))
340 memcpy (&subptr, info, sizeof(Dict_ptr));
343 if (dict_grep (dict, subptr, mc, Rj1, pos+1,
344 client, userfunc, prefix, dfa, max_pos,
347 dict_bf_readp (dict->dbf, ptr, &p);
348 indxp = (short*) ((char*) p+DICT_bsize(p)-sizeof(short));
357 int dict_lookup_grep (Dict dict, const char *pattern, int range, void *client,
358 int *max_pos, int init_pos,
359 int (*userfunc)(char *name, const char *info,
363 Dict_char prefix[MAX_LENGTH+1];
364 const char *this_pattern = pattern;
366 struct DFA *dfa = dfa_init();
372 debug_dfa_followpos = 1;
376 logf (LOG_DEBUG, "dict_lookup_grep range=%d", range);
377 for (i = 0; pattern[i]; i++)
379 logf (LOG_DEBUG, " %3d %c", pattern[i],
380 (pattern[i] > ' ' && pattern[i] < 127) ? pattern[i] : '?');
383 dfa_set_cmap (dfa, dict->grep_cmap_data, dict->grep_cmap);
385 i = dfa_parse (dfa, &this_pattern);
386 if (i || *this_pattern)
393 mc = mk_MatchContext (dfa, range);
395 Rj = (MatchWord *) xcalloc ((MAX_LENGTH+1) * mc->n, sizeof(*Rj));
397 set_bit (mc, Rj, 0, 0);
398 for (d = 1; d<=mc->range; d++)
401 memcpy (Rj + mc->n * d, Rj + mc->n * (d-1), mc->n * sizeof(*Rj));
402 for (s = 0; s<dfa->no_states; s++)
404 if (get_bit (mc, Rj, d-1, s))
406 struct DFA_state *state = dfa->states[s];
407 int i = state->tran_no;
409 set_bit (mc, Rj, d, state->trans[i].to);
415 i = dict_grep (dict, dict->head.root, mc, Rj, 0, client,
417 dfa, max_pos, init_pos);
420 logf (LOG_DEBUG, "max_pos = %d", *max_pos);
423 rm_MatchContext (&mc);
427 void dict_grep_cmap (Dict dict, void *vp,
428 const char **(*cmap)(void *vp, const char **from, int len))
430 dict->grep_cmap = cmap;
431 dict->grep_cmap_data = vp;