Implemented data1_add_insert_taggeddata utility which is more flexible
[yaz-moved-to-github.git] / ccl / cclfind.c
1 /*
2  * Copyright (c) 1995, the EUROPAGATE consortium (see below).
3  *
4  * The EUROPAGATE consortium members are:
5  *
6  *    University College Dublin
7  *    Danmarks Teknologiske Videnscenter
8  *    An Chomhairle Leabharlanna
9  *    Consejo Superior de Investigaciones Cientificas
10  *
11  * Permission to use, copy, modify, distribute, and sell this software and
12  * its documentation, in whole or in part, for any purpose, is hereby granted,
13  * provided that:
14  *
15  * 1. This copyright and permission notice appear in all copies of the
16  * software and its documentation. Notices of copyright or attribution
17  * which appear at the beginning of any file must remain unchanged.
18  *
19  * 2. The names of EUROPAGATE or the project partners may not be used to
20  * endorse or promote products derived from this software without specific
21  * prior written permission.
22  *
23  * 3. Users of this software (implementors and gateway operators) agree to
24  * inform the EUROPAGATE consortium of their use of the software. This
25  * information will be used to evaluate the EUROPAGATE project and the
26  * software, and to plan further developments. The consortium may use
27  * the information in later publications.
28  * 
29  * 4. Users of this software agree to make their best efforts, when
30  * documenting their use of the software, to acknowledge the EUROPAGATE
31  * consortium, and the role played by the software in their work.
32  *
33  * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTY OF ANY KIND,
34  * EXPRESS, IMPLIED, OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
35  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
36  * IN NO EVENT SHALL THE EUROPAGATE CONSORTIUM OR ITS MEMBERS BE LIABLE
37  * FOR ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF
38  * ANY KIND, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
39  * OR PROFITS, WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND
40  * ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
41  * USE OR PERFORMANCE OF THIS SOFTWARE.
42  *
43  */
44 /* CCL find (to rpn conversion)
45  * Europagate, 1995
46  *
47  * $Log: cclfind.c,v $
48  * Revision 1.10  1998-02-11 11:53:33  adam
49  * Changed code so that it compiles as C++.
50  *
51  * Revision 1.9  1997/09/29 08:56:37  adam
52  * Changed CCL parser to be thread safe. New type, CCL_parser, declared
53  * and a create/destructers ccl_parser_create/ccl_parser/destory has
54  * been added.
55  *
56  * Revision 1.8  1997/09/01 08:48:11  adam
57  * New windows NT/95 port using MSV5.0. Only a few changes made
58  * to avoid warnings.
59  *
60  * Revision 1.7  1997/05/14 06:53:26  adam
61  * C++ support.
62  *
63  * Revision 1.6  1997/04/30 08:52:06  quinn
64  * Null
65  *
66  * Revision 1.5  1996/10/11  15:00:24  adam
67  * CCL parser from Europagate Email gateway 1.0.
68  *
69  * Revision 1.16  1996/01/08  08:41:13  adam
70  * Removed unused function.
71  *
72  * Revision 1.15  1995/07/20  08:14:34  adam
73  * Qualifiers were observed too often. Instead tokens are treated as
74  * qualifiers only when separated by comma.
75  *
76  * Revision 1.14  1995/05/16  09:39:26  adam
77  * LICENSE.
78  *
79  * Revision 1.13  1995/04/17  09:31:42  adam
80  * Improved handling of qualifiers. Aliases or reserved words.
81  *
82  * Revision 1.12  1995/03/20  15:27:43  adam
83  * Minor changes.
84  *
85  * Revision 1.11  1995/02/23  08:31:59  adam
86  * Changed header.
87  *
88  * Revision 1.9  1995/02/16  13:20:06  adam
89  * Spell fix.
90  *
91  * Revision 1.8  1995/02/14  19:59:42  adam
92  * Removed a syntax error.
93  *
94  * Revision 1.7  1995/02/14  19:55:10  adam
95  * Header files ccl.h/cclp.h are gone! They have been merged an
96  * moved to ../include/ccl.h.
97  * Node kind(s) in ccl_rpn_node have changed names.
98  *
99  * Revision 1.6  1995/02/14  16:20:55  adam
100  * Qualifiers are read from a file now.
101  *
102  * Revision 1.5  1995/02/14  14:12:41  adam
103  * Ranges for ordered qualfiers implemented (e.g. pd=1980-1990).
104  *
105  * Revision 1.4  1995/02/14  13:16:29  adam
106  * Left and/or right truncation implemented.
107  *
108  * Revision 1.3  1995/02/14  10:25:56  adam
109  * The constructions 'qualifier rel term ...' implemented.
110  *
111  * Revision 1.2  1995/02/13  15:15:07  adam
112  * Added handling of qualifiers. Not finished yet.
113  *
114  * Revision 1.1  1995/02/13  12:35:20  adam
115  * First version of CCL. Qualifiers aren't handled yet.
116  *
117  */
118
119 #include <stdio.h>
120 #include <stdlib.h>
121 #include <assert.h>
122 #include <string.h>
123
124 #include <ccl.h>
125
126 /* returns type of current lookahead */
127 #define KIND (cclp->look_token->kind)
128
129 /* move one token forward */
130 #define ADVANCE cclp->look_token = cclp->look_token->next
131
132 /* 
133  * qual_val_type: test for existance of attribute type/value pair.
134  * qa:     Attribute array
135  * type:   Type of attribute to search for
136  * value:  Value of attribute to seach for
137  * return: 1 if found; 0 otherwise.
138  */
139 static int qual_val_type (struct ccl_rpn_attr **qa, int type, int value)
140 {
141     int i;
142     struct ccl_rpn_attr *q;
143
144     if (!qa)
145         return 0;
146     for (i = 0;  (q=qa[i]); i++)
147         while (q)
148         {
149             if (q->type == type && q->value == value)
150                 return 1;
151             q = q->next;
152         }
153     return 0;
154 }
155
156 /*
157  * strxcat: concatenate strings.
158  * n:      Null-terminated Destination string 
159  * src:    Source string to be appended (not null-terminated)
160  * len:    Length of source string.
161  */
162 static void strxcat (char *n, const char *src, int len)
163 {
164     while (*n)
165         n++;
166     while (--len >= 0)
167         *n++ = *src++;
168     *n = '\0';
169 }
170
171 /*
172  * copy_token_name: Return copy of CCL token name
173  * tp:      Pointer to token info.
174  * return:  malloc(3) allocated copy of token name.
175  */
176 static char *copy_token_name (struct ccl_token *tp)
177 {
178     char *str = (char *)malloc (tp->len + 1);
179     assert (str);
180     memcpy (str, tp->name, tp->len);
181     str[tp->len] = '\0';
182     return str;
183 }
184
185 /*
186  * mk_node: Create RPN node.
187  * kind:   Type of node.
188  * return: pointer to allocated node.
189  */
190 static struct ccl_rpn_node *mk_node (int kind)
191 {
192     struct ccl_rpn_node *p;
193     p = (struct ccl_rpn_node *)malloc (sizeof(*p));
194     assert (p);
195     p->kind = kind;
196     return p;
197 }
198
199 /*
200  * ccl_rpn_delete: Delete RPN tree.
201  * rpn:   Pointer to tree.
202  */
203 void ccl_rpn_delete (struct ccl_rpn_node *rpn)
204 {
205     struct ccl_rpn_attr *attr, *attr1;
206     if (!rpn)
207         return;
208     switch (rpn->kind)
209     {
210     case CCL_RPN_AND:
211     case CCL_RPN_OR:
212     case CCL_RPN_NOT:
213         ccl_rpn_delete (rpn->u.p[0]);
214         ccl_rpn_delete (rpn->u.p[1]);
215         break;
216     case CCL_RPN_TERM:
217         free (rpn->u.t.term);
218         for (attr = rpn->u.t.attr_list; attr; attr = attr1)
219         {
220             attr1 = attr->next;
221             free (attr);
222         }
223         break;
224     case CCL_RPN_SET:
225         free (rpn->u.setname);
226         break;
227     case CCL_RPN_PROX:
228         ccl_rpn_delete (rpn->u.p[0]);
229         ccl_rpn_delete (rpn->u.p[1]);
230         break;
231     }
232     free (rpn);
233 }
234
235 static struct ccl_rpn_node *find_spec (CCL_parser cclp,
236                                        struct ccl_rpn_attr **qa);
237 static struct ccl_rpn_node *search_terms (CCL_parser cclp,
238                                           struct ccl_rpn_attr **qa);
239
240 /*
241  * add_attr: Add attribute (type/value) to RPN term node.
242  * p:     RPN node of type term.
243  * type:  Type of attribute
244  * value: Value of attribute
245  */
246 static void add_attr (struct ccl_rpn_node *p, int type, int value)
247 {
248     struct ccl_rpn_attr *n;
249
250     n = (struct ccl_rpn_attr *)malloc (sizeof(*n));
251     assert (n);
252     n->type = type;
253     n->value = value;
254     n->next = p->u.t.attr_list;
255     p->u.t.attr_list = n;
256 }
257
258 /*
259  * search_term: Parse CCL search term. 
260  * cclp:   CCL Parser
261  * qa:     Qualifier attributes already applied.
262  * return: pointer to node(s); NULL on error.
263  */
264 static struct ccl_rpn_node *search_term (CCL_parser cclp,
265                                          struct ccl_rpn_attr **qa)
266 {
267     struct ccl_rpn_node *p;
268     struct ccl_token *lookahead = cclp->look_token;
269     int len = 0;
270     size_t no, i;
271     int left_trunc = 0;
272     int right_trunc = 0;
273     int mid_trunc = 0;
274     int relation_value = -1;
275     int position_value = -1;
276     int structure_value = -1;
277     int truncation_value = -1;
278     int completeness_value = -1;
279
280     if (KIND != CCL_TOK_TERM)
281     {
282         cclp->error_code = CCL_ERR_TERM_EXPECTED;
283         return NULL;
284     }
285     /* create the term node, but wait a moment before adding the term */
286     p = mk_node (CCL_RPN_TERM);
287     p->u.t.attr_list = NULL;
288     p->u.t.term = NULL;
289
290     if (!qa)
291     {
292         /* no qualifier(s) applied. Use 'term' if it is defined */
293
294         qa = (struct ccl_rpn_attr **)malloc (2*sizeof(*qa));
295         assert (qa);
296         qa[0] = ccl_qual_search (cclp, "term", 4);
297         qa[1] = NULL;
298     }
299
300     /* go through all attributes and add them to the attribute list */
301     for (i=0; qa && qa[i]; i++)
302     {
303         struct ccl_rpn_attr *attr;
304
305         for (attr = qa[i]; attr; attr = attr->next)
306             if (attr->value > 0)
307             {   /* deal only with REAL attributes (positive) */
308                 switch (attr->type)
309                 {
310                 case CCL_BIB1_REL:
311                     if (relation_value != -1)
312                         continue;
313                     relation_value = attr->value;
314                     break;
315                 case CCL_BIB1_POS:
316                     if (position_value != -1)
317                         continue;
318                     position_value = attr->value;
319                     break;
320                 case CCL_BIB1_STR:
321                     if (structure_value != -1)
322                         continue;
323                     structure_value = attr->value;
324                     break;
325                 case CCL_BIB1_TRU:
326                     if (truncation_value != -1)
327                         continue;
328                     truncation_value = attr->value;
329                     break;
330                 case CCL_BIB1_COM:
331                     if (completeness_value != -1)
332                         continue;
333                     completeness_value = attr->value;
334                     break;
335                 }
336                 add_attr (p, attr->type, attr->value);
337             }
338     }
339     /* go through each TERM token. If no truncation attribute is yet
340        met, then look for left/right truncation markers (?) and
341        set left_trunc/right_trunc/mid_trunc accordingly */
342     for (no = 0; lookahead->kind == CCL_TOK_TERM; no++)
343     {
344         for (i = 0; i<lookahead->len; i++)
345             if (truncation_value == -1 && lookahead->name[i] == '?')
346             {
347                 if (no == 0 && i == 0 && lookahead->len >= 1)
348                     left_trunc = 1;
349                 else if (lookahead->next->kind != CCL_TOK_TERM &&
350                          i == lookahead->len-1 && i >= 1)
351                     right_trunc = 1;
352                 else
353                     mid_trunc = 1;
354             }
355         len += 1+lookahead->len;
356         lookahead = lookahead->next;
357     }
358     /* len now holds the number of characters in the RPN term */
359     /* no holds the number of CCL tokens (1 or more) */
360
361     if (structure_value == -1 && 
362         qual_val_type (qa, CCL_BIB1_STR, CCL_BIB1_STR_WP))
363     {   /* no structure attribute met. Apply either structure attribute 
364            WORD or PHRASE depending on number of CCL tokens */
365         if (no == 1)
366             add_attr (p, CCL_BIB1_STR, 2);
367         else
368             add_attr (p, CCL_BIB1_STR, 1);
369     }
370
371     /* make the RPN token */
372     p->u.t.term = (char *)malloc (len);
373     assert (p->u.t.term);
374     p->u.t.term[0] = '\0';
375     for (i = 0; i<no; i++)
376     {
377         const char *src_str = cclp->look_token->name;
378         int src_len = cclp->look_token->len;
379         
380         if (i == 0 && left_trunc)
381         {
382             src_len--;
383             src_str++;
384         }
385         else if (i == no-1 && right_trunc)
386             src_len--;
387         if (i)
388             strcat (p->u.t.term, " ");
389         strxcat (p->u.t.term, src_str, src_len);
390         ADVANCE;
391     }
392     if (left_trunc && right_trunc)
393     {
394         if (!qual_val_type (qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_BOTH))
395         {
396             cclp->error_code = CCL_ERR_TRUNC_NOT_BOTH;
397             free (qa);
398             ccl_rpn_delete (p);
399             return NULL;
400         }
401         add_attr (p, CCL_BIB1_TRU, 3);
402     }
403     else if (right_trunc)
404     {
405         if (!qual_val_type (qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_RIGHT))
406         {
407             cclp->error_code = CCL_ERR_TRUNC_NOT_RIGHT;
408             free (qa);
409             ccl_rpn_delete (p);
410             return NULL;
411         }
412         add_attr (p, CCL_BIB1_TRU, 1);
413     }
414     else if (left_trunc)
415     {
416         if (!qual_val_type (qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_LEFT))
417         {
418             cclp->error_code = CCL_ERR_TRUNC_NOT_LEFT;
419             free (qa);
420             ccl_rpn_delete (p);
421             return NULL;
422         }
423         add_attr (p, CCL_BIB1_TRU, 2);
424     }
425     else
426     {
427         if (qual_val_type (qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_NONE))
428             add_attr (p, CCL_BIB1_TRU, 100);
429     }
430     return p;
431 }
432
433 /*
434  * qualifiers: Parse CCL qualifiers and search terms. 
435  * cclp:   CCL Parser
436  * la:     Token pointer to RELATION token.
437  * qa:     Qualifier attributes already applied.
438  * return: pointer to node(s); NULL on error.
439  */
440 static struct ccl_rpn_node *qualifiers (CCL_parser cclp, struct ccl_token *la,
441                                         struct ccl_rpn_attr **qa)
442 {
443     struct ccl_token *lookahead = cclp->look_token;
444     struct ccl_rpn_attr **ap;
445     int no = 0;
446     int i, rel;
447 #if 0
448     if (qa)
449     {
450         cclp->error_code = CCL_ERR_DOUBLE_QUAL;
451         return NULL;
452     }
453 #endif
454     for (lookahead = cclp->look_token; lookahead != la;
455          lookahead=lookahead->next)
456         no++;
457     if (qa)
458         for (i=0; qa[i]; i++)
459             no++;
460     ap = (struct ccl_rpn_attr **)malloc ((no+1) * sizeof(*ap));
461     assert (ap);
462     for (i = 0; cclp->look_token != la; i++)
463     {
464         ap[i] = ccl_qual_search (cclp, cclp->look_token->name,
465                                  cclp->look_token->len);
466         if (!ap[i])
467         {
468             cclp->error_code = CCL_ERR_UNKNOWN_QUAL;
469             free (ap);
470             return NULL;
471         }
472         ADVANCE;
473         if (KIND == CCL_TOK_COMMA)
474             ADVANCE;
475     }
476     if (qa)
477         while (*qa)
478             ap[i++] = *qa++;
479     ap[i] = NULL;
480     if (!qual_val_type (ap, CCL_BIB1_REL, CCL_BIB1_REL_ORDER))
481     {                
482         /* unordered relation */
483         struct ccl_rpn_node *p;
484         if (KIND != CCL_TOK_EQ)
485         {
486             cclp->error_code = CCL_ERR_EQ_EXPECTED;
487             free (ap);
488             return NULL;
489         }
490         ADVANCE;
491         if (KIND == CCL_TOK_LP)
492         {
493             ADVANCE;
494             if (!(p = find_spec (cclp, ap)))
495             {
496                 free (ap);
497                 return NULL;
498             }
499             if (KIND != CCL_TOK_RP)
500             {
501                 cclp->error_code = CCL_ERR_RP_EXPECTED;
502                 ccl_rpn_delete (p);
503                 free (ap);
504                 return NULL;
505             }
506             ADVANCE;
507         }
508         else
509             p = search_terms (cclp, ap);
510         free (ap);
511         return p;
512     }
513     rel = 0;
514     if (cclp->look_token->len == 1)
515     {
516         if (cclp->look_token->name[0] == '<')
517             rel = 1;
518         else if (cclp->look_token->name[0] == '=')
519             rel = 3;
520         else if (cclp->look_token->name[0] == '>')
521             rel = 5;
522     }
523     else if (cclp->look_token->len == 2)
524     {
525         if (!memcmp (cclp->look_token->name, "<=", 2))
526             rel = 2;
527         else if (!memcmp (cclp->look_token->name, ">=", 2))
528             rel = 4;
529         else if (!memcmp (cclp->look_token->name, "<>", 2))
530             rel = 6;
531     }
532     if (!rel)
533         cclp->error_code = CCL_ERR_BAD_RELATION;
534     else
535     {
536         struct ccl_rpn_node *p;
537
538         ADVANCE;                      /* skip relation */
539         if (KIND == CCL_TOK_TERM &&
540             cclp->look_token->next->kind == CCL_TOK_MINUS)
541         {
542             struct ccl_rpn_node *p1;
543             if (!(p1 = search_term (cclp, ap)))
544             {
545                 free (ap);
546                 return NULL;
547             }
548             ADVANCE;                   /* skip '-' */
549             if (KIND == CCL_TOK_TERM)  /* = term - term  ? */
550             {
551                 struct ccl_rpn_node *p2;
552                 
553                 if (!(p2 = search_term (cclp, ap)))
554                 {
555                     ccl_rpn_delete (p1);
556                     free (ap);
557                     return NULL;
558                 }
559                 p = mk_node (CCL_RPN_AND);
560                 p->u.p[0] = p1;
561                 add_attr (p1, CCL_BIB1_REL, 4);
562                 p->u.p[1] = p2;
563                 add_attr (p2, CCL_BIB1_REL, 2);
564                 free (ap);
565                 return p;
566             }
567             else                       /* = term -    */
568             {
569                 add_attr (p1, CCL_BIB1_REL, 4);
570                 free (ap);
571                 return p1;
572             }
573         }
574         else if (KIND == CCL_TOK_MINUS)   /* = - term  ? */
575         {
576             ADVANCE;
577             if (!(p = search_term (cclp, ap)))
578             {
579                 free (ap);
580                 return NULL;
581             }
582             add_attr (p, CCL_BIB1_REL, 2);
583             free (ap);
584             return p;
585         }
586         else if (KIND == CCL_TOK_LP)
587         {
588             ADVANCE;
589             if (!(p = find_spec (cclp, ap)))
590             {
591                 free (ap);
592                 return NULL;
593             }
594             if (KIND != CCL_TOK_RP)
595             {
596                 cclp->error_code = CCL_ERR_RP_EXPECTED;
597                 ccl_rpn_delete (p);
598                 free (ap);
599                 return NULL;
600             }
601             ADVANCE;
602             free (ap);
603             return p;
604         }
605         else
606         {
607             if (!(p = search_terms (cclp, ap)))
608             {
609                 free (ap);
610                 return NULL;
611             }
612             add_attr (p, CCL_BIB1_REL, rel);
613             free (ap);
614             return p;
615         }
616         cclp->error_code = CCL_ERR_TERM_EXPECTED;
617     }
618     free (ap);
619     return NULL;
620 }
621
622 /*
623  * search_terms: Parse CCL search terms - including proximity.
624  * cclp:   CCL Parser
625  * qa:     Qualifier attributes already applied.
626  * return: pointer to node(s); NULL on error.
627  */
628 static struct ccl_rpn_node *search_terms (CCL_parser cclp,
629                                           struct ccl_rpn_attr **qa)
630 {
631     struct ccl_rpn_node *p1, *p2, *pn;
632     p1 = search_term (cclp, qa);
633     if (!p1)
634         return NULL;
635     while (1)
636     {
637         if (KIND == CCL_TOK_PROX)
638         {
639             ADVANCE;
640             p2 = search_term (cclp, qa);
641             if (!p2)
642             {
643                 ccl_rpn_delete (p1);
644                 return NULL;
645             }
646             pn = mk_node (CCL_RPN_PROX);
647             pn->u.p[0] = p1;
648             pn->u.p[1] = p2;
649             p1 = pn;
650         }
651         else if (KIND == CCL_TOK_TERM)
652         {
653             p2 = search_term (cclp, qa);
654             if (!p2)
655             {
656                 ccl_rpn_delete (p1);
657                 return NULL;
658             }
659             pn = mk_node (CCL_RPN_PROX);
660             pn->u.p[0] = p1;
661             pn->u.p[1] = p2;
662             p1 = pn;
663         }
664         else
665             break;
666     }
667     return p1;
668 }
669
670 /*
671  * search_elements: Parse CCL search elements
672  * cclp:   CCL Parser
673  * qa:     Qualifier attributes already applied.
674  * return: pointer to node(s); NULL on error.
675  */
676 static struct ccl_rpn_node *search_elements (CCL_parser cclp,
677                                              struct ccl_rpn_attr **qa)
678 {
679     struct ccl_rpn_node *p1;
680     struct ccl_token *lookahead;
681     if (KIND == CCL_TOK_LP)
682     {
683         ADVANCE;
684         p1 = find_spec (cclp, qa);
685         if (!p1)
686             return NULL;
687         if (KIND != CCL_TOK_RP)
688         {
689             cclp->error_code = CCL_ERR_RP_EXPECTED;
690             ccl_rpn_delete (p1);
691             return NULL;
692         }
693         ADVANCE;
694         return p1;
695     }
696     else if (KIND == CCL_TOK_SET)
697     {
698         ADVANCE;
699         if (KIND == CCL_TOK_EQ)
700             ADVANCE;
701         if (KIND != CCL_TOK_TERM)
702         {
703             cclp->error_code = CCL_ERR_SETNAME_EXPECTED;
704             return NULL;
705         }
706         p1 = mk_node (CCL_RPN_SET);
707         p1->u.setname = copy_token_name (cclp->look_token);
708         ADVANCE;
709         return p1;
710     }
711     lookahead = cclp->look_token;
712
713     while (lookahead->kind==CCL_TOK_TERM)
714     {
715         lookahead = lookahead->next;
716         if (lookahead->kind == CCL_TOK_REL || lookahead->kind == CCL_TOK_EQ)
717             return qualifiers (cclp, lookahead, qa);
718         if (lookahead->kind != CCL_TOK_COMMA)
719             break;
720         lookahead = lookahead->next;
721     }
722     return search_terms (cclp, qa);
723 }
724
725 /*
726  * find_spec: Parse CCL find specification
727  * cclp:   CCL Parser
728  * qa:     Qualifier attributes already applied.
729  * return: pointer to node(s); NULL on error.
730  */
731 static struct ccl_rpn_node *find_spec (CCL_parser cclp,
732                                        struct ccl_rpn_attr **qa)
733 {
734     struct ccl_rpn_node *p1, *p2, *pn;
735     if (!(p1 = search_elements (cclp, qa)))
736         return NULL;
737     while (1)
738     {
739         switch (KIND)
740         {
741         case CCL_TOK_AND:
742             ADVANCE;
743             p2 = search_elements (cclp, qa);
744             if (!p2)
745             {
746                 ccl_rpn_delete (p1);
747                 return NULL;
748             }
749             pn = mk_node (CCL_RPN_AND);
750             pn->u.p[0] = p1;
751             pn->u.p[1] = p2;
752             p1 = pn;
753             continue;
754         case CCL_TOK_OR:
755             ADVANCE;
756             p2 = search_elements (cclp, qa);
757             if (!p2)
758             {
759                 ccl_rpn_delete (p1);
760                 return NULL;
761             }
762             pn = mk_node (CCL_RPN_OR);
763             pn->u.p[0] = p1;
764             pn->u.p[1] = p2;
765             p1 = pn;
766             continue;
767         case CCL_TOK_NOT:
768             ADVANCE;
769             p2 = search_elements (cclp, qa);
770             if (!p2)
771             {
772                 ccl_rpn_delete (p1);
773                 return NULL;
774             }
775             pn = mk_node (CCL_RPN_NOT);
776             pn->u.p[0] = p1;
777             pn->u.p[1] = p2;
778             p1 = pn;
779             continue;
780         }
781         break;
782     }
783     return p1;
784 }
785
786 struct ccl_rpn_node *ccl_parser_find (CCL_parser cclp, struct ccl_token *list)
787 {
788     struct ccl_rpn_node *p;
789
790     cclp->look_token = list;
791     p = find_spec (cclp, NULL);
792     if (p && KIND != CCL_TOK_EOL)
793     {
794         if (KIND == CCL_TOK_RP)
795             cclp->error_code = CCL_ERR_BAD_RP;
796         else
797             cclp->error_code = CCL_ERR_OP_EXPECTED;
798         ccl_rpn_delete (p);
799         p = NULL;
800     }
801     cclp->error_pos = cclp->look_token->name;
802     if (p)
803         cclp->error_code = CCL_ERR_OK;
804     else
805         cclp->error_code = cclp->error_code;
806     return p;
807 }
808
809 /*
810  * ccl_find: Parse CCL find - token representation
811  * bibset:  Bibset to be used for the parsing
812  * list:    List of tokens
813  * error:   Pointer to integer. Holds error no. on completion.
814  * pos:     Pointer to char position. Holds approximate error position.
815  * return:  RPN tree on successful completion; NULL otherwise.
816  */
817 struct ccl_rpn_node *ccl_find (CCL_bibset bibset, struct ccl_token *list,
818                                int *error, const char **pos)
819 {
820     struct ccl_rpn_node *p;
821     CCL_parser cclp = ccl_parser_create ();
822
823     cclp->bibset = bibset;
824
825     p = ccl_parser_find (cclp, list);
826
827     *error = cclp->error_code;
828     *pos = cclp->error_pos;
829
830     ccl_parser_destroy (cclp);
831
832     return p;
833 }
834
835 /*
836  * ccl_find_str: Parse CCL find - string representation
837  * bibset:  Bibset to be used for the parsing
838  * str:     String to be parsed
839  * error:   Pointer to integer. Holds error no. on completion.
840  * pos:     Pointer to char position. Holds approximate error position.
841  * return:  RPN tree on successful completion; NULL otherwise.
842  */
843 struct ccl_rpn_node *ccl_find_str (CCL_bibset bibset, const char *str,
844                                    int *error, int *pos)
845 {
846     CCL_parser cclp = ccl_parser_create ();
847     struct ccl_token *list;
848     struct ccl_rpn_node *p;
849
850     cclp->bibset = bibset;
851
852     list = ccl_parser_tokenize (cclp, str);
853     p = ccl_parser_find (cclp, list);
854
855     *error = cclp->error_code;
856     if (*error)
857         *pos = cclp->error_pos - str;
858     ccl_parser_destroy (cclp);
859     return p;
860 }