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