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