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