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