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