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