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