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