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