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