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