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