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