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