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