Qualifiers were observed too often. Instead tokens are treated as
[egate.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.15  1995/07/20 08:14:34  adam
49  * Qualifiers were observed too often. Instead tokens are treated as
50  * qualifiers only when separated by comma.
51  *
52  * Revision 1.14  1995/05/16  09:39:26  adam
53  * LICENSE.
54  *
55  * Revision 1.13  1995/04/17  09:31:42  adam
56  * Improved handling of qualifiers. Aliases or reserved words.
57  *
58  * Revision 1.12  1995/03/20  15:27:43  adam
59  * Minor changes.
60  *
61  * Revision 1.11  1995/02/23  08:31:59  adam
62  * Changed header.
63  *
64  * Revision 1.9  1995/02/16  13:20:06  adam
65  * Spell fix.
66  *
67  * Revision 1.8  1995/02/14  19:59:42  adam
68  * Removed a syntax error.
69  *
70  * Revision 1.7  1995/02/14  19:55:10  adam
71  * Header files ccl.h/cclp.h are gone! They have been merged an
72  * moved to ../include/ccl.h.
73  * Node kind(s) in ccl_rpn_node have changed names.
74  *
75  * Revision 1.6  1995/02/14  16:20:55  adam
76  * Qualifiers are read from a file now.
77  *
78  * Revision 1.5  1995/02/14  14:12:41  adam
79  * Ranges for ordered qualfiers implemented (e.g. pd=1980-1990).
80  *
81  * Revision 1.4  1995/02/14  13:16:29  adam
82  * Left and/or right truncation implemented.
83  *
84  * Revision 1.3  1995/02/14  10:25:56  adam
85  * The constructions 'qualifier rel term ...' implemented.
86  *
87  * Revision 1.2  1995/02/13  15:15:07  adam
88  * Added handling of qualifiers. Not finished yet.
89  *
90  * Revision 1.1  1995/02/13  12:35:20  adam
91  * First version of CCL. Qualifiers aren't handled yet.
92  *
93  */
94
95 #include <stdio.h>
96 #include <stdlib.h>
97 #include <assert.h>
98 #include <string.h>
99
100 #include <ccl.h>
101
102 /* current lookahead token */
103 static struct ccl_token *look_token;
104
105 /* holds error no if error occur */
106 static int ccl_error;
107
108 /* current bibset */
109 static CCL_bibset bibset;
110
111 /* returns type of current lookahead */
112 #define KIND (look_token->kind)
113
114 /* move one token forward */
115 #define ADVANCE look_token = look_token->next
116
117 /* 
118  * qual_val_range: search for attribute of type with range
119  * qa:     Attribute array
120  * type:   Type of attribute to search for
121  * vmin:   Lower bound of value to search for
122  * vmax:   Upper bound of value to search for
123  * return: Return pointer to integer of attribute value found; NULL
124  *         otherwise.
125  */
126 static int *qual_val_range (struct ccl_rpn_attr **qa, int type,
127                             int vmin, int vmax)
128 {
129     int i;
130     struct ccl_rpn_attr *q;
131
132     if (!qa)
133         return NULL;
134     for (i = 0; (q=qa[i]); i++)
135         while (q)
136         {
137             if (q->type == type && q->value >= vmin && q->value <= vmax)
138                 return &q->value;
139             q = q->next;
140         }
141     return NULL;
142 }
143
144 /* 
145  * qual_val_type: test for existance of attribute type/value pair.
146  * qa:     Attribute array
147  * type:   Type of attribute to search for
148  * value:  Value of attribute to seach for
149  * return: 1 if found; 0 otherwise.
150  */
151 static int qual_val_type (struct ccl_rpn_attr **qa, int type, int value)
152 {
153     int i;
154     struct ccl_rpn_attr *q;
155
156     if (!qa)
157         return 0;
158     for (i = 0;  (q=qa[i]); i++)
159         while (q)
160         {
161             if (q->type == type && q->value == value)
162                 return 1;
163             q = q->next;
164         }
165     return 0;
166 }
167
168 /*
169  * strxcat: concatenate strings.
170  * n:      Null-terminated Destination string 
171  * src:    Source string to be appended (not null-terminated)
172  * len:    Length of source string.
173  */
174 static void strxcat (char *n, const char *src, int len)
175 {
176     while (*n)
177         n++;
178     while (--len >= 0)
179         *n++ = *src++;
180     *n = '\0';
181 }
182
183 /*
184  * copy_token_name: Return copy of CCL token name
185  * tp:      Pointer to token info.
186  * return:  malloc(3) allocated copy of token name.
187  */
188 static char *copy_token_name (struct ccl_token *tp)
189 {
190     char *str = malloc (tp->len + 1);
191     assert (str);
192     memcpy (str, tp->name, tp->len);
193     str[tp->len] = '\0';
194     return str;
195 }
196
197 /*
198  * mk_node: Create RPN node.
199  * kind:   Type of node.
200  * return: pointer to allocated node.
201  */
202 static struct ccl_rpn_node *mk_node (enum rpn_node_kind kind)
203 {
204     struct ccl_rpn_node *p;
205     p = malloc (sizeof(*p));
206     assert (p);
207     p->kind = kind;
208     return p;
209 }
210
211 /*
212  * ccl_rpn_delete: Delete RPN tree.
213  * rpn:   Pointer to tree.
214  */
215 void ccl_rpn_delete (struct ccl_rpn_node *rpn)
216 {
217     struct ccl_rpn_attr *attr, *attr1;
218     if (!rpn)
219         return;
220     switch (rpn->kind)
221     {
222     case CCL_RPN_AND:
223     case CCL_RPN_OR:
224     case CCL_RPN_NOT:
225         ccl_rpn_delete (rpn->u.p[0]);
226         ccl_rpn_delete (rpn->u.p[1]);
227         break;
228     case CCL_RPN_TERM:
229         free (rpn->u.t.term);
230         for (attr = rpn->u.t.attr_list; attr; attr = attr1)
231         {
232             attr1 = attr->next;
233             free (attr);
234         }
235         break;
236     case CCL_RPN_SET:
237         free (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         break;
243     }
244     free (rpn);
245 }
246
247 static struct ccl_rpn_node *find_spec (struct ccl_rpn_attr **qa);
248 static struct ccl_rpn_node *search_terms (struct ccl_rpn_attr **qa);
249
250 /*
251  * add_attr: Add attribute (type/value) to RPN term node.
252  * p:     RPN node of type term.
253  * type:  Type of attribute
254  * value: Value of attribute
255  */
256 static void add_attr (struct ccl_rpn_node *p, int type, int value)
257 {
258     struct ccl_rpn_attr *n;
259
260     n = malloc (sizeof(*n));
261     assert (n);
262     n->type = type;
263     n->value = value;
264     n->next = p->u.t.attr_list;
265     p->u.t.attr_list = n;
266 }
267
268 /*
269  * search_term: Parse CCL search term. 
270  * qa:     Qualifier attributes already applied.
271  * return: pointer to node(s); NULL on error.
272  */
273 static struct ccl_rpn_node *search_term (struct ccl_rpn_attr **qa)
274 {
275     struct ccl_rpn_node *p;
276     struct ccl_token *lookahead = look_token;
277     int len = 0;
278     int no, i;
279     int left_trunc = 0;
280     int right_trunc = 0;
281     int mid_trunc = 0;
282     int relation_value = -1;
283     int position_value = -1;
284     int structure_value = -1;
285     int truncation_value = -1;
286     int completeness_value = -1;
287
288     if (KIND != CCL_TOK_TERM)
289     {
290         ccl_error = CCL_ERR_TERM_EXPECTED;
291         return NULL;
292     }
293     /* create the term node, but wait a moment before adding the term */
294     p = mk_node (CCL_RPN_TERM);
295     p->u.t.attr_list = NULL;
296     p->u.t.term = NULL;
297
298     if (!qa)
299     {
300         /* no qualifier(s) applied. Use 'term' if it is defined */
301
302         qa = malloc (2*sizeof(*qa));
303         assert (qa);
304         qa[0] = ccl_qual_search (bibset, "term", 4);
305         qa[1] = NULL;
306     }
307
308     /* go through all attributes and add them to the attribute list */
309     for (i=0; qa && qa[i]; i++)
310     {
311         struct ccl_rpn_attr *attr;
312
313         for (attr = qa[i]; attr; attr = attr->next)
314             if (attr->value > 0)
315             {   /* deal only with REAL attributes (positive) */
316                 switch (attr->type)
317                 {
318                 case CCL_BIB1_REL:
319                     if (relation_value != -1)
320                         continue;
321                     relation_value = attr->value;
322                     break;
323                 case CCL_BIB1_POS:
324                     if (position_value != -1)
325                         continue;
326                     position_value = attr->value;
327                     break;
328                 case CCL_BIB1_STR:
329                     if (structure_value != -1)
330                         continue;
331                     structure_value = attr->value;
332                     break;
333                 case CCL_BIB1_TRU:
334                     if (truncation_value != -1)
335                         continue;
336                     truncation_value = attr->value;
337                     break;
338                 case CCL_BIB1_COM:
339                     if (completeness_value != -1)
340                         continue;
341                     completeness_value = attr->value;
342                     break;
343                 }
344                 add_attr (p, attr->type, attr->value);
345             }
346     }
347     /* go through each TERM token. If no truncation attribute is yet
348        met, then look for left/right truncation markers (?) and
349        set left_trunc/right_trunc/mid_trunc accordingly */
350     for (no = 0; lookahead->kind == CCL_TOK_TERM; no++)
351     {
352         for (i = 0; i<lookahead->len; i++)
353             if (truncation_value == -1 && lookahead->name[i] == '?')
354             {
355                 if (no == 0 && i == 0 && lookahead->len >= 1)
356                     left_trunc = 1;
357                 else if (lookahead->next->kind != CCL_TOK_TERM &&
358                          i == lookahead->len-1 && i >= 1)
359                     right_trunc = 1;
360                 else
361                     mid_trunc = 1;
362             }
363         len += 1+lookahead->len;
364         lookahead = lookahead->next;
365     }
366     /* len now holds the number of characters in the RPN term */
367     /* no holds the number of CCL tokens (1 or more) */
368
369     if (structure_value == -1 && 
370         qual_val_type (qa, CCL_BIB1_STR, CCL_BIB1_STR_WP))
371     {   /* no structure attribute met. Apply either structure attribute 
372            WORD or PHRASE depending on number of CCL tokens */
373         if (no == 1)
374             add_attr (p, CCL_BIB1_STR, 2);
375         else
376             add_attr (p, CCL_BIB1_STR, 1);
377     }
378
379     /* make the RPN token */
380     p->u.t.term = malloc (len);
381     assert (p->u.t.term);
382     p->u.t.term[0] = '\0';
383     for (i = 0; i<no; i++)
384     {
385         const char *src_str = look_token->name;
386         int src_len = look_token->len;
387         
388         if (i == 0 && left_trunc)
389         {
390             src_len--;
391             src_str++;
392         }
393         else if (i == no-1 && right_trunc)
394             src_len--;
395         if (i)
396             strcat (p->u.t.term, " ");
397         strxcat (p->u.t.term, src_str, src_len);
398         ADVANCE;
399     }
400     if (left_trunc && right_trunc)
401     {
402         if (!qual_val_type (qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_BOTH))
403         {
404             ccl_error = CCL_ERR_TRUNC_NOT_BOTH;
405             free (qa);
406             ccl_rpn_delete (p);
407             return NULL;
408         }
409         add_attr (p, CCL_BIB1_TRU, 3);
410     }
411     else if (right_trunc)
412     {
413         if (!qual_val_type (qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_RIGHT))
414         {
415             ccl_error = CCL_ERR_TRUNC_NOT_RIGHT;
416             free (qa);
417             ccl_rpn_delete (p);
418             return NULL;
419         }
420         add_attr (p, CCL_BIB1_TRU, 1);
421     }
422     else if (left_trunc)
423     {
424         if (!qual_val_type (qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_LEFT))
425         {
426             ccl_error = CCL_ERR_TRUNC_NOT_LEFT;
427             free (qa);
428             ccl_rpn_delete (p);
429             return NULL;
430         }
431         add_attr (p, CCL_BIB1_TRU, 2);
432     }
433     else
434     {
435         if (qual_val_type (qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_NONE))
436             add_attr (p, CCL_BIB1_TRU, 100);
437     }
438     return p;
439 }
440
441 /*
442  * qualifiers: Parse CCL qualifiers and search terms. 
443  * la:     Token pointer to RELATION token.
444  * qa:     Qualifier attributes already applied.
445  * return: pointer to node(s); NULL on error.
446  */
447 static struct ccl_rpn_node *qualifiers (struct ccl_token *la,
448                                         struct ccl_rpn_attr **qa)
449 {
450     struct ccl_token *lookahead = look_token;
451     struct ccl_rpn_attr **ap;
452     int no = 0;
453     int i, rel;
454 #if 0
455     if (qa)
456     {
457         ccl_error = CCL_ERR_DOUBLE_QUAL;
458         return NULL;
459     }
460 #endif
461     for (lookahead = look_token; lookahead != la; lookahead=lookahead->next)
462         no++;
463     if (qa)
464         for (i=0; qa[i]; i++)
465             no++;
466     ap = malloc ((no+1) * sizeof(*ap));
467     assert (ap);
468     for (i = 0; look_token != la; i++)
469     {
470         ap[i] = ccl_qual_search (bibset, look_token->name, look_token->len);
471         if (!ap[i])
472         {
473             ccl_error = CCL_ERR_UNKNOWN_QUAL;
474             free (ap);
475             return NULL;
476         }
477         ADVANCE;
478         if (KIND == CCL_TOK_COMMA)
479             ADVANCE;
480     }
481     if (qa)
482         while (*qa)
483             ap[i++] = *qa++;
484     ap[i] = NULL;
485     if (!qual_val_type (ap, CCL_BIB1_REL, CCL_BIB1_REL_ORDER))
486     {                
487         /* unordered relation */
488         struct ccl_rpn_node *p;
489         if (KIND != CCL_TOK_EQ)
490         {
491             ccl_error = CCL_ERR_EQ_EXPECTED;
492             free (ap);
493             return NULL;
494         }
495         ADVANCE;
496         if (KIND == CCL_TOK_LP)
497         {
498             ADVANCE;
499             if (!(p = find_spec (ap)))
500             {
501                 free (ap);
502                 return NULL;
503             }
504             if (KIND != CCL_TOK_RP)
505             {
506                 ccl_error = CCL_ERR_RP_EXPECTED;
507                 ccl_rpn_delete (p);
508                 free (ap);
509                 return NULL;
510             }
511             ADVANCE;
512         }
513         else
514             p = search_terms (ap);
515         free (ap);
516         return p;
517     }
518     rel = 0;
519     if (look_token->len == 1)
520     {
521         if (look_token->name[0] == '<')
522             rel = 1;
523         else if (look_token->name[0] == '=')
524             rel = 3;
525         else if (look_token->name[0] == '>')
526             rel = 5;
527     }
528     else if (look_token->len == 2)
529     {
530         if (!memcmp (look_token->name, "<=", 2))
531             rel = 2;
532         else if (!memcmp (look_token->name, ">=", 2))
533             rel = 4;
534         else if (!memcmp (look_token->name, "<>", 2))
535             rel = 6;
536     }
537     if (!rel)
538         ccl_error = CCL_ERR_BAD_RELATION;
539     else
540     {
541         struct ccl_rpn_node *p;
542
543         ADVANCE;                      /* skip relation */
544         if (KIND == CCL_TOK_TERM && look_token->next->kind == CCL_TOK_MINUS)
545         {
546             struct ccl_rpn_node *p1;
547             if (!(p1 = search_term (ap)))
548             {
549                 free (ap);
550                 return NULL;
551             }
552             ADVANCE;                   /* skip '-' */
553             if (KIND == CCL_TOK_TERM)  /* = term - term  ? */
554             {
555                 struct ccl_rpn_node *p2;
556                 
557                 if (!(p2 = search_term (ap)))
558                 {
559                     ccl_rpn_delete (p1);
560                     free (ap);
561                     return NULL;
562                 }
563                 p = mk_node (CCL_RPN_AND);
564                 p->u.p[0] = p1;
565                 add_attr (p1, CCL_BIB1_REL, 4);
566                 p->u.p[1] = p2;
567                 add_attr (p2, CCL_BIB1_REL, 2);
568                 free (ap);
569                 return p;
570             }
571             else                       /* = term -    */
572             {
573                 add_attr (p1, CCL_BIB1_REL, 4);
574                 free (ap);
575                 return p1;
576             }
577         }
578         else if (KIND == CCL_TOK_MINUS)   /* = - term  ? */
579         {
580             ADVANCE;
581             if (!(p = search_term (ap)))
582             {
583                 free (ap);
584                 return NULL;
585             }
586             add_attr (p, CCL_BIB1_REL, 2);
587             free (ap);
588             return p;
589         }
590         else if (KIND == CCL_TOK_LP)
591         {
592             ADVANCE;
593             if (!(p = find_spec (ap)))
594             {
595                 free (ap);
596                 return NULL;
597             }
598             if (KIND != CCL_TOK_RP)
599             {
600                 ccl_error = CCL_ERR_RP_EXPECTED;
601                 ccl_rpn_delete (p);
602                 free (ap);
603                 return NULL;
604             }
605             ADVANCE;
606             free (ap);
607             return p;
608         }
609         else
610         {
611             if (!(p = search_terms (ap)))
612             {
613                 free (ap);
614                 return NULL;
615             }
616             add_attr (p, CCL_BIB1_REL, rel);
617             free (ap);
618             return p;
619         }
620         ccl_error = CCL_ERR_TERM_EXPECTED;
621     }
622     free (ap);
623     return NULL;
624 }
625
626 /*
627  * search_terms: Parse CCL search terms - including proximity.
628  * qa:     Qualifier attributes already applied.
629  * return: pointer to node(s); NULL on error.
630  */
631 static struct ccl_rpn_node *search_terms (struct ccl_rpn_attr **qa)
632 {
633     struct ccl_rpn_node *p1, *p2, *pn;
634     p1 = search_term (qa);
635     if (!p1)
636         return NULL;
637     while (1)
638     {
639         if (KIND == CCL_TOK_PROX)
640         {
641             ADVANCE;
642             p2 = search_term (qa);
643             if (!p2)
644             {
645                 ccl_rpn_delete (p1);
646                 return NULL;
647             }
648             pn = mk_node (CCL_RPN_PROX);
649             pn->u.p[0] = p1;
650             pn->u.p[1] = p2;
651             p1 = pn;
652         }
653         else if (KIND == CCL_TOK_TERM)
654         {
655             p2 = search_term (qa);
656             if (!p2)
657             {
658                 ccl_rpn_delete (p1);
659                 return NULL;
660             }
661             pn = mk_node (CCL_RPN_PROX);
662             pn->u.p[0] = p1;
663             pn->u.p[1] = p2;
664             p1 = pn;
665         }
666         else
667             break;
668     }
669     return p1;
670 }
671
672 /*
673  * search_elements: Parse CCL search elements
674  * qa:     Qualifier attributes already applied.
675  * return: pointer to node(s); NULL on error.
676  */
677 static struct ccl_rpn_node *search_elements (struct ccl_rpn_attr **qa)
678 {
679     struct ccl_rpn_node *p1;
680     struct ccl_token *lookahead;
681     if (KIND == CCL_TOK_LP)
682     {
683         ADVANCE;
684         p1 = find_spec (qa);
685         if (!p1)
686             return NULL;
687         if (KIND != CCL_TOK_RP)
688         {
689             ccl_error = CCL_ERR_RP_EXPECTED;
690             ccl_rpn_delete (p1);
691             return NULL;
692         }
693         ADVANCE;
694         return p1;
695     }
696     else if (KIND == CCL_TOK_SET)
697     {
698         ADVANCE;
699         if (KIND == CCL_TOK_EQ)
700             ADVANCE;
701         if (KIND != CCL_TOK_TERM)
702         {
703             ccl_error = CCL_ERR_SETNAME_EXPECTED;
704             return NULL;
705         }
706         p1 = mk_node (CCL_RPN_SET);
707         p1->u.setname = copy_token_name (look_token);
708         ADVANCE;
709         return p1;
710     }
711     lookahead = look_token;
712
713     while (lookahead->kind==CCL_TOK_TERM)
714     {
715         lookahead = lookahead->next;
716         if (lookahead->kind == CCL_TOK_REL || lookahead->kind == CCL_TOK_EQ)
717             return qualifiers (lookahead, qa);
718         if (lookahead->kind != CCL_TOK_COMMA)
719             break;
720         lookahead = lookahead->next;
721     }
722     return search_terms (qa);
723 }
724
725 /*
726  * find_spec: Parse CCL find specification
727  * qa:     Qualifier attributes already applied.
728  * return: pointer to node(s); NULL on error.
729  */
730 static struct ccl_rpn_node *find_spec (struct ccl_rpn_attr **qa)
731 {
732     struct ccl_rpn_node *p1, *p2, *pn;
733     if (!(p1 = search_elements (qa)))
734         return NULL;
735     while (1)
736     {
737         switch (KIND)
738         {
739         case CCL_TOK_AND:
740             ADVANCE;
741             p2 = search_elements (qa);
742             if (!p2)
743             {
744                 ccl_rpn_delete (p1);
745                 return NULL;
746             }
747             pn = mk_node (CCL_RPN_AND);
748             pn->u.p[0] = p1;
749             pn->u.p[1] = p2;
750             p1 = pn;
751             continue;
752         case CCL_TOK_OR:
753             ADVANCE;
754             p2 = search_elements (qa);
755             if (!p2)
756             {
757                 ccl_rpn_delete (p1);
758                 return NULL;
759             }
760             pn = mk_node (CCL_RPN_OR);
761             pn->u.p[0] = p1;
762             pn->u.p[1] = p2;
763             p1 = pn;
764             continue;
765         case CCL_TOK_NOT:
766             ADVANCE;
767             p2 = search_elements (qa);
768             if (!p2)
769             {
770                 ccl_rpn_delete (p1);
771                 return NULL;
772             }
773             pn = mk_node (CCL_RPN_NOT);
774             pn->u.p[0] = p1;
775             pn->u.p[1] = p2;
776             p1 = pn;
777             continue;
778         }
779         break;
780     }
781     return p1;
782 }
783
784 /*
785  * ccl_find: Parse CCL find - token representation
786  * abibset: Bibset to be used for the parsing
787  * list:    List of tokens
788  * error:   Pointer to integer. Holds error no. on completion.
789  * pos:     Pointer to char position. Holds approximate error position.
790  * return:  RPN tree on successful completion; NULL otherwise.
791  */
792 struct ccl_rpn_node *ccl_find (CCL_bibset abibset, struct ccl_token *list,
793                                int *error, const char **pos)
794 {
795     struct ccl_rpn_node *p;
796
797     look_token = list;
798     bibset = abibset;
799     p = find_spec (NULL);
800     if (p && KIND != CCL_TOK_EOL)
801     {
802         if (KIND == CCL_TOK_RP)
803             ccl_error = CCL_ERR_BAD_RP;
804         else
805             ccl_error = CCL_ERR_OP_EXPECTED;
806         ccl_rpn_delete (p);
807         p = NULL;
808     }
809     *pos = look_token->name;
810     if (p)
811         *error = CCL_ERR_OK;
812     else
813         *error = ccl_error;
814     return p;
815 }
816
817 /*
818  * ccl_find_str: Parse CCL find - string representation
819  * bibset:  Bibset to be used for the parsing
820  * str:     String to be parsed
821  * error:   Pointer to integer. Holds error no. on completion.
822  * pos:     Pointer to char position. Holds approximate error position.
823  * return:  RPN tree on successful completion; NULL otherwise.
824  */
825 struct ccl_rpn_node *ccl_find_str (CCL_bibset bibset, const char *str,
826                                    int *error, int *pos)
827 {
828     struct ccl_token *list;
829     struct ccl_rpn_node *rpn;
830     const char *char_pos;
831
832     list = ccl_tokenize (str);
833     rpn = ccl_find (bibset, list, error, &char_pos);
834     if (*error)
835         *pos = char_pos - str;
836     return rpn;
837 }