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