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