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