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