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