dc922695e93aca4448eb4162840473497d3a35ed
[idzebra-moved-to-github.git] / dict / lookgrep.c
1 /*
2  * Copyright (C) 1994, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: lookgrep.c,v $
7  * Revision 1.8  1995-10-19 14:57:21  adam
8  * New feature: grep lookup saves length of longest prefix match.
9  *
10  * Revision 1.7  1995/10/17  18:01:22  adam
11  * Userfunc may return non-zero in which case the the grepping stops
12  * immediately.
13  *
14  * Revision 1.6  1995/10/09  16:18:32  adam
15  * Function dict_lookup_grep got extra client data parameter.
16  *
17  * Revision 1.5  1995/09/14  11:52:59  adam
18  * Grep handle function parameter info is const now.
19  *
20  * Revision 1.4  1995/01/24  16:01:02  adam
21  * Added -ansi to CFLAGS.
22  * Use new API of dfa module.
23  *
24  * Revision 1.3  1994/10/05  12:16:50  adam
25  * Pagesize is a resource now.
26  *
27  * Revision 1.2  1994/10/04  12:08:07  adam
28  * Some bug fixes and some optimizations.
29  *
30  * Revision 1.1  1994/10/03  17:23:04  adam
31  * First version of dictionary lookup with regular expressions and errors.
32  *
33  */
34
35 #include <stdlib.h>
36 #include <string.h>
37 #include <stdio.h>
38 #include <assert.h>
39
40 #include <dfa.h>
41 #include <dict.h>
42
43 typedef unsigned MatchWord;
44 #define WORD_BITS 32
45 #define MAX_LENGTH 1024
46
47 typedef struct {
48   int n;                 /* no of MatchWord needed */
49   int range;             /* max no. of errors */
50   int fact;              /* (range+1)*n */
51   MatchWord *match_mask; /* match_mask */
52 } MatchContext;
53
54 #define INLINE 
55
56 static INLINE void set_bit (MatchContext *mc, MatchWord *m, int ch, int state)
57 {
58     int off = state & (WORD_BITS-1);
59     int wno = state / WORD_BITS;
60   
61     m[mc->n * ch + wno] |= 1<<off;
62 }
63
64 static INLINE MatchWord get_bit (MatchContext *mc, MatchWord *m, int ch,
65                                  int state)
66 {
67     int off = state & (WORD_BITS-1);
68     int wno = state / WORD_BITS;
69
70     return m[mc->n * ch + wno] & (1<<off);
71 }
72
73 static MatchContext *mk_MatchContext (struct DFA *dfa, int range)
74 {
75     MatchContext *mc = xmalloc (sizeof(*mc));
76     int s;
77
78     mc->n = (dfa->no_states+WORD_BITS) / WORD_BITS;
79     mc->range = range;
80     mc->fact = (range+1)*mc->n;
81     mc->match_mask = xcalloc (mc->n, sizeof(*mc->match_mask));
82
83     for (s = 0; s<dfa->no_states; s++)
84         if (dfa->states[s]->rule_no)
85             set_bit (mc, mc->match_mask, 0, s);
86     return mc;
87 }
88
89 static void rm_MatchContext (MatchContext **mc)
90 {
91     xfree ((*mc)->match_mask);
92     xfree (*mc);
93     *mc = NULL;
94 }
95
96 static void mask_shift (MatchContext *mc, MatchWord *Rdst, MatchWord *Rsrc,
97                    struct DFA *dfa, int ch)
98 {
99     int j, s = 0;
100     MatchWord *Rsrc_p = Rsrc, mask;
101
102     for (j = 0; j<mc->n; j++)
103         Rdst[j] = 0;
104     while (1)
105     {
106         mask = *Rsrc_p++;
107         for (j = 0; j<WORD_BITS/4; j++)
108         {
109             if (mask & 15)
110             {
111                 if (mask & 1)
112                 {
113                     struct DFA_state *state = dfa->states[s];
114                     int i = state->tran_no;
115                     while (--i >= 0)
116                         if (ch >= state->trans[i].ch[0] &&
117                             ch <= state->trans[i].ch[1])
118                             set_bit (mc, Rdst, 0, state->trans[i].to);
119                 }
120                 if (mask & 2)
121                 {
122                     struct DFA_state *state = dfa->states[s+1];
123                     int i = state->tran_no;
124                     while (--i >= 0)
125                         if (ch >= state->trans[i].ch[0] &&
126                             ch <= state->trans[i].ch[1])
127                             set_bit (mc, Rdst, 0, state->trans[i].to);
128                 }
129                 if (mask & 4)
130                 {
131                     struct DFA_state *state = dfa->states[s+2];
132                     int i = state->tran_no;
133                     while (--i >= 0)
134                         if (ch >= state->trans[i].ch[0] &&
135                             ch <= state->trans[i].ch[1])
136                             set_bit (mc, Rdst, 0, state->trans[i].to);
137                 }
138                 if (mask & 8)
139                 {
140                     struct DFA_state *state = dfa->states[s+3];
141                     int i = state->tran_no;
142                     while (--i >= 0)
143                         if (ch >= state->trans[i].ch[0] &&
144                             ch <= state->trans[i].ch[1])
145                             set_bit (mc, Rdst, 0, state->trans[i].to);
146                 }
147             }
148             s += 4;
149             if (s >= dfa->no_states)
150                 return;
151             mask >>= 4;
152         }
153     }
154 }
155
156 static void shift (MatchContext *mc, MatchWord *Rdst, MatchWord *Rsrc,
157                    struct DFA *dfa)
158 {
159     int j, s = 0;
160     MatchWord *Rsrc_p = Rsrc, mask;
161     for (j = 0; j<mc->n; j++)
162         Rdst[j] = 0;
163     while (1)
164     {
165         mask = *Rsrc_p++;
166         for (j = 0; j<WORD_BITS/4; j++)
167         {
168             if (mask & 15)
169             {
170                 if (mask & 1)
171                 {
172                     struct DFA_state *state = dfa->states[s];
173                     int i = state->tran_no;
174                     while (--i >= 0)
175                         set_bit (mc, Rdst, 0, state->trans[i].to);
176                 }
177                 if (mask & 2)
178                 {
179                     struct DFA_state *state = dfa->states[s+1];
180                     int i = state->tran_no;
181                     while (--i >= 0)
182                         set_bit (mc, Rdst, 0, state->trans[i].to);
183                 }
184                 if (mask & 4)
185                 {
186                     struct DFA_state *state = dfa->states[s+2];
187                     int i = state->tran_no;
188                     while (--i >= 0)
189                         set_bit (mc, Rdst, 0, state->trans[i].to);
190                 }
191                 if (mask & 8)
192                 {
193                     struct DFA_state *state = dfa->states[s+3];
194                     int i = state->tran_no;
195                     while (--i >= 0)
196                         set_bit (mc, Rdst, 0, state->trans[i].to);
197                 }
198             }
199             s += 4;
200             if (s >= dfa->no_states)
201                 return;
202             mask >>= 4;
203         }
204     }
205 }
206
207 static void or (MatchContext *mc, MatchWord *Rdst,
208                 MatchWord *Rsrc1, MatchWord *Rsrc2)
209 {
210     int i;
211     for (i = 0; i<mc->n; i++)
212         Rdst[i] = Rsrc1[i] | Rsrc2[i];
213 }
214
215 static INLINE int move (MatchContext *mc, MatchWord *Rj1, MatchWord *Rj,
216                  Dict_char ch, struct DFA *dfa, MatchWord *Rtmp)
217 {
218     int d;
219     MatchWord *Rtmp_2 = Rtmp + mc->n;
220
221     mask_shift (mc, Rj1, Rj, dfa, ch);
222     for (d = 1; d <= mc->range; d++)
223     {
224         or (mc, Rtmp, Rj, Rj1);                         /* 2,3 */
225         
226         shift (mc, Rtmp_2, Rtmp, dfa);
227
228         mask_shift (mc, Rtmp, Rj+mc->n, dfa, ch);      /* 1 */
229                 
230         or (mc, Rtmp, Rtmp_2, Rtmp);                    /* 1,2,3*/
231
232         Rj1 += mc->n;
233         
234         or (mc, Rj1, Rtmp, Rj);                         /* 1,2,3,4 */
235
236         Rj += mc->n;
237     }
238     return 1;
239
240 }
241
242
243 static int dict_grep (Dict dict, Dict_ptr ptr, MatchContext *mc,
244                       MatchWord *Rj, int pos, void *client,
245                       int (*userfunc)(Dict_char *, const char *, void *),
246                       Dict_char *prefix, struct DFA *dfa,
247                       int *max_pos)
248 {
249     int lo, hi, d;
250     void *p;
251     short *indxp;
252     char *info;
253
254     dict_bf_readp (dict->dbf, ptr, &p);
255     lo = 0;
256     hi = DICT_nodir(p)-1;
257     indxp = (short*) ((char*) p+DICT_pagesize(dict)-sizeof(short));    
258
259     while (lo <= hi)
260     {
261         if (indxp[-lo] > 0)
262         {
263             /* string (Dict_char *) DICT_EOS terminated */
264             /* unsigned char        length of information */
265             /* char *               information */
266             int j;
267             int was_match = 0;
268             info = (char*)p + indxp[-lo];
269             for (j=0; ; j++)
270             {
271                 Dict_char ch;
272                 MatchWord *Rj0 =    Rj + j    *mc->fact;
273                 MatchWord *Rj1 =    Rj + (j+1)*mc->fact;
274                 MatchWord *Rj_tmp = Rj + (j+2)*mc->fact;
275
276                 memcpy (&ch, info+j*sizeof(Dict_char), sizeof(Dict_char));
277                 prefix[pos+j] = ch;
278                 if (pos+j > *max_pos)
279                     *max_pos = pos+j;
280                 if (ch == DICT_EOS)
281                 {
282                     if (was_match)
283                         if ((*userfunc)(prefix, info+(j+1)*sizeof(Dict_char),
284                                         client))
285                             return 1;
286                     break;
287                 }
288                 move (mc, Rj1, Rj0, ch, dfa, Rj_tmp);
289                 for (d = mc->n; --d >= 0; )
290                     if (Rj1[mc->range*mc->n + d])
291                         break;
292                 if (d < 0)
293                     break;
294                 was_match = 0;
295                 for (d = mc->n; --d >= 0; )
296                     if (Rj1[mc->range*mc->n + d] & mc->match_mask[d])
297                     {
298                         was_match = 1;
299                         break;
300                     }
301             }
302         }
303         else
304         {
305             MatchWord *Rj1 =    Rj+  mc->fact;
306             MatchWord *Rj_tmp = Rj+2*mc->fact;
307             Dict_char ch;
308
309             /* Dict_ptr             subptr */
310             /* Dict_char            sub char */
311             /* unsigned char        length of information */
312             /* char *               information */
313             info = (char*)p - indxp[-lo];
314             memcpy (&ch, info+sizeof(Dict_ptr), sizeof(Dict_char));
315             prefix[pos] = ch;
316             
317             if (pos > *max_pos)
318                 *max_pos = pos;
319             move (mc, Rj1, Rj, ch, dfa, Rj_tmp);
320             for (d = mc->n; --d >= 0; )
321                 if (Rj1[mc->range*mc->n + d])
322                     break;
323             if (d >= 0)
324             {
325                 Dict_ptr subptr;
326                 if (info[sizeof(Dict_ptr)+sizeof(Dict_char)])
327                 {
328                     for (d = mc->n; --d >= 0; )
329                         if (Rj1[mc->range*mc->n + d] & mc->match_mask[d])
330                         {
331                             prefix[pos+1] = DICT_EOS;
332                             if ((*userfunc)(prefix, info+sizeof(Dict_ptr)+
333                                             sizeof(Dict_char), client))
334                                 return 1;
335                             break;
336                         }
337                 }
338                 memcpy (&subptr, info, sizeof(Dict_ptr));
339                 if (subptr)
340                 {
341                     if (dict_grep (dict, subptr, mc, Rj1, pos+1,
342                                    client, userfunc, prefix, dfa, max_pos))
343                         return 1;
344                     dict_bf_readp (dict->dbf, ptr, &p);
345                     indxp = (short*) ((char*) p+DICT_pagesize(dict)
346                                       -sizeof(short));
347                 }
348             }
349         }
350         lo++;
351     }
352     return 0;
353 }
354
355 int dict_lookup_grep (Dict dict, Dict_char *pattern, int range, void *client,
356                       int (*userfunc)(Dict_char *name, const char *info,
357                                       void *client))
358 {
359     MatchWord *Rj;
360     Dict_char prefix[MAX_LENGTH+1];
361     char *this_pattern = pattern;
362     MatchContext *mc;
363     struct DFA *dfa = dfa_init();
364     int i, d, max_pos;
365
366     i = dfa_parse (dfa, &this_pattern);
367     if (i || *this_pattern)
368     {
369         dfa_delete (&dfa);
370         return -1;
371     }
372     dfa_mkstate (dfa);
373
374     mc = mk_MatchContext (dfa, range);
375
376     Rj = xcalloc ((MAX_LENGTH+1) * mc->n, sizeof(*Rj));
377
378     max_pos = 0;
379     set_bit (mc, Rj, 0, 0);
380     for (d = 1; d<=mc->range; d++)
381     {
382         int s;
383         memcpy (Rj + mc->n * d, Rj + mc->n * (d-1), mc->n * sizeof(*Rj));
384         for (s = 0; s<dfa->no_states; s++)
385         {
386             if (get_bit (mc, Rj, d-1, s))
387             {
388                 struct DFA_state *state = dfa->states[s];
389                 int i = state->tran_no;
390                 while (--i >= 0)
391                     set_bit (mc, Rj, d, state->trans[i].to);
392             }
393         }
394     }
395     i = dict_grep (dict, 1, mc, Rj, 0, client, userfunc, prefix, dfa,
396                    &max_pos);
397     logf (LOG_DEBUG, "max_pos = %d", max_pos);
398     dfa_delete (&dfa);
399     xfree (Rj);
400     rm_MatchContext (&mc);
401     return i;
402 }