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