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