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