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