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