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