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