Directive s=pw sets structure to phrase if term includes blank(s).
[yaz-moved-to-github.git] / ccl / 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 /* CCL find (to rpn conversion)
45  * Europagate, 1995
46  *
47  * $Log: cclfind.c,v $
48  * Revision 1.24  2001-03-22 21:23:30  adam
49  * Directive s=pw sets structure to phrase if term includes blank(s).
50  *
51  * Revision 1.23  2001/03/20 11:22:58  adam
52  * CCL Truncation character may be defined.
53  *
54  * Revision 1.22  2001/03/07 13:24:40  adam
55  * Member and_not in Z_Operator is kept for backwards compatibility.
56  * Added support for definition of CCL operators in field spec file.
57  *
58  * Revision 1.21  2001/02/21 13:46:53  adam
59  * C++ fixes.
60  *
61  * Revision 1.20  2000/11/16 13:03:12  adam
62  * Function ccl_rpn_query sets attributeSet to Bib-1.
63  *
64  * Revision 1.19  2000/11/16 09:58:02  adam
65  * Implemented local AttributeSet setting for CCL field maps.
66  *
67  * Revision 1.18  2000/10/17 19:50:28  adam
68  * Implemented and-list and or-list for CCL module.
69  *
70  * Revision 1.17  2000/05/01 09:36:50  adam
71  * Range operator only treated in ordered ranges so that minus (-) can be
72  * used for, say, the and-not operator.
73  *
74  * Revision 1.16  2000/03/14 09:06:11  adam
75  * Added POSIX threads support for frontend server.
76  *
77  * Revision 1.15  2000/02/24 23:49:13  adam
78  * Fixed memory allocation problem.
79  *
80  * Revision 1.14  2000/01/31 13:15:21  adam
81  * Removed uses of assert(3). Cleanup of ODR. CCL parser update so
82  * that some characters are not surrounded by spaces in resulting term.
83  * ILL-code updates.
84  *
85  * Revision 1.13  1999/12/22 13:13:32  adam
86  * Search terms may include "operators" without causing error.
87  *
88  * Revision 1.12  1999/11/30 13:47:11  adam
89  * Improved installation. Moved header files to include/yaz.
90  *
91  * Revision 1.11  1999/03/31 11:15:37  adam
92  * Fixed memory leaks in ccl_find_str and ccl_qual_rm.
93  *
94  * Revision 1.10  1998/02/11 11:53:33  adam
95  * Changed code so that it compiles as C++.
96  *
97  * Revision 1.9  1997/09/29 08:56:37  adam
98  * Changed CCL parser to be thread safe. New type, CCL_parser, declared
99  * and a create/destructers ccl_parser_create/ccl_parser/destory has
100  * been added.
101  *
102  * Revision 1.8  1997/09/01 08:48:11  adam
103  * New windows NT/95 port using MSV5.0. Only a few changes made
104  * to avoid warnings.
105  *
106  * Revision 1.7  1997/05/14 06:53:26  adam
107  * C++ support.
108  *
109  * Revision 1.6  1997/04/30 08:52:06  quinn
110  * Null
111  *
112  * Revision 1.5  1996/10/11  15:00:24  adam
113  * CCL parser from Europagate Email gateway 1.0.
114  *
115  * Revision 1.16  1996/01/08  08:41:13  adam
116  * Removed unused function.
117  *
118  * Revision 1.15  1995/07/20  08:14:34  adam
119  * Qualifiers were observed too often. Instead tokens are treated as
120  * qualifiers only when separated by comma.
121  *
122  * Revision 1.14  1995/05/16  09:39:26  adam
123  * LICENSE.
124  *
125  * Revision 1.13  1995/04/17  09:31:42  adam
126  * Improved handling of qualifiers. Aliases or reserved words.
127  *
128  * Revision 1.12  1995/03/20  15:27:43  adam
129  * Minor changes.
130  *
131  * Revision 1.11  1995/02/23  08:31:59  adam
132  * Changed header.
133  *
134  * Revision 1.9  1995/02/16  13:20:06  adam
135  * Spell fix.
136  *
137  * Revision 1.8  1995/02/14  19:59:42  adam
138  * Removed a syntax error.
139  *
140  * Revision 1.7  1995/02/14  19:55:10  adam
141  * Header files ccl.h/cclp.h are gone! They have been merged an
142  * moved to ../include/ccl.h.
143  * Node kind(s) in ccl_rpn_node have changed names.
144  *
145  * Revision 1.6  1995/02/14  16:20:55  adam
146  * Qualifiers are read from a file now.
147  *
148  * Revision 1.5  1995/02/14  14:12:41  adam
149  * Ranges for ordered qualfiers implemented (e.g. pd=1980-1990).
150  *
151  * Revision 1.4  1995/02/14  13:16:29  adam
152  * Left and/or right truncation implemented.
153  *
154  * Revision 1.3  1995/02/14  10:25:56  adam
155  * The constructions 'qualifier rel term ...' implemented.
156  *
157  * Revision 1.2  1995/02/13  15:15:07  adam
158  * Added handling of qualifiers. Not finished yet.
159  *
160  * Revision 1.1  1995/02/13  12:35:20  adam
161  * First version of CCL. Qualifiers aren't handled yet.
162  *
163  */
164
165 #include <stdlib.h>
166 #include <string.h>
167
168 #include <yaz/ccl.h>
169
170 /* returns type of current lookahead */
171 #define KIND (cclp->look_token->kind)
172
173 /* move one token forward */
174 #define ADVANCE cclp->look_token = cclp->look_token->next
175
176 /* 
177  * qual_val_type: test for existance of attribute type/value pair.
178  * qa:     Attribute array
179  * type:   Type of attribute to search for
180  * value:  Value of attribute to seach for
181  * return: 1 if found; 0 otherwise.
182  */
183 static int qual_val_type (struct ccl_rpn_attr **qa, int type, int value,
184                            char **attset)
185 {
186     int i;
187     struct ccl_rpn_attr *q;
188
189     if (!qa)
190         return 0;
191     for (i = 0;  (q=qa[i]); i++)
192         while (q)
193         {
194             if (q->type == type && q->value == value)
195             {
196                 if (attset)
197                     *attset = q->set;
198                 return 1;
199             }
200             q = q->next;
201         }
202     return 0;
203 }
204
205 /*
206  * strxcat: concatenate strings.
207  * n:      Null-terminated Destination string 
208  * src:    Source string to be appended (not null-terminated)
209  * len:    Length of source string.
210  */
211 static void strxcat (char *n, const char *src, int len)
212 {
213     while (*n)
214         n++;
215     while (--len >= 0)
216         *n++ = *src++;
217     *n = '\0';
218 }
219
220 /*
221  * copy_token_name: Return copy of CCL token name
222  * tp:      Pointer to token info.
223  * return:  malloc(3) allocated copy of token name.
224  */
225 static char *copy_token_name (struct ccl_token *tp)
226 {
227     char *str = (char *)malloc (tp->len + 1);
228     ccl_assert (str);
229     memcpy (str, tp->name, tp->len);
230     str[tp->len] = '\0';
231     return str;
232 }
233
234 /*
235  * mk_node: Create RPN node.
236  * kind:   Type of node.
237  * return: pointer to allocated node.
238  */
239 static struct ccl_rpn_node *mk_node (int kind)
240 {
241     struct ccl_rpn_node *p;
242     p = (struct ccl_rpn_node *)malloc (sizeof(*p));
243     ccl_assert (p);
244     p->kind = kind;
245     return p;
246 }
247
248 /*
249  * ccl_rpn_delete: Delete RPN tree.
250  * rpn:   Pointer to tree.
251  */
252 void ccl_rpn_delete (struct ccl_rpn_node *rpn)
253 {
254     struct ccl_rpn_attr *attr, *attr1;
255     if (!rpn)
256         return;
257     switch (rpn->kind)
258     {
259     case CCL_RPN_AND:
260     case CCL_RPN_OR:
261     case CCL_RPN_NOT:
262         ccl_rpn_delete (rpn->u.p[0]);
263         ccl_rpn_delete (rpn->u.p[1]);
264         break;
265     case CCL_RPN_TERM:
266         free (rpn->u.t.term);
267         for (attr = rpn->u.t.attr_list; attr; attr = attr1)
268         {
269             attr1 = attr->next;
270             if (attr->set)
271                 free (attr->set);
272             free (attr);
273         }
274         break;
275     case CCL_RPN_SET:
276         free (rpn->u.setname);
277         break;
278     case CCL_RPN_PROX:
279         ccl_rpn_delete (rpn->u.p[0]);
280         ccl_rpn_delete (rpn->u.p[1]);
281         break;
282     }
283     free (rpn);
284 }
285
286 static struct ccl_rpn_node *find_spec (CCL_parser cclp,
287                                        struct ccl_rpn_attr **qa);
288
289 static int is_term_ok (int look, int *list)
290 {
291     for (;*list >= 0; list++)
292         if (look == *list)
293             return 1;
294     return 0;
295 }
296
297 static struct ccl_rpn_node *search_terms (CCL_parser cclp,
298                                           struct ccl_rpn_attr **qa);
299
300 /*
301  * add_attr: Add attribute (type/value) to RPN term node.
302  * p:     RPN node of type term.
303  * type:  Type of attribute
304  * value: Value of attribute
305  * set: Attribute set name
306  */
307 static void add_attr (struct ccl_rpn_node *p, const char *set,
308                       int type, int value)
309 {
310     struct ccl_rpn_attr *n;
311
312     n = (struct ccl_rpn_attr *)malloc (sizeof(*n));
313     ccl_assert (n);
314     if (set)
315     {
316         n->set = (char*) malloc (strlen(set)+1);
317         strcpy (n->set, set);
318     }
319     else
320         n->set = 0;
321     n->type = type;
322     n->value = value;
323     n->next = p->u.t.attr_list;
324     p->u.t.attr_list = n;
325 }
326
327 /*
328  * search_term: Parse CCL search term. 
329  * cclp:   CCL Parser
330  * qa:     Qualifier attributes already applied.
331  * return: pointer to node(s); NULL on error.
332  */
333 static struct ccl_rpn_node *search_term_x (CCL_parser cclp,
334                                            struct ccl_rpn_attr **qa,
335                                            int *term_list)
336 {
337     struct ccl_rpn_attr *qa_tmp[2];
338     struct ccl_rpn_node *p_top = 0;
339     struct ccl_token *lookahead = cclp->look_token;
340     int and_list = 0;
341     int or_list = 0;
342     char *attset;
343     const char *truncation_aliases;
344
345     truncation_aliases =
346         ccl_qual_search_special(cclp->bibset, "truncation");
347     if (!truncation_aliases)
348         truncation_aliases = "?";
349
350     if (!qa)
351     {
352         /* no qualifier(s) applied. Use 'term' if it is defined */
353         
354         qa = qa_tmp;
355         ccl_assert (qa);
356         qa[0] = ccl_qual_search (cclp, "term", 4);
357         qa[1] = NULL;
358     }
359     if (qual_val_type (qa, CCL_BIB1_STR, CCL_BIB1_STR_AND_LIST, 0))
360         and_list = 1;
361     if (qual_val_type (qa, CCL_BIB1_STR, CCL_BIB1_STR_OR_LIST, 0))
362         or_list = 1;
363     while (1)
364     {
365         struct ccl_rpn_node *p;
366         size_t no, i;
367         int no_spaces = 0;
368         int left_trunc = 0;
369         int right_trunc = 0;
370         int mid_trunc = 0;
371         int relation_value = -1;
372         int position_value = -1;
373         int structure_value = -1;
374         int truncation_value = -1;
375         int completeness_value = -1;
376         int len = 0;
377         size_t max = 200;
378         if (and_list || or_list)
379             max = 1;
380
381         /* go through each TERM token. If no truncation attribute is yet
382            met, then look for left/right truncation markers (?) and
383            set left_trunc/right_trunc/mid_trunc accordingly */
384         for (no = 0; no < max && is_term_ok(lookahead->kind, term_list); no++)
385         {
386             for (i = 0; i<lookahead->len; i++)
387                 if (lookahead->name[i] == ' ')
388                     no_spaces++;
389                 else if (strchr(truncation_aliases, lookahead->name[i]))
390                 {
391                     if (no == 0 && i == 0 && lookahead->len >= 1)
392                         left_trunc = 1;
393                     else if (!is_term_ok(lookahead->next->kind, term_list) &&
394                              i == lookahead->len-1 && i >= 1)
395                         right_trunc = 1;
396                     else
397                         mid_trunc = 1;
398                 }
399             len += 1+lookahead->len;
400             lookahead = lookahead->next;
401         }
402
403         if (len == 0)
404             break;      /* no more terms . stop . */
405
406         if (p_top)
407         {
408             if (or_list)
409                 p = mk_node (CCL_RPN_OR);
410             else if (and_list)
411                 p = mk_node (CCL_RPN_AND);
412             else
413                 p = mk_node (CCL_RPN_AND);
414             p->u.p[0] = p_top;
415             p_top = p;
416         }
417                 
418         /* create the term node, but wait a moment before adding the term */
419         p = mk_node (CCL_RPN_TERM);
420         p->u.t.attr_list = NULL;
421         p->u.t.term = NULL;
422
423         /* make the top node point to us.. */
424         if (p_top)
425             p_top->u.p[1] = p;
426         else
427             p_top = p;
428
429         
430         /* go through all attributes and add them to the attribute list */
431         for (i=0; qa && qa[i]; i++)
432         {
433             struct ccl_rpn_attr *attr;
434             
435             for (attr = qa[i]; attr; attr = attr->next)
436                 if (attr->value > 0)
437                 {   /* deal only with REAL attributes (positive) */
438                     switch (attr->type)
439                     {
440                     case CCL_BIB1_REL:
441                         if (relation_value != -1)
442                             continue;
443                         relation_value = attr->value;
444                         break;
445                     case CCL_BIB1_POS:
446                         if (position_value != -1)
447                             continue;
448                         position_value = attr->value;
449                         break;
450                     case CCL_BIB1_STR:
451                         if (structure_value != -1)
452                             continue;
453                         structure_value = attr->value;
454                         break;
455                     case CCL_BIB1_TRU:
456                         if (truncation_value != -1)
457                             continue;
458                         truncation_value = attr->value;
459                         left_trunc = right_trunc = mid_trunc = 0;
460                         break;
461                     case CCL_BIB1_COM:
462                         if (completeness_value != -1)
463                             continue;
464                         completeness_value = attr->value;
465                         break;
466                     }
467                     add_attr (p, attr->set, attr->type, attr->value);
468             }
469         }
470         /* len now holds the number of characters in the RPN term */
471         /* no holds the number of CCL tokens (1 or more) */
472         
473         if (structure_value == -1 && 
474             qual_val_type (qa, CCL_BIB1_STR, CCL_BIB1_STR_WP, &attset))
475         {   /* no structure attribute met. Apply either structure attribute 
476                WORD or PHRASE depending on number of CCL tokens */
477             if (no == 1 && no_spaces == 0)
478                 add_attr (p, attset, CCL_BIB1_STR, 2);
479             else
480                 add_attr (p, attset, CCL_BIB1_STR, 1);
481         }
482         
483         /* make the RPN token */
484         p->u.t.term = (char *)malloc (len);
485         ccl_assert (p->u.t.term);
486         p->u.t.term[0] = '\0';
487         for (i = 0; i<no; i++)
488         {
489             const char *src_str = cclp->look_token->name;
490             int src_len = cclp->look_token->len;
491             
492             if (i == 0 && left_trunc)
493             {
494                 src_len--;
495                 src_str++;
496             }
497             else if (i == no-1 && right_trunc)
498                 src_len--;
499             if (src_len)
500             {
501                 int len = strlen(p->u.t.term);
502                 if (len &&
503                     !strchr("-+", *src_str) &&
504                     !strchr("-+", p->u.t.term[len-1]))
505                 {
506                     strcat (p->u.t.term, " ");
507                 }
508             }
509             strxcat (p->u.t.term, src_str, src_len);
510             ADVANCE;
511         }
512         if (left_trunc && right_trunc)
513         {
514             if (!qual_val_type (qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_BOTH,
515                                 &attset))
516             {
517                 cclp->error_code = CCL_ERR_TRUNC_NOT_BOTH;
518                 ccl_rpn_delete (p);
519                 return NULL;
520             }
521             add_attr (p, attset, CCL_BIB1_TRU, 3);
522         }
523         else if (right_trunc)
524         {
525             if (!qual_val_type (qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_RIGHT,
526                                  &attset))
527             {
528                 cclp->error_code = CCL_ERR_TRUNC_NOT_RIGHT;
529                 ccl_rpn_delete (p);
530                 return NULL;
531             }
532             add_attr (p, attset, CCL_BIB1_TRU, 1);
533         }
534         else if (left_trunc)
535         {
536             if (!qual_val_type (qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_LEFT,
537                                 &attset))
538             {
539                 cclp->error_code = CCL_ERR_TRUNC_NOT_LEFT;
540                 ccl_rpn_delete (p);
541                 return NULL;
542             }
543             add_attr (p, attset, CCL_BIB1_TRU, 2);
544         }
545         else
546         {
547             if (qual_val_type (qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_NONE,
548                                &attset))
549                 add_attr (p, attset, CCL_BIB1_TRU, 100);
550         }
551     }
552     if (!p_top)
553         cclp->error_code = CCL_ERR_TERM_EXPECTED;
554     return p_top;
555 }
556
557 static struct ccl_rpn_node *search_term (CCL_parser cclp,
558                                          struct ccl_rpn_attr **qa)
559 {
560     static int list[] = {CCL_TOK_TERM, CCL_TOK_COMMA, -1};
561     return search_term_x(cclp, qa, list);
562 }
563
564 /*
565  * qualifiers: Parse CCL qualifiers and search terms. 
566  * cclp:   CCL Parser
567  * la:     Token pointer to RELATION token.
568  * qa:     Qualifier attributes already applied.
569  * return: pointer to node(s); NULL on error.
570  */
571 static struct ccl_rpn_node *qualifiers (CCL_parser cclp, struct ccl_token *la,
572                                         struct ccl_rpn_attr **qa)
573 {
574     struct ccl_token *lookahead = cclp->look_token;
575     struct ccl_rpn_attr **ap;
576     int no = 0;
577     int i, rel;
578     char *attset;
579 #if 0
580     if (qa)
581     {
582         cclp->error_code = CCL_ERR_DOUBLE_QUAL;
583         return NULL;
584     }
585 #endif
586     for (lookahead = cclp->look_token; lookahead != la;
587          lookahead=lookahead->next)
588         no++;
589     if (qa)
590         for (i=0; qa[i]; i++)
591             no++;
592     ap = (struct ccl_rpn_attr **)malloc ((no+1) * sizeof(*ap));
593     ccl_assert (ap);
594     for (i = 0; cclp->look_token != la; i++)
595     {
596         ap[i] = ccl_qual_search (cclp, cclp->look_token->name,
597                                  cclp->look_token->len);
598         if (!ap[i])
599         {
600             cclp->error_code = CCL_ERR_UNKNOWN_QUAL;
601             free (ap);
602             return NULL;
603         }
604         ADVANCE;
605         if (KIND == CCL_TOK_COMMA)
606             ADVANCE;
607     }
608     if (qa)
609         while (*qa)
610             ap[i++] = *qa++;
611     ap[i] = NULL;
612     if (!qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_ORDER, &attset))
613     {                
614         /* unordered relation */
615         struct ccl_rpn_node *p;
616         if (KIND != CCL_TOK_EQ)
617         {
618             cclp->error_code = CCL_ERR_EQ_EXPECTED;
619             free (ap);
620             return NULL;
621         }
622         ADVANCE;
623         if (KIND == CCL_TOK_LP)
624         {
625             ADVANCE;
626             if (!(p = find_spec (cclp, ap)))
627             {
628                 free (ap);
629                 return NULL;
630             }
631             if (KIND != CCL_TOK_RP)
632             {
633                 cclp->error_code = CCL_ERR_RP_EXPECTED;
634                 ccl_rpn_delete (p);
635                 free (ap);
636                 return NULL;
637             }
638             ADVANCE;
639         }
640         else
641             p = search_terms (cclp, ap);
642         free (ap);
643         return p;
644     }
645     /* ordered relation ... */
646     rel = 0;
647     if (cclp->look_token->len == 1)
648     {
649         if (cclp->look_token->name[0] == '<')
650             rel = 1;
651         else if (cclp->look_token->name[0] == '=')
652             rel = 3;
653         else if (cclp->look_token->name[0] == '>')
654             rel = 5;
655     }
656     else if (cclp->look_token->len == 2)
657     {
658         if (!memcmp (cclp->look_token->name, "<=", 2))
659             rel = 2;
660         else if (!memcmp (cclp->look_token->name, ">=", 2))
661             rel = 4;
662         else if (!memcmp (cclp->look_token->name, "<>", 2))
663             rel = 6;
664     }
665     if (!rel)
666         cclp->error_code = CCL_ERR_BAD_RELATION;
667     else
668     {
669         struct ccl_rpn_node *p;
670
671         ADVANCE;                      /* skip relation */
672         if (KIND == CCL_TOK_TERM &&
673             cclp->look_token->next->len == 1 &&
674             cclp->look_token->next->name[0] == '-')
675         {
676             struct ccl_rpn_node *p1;
677             if (!(p1 = search_term (cclp, ap)))
678             {
679                 free (ap);
680                 return NULL;
681             }
682             ADVANCE;                   /* skip '-' */
683             if (KIND == CCL_TOK_TERM)  /* = term - term  ? */
684             {
685                 struct ccl_rpn_node *p2;
686                 
687                 if (!(p2 = search_term (cclp, ap)))
688                 {
689                     ccl_rpn_delete (p1);
690                     free (ap);
691                     return NULL;
692                 }
693                 p = mk_node (CCL_RPN_AND);
694                 p->u.p[0] = p1;
695                 add_attr (p1, attset, CCL_BIB1_REL, 4);
696                 p->u.p[1] = p2;
697                 add_attr (p2, attset, CCL_BIB1_REL, 2);
698                 free (ap);
699                 return p;
700             }
701             else                       /* = term -    */
702             {
703                 add_attr (p1, attset, CCL_BIB1_REL, 4);
704                 free (ap);
705                 return p1;
706             }
707         }
708         else if (cclp->look_token->len == 1 &&
709                  cclp->look_token->name[0] == '"')   /* = - term  ? */
710         {
711             ADVANCE;
712             if (!(p = search_term (cclp, ap)))
713             {
714                 free (ap);
715                 return NULL;
716             }
717             add_attr (p, attset, CCL_BIB1_REL, 2);
718             free (ap);
719             return p;
720         }
721         else if (KIND == CCL_TOK_LP)
722         {
723             ADVANCE;
724             if (!(p = find_spec (cclp, ap)))
725             {
726                 free (ap);
727                 return NULL;
728             }
729             if (KIND != CCL_TOK_RP)
730             {
731                 cclp->error_code = CCL_ERR_RP_EXPECTED;
732                 ccl_rpn_delete (p);
733                 free (ap);
734                 return NULL;
735             }
736             ADVANCE;
737             free (ap);
738             return p;
739         }
740         else
741         {
742             if (!(p = search_terms (cclp, ap)))
743             {
744                 free (ap);
745                 return NULL;
746             }
747             add_attr (p, attset, CCL_BIB1_REL, rel);
748             free (ap);
749             return p;
750         }
751         cclp->error_code = CCL_ERR_TERM_EXPECTED;
752     }
753     free (ap);
754     return NULL;
755 }
756
757 /*
758  * search_terms: Parse CCL search terms - including proximity.
759  * cclp:   CCL Parser
760  * qa:     Qualifier attributes already applied.
761  * return: pointer to node(s); NULL on error.
762  */
763 static struct ccl_rpn_node *search_terms (CCL_parser cclp,
764                                           struct ccl_rpn_attr **qa)
765 {
766     static int list[] = {
767         CCL_TOK_TERM, CCL_TOK_COMMA,CCL_TOK_EQ, CCL_TOK_REL, -1};
768     struct ccl_rpn_node *p1, *p2, *pn;
769     p1 = search_term_x (cclp, qa, list);
770     if (!p1)
771         return NULL;
772     while (1)
773     {
774         if (KIND == CCL_TOK_PROX)
775         {
776             ADVANCE;
777             p2 = search_term_x (cclp, qa, list);
778             if (!p2)
779             {
780                 ccl_rpn_delete (p1);
781                 return NULL;
782             }
783             pn = mk_node (CCL_RPN_PROX);
784             pn->u.p[0] = p1;
785             pn->u.p[1] = p2;
786             p1 = pn;
787         }
788         else if (is_term_ok(KIND, list))
789         {
790             p2 = search_term_x (cclp, qa, list);
791             if (!p2)
792             {
793                 ccl_rpn_delete (p1);
794                 return NULL;
795             }
796             pn = mk_node (CCL_RPN_PROX);
797             pn->u.p[0] = p1;
798             pn->u.p[1] = p2;
799             p1 = pn;
800         }
801         else
802             break;
803     }
804     return p1;
805 }
806
807 /*
808  * search_elements: Parse CCL search elements
809  * cclp:   CCL Parser
810  * qa:     Qualifier attributes already applied.
811  * return: pointer to node(s); NULL on error.
812  */
813 static struct ccl_rpn_node *search_elements (CCL_parser cclp,
814                                              struct ccl_rpn_attr **qa)
815 {
816     struct ccl_rpn_node *p1;
817     struct ccl_token *lookahead;
818     if (KIND == CCL_TOK_LP)
819     {
820         ADVANCE;
821         p1 = find_spec (cclp, qa);
822         if (!p1)
823             return NULL;
824         if (KIND != CCL_TOK_RP)
825         {
826             cclp->error_code = CCL_ERR_RP_EXPECTED;
827             ccl_rpn_delete (p1);
828             return NULL;
829         }
830         ADVANCE;
831         return p1;
832     }
833     else if (KIND == CCL_TOK_SET)
834     {
835         ADVANCE;
836         if (KIND == CCL_TOK_EQ)
837             ADVANCE;
838         if (KIND != CCL_TOK_TERM)
839         {
840             cclp->error_code = CCL_ERR_SETNAME_EXPECTED;
841             return NULL;
842         }
843         p1 = mk_node (CCL_RPN_SET);
844         p1->u.setname = copy_token_name (cclp->look_token);
845         ADVANCE;
846         return p1;
847     }
848     lookahead = cclp->look_token;
849
850     while (lookahead->kind==CCL_TOK_TERM)
851     {
852         lookahead = lookahead->next;
853         if (lookahead->kind == CCL_TOK_REL || lookahead->kind == CCL_TOK_EQ)
854             return qualifiers (cclp, lookahead, qa);
855         if (lookahead->kind != CCL_TOK_COMMA)
856             break;
857         lookahead = lookahead->next;
858     }
859     return search_terms (cclp, qa);
860 }
861
862 /*
863  * find_spec: Parse CCL find specification
864  * cclp:   CCL Parser
865  * qa:     Qualifier attributes already applied.
866  * return: pointer to node(s); NULL on error.
867  */
868 static struct ccl_rpn_node *find_spec (CCL_parser cclp,
869                                        struct ccl_rpn_attr **qa)
870 {
871     struct ccl_rpn_node *p1, *p2, *pn;
872     if (!(p1 = search_elements (cclp, qa)))
873         return NULL;
874     while (1)
875     {
876         switch (KIND)
877         {
878         case CCL_TOK_AND:
879             ADVANCE;
880             p2 = search_elements (cclp, qa);
881             if (!p2)
882             {
883                 ccl_rpn_delete (p1);
884                 return NULL;
885             }
886             pn = mk_node (CCL_RPN_AND);
887             pn->u.p[0] = p1;
888             pn->u.p[1] = p2;
889             p1 = pn;
890             continue;
891         case CCL_TOK_OR:
892             ADVANCE;
893             p2 = search_elements (cclp, qa);
894             if (!p2)
895             {
896                 ccl_rpn_delete (p1);
897                 return NULL;
898             }
899             pn = mk_node (CCL_RPN_OR);
900             pn->u.p[0] = p1;
901             pn->u.p[1] = p2;
902             p1 = pn;
903             continue;
904         case CCL_TOK_NOT:
905             ADVANCE;
906             p2 = search_elements (cclp, qa);
907             if (!p2)
908             {
909                 ccl_rpn_delete (p1);
910                 return NULL;
911             }
912             pn = mk_node (CCL_RPN_NOT);
913             pn->u.p[0] = p1;
914             pn->u.p[1] = p2;
915             p1 = pn;
916             continue;
917         }
918         break;
919     }
920     return p1;
921 }
922
923 struct ccl_rpn_node *ccl_parser_find (CCL_parser cclp, struct ccl_token *list)
924 {
925     struct ccl_rpn_node *p;
926
927     
928
929     cclp->look_token = list;
930     p = find_spec (cclp, NULL);
931     if (p && KIND != CCL_TOK_EOL)
932     {
933         if (KIND == CCL_TOK_RP)
934             cclp->error_code = CCL_ERR_BAD_RP;
935         else
936             cclp->error_code = CCL_ERR_OP_EXPECTED;
937         ccl_rpn_delete (p);
938         p = NULL;
939     }
940     cclp->error_pos = cclp->look_token->name;
941     if (p)
942         cclp->error_code = CCL_ERR_OK;
943     else
944         cclp->error_code = cclp->error_code;
945     return p;
946 }
947
948 /*
949  * ccl_find: Parse CCL find - token representation
950  * bibset:  Bibset to be used for the parsing
951  * list:    List of tokens
952  * error:   Pointer to integer. Holds error no. on completion.
953  * pos:     Pointer to char position. Holds approximate error position.
954  * return:  RPN tree on successful completion; NULL otherwise.
955  */
956 struct ccl_rpn_node *ccl_find (CCL_bibset bibset, struct ccl_token *list,
957                                int *error, const char **pos)
958 {
959     struct ccl_rpn_node *p;
960     CCL_parser cclp = ccl_parser_create ();
961
962     cclp->bibset = bibset;
963
964     p = ccl_parser_find (cclp, list);
965
966     *error = cclp->error_code;
967     *pos = cclp->error_pos;
968
969     ccl_parser_destroy (cclp);
970
971     return p;
972 }
973
974 /*
975  * ccl_find_str: Parse CCL find - string representation
976  * bibset:  Bibset to be used for the parsing
977  * str:     String to be parsed
978  * error:   Pointer to integer. Holds error no. on completion.
979  * pos:     Pointer to char position. Holds approximate error position.
980  * return:  RPN tree on successful completion; NULL otherwise.
981  */
982 struct ccl_rpn_node *ccl_find_str (CCL_bibset bibset, const char *str,
983                                    int *error, int *pos)
984 {
985     CCL_parser cclp = ccl_parser_create ();
986     struct ccl_token *list;
987     struct ccl_rpn_node *p;
988
989     cclp->bibset = bibset;
990
991     list = ccl_parser_tokenize (cclp, str);
992     p = ccl_parser_find (cclp, list);
993
994     *error = cclp->error_code;
995     if (*error)
996         *pos = cclp->error_pos - str;
997     ccl_parser_destroy (cclp);
998     ccl_token_del (list);
999     return p;
1000 }