CCL: s=ag automatically adds s=pw
[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 auto_group = 0;
338     int or_list = 0;
339     char *attset;
340     const char **truncation_aliases;
341     const char *t_default[2];
342
343     truncation_aliases =
344         ccl_qual_search_special(cclp->bibset, "truncation");
345     if (!truncation_aliases)
346     {
347         truncation_aliases = t_default;
348         t_default[0] = "?";
349         t_default[1] = 0;
350     }
351
352     if (qual_val_type(qa, CCL_BIB1_STR, CCL_BIB1_STR_AND_LIST, 0))
353         and_list = 1;
354     if (qual_val_type(qa, CCL_BIB1_STR, CCL_BIB1_STR_AUTO_GROUP, 0))
355         auto_group = 1;
356     if (qual_val_type(qa, CCL_BIB1_STR, CCL_BIB1_STR_OR_LIST, 0))
357         or_list = 1;
358     while (1)
359     {
360         struct ccl_rpn_node *p;
361         size_t no, i;
362         int is_phrase = 0;
363         int relation_value = -1;
364         int position_value = -1;
365         int structure_value = -1;
366         int truncation_value = -1;
367         int completeness_value = -1;
368         int len = 0;
369         int left_trunc = 0;
370         int right_trunc = 0;
371         int regex_trunc = 0;
372         int z3958_trunc = 0;
373         size_t max = 200;
374         if (and_list || or_list || !multi)
375             max = 1;
376         
377         /* ignore commas when dealing with and-lists .. */
378         if (and_list && lookahead && lookahead->kind == CCL_TOK_COMMA)
379         {
380             lookahead = lookahead->next;
381             ADVANCE;
382             continue;
383         }
384         for (no = 0; no < max && is_term_ok(lookahead->kind, term_list); no++)
385         {
386             int this_is_phrase = 0;
387             for (i = 0; i<lookahead->len; i++)
388                 if (lookahead->name[i] == ' ')
389                     this_is_phrase = 1;
390
391             if (auto_group)
392             {
393                 if (no > 0 && (is_phrase || is_phrase != this_is_phrase))
394                     break;
395                 is_phrase = this_is_phrase;
396             }
397             else if (this_is_phrase || no > 0)
398                 is_phrase = 1;
399             len += 1+lookahead->len+lookahead->ws_prefix_len;
400             lookahead = lookahead->next;
401         }
402
403         if (len == 0)
404             break;      /* no more terms . stop . */
405                 
406         /* create the term node, but wait a moment before adding the term */
407         p = ccl_rpn_node_create(CCL_RPN_TERM);
408         p->u.t.attr_list = NULL;
409         p->u.t.term = NULL;
410         if (qa && qa[0])
411         {
412             const char *n = ccl_qual_get_name(qa[0]);
413             if (n)
414                 p->u.t.qual = xstrdup(n);
415         }
416
417         /* go through all attributes and add them to the attribute list */
418         for (i=0; qa && qa[i]; i++)
419         {
420             struct ccl_rpn_attr *attr;
421             
422             for (attr = ccl_qual_get_attr(qa[i]); attr; attr = attr->next)
423                 switch(attr->kind)
424                 {
425                 case CCL_RPN_ATTR_STRING:
426                     ccl_add_attr_string(p, attr->set, attr->type,
427                                         attr->value.str);
428                     break;
429                 case CCL_RPN_ATTR_NUMERIC:
430                     if (attr->value.numeric > 0)
431                     {   /* deal only with REAL attributes (positive) */
432                         switch (attr->type)
433                         {
434                         case CCL_BIB1_REL:
435                             if (relation_value != -1)
436                                 continue;
437                             relation_value = attr->value.numeric;
438                             break;
439                         case CCL_BIB1_POS:
440                             if (position_value != -1)
441                                 continue;
442                             position_value = attr->value.numeric;
443                             break;
444                         case CCL_BIB1_STR:
445                             if (structure_value != -1)
446                                 continue;
447                             structure_value = attr->value.numeric;
448                             break;
449                         case CCL_BIB1_TRU:
450                             if (truncation_value != -1)
451                                 continue;
452                             truncation_value = attr->value.numeric;
453                             break;
454                         case CCL_BIB1_COM:
455                             if (completeness_value != -1)
456                                 continue;
457                             completeness_value = attr->value.numeric;
458                             break;
459                         }
460                         ccl_add_attr_numeric(p, attr->set, attr->type,
461                                              attr->value.numeric);
462                     }
463                 }
464         }
465         if (structure_value == -1 && (
466                 auto_group || 
467                 qual_val_type(qa, CCL_BIB1_STR, CCL_BIB1_STR_WP, &attset))
468             )
469         {
470             if (!is_phrase)
471                 ccl_add_attr_numeric(p, attset, CCL_BIB1_STR, 2);
472             else
473                 ccl_add_attr_numeric(p, attset, CCL_BIB1_STR, 1);
474         }
475
476         if (qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_REGEX,
477                           &attset))
478         {
479             regex_trunc = 1; /* regex trunc (102) allowed */
480         }
481         else if (qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_Z3958,
482                           &attset))
483         {
484             z3958_trunc = 1; /* Z39.58 trunc (CCL) trunc allowed */
485         }
486
487         /* make the RPN token */
488         p->u.t.term = (char *)xmalloc(len * 2 + 2);
489         ccl_assert(p->u.t.term);
490         p->u.t.term[0] = '\0';
491         for (i = 0; i<no; i++)
492         {
493             const char *src_str = cclp->look_token->name;
494             size_t src_len = cclp->look_token->len;
495
496             if (p->u.t.term[0] && cclp->look_token->ws_prefix_len)
497             {
498                 strxcat(p->u.t.term, cclp->look_token->ws_prefix_buf,
499                         cclp->look_token->ws_prefix_len);
500             }
501             if (append_term(cclp, src_str, src_len, p->u.t.term, &regex_trunc,
502                             &z3958_trunc, truncation_aliases, i == 0, i == no - 1,
503                             &left_trunc, &right_trunc))
504             {
505                 ccl_rpn_delete(p);
506                 return NULL;
507             }
508             ADVANCE;
509         }
510         /* make the top node point to us.. */
511         if (p_top)
512         {
513             struct ccl_rpn_node *tmp;
514
515             if (or_list)
516                 tmp = ccl_rpn_node_create(CCL_RPN_OR);
517             else if (and_list)
518                 tmp = ccl_rpn_node_create(CCL_RPN_AND);
519             else
520                 tmp = ccl_rpn_node_create(CCL_RPN_AND);
521             tmp->u.p[0] = p_top;
522             tmp->u.p[1] = p;
523
524             p_top = tmp;
525         }
526         else
527             p_top = p;
528
529
530         if (left_trunc && right_trunc)
531         {
532             if (!qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_BOTH,
533                                 &attset))
534             {
535                 cclp->error_code = CCL_ERR_TRUNC_NOT_BOTH;
536                 ccl_rpn_delete(p);
537                 return NULL;
538             }
539             ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 3);
540         }
541         else if (right_trunc)
542         {
543             if (!qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_RIGHT,
544                                  &attset))
545             {
546                 cclp->error_code = CCL_ERR_TRUNC_NOT_RIGHT;
547                 ccl_rpn_delete(p);
548                 return NULL;
549             }
550             ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 1);
551         }
552         else if (left_trunc)
553         {
554             if (!qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_LEFT,
555                                 &attset))
556             {
557                 cclp->error_code = CCL_ERR_TRUNC_NOT_LEFT;
558                 ccl_rpn_delete(p);
559                 return NULL;
560             }
561             ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 2);
562         }
563         else if (regex_trunc == 2)
564         {
565             ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 102);
566         }
567         else if (z3958_trunc == 2)
568         {
569             ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 104);
570         }
571         else
572         {
573             if (qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_NONE,
574                                &attset))
575                 ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 100);
576         }
577         if (!multi)
578             break;
579     }
580     if (!p_top)
581         cclp->error_code = CCL_ERR_TERM_EXPECTED;
582     return p_top;
583 }
584
585 static struct ccl_rpn_node *search_term(CCL_parser cclp, ccl_qualifier_t *qa)
586 {
587     static int list[] = {CCL_TOK_TERM, CCL_TOK_COMMA, -1};
588     return search_term_x(cclp, qa, list, 0);
589 }
590
591
592 static struct ccl_rpn_node *search_terms2(CCL_parser cclp,
593                                           ccl_qualifier_t *qa)
594 {
595     if (KIND == CCL_TOK_LP)
596     {
597         struct ccl_rpn_node *p;
598         ADVANCE;
599         if (!(p = find_spec(cclp, qa)))
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         static int list[] = {
613             CCL_TOK_TERM, CCL_TOK_COMMA,CCL_TOK_EQ,
614             CCL_TOK_REL, CCL_TOK_SET, -1};
615         
616         return search_term_x(cclp, qa, list, 1);
617     }
618 }
619
620
621
622 static
623 struct ccl_rpn_node *qualifiers_order(CCL_parser cclp,
624                                       ccl_qualifier_t *ap, char *attset)
625 {
626     int rel = 0;
627     struct ccl_rpn_node *p;
628
629     if (cclp->look_token->len == 1)
630     {
631         if (cclp->look_token->name[0] == '<')
632             rel = 1;
633         else if (cclp->look_token->name[0] == '=')
634             rel = 3;
635         else if (cclp->look_token->name[0] == '>')
636             rel = 5;
637     }
638     else if (cclp->look_token->len == 2)
639     {
640         if (!memcmp(cclp->look_token->name, "<=", 2))
641             rel = 2;
642         else if (!memcmp(cclp->look_token->name, ">=", 2))
643             rel = 4;
644         else if (!memcmp(cclp->look_token->name, "<>", 2))
645             rel = 6;
646     }
647     if (!rel)
648     {
649         cclp->error_code = CCL_ERR_BAD_RELATION;
650         return NULL;
651     }
652     ADVANCE;  /* skip relation */
653     if (rel == 3 &&
654         qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_PORDER, 0))
655     {
656         /* allow - inside term and treat it as range _always_ */
657         /* relation is =. Extract "embedded" - to separate terms */
658         if (KIND == CCL_TOK_TERM)
659         {
660             size_t i;
661             for (i = 0; i<cclp->look_token->len; i++)
662             {
663                 if (cclp->look_token->name[i] == '-')
664                     break;
665             }
666             
667             if (cclp->look_token->len > 1 && i == 0)
668             {   /*  -xx*/
669                 struct ccl_token *ntoken = ccl_token_add(cclp->look_token);
670
671                 ntoken->kind = CCL_TOK_TERM;
672                 ntoken->name = cclp->look_token->name + 1;
673                 ntoken->len = cclp->look_token->len - 1;
674
675                 cclp->look_token->len = 1;
676                 cclp->look_token->name = "-";
677             }
678             else if (cclp->look_token->len > 1 && i == cclp->look_token->len-1)
679             {   /* xx- */
680                 struct ccl_token *ntoken = ccl_token_add(cclp->look_token);
681
682                 ntoken->kind = CCL_TOK_TERM;
683                 ntoken->name = "-";
684                 ntoken->len = 1;
685
686                 (cclp->look_token->len)--;
687             }
688             else if (cclp->look_token->len > 2 && i < cclp->look_token->len)
689             {   /* xx-yy */
690                 struct ccl_token *ntoken1 = ccl_token_add(cclp->look_token);
691                 struct ccl_token *ntoken2 = ccl_token_add(ntoken1);
692
693                 ntoken1->kind = CCL_TOK_TERM;  /* generate - */
694                 ntoken1->name = "-";
695                 ntoken1->len = 1;
696
697                 ntoken2->kind = CCL_TOK_TERM;  /* generate yy */
698                 ntoken2->name = cclp->look_token->name + (i+1);
699                 ntoken2->len = cclp->look_token->len - (i+1);
700
701                 cclp->look_token->len = i;     /* adjust xx */
702             }
703             else if (i == cclp->look_token->len &&
704                      cclp->look_token->next &&
705                      cclp->look_token->next->kind == CCL_TOK_TERM &&
706                      cclp->look_token->next->len > 1 &&
707                      cclp->look_token->next->name[0] == '-')
708                      
709             {   /* xx -yy */
710                 /* we _know_ that xx does not have - in it */
711                 struct ccl_token *ntoken = ccl_token_add(cclp->look_token);
712
713                 ntoken->kind = CCL_TOK_TERM;    /* generate - */
714                 ntoken->name = "-";
715                 ntoken->len = 1;
716
717                 (ntoken->next->name)++;        /* adjust yy */
718                 (ntoken->next->len)--; 
719             }
720         }
721     }
722         
723     if (rel == 3 &&
724         KIND == CCL_TOK_TERM &&
725         cclp->look_token->next && cclp->look_token->next->len == 1 &&
726         cclp->look_token->next->name[0] == '-')
727     {
728         struct ccl_rpn_node *p1;
729         if (!(p1 = search_term(cclp, ap)))
730             return NULL;
731         ADVANCE;                   /* skip '-' */
732         if (KIND == CCL_TOK_TERM)  /* = term - term  ? */
733         {
734             struct ccl_rpn_node *p2;
735             
736             if (!(p2 = search_term(cclp, ap)))
737             {
738                 ccl_rpn_delete(p1);
739                 return NULL;
740             }
741             p = ccl_rpn_node_create(CCL_RPN_AND);
742             p->u.p[0] = p1;
743             ccl_add_attr_numeric(p1, attset, CCL_BIB1_REL, 4);
744             p->u.p[1] = p2;
745             ccl_add_attr_numeric(p2, attset, CCL_BIB1_REL, 2);
746             return p;
747         }
748         else                       /* = term -    */
749         {
750             ccl_add_attr_numeric(p1, attset, CCL_BIB1_REL, 4);
751             return p1;
752         }
753     }
754     else if (rel == 3 &&
755              cclp->look_token->len == 1 &&
756              cclp->look_token->name[0] == '-')   /* = - term  ? */
757     {
758         ADVANCE;
759         if (!(p = search_term(cclp, ap)))
760             return NULL;
761         ccl_add_attr_numeric(p, attset, CCL_BIB1_REL, 2);
762         return p;
763     }
764     else
765     {
766         if (!(p = search_terms(cclp, ap)))
767             return NULL;
768         ccl_add_attr_numeric(p, attset, CCL_BIB1_REL, rel);
769         return p;
770     }
771     cclp->error_code = CCL_ERR_TERM_EXPECTED;
772     return NULL;
773 }
774
775 static
776 struct ccl_rpn_node *qualifier_relation(CCL_parser cclp, ccl_qualifier_t *ap)
777 {
778     char *attset;
779     
780     if (qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_ORDER, &attset)
781         || qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_PORDER, &attset))
782         return qualifiers_order(cclp, ap, attset);
783
784     /* unordered relation */
785     if (KIND != CCL_TOK_EQ)
786     {
787         cclp->error_code = CCL_ERR_EQ_EXPECTED;
788         return NULL;
789     }
790     ADVANCE;
791     return search_terms(cclp, ap);
792 }
793
794 /**
795  * qualifier_list: Parse CCL qualifiers and search terms. 
796  * cclp:   CCL Parser
797  * la:     Token pointer to RELATION token.
798  * qa:     Qualifier attributes already applied.
799  * return: pointer to node(s); NULL on error.
800  */
801 static struct ccl_rpn_node *qualifier_list(CCL_parser cclp, 
802                                            struct ccl_token *la,
803                                            ccl_qualifier_t *qa)
804 {
805     struct ccl_token *lookahead = cclp->look_token;
806     struct ccl_token *look_start = cclp->look_token;
807     ccl_qualifier_t *ap;
808     struct ccl_rpn_node *node = 0;
809     const char **field_str;
810     int no = 0;
811     int seq = 0;
812     int i;
813     int mode_merge = 1;
814 #if 0
815     if (qa)
816     {
817         cclp->error_code = CCL_ERR_DOUBLE_QUAL;
818         return NULL;
819     }
820 #endif
821     for (lookahead = cclp->look_token; lookahead != la;
822          lookahead=lookahead->next)
823         no++;
824     if (qa)
825         for (i=0; qa[i]; i++)
826             no++;
827     ap = (ccl_qualifier_t *)xmalloc((no ? (no+1) : 2) * sizeof(*ap));
828     ccl_assert(ap);
829
830     field_str = ccl_qual_search_special(cclp->bibset, "field");
831     if (field_str)
832     {
833         if (!strcmp(field_str[0], "or"))
834             mode_merge = 0;
835         else if (!strcmp(field_str[0], "merge"))
836             mode_merge = 1;
837     }
838     if (!mode_merge)
839     {
840         /* consider each field separately and OR */
841         lookahead = look_start;
842         while (lookahead != la)
843         {
844             ap[1] = 0;
845             seq = 0;
846             while ((ap[0] = ccl_qual_search(cclp, lookahead->name,
847                                             lookahead->len, seq)) != 0)
848             {
849                 struct ccl_rpn_node *node_sub;
850                 cclp->look_token = la;
851                 
852                 node_sub = qualifier_relation(cclp, ap);
853                 if (!node_sub)
854                 {
855                     ccl_rpn_delete(node);
856                     xfree(ap);
857                     return 0;
858                 }
859                 if (node)
860                 {
861                     struct ccl_rpn_node *node_this = 
862                         ccl_rpn_node_create(CCL_RPN_OR);
863                     node_this->u.p[0] = node;
864                     node_this->u.p[1] = node_sub;
865                     node = node_this;
866                 }
867                 else
868                     node = node_sub;
869                 seq++;
870             }
871             if (seq == 0)
872             {
873                 cclp->look_token = lookahead;
874                 cclp->error_code = CCL_ERR_UNKNOWN_QUAL;
875                 xfree(ap);
876                 return NULL;
877             }
878             lookahead = lookahead->next;
879             if (lookahead->kind == CCL_TOK_COMMA)
880                 lookahead = lookahead->next;
881         }
882     }
883     else
884     {
885         /* merge attributes from ALL fields - including inherited ones */
886         while (1)
887         {
888             struct ccl_rpn_node *node_sub;
889             int found = 0;
890             lookahead = look_start;
891             for (i = 0; lookahead != la; i++)
892             {
893                 ap[i] = ccl_qual_search(cclp, lookahead->name,
894                                          lookahead->len, seq);
895                 if (ap[i])
896                     found++;
897                 if (!ap[i] && seq > 0)
898                     ap[i] = ccl_qual_search(cclp, lookahead->name,
899                                              lookahead->len, 0);
900                 if (!ap[i])
901                 {
902                     cclp->look_token = lookahead;
903                     cclp->error_code = CCL_ERR_UNKNOWN_QUAL;
904                     xfree(ap);
905                     return NULL;
906                 }
907                 lookahead = lookahead->next;
908                 if (lookahead->kind == CCL_TOK_COMMA)
909                     lookahead = lookahead->next;
910             }
911             if (qa)
912             {
913                 ccl_qualifier_t *qa0 = qa;
914                 
915                 while (*qa0)
916                     ap[i++] = *qa0++;
917             }
918             ap[i] = NULL;
919             
920             if (!found)
921                 break;
922             
923             cclp->look_token = lookahead;
924             
925             node_sub = qualifier_relation(cclp, ap);
926             if (!node_sub)
927             {
928                 ccl_rpn_delete(node);
929                 break;
930             }
931             if (node)
932             {
933                 struct ccl_rpn_node *node_this = 
934                     ccl_rpn_node_create(CCL_RPN_OR);
935                 node_this->u.p[0] = node;
936                 node_this->u.p[1] = node_sub;
937                 node = node_this;
938             }
939             else
940                 node = node_sub;
941             seq++;
942         }
943     }
944     xfree(ap);
945     return node;
946 }
947
948
949 /**
950  * search_terms: Parse CCL search terms - including proximity.
951  * cclp:   CCL Parser
952  * qa:     Qualifier attributes already applied.
953  * return: pointer to node(s); NULL on error.
954  */
955 static struct ccl_rpn_node *search_terms(CCL_parser cclp, ccl_qualifier_t *qa)
956 {
957     static int list[] = {
958         CCL_TOK_TERM, CCL_TOK_COMMA,CCL_TOK_EQ,
959         CCL_TOK_REL, CCL_TOK_SET, -1};
960     struct ccl_rpn_node *p1, *p2, *pn;
961     p1 = search_terms2(cclp, qa);
962     if (!p1)
963         return NULL;
964     while (1)
965     {
966         if (KIND == CCL_TOK_PROX)
967         {
968             struct ccl_rpn_node *p_prox = 0;
969             /* ! word order specified */
970             /* % word order not specified */
971             p_prox = ccl_rpn_node_create(CCL_RPN_TERM);
972             p_prox->u.t.term = (char *) xmalloc(1 + cclp->look_token->len);
973             memcpy(p_prox->u.t.term, cclp->look_token->name,
974                    cclp->look_token->len);
975             p_prox->u.t.term[cclp->look_token->len] = 0;
976             p_prox->u.t.attr_list = 0;
977
978             ADVANCE;
979             p2 = search_terms2(cclp, qa);
980             if (!p2)
981             {
982                 ccl_rpn_delete(p1);
983                 return NULL;
984             }
985             pn = ccl_rpn_node_create(CCL_RPN_PROX);
986             pn->u.p[0] = p1;
987             pn->u.p[1] = p2;
988             pn->u.p[2] = p_prox;
989             p1 = pn;
990         }
991         else if (is_term_ok(KIND, list))
992         {
993             p2 = search_terms2(cclp, qa);
994             if (!p2)
995             {
996                 ccl_rpn_delete(p1);
997                 return NULL;
998             }
999             pn = ccl_rpn_node_create(CCL_RPN_PROX);
1000             pn->u.p[0] = p1;
1001             pn->u.p[1] = p2;
1002             pn->u.p[2] = 0;
1003             p1 = pn;
1004         }
1005         else
1006             break;
1007     }
1008     return p1;
1009 }
1010
1011 /**
1012  * search_elements: Parse CCL search elements
1013  * cclp:   CCL Parser
1014  * qa:     Qualifier attributes already applied.
1015  * return: pointer to node(s); NULL on error.
1016  */
1017 static struct ccl_rpn_node *search_elements(CCL_parser cclp,
1018                                             ccl_qualifier_t *qa)
1019 {
1020     struct ccl_rpn_node *p1;
1021     struct ccl_token *lookahead;
1022     if (KIND == CCL_TOK_SET)
1023     {
1024         ADVANCE;
1025         if (KIND == CCL_TOK_EQ)
1026             ADVANCE;
1027         if (KIND != CCL_TOK_TERM)
1028         {
1029             cclp->error_code = CCL_ERR_SETNAME_EXPECTED;
1030             return NULL;
1031         }
1032         p1 = ccl_rpn_node_create(CCL_RPN_SET);
1033         p1->u.setname = copy_token_name(cclp->look_token);
1034         ADVANCE;
1035         return p1;
1036     }
1037     lookahead = cclp->look_token;
1038
1039     while (lookahead->kind==CCL_TOK_TERM)
1040     {
1041         lookahead = lookahead->next;
1042         if (lookahead->kind == CCL_TOK_REL || lookahead->kind == CCL_TOK_EQ)
1043             return qualifier_list(cclp, lookahead, qa);
1044         if (lookahead->kind != CCL_TOK_COMMA)
1045             break;
1046         lookahead = lookahead->next;
1047     }
1048     if (qa || lookahead->kind == CCL_TOK_LP)
1049         return search_terms(cclp, qa);
1050     else
1051     {
1052         ccl_qualifier_t qa[2];
1053         struct ccl_rpn_node *node = 0;
1054         int seq;
1055         lookahead = cclp->look_token;
1056
1057         qa[1] = 0;
1058         for(seq = 0; ;seq++)
1059         {
1060             struct ccl_rpn_node *node_sub;
1061             qa[0] = ccl_qual_search(cclp, "term", 4, seq);
1062             if (!qa[0])
1063                 break;
1064
1065             cclp->look_token = lookahead;
1066
1067             node_sub = search_terms(cclp, qa);
1068             if (!node_sub)
1069             {
1070                 ccl_rpn_delete(node);
1071                 return 0;
1072             }
1073             if (node)
1074             {
1075                 struct ccl_rpn_node *node_this = 
1076                     ccl_rpn_node_create(CCL_RPN_OR);
1077                 node_this->u.p[0] = node;
1078                 node_this->u.p[1] = node_sub;
1079                 node_this->u.p[2] = 0;
1080                 node = node_this;
1081             }
1082             else
1083                 node = node_sub;
1084         }
1085         if (!node)
1086             node = search_terms(cclp, 0);
1087         return node;
1088     }
1089 }
1090
1091 /**
1092  * find_spec: Parse CCL find specification
1093  * cclp:   CCL Parser
1094  * qa:     Qualifier attributes already applied.
1095  * return: pointer to node(s); NULL on error.
1096  */
1097 static struct ccl_rpn_node *find_spec(CCL_parser cclp, ccl_qualifier_t *qa)
1098 {
1099     struct ccl_rpn_node *p1, *p2, *pn;
1100     if (!(p1 = search_elements(cclp, qa)))
1101         return NULL;
1102     while (1)
1103     {
1104         switch (KIND)
1105         {
1106         case CCL_TOK_AND:
1107             ADVANCE;
1108             p2 = search_elements(cclp, qa);
1109             if (!p2)
1110             {
1111                 ccl_rpn_delete(p1);
1112                 return NULL;
1113             }
1114             pn = ccl_rpn_node_create(CCL_RPN_AND);
1115             pn->u.p[0] = p1;
1116             pn->u.p[1] = p2;
1117             pn->u.p[2] = 0;
1118             p1 = pn;
1119             continue;
1120         case CCL_TOK_OR:
1121             ADVANCE;
1122             p2 = search_elements(cclp, qa);
1123             if (!p2)
1124             {
1125                 ccl_rpn_delete(p1);
1126                 return NULL;
1127             }
1128             pn = ccl_rpn_node_create(CCL_RPN_OR);
1129             pn->u.p[0] = p1;
1130             pn->u.p[1] = p2;
1131             pn->u.p[2] = 0;
1132             p1 = pn;
1133             continue;
1134         case CCL_TOK_NOT:
1135             ADVANCE;
1136             p2 = search_elements(cclp, qa);
1137             if (!p2)
1138             {
1139                 ccl_rpn_delete(p1);
1140                 return NULL;
1141             }
1142             pn = ccl_rpn_node_create(CCL_RPN_NOT);
1143             pn->u.p[0] = p1;
1144             pn->u.p[1] = p2;
1145             pn->u.p[2] = 0;
1146             p1 = pn;
1147             continue;
1148         }
1149         break;
1150     }
1151     return p1;
1152 }
1153
1154 struct ccl_rpn_node *ccl_parser_find_str(CCL_parser cclp, const char *str)
1155 {
1156     struct ccl_rpn_node *p;
1157     struct ccl_token *list = ccl_parser_tokenize(cclp, str);
1158     p = ccl_parser_find_token(cclp, list);
1159     ccl_token_del(list);
1160     return p;
1161 }
1162
1163 struct ccl_rpn_node *ccl_parser_find_token(CCL_parser cclp, 
1164                                            struct ccl_token *list)
1165 {
1166     struct ccl_rpn_node *p;
1167
1168     cclp->look_token = list;
1169     p = find_spec(cclp, NULL);
1170     if (p && KIND != CCL_TOK_EOL)
1171     {
1172         if (KIND == CCL_TOK_RP)
1173             cclp->error_code = CCL_ERR_BAD_RP;
1174         else
1175             cclp->error_code = CCL_ERR_OP_EXPECTED;
1176         ccl_rpn_delete(p);
1177         p = NULL;
1178     }
1179     cclp->error_pos = cclp->look_token->name;
1180     if (p)
1181         cclp->error_code = CCL_ERR_OK;
1182     else
1183         cclp->error_code = cclp->error_code;
1184     return p;
1185 }
1186
1187 /**
1188  * ccl_find_str: Parse CCL find - string representation
1189  * bibset:  Bibset to be used for the parsing
1190  * str:     String to be parsed
1191  * error:   Pointer to integer. Holds error no. on completion.
1192  * pos:     Pointer to char position. Holds approximate error position.
1193  * return:  RPN tree on successful completion; NULL otherwise.
1194  */
1195 struct ccl_rpn_node *ccl_find_str(CCL_bibset bibset, const char *str,
1196                                   int *error, int *pos)
1197 {
1198     CCL_parser cclp = ccl_parser_create(bibset);
1199     struct ccl_token *list;
1200     struct ccl_rpn_node *p;
1201
1202     list = ccl_parser_tokenize(cclp, str);
1203     p = ccl_parser_find_token(cclp, list);
1204
1205     *error = cclp->error_code;
1206     if (*error)
1207         *pos = cclp->error_pos - str;
1208     ccl_parser_destroy(cclp);
1209     ccl_token_del(list);
1210     return p;
1211 }
1212
1213 /*
1214  * Local variables:
1215  * c-basic-offset: 4
1216  * c-file-style: "Stroustrup"
1217  * indent-tabs-mode: nil
1218  * End:
1219  * vim: shiftwidth=4 tabstop=8 expandtab
1220  */
1221