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