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