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