CCL: fix r=o, r=r WRT inherited attributes YAZ-864
[yaz-moved-to-github.git] / src / cclfind.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 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 static struct ccl_rpn_node *ccl_rpn_node_mkbool(struct ccl_rpn_node *l,
117                                                 struct ccl_rpn_node *r,
118                                                 enum ccl_rpn_kind op)
119 {
120     if (l && r)
121     {
122         struct ccl_rpn_node *tmp = ccl_rpn_node_create(op);
123         tmp->u.p[0] = l;
124         tmp->u.p[1] = r;
125         tmp->u.p[2] = 0;
126         return tmp;
127     }
128     else if (r)
129         return r;
130     return l;
131 }
132
133 /**
134  * ccl_rpn_delete: Delete RPN tree.
135  * rpn:   Pointer to tree.
136  */
137 void ccl_rpn_delete(struct ccl_rpn_node *rpn)
138 {
139     struct ccl_rpn_attr *attr, *attr1;
140     if (!rpn)
141         return;
142     switch (rpn->kind)
143     {
144     case CCL_RPN_AND:
145     case CCL_RPN_OR:
146     case CCL_RPN_NOT:
147         ccl_rpn_delete(rpn->u.p[0]);
148         ccl_rpn_delete(rpn->u.p[1]);
149         break;
150     case CCL_RPN_TERM:
151         xfree(rpn->u.t.term);
152         xfree(rpn->u.t.qual);
153         for (attr = rpn->u.t.attr_list; attr; attr = attr1)
154         {
155             attr1 = attr->next;
156             if (attr->kind == CCL_RPN_ATTR_STRING)
157                 xfree(attr->value.str);
158             if (attr->set)
159                 xfree(attr->set);
160             xfree(attr);
161         }
162         break;
163     case CCL_RPN_SET:
164         xfree(rpn->u.setname);
165         break;
166     case CCL_RPN_PROX:
167         ccl_rpn_delete(rpn->u.p[0]);
168         ccl_rpn_delete(rpn->u.p[1]);
169         ccl_rpn_delete(rpn->u.p[2]);
170         break;
171     }
172     xfree(rpn);
173 }
174
175 static struct ccl_rpn_node *find_spec(CCL_parser cclp, ccl_qualifier_t *qa);
176
177 static int is_term_ok(int look, int *list)
178 {
179     for (; *list >= 0; list++)
180         if (look == *list)
181             return 1;
182     return 0;
183 }
184
185 static struct ccl_rpn_node *search_terms(CCL_parser cclp, ccl_qualifier_t *qa);
186
187 static struct ccl_rpn_attr *add_attr_node(struct ccl_rpn_node *p,
188                                            const char *set, int type)
189 {
190     struct ccl_rpn_attr *n = (struct ccl_rpn_attr *) xmalloc(sizeof(*n));
191     ccl_assert(n);
192     if (set)
193         n->set = xstrdup(set);
194     else
195         n->set = 0;
196     n->type = type;
197     n->next = p->u.t.attr_list;
198     p->u.t.attr_list = n;
199     return n;
200 }
201
202 /**
203  * add_attr_numeric: Add attribute (type/value) to RPN term node.
204  * p:     RPN node of type term.
205  * type:  Type of attribute
206  * value: Value of attribute
207  * set: Attribute set name
208  */
209 void ccl_add_attr_numeric(struct ccl_rpn_node *p, const char *set,
210                           int type, int value)
211 {
212     struct ccl_rpn_attr *n = add_attr_node(p, set, type);
213     n->kind = CCL_RPN_ATTR_NUMERIC;
214     n->value.numeric = value;
215 }
216
217 void ccl_set_attr_numeric(struct ccl_rpn_node *p, const char *set,
218                           int type, int value)
219 {
220     struct ccl_rpn_attr *n;
221     for (n = p->u.t.attr_list; n; n = n->next)
222         if (n->type == type)
223         {
224             xfree(n->set);
225             n->set = set ? xstrdup(set) : 0;
226             if (n->kind == CCL_RPN_ATTR_STRING)
227                 xfree(n->value.str);
228             n->kind = CCL_RPN_ATTR_NUMERIC;
229             n->value.numeric = value;
230             return;
231         }
232     ccl_add_attr_numeric(p, set, type, value);
233 }
234
235 void ccl_add_attr_string(struct ccl_rpn_node *p, const char *set,
236                          int type, char *value)
237 {
238     struct ccl_rpn_attr *n = add_attr_node(p, set, type);
239     n->kind = CCL_RPN_ATTR_STRING;
240     n->value.str = xstrdup(value);
241 }
242
243 static size_t cmp_operator(const char **aliases, const char *input)
244 {
245     for (; *aliases; aliases++)
246     {
247         const char *cp = *aliases;
248         size_t i;
249         for (i = 0; *cp && *cp == input[i]; i++, cp++)
250             ;
251         if (*cp == '\0')
252             return i;
253     }
254     return 0;
255 }
256
257
258 #define REGEX_CHARS "^[]{}()|.*+?!$"
259 #define CCL_CHARS "#?\\"
260
261 static int has_ccl_masking(const char *src_str,
262                            size_t src_len,
263                            const char **truncation_aliases,
264                            const char **mask_aliases)
265 {
266     size_t j;
267     int quote_mode = 0;
268
269     for (j = 0; j < src_len; j++)
270     {
271         size_t op_size;
272         if (j > 0 && src_str[j-1] == '\\')
273             ;
274         else if (src_str[j] == '"')
275             quote_mode = !quote_mode;
276         else if (!quote_mode &&
277                  (op_size = cmp_operator(truncation_aliases,
278                                          src_str + j)))
279             return 1;
280         else if (!quote_mode &&
281                  (op_size = cmp_operator(mask_aliases,
282                                           src_str + j)))
283             return 1;
284     }
285     return 0;
286 }
287
288 static int append_term(CCL_parser cclp, const char *src_str, size_t src_len,
289                        char *dst_term, int regex_trunc, int z3958_trunc,
290                        const char **truncation_aliases,
291                        const char **mask_aliases,
292                        int is_first, int is_last,
293                        int *left_trunc, int *right_trunc)
294 {
295     size_t j;
296     int quote_mode = 0;
297
298     for (j = 0; j < src_len; j++)
299     {
300         size_t op_size;
301         if (j > 0 && src_str[j-1] == '\\')
302         {
303             if (regex_trunc && strchr(REGEX_CHARS "\\", src_str[j]))
304                 strcat(dst_term, "\\");
305             else if (z3958_trunc && strchr(CCL_CHARS "\\", src_str[j]))
306                 strcat(dst_term, "\\");
307             strxcat(dst_term, src_str + j, 1);
308         }
309         else if (src_str[j] == '"')
310             quote_mode = !quote_mode;
311         else if (!quote_mode &&
312                  (op_size = cmp_operator(truncation_aliases,
313                                          src_str + j))
314             )
315         {
316             j += (op_size - 1);  /* j++ in for loop */
317             if (regex_trunc)
318                 strcat(dst_term, ".*");
319             else if (z3958_trunc)
320                 strcat(dst_term, "?");
321             else if (is_first && j == 0)
322                 *left_trunc = 1;
323             else if (is_last && j == src_len - 1)
324                 *right_trunc = 1;
325             else
326             {
327                 cclp->error_code = CCL_ERR_TRUNC_NOT_EMBED;
328                 return -1;
329             }
330         }
331         else if (!quote_mode &&
332                  (op_size = cmp_operator(mask_aliases, src_str + j)))
333         {
334             j += (op_size - 1);  /* j++ in for loop */
335             if (regex_trunc)
336                 strcat(dst_term, ".");
337             else if (z3958_trunc)
338                 strcat(dst_term, "#");
339             else
340             {
341                 cclp->error_code = CCL_ERR_TRUNC_NOT_SINGLE;
342                 return -1;
343             }
344         }
345         else if (src_str[j] != '\\')
346         {
347             if (regex_trunc && strchr(REGEX_CHARS, src_str[j]))
348                 strcat(dst_term, "\\");
349             else if (z3958_trunc && strchr(CCL_CHARS, src_str[j]))
350                 strcat(dst_term, "\\");
351             strxcat(dst_term, src_str + j, 1);
352         }
353     }
354     return 0;
355 }
356
357
358 static struct ccl_rpn_node *ccl_term_one_use(CCL_parser cclp,
359                                              struct ccl_token *lookahead0,
360                                              struct ccl_rpn_attr *attr_use,
361                                              ccl_qualifier_t *qa,
362                                              size_t no,
363                                              int is_phrase,
364                                              int auto_group)
365 {
366     struct ccl_rpn_node *p;
367     size_t i;
368     int relation_value = -1;
369     int position_value = -1;
370     int structure_value = -1;
371     int truncation_value = -1;
372     int completeness_value = -1;
373
374     int left_trunc = 0;
375     int right_trunc = 0;
376     int regex_trunc = 0;
377     int z3958_trunc = 0;
378     int is_ccl_masked = 0;
379     char *attset;
380     struct ccl_token *lookahead = lookahead0;
381     const char **truncation_aliases;
382     const char *t_default[2];
383     const char **mask_aliases;
384     const char *m_default[2];
385     int term_len = 0;
386
387     truncation_aliases =
388         ccl_qual_search_special(cclp->bibset, "truncation");
389     if (!truncation_aliases)
390     {
391         truncation_aliases = t_default;
392         t_default[0] = "?";
393         t_default[1] = 0;
394     }
395     mask_aliases =
396         ccl_qual_search_special(cclp->bibset, "mask");
397     if (!mask_aliases)
398     {
399         mask_aliases = m_default;
400         m_default[0] = "#";
401         m_default[1] = 0;
402     }
403     for (i = 0; i < no; i++)
404     {
405         if (has_ccl_masking(lookahead->name, lookahead->len,
406                             truncation_aliases,
407                             mask_aliases))
408             is_ccl_masked = 1;
409
410         term_len += 1 + lookahead->len + lookahead->ws_prefix_len;
411         lookahead = lookahead->next;
412     }
413     lookahead = lookahead0;
414
415     p = ccl_rpn_node_create(CCL_RPN_TERM);
416     p->u.t.attr_list = NULL;
417     p->u.t.term = NULL;
418     if (qa && qa[0])
419     {
420         const char *n = ccl_qual_get_name(qa[0]);
421         if (n)
422             p->u.t.qual = xstrdup(n);
423     }
424     /* go through all attributes and add them to the attribute list */
425     for (i = 0; qa && qa[i]; i++)
426     {
427         struct ccl_rpn_attr *attr;
428         for (attr = ccl_qual_get_attr(qa[i]); attr; attr = attr->next)
429             if (attr->type != 1 || !attr_use || attr == attr_use)
430             {
431                 switch (attr->kind)
432                 {
433                 case CCL_RPN_ATTR_STRING:
434                     ccl_add_attr_string(p, attr->set, attr->type,
435                                         attr->value.str);
436                     break;
437                 case CCL_RPN_ATTR_NUMERIC:
438                     if (attr->value.numeric > 0)
439                     {   /* deal only with REAL attributes (positive) */
440                         switch (attr->type)
441                         {
442                         case CCL_BIB1_REL:
443                             if (relation_value != -1)
444                                 continue;
445                             relation_value = attr->value.numeric;
446                             break;
447                         case CCL_BIB1_POS:
448                             if (position_value != -1)
449                                 continue;
450                             position_value = attr->value.numeric;
451                             break;
452                         case CCL_BIB1_STR:
453                             if (structure_value != -1)
454                                 continue;
455                             structure_value = attr->value.numeric;
456                             break;
457                         case CCL_BIB1_TRU:
458                             if (truncation_value != -1)
459                                 continue;
460                             truncation_value = attr->value.numeric;
461                             break;
462                         case CCL_BIB1_COM:
463                             if (completeness_value != -1)
464                                 continue;
465                             completeness_value = attr->value.numeric;
466                             break;
467                         }
468                         ccl_add_attr_numeric(p, attr->set, attr->type,
469                                              attr->value.numeric);
470                     }
471                 }
472             }
473     }
474     attset = 0;
475     if (structure_value == -1 && (
476             auto_group ||
477             qual_val_type(qa, CCL_BIB1_STR, CCL_BIB1_STR_WP, &attset))
478         )
479     {
480         if (!is_phrase)
481             ccl_add_attr_numeric(p, attset, CCL_BIB1_STR, 2);
482         else
483             ccl_add_attr_numeric(p, attset, CCL_BIB1_STR, 1);
484     }
485     if (qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_REGEX,
486                       &attset))
487     {
488         if (is_ccl_masked)
489             regex_trunc = 1; /* regex trunc (102) allowed */
490     }
491     else if (qual_val_type(qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_Z3958,
492                            &attset))
493     {
494         if (is_ccl_masked)
495             z3958_trunc = 1; /* Z39.58 trunc (CCL) trunc allowed */
496     }
497     /* make the RPN token */
498     p->u.t.term = (char *)xmalloc(term_len * 2 + 2);
499     ccl_assert(p->u.t.term);
500     p->u.t.term[0] = '\0';
501
502     for (i = 0; i < no; i++)
503     {
504         const char *src_str = lookahead->name;
505         size_t src_len = lookahead->len;
506
507         if (p->u.t.term[0] && lookahead->ws_prefix_len)
508         {
509             strxcat(p->u.t.term, lookahead->ws_prefix_buf,
510                     lookahead->ws_prefix_len);
511         }
512         if (append_term(cclp, src_str, src_len, p->u.t.term, regex_trunc,
513                         z3958_trunc, truncation_aliases, mask_aliases,
514                         i == 0, i == no - 1,
515                         &left_trunc, &right_trunc))
516         {
517             ccl_rpn_delete(p);
518             return NULL;
519         }
520         lookahead = lookahead->next;
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)
556     {
557         ccl_add_attr_numeric(p, attset, CCL_BIB1_TRU, 102);
558     }
559     else if (z3958_trunc)
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     return p;
570 }
571
572 static struct ccl_rpn_node *ccl_term_multi_use(CCL_parser cclp,
573                                                struct ccl_token *lookahead0,
574                                                ccl_qualifier_t *qa,
575                                                size_t no,
576                                                int is_phrase,
577                                                int auto_group)
578 {
579     struct ccl_rpn_node *p = 0;
580     int i;
581     for (i = 0; qa && qa[i]; i++)
582     {
583         struct ccl_rpn_attr *attr;
584         for (attr = ccl_qual_get_attr(qa[i]); attr; attr = attr->next)
585             if (attr->type == 1 && i == 0)
586             {
587                 struct ccl_rpn_node *tmp2;
588                 tmp2 = ccl_term_one_use(cclp, lookahead0,
589                                         attr, qa, no,
590                                         is_phrase, auto_group);
591                 if (!tmp2)
592                 {
593                     ccl_rpn_delete(p);
594                     return 0;
595                 }
596                 p = ccl_rpn_node_mkbool(p, tmp2, CCL_RPN_OR);
597             }
598     }
599     if (!p)
600         p = ccl_term_one_use(cclp, lookahead0,
601                              0 /* attr: no use */, qa, no,
602                              is_phrase, auto_group);
603     return p;
604 }
605
606 static struct ccl_rpn_node *split_recur(CCL_parser cclp, ccl_qualifier_t *qa,
607                                         struct ccl_token **ar, size_t sz,
608                                         size_t sub_len)
609 {
610     size_t l;
611     struct ccl_rpn_node *p_top = 0;
612     assert(sz > 0);
613     for (l = 1; l <= sz && l <= sub_len; l++)
614     {
615         struct ccl_rpn_node *p2 = ccl_term_multi_use(cclp, ar[0],
616                                                      qa, l,
617                                                      l > 1,
618                                                      /* auto_group */0);
619         if (!p2)
620         {
621             ccl_rpn_delete(p_top);
622             return 0;
623         }
624         if (sz > l)
625         {
626             struct ccl_rpn_node *p1 = split_recur(cclp, qa, ar + l, sz - l,
627                 sub_len);
628             if (!p1)
629             {
630                 ccl_rpn_delete(p2);
631                 return 0;
632             }
633             p2 = ccl_rpn_node_mkbool(p2, p1, CCL_RPN_AND);
634         }
635         p_top = ccl_rpn_node_mkbool(p_top, p2, CCL_RPN_OR);
636     }
637     assert(p_top);
638     return p_top;
639 }
640
641 static struct ccl_rpn_node *search_term_split_list(CCL_parser cclp,
642                                                    ccl_qualifier_t *qa,
643                                                    int *term_list, int multi)
644 {
645     struct ccl_rpn_node *p;
646     struct ccl_token **ar;
647     struct ccl_token *lookahead = cclp->look_token;
648     size_t i, sz, sub_len;
649     for (sz = 0; is_term_ok(lookahead->kind, term_list); sz++)
650         lookahead = lookahead->next;
651     if (sz == 0)
652     {
653         cclp->error_code = CCL_ERR_TERM_EXPECTED;
654         return 0;
655     }
656     ar = (struct ccl_token **) xmalloc(sizeof(*lookahead) * sz);
657     lookahead = cclp->look_token;
658     for (i = 0; is_term_ok(lookahead->kind, term_list); i++)
659     {
660         ar[i] = lookahead;
661         lookahead = lookahead->next;
662     }
663     /* choose sub phrase carefully to avoid huge expansions */
664     if (sz >= 7)
665         sub_len = 1;
666     else if (sz >= 5)
667         sub_len = 2;
668     else
669         sub_len = 3;
670     p = split_recur(cclp, qa, ar, sz, sub_len);
671     xfree(ar);
672     for (i = 0; i < sz; i++)
673         ADVANCE;
674     return p;
675 }
676
677 /**
678  * search_term: Parse CCL search term.
679  * cclp:   CCL Parser
680  * qa:     Qualifier attributes already applied.
681  * term_list: tokens we accept as terms in context
682  * multi:  whether we accept "multiple" tokens
683  * return: pointer to node(s); NULL on error.
684  */
685 static struct ccl_rpn_node *search_term_x(CCL_parser cclp,
686                                           ccl_qualifier_t *qa,
687                                           int *term_list, int multi)
688 {
689     struct ccl_rpn_node *p_top = 0;
690     struct ccl_token *lookahead = cclp->look_token;
691     int and_list = 0;
692     int auto_group = 0;
693     int or_list = 0;
694
695     if (qual_val_type(qa, CCL_BIB1_STR, CCL_BIB1_STR_AND_LIST, 0))
696         and_list = 1;
697     if (qual_val_type(qa, CCL_BIB1_STR, CCL_BIB1_STR_AUTO_GROUP, 0))
698         auto_group = 1;
699     if (qual_val_type(qa, CCL_BIB1_STR, CCL_BIB1_STR_OR_LIST, 0))
700         or_list = 1;
701     if (qual_val_type(qa, CCL_BIB1_STR, CCL_BIB1_STR_SPLIT_LIST, 0))
702     {
703         return search_term_split_list(cclp, qa, term_list, multi);
704     }
705     while (1)
706     {
707         struct ccl_rpn_node *p = 0;
708         size_t no, i;
709         int is_phrase = 0;
710         size_t max = 200;
711         if (and_list || or_list || !multi)
712             max = 1;
713
714         /* ignore commas when dealing with and-lists .. */
715         if (and_list && lookahead && lookahead->kind == CCL_TOK_COMMA)
716         {
717             lookahead = lookahead->next;
718             ADVANCE;
719             continue;
720         }
721         for (no = 0; no < max && is_term_ok(lookahead->kind, term_list); no++)
722         {
723             int this_is_phrase = 0;
724             for (i = 0; i<lookahead->len; i++)
725                 if (lookahead->name[i] == ' ')
726                     this_is_phrase = 1;
727             if (auto_group)
728             {
729                 if (no > 0 && (is_phrase || is_phrase != this_is_phrase))
730                     break;
731                 is_phrase = this_is_phrase;
732             }
733             else if (this_is_phrase || no > 0)
734                 is_phrase = 1;
735             lookahead = lookahead->next;
736         }
737
738         if (no == 0)
739             break;      /* no more terms . stop . */
740         p = ccl_term_multi_use(cclp, cclp->look_token, qa, no,
741                                is_phrase, auto_group);
742         for (i = 0; i < no; i++)
743             ADVANCE;
744         if (!p)
745             return 0;
746         p_top = ccl_rpn_node_mkbool(p_top, p, or_list ? CCL_RPN_OR : CCL_RPN_AND);
747         if (!multi)
748             break;
749     }
750     if (!p_top)
751         cclp->error_code = CCL_ERR_TERM_EXPECTED;
752     return p_top;
753 }
754
755 static struct ccl_rpn_node *search_term(CCL_parser cclp, ccl_qualifier_t *qa)
756 {
757     static int list[] = {CCL_TOK_TERM, CCL_TOK_COMMA, -1};
758     return search_term_x(cclp, qa, list, 0);
759 }
760
761
762 static struct ccl_rpn_node *search_terms2(CCL_parser cclp,
763                                           ccl_qualifier_t *qa)
764 {
765     if (KIND == CCL_TOK_LP)
766     {
767         struct ccl_rpn_node *p;
768         ADVANCE;
769         if (!(p = find_spec(cclp, qa)))
770             return NULL;
771         if (KIND != CCL_TOK_RP)
772         {
773             cclp->error_code = CCL_ERR_RP_EXPECTED;
774             ccl_rpn_delete(p);
775             return NULL;
776         }
777         ADVANCE;
778         return p;
779     }
780     else
781     {
782         static int list[] = {
783             CCL_TOK_TERM, CCL_TOK_COMMA,CCL_TOK_EQ,
784             CCL_TOK_REL, CCL_TOK_SET, -1};
785
786         return search_term_x(cclp, qa, list, 1);
787     }
788 }
789
790
791 static
792 struct ccl_rpn_node *qualifiers_order(CCL_parser cclp,
793                                       ccl_qualifier_t *ap, char *attset)
794 {
795     int rel = 0;
796     struct ccl_rpn_node *p;
797
798     if (cclp->look_token->len == 1)
799     {
800         if (cclp->look_token->name[0] == '<')
801             rel = 1;
802         else if (cclp->look_token->name[0] == '=')
803             rel = 3;
804         else if (cclp->look_token->name[0] == '>')
805             rel = 5;
806     }
807     else if (cclp->look_token->len == 2)
808     {
809         if (!memcmp(cclp->look_token->name, "<=", 2))
810             rel = 2;
811         else if (!memcmp(cclp->look_token->name, ">=", 2))
812             rel = 4;
813         else if (!memcmp(cclp->look_token->name, "<>", 2))
814             rel = 6;
815     }
816     if (!rel)
817     {
818         cclp->error_code = CCL_ERR_BAD_RELATION;
819         return NULL;
820     }
821     ADVANCE;  /* skip relation */
822     if (rel == 3 &&
823         qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_PORDER, 0))
824     {
825         /* allow - inside term and treat it as range _always_ */
826         /* relation is =. Extract "embedded" - to separate terms */
827         if (KIND == CCL_TOK_TERM)
828         {
829             size_t i;
830             int quote_mode = 0;
831             for (i = 0; i<cclp->look_token->len; i++)
832             {
833                 if (i > 0 && cclp->look_token->name[i] == '\\')
834                     ;
835                 else if (cclp->look_token->name[i] == '"')
836                     quote_mode = !quote_mode;
837                 else if (cclp->look_token->name[i] == '-' && !quote_mode)
838                     break;
839             }
840
841             if (cclp->look_token->len > 1 && i == 0)
842             {   /*  -xx*/
843                 struct ccl_token *ntoken = ccl_token_add(cclp->look_token);
844
845                 ntoken->kind = CCL_TOK_TERM;
846                 ntoken->name = cclp->look_token->name + 1;
847                 ntoken->len = cclp->look_token->len - 1;
848
849                 cclp->look_token->len = 1;
850                 cclp->look_token->name = "-";
851             }
852             else if (cclp->look_token->len > 1 && i == cclp->look_token->len-1)
853             {   /* xx- */
854                 struct ccl_token *ntoken = ccl_token_add(cclp->look_token);
855
856                 ntoken->kind = CCL_TOK_TERM;
857                 ntoken->name = "-";
858                 ntoken->len = 1;
859
860                 (cclp->look_token->len)--;
861             }
862             else if (cclp->look_token->len > 2 && i < cclp->look_token->len)
863             {   /* xx-yy */
864                 struct ccl_token *ntoken1 = ccl_token_add(cclp->look_token);
865                 struct ccl_token *ntoken2 = ccl_token_add(ntoken1);
866
867                 ntoken1->kind = CCL_TOK_TERM;  /* generate - */
868                 ntoken1->name = "-";
869                 ntoken1->len = 1;
870
871                 ntoken2->kind = CCL_TOK_TERM;  /* generate yy */
872                 ntoken2->name = cclp->look_token->name + (i+1);
873                 ntoken2->len = cclp->look_token->len - (i+1);
874
875                 cclp->look_token->len = i;     /* adjust xx */
876             }
877             else if (i == cclp->look_token->len &&
878                      cclp->look_token->next &&
879                      cclp->look_token->next->kind == CCL_TOK_TERM &&
880                      cclp->look_token->next->len > 1 &&
881                      cclp->look_token->next->name[0] == '-')
882
883             {   /* xx -yy */
884                 /* we _know_ that xx does not have - in it */
885                 struct ccl_token *ntoken = ccl_token_add(cclp->look_token);
886
887                 ntoken->kind = CCL_TOK_TERM;    /* generate - */
888                 ntoken->name = "-";
889                 ntoken->len = 1;
890
891                 (ntoken->next->name)++;        /* adjust yy */
892                 (ntoken->next->len)--;
893             }
894         }
895     }
896
897     if (rel == 3 &&
898         KIND == CCL_TOK_TERM &&
899         cclp->look_token->next && cclp->look_token->next->len == 1 &&
900         cclp->look_token->next->name[0] == '-')
901     {
902         struct ccl_rpn_node *p1;
903         if (!(p1 = search_term(cclp, ap)))
904             return NULL;
905         ADVANCE;                   /* skip '-' */
906         if (KIND == CCL_TOK_TERM)  /* = term - term  ? */
907         {
908             struct ccl_rpn_node *p2;
909
910             if (!(p2 = search_term(cclp, ap)))
911             {
912                 ccl_rpn_delete(p1);
913                 return NULL;
914             }
915             p = ccl_rpn_node_create(CCL_RPN_AND);
916             p->u.p[0] = p1;
917             ccl_set_attr_numeric(p1, attset, CCL_BIB1_REL, 4);
918             p->u.p[1] = p2;
919             ccl_set_attr_numeric(p2, attset, CCL_BIB1_REL, 2);
920             return p;
921         }
922         else                       /* = term -    */
923         {
924             ccl_set_attr_numeric(p1, attset, CCL_BIB1_REL, 4);
925             return p1;
926         }
927     }
928     else if (rel == 3 &&
929              cclp->look_token->len == 1 &&
930              cclp->look_token->name[0] == '-')   /* = - term  ? */
931     {
932         ADVANCE;
933         if (!(p = search_term(cclp, ap)))
934             return NULL;
935         ccl_set_attr_numeric(p, attset, CCL_BIB1_REL, 2);
936         return p;
937     }
938     else
939     {
940         if (!(p = search_terms(cclp, ap)))
941             return NULL;
942         if (rel != 3 ||
943             !qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_OMIT_EQUALS, 0))
944             ccl_set_attr_numeric(p, attset, CCL_BIB1_REL, rel);
945         return p;
946     }
947     return NULL;
948 }
949
950 static
951 struct ccl_rpn_node *qualifier_relation(CCL_parser cclp, ccl_qualifier_t *ap)
952 {
953     char *attset;
954
955     if (qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_ORDER, &attset)
956         || qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_PORDER, &attset))
957         return qualifiers_order(cclp, ap, attset);
958
959     /* unordered relation */
960     if (KIND != CCL_TOK_EQ)
961     {
962         cclp->error_code = CCL_ERR_EQ_EXPECTED;
963         return NULL;
964     }
965     ADVANCE;
966     return search_terms(cclp, ap);
967 }
968
969 /**
970  * qualifier_list: Parse CCL qualifiers and search terms.
971  * cclp:   CCL Parser
972  * la:     Token pointer to RELATION token.
973  * qa:     Qualifier attributes already applied.
974  * return: pointer to node(s); NULL on error.
975  */
976 static struct ccl_rpn_node *qualifier_list(CCL_parser cclp,
977                                            struct ccl_token *la,
978                                            ccl_qualifier_t *qa)
979 {
980     struct ccl_token *lookahead = cclp->look_token;
981     struct ccl_token *look_start = cclp->look_token;
982     ccl_qualifier_t *ap;
983     struct ccl_rpn_node *node = 0;
984     const char **field_str;
985     int no = 0;
986     int seq = 0;
987     int i;
988     int mode_merge = 1;
989 #if 0
990     if (qa)
991     {
992         cclp->error_code = CCL_ERR_DOUBLE_QUAL;
993         return NULL;
994     }
995 #endif
996     for (lookahead = cclp->look_token; lookahead != la;
997          lookahead=lookahead->next)
998         no++;
999     if (qa)
1000         for (i=0; qa[i]; i++)
1001             no++;
1002     ap = (ccl_qualifier_t *)xmalloc((no ? (no+1) : 2) * sizeof(*ap));
1003     ccl_assert(ap);
1004
1005     field_str = ccl_qual_search_special(cclp->bibset, "field");
1006     if (field_str)
1007     {
1008         if (!strcmp(field_str[0], "or"))
1009             mode_merge = 0;
1010         else if (!strcmp(field_str[0], "merge"))
1011             mode_merge = 1;
1012     }
1013     if (!mode_merge)
1014     {
1015         /* consider each field separately and OR */
1016         lookahead = look_start;
1017         while (lookahead != la)
1018         {
1019             ap[1] = 0;
1020             seq = 0;
1021             while ((ap[0] = ccl_qual_search(cclp, lookahead->name,
1022                                             lookahead->len, seq)) != 0)
1023             {
1024                 struct ccl_rpn_node *node_sub;
1025                 cclp->look_token = la;
1026
1027                 node_sub = qualifier_relation(cclp, ap);
1028                 if (!node_sub)
1029                 {
1030                     ccl_rpn_delete(node);
1031                     xfree(ap);
1032                     return 0;
1033                 }
1034                 node = ccl_rpn_node_mkbool(node, node_sub, CCL_RPN_OR);
1035                 seq++;
1036             }
1037             if (seq == 0)
1038             {
1039                 cclp->look_token = lookahead;
1040                 cclp->error_code = CCL_ERR_UNKNOWN_QUAL;
1041                 xfree(ap);
1042                 return NULL;
1043             }
1044             lookahead = lookahead->next;
1045             if (lookahead->kind == CCL_TOK_COMMA)
1046                 lookahead = lookahead->next;
1047         }
1048     }
1049     else
1050     {
1051         /* merge attributes from ALL fields - including inherited ones */
1052         while (1)
1053         {
1054             struct ccl_rpn_node *node_sub;
1055             int found = 0;
1056             lookahead = look_start;
1057             for (i = 0; lookahead != la; i++)
1058             {
1059                 ap[i] = ccl_qual_search(cclp, lookahead->name,
1060                                          lookahead->len, seq);
1061                 if (ap[i])
1062                     found++;
1063                 if (!ap[i] && seq > 0)
1064                     ap[i] = ccl_qual_search(cclp, lookahead->name,
1065                                              lookahead->len, 0);
1066                 if (!ap[i])
1067                 {
1068                     cclp->look_token = lookahead;
1069                     cclp->error_code = CCL_ERR_UNKNOWN_QUAL;
1070                     xfree(ap);
1071                     return NULL;
1072                 }
1073                 lookahead = lookahead->next;
1074                 if (lookahead->kind == CCL_TOK_COMMA)
1075                     lookahead = lookahead->next;
1076             }
1077             if (qa)
1078             {
1079                 ccl_qualifier_t *qa0 = qa;
1080
1081                 while (*qa0)
1082                     ap[i++] = *qa0++;
1083             }
1084             ap[i] = NULL;
1085
1086             if (!found)
1087                 break;
1088
1089             cclp->look_token = lookahead;
1090
1091             node_sub = qualifier_relation(cclp, ap);
1092             if (!node_sub)
1093             {
1094                 ccl_rpn_delete(node);
1095                 break;
1096             }
1097             node = ccl_rpn_node_mkbool(node, node_sub, CCL_RPN_OR);
1098             seq++;
1099         }
1100     }
1101     xfree(ap);
1102     return node;
1103 }
1104
1105
1106 /**
1107  * search_terms: Parse CCL search terms - including proximity.
1108  * cclp:   CCL Parser
1109  * qa:     Qualifier attributes already applied.
1110  * return: pointer to node(s); NULL on error.
1111  */
1112 static struct ccl_rpn_node *search_terms(CCL_parser cclp, ccl_qualifier_t *qa)
1113 {
1114     static int list[] = {
1115         CCL_TOK_TERM, CCL_TOK_COMMA,CCL_TOK_EQ,
1116         CCL_TOK_REL, CCL_TOK_SET, -1};
1117     struct ccl_rpn_node *p1, *p2, *pn;
1118     p1 = search_terms2(cclp, qa);
1119     if (!p1)
1120         return NULL;
1121     while (1)
1122     {
1123         if (KIND == CCL_TOK_PROX)
1124         {
1125             struct ccl_rpn_node *p_prox = 0;
1126             /* ! word order specified */
1127             /* % word order not specified */
1128             p_prox = ccl_rpn_node_create(CCL_RPN_TERM);
1129             p_prox->u.t.term = (char *) xmalloc(1 + cclp->look_token->len);
1130             memcpy(p_prox->u.t.term, cclp->look_token->name,
1131                    cclp->look_token->len);
1132             p_prox->u.t.term[cclp->look_token->len] = 0;
1133             p_prox->u.t.attr_list = 0;
1134
1135             ADVANCE;
1136             p2 = search_terms2(cclp, qa);
1137             if (!p2)
1138             {
1139                 ccl_rpn_delete(p1);
1140                 return NULL;
1141             }
1142             pn = ccl_rpn_node_create(CCL_RPN_PROX);
1143             pn->u.p[0] = p1;
1144             pn->u.p[1] = p2;
1145             pn->u.p[2] = p_prox;
1146             p1 = pn;
1147         }
1148         else if (is_term_ok(KIND, list))
1149         {
1150             p2 = search_terms2(cclp, qa);
1151             if (!p2)
1152             {
1153                 ccl_rpn_delete(p1);
1154                 return NULL;
1155             }
1156             pn = ccl_rpn_node_create(CCL_RPN_PROX);
1157             pn->u.p[0] = p1;
1158             pn->u.p[1] = p2;
1159             pn->u.p[2] = 0;
1160             p1 = pn;
1161         }
1162         else
1163             break;
1164     }
1165     return p1;
1166 }
1167
1168 /**
1169  * search_elements: Parse CCL search elements
1170  * cclp:   CCL Parser
1171  * qa:     Qualifier attributes already applied.
1172  * return: pointer to node(s); NULL on error.
1173  */
1174 static struct ccl_rpn_node *search_elements(CCL_parser cclp,
1175                                             ccl_qualifier_t *qa)
1176 {
1177     struct ccl_rpn_node *p1;
1178     struct ccl_token *lookahead;
1179     if (KIND == CCL_TOK_SET)
1180     {
1181         ADVANCE;
1182         if (KIND == CCL_TOK_EQ)
1183             ADVANCE;
1184         if (KIND != CCL_TOK_TERM)
1185         {
1186             cclp->error_code = CCL_ERR_SETNAME_EXPECTED;
1187             return NULL;
1188         }
1189         p1 = ccl_rpn_node_create(CCL_RPN_SET);
1190         p1->u.setname = copy_token_name(cclp->look_token);
1191         ADVANCE;
1192         return p1;
1193     }
1194     lookahead = cclp->look_token;
1195
1196     while (lookahead->kind==CCL_TOK_TERM)
1197     {
1198         lookahead = lookahead->next;
1199         if (lookahead->kind == CCL_TOK_REL || lookahead->kind == CCL_TOK_EQ)
1200             return qualifier_list(cclp, lookahead, qa);
1201         if (lookahead->kind != CCL_TOK_COMMA)
1202             break;
1203         lookahead = lookahead->next;
1204     }
1205     if (qa || lookahead->kind == CCL_TOK_LP)
1206         return search_terms(cclp, qa);
1207     else
1208     {
1209         ccl_qualifier_t qa[2];
1210         struct ccl_rpn_node *node = 0;
1211         int seq;
1212         lookahead = cclp->look_token;
1213
1214         qa[1] = 0;
1215         for(seq = 0; ;seq++)
1216         {
1217             struct ccl_rpn_node *node_sub;
1218             qa[0] = ccl_qual_search(cclp, "term", 4, seq);
1219             if (!qa[0])
1220                 break;
1221
1222             cclp->look_token = lookahead;
1223
1224             node_sub = search_terms(cclp, qa);
1225             if (!node_sub)
1226             {
1227                 ccl_rpn_delete(node);
1228                 return 0;
1229             }
1230             node = ccl_rpn_node_mkbool(node, node_sub, CCL_RPN_OR);
1231         }
1232         if (!node)
1233             node = search_terms(cclp, 0);
1234         return node;
1235     }
1236 }
1237
1238 /**
1239  * find_spec: Parse CCL find specification
1240  * cclp:   CCL Parser
1241  * qa:     Qualifier attributes already applied.
1242  * return: pointer to node(s); NULL on error.
1243  */
1244 static struct ccl_rpn_node *find_spec(CCL_parser cclp, ccl_qualifier_t *qa)
1245 {
1246     struct ccl_rpn_node *p1, *p2;
1247     if (!(p1 = search_elements(cclp, qa)))
1248         return NULL;
1249     while (1)
1250     {
1251         switch (KIND)
1252         {
1253         case CCL_TOK_AND:
1254             ADVANCE;
1255             p2 = search_elements(cclp, qa);
1256             if (!p2)
1257             {
1258                 ccl_rpn_delete(p1);
1259                 return NULL;
1260             }
1261             p1 = ccl_rpn_node_mkbool(p1, p2, CCL_RPN_AND);
1262             continue;
1263         case CCL_TOK_OR:
1264             ADVANCE;
1265             p2 = search_elements(cclp, qa);
1266             if (!p2)
1267             {
1268                 ccl_rpn_delete(p1);
1269                 return NULL;
1270             }
1271             p1 = ccl_rpn_node_mkbool(p1, p2, CCL_RPN_OR);
1272             continue;
1273         case CCL_TOK_NOT:
1274             ADVANCE;
1275             p2 = search_elements(cclp, qa);
1276             if (!p2)
1277             {
1278                 ccl_rpn_delete(p1);
1279                 return NULL;
1280             }
1281             p1 = ccl_rpn_node_mkbool(p1, p2, CCL_RPN_NOT);
1282             continue;
1283         }
1284         break;
1285     }
1286     return p1;
1287 }
1288
1289 struct ccl_rpn_node *ccl_parser_find_str(CCL_parser cclp, const char *str)
1290 {
1291     struct ccl_rpn_node *p;
1292     struct ccl_token *list = ccl_parser_tokenize(cclp, str);
1293     p = ccl_parser_find_token(cclp, list);
1294     ccl_token_del(list);
1295     return p;
1296 }
1297
1298 struct ccl_rpn_node *ccl_parser_find_token(CCL_parser cclp,
1299                                            struct ccl_token *list)
1300 {
1301     struct ccl_rpn_node *p;
1302
1303     cclp->look_token = list;
1304     p = find_spec(cclp, NULL);
1305     if (p && KIND != CCL_TOK_EOL)
1306     {
1307         if (KIND == CCL_TOK_RP)
1308             cclp->error_code = CCL_ERR_BAD_RP;
1309         else
1310             cclp->error_code = CCL_ERR_OP_EXPECTED;
1311         ccl_rpn_delete(p);
1312         p = NULL;
1313     }
1314     cclp->error_pos = cclp->look_token->name;
1315     if (p)
1316         cclp->error_code = CCL_ERR_OK;
1317     else
1318         cclp->error_code = cclp->error_code;
1319     return p;
1320 }
1321
1322 /**
1323  * ccl_find_str: Parse CCL find - string representation
1324  * bibset:  Bibset to be used for the parsing
1325  * str:     String to be parsed
1326  * error:   Pointer to integer. Holds error no. on completion.
1327  * pos:     Pointer to char position. Holds approximate error position.
1328  * return:  RPN tree on successful completion; NULL otherwise.
1329  */
1330 struct ccl_rpn_node *ccl_find_str(CCL_bibset bibset, const char *str,
1331                                   int *error, int *pos)
1332 {
1333     CCL_parser cclp = ccl_parser_create(bibset);
1334     struct ccl_token *list;
1335     struct ccl_rpn_node *p;
1336
1337     list = ccl_parser_tokenize(cclp, str);
1338     p = ccl_parser_find_token(cclp, list);
1339
1340     *error = cclp->error_code;
1341     if (*error)
1342         *pos = cclp->error_pos - str;
1343     ccl_parser_destroy(cclp);
1344     ccl_token_del(list);
1345     return p;
1346 }
1347
1348 /*
1349  * Local variables:
1350  * c-basic-offset: 4
1351  * c-file-style: "Stroustrup"
1352  * indent-tabs-mode: nil
1353  * End:
1354  * vim: shiftwidth=4 tabstop=8 expandtab
1355  */
1356