Move code from search_term_x to ccl_term_on_use
[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         {
636             p = ccl_term_one_use(cclp, 0 /* attr: no use */, qa, no, len,
637                                  is_phrase, auto_group);
638             if (!p)
639                 return 0;
640         }
641         for (i = 0; i < no; i++)
642             ADVANCE;
643         /* make the top node point to us.. */
644         if (p_top)
645         {
646             struct ccl_rpn_node *tmp;
647
648             if (or_list)
649                 tmp = ccl_rpn_node_create(CCL_RPN_OR);
650             else if (and_list)
651                 tmp = ccl_rpn_node_create(CCL_RPN_AND);
652             else
653                 tmp = ccl_rpn_node_create(CCL_RPN_AND);
654             tmp->u.p[0] = p_top;
655             tmp->u.p[1] = p;
656
657             p_top = tmp;
658         }
659         else
660             p_top = p;
661
662         if (!multi)
663             break;
664     }
665     if (!p_top)
666         cclp->error_code = CCL_ERR_TERM_EXPECTED;
667     return p_top;
668 }
669
670 static struct ccl_rpn_node *search_term(CCL_parser cclp, ccl_qualifier_t *qa)
671 {
672     static int list[] = {CCL_TOK_TERM, CCL_TOK_COMMA, -1};
673     return search_term_x(cclp, qa, list, 0);
674 }
675
676
677 static struct ccl_rpn_node *search_terms2(CCL_parser cclp,
678                                           ccl_qualifier_t *qa)
679 {
680     if (KIND == CCL_TOK_LP)
681     {
682         struct ccl_rpn_node *p;
683         ADVANCE;
684         if (!(p = find_spec(cclp, qa)))
685             return NULL;
686         if (KIND != CCL_TOK_RP)
687         {
688             cclp->error_code = CCL_ERR_RP_EXPECTED;
689             ccl_rpn_delete(p);
690             return NULL;
691         }
692         ADVANCE;
693         return p;
694     }
695     else
696     {
697         static int list[] = {
698             CCL_TOK_TERM, CCL_TOK_COMMA,CCL_TOK_EQ,
699             CCL_TOK_REL, CCL_TOK_SET, -1};
700
701         return search_term_x(cclp, qa, list, 1);
702     }
703 }
704
705
706
707 static
708 struct ccl_rpn_node *qualifiers_order(CCL_parser cclp,
709                                       ccl_qualifier_t *ap, char *attset)
710 {
711     int rel = 0;
712     struct ccl_rpn_node *p;
713
714     if (cclp->look_token->len == 1)
715     {
716         if (cclp->look_token->name[0] == '<')
717             rel = 1;
718         else if (cclp->look_token->name[0] == '=')
719             rel = 3;
720         else if (cclp->look_token->name[0] == '>')
721             rel = 5;
722     }
723     else if (cclp->look_token->len == 2)
724     {
725         if (!memcmp(cclp->look_token->name, "<=", 2))
726             rel = 2;
727         else if (!memcmp(cclp->look_token->name, ">=", 2))
728             rel = 4;
729         else if (!memcmp(cclp->look_token->name, "<>", 2))
730             rel = 6;
731     }
732     if (!rel)
733     {
734         cclp->error_code = CCL_ERR_BAD_RELATION;
735         return NULL;
736     }
737     ADVANCE;  /* skip relation */
738     if (rel == 3 &&
739         qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_PORDER, 0))
740     {
741         /* allow - inside term and treat it as range _always_ */
742         /* relation is =. Extract "embedded" - to separate terms */
743         if (KIND == CCL_TOK_TERM)
744         {
745             size_t i;
746             int quote_mode = 0;
747             for (i = 0; i<cclp->look_token->len; i++)
748             {
749                 if (i > 0 && cclp->look_token->name[i] == '\\')
750                     ;
751                 else if (cclp->look_token->name[i] == '"')
752                     quote_mode = !quote_mode;
753                 else if (cclp->look_token->name[i] == '-' && !quote_mode)
754                     break;
755             }
756
757             if (cclp->look_token->len > 1 && i == 0)
758             {   /*  -xx*/
759                 struct ccl_token *ntoken = ccl_token_add(cclp->look_token);
760
761                 ntoken->kind = CCL_TOK_TERM;
762                 ntoken->name = cclp->look_token->name + 1;
763                 ntoken->len = cclp->look_token->len - 1;
764
765                 cclp->look_token->len = 1;
766                 cclp->look_token->name = "-";
767             }
768             else if (cclp->look_token->len > 1 && i == cclp->look_token->len-1)
769             {   /* xx- */
770                 struct ccl_token *ntoken = ccl_token_add(cclp->look_token);
771
772                 ntoken->kind = CCL_TOK_TERM;
773                 ntoken->name = "-";
774                 ntoken->len = 1;
775
776                 (cclp->look_token->len)--;
777             }
778             else if (cclp->look_token->len > 2 && i < cclp->look_token->len)
779             {   /* xx-yy */
780                 struct ccl_token *ntoken1 = ccl_token_add(cclp->look_token);
781                 struct ccl_token *ntoken2 = ccl_token_add(ntoken1);
782
783                 ntoken1->kind = CCL_TOK_TERM;  /* generate - */
784                 ntoken1->name = "-";
785                 ntoken1->len = 1;
786
787                 ntoken2->kind = CCL_TOK_TERM;  /* generate yy */
788                 ntoken2->name = cclp->look_token->name + (i+1);
789                 ntoken2->len = cclp->look_token->len - (i+1);
790
791                 cclp->look_token->len = i;     /* adjust xx */
792             }
793             else if (i == cclp->look_token->len &&
794                      cclp->look_token->next &&
795                      cclp->look_token->next->kind == CCL_TOK_TERM &&
796                      cclp->look_token->next->len > 1 &&
797                      cclp->look_token->next->name[0] == '-')
798
799             {   /* xx -yy */
800                 /* we _know_ that xx does not have - in it */
801                 struct ccl_token *ntoken = ccl_token_add(cclp->look_token);
802
803                 ntoken->kind = CCL_TOK_TERM;    /* generate - */
804                 ntoken->name = "-";
805                 ntoken->len = 1;
806
807                 (ntoken->next->name)++;        /* adjust yy */
808                 (ntoken->next->len)--;
809             }
810         }
811     }
812
813     if (rel == 3 &&
814         KIND == CCL_TOK_TERM &&
815         cclp->look_token->next && cclp->look_token->next->len == 1 &&
816         cclp->look_token->next->name[0] == '-')
817     {
818         struct ccl_rpn_node *p1;
819         if (!(p1 = search_term(cclp, ap)))
820             return NULL;
821         ADVANCE;                   /* skip '-' */
822         if (KIND == CCL_TOK_TERM)  /* = term - term  ? */
823         {
824             struct ccl_rpn_node *p2;
825
826             if (!(p2 = search_term(cclp, ap)))
827             {
828                 ccl_rpn_delete(p1);
829                 return NULL;
830             }
831             p = ccl_rpn_node_create(CCL_RPN_AND);
832             p->u.p[0] = p1;
833             ccl_add_attr_numeric(p1, attset, CCL_BIB1_REL, 4);
834             p->u.p[1] = p2;
835             ccl_add_attr_numeric(p2, attset, CCL_BIB1_REL, 2);
836             return p;
837         }
838         else                       /* = term -    */
839         {
840             ccl_add_attr_numeric(p1, attset, CCL_BIB1_REL, 4);
841             return p1;
842         }
843     }
844     else if (rel == 3 &&
845              cclp->look_token->len == 1 &&
846              cclp->look_token->name[0] == '-')   /* = - term  ? */
847     {
848         ADVANCE;
849         if (!(p = search_term(cclp, ap)))
850             return NULL;
851         ccl_add_attr_numeric(p, attset, CCL_BIB1_REL, 2);
852         return p;
853     }
854     else
855     {
856         if (!(p = search_terms(cclp, ap)))
857             return NULL;
858         if (rel != 3 ||
859             !qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_OMIT_EQUALS, 0))
860             ccl_add_attr_numeric(p, attset, CCL_BIB1_REL, rel);
861         return p;
862     }
863     return NULL;
864 }
865
866 static
867 struct ccl_rpn_node *qualifier_relation(CCL_parser cclp, ccl_qualifier_t *ap)
868 {
869     char *attset;
870
871     if (qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_ORDER, &attset)
872         || qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_PORDER, &attset))
873         return qualifiers_order(cclp, ap, attset);
874
875     /* unordered relation */
876     if (KIND != CCL_TOK_EQ)
877     {
878         cclp->error_code = CCL_ERR_EQ_EXPECTED;
879         return NULL;
880     }
881     ADVANCE;
882     return search_terms(cclp, ap);
883 }
884
885 /**
886  * qualifier_list: Parse CCL qualifiers and search terms.
887  * cclp:   CCL Parser
888  * la:     Token pointer to RELATION token.
889  * qa:     Qualifier attributes already applied.
890  * return: pointer to node(s); NULL on error.
891  */
892 static struct ccl_rpn_node *qualifier_list(CCL_parser cclp,
893                                            struct ccl_token *la,
894                                            ccl_qualifier_t *qa)
895 {
896     struct ccl_token *lookahead = cclp->look_token;
897     struct ccl_token *look_start = cclp->look_token;
898     ccl_qualifier_t *ap;
899     struct ccl_rpn_node *node = 0;
900     const char **field_str;
901     int no = 0;
902     int seq = 0;
903     int i;
904     int mode_merge = 1;
905 #if 0
906     if (qa)
907     {
908         cclp->error_code = CCL_ERR_DOUBLE_QUAL;
909         return NULL;
910     }
911 #endif
912     for (lookahead = cclp->look_token; lookahead != la;
913          lookahead=lookahead->next)
914         no++;
915     if (qa)
916         for (i=0; qa[i]; i++)
917             no++;
918     ap = (ccl_qualifier_t *)xmalloc((no ? (no+1) : 2) * sizeof(*ap));
919     ccl_assert(ap);
920
921     field_str = ccl_qual_search_special(cclp->bibset, "field");
922     if (field_str)
923     {
924         if (!strcmp(field_str[0], "or"))
925             mode_merge = 0;
926         else if (!strcmp(field_str[0], "merge"))
927             mode_merge = 1;
928     }
929     if (!mode_merge)
930     {
931         /* consider each field separately and OR */
932         lookahead = look_start;
933         while (lookahead != la)
934         {
935             ap[1] = 0;
936             seq = 0;
937             while ((ap[0] = ccl_qual_search(cclp, lookahead->name,
938                                             lookahead->len, seq)) != 0)
939             {
940                 struct ccl_rpn_node *node_sub;
941                 cclp->look_token = la;
942
943                 node_sub = qualifier_relation(cclp, ap);
944                 if (!node_sub)
945                 {
946                     ccl_rpn_delete(node);
947                     xfree(ap);
948                     return 0;
949                 }
950                 if (node)
951                 {
952                     struct ccl_rpn_node *node_this =
953                         ccl_rpn_node_create(CCL_RPN_OR);
954                     node_this->u.p[0] = node;
955                     node_this->u.p[1] = node_sub;
956                     node = node_this;
957                 }
958                 else
959                     node = node_sub;
960                 seq++;
961             }
962             if (seq == 0)
963             {
964                 cclp->look_token = lookahead;
965                 cclp->error_code = CCL_ERR_UNKNOWN_QUAL;
966                 xfree(ap);
967                 return NULL;
968             }
969             lookahead = lookahead->next;
970             if (lookahead->kind == CCL_TOK_COMMA)
971                 lookahead = lookahead->next;
972         }
973     }
974     else
975     {
976         /* merge attributes from ALL fields - including inherited ones */
977         while (1)
978         {
979             struct ccl_rpn_node *node_sub;
980             int found = 0;
981             lookahead = look_start;
982             for (i = 0; lookahead != la; i++)
983             {
984                 ap[i] = ccl_qual_search(cclp, lookahead->name,
985                                          lookahead->len, seq);
986                 if (ap[i])
987                     found++;
988                 if (!ap[i] && seq > 0)
989                     ap[i] = ccl_qual_search(cclp, lookahead->name,
990                                              lookahead->len, 0);
991                 if (!ap[i])
992                 {
993                     cclp->look_token = lookahead;
994                     cclp->error_code = CCL_ERR_UNKNOWN_QUAL;
995                     xfree(ap);
996                     return NULL;
997                 }
998                 lookahead = lookahead->next;
999                 if (lookahead->kind == CCL_TOK_COMMA)
1000                     lookahead = lookahead->next;
1001             }
1002             if (qa)
1003             {
1004                 ccl_qualifier_t *qa0 = qa;
1005
1006                 while (*qa0)
1007                     ap[i++] = *qa0++;
1008             }
1009             ap[i] = NULL;
1010
1011             if (!found)
1012                 break;
1013
1014             cclp->look_token = lookahead;
1015
1016             node_sub = qualifier_relation(cclp, ap);
1017             if (!node_sub)
1018             {
1019                 ccl_rpn_delete(node);
1020                 break;
1021             }
1022             if (node)
1023             {
1024                 struct ccl_rpn_node *node_this =
1025                     ccl_rpn_node_create(CCL_RPN_OR);
1026                 node_this->u.p[0] = node;
1027                 node_this->u.p[1] = node_sub;
1028                 node = node_this;
1029             }
1030             else
1031                 node = node_sub;
1032             seq++;
1033         }
1034     }
1035     xfree(ap);
1036     return node;
1037 }
1038
1039
1040 /**
1041  * search_terms: Parse CCL search terms - including proximity.
1042  * cclp:   CCL Parser
1043  * qa:     Qualifier attributes already applied.
1044  * return: pointer to node(s); NULL on error.
1045  */
1046 static struct ccl_rpn_node *search_terms(CCL_parser cclp, ccl_qualifier_t *qa)
1047 {
1048     static int list[] = {
1049         CCL_TOK_TERM, CCL_TOK_COMMA,CCL_TOK_EQ,
1050         CCL_TOK_REL, CCL_TOK_SET, -1};
1051     struct ccl_rpn_node *p1, *p2, *pn;
1052     p1 = search_terms2(cclp, qa);
1053     if (!p1)
1054         return NULL;
1055     while (1)
1056     {
1057         if (KIND == CCL_TOK_PROX)
1058         {
1059             struct ccl_rpn_node *p_prox = 0;
1060             /* ! word order specified */
1061             /* % word order not specified */
1062             p_prox = ccl_rpn_node_create(CCL_RPN_TERM);
1063             p_prox->u.t.term = (char *) xmalloc(1 + cclp->look_token->len);
1064             memcpy(p_prox->u.t.term, cclp->look_token->name,
1065                    cclp->look_token->len);
1066             p_prox->u.t.term[cclp->look_token->len] = 0;
1067             p_prox->u.t.attr_list = 0;
1068
1069             ADVANCE;
1070             p2 = search_terms2(cclp, qa);
1071             if (!p2)
1072             {
1073                 ccl_rpn_delete(p1);
1074                 return NULL;
1075             }
1076             pn = ccl_rpn_node_create(CCL_RPN_PROX);
1077             pn->u.p[0] = p1;
1078             pn->u.p[1] = p2;
1079             pn->u.p[2] = p_prox;
1080             p1 = pn;
1081         }
1082         else if (is_term_ok(KIND, list))
1083         {
1084             p2 = search_terms2(cclp, qa);
1085             if (!p2)
1086             {
1087                 ccl_rpn_delete(p1);
1088                 return NULL;
1089             }
1090             pn = ccl_rpn_node_create(CCL_RPN_PROX);
1091             pn->u.p[0] = p1;
1092             pn->u.p[1] = p2;
1093             pn->u.p[2] = 0;
1094             p1 = pn;
1095         }
1096         else
1097             break;
1098     }
1099     return p1;
1100 }
1101
1102 /**
1103  * search_elements: Parse CCL search elements
1104  * cclp:   CCL Parser
1105  * qa:     Qualifier attributes already applied.
1106  * return: pointer to node(s); NULL on error.
1107  */
1108 static struct ccl_rpn_node *search_elements(CCL_parser cclp,
1109                                             ccl_qualifier_t *qa)
1110 {
1111     struct ccl_rpn_node *p1;
1112     struct ccl_token *lookahead;
1113     if (KIND == CCL_TOK_SET)
1114     {
1115         ADVANCE;
1116         if (KIND == CCL_TOK_EQ)
1117             ADVANCE;
1118         if (KIND != CCL_TOK_TERM)
1119         {
1120             cclp->error_code = CCL_ERR_SETNAME_EXPECTED;
1121             return NULL;
1122         }
1123         p1 = ccl_rpn_node_create(CCL_RPN_SET);
1124         p1->u.setname = copy_token_name(cclp->look_token);
1125         ADVANCE;
1126         return p1;
1127     }
1128     lookahead = cclp->look_token;
1129
1130     while (lookahead->kind==CCL_TOK_TERM)
1131     {
1132         lookahead = lookahead->next;
1133         if (lookahead->kind == CCL_TOK_REL || lookahead->kind == CCL_TOK_EQ)
1134             return qualifier_list(cclp, lookahead, qa);
1135         if (lookahead->kind != CCL_TOK_COMMA)
1136             break;
1137         lookahead = lookahead->next;
1138     }
1139     if (qa || lookahead->kind == CCL_TOK_LP)
1140         return search_terms(cclp, qa);
1141     else
1142     {
1143         ccl_qualifier_t qa[2];
1144         struct ccl_rpn_node *node = 0;
1145         int seq;
1146         lookahead = cclp->look_token;
1147
1148         qa[1] = 0;
1149         for(seq = 0; ;seq++)
1150         {
1151             struct ccl_rpn_node *node_sub;
1152             qa[0] = ccl_qual_search(cclp, "term", 4, seq);
1153             if (!qa[0])
1154                 break;
1155
1156             cclp->look_token = lookahead;
1157
1158             node_sub = search_terms(cclp, qa);
1159             if (!node_sub)
1160             {
1161                 ccl_rpn_delete(node);
1162                 return 0;
1163             }
1164             if (node)
1165             {
1166                 struct ccl_rpn_node *node_this =
1167                     ccl_rpn_node_create(CCL_RPN_OR);
1168                 node_this->u.p[0] = node;
1169                 node_this->u.p[1] = node_sub;
1170                 node_this->u.p[2] = 0;
1171                 node = node_this;
1172             }
1173             else
1174                 node = node_sub;
1175         }
1176         if (!node)
1177             node = search_terms(cclp, 0);
1178         return node;
1179     }
1180 }
1181
1182 /**
1183  * find_spec: Parse CCL find specification
1184  * cclp:   CCL Parser
1185  * qa:     Qualifier attributes already applied.
1186  * return: pointer to node(s); NULL on error.
1187  */
1188 static struct ccl_rpn_node *find_spec(CCL_parser cclp, ccl_qualifier_t *qa)
1189 {
1190     struct ccl_rpn_node *p1, *p2, *pn;
1191     if (!(p1 = search_elements(cclp, qa)))
1192         return NULL;
1193     while (1)
1194     {
1195         switch (KIND)
1196         {
1197         case CCL_TOK_AND:
1198             ADVANCE;
1199             p2 = search_elements(cclp, qa);
1200             if (!p2)
1201             {
1202                 ccl_rpn_delete(p1);
1203                 return NULL;
1204             }
1205             pn = ccl_rpn_node_create(CCL_RPN_AND);
1206             pn->u.p[0] = p1;
1207             pn->u.p[1] = p2;
1208             pn->u.p[2] = 0;
1209             p1 = pn;
1210             continue;
1211         case CCL_TOK_OR:
1212             ADVANCE;
1213             p2 = search_elements(cclp, qa);
1214             if (!p2)
1215             {
1216                 ccl_rpn_delete(p1);
1217                 return NULL;
1218             }
1219             pn = ccl_rpn_node_create(CCL_RPN_OR);
1220             pn->u.p[0] = p1;
1221             pn->u.p[1] = p2;
1222             pn->u.p[2] = 0;
1223             p1 = pn;
1224             continue;
1225         case CCL_TOK_NOT:
1226             ADVANCE;
1227             p2 = search_elements(cclp, qa);
1228             if (!p2)
1229             {
1230                 ccl_rpn_delete(p1);
1231                 return NULL;
1232             }
1233             pn = ccl_rpn_node_create(CCL_RPN_NOT);
1234             pn->u.p[0] = p1;
1235             pn->u.p[1] = p2;
1236             pn->u.p[2] = 0;
1237             p1 = pn;
1238             continue;
1239         }
1240         break;
1241     }
1242     return p1;
1243 }
1244
1245 struct ccl_rpn_node *ccl_parser_find_str(CCL_parser cclp, const char *str)
1246 {
1247     struct ccl_rpn_node *p;
1248     struct ccl_token *list = ccl_parser_tokenize(cclp, str);
1249     p = ccl_parser_find_token(cclp, list);
1250     ccl_token_del(list);
1251     return p;
1252 }
1253
1254 struct ccl_rpn_node *ccl_parser_find_token(CCL_parser cclp,
1255                                            struct ccl_token *list)
1256 {
1257     struct ccl_rpn_node *p;
1258
1259     cclp->look_token = list;
1260     p = find_spec(cclp, NULL);
1261     if (p && KIND != CCL_TOK_EOL)
1262     {
1263         if (KIND == CCL_TOK_RP)
1264             cclp->error_code = CCL_ERR_BAD_RP;
1265         else
1266             cclp->error_code = CCL_ERR_OP_EXPECTED;
1267         ccl_rpn_delete(p);
1268         p = NULL;
1269     }
1270     cclp->error_pos = cclp->look_token->name;
1271     if (p)
1272         cclp->error_code = CCL_ERR_OK;
1273     else
1274         cclp->error_code = cclp->error_code;
1275     return p;
1276 }
1277
1278 /**
1279  * ccl_find_str: Parse CCL find - string representation
1280  * bibset:  Bibset to be used for the parsing
1281  * str:     String to be parsed
1282  * error:   Pointer to integer. Holds error no. on completion.
1283  * pos:     Pointer to char position. Holds approximate error position.
1284  * return:  RPN tree on successful completion; NULL otherwise.
1285  */
1286 struct ccl_rpn_node *ccl_find_str(CCL_bibset bibset, const char *str,
1287                                   int *error, int *pos)
1288 {
1289     CCL_parser cclp = ccl_parser_create(bibset);
1290     struct ccl_token *list;
1291     struct ccl_rpn_node *p;
1292
1293     list = ccl_parser_tokenize(cclp, str);
1294     p = ccl_parser_find_token(cclp, list);
1295
1296     *error = cclp->error_code;
1297     if (*error)
1298         *pos = cclp->error_pos - str;
1299     ccl_parser_destroy(cclp);
1300     ccl_token_del(list);
1301     return p;
1302 }
1303
1304 /*
1305  * Local variables:
1306  * c-basic-offset: 4
1307  * c-file-style: "Stroustrup"
1308  * indent-tabs-mode: nil
1309  * End:
1310  * vim: shiftwidth=4 tabstop=8 expandtab
1311  */
1312