c9a4ef29a2f616ca784d99ed081e89bb959a18fb
[idzebra-moved-to-github.git] / index / zrpn.c
1 /*
2  * Copyright (C) 1995-1999, Index Data
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: zrpn.c,v $
7  * Revision 1.93  1999-06-17 14:38:40  adam
8  * Bug fix: Scan SEGV'ed when getting unknown use attribute.
9  *
10  * Revision 1.92  1999/05/26 07:49:13  adam
11  * C++ compilation.
12  *
13  * Revision 1.91  1999/02/02 14:51:13  adam
14  * Updated WIN32 code specific sections. Changed header.
15  *
16  * Revision 1.90  1998/11/16 16:03:43  adam
17  * Moved loggin utilities to Yaz. Was implemented in file zlogs.c.
18  *
19  * Revision 1.89  1998/11/16 10:11:55  adam
20  * Added addtional info for error 114 - unsupported use attribute.
21  *
22  * Revision 1.88  1998/10/18 07:54:52  adam
23  * Additional info added for diagnostics 114 (Unsupported use attribute) and
24  * 121 (Unsupported attribute set).
25  *
26  * Revision 1.87  1998/09/28 11:19:12  adam
27  * Fix for Compiled ASN.1.
28  *
29  * Revision 1.86  1998/09/22 10:48:20  adam
30  * Minor changes in search API.
31  *
32  * Revision 1.85  1998/09/22 10:03:43  adam
33  * Changed result sets to be persistent in the sense that they can
34  * be re-searched if needed.
35  * Fixed memory leak in rsm_or.
36  *
37  * Revision 1.84  1998/09/18 12:41:00  adam
38  * Fixed bug with numerical relations.
39  *
40  * Revision 1.83  1998/09/02 13:53:19  adam
41  * Extra parameter decode added to search routines to implement
42  * persistent queries.
43  *
44  * Revision 1.82  1998/06/26 11:16:40  quinn
45  * Added support (un-optimised) for left and left/right truncation
46  *
47  * Revision 1.81  1998/06/24 12:16:14  adam
48  * Support for relations on text operands. Open range support in
49  * DFA module (i.e. [-j], [g-]).
50  *
51  * Revision 1.80  1998/06/23 15:33:34  adam
52  * Added feature to specify sort criteria in query (type 7 specifies
53  * sort flags).
54  *
55  * Revision 1.79  1998/06/22 11:35:09  adam
56  * Minor changes.
57  *
58  * Revision 1.78  1998/06/08 14:43:17  adam
59  * Added suport for EXPLAIN Proxy servers - added settings databasePath
60  * and explainDatabase to facilitate this. Increased maximum number
61  * of databases and attributes in one register.
62  *
63  * Revision 1.77  1998/05/20 10:12:22  adam
64  * Implemented automatic EXPLAIN database maintenance.
65  * Modified Zebra to work with ASN.1 compiled version of YAZ.
66  *
67  * Revision 1.76  1998/04/02 14:35:29  adam
68  * First version of Zebra that works with compiled ASN.1.
69  *
70  * Revision 1.75  1998/03/05 08:45:13  adam
71  * New result set model and modular ranking system. Moved towards
72  * descent server API. System information stored as "SGML" records.
73  *
74  * Revision 1.74  1998/02/10 12:03:06  adam
75  * Implemented Sort.
76  *
77  * Revision 1.73  1998/01/29 13:40:11  adam
78  * Better logging for scan service.
79  *
80  * Revision 1.72  1998/01/07 13:53:41  adam
81  * Queries using simple ranked operands returns right number of hits.
82  *
83  * Revision 1.71  1997/12/18 10:54:24  adam
84  * New method result set method rs_hits that returns the number of
85  * hits in result-set (if known). The ranked result set returns real
86  * number of hits but only when not combined with other operands.
87  *
88  * Revision 1.70  1997/10/31 12:34:43  adam
89  * Changed a few log statements.
90  *
91  * Revision 1.69  1997/10/29 12:05:02  adam
92  * Server produces diagnostic "Unsupported Attribute Set" when appropriate.
93  *
94  * Revision 1.68  1997/10/27 14:33:06  adam
95  * Moved towards generic character mapping depending on "structure"
96  * field in abstract syntax file. Fixed a few memory leaks. Fixed
97  * bug with negative integers when doing searches with relational
98  * operators.
99  *
100  * Revision 1.67  1997/09/29 09:06:10  adam
101  * Removed one static var in order to make this module thread safe.
102  *
103  * Revision 1.66  1997/09/25 14:58:03  adam
104  * Windows NT port.
105  *
106  * Revision 1.65  1997/09/22 12:39:06  adam
107  * Added get_pos method for the ranked result sets.
108  *
109  * Revision 1.64  1997/09/18 08:59:20  adam
110  * Extra generic handle for the character mapping routines.
111  *
112  * Revision 1.63  1997/09/17 12:19:18  adam
113  * Zebra version corresponds to YAZ version 1.4.
114  * Changed Zebra server so that it doesn't depend on global common_resource.
115  *
116  * Revision 1.62  1997/09/05 15:30:09  adam
117  * Changed prototype for chr_map_input - added const.
118  * Added support for C++, headers uses extern "C" for public definitions.
119  *
120  * Revision 1.61  1997/02/10 10:21:14  adam
121  * Bug fix: in search terms character (^) wasn't observed.
122  *
123  * Revision 1.60  1997/01/31 11:10:34  adam
124  * Bug fix: Leading and trailing white space weren't removed in scan tokens.
125  *
126  * Revision 1.59  1997/01/17 11:31:46  adam
127  * Bug fix: complete phrase search didn't work.
128  *
129  * Revision 1.58  1996/12/23 15:30:45  adam
130  * Work on truncation.
131  * Bug fix: result sets weren't deleted after server shut down.
132  *
133  * Revision 1.57  1996/11/11 13:38:02  adam
134  * Added proximity support in search.
135  *
136  * Revision 1.56  1996/11/08 11:10:32  adam
137  * Buffers used during file match got bigger.
138  * Compressed ISAM support everywhere.
139  * Bug fixes regarding masking characters in queries.
140  * Redesigned Regexp-2 queries.
141  *
142  * Revision 1.55  1996/11/04 14:07:44  adam
143  * Moved truncation code to trunc.c.
144  *
145  * Revision 1.54  1996/10/29 14:09:52  adam
146  * Use of cisam system - enabled if setting isamc is 1.
147  *
148  * Revision 1.53  1996/06/26 09:21:43  adam
149  * Bug fix: local attribute set wasn't obeyed in scan.
150  *
151  * Revision 1.52  1996/06/17  14:26:20  adam
152  * Function gen_regular_rel changed to handle negative numbers.
153  *
154  * Revision 1.51  1996/06/11 10:54:15  quinn
155  * Relevance work
156  *
157  * Revision 1.50  1996/06/07  08:51:53  adam
158  * Bug fix: Character mapping was broken (introducued by last revision).
159  *
160  * Revision 1.49  1996/06/04  10:18:11  adam
161  * Search/scan uses character mapping module.
162  *
163  * Revision 1.48  1996/05/28  15:15:01  adam
164  * Bug fix: Didn't handle unknown database correctly.
165  *
166  * Revision 1.47  1996/05/15  18:36:28  adam
167  * Function trans_term transforms unsearchable characters to blanks.
168  *
169  * Revision 1.46  1996/05/15  11:57:56  adam
170  * Fixed bug introduced by set/field mapping in search operations.
171  *
172  * Revision 1.45  1996/05/14  11:34:00  adam
173  * Scan support in multiple registers/databases.
174  *
175  * Revision 1.44  1996/05/14  06:16:44  adam
176  * Compact use/set bytes used in search service.
177  *
178  * Revision 1.43  1996/05/09 09:54:43  adam
179  * Server supports maps from one logical attributes to a list of physical
180  * attributes.
181  * The extraction process doesn't make space consuming 'any' keys.
182  *
183  * Revision 1.42  1996/05/09  07:28:56  quinn
184  * Work towards phrases and multiple registers
185  *
186  * Revision 1.41  1996/03/20  09:36:43  adam
187  * Function dict_lookup_grep got extra parameter, init_pos, which marks
188  * from which position in pattern approximate pattern matching should occur.
189  * Approximate pattern matching is used in relevance=re-2.
190  *
191  * Revision 1.40  1996/02/02  13:44:44  adam
192  * The public dictionary functions simply use char instead of Dict_char
193  * to represent search strings. Dict_char is used internally only.
194  *
195  * Revision 1.39  1996/01/03  16:22:13  quinn
196  * operator->roperator
197  *
198  * Revision 1.38  1995/12/11  09:12:55  adam
199  * The rec_get function returns NULL if record doesn't exist - will
200  * happen in the server if the result set records have been deleted since
201  * the creation of the set (i.e. the search).
202  * The server saves a result temporarily if it is 'volatile', i.e. the
203  * set is register dependent.
204  *
205  * Revision 1.37  1995/12/06  15:05:28  adam
206  * More verbose in count_set.
207  *
208  * Revision 1.36  1995/12/06  12:41:27  adam
209  * New command 'stat' for the index program.
210  * Filenames can be read from stdin by specifying '-'.
211  * Bug fix/enhancement of the transformation from terms to regular
212  * expressons in the search engine.
213  *
214  * Revision 1.35  1995/11/27  09:29:00  adam
215  * Bug fixes regarding conversion to regular expressions.
216  *
217  * Revision 1.34  1995/11/16  17:00:56  adam
218  * Better logging of rpn query.
219  *
220  * Revision 1.33  1995/11/01  13:58:28  quinn
221  * Moving data1 to yaz/retrieval
222  *
223  * Revision 1.32  1995/10/27  14:00:11  adam
224  * Implemented detection of database availability.
225  *
226  * Revision 1.31  1995/10/17  18:02:10  adam
227  * New feature: databases. Implemented as prefix to words in dictionary.
228  *
229  * Revision 1.30  1995/10/16  09:32:38  adam
230  * More work on relational op.
231  *
232  * Revision 1.29  1995/10/13  16:01:49  adam
233  * Work on relations.
234  *
235  * Revision 1.28  1995/10/13  12:26:43  adam
236  * Optimization of truncation.
237  *
238  * Revision 1.27  1995/10/12  17:07:22  adam
239  * Truncation works.
240  *
241  * Revision 1.26  1995/10/12  12:40:54  adam
242  * Bug fixes in rpn_prox.
243  *
244  * Revision 1.25  1995/10/10  13:59:24  adam
245  * Function rset_open changed its wflag parameter to general flags.
246  *
247  * Revision 1.24  1995/10/09  16:18:37  adam
248  * Function dict_lookup_grep got extra client data parameter.
249  *
250  * Revision 1.23  1995/10/06  16:33:37  adam
251  * Use attribute mappings.
252  *
253  * Revision 1.22  1995/10/06  15:07:39  adam
254  * Structure 'local-number' handled.
255  *
256  * Revision 1.21  1995/10/06  13:52:06  adam
257  * Bug fixes. Handler may abort further scanning.
258  *
259  * Revision 1.20  1995/10/06  11:06:33  adam
260  * Scan entries include 'occurrences' now.
261  *
262  * Revision 1.19  1995/10/06  10:43:56  adam
263  * Scan added. 'occurrences' in scan entries not set yet.
264  *
265  * Revision 1.18  1995/10/04  16:57:20  adam
266  * Key input and merge sort in one pass.
267  *
268  * Revision 1.17  1995/10/04  12:55:17  adam
269  * Bug fix in ranked search. Use=Any keys inserted.
270  *
271  * Revision 1.16  1995/10/02  16:24:40  adam
272  * Use attribute actually used in search requests.
273  *
274  * Revision 1.15  1995/10/02  15:18:52  adam
275  * New member in recRetrieveCtrl: diagnostic.
276  *
277  * Revision 1.14  1995/09/28  12:10:32  adam
278  * Bug fixes. Field prefix used in queries.
279  *
280  * Revision 1.13  1995/09/18  14:17:50  adam
281  * Minor changes.
282  *
283  * Revision 1.12  1995/09/15  14:45:21  adam
284  * Retrieve control.
285  * Work on truncation.
286  *
287  * Revision 1.11  1995/09/14  11:53:27  adam
288  * First work on regular expressions/truncations.
289  *
290  * Revision 1.10  1995/09/11  15:23:26  adam
291  * More work on relevance search.
292  *
293  * Revision 1.9  1995/09/11  13:09:35  adam
294  * More work on relevance feedback.
295  *
296  * Revision 1.8  1995/09/08  14:52:27  adam
297  * Minor changes. Dictionary is lower case now.
298  *
299  * Revision 1.7  1995/09/07  13:58:36  adam
300  * New parameter: result-set file descriptor (RSFD) to support multiple
301  * positions within the same result-set.
302  * Boolean operators: and, or, not implemented.
303  * Result-set references.
304  *
305  * Revision 1.6  1995/09/06  16:11:18  adam
306  * Option: only one word key per file.
307  *
308  * Revision 1.5  1995/09/06  10:33:04  adam
309  * More work on present. Some log messages removed.
310  *
311  * Revision 1.4  1995/09/05  15:28:40  adam
312  * More work on search engine.
313  *
314  * Revision 1.3  1995/09/04  15:20:22  adam
315  * Minor changes.
316  *
317  * Revision 1.2  1995/09/04  12:33:43  adam
318  * Various cleanup. YAZ util used instead.
319  *
320  * Revision 1.1  1995/09/04  09:10:40  adam
321  * More work on index add/del/update.
322  * Merge sort implemented.
323  * Initial work on z39 server.
324  *
325  */
326 #include <stdio.h>
327 #include <assert.h>
328 #ifdef WIN32
329 #include <io.h>
330 #else
331 #include <unistd.h>
332 #endif
333 #include <ctype.h>
334
335 #include "zserver.h"
336
337 #include <charmap.h>
338 #include <rstemp.h>
339 #include <rsnull.h>
340 #include <rsbool.h>
341
342 struct rpn_char_map_info {
343     ZebraMaps zm;
344     int reg_type;
345 };
346
347 static const char **rpn_char_map_handler (void *vp, const char **from, int len)
348 {
349     struct rpn_char_map_info *p = (struct rpn_char_map_info *) vp;
350     return zebra_maps_input (p->zm, p->reg_type, from, len);
351 }
352
353 static void rpn_char_map_prepare (ZebraHandle zh, int reg_type,
354                                   struct rpn_char_map_info *map_info)
355 {
356     map_info->zm = zh->zebra_maps;
357     map_info->reg_type = reg_type;
358     dict_grep_cmap (zh->dict, map_info, rpn_char_map_handler);
359 }
360
361 typedef struct {
362     int type;
363     int major;
364     int minor;
365     Z_AttributesPlusTerm *zapt;
366 } AttrType;
367
368 static int attr_find (AttrType *src, oid_value *attributeSetP)
369 {
370     int num_attributes;
371
372 #ifdef ASN_COMPILED
373     num_attributes = src->zapt->attributes->num_attributes;
374 #else
375     num_attributes = src->zapt->num_attributes;
376 #endif
377     while (src->major < num_attributes)
378     {
379         Z_AttributeElement *element;
380
381 #ifdef ASN_COMPILED
382         element = src->zapt->attributes->attributes[src->major];
383 #else
384         element = src->zapt->attributeList[src->major];
385 #endif
386         if (src->type == *element->attributeType)
387         {
388             switch (element->which) 
389             {
390             case Z_AttributeValue_numeric:
391                 ++(src->major);
392                 if (element->attributeSet && attributeSetP)
393                 {
394                     oident *attrset;
395
396                     attrset = oid_getentbyoid (element->attributeSet);
397                     *attributeSetP = attrset->value;
398                 }
399                 return *element->value.numeric;
400                 break;
401             case Z_AttributeValue_complex:
402                 if (src->minor >= element->value.complex->num_list ||
403                     element->value.complex->list[src->minor]->which !=  
404                     Z_StringOrNumeric_numeric)
405                     break;
406                 ++(src->minor);
407                 if (element->attributeSet && attributeSetP)
408                 {
409                     oident *attrset;
410
411                     attrset = oid_getentbyoid (element->attributeSet);
412                     *attributeSetP = attrset->value;
413                 }
414                 return *element->value.complex->list[src->minor-1]->u.numeric;
415             default:
416                 assert (0);
417             }
418         }
419         ++(src->major);
420     }
421     return -1;
422 }
423
424 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
425                        int type)
426 {
427     src->zapt = zapt;
428     src->type = type;
429     src->major = 0;
430     src->minor = 0;
431 }
432
433 #define TERM_COUNT        
434        
435 struct grep_info {        
436 #ifdef TERM_COUNT        
437     int *term_no;        
438 #endif        
439     ISAM_P *isam_p_buf;
440     int isam_p_size;        
441     int isam_p_indx;
442     ZebraHandle zh;
443     int reg_type;
444 };        
445
446 static void term_untrans  (ZebraHandle zh, int reg_type,
447                            char *dst, const char *src)
448 {
449     while (*src)
450     {
451         const char *cp = zebra_maps_output (zh->zebra_maps, reg_type, &src);
452         while (*cp)
453             *dst++ = *cp++;
454     }
455     *dst = '\0';
456 }
457
458 static void add_isam_p (const char *name, const char *info,
459                         struct grep_info *p)
460 {
461     if (p->isam_p_indx == p->isam_p_size)
462     {
463         ISAM_P *new_isam_p_buf;
464 #ifdef TERM_COUNT        
465         int *new_term_no;        
466 #endif
467         p->isam_p_size = 2*p->isam_p_size + 100;
468         new_isam_p_buf = (ISAM_P *) xmalloc (sizeof(*new_isam_p_buf) *
469                                              p->isam_p_size);
470         if (p->isam_p_buf)
471         {
472             memcpy (new_isam_p_buf, p->isam_p_buf,
473                     p->isam_p_indx * sizeof(*p->isam_p_buf));
474             xfree (p->isam_p_buf);
475         }
476         p->isam_p_buf = new_isam_p_buf;
477
478 #ifdef TERM_COUNT
479         new_term_no = (int *) xmalloc (sizeof(*new_term_no) *
480                                        p->isam_p_size);
481         if (p->term_no)
482         {
483             memcpy (new_term_no, p->isam_p_buf,
484                     p->isam_p_indx * sizeof(*p->term_no));
485             xfree (p->term_no);
486         }
487         p->term_no = new_term_no;
488 #endif
489     }
490     assert (*info == sizeof(*p->isam_p_buf));
491     memcpy (p->isam_p_buf + p->isam_p_indx, info+1, sizeof(*p->isam_p_buf));
492
493 #if 0
494     term_untrans  (p->zh, p->reg_type, term_tmp, name+2);
495     logf (LOG_DEBUG, "grep: %s", term_tmp);
496 #endif
497     (p->isam_p_indx)++;
498 }
499
500 static int grep_handle (char *name, const char *info, void *p)
501 {
502     add_isam_p (name, info, (struct grep_info *) p);
503     return 0;
504 }
505
506 static int term_pre (ZebraMaps zebra_maps, int reg_type, const char **src,
507                      const char *ct1, const char *ct2)
508 {
509     const char *s1, *s0 = *src;
510     const char **map;
511
512     /* skip white space */
513     while (*s0)
514     {
515         if (ct1 && strchr (ct1, *s0))
516             break;
517         if (ct2 && strchr (ct2, *s0))
518             break;
519         s1 = s0;
520         map = zebra_maps_input (zebra_maps, reg_type, &s1, strlen(s1));
521         if (**map != *CHR_SPACE)
522             break;
523         s0 = s1;
524     }
525     *src = s0;
526     return *s0;
527 }
528
529 static int term_100 (ZebraMaps zebra_maps, int reg_type,
530                      const char **src, char *dst, int space_split,
531                      char *dst_term)
532 {
533     const char *s0, *s1;
534     const char **map;
535     int i = 0;
536     int j = 0;
537
538     if (!term_pre (zebra_maps, reg_type, src, NULL, NULL))
539         return 0;
540     s0 = *src;
541     while (*s0)
542     {
543         s1 = s0;
544         map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
545         if (space_split && **map == *CHR_SPACE)
546             break;
547         while (s1 < s0)
548         {
549             if (!isalnum (*s1) && *s1 != '-')
550                 dst[i++] = '\\';
551             dst_term[j++] = *s1;
552             dst[i++] = *s1++;
553         }
554     }
555     dst[i] = '\0';
556     dst_term[j] = '\0';
557     *src = s0;
558     return i;
559 }
560
561 static int term_101 (ZebraMaps zebra_maps, int reg_type,
562                      const char **src, char *dst, int space_split,
563                      char *dst_term)
564 {
565     const char *s0, *s1;
566     const char **map;
567     int i = 0;
568     int j = 0;
569
570     if (!term_pre (zebra_maps, reg_type, src, "#", "#"))
571         return 0;
572     s0 = *src;
573     while (*s0)
574     {
575         if (*s0 == '#')
576         {
577             dst[i++] = '.';
578             dst[i++] = '*';
579             dst_term[j++] = *s0++;
580         }
581         else
582         {
583             s1 = s0;
584             map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
585             if (space_split && **map == *CHR_SPACE)
586                 break;
587             while (s1 < s0)
588             {
589                 if (!isalnum (*s1))
590                     dst[i++] = '\\';
591                 dst_term[j++] = *s1;
592                 dst[i++] = *s1++;
593             }
594         }
595     }
596     dst[i] = '\0';
597     dst_term[j++] = '\0';
598     *src = s0;
599     return i;
600 }
601
602
603 static int term_103 (ZebraMaps zebra_maps, int reg_type, const char **src,
604                      char *dst, int *errors, int space_split,
605                      char *dst_term)
606 {
607     int i = 0;
608     int j = 0;
609     const char *s0, *s1;
610     const char **map;
611
612     if (!term_pre (zebra_maps, reg_type, src, "^\\()[].*+?|", "("))
613         return 0;
614     s0 = *src;
615     if (errors && *s0 == '+' && s0[1] && s0[2] == '+' && s0[3] &&
616         isdigit (s0[1]))
617     {
618         *errors = s0[1] - '0';
619         s0 += 3;
620         if (*errors > 3)
621             *errors = 3;
622     }
623     while (*s0)
624     {
625         if (strchr ("^\\()[].*+?|-", *s0))
626         {
627             dst_term[j++] = *s0;
628             dst[i++] = *s0++;
629         }
630         else
631         {
632             s1 = s0;
633             map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
634             if (**map == *CHR_SPACE)
635                 break;
636             while (s1 < s0)
637             {
638                 if (!isalnum (*s1))
639                     dst[i++] = '\\';
640                 dst_term[j++] = *s1;
641                 dst[i++] = *s1++;
642             }
643         }
644     }
645     dst[i] = '\0';
646     dst_term[j] = '\0';
647     *src = s0;
648     return i;
649 }
650
651 static int term_102 (ZebraMaps zebra_maps, int reg_type, const char **src,
652                      char *dst, int space_split, char *dst_term)
653 {
654     return term_103 (zebra_maps, reg_type, src, dst, NULL, space_split,
655                      dst_term);
656 }
657
658 /* gen_regular_rel - generate regular expression from relation
659  *  val:     border value (inclusive)
660  *  islt:    1 if <=; 0 if >=.
661  */
662 static void gen_regular_rel (char *dst, int val, int islt)
663 {
664     int dst_p;
665     int w, d, i;
666     int pos = 0;
667     char numstr[20];
668
669     logf (LOG_DEBUG, "gen_regular_rel. val=%d, islt=%d", val, islt);
670     if (val >= 0)
671     {
672         if (islt)
673             strcpy (dst, "(-[0-9]+|(");
674         else
675             strcpy (dst, "((");
676     } 
677     else
678     {
679         if (!islt)
680         {
681             strcpy (dst, "([0-9]+|-(");
682             dst_p = strlen (dst);
683             islt = 1;
684         }
685         else
686         {
687             strcpy (dst, "(-(");
688             islt = 0;
689         }
690         val = -val;
691     }
692     dst_p = strlen (dst);
693     sprintf (numstr, "%d", val);
694     for (w = strlen(numstr); --w >= 0; pos++)
695     {
696         d = numstr[w];
697         if (pos > 0)
698         {
699             if (islt)
700             {
701                 if (d == '0')
702                     continue;
703                 d--;
704             } 
705             else
706             {
707                 if (d == '9')
708                     continue;
709                 d++;
710             }
711         }
712         
713         strcpy (dst + dst_p, numstr);
714         dst_p = strlen(dst) - pos - 1;
715
716         if (islt)
717         {
718             if (d != '0')
719             {
720                 dst[dst_p++] = '[';
721                 dst[dst_p++] = '0';
722                 dst[dst_p++] = '-';
723                 dst[dst_p++] = d;
724                 dst[dst_p++] = ']';
725             }
726             else
727                 dst[dst_p++] = d;
728         }
729         else
730         {
731             if (d != '9')
732             { 
733                 dst[dst_p++] = '[';
734                 dst[dst_p++] = d;
735                 dst[dst_p++] = '-';
736                 dst[dst_p++] = '9';
737                 dst[dst_p++] = ']';
738             }
739             else
740                 dst[dst_p++] = d;
741         }
742         for (i = 0; i<pos; i++)
743         {
744             dst[dst_p++] = '[';
745             dst[dst_p++] = '0';
746             dst[dst_p++] = '-';
747             dst[dst_p++] = '9';
748             dst[dst_p++] = ']';
749         }
750         dst[dst_p++] = '|';
751     }
752     dst[dst_p] = '\0';
753     if (islt)
754     {
755         for (i=1; i<pos; i++)
756             strcat (dst, "[0-9]?");
757     }
758     else
759     {
760         for (i = 0; i <= pos; i++)
761             strcat (dst, "[0-9]");
762         strcat (dst, "[0-9]*");
763     }
764     strcat (dst, "))");
765 }
766
767 void string_rel_add_char (char **term_p, const char *src, int *indx)
768 {
769     if (src[*indx] == '\\')
770         *(*term_p)++ = src[(*indx)++];
771     *(*term_p)++ = src[(*indx)++];
772 }
773
774 /*
775  *   >  abc     ([b-].*|a[c-].*|ab[d-].*|abc.+)
776  *              ([^-a].*|a[^-b].*ab[^-c].*|abc.+)
777  *   >= abc     ([b-].*|a[c-].*|ab[c-].*)
778  *              ([^-a].*|a[^-b].*|ab[c-].*)
779  *   <  abc     ([-0].*|a[-a].*|ab[-b].*)
780  *              ([^a-].*|a[^b-].*|ab[^c-].*)
781  *   <= abc     ([-0].*|a[-a].*|ab[-b].*|abc)
782  *              ([^a-].*|a[^b-].*|ab[^c-].*|abc)
783  */
784 static int string_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
785                             const char **term_sub, char *term_dict,
786                             oid_value attributeSet,
787                             int reg_type, int space_split, char *term_dst)
788 {
789     AttrType relation;
790     int relation_value;
791     int i;
792     char *term_tmp = term_dict + strlen(term_dict);
793     char term_component[256];
794
795     attr_init (&relation, zapt, 2);
796     relation_value = attr_find (&relation, NULL);
797
798     logf (LOG_DEBUG, "string relation value=%d", relation_value);
799     switch (relation_value)
800     {
801     case 1:
802         if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_component,
803                        space_split, term_dst))
804             return 0;
805         logf (LOG_DEBUG, "Relation <");
806         
807         *term_tmp++ = '(';
808         for (i = 0; term_component[i]; )
809         {
810             int j = 0;
811
812             if (i)
813                 *term_tmp++ = '|';
814             while (j < i)
815                 string_rel_add_char (&term_tmp, term_component, &j);
816
817             *term_tmp++ = '[';
818
819             *term_tmp++ = '^';
820             string_rel_add_char (&term_tmp, term_component, &i);
821             *term_tmp++ = '-';
822
823             *term_tmp++ = ']';
824             *term_tmp++ = '.';
825             *term_tmp++ = '*';
826         }
827         *term_tmp++ = ')';
828         *term_tmp = '\0';
829         break;
830     case 2:
831         if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_component,
832                        space_split, term_dst))
833             return 0;
834         logf (LOG_DEBUG, "Relation <=");
835
836         *term_tmp++ = '(';
837         for (i = 0; term_component[i]; )
838         {
839             int j = 0;
840
841             while (j < i)
842                 string_rel_add_char (&term_tmp, term_component, &j);
843             *term_tmp++ = '[';
844
845             *term_tmp++ = '^';
846             string_rel_add_char (&term_tmp, term_component, &i);
847             *term_tmp++ = '-';
848
849             *term_tmp++ = ']';
850             *term_tmp++ = '.';
851             *term_tmp++ = '*';
852
853             *term_tmp++ = '|';
854         }
855         for (i = 0; term_component[i]; )
856             string_rel_add_char (&term_tmp, term_component, &i);
857         *term_tmp++ = ')';
858         *term_tmp = '\0';
859         break;
860     case 5:
861         if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_component,
862                        space_split, term_dst))
863             return 0;
864         logf (LOG_DEBUG, "Relation >");
865
866         *term_tmp++ = '(';
867         for (i = 0; term_component[i];)
868         {
869             int j = 0;
870
871             while (j < i)
872                 string_rel_add_char (&term_tmp, term_component, &j);
873             *term_tmp++ = '[';
874             
875             *term_tmp++ = '^';
876             *term_tmp++ = '-';
877             string_rel_add_char (&term_tmp, term_component, &i);
878
879             *term_tmp++ = ']';
880             *term_tmp++ = '.';
881             *term_tmp++ = '*';
882
883             *term_tmp++ = '|';
884         }
885         for (i = 0; term_component[i];)
886             string_rel_add_char (&term_tmp, term_component, &i);
887         *term_tmp++ = '.';
888         *term_tmp++ = '+';
889         *term_tmp++ = ')';
890         *term_tmp = '\0';
891         break;
892     case 4:
893         if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_component,
894                        space_split, term_dst))
895             return 0;
896         logf (LOG_DEBUG, "Relation >=");
897
898         *term_tmp++ = '(';
899         for (i = 0; term_component[i];)
900         {
901             int j = 0;
902
903             if (i)
904                 *term_tmp++ = '|';
905             while (j < i)
906                 string_rel_add_char (&term_tmp, term_component, &j);
907             *term_tmp++ = '[';
908
909             if (term_component[i+1])
910             {
911                 *term_tmp++ = '^';
912                 *term_tmp++ = '-';
913                 string_rel_add_char (&term_tmp, term_component, &i);
914             }
915             else
916             {
917                 string_rel_add_char (&term_tmp, term_component, &i);
918                 *term_tmp++ = '-';
919             }
920             *term_tmp++ = ']';
921             *term_tmp++ = '.';
922             *term_tmp++ = '*';
923         }
924         *term_tmp++ = ')';
925         *term_tmp = '\0';
926         break;
927     case 3:
928     default:
929         logf (LOG_DEBUG, "Relation =");
930         if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_component,
931                        space_split, term_dst))
932             return 0;
933         strcat (term_tmp, "(");
934         strcat (term_tmp, term_component);
935         strcat (term_tmp, ")");
936     }
937     return 1;
938 }
939
940 static int string_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
941                         const char **term_sub, 
942                         oid_value attributeSet, NMEM stream,
943                         struct grep_info *grep_info,
944                         int reg_type, int complete_flag,
945                         int num_bases, char **basenames,
946                         char *term_dst)
947 {
948     char term_dict[2*IT_MAX_WORD+4000];
949     int j, r, base_no;
950     AttrType truncation;
951     int truncation_value;
952     AttrType use;
953     int use_value;
954     oid_value curAttributeSet = attributeSet;
955     const char *termp;
956     struct rpn_char_map_info rcmi;
957     int space_split = complete_flag ? 0 : 1;
958
959     rpn_char_map_prepare (zh, reg_type, &rcmi);
960     attr_init (&use, zapt, 1);
961     use_value = attr_find (&use, &curAttributeSet);
962     logf (LOG_DEBUG, "string_term, use value %d", use_value);
963     attr_init (&truncation, zapt, 5);
964     truncation_value = attr_find (&truncation, NULL);
965     logf (LOG_DEBUG, "truncation value %d", truncation_value);
966
967     if (use_value == -1)
968         use_value = 1016;
969
970     for (base_no = 0; base_no < num_bases; base_no++)
971     {
972         attent attp;
973         data1_local_attribute *local_attr;
974         int max_pos, prefix_len = 0;
975
976         termp = *term_sub;
977         if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
978         {
979             logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
980                   curAttributeSet, use_value, r);
981             if (r == -1)
982             {
983                 char val_str[32];
984                 sprintf (val_str, "%d", use_value);
985                 zh->errCode = 114;
986                 zh->errString = nmem_strdup (stream, val_str);
987             }
988             else
989             {
990                 int oid[OID_SIZE];
991                 struct oident oident;
992
993                 oident.proto = PROTO_Z3950;
994                 oident.oclass = CLASS_ATTSET;
995                 oident.value = curAttributeSet;
996                 oid_ent_to_oid (&oident, oid);
997
998                 zh->errCode = 121;
999                 zh->errString = nmem_strdup (stream, oident.desc);
1000             }
1001             return -1;
1002         }
1003         if (zebraExplain_curDatabase (zh->zei, basenames[base_no]))
1004         {
1005             zh->errCode = 109; /* Database unavailable */
1006             zh->errString = basenames[base_no];
1007             return -1;
1008         }
1009         for (local_attr = attp.local_attributes; local_attr;
1010              local_attr = local_attr->next)
1011         {
1012             int ord;
1013             char ord_buf[32];
1014             int i, ord_len;
1015
1016             ord = zebraExplain_lookupSU (zh->zei, attp.attset_ordinal,
1017                                           local_attr->local);
1018             if (ord < 0)
1019                 continue;
1020             if (prefix_len)
1021                 term_dict[prefix_len++] = '|';
1022             else
1023                 term_dict[prefix_len++] = '(';
1024
1025             ord_len = key_SU_code (ord, ord_buf);
1026             for (i = 0; i<ord_len; i++)
1027             {
1028                 term_dict[prefix_len++] = 1;
1029                 term_dict[prefix_len++] = ord_buf[i];
1030             }
1031         }
1032         if (!prefix_len)
1033         {
1034             char val_str[32];
1035             sprintf (val_str, "%d", use_value);
1036             zh->errCode = 114;
1037             zh->errString = nmem_strdup (stream, val_str);
1038             return -1;
1039         }
1040         term_dict[prefix_len++] = ')';        
1041         term_dict[prefix_len++] = 1;
1042         term_dict[prefix_len++] = reg_type;
1043         logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1044         term_dict[prefix_len] = '\0';
1045         j = prefix_len;
1046         switch (truncation_value)
1047         {
1048         case -1:         /* not specified */
1049         case 100:        /* do not truncate */
1050             if (!string_relation (zh, zapt, &termp, term_dict,
1051                                   attributeSet,
1052                                   reg_type, space_split, term_dst))
1053                 return 0;
1054             logf (LOG_DEBUG, "dict_lookup_grep: %s", term_dict+prefix_len);
1055             r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info, &max_pos,
1056                                   0, grep_handle);
1057             if (r)
1058                 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
1059             break;
1060         case 1:          /* right truncation */
1061             term_dict[j++] = '(';
1062             if (!term_100 (zh->zebra_maps, reg_type,
1063                            &termp, term_dict + j, space_split, term_dst))
1064                 return 0;
1065             strcat (term_dict, ".*)");
1066             dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1067                               &max_pos, 0, grep_handle);
1068             break;
1069         case 2:          /* keft truncation */
1070             term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1071             if (!term_100 (zh->zebra_maps, reg_type,
1072                            &termp, term_dict + j, space_split, term_dst))
1073                 return 0;
1074             strcat (term_dict, ")");
1075             dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1076                               &max_pos, 0, grep_handle);
1077             break;
1078         case 3:          /* left&right truncation */
1079             term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1080             if (!term_100 (zh->zebra_maps, reg_type,
1081                            &termp, term_dict + j, space_split, term_dst))
1082                 return 0;
1083             strcat (term_dict, ".*)");
1084             dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1085                               &max_pos, 0, grep_handle);
1086             break;
1087             zh->errCode = 120;
1088             return -1;
1089         case 101:        /* process # in term */
1090             term_dict[j++] = '(';
1091             if (!term_101 (zh->zebra_maps, reg_type,
1092                            &termp, term_dict + j, space_split, term_dst))
1093                 return 0;
1094             strcat (term_dict, ")");
1095             r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1096                                   &max_pos, 0, grep_handle);
1097             if (r)
1098                 logf (LOG_WARN, "dict_lookup_grep err, trunc=#: %d", r);
1099             break;
1100         case 102:        /* Regexp-1 */
1101             term_dict[j++] = '(';
1102             if (!term_102 (zh->zebra_maps, reg_type,
1103                            &termp, term_dict + j, space_split, term_dst))
1104                 return 0;
1105             strcat (term_dict, ")");
1106             logf (LOG_DEBUG, "Regexp-1 tolerance=%d", r);
1107             r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1108                                   &max_pos, 0, grep_handle);
1109             if (r)
1110                 logf (LOG_WARN, "dict_lookup_grep err, trunc=regular: %d",
1111                       r);
1112             break;
1113         case 103:       /* Regexp-2 */
1114             r = 1;
1115             term_dict[j++] = '(';
1116             if (!term_103 (zh->zebra_maps, reg_type,
1117                            &termp, term_dict + j, &r, space_split, term_dst))
1118                 return 0;
1119             strcat (term_dict, ")");
1120             logf (LOG_DEBUG, "Regexp-2 tolerance=%d", r);
1121             r = dict_lookup_grep (zh->dict, term_dict, r, grep_info,
1122                                   &max_pos, 2, grep_handle);
1123             if (r)
1124                 logf (LOG_WARN, "dict_lookup_grep err, trunc=eregular: %d",
1125                       r);
1126             break;
1127         }
1128     }
1129     *term_sub = termp;
1130     logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1131     return 1;
1132 }
1133
1134 static void trans_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1135                         char *termz)
1136 {
1137     size_t sizez;
1138     Z_Term *term = zapt->term;
1139
1140     sizez = term->u.general->len;
1141     if (sizez > IT_MAX_WORD-1)
1142         sizez = IT_MAX_WORD-1;
1143     memcpy (termz, term->u.general->buf, sizez);
1144     termz[sizez] = '\0';
1145 }
1146
1147 static void trans_scan_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1148                              char *termz, int reg_type)
1149 {
1150     Z_Term *term = zapt->term;
1151     const char **map;
1152     const char *cp = (const char *) term->u.general->buf;
1153     const char *cp_end = cp + term->u.general->len;
1154     const char *src;
1155     int i = 0;
1156     const char *space_map = NULL;
1157     int len;
1158     
1159     while ((len = (cp_end - cp)) > 0)
1160     {
1161         map = zebra_maps_input (zh->zebra_maps, reg_type, &cp, len);
1162         if (**map == *CHR_SPACE)
1163             space_map = *map;
1164         else
1165         {
1166             if (i && space_map)
1167                 for (src = space_map; *src; src++)
1168                     termz[i++] = *src;
1169             space_map = NULL;
1170             for (src = *map; *src; src++)
1171                 termz[i++] = *src;
1172         }
1173     }
1174     termz[i] = '\0';
1175 }
1176
1177 static RSET rpn_proximity (ZebraHandle zh, RSET rset1, RSET rset2,
1178                            int ordered,
1179                            int exclusion, int relation, int distance)
1180 {
1181     int i;
1182     RSFD rsfd1, rsfd2;
1183     int  more1, more2;
1184     struct it_key buf1, buf2;
1185     RSFD rsfd_result;
1186     RSET result;
1187     rset_temp_parms parms;
1188     int term_index;
1189     
1190     rsfd1 = rset_open (rset1, RSETF_READ);
1191     more1 = rset_read (rset1, rsfd1, &buf1, &term_index);
1192     
1193     rsfd2 = rset_open (rset2, RSETF_READ);
1194     more2 = rset_read (rset2, rsfd2, &buf2, &term_index);
1195
1196     parms.key_size = sizeof (struct it_key);
1197     parms.temp_path = res_get (zh->res, "setTmpDir");
1198     result = rset_create (rset_kind_temp, &parms);
1199     rsfd_result = rset_open (result, RSETF_WRITE);
1200    
1201     logf (LOG_DEBUG, "rpn_proximity  excl=%d ord=%d rel=%d dis=%d",
1202           exclusion, ordered, relation, distance);
1203     while (more1 && more2)
1204     {
1205         int cmp = key_compare_it (&buf1, &buf2);
1206         if (cmp < -1)
1207             more1 = rset_read (rset1, rsfd1, &buf1, &term_index);
1208         else if (cmp > 1)
1209             more2 = rset_read (rset2, rsfd2, &buf2, &term_index);
1210         else
1211         {
1212             int sysno = buf1.sysno;
1213             int seqno[500];
1214             int n = 0;
1215
1216             seqno[n++] = buf1.seqno;
1217             while ((more1 = rset_read (rset1, rsfd1, &buf1, &term_index)) &&
1218                    sysno == buf1.sysno)
1219                 if (n < 500)
1220                     seqno[n++] = buf1.seqno;
1221             do
1222             {
1223                 for (i = 0; i<n; i++)
1224                 {
1225                     int diff = buf2.seqno - seqno[i];
1226                     int excl = exclusion;
1227                     if (!ordered && diff < 0)
1228                         diff = -diff;
1229                     switch (relation)
1230                     {
1231                     case 1:      /* < */
1232                         if (diff < distance)
1233                             excl = !excl;
1234                         break;
1235                     case 2:      /* <= */
1236                         if (diff <= distance)
1237                             excl = !excl;
1238                         break;
1239                     case 3:      /* == */
1240                         if (diff == distance)
1241                             excl = !excl;
1242                         break;
1243                     case 4:      /* >= */
1244                         if (diff >= distance)
1245                             excl = !excl;
1246                         break;
1247                     case 5:      /* > */
1248                         if (diff > distance)
1249                             excl = !excl;
1250                         break;
1251                     case 6:      /* != */
1252                         if (diff != distance)
1253                             excl = !excl;
1254                         break;
1255                     }
1256                     if (excl)
1257                         rset_write (result, rsfd_result, &buf2);
1258                 }
1259             } while ((more2 = rset_read (rset2, rsfd2, &buf2, &term_index)) &&
1260                       sysno == buf2.sysno);
1261         }
1262     }
1263     rset_close (result, rsfd_result);
1264     rset_close (rset1, rsfd1);
1265     rset_close (rset2, rsfd2);
1266     return result;
1267 }
1268
1269 static RSET rpn_prox (ZebraHandle zh, RSET *rset, int rset_no)
1270 {
1271     int i;
1272     RSFD *rsfd;
1273     int  *more;
1274     struct it_key **buf;
1275     RSET result;
1276     char prox_term[1024];
1277     int length_prox_term = 0;
1278     int min_nn = 10000000;
1279     int term_index;
1280     const char *flags = NULL;
1281     
1282     rsfd = (RSFD *) xmalloc (sizeof(*rsfd)*rset_no);
1283     more = (int *) xmalloc (sizeof(*more)*rset_no);
1284     buf = (struct it_key **) xmalloc (sizeof(*buf)*rset_no);
1285
1286     for (i = 0; i<rset_no; i++)
1287     {
1288         int j;
1289         buf[i] = (struct it_key *) xmalloc (sizeof(**buf));
1290         rsfd[i] = rset_open (rset[i], RSETF_READ);
1291         if (!(more[i] = rset_read (rset[i], rsfd[i], buf[i], &term_index)))
1292             break;
1293         for (j = 0; j<rset[i]->no_rset_terms; j++)
1294         {
1295             const char *nflags = rset[i]->rset_terms[j]->flags;
1296             char *term = rset[i]->rset_terms[j]->name;
1297             int lterm = strlen(term);
1298             if (length_prox_term)
1299                 prox_term[length_prox_term++] = ' ';
1300             strcpy (prox_term + length_prox_term, term);
1301             length_prox_term += lterm;
1302             if (min_nn > rset[i]->rset_terms[j]->nn)
1303                 min_nn = rset[i]->rset_terms[j]->nn;
1304             flags = nflags;
1305         }
1306     }
1307     if (i != rset_no)
1308     {
1309         rset_null_parms parms;
1310
1311         while (i >= 0)
1312         {
1313             rset_close (rset[i], rsfd[i]);
1314             xfree (buf[i]);
1315             --i;
1316         }
1317         parms.rset_term = rset_term_create (prox_term, -1, flags);
1318         parms.rset_term->nn = 0;
1319         result = rset_create (rset_kind_null, &parms);
1320     }
1321     else
1322     {
1323         rset_temp_parms parms;
1324         RSFD rsfd_result;
1325
1326         parms.rset_term = rset_term_create (prox_term, -1, flags);
1327         parms.rset_term->nn = min_nn;
1328         parms.key_size = sizeof (struct it_key);
1329         parms.temp_path = res_get (zh->res, "setTmpDir");
1330         result = rset_create (rset_kind_temp, &parms);
1331         rsfd_result = rset_open (result, RSETF_WRITE);
1332         
1333         while (*more)
1334         {
1335             for (i = 1; i<rset_no; i++)
1336             {
1337                 int cmp;
1338                 
1339                 if (!more[i])
1340                 {
1341                     *more = 0;
1342                     break;
1343                 }
1344                 cmp = key_compare_it (buf[i], buf[i-1]);
1345                 if (cmp > 1)
1346                 {
1347                     more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1348                                            buf[i-1], &term_index);
1349                     break;
1350                 }
1351                 else if (cmp == 1)
1352                 {
1353                     if (buf[i-1]->seqno+1 != buf[i]->seqno)
1354                     {
1355                         more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1356                                                buf[i-1], &term_index);
1357                         break;
1358                     }
1359                 }
1360                 else
1361                 {
1362                     more[i] = rset_read (rset[i], rsfd[i], buf[i],
1363                                          &term_index);
1364                     break;
1365                 }
1366             }
1367             if (i == rset_no)
1368             {
1369                 rset_write (result, rsfd_result, buf[0]);
1370                 more[0] = rset_read (*rset, *rsfd, *buf, &term_index);
1371             }
1372         }
1373         
1374         for (i = 0; i<rset_no; i++)
1375         {
1376             rset_close (rset[i], rsfd[i]);
1377             xfree (buf[i]);
1378         }
1379         rset_close (result, rsfd_result);
1380     }
1381     xfree (buf);
1382     xfree (more);
1383     xfree (rsfd);
1384     return result;
1385 }
1386
1387 static RSET rpn_search_APT_phrase (ZebraHandle zh,
1388                                    Z_AttributesPlusTerm *zapt,
1389                                    const char *termz,
1390                                    oid_value attributeSet,
1391                                    NMEM stream,
1392                                    int reg_type, int complete_flag,
1393                                    const char *rank_type,
1394                                    int num_bases, char **basenames)
1395 {
1396     char term_dst[IT_MAX_WORD+1];
1397     const char *termp = termz;
1398     RSET rset[60], result;
1399     int i, r, rset_no = 0;
1400     struct grep_info grep_info;
1401
1402 #ifdef TERM_COUNT
1403     grep_info.term_no = 0;
1404 #endif
1405     grep_info.isam_p_size = 0;
1406     grep_info.isam_p_buf = NULL;
1407     grep_info.zh = zh;
1408     grep_info.reg_type = reg_type;
1409
1410     while (1)
1411     { 
1412         logf (LOG_DEBUG, "APT_phrase termp=%s", termp);
1413         grep_info.isam_p_indx = 0;
1414         r = string_term (zh, zapt, &termp, attributeSet, stream, &grep_info,
1415                         reg_type, complete_flag, num_bases, basenames,
1416                         term_dst);
1417         if (r < 1)
1418             break;
1419         logf (LOG_DEBUG, "term: %s", term_dst);
1420         rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1421                                     grep_info.isam_p_indx, term_dst,
1422                                     strlen(term_dst), rank_type);
1423         assert (rset[rset_no]);
1424         if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1425             break;
1426     }
1427 #ifdef TERM_COUNT
1428     xfree(grep_info.term_no);
1429 #endif
1430     xfree (grep_info.isam_p_buf);
1431     if (rset_no == 0)
1432     {
1433         rset_null_parms parms;
1434         
1435         parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1436         return rset_create (rset_kind_null, &parms);
1437     }
1438     else if (rset_no == 1)
1439         return (rset[0]);
1440     result = rpn_prox (zh, rset, rset_no);
1441     for (i = 0; i<rset_no; i++)
1442         rset_delete (rset[i]);
1443     return result;
1444 }
1445
1446 static RSET rpn_search_APT_or_list (ZebraHandle zh,
1447                                     Z_AttributesPlusTerm *zapt,
1448                                     const char *termz,
1449                                     oid_value attributeSet,
1450                                     NMEM stream,
1451                                     int reg_type, int complete_flag,
1452                                     const char *rank_type,
1453                                     int num_bases, char **basenames)
1454 {
1455     char term_dst[IT_MAX_WORD+1];
1456     const char *termp = termz;
1457     RSET rset[60], result;
1458     int i, r, rset_no = 0;
1459     struct grep_info grep_info;
1460
1461 #ifdef TERM_COUNT
1462     grep_info.term_no = 0;
1463 #endif
1464     grep_info.isam_p_size = 0;
1465     grep_info.isam_p_buf = NULL;
1466     grep_info.zh = zh;
1467     grep_info.reg_type = reg_type;
1468
1469     while (1)
1470     { 
1471         logf (LOG_DEBUG, "APT_or_list termp=%s", termp);
1472         grep_info.isam_p_indx = 0;
1473         r = string_term (zh, zapt, &termp, attributeSet, stream, &grep_info,
1474                         reg_type, complete_flag, num_bases, basenames,
1475                         term_dst);
1476         if (r < 1)
1477             break;
1478         logf (LOG_DEBUG, "term: %s", term_dst);
1479         rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1480                                     grep_info.isam_p_indx, term_dst,
1481                                     strlen(term_dst), rank_type);
1482         assert (rset[rset_no]);
1483         if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1484             break;
1485     }
1486 #ifdef TERM_COUNT
1487     xfree(grep_info.term_no);
1488 #endif
1489     xfree (grep_info.isam_p_buf);
1490     if (rset_no == 0)
1491     {
1492         rset_null_parms parms;
1493         
1494         parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1495         return rset_create (rset_kind_null, &parms);
1496     }
1497     result = rset[0];
1498     for (i = 1; i<rset_no; i++)
1499     {
1500         rset_bool_parms bool_parms;
1501
1502         bool_parms.rset_l = result;
1503         bool_parms.rset_r = rset[i];
1504         bool_parms.key_size = sizeof(struct it_key);
1505         bool_parms.cmp = key_compare_it;
1506         result = rset_create (rset_kind_or, &bool_parms);
1507     }
1508     return result;
1509 }
1510
1511 static RSET rpn_search_APT_and_list (ZebraHandle zh,
1512                                      Z_AttributesPlusTerm *zapt,
1513                                      const char *termz,
1514                                      oid_value attributeSet,
1515                                      NMEM stream,
1516                                      int reg_type, int complete_flag,
1517                                      const char *rank_type,
1518                                      int num_bases, char **basenames)
1519 {
1520     char term_dst[IT_MAX_WORD+1];
1521     const char *termp = termz;
1522     RSET rset[60], result;
1523     int i, r, rset_no = 0;
1524     struct grep_info grep_info;
1525
1526 #ifdef TERM_COUNT
1527     grep_info.term_no = 0;
1528 #endif
1529     grep_info.isam_p_size = 0;
1530     grep_info.isam_p_buf = NULL;
1531     grep_info.zh = zh;
1532     grep_info.reg_type = reg_type;
1533
1534     while (1)
1535     { 
1536         logf (LOG_DEBUG, "APT_and_list termp=%s", termp);
1537         grep_info.isam_p_indx = 0;
1538         r = string_term (zh, zapt, &termp, attributeSet, stream, &grep_info,
1539                         reg_type, complete_flag, num_bases, basenames,
1540                         term_dst);
1541         if (r < 1)
1542             break;
1543         logf (LOG_DEBUG, "term: %s", term_dst);
1544         rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1545                                     grep_info.isam_p_indx, term_dst,
1546                                     strlen(term_dst), rank_type);
1547         assert (rset[rset_no]);
1548         if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1549             break;
1550     }
1551 #ifdef TERM_COUNT
1552     xfree(grep_info.term_no);
1553 #endif
1554     xfree (grep_info.isam_p_buf);
1555     if (rset_no == 0)
1556     {
1557         rset_null_parms parms;
1558         
1559         parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1560         return rset_create (rset_kind_null, &parms);
1561     }
1562     result = rset[0];
1563     for (i = 1; i<rset_no; i++)
1564     {
1565         rset_bool_parms bool_parms;
1566
1567         bool_parms.rset_l = result;
1568         bool_parms.rset_r = rset[i];
1569         bool_parms.key_size = sizeof(struct it_key);
1570         bool_parms.cmp = key_compare_it;
1571         result = rset_create (rset_kind_and, &bool_parms);
1572     }
1573     return result;
1574 }
1575
1576 static int numeric_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1577                              const char **term_sub,
1578                              char *term_dict,
1579                              oid_value attributeSet,
1580                              struct grep_info *grep_info,
1581                              int *max_pos,
1582                              int reg_type,
1583                              char *term_dst)
1584 {
1585     AttrType relation;
1586     int relation_value;
1587     int term_value;
1588     int r;
1589     char *term_tmp = term_dict + strlen(term_dict);
1590
1591     attr_init (&relation, zapt, 2);
1592     relation_value = attr_find (&relation, NULL);
1593
1594     logf (LOG_DEBUG, "numeric relation value=%d", relation_value);
1595
1596     if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_tmp, 1,
1597                    term_dst))
1598         return 0;
1599     term_value = atoi (term_tmp);
1600     switch (relation_value)
1601     {
1602     case 1:
1603         logf (LOG_DEBUG, "Relation <");
1604         gen_regular_rel (term_tmp, term_value-1, 1);
1605         break;
1606     case 2:
1607         logf (LOG_DEBUG, "Relation <=");
1608         gen_regular_rel (term_tmp, term_value, 1);
1609         break;
1610     case 4:
1611         logf (LOG_DEBUG, "Relation >=");
1612         gen_regular_rel (term_tmp, term_value, 0);
1613         break;
1614     case 5:
1615         logf (LOG_DEBUG, "Relation >");
1616         gen_regular_rel (term_tmp, term_value+1, 0);
1617         break;
1618     case 3:
1619     default:
1620         logf (LOG_DEBUG, "Relation =");
1621         sprintf (term_tmp, "(0*%d)", term_value);
1622     }
1623     logf (LOG_DEBUG, "dict_lookup_grep: %s", term_tmp);
1624     r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info, max_pos,
1625                           0, grep_handle);
1626     if (r)
1627         logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
1628     logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1629     return 1;
1630 }
1631
1632 static int numeric_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1633                          const char **term_sub, 
1634                          oid_value attributeSet, struct grep_info *grep_info,
1635                          int reg_type, int complete_flag,
1636                          int num_bases, char **basenames,
1637                          char *term_dst)
1638 {
1639     char term_dict[2*IT_MAX_WORD+2];
1640     int r, base_no;
1641     AttrType use;
1642     int use_value;
1643     oid_value curAttributeSet = attributeSet;
1644     const char *termp;
1645     struct rpn_char_map_info rcmi;
1646
1647     rpn_char_map_prepare (zh, reg_type, &rcmi);
1648     attr_init (&use, zapt, 1);
1649     use_value = attr_find (&use, &curAttributeSet);
1650     logf (LOG_DEBUG, "numeric_term, use value %d", use_value);
1651
1652     if (use_value == -1)
1653         use_value = 1016;
1654
1655     for (base_no = 0; base_no < num_bases; base_no++)
1656     {
1657         attent attp;
1658         data1_local_attribute *local_attr;
1659         int max_pos, prefix_len = 0;
1660
1661         termp = *term_sub;
1662         if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
1663         {
1664             logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
1665                   curAttributeSet, use_value, r);
1666             if (r == -1)
1667                 zh->errCode = 114;
1668             else
1669                 zh->errCode = 121;
1670             return -1;
1671         }
1672         if (zebraExplain_curDatabase (zh->zei, basenames[base_no]))
1673         {
1674             zh->errCode = 109; /* Database unavailable */
1675             zh->errString = basenames[base_no];
1676             return -1;
1677         }
1678         for (local_attr = attp.local_attributes; local_attr;
1679              local_attr = local_attr->next)
1680         {
1681             int ord;
1682             char ord_buf[32];
1683             int i, ord_len;
1684
1685             ord = zebraExplain_lookupSU (zh->zei, attp.attset_ordinal,
1686                                           local_attr->local);
1687             if (ord < 0)
1688                 continue;
1689             if (prefix_len)
1690                 term_dict[prefix_len++] = '|';
1691             else
1692                 term_dict[prefix_len++] = '(';
1693
1694             ord_len = key_SU_code (ord, ord_buf);
1695             for (i = 0; i<ord_len; i++)
1696             {
1697                 term_dict[prefix_len++] = 1;
1698                 term_dict[prefix_len++] = ord_buf[i];
1699             }
1700         }
1701         if (!prefix_len)
1702         {
1703             zh->errCode = 114;
1704             return -1;
1705         }
1706         term_dict[prefix_len++] = ')';        
1707         term_dict[prefix_len++] = 1;
1708         term_dict[prefix_len++] = reg_type;
1709         logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1710         term_dict[prefix_len] = '\0';
1711         if (!numeric_relation (zh, zapt, &termp, term_dict,
1712                                attributeSet, grep_info, &max_pos, reg_type,
1713                                term_dst))
1714             return 0;
1715     }
1716     *term_sub = termp;
1717     logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1718     return 1;
1719 }
1720
1721 static RSET rpn_search_APT_numeric (ZebraHandle zh,
1722                                     Z_AttributesPlusTerm *zapt,
1723                                     const char *termz,
1724                                     oid_value attributeSet,
1725                                     NMEM stream,
1726                                     int reg_type, int complete_flag,
1727                                     const char *rank_type,
1728                                     int num_bases, char **basenames)
1729 {
1730     char term_dst[IT_MAX_WORD+1];
1731     const char *termp = termz;
1732     RSET rset[60], result;
1733     int i, r, rset_no = 0;
1734     struct grep_info grep_info;
1735
1736 #ifdef TERM_COUNT
1737     grep_info.term_no = 0;
1738 #endif
1739     grep_info.isam_p_size = 0;
1740     grep_info.isam_p_buf = NULL;
1741     grep_info.zh = zh;
1742     grep_info.reg_type = reg_type;
1743
1744     while (1)
1745     { 
1746         logf (LOG_DEBUG, "APT_numeric termp=%s", termp);
1747         grep_info.isam_p_indx = 0;
1748         r = numeric_term (zh, zapt, &termp, attributeSet, &grep_info,
1749                           reg_type, complete_flag, num_bases, basenames,
1750                           term_dst);
1751         if (r < 1)
1752             break;
1753         logf (LOG_DEBUG, "term: %s", term_dst);
1754         rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1755                                     grep_info.isam_p_indx, term_dst,
1756                                     strlen(term_dst), rank_type);
1757         assert (rset[rset_no]);
1758         if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1759             break;
1760     }
1761 #ifdef TERM_COUNT
1762     xfree(grep_info.term_no);
1763 #endif
1764     xfree (grep_info.isam_p_buf);
1765     if (rset_no == 0)
1766     {
1767         rset_null_parms parms;
1768         
1769         parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1770         return rset_create (rset_kind_null, &parms);
1771     }
1772     result = rset[0];
1773     for (i = 1; i<rset_no; i++)
1774     {
1775         rset_bool_parms bool_parms;
1776
1777         bool_parms.rset_l = result;
1778         bool_parms.rset_r = rset[i];
1779         bool_parms.key_size = sizeof(struct it_key);
1780         bool_parms.cmp = key_compare_it;
1781         result = rset_create (rset_kind_and, &bool_parms);
1782     }
1783     return result;
1784 }
1785
1786 static RSET rpn_search_APT_local (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1787                                   const char *termz,
1788                                   oid_value attributeSet,
1789                                   NMEM stream,
1790                                   const char *rank_type)
1791 {
1792     RSET result;
1793     RSFD rsfd;
1794     struct it_key key;
1795     rset_temp_parms parms;
1796
1797     parms.rset_term = rset_term_create (termz, -1, rank_type);
1798     parms.key_size = sizeof (struct it_key);
1799     parms.temp_path = res_get (zh->res, "setTmpDir");
1800     result = rset_create (rset_kind_temp, &parms);
1801     rsfd = rset_open (result, RSETF_WRITE);
1802
1803     key.sysno = atoi (termz);
1804     key.seqno = 1;
1805     if (key.sysno <= 0)
1806         key.sysno = 1;
1807     rset_write (result, rsfd, &key);
1808     rset_close (result, rsfd);
1809     return result;
1810 }
1811
1812 static RSET rpn_sort_spec (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1813                            oid_value attributeSet, NMEM stream,
1814                            Z_SortKeySpecList *sort_sequence,
1815                            const char *rank_type)
1816 {
1817     rset_null_parms parms;    
1818     int i;
1819     int sort_relation_value;
1820     AttrType sort_relation_type;
1821     int use_value;
1822     AttrType use_type;
1823     Z_SortKeySpec *sks;
1824     Z_SortKey *sk;
1825     Z_AttributeElement *ae;
1826     int oid[OID_SIZE];
1827     oident oe;
1828     
1829     attr_init (&sort_relation_type, zapt, 7);
1830     sort_relation_value = attr_find (&sort_relation_type, &attributeSet);
1831
1832     attr_init (&use_type, zapt, 1);
1833     use_value = attr_find (&use_type, &attributeSet);
1834
1835     if (!sort_sequence->specs)
1836     {
1837         sort_sequence->num_specs = 10;
1838         sort_sequence->specs = (Z_SortKeySpec **)
1839             nmem_malloc (stream, sort_sequence->num_specs *
1840                          sizeof(*sort_sequence->specs));
1841         for (i = 0; i<sort_sequence->num_specs; i++)
1842             sort_sequence->specs[i] = 0;
1843     }
1844     if (zapt->term->which != Z_Term_general)
1845         i = 0;
1846     else
1847         i = atoi_n ((char *) zapt->term->u.general->buf,
1848                     zapt->term->u.general->len);
1849     if (i >= sort_sequence->num_specs)
1850         i = 0;
1851
1852     oe.proto = PROTO_Z3950;
1853     oe.oclass = CLASS_ATTSET;
1854     oe.value = attributeSet;
1855     if (!oid_ent_to_oid (&oe, oid))
1856         return 0;
1857
1858     sks = (Z_SortKeySpec *) nmem_malloc (stream, sizeof(*sks));
1859     sks->sortElement = (Z_SortElement *)
1860         nmem_malloc (stream, sizeof(*sks->sortElement));
1861     sks->sortElement->which = Z_SortElement_generic;
1862     sk = sks->sortElement->u.generic = (Z_SortKey *)
1863         nmem_malloc (stream, sizeof(*sk));
1864     sk->which = Z_SortKey_sortAttributes;
1865     sk->u.sortAttributes = (Z_SortAttributes *)
1866         nmem_malloc (stream, sizeof(*sk->u.sortAttributes));
1867
1868     sk->u.sortAttributes->id = oid;
1869     sk->u.sortAttributes->list = (Z_AttributeList *)
1870         nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list));
1871     sk->u.sortAttributes->list->num_attributes = 1;
1872     sk->u.sortAttributes->list->attributes = (Z_AttributeElement **)
1873         nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list->attributes));
1874     ae = *sk->u.sortAttributes->list->attributes = (Z_AttributeElement *)
1875         nmem_malloc (stream, sizeof(**sk->u.sortAttributes->list->attributes));
1876     ae->attributeSet = 0;
1877     ae->attributeType = (int *)
1878         nmem_malloc (stream, sizeof(*ae->attributeType));
1879     *ae->attributeType = 1;
1880     ae->which = Z_AttributeValue_numeric;
1881     ae->value.numeric = (int *)
1882         nmem_malloc (stream, sizeof(*ae->value.numeric));
1883     *ae->value.numeric = use_value;
1884
1885     sks->sortRelation = (int *)
1886         nmem_malloc (stream, sizeof(*sks->sortRelation));
1887     if (sort_relation_value == 1)
1888         *sks->sortRelation = Z_SortRelation_ascending;
1889     else if (sort_relation_value == 2)
1890         *sks->sortRelation = Z_SortRelation_descending;
1891     else 
1892         *sks->sortRelation = Z_SortRelation_ascending;
1893
1894     sks->caseSensitivity = (int *)
1895         nmem_malloc (stream, sizeof(*sks->caseSensitivity));
1896     *sks->caseSensitivity = 0;
1897
1898 #ifdef ASN_COMPILED
1899     sks->which = Z_SortKeySpec_null;
1900     sks->u.null = odr_nullval ();
1901 #else
1902     sks->missingValueAction = 0;
1903 #endif
1904
1905     sort_sequence->specs[i] = sks;
1906
1907     parms.rset_term = rset_term_create ("", -1, rank_type);
1908     return rset_create (rset_kind_null, &parms);
1909 }
1910
1911
1912 static RSET rpn_search_APT (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1913                             oid_value attributeSet, NMEM stream,
1914                             Z_SortKeySpecList *sort_sequence,
1915                             int num_bases, char **basenames)
1916 {
1917     unsigned reg_id;
1918     char *search_type = NULL;
1919     char *rank_type = NULL;
1920     int complete_flag;
1921     int sort_flag;
1922     char termz[IT_MAX_WORD+1];
1923
1924     zebra_maps_attr (zh->zebra_maps, zapt, &reg_id, &search_type,
1925                      &rank_type, &complete_flag, &sort_flag);
1926     
1927     logf (LOG_DEBUG, "reg_id=%c", reg_id);
1928     logf (LOG_DEBUG, "complete_flag=%d", complete_flag);
1929     logf (LOG_DEBUG, "search_type=%s", search_type);
1930     logf (LOG_DEBUG, "rank_type=%s", rank_type);
1931
1932     if (zapt->term->which != Z_Term_general)
1933     {
1934         zh->errCode = 124;
1935         return NULL;
1936     }
1937     trans_term (zh, zapt, termz);
1938
1939     if (sort_flag)
1940         return rpn_sort_spec (zh, zapt, attributeSet, stream, sort_sequence,
1941                               rank_type);
1942
1943     if (!strcmp (search_type, "phrase"))
1944     {
1945         return rpn_search_APT_phrase (zh, zapt, termz, attributeSet, stream,
1946                                       reg_id, complete_flag, rank_type,
1947                                       num_bases, basenames);
1948     }
1949     else if (!strcmp (search_type, "and-list"))
1950     {
1951         return rpn_search_APT_and_list (zh, zapt, termz, attributeSet, stream,
1952                                         reg_id, complete_flag, rank_type,
1953                                         num_bases, basenames);
1954     }
1955     else if (!strcmp (search_type, "or-list"))
1956     {
1957         return rpn_search_APT_or_list (zh, zapt, termz, attributeSet, stream,
1958                                        reg_id, complete_flag, rank_type,
1959                                        num_bases, basenames);
1960     }
1961     else if (!strcmp (search_type, "local"))
1962     {
1963         return rpn_search_APT_local (zh, zapt, termz, attributeSet, stream,
1964                                      rank_type);
1965     }
1966     else if (!strcmp (search_type, "numeric"))
1967     {
1968         return rpn_search_APT_numeric (zh, zapt, termz, attributeSet, stream,
1969                                        reg_id, complete_flag, rank_type,
1970                                        num_bases, basenames);
1971     }
1972     zh->errCode = 118;
1973     return NULL;
1974 }
1975
1976 static RSET rpn_search_structure (ZebraHandle zh, Z_RPNStructure *zs,
1977                                   oid_value attributeSet, NMEM stream,
1978                                   Z_SortKeySpecList *sort_sequence,
1979                                   int num_bases, char **basenames)
1980 {
1981     RSET r = NULL;
1982     if (zs->which == Z_RPNStructure_complex)
1983     {
1984         Z_Operator *zop = zs->u.complex->roperator;
1985         rset_bool_parms bool_parms;
1986
1987         bool_parms.rset_l = rpn_search_structure (zh, zs->u.complex->s1,
1988                                                   attributeSet, stream,
1989                                                   sort_sequence,
1990                                                   num_bases, basenames);
1991         if (bool_parms.rset_l == NULL)
1992             return NULL;
1993         bool_parms.rset_r = rpn_search_structure (zh, zs->u.complex->s2,
1994                                                   attributeSet, stream,
1995                                                   sort_sequence,
1996                                                   num_bases, basenames);
1997         if (bool_parms.rset_r == NULL)
1998         {
1999             rset_delete (bool_parms.rset_l);
2000             return NULL;
2001         }
2002         bool_parms.key_size = sizeof(struct it_key);
2003         bool_parms.cmp = key_compare_it;
2004
2005         switch (zop->which)
2006         {
2007         case Z_Operator_and:
2008             r = rset_create (rset_kind_and, &bool_parms);
2009             break;
2010         case Z_Operator_or:
2011             r = rset_create (rset_kind_or, &bool_parms);
2012             break;
2013         case Z_Operator_and_not:
2014             r = rset_create (rset_kind_not, &bool_parms);
2015             break;
2016         case Z_Operator_prox:
2017 #ifdef ASN_COMPILED
2018             if (zop->u.prox->which != Z_ProximityOperator_known)
2019             {
2020                 zh->errCode = 132;
2021                 return NULL;
2022             }
2023 #else
2024             if (zop->u.prox->which != Z_ProxCode_known)
2025             {
2026                 zh->errCode = 132;
2027                 return NULL;
2028             }
2029 #endif
2030
2031 #ifdef ASN_COMPILED
2032             if (*zop->u.prox->u.known != Z_ProxUnit_word)
2033             {
2034                 char *val = (char *) nmem_malloc (stream, 16);
2035                 zh->errCode = 132;
2036                 zh->errString = val;
2037                 sprintf (val, "%d", *zop->u.prox->u.known);
2038                 return NULL;
2039             }
2040 #else
2041             if (*zop->u.prox->proximityUnitCode != Z_ProxUnit_word)
2042             {
2043                 char *val = (char *) nmem_malloc (stream, 16);
2044                 zh->errCode = 132;
2045                 zh->errString = val;
2046                 sprintf (val, "%d", *zop->u.prox->proximityUnitCode);
2047                 return NULL;
2048             }
2049 #endif
2050             r = rpn_proximity (zh, bool_parms.rset_l, bool_parms.rset_r,
2051                                *zop->u.prox->ordered,
2052                                (!zop->u.prox->exclusion ? 0 :
2053                                          *zop->u.prox->exclusion),
2054                                *zop->u.prox->relationType,
2055                                *zop->u.prox->distance);
2056             break;
2057         default:
2058             zh->errCode = 110;
2059             return NULL;
2060         }
2061     }
2062     else if (zs->which == Z_RPNStructure_simple)
2063     {
2064         if (zs->u.simple->which == Z_Operand_APT)
2065         {
2066             logf (LOG_DEBUG, "rpn_search_APT");
2067             r = rpn_search_APT (zh, zs->u.simple->u.attributesPlusTerm,
2068                                 attributeSet, stream, sort_sequence,
2069                                 num_bases, basenames);
2070         }
2071         else if (zs->u.simple->which == Z_Operand_resultSetId)
2072         {
2073             logf (LOG_DEBUG, "rpn_search_ref");
2074             r = resultSetRef (zh, zs->u.simple->u.resultSetId);
2075             if (!r)
2076                 r = rset_create (rset_kind_null, NULL);
2077         }
2078         else
2079         {
2080             zh->errCode = 3;
2081             return NULL;
2082         }
2083     }
2084     else
2085     {
2086         zh->errCode = 3;
2087         return NULL;
2088     }
2089     return r;
2090 }
2091
2092
2093 RSET rpn_search (ZebraHandle zh, NMEM nmem,
2094                  Z_RPNQuery *rpn, int num_bases, char **basenames, 
2095                  const char *setname,
2096                  ZebraSet sset)
2097 {
2098     RSET rset;
2099     oident *attrset;
2100     oid_value attributeSet;
2101     Z_SortKeySpecList *sort_sequence;
2102     int sort_status, i;
2103
2104     zh->errCode = 0;
2105     zh->errString = NULL;
2106     zh->hits = 0;
2107
2108     sort_sequence = (Z_SortKeySpecList *)
2109         nmem_malloc (nmem, sizeof(*sort_sequence));
2110     sort_sequence->num_specs = 10;
2111     sort_sequence->specs = (Z_SortKeySpec **)
2112         nmem_malloc (nmem, sort_sequence->num_specs *
2113                      sizeof(*sort_sequence->specs));
2114     for (i = 0; i<sort_sequence->num_specs; i++)
2115         sort_sequence->specs[i] = 0;
2116     
2117     attrset = oid_getentbyoid (rpn->attributeSetId);
2118     attributeSet = attrset->value;
2119     rset = rpn_search_structure (zh, rpn->RPNStructure, attributeSet,
2120                                  nmem, sort_sequence, num_bases, basenames);
2121     if (!rset)
2122         return 0;
2123
2124     if (zh->errCode)
2125         logf (LOG_DEBUG, "search error: %d", zh->errCode);
2126     
2127     for (i = 0; sort_sequence->specs[i]; i++)
2128         ;
2129     sort_sequence->num_specs = i;
2130     if (!i)
2131         resultSetRank (zh, sset, rset);
2132     else
2133     {
2134         logf (LOG_DEBUG, "resultSetSortSingle in rpn_search");
2135         resultSetSortSingle (zh, nmem, sset, rset,
2136                              sort_sequence, &sort_status);
2137         if (zh->errCode)
2138         {
2139             logf (LOG_DEBUG, "resultSetSortSingle status = %d", zh->errCode);
2140         }
2141     }
2142     return rset;
2143 }
2144
2145 struct scan_info_entry {
2146     char *term;
2147     ISAM_P isam_p;
2148 };
2149
2150 struct scan_info {
2151     struct scan_info_entry *list;
2152     ODR odr;
2153     int before, after;
2154     char prefix[20];
2155 };
2156
2157 static int scan_handle (char *name, const char *info, int pos, void *client)
2158 {
2159     int len_prefix, idx;
2160     struct scan_info *scan_info = (struct scan_info *) client;
2161
2162     len_prefix = strlen(scan_info->prefix);
2163     if (memcmp (name, scan_info->prefix, len_prefix))
2164         return 1;
2165     if (pos > 0)
2166         idx = scan_info->after - pos + scan_info->before;
2167     else
2168         idx = - pos - 1;
2169     scan_info->list[idx].term = (char *)
2170         odr_malloc (scan_info->odr, strlen(name + len_prefix)+1);
2171     strcpy (scan_info->list[idx].term, name + len_prefix);
2172     assert (*info == sizeof(ISAM_P));
2173     memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAM_P));
2174     return 0;
2175 }
2176
2177 static void scan_term_untrans (ZebraHandle zh, NMEM stream, int reg_type,
2178                                char **dst, const char *src)
2179 {
2180     char term_dst[1024];
2181     
2182     term_untrans (zh, reg_type, term_dst, src);
2183     
2184     *dst = (char *) nmem_malloc (stream, strlen(term_dst)+1);
2185     strcpy (*dst, term_dst);
2186 }
2187
2188 static void count_set (RSET r, int *count)
2189 {
2190     int psysno = 0;
2191     int kno = 0;
2192     struct it_key key;
2193     RSFD rfd;
2194     int term_index;
2195
2196     logf (LOG_DEBUG, "count_set");
2197
2198     *count = 0;
2199     rfd = rset_open (r, RSETF_READ);
2200     while (rset_read (r, rfd, &key, &term_index))
2201     {
2202         if (key.sysno != psysno)
2203         {
2204             psysno = key.sysno;
2205             (*count)++;
2206         }
2207         kno++;
2208     }
2209     rset_close (r, rfd);
2210     logf (LOG_DEBUG, "%d keys, %d records", kno, *count);
2211 }
2212
2213 void rpn_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
2214                oid_value attributeset,
2215                int num_bases, char **basenames,
2216                int *position, int *num_entries, ZebraScanEntry **list,
2217                int *is_partial)
2218 {
2219     int i;
2220     int pos = *position;
2221     int num = *num_entries;
2222     int before;
2223     int after;
2224     int base_no;
2225     char termz[IT_MAX_WORD+20];
2226     AttrType use;
2227     int use_value;
2228     struct scan_info *scan_info_array;
2229     ZebraScanEntry *glist;
2230     int ords[32], ord_no = 0;
2231     int ptr[32];
2232
2233     unsigned reg_id;
2234     char *search_type = NULL;
2235     char *rank_type = NULL;
2236     int complete_flag;
2237     int sort_flag;
2238     *list = 0;
2239
2240     if (attributeset == VAL_NONE)
2241         attributeset = VAL_BIB1;
2242
2243     logf (LOG_DEBUG, "position = %d, num = %d", pos, num);
2244         
2245     attr_init (&use, zapt, 1);
2246     use_value = attr_find (&use, &attributeset);
2247
2248     if (zebra_maps_attr (zh->zebra_maps, zapt, &reg_id, &search_type,
2249                          &rank_type, &complete_flag, &sort_flag))
2250     {
2251         zh->errCode = 113;
2252         return ;
2253     }
2254
2255     if (use_value == -1)
2256         use_value = 1016;
2257     for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
2258     {
2259         int r;
2260         attent attp;
2261         data1_local_attribute *local_attr;
2262
2263         if ((r=att_getentbyatt (zh, &attp, attributeset, use_value)))
2264         {
2265             logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
2266                   attributeset, use_value);
2267             if (r == -1)
2268                 zh->errCode = 114;
2269             else
2270                 zh->errCode = 121;
2271             *num_entries = 0;
2272             return;
2273         }
2274         if (zebraExplain_curDatabase (zh->zei, basenames[base_no]))
2275         {
2276             zh->errString = basenames[base_no];
2277             zh->errCode = 109; /* Database unavailable */
2278             return;
2279         }
2280         for (local_attr = attp.local_attributes; local_attr && ord_no < 32;
2281              local_attr = local_attr->next)
2282         {
2283             int ord;
2284
2285             ord = zebraExplain_lookupSU (zh->zei, attp.attset_ordinal,
2286                                          local_attr->local);
2287             if (ord > 0)
2288                 ords[ord_no++] = ord;
2289         }
2290     }
2291     if (ord_no == 0)
2292     {
2293         zh->errCode = 113;
2294         return;
2295     }
2296     /* prepare dictionary scanning */
2297     before = pos-1;
2298     after = 1+num-pos;
2299     scan_info_array = (struct scan_info *)
2300         odr_malloc (stream, ord_no * sizeof(*scan_info_array));
2301     for (i = 0; i < ord_no; i++)
2302     {
2303         int j, prefix_len = 0;
2304         int before_tmp = before, after_tmp = after;
2305         struct scan_info *scan_info = scan_info_array + i;
2306         struct rpn_char_map_info rcmi;
2307
2308         rpn_char_map_prepare (zh, reg_id, &rcmi);
2309
2310         scan_info->before = before;
2311         scan_info->after = after;
2312         scan_info->odr = stream;
2313
2314         scan_info->list = (struct scan_info_entry *)
2315             odr_malloc (stream, (before+after) * sizeof(*scan_info->list));
2316         for (j = 0; j<before+after; j++)
2317             scan_info->list[j].term = NULL;
2318
2319         prefix_len += key_SU_code (ords[i], termz + prefix_len);
2320         termz[prefix_len++] = reg_id;
2321         termz[prefix_len] = 0;
2322         strcpy (scan_info->prefix, termz);
2323
2324         trans_scan_term (zh, zapt, termz+prefix_len, reg_id);
2325                     
2326         dict_scan (zh->dict, termz, &before_tmp, &after_tmp, scan_info,
2327                    scan_handle);
2328     }
2329     glist = (ZebraScanEntry *)
2330         odr_malloc (stream, (before+after)*sizeof(*glist));
2331
2332     /* consider terms after main term */
2333     for (i = 0; i < ord_no; i++)
2334         ptr[i] = before;
2335     
2336     *is_partial = 0;
2337     for (i = 0; i<after; i++)
2338     {
2339         int j, j0 = -1;
2340         const char *mterm = NULL;
2341         const char *tst;
2342         RSET rset;
2343         
2344         for (j = 0; j < ord_no; j++)
2345         {
2346             if (ptr[j] < before+after &&
2347                 (tst=scan_info_array[j].list[ptr[j]].term) &&
2348                 (!mterm || strcmp (tst, mterm) < 0))
2349             {
2350                 j0 = j;
2351                 mterm = tst;
2352             }
2353         }
2354         if (j0 == -1)
2355             break;
2356         scan_term_untrans (zh, stream->mem, reg_id,
2357                            &glist[i+before].term, mterm);
2358         rset = rset_trunc (zh, &scan_info_array[j0].list[ptr[j0]].isam_p, 1,
2359                            glist[i+before].term, strlen(glist[i+before].term),
2360                            NULL);
2361
2362         ptr[j0]++;
2363         for (j = j0+1; j<ord_no; j++)
2364         {
2365             if (ptr[j] < before+after &&
2366                 (tst=scan_info_array[j].list[ptr[j]].term) &&
2367                 !strcmp (tst, mterm))
2368             {
2369                 rset_bool_parms bool_parms;
2370                 RSET rset2;
2371
2372                 rset2 =
2373                    rset_trunc (zh, &scan_info_array[j].list[ptr[j]].isam_p, 1,
2374                                glist[i+before].term,
2375                                strlen(glist[i+before].term), NULL);
2376
2377                 bool_parms.key_size = sizeof(struct it_key);
2378                 bool_parms.cmp = key_compare_it;
2379                 bool_parms.rset_l = rset;
2380                 bool_parms.rset_r = rset2;
2381               
2382                 rset = rset_create (rset_kind_or, &bool_parms);
2383
2384                 ptr[j]++;
2385             }
2386         }
2387         count_set (rset, &glist[i+before].occurrences);
2388         rset_delete (rset);
2389     }
2390     if (i < after)
2391     {
2392         *num_entries -= (after-i);
2393         *is_partial = 1;
2394     }
2395
2396     /* consider terms before main term */
2397     for (i = 0; i<ord_no; i++)
2398         ptr[i] = 0;
2399
2400     for (i = 0; i<before; i++)
2401     {
2402         int j, j0 = -1;
2403         const char *mterm = NULL;
2404         const char *tst;
2405         RSET rset;
2406         
2407         for (j = 0; j <ord_no; j++)
2408         {
2409             if (ptr[j] < before &&
2410                 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2411                 (!mterm || strcmp (tst, mterm) > 0))
2412             {
2413                 j0 = j;
2414                 mterm = tst;
2415             }
2416         }
2417         if (j0 == -1)
2418             break;
2419
2420         scan_term_untrans (zh, stream->mem, reg_id,
2421                            &glist[before-1-i].term, mterm);
2422
2423         rset = rset_trunc
2424                (zh, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1,
2425                 glist[before-1-i].term, strlen(glist[before-1-i].term),
2426                 NULL);
2427
2428         ptr[j0]++;
2429
2430         for (j = j0+1; j<ord_no; j++)
2431         {
2432             if (ptr[j] < before &&
2433                 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2434                 !strcmp (tst, mterm))
2435             {
2436                 rset_bool_parms bool_parms;
2437                 RSET rset2;
2438
2439                 rset2 = rset_trunc (zh,
2440                          &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1,
2441                                     glist[before-1-i].term,
2442                                     strlen(glist[before-1-i].term), NULL);
2443
2444                 bool_parms.key_size = sizeof(struct it_key);
2445                 bool_parms.cmp = key_compare_it;
2446                 bool_parms.rset_l = rset;
2447                 bool_parms.rset_r = rset2;
2448               
2449                 rset = rset_create (rset_kind_or, &bool_parms);
2450
2451                 ptr[j]++;
2452             }
2453         }
2454         count_set (rset, &glist[before-1-i].occurrences);
2455         rset_delete (rset);
2456     }
2457     i = before-i;
2458     if (i)
2459     {
2460         *is_partial = 1;
2461         *position -= i;
2462         *num_entries -= i;
2463     }
2464     *list = glist + i;               /* list is set to first 'real' entry */
2465     
2466     logf (LOG_DEBUG, "position = %d, num_entries = %d",
2467           *position, *num_entries);
2468     if (zh->errCode)
2469         logf (LOG_DEBUG, "scan error: %d", zh->errCode);
2470 }
2471