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