Various clean up. Removed function ccl_parset_get_bibset. Removed
[yaz-moved-to-github.git] / src / cclfind.c
1 /*
2  * Copyright (c) 1995, the EUROPAGATE consortium (see below).
3  *
4  * The EUROPAGATE consortium members are:
5  *
6  *    University College Dublin
7  *    Danmarks Teknologiske Videnscenter
8  *    An Chomhairle Leabharlanna
9  *    Consejo Superior de Investigaciones Cientificas
10  *
11  * Permission to use, copy, modify, distribute, and sell this software and
12  * its documentation, in whole or in part, for any purpose, is hereby granted,
13  * provided that:
14  *
15  * 1. This copyright and permission notice appear in all copies of the
16  * software and its documentation. Notices of copyright or attribution
17  * which appear at the beginning of any file must remain unchanged.
18  *
19  * 2. The names of EUROPAGATE or the project partners may not be used to
20  * endorse or promote products derived from this software without specific
21  * prior written permission.
22  *
23  * 3. Users of this software (implementors and gateway operators) agree to
24  * inform the EUROPAGATE consortium of their use of the software. This
25  * information will be used to evaluate the EUROPAGATE project and the
26  * software, and to plan further developments. The consortium may use
27  * the information in later publications.
28  * 
29  * 4. Users of this software agree to make their best efforts, when
30  * documenting their use of the software, to acknowledge the EUROPAGATE
31  * consortium, and the role played by the software in their work.
32  *
33  * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTY OF ANY KIND,
34  * EXPRESS, IMPLIED, OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
35  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
36  * IN NO EVENT SHALL THE EUROPAGATE CONSORTIUM OR ITS MEMBERS BE LIABLE
37  * FOR ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF
38  * ANY KIND, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
39  * OR PROFITS, WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND
40  * ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
41  * USE OR PERFORMANCE OF THIS SOFTWARE.
42  *
43  */
44
45 /** 
46  * \file cclfind.c
47  * \brief Implements parsing of a CCL FIND query.
48  *
49  * This source file implements parsing of a CCL Query (ISO8777).
50  * The parser uses predictive parsing, but it does several tokens
51  * of lookahead in the handling of relational operations.. So
52  * it's not really pure.
53  */
54
55
56 /* CCL find (to rpn conversion)
57  * Europagate, 1995
58  *
59  * $Id: cclfind.c,v 1.10 2007-04-26 09:11:56 adam Exp $
60  *
61  * Old Europagate log:
62  *
63  * Revision 1.16  1996/01/08  08:41:13  adam
64  * Removed unused function.
65  *
66  * Revision 1.15  1995/07/20  08:14:34  adam
67  * Qualifiers were observed too often. Instead tokens are treated as
68  * qualifiers only when separated by comma.
69  *
70  * Revision 1.14  1995/05/16  09:39:26  adam
71  * LICENSE.
72  *
73  * Revision 1.13  1995/04/17  09:31:42  adam
74  * Improved handling of qualifiers. Aliases or reserved words.
75  *
76  * Revision 1.12  1995/03/20  15:27:43  adam
77  * Minor changes.
78  *
79  * Revision 1.11  1995/02/23  08:31:59  adam
80  * Changed header.
81  *
82  * Revision 1.9  1995/02/16  13:20:06  adam
83  * Spell fix.
84  *
85  * Revision 1.8  1995/02/14  19:59:42  adam
86  * Removed a syntax error.
87  *
88  * Revision 1.7  1995/02/14  19:55:10  adam
89  * Header files ccl.h/cclp.h are gone! They have been merged an
90  * moved to ../include/ccl.h.
91  * Node kind(s) in ccl_rpn_node have changed names.
92  *
93  * Revision 1.6  1995/02/14  16:20:55  adam
94  * Qualifiers are read from a file now.
95  *
96  * Revision 1.5  1995/02/14  14:12:41  adam
97  * Ranges for ordered qualfiers implemented (e.g. pd=1980-1990).
98  *
99  * Revision 1.4  1995/02/14  13:16:29  adam
100  * Left and/or right truncation implemented.
101  *
102  * Revision 1.3  1995/02/14  10:25:56  adam
103  * The constructions 'qualifier rel term ...' implemented.
104  *
105  * Revision 1.2  1995/02/13  15:15:07  adam
106  * Added handling of qualifiers. Not finished yet.
107  *
108  * Revision 1.1  1995/02/13  12:35:20  adam
109  * First version of CCL. Qualifiers aren't handled yet.
110  *
111  */
112
113 #include <stdlib.h>
114 #include <string.h>
115
116 #include "cclp.h"
117
118 /* returns type of current lookahead */
119 #define KIND (cclp->look_token->kind)
120
121 /* move one token forward */
122 #define ADVANCE cclp->look_token = cclp->look_token->next
123
124 /**
125  * qual_val_type: test for existance of attribute type/value pair.
126  * qa:     Attribute array
127  * type:   Type of attribute to search for
128  * value:  Value of attribute to seach for
129  * return: 1 if found; 0 otherwise.
130  */
131 static int qual_val_type (struct ccl_rpn_attr **qa, int type, int value,
132                            char **attset)
133 {
134     int i;
135     struct ccl_rpn_attr *q;
136
137     if (!qa)
138         return 0;
139     for (i = 0;  (q=qa[i]); i++)
140         while (q)
141         {
142             if (q->type == type && q->kind == CCL_RPN_ATTR_NUMERIC &&
143                 q->value.numeric == value)
144             {
145                 if (attset)
146                     *attset = q->set;
147                 return 1;
148             }
149             q = q->next;
150         }
151     return 0;
152 }
153
154 /**
155  * strxcat: concatenate strings.
156  * n:      Null-terminated Destination string 
157  * src:    Source string to be appended (not null-terminated)
158  * len:    Length of source string.
159  */
160 static void strxcat (char *n, const char *src, int len)
161 {
162     while (*n)
163         n++;
164     while (--len >= 0)
165         *n++ = *src++;
166     *n = '\0';
167 }
168
169 /**
170  * copy_token_name: Return copy of CCL token name
171  * tp:      Pointer to token info.
172  * return:  malloc(3) allocated copy of token name.
173  */
174 static char *copy_token_name (struct ccl_token *tp)
175 {
176     char *str = (char *)xmalloc (tp->len + 1);
177     ccl_assert (str);
178     memcpy (str, tp->name, tp->len);
179     str[tp->len] = '\0';
180     return str;
181 }
182
183 /**
184  * mk_node: Create RPN node.
185  * kind:   Type of node.
186  * return: pointer to allocated node.
187  */
188 static struct ccl_rpn_node *mk_node (int kind)
189 {
190     struct ccl_rpn_node *p;
191     p = (struct ccl_rpn_node *)xmalloc (sizeof(*p));
192     ccl_assert (p);
193     p->kind = kind;
194     return p;
195 }
196
197 /**
198  * ccl_rpn_delete: Delete RPN tree.
199  * rpn:   Pointer to tree.
200  */
201 void ccl_rpn_delete (struct ccl_rpn_node *rpn)
202 {
203     struct ccl_rpn_attr *attr, *attr1;
204     if (!rpn)
205         return;
206     switch (rpn->kind)
207     {
208     case CCL_RPN_AND:
209     case CCL_RPN_OR:
210     case CCL_RPN_NOT:
211         ccl_rpn_delete (rpn->u.p[0]);
212         ccl_rpn_delete (rpn->u.p[1]);
213         break;
214     case CCL_RPN_TERM:
215         xfree (rpn->u.t.term);
216         for (attr = rpn->u.t.attr_list; attr; attr = attr1)
217         {
218             attr1 = attr->next;
219             if (attr->kind == CCL_RPN_ATTR_STRING)
220                 xfree(attr->value.str);
221             if (attr->set)
222                 xfree (attr->set);
223             xfree (attr);
224         }
225         break;
226     case CCL_RPN_SET:
227         xfree (rpn->u.setname);
228         break;
229     case CCL_RPN_PROX:
230         ccl_rpn_delete (rpn->u.p[0]);
231         ccl_rpn_delete (rpn->u.p[1]);
232         ccl_rpn_delete (rpn->u.p[2]);
233         break;
234     }
235     xfree (rpn);
236 }
237
238 static struct ccl_rpn_node *find_spec (CCL_parser cclp,
239                                        struct ccl_rpn_attr **qa);
240
241 static int is_term_ok (int look, int *list)
242 {
243     for (;*list >= 0; list++)
244         if (look == *list)
245             return 1;
246     return 0;
247 }
248
249 static struct ccl_rpn_node *search_terms (CCL_parser cclp,
250                                           struct ccl_rpn_attr **qa);
251
252 static struct ccl_rpn_attr *add_attr_node (struct ccl_rpn_node *p,
253                                            const char *set, int type)
254 {
255     struct ccl_rpn_attr *n;
256     
257     n = (struct ccl_rpn_attr *)xmalloc (sizeof(*n));
258     ccl_assert (n);
259     if (set)
260         n->set = xstrdup(set);
261     else
262         n->set = 0;
263     n->type = type;
264     n->next = p->u.t.attr_list;
265     p->u.t.attr_list = n;
266     
267     n->kind = CCL_RPN_ATTR_NUMERIC;
268     n->value.numeric = 0;
269     return n;
270 }
271
272 /**
273  * add_attr_numeric: Add attribute (type/value) to RPN term node.
274  * p:     RPN node of type term.
275  * type:  Type of attribute
276  * value: Value of attribute
277  * set: Attribute set name
278  */
279 static void add_attr_numeric (struct ccl_rpn_node *p, const char *set,
280                               int type, int value)
281 {
282     struct ccl_rpn_attr *n;
283
284     n = add_attr_node(p, set, type);
285     n->kind = CCL_RPN_ATTR_NUMERIC;
286     n->value.numeric = value;
287 }
288
289 static void add_attr_string (struct ccl_rpn_node *p, const char *set,
290                              int type, char *value)
291 {
292     struct ccl_rpn_attr *n;
293
294     n = add_attr_node(p, set, type);
295     n->kind = CCL_RPN_ATTR_STRING;
296     n->value.str = xstrdup(value);
297 }
298
299
300 /**
301  * search_term: Parse CCL search term. 
302  * cclp:   CCL Parser
303  * qa:     Qualifier attributes already applied.
304  * term_list: tokens we accept as terms in context
305  * multi:  whether we accept "multiple" tokens
306  * return: pointer to node(s); NULL on error.
307  */
308 static struct ccl_rpn_node *search_term_x (CCL_parser cclp,
309                                            struct ccl_rpn_attr **qa,
310                                            int *term_list, int multi)
311 {
312     struct ccl_rpn_node *p_top = 0;
313     struct ccl_token *lookahead = cclp->look_token;
314     int and_list = 0;
315     int or_list = 0;
316     char *attset;
317     const char *truncation_aliases;
318
319     truncation_aliases =
320         ccl_qual_search_special(cclp->bibset, "truncation");
321     if (!truncation_aliases)
322         truncation_aliases = "?";
323
324     if (qual_val_type (qa, CCL_BIB1_STR, CCL_BIB1_STR_AND_LIST, 0))
325         and_list = 1;
326     if (qual_val_type (qa, CCL_BIB1_STR, CCL_BIB1_STR_OR_LIST, 0))
327         or_list = 1;
328     while (1)
329     {
330         struct ccl_rpn_node *p;
331         size_t no, i;
332         int no_spaces = 0;
333         int left_trunc = 0;
334         int right_trunc = 0;
335         int mid_trunc = 0;
336         int relation_value = -1;
337         int position_value = -1;
338         int structure_value = -1;
339         int truncation_value = -1;
340         int completeness_value = -1;
341         int len = 0;
342         size_t max = 200;
343         if (and_list || or_list || !multi)
344             max = 1;
345         
346         /* ignore commas when dealing with and-lists .. */
347         if (and_list && lookahead && lookahead->kind == CCL_TOK_COMMA)
348         {
349             lookahead = lookahead->next;
350             ADVANCE;
351             continue;
352         }
353         /* go through each TERM token. If no truncation attribute is yet
354            met, then look for left/right truncation markers (?) and
355            set left_trunc/right_trunc/mid_trunc accordingly */
356         for (no = 0; no < max && is_term_ok(lookahead->kind, term_list); no++)
357         {
358             for (i = 0; i<lookahead->len; i++)
359                 if (lookahead->name[i] == ' ')
360                     no_spaces++;
361                 else if (strchr(truncation_aliases, lookahead->name[i]))
362                 {
363                     if (no == 0 && i == 0 && lookahead->len >= 1)
364                         left_trunc = 1;
365                     else if (!is_term_ok(lookahead->next->kind, term_list) &&
366                              i == lookahead->len-1 && i >= 1)
367                         right_trunc = 1;
368                     else
369                         mid_trunc = 1;
370                 }
371             len += 1+lookahead->len+lookahead->ws_prefix_len;
372             lookahead = lookahead->next;
373         }
374
375         if (len == 0)
376             break;      /* no more terms . stop . */
377
378
379         if (p_top)
380         {
381             if (or_list)
382                 p = mk_node (CCL_RPN_OR);
383             else if (and_list)
384                 p = mk_node (CCL_RPN_AND);
385             else
386                 p = mk_node (CCL_RPN_AND);
387             p->u.p[0] = p_top;
388             p_top = p;
389         }
390                 
391         /* create the term node, but wait a moment before adding the term */
392         p = mk_node (CCL_RPN_TERM);
393         p->u.t.attr_list = NULL;
394         p->u.t.term = NULL;
395
396         /* make the top node point to us.. */
397         if (p_top)
398             p_top->u.p[1] = p;
399         else
400             p_top = p;
401
402         
403         /* go through all attributes and add them to the attribute list */
404         for (i=0; qa && qa[i]; i++)
405         {
406             struct ccl_rpn_attr *attr;
407             
408             for (attr = qa[i]; attr; attr = attr->next)
409                 switch(attr->kind)
410                 {
411                 case CCL_RPN_ATTR_STRING:
412                     add_attr_string(p, attr->set, attr->type,
413                                     attr->value.str);
414                     break;
415                 case CCL_RPN_ATTR_NUMERIC:
416                     if (attr->value.numeric > 0)
417                     {   /* deal only with REAL attributes (positive) */
418                         switch (attr->type)
419                         {
420                         case CCL_BIB1_REL:
421                             if (relation_value != -1)
422                                 continue;
423                             relation_value = attr->value.numeric;
424                             break;
425                         case CCL_BIB1_POS:
426                             if (position_value != -1)
427                                 continue;
428                             position_value = attr->value.numeric;
429                             break;
430                         case CCL_BIB1_STR:
431                             if (structure_value != -1)
432                                 continue;
433                             structure_value = attr->value.numeric;
434                             break;
435                         case CCL_BIB1_TRU:
436                             if (truncation_value != -1)
437                                 continue;
438                             truncation_value = attr->value.numeric;
439                             left_trunc = right_trunc = mid_trunc = 0;
440                             break;
441                         case CCL_BIB1_COM:
442                             if (completeness_value != -1)
443                                 continue;
444                             completeness_value = attr->value.numeric;
445                             break;
446                         }
447                         add_attr_numeric(p, attr->set, attr->type,
448                                          attr->value.numeric);
449                     }
450                 }
451         }
452         /* len now holds the number of characters in the RPN term */
453         /* no holds the number of CCL tokens (1 or more) */
454         
455         if (structure_value == -1 && 
456             qual_val_type (qa, CCL_BIB1_STR, CCL_BIB1_STR_WP, &attset))
457         {   /* no structure attribute met. Apply either structure attribute 
458                WORD or PHRASE depending on number of CCL tokens */
459             if (no == 1 && no_spaces == 0)
460                 add_attr_numeric (p, attset, CCL_BIB1_STR, 2);
461             else
462                 add_attr_numeric (p, attset, CCL_BIB1_STR, 1);
463         }
464
465         /* make the RPN token */
466         p->u.t.term = (char *)xmalloc (len);
467         ccl_assert (p->u.t.term);
468         p->u.t.term[0] = '\0';
469         for (i = 0; i<no; i++)
470         {
471             const char *src_str = cclp->look_token->name;
472             int src_len = cclp->look_token->len;
473             
474             if (i == 0 && left_trunc)
475             {
476                 src_len--;
477                 src_str++;
478             }
479             if (i == no-1 && right_trunc)
480                 src_len--;
481             if (i && cclp->look_token->ws_prefix_len)
482             {
483                 size_t len = strlen(p->u.t.term);
484                 memcpy(p->u.t.term + len, cclp->look_token->ws_prefix_buf,
485                                 cclp->look_token->ws_prefix_len);
486                 p->u.t.term[len + cclp->look_token->ws_prefix_len] = '\0';
487             }
488             strxcat (p->u.t.term, src_str, src_len);
489             ADVANCE;
490         }
491         if (left_trunc && right_trunc)
492         {
493             if (!qual_val_type (qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_BOTH,
494                                 &attset))
495             {
496                 cclp->error_code = CCL_ERR_TRUNC_NOT_BOTH;
497                 ccl_rpn_delete (p);
498                 return NULL;
499             }
500             add_attr_numeric (p, attset, CCL_BIB1_TRU, 3);
501         }
502         else if (right_trunc)
503         {
504             if (!qual_val_type (qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_RIGHT,
505                                  &attset))
506             {
507                 cclp->error_code = CCL_ERR_TRUNC_NOT_RIGHT;
508                 ccl_rpn_delete (p);
509                 return NULL;
510             }
511             add_attr_numeric (p, attset, CCL_BIB1_TRU, 1);
512         }
513         else if (left_trunc)
514         {
515             if (!qual_val_type (qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_LEFT,
516                                 &attset))
517             {
518                 cclp->error_code = CCL_ERR_TRUNC_NOT_LEFT;
519                 ccl_rpn_delete (p);
520                 return NULL;
521             }
522             add_attr_numeric (p, attset, CCL_BIB1_TRU, 2);
523         }
524         else
525         {
526             if (qual_val_type (qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_NONE,
527                                &attset))
528                 add_attr_numeric (p, attset, CCL_BIB1_TRU, 100);
529         }
530         if (!multi)
531             break;
532     }
533     if (!p_top)
534         cclp->error_code = CCL_ERR_TERM_EXPECTED;
535     return p_top;
536 }
537
538 static struct ccl_rpn_node *search_term (CCL_parser cclp,
539                                          struct ccl_rpn_attr **qa)
540 {
541     static int list[] = {CCL_TOK_TERM, CCL_TOK_COMMA, -1};
542     return search_term_x(cclp, qa, list, 0);
543 }
544
545 static
546 struct ccl_rpn_node *qualifiers_order (CCL_parser cclp,
547                                        struct ccl_rpn_attr **ap, char *attset)
548 {
549     int rel = 0;
550     struct ccl_rpn_node *p;
551
552     if (cclp->look_token->len == 1)
553     {
554         if (cclp->look_token->name[0] == '<')
555             rel = 1;
556         else if (cclp->look_token->name[0] == '=')
557             rel = 3;
558         else if (cclp->look_token->name[0] == '>')
559             rel = 5;
560     }
561     else if (cclp->look_token->len == 2)
562     {
563         if (!memcmp (cclp->look_token->name, "<=", 2))
564             rel = 2;
565         else if (!memcmp (cclp->look_token->name, ">=", 2))
566             rel = 4;
567         else if (!memcmp (cclp->look_token->name, "<>", 2))
568             rel = 6;
569     }
570     if (!rel)
571     {
572         cclp->error_code = CCL_ERR_BAD_RELATION;
573         return NULL;
574     }
575     ADVANCE;  /* skip relation */
576     if (rel == 3 &&
577         qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_PORDER, 0))
578     {
579         /* allow - inside term and treat it as range _always_ */
580         /* relation is =. Extract "embedded" - to separate terms */
581         if (KIND == CCL_TOK_TERM)
582         {
583             size_t i;
584             for (i = 0; i<cclp->look_token->len; i++)
585             {
586                 if (cclp->look_token->name[i] == '-')
587                     break;
588             }
589             
590             if (cclp->look_token->len > 1 && i == 0)
591             {   /*  -xx*/
592                 struct ccl_token *ntoken = ccl_token_add (cclp->look_token);
593
594                 ntoken->kind = CCL_TOK_TERM;
595                 ntoken->name = cclp->look_token->name + 1;
596                 ntoken->len = cclp->look_token->len - 1;
597
598                 cclp->look_token->len = 1;
599                 cclp->look_token->name = "-";
600             }
601             else if (cclp->look_token->len > 1 && i == cclp->look_token->len-1)
602             {   /* xx- */
603                 struct ccl_token *ntoken = ccl_token_add (cclp->look_token);
604
605                 ntoken->kind = CCL_TOK_TERM;
606                 ntoken->name = "-";
607                 ntoken->len = 1;
608
609                 (cclp->look_token->len)--;
610             }
611             else if (cclp->look_token->len > 2 && i < cclp->look_token->len)
612             {   /* xx-yy */
613                 struct ccl_token *ntoken1 = ccl_token_add (cclp->look_token);
614                 struct ccl_token *ntoken2 = ccl_token_add (ntoken1);
615
616                 ntoken1->kind = CCL_TOK_TERM;  /* generate - */
617                 ntoken1->name = "-";
618                 ntoken1->len = 1;
619
620                 ntoken2->kind = CCL_TOK_TERM;  /* generate yy */
621                 ntoken2->name = cclp->look_token->name + (i+1);
622                 ntoken2->len = cclp->look_token->len - (i+1);
623
624                 cclp->look_token->len = i;     /* adjust xx */
625             }
626             else if (i == cclp->look_token->len &&
627                      cclp->look_token->next &&
628                      cclp->look_token->next->kind == CCL_TOK_TERM &&
629                      cclp->look_token->next->len > 1 &&
630                      cclp->look_token->next->name[0] == '-')
631                      
632             {   /* xx -yy */
633                 /* we _know_ that xx does not have - in it */
634                 struct ccl_token *ntoken = ccl_token_add (cclp->look_token);
635
636                 ntoken->kind = CCL_TOK_TERM;    /* generate - */
637                 ntoken->name = "-";
638                 ntoken->len = 1;
639
640                 (ntoken->next->name)++;        /* adjust yy */
641                 (ntoken->next->len)--; 
642             }
643         }
644     }
645         
646     if (rel == 3 &&
647         KIND == CCL_TOK_TERM &&
648         cclp->look_token->next && cclp->look_token->next->len == 1 &&
649         cclp->look_token->next->name[0] == '-')
650     {
651         struct ccl_rpn_node *p1;
652         if (!(p1 = search_term (cclp, ap)))
653             return NULL;
654         ADVANCE;                   /* skip '-' */
655         if (KIND == CCL_TOK_TERM)  /* = term - term  ? */
656         {
657             struct ccl_rpn_node *p2;
658             
659             if (!(p2 = search_term (cclp, ap)))
660             {
661                 ccl_rpn_delete (p1);
662                 return NULL;
663             }
664             p = mk_node (CCL_RPN_AND);
665             p->u.p[0] = p1;
666             add_attr_numeric (p1, attset, CCL_BIB1_REL, 4);
667             p->u.p[1] = p2;
668             add_attr_numeric (p2, attset, CCL_BIB1_REL, 2);
669             return p;
670         }
671         else                       /* = term -    */
672         {
673             add_attr_numeric (p1, attset, CCL_BIB1_REL, 4);
674             return p1;
675         }
676     }
677     else if (rel == 3 &&
678              cclp->look_token->len == 1 &&
679              cclp->look_token->name[0] == '-')   /* = - term  ? */
680     {
681         ADVANCE;
682         if (!(p = search_term (cclp, ap)))
683             return NULL;
684         add_attr_numeric (p, attset, CCL_BIB1_REL, 2);
685         return p;
686     }
687     else if (KIND == CCL_TOK_LP)
688     {
689         ADVANCE;
690         if (!(p = find_spec (cclp, ap)))
691             return NULL;
692         if (KIND != CCL_TOK_RP)
693         {
694             cclp->error_code = CCL_ERR_RP_EXPECTED;
695             ccl_rpn_delete (p);
696             return NULL;
697         }
698         ADVANCE;
699         return p;
700     }
701     else
702     {
703         if (!(p = search_terms (cclp, ap)))
704             return NULL;
705         add_attr_numeric (p, attset, CCL_BIB1_REL, rel);
706         return p;
707     }
708     cclp->error_code = CCL_ERR_TERM_EXPECTED;
709     return NULL;
710 }
711
712 static
713 struct ccl_rpn_node *qualifiers2 (CCL_parser cclp, struct ccl_rpn_attr **ap)
714 {
715     char *attset;
716     struct ccl_rpn_node *p;
717     
718     if (qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_ORDER, &attset)
719         || qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_PORDER, &attset))
720         return qualifiers_order(cclp, ap, attset);
721
722     /* unordered relation */
723     if (KIND != CCL_TOK_EQ)
724     {
725         cclp->error_code = CCL_ERR_EQ_EXPECTED;
726         return NULL;
727     }
728     ADVANCE;
729     if (KIND == CCL_TOK_LP)
730     {
731         ADVANCE;
732         if (!(p = find_spec (cclp, ap)))
733         {
734             return NULL;
735         }
736         if (KIND != CCL_TOK_RP)
737         {
738             cclp->error_code = CCL_ERR_RP_EXPECTED;
739             ccl_rpn_delete (p);
740             return NULL;
741         }
742         ADVANCE;
743     }
744     else
745         p = search_terms (cclp, ap);
746     return p;
747 }
748
749 /**
750  * qualifiers1: Parse CCL qualifiers and search terms. 
751  * cclp:   CCL Parser
752  * la:     Token pointer to RELATION token.
753  * qa:     Qualifier attributes already applied.
754  * return: pointer to node(s); NULL on error.
755  */
756 static struct ccl_rpn_node *qualifiers1 (CCL_parser cclp, struct ccl_token *la,
757                                          struct ccl_rpn_attr **qa)
758 {
759     struct ccl_token *lookahead = cclp->look_token;
760     struct ccl_token *look_start = cclp->look_token;
761     struct ccl_rpn_attr **ap;
762     struct ccl_rpn_node *node = 0;
763     const char *field_str;
764     int no = 0;
765     int seq = 0;
766     int i;
767     int mode_merge = 1;
768 #if 0
769     if (qa)
770     {
771         cclp->error_code = CCL_ERR_DOUBLE_QUAL;
772         return NULL;
773     }
774 #endif
775     for (lookahead = cclp->look_token; lookahead != la;
776          lookahead=lookahead->next)
777         no++;
778     if (qa)
779         for (i=0; qa[i]; i++)
780             no++;
781     ap = (struct ccl_rpn_attr **)xmalloc ((no ? (no+1) : 2) * sizeof(*ap));
782     ccl_assert (ap);
783
784     field_str = ccl_qual_search_special(cclp->bibset, "field");
785     if (field_str)
786     {
787         if (!strcmp (field_str, "or"))
788             mode_merge = 0;
789         else if (!strcmp (field_str, "merge"))
790             mode_merge = 1;
791     }
792     if (!mode_merge)
793     {
794         /* consider each field separately and OR */
795         lookahead = look_start;
796         while (lookahead != la)
797         {
798             ap[1] = 0;
799             seq = 0;
800             while ((ap[0] = ccl_qual_search (cclp, lookahead->name,
801                                              lookahead->len, seq)) != 0)
802             {
803                 struct ccl_rpn_node *node_sub;
804                 cclp->look_token = la;
805                 
806                 node_sub = qualifiers2(cclp, ap);
807                 if (!node_sub)
808                 {
809                     ccl_rpn_delete (node);
810                     xfree (ap);
811                     return 0;
812                 }
813                 if (node)
814                 {
815                     struct ccl_rpn_node *node_this = mk_node(CCL_RPN_OR);
816                     node_this->u.p[0] = node;
817                     node_this->u.p[1] = node_sub;
818                     node = node_this;
819                 }
820                 else
821                     node = node_sub;
822                 seq++;
823             }
824             if (seq == 0)
825             {
826                 cclp->look_token = lookahead;
827                 cclp->error_code = CCL_ERR_UNKNOWN_QUAL;
828                 xfree (ap);
829                 return NULL;
830             }
831             lookahead = lookahead->next;
832             if (lookahead->kind == CCL_TOK_COMMA)
833                 lookahead = lookahead->next;
834         }
835     }
836     else
837     {
838         /* merge attributes from ALL fields - including inherited ones */
839         while (1)
840         {
841             struct ccl_rpn_node *node_sub;
842             int found = 0;
843             lookahead = look_start;
844             for (i = 0; lookahead != la; i++)
845             {
846                 ap[i] = ccl_qual_search (cclp, lookahead->name,
847                                          lookahead->len, seq);
848                 if (ap[i])
849                     found++;
850                 if (!ap[i] && seq > 0)
851                     ap[i] = ccl_qual_search (cclp, lookahead->name,
852                                              lookahead->len, 0);
853                 if (!ap[i])
854                 {
855                     cclp->look_token = lookahead;
856                     cclp->error_code = CCL_ERR_UNKNOWN_QUAL;
857                     xfree (ap);
858                     return NULL;
859                 }
860                 lookahead = lookahead->next;
861                 if (lookahead->kind == CCL_TOK_COMMA)
862                     lookahead = lookahead->next;
863             }
864             if (qa)
865             {
866                 struct ccl_rpn_attr **qa0 = qa;
867                 
868                 while (*qa0)
869                     ap[i++] = *qa0++;
870             }
871             ap[i] = NULL;
872             
873             if (!found)
874                 break;
875             
876             cclp->look_token = lookahead;
877             
878             node_sub = qualifiers2(cclp, ap);
879             if (!node_sub)
880             {
881                 ccl_rpn_delete (node);
882                 break;
883             }
884             if (node)
885             {
886                 struct ccl_rpn_node *node_this = mk_node(CCL_RPN_OR);
887                 node_this->u.p[0] = node;
888                 node_this->u.p[1] = node_sub;
889                 node = node_this;
890             }
891             else
892                 node = node_sub;
893             seq++;
894         }
895     }
896     xfree (ap);
897     return node;
898 }
899
900
901 /**
902  * search_terms: Parse CCL search terms - including proximity.
903  * cclp:   CCL Parser
904  * qa:     Qualifier attributes already applied.
905  * return: pointer to node(s); NULL on error.
906  */
907 static struct ccl_rpn_node *search_terms (CCL_parser cclp,
908                                           struct ccl_rpn_attr **qa)
909 {
910     static int list[] = {
911         CCL_TOK_TERM, CCL_TOK_COMMA,CCL_TOK_EQ, CCL_TOK_REL, CCL_TOK_SET, -1};
912     struct ccl_rpn_node *p1, *p2, *pn;
913     p1 = search_term_x (cclp, qa, list, 1);
914     if (!p1)
915         return NULL;
916     while (1)
917     {
918         if (KIND == CCL_TOK_PROX)
919         {
920             struct ccl_rpn_node *p_prox = 0;
921             /* ! word order specified */
922             /* % word order not specified */
923             p_prox = mk_node(CCL_RPN_TERM);
924             p_prox->u.t.term = (char *) xmalloc(1 + cclp->look_token->len);
925             memcpy(p_prox->u.t.term, cclp->look_token->name,
926                    cclp->look_token->len);
927             p_prox->u.t.term[cclp->look_token->len] = 0;
928             p_prox->u.t.attr_list = 0;
929
930             ADVANCE;
931             p2 = search_term_x (cclp, qa, list, 1);
932             if (!p2)
933             {
934                 ccl_rpn_delete (p1);
935                 return NULL;
936             }
937             pn = mk_node (CCL_RPN_PROX);
938             pn->u.p[0] = p1;
939             pn->u.p[1] = p2;
940             pn->u.p[2] = p_prox;
941             p1 = pn;
942         }
943         else if (is_term_ok(KIND, list))
944         {
945             p2 = search_term_x (cclp, qa, list, 1);
946             if (!p2)
947             {
948                 ccl_rpn_delete (p1);
949                 return NULL;
950             }
951             pn = mk_node (CCL_RPN_PROX);
952             pn->u.p[0] = p1;
953             pn->u.p[1] = p2;
954             pn->u.p[2] = 0;
955             p1 = pn;
956         }
957         else
958             break;
959     }
960     return p1;
961 }
962
963 /**
964  * search_elements: Parse CCL search elements
965  * cclp:   CCL Parser
966  * qa:     Qualifier attributes already applied.
967  * return: pointer to node(s); NULL on error.
968  */
969 static struct ccl_rpn_node *search_elements (CCL_parser cclp,
970                                              struct ccl_rpn_attr **qa)
971 {
972     struct ccl_rpn_node *p1;
973     struct ccl_token *lookahead;
974     if (KIND == CCL_TOK_LP)
975     {
976         ADVANCE;
977         p1 = find_spec (cclp, qa);
978         if (!p1)
979             return NULL;
980         if (KIND != CCL_TOK_RP)
981         {
982             cclp->error_code = CCL_ERR_RP_EXPECTED;
983             ccl_rpn_delete (p1);
984             return NULL;
985         }
986         ADVANCE;
987         return p1;
988     }
989     else if (KIND == CCL_TOK_SET)
990     {
991         ADVANCE;
992         if (KIND == CCL_TOK_EQ)
993             ADVANCE;
994         if (KIND != CCL_TOK_TERM)
995         {
996             cclp->error_code = CCL_ERR_SETNAME_EXPECTED;
997             return NULL;
998         }
999         p1 = mk_node (CCL_RPN_SET);
1000         p1->u.setname = copy_token_name (cclp->look_token);
1001         ADVANCE;
1002         return p1;
1003     }
1004     lookahead = cclp->look_token;
1005
1006     while (lookahead->kind==CCL_TOK_TERM)
1007     {
1008         lookahead = lookahead->next;
1009         if (lookahead->kind == CCL_TOK_REL || lookahead->kind == CCL_TOK_EQ)
1010             return qualifiers1 (cclp, lookahead, qa);
1011         if (lookahead->kind != CCL_TOK_COMMA)
1012             break;
1013         lookahead = lookahead->next;
1014     }
1015     if (qa)
1016         return search_terms (cclp, qa);
1017     else
1018     {
1019         struct ccl_rpn_attr *qa[2];
1020         struct ccl_rpn_node *node = 0;
1021         int seq;
1022         lookahead = cclp->look_token;
1023
1024         qa[1] = 0;
1025         for(seq = 0; ;seq++)
1026         {
1027             struct ccl_rpn_node *node_sub;
1028             qa[0] = ccl_qual_search(cclp, "term", 4, seq);
1029             if (!qa[0])
1030                 break;
1031
1032             cclp->look_token = lookahead;
1033
1034             node_sub = search_terms (cclp, qa);
1035             if (!node_sub)
1036             {
1037                 ccl_rpn_delete (node);
1038                 return 0;
1039             }
1040             if (node)
1041             {
1042                 struct ccl_rpn_node *node_this = mk_node(CCL_RPN_OR);
1043                 node_this->u.p[0] = node;
1044                 node_this->u.p[1] = node_sub;
1045                 node_this->u.p[2] = 0;
1046                 node = node_this;
1047             }
1048             else
1049                 node = node_sub;
1050         }
1051         if (!node)
1052             node = search_terms (cclp, 0);
1053         return node;
1054     }
1055 }
1056
1057 /**
1058  * find_spec: Parse CCL find specification
1059  * cclp:   CCL Parser
1060  * qa:     Qualifier attributes already applied.
1061  * return: pointer to node(s); NULL on error.
1062  */
1063 static struct ccl_rpn_node *find_spec (CCL_parser cclp,
1064                                        struct ccl_rpn_attr **qa)
1065 {
1066     struct ccl_rpn_node *p1, *p2, *pn;
1067     if (!(p1 = search_elements (cclp, qa)))
1068         return NULL;
1069     while (1)
1070     {
1071         switch (KIND)
1072         {
1073         case CCL_TOK_AND:
1074             ADVANCE;
1075             p2 = search_elements (cclp, qa);
1076             if (!p2)
1077             {
1078                 ccl_rpn_delete (p1);
1079                 return NULL;
1080             }
1081             pn = mk_node (CCL_RPN_AND);
1082             pn->u.p[0] = p1;
1083             pn->u.p[1] = p2;
1084             pn->u.p[2] = 0;
1085             p1 = pn;
1086             continue;
1087         case CCL_TOK_OR:
1088             ADVANCE;
1089             p2 = search_elements (cclp, qa);
1090             if (!p2)
1091             {
1092                 ccl_rpn_delete (p1);
1093                 return NULL;
1094             }
1095             pn = mk_node (CCL_RPN_OR);
1096             pn->u.p[0] = p1;
1097             pn->u.p[1] = p2;
1098             pn->u.p[2] = 0;
1099             p1 = pn;
1100             continue;
1101         case CCL_TOK_NOT:
1102             ADVANCE;
1103             p2 = search_elements (cclp, qa);
1104             if (!p2)
1105             {
1106                 ccl_rpn_delete (p1);
1107                 return NULL;
1108             }
1109             pn = mk_node (CCL_RPN_NOT);
1110             pn->u.p[0] = p1;
1111             pn->u.p[1] = p2;
1112             pn->u.p[2] = 0;
1113             p1 = pn;
1114             continue;
1115         }
1116         break;
1117     }
1118     return p1;
1119 }
1120
1121 struct ccl_rpn_node *ccl_parser_find_str(CCL_parser cclp, const char *str)
1122 {
1123     struct ccl_rpn_node *p;
1124     struct ccl_token *list = ccl_parser_tokenize(cclp, str);
1125     p = ccl_parser_find_token(cclp, list);
1126     ccl_token_del(list);
1127     return p;
1128 }
1129
1130 struct ccl_rpn_node *ccl_parser_find_token(CCL_parser cclp, 
1131                                            struct ccl_token *list)
1132 {
1133     struct ccl_rpn_node *p;
1134
1135     cclp->look_token = list;
1136     p = find_spec (cclp, NULL);
1137     if (p && KIND != CCL_TOK_EOL)
1138     {
1139         if (KIND == CCL_TOK_RP)
1140             cclp->error_code = CCL_ERR_BAD_RP;
1141         else
1142             cclp->error_code = CCL_ERR_OP_EXPECTED;
1143         ccl_rpn_delete (p);
1144         p = NULL;
1145     }
1146     cclp->error_pos = cclp->look_token->name;
1147     if (p)
1148         cclp->error_code = CCL_ERR_OK;
1149     else
1150         cclp->error_code = cclp->error_code;
1151     return p;
1152 }
1153
1154 /**
1155  * ccl_find_str: Parse CCL find - string representation
1156  * bibset:  Bibset to be used for the parsing
1157  * str:     String to be parsed
1158  * error:   Pointer to integer. Holds error no. on completion.
1159  * pos:     Pointer to char position. Holds approximate error position.
1160  * return:  RPN tree on successful completion; NULL otherwise.
1161  */
1162 struct ccl_rpn_node *ccl_find_str(CCL_bibset bibset, const char *str,
1163                                   int *error, int *pos)
1164 {
1165     CCL_parser cclp = ccl_parser_create (bibset);
1166     struct ccl_token *list;
1167     struct ccl_rpn_node *p;
1168
1169     list = ccl_parser_tokenize (cclp, str);
1170     p = ccl_parser_find_token(cclp, list);
1171
1172     *error = cclp->error_code;
1173     if (*error)
1174         *pos = cclp->error_pos - str;
1175     ccl_parser_destroy (cclp);
1176     ccl_token_del (list);
1177     return p;
1178 }
1179 /*
1180  * Local variables:
1181  * c-basic-offset: 4
1182  * indent-tabs-mode: nil
1183  * End:
1184  * vim: shiftwidth=4 tabstop=8 expandtab
1185  */
1186