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