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