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