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