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