2 * Copyright (c) 1995, the EUROPAGATE consortium (see below).
4 * The EUROPAGATE consortium members are:
6 * University College Dublin
7 * Danmarks Teknologiske Videnscenter
8 * An Chomhairle Leabharlanna
9 * Consejo Superior de Investigaciones Cientificas
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,
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.
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.
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.
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.
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.
44 /* CCL find (to rpn conversion)
48 * Revision 1.12 1999-11-30 13:47:11 adam
49 * Improved installation. Moved header files to include/yaz.
51 * Revision 1.11 1999/03/31 11:15:37 adam
52 * Fixed memory leaks in ccl_find_str and ccl_qual_rm.
54 * Revision 1.10 1998/02/11 11:53:33 adam
55 * Changed code so that it compiles as C++.
57 * Revision 1.9 1997/09/29 08:56:37 adam
58 * Changed CCL parser to be thread safe. New type, CCL_parser, declared
59 * and a create/destructers ccl_parser_create/ccl_parser/destory has
62 * Revision 1.8 1997/09/01 08:48:11 adam
63 * New windows NT/95 port using MSV5.0. Only a few changes made
66 * Revision 1.7 1997/05/14 06:53:26 adam
69 * Revision 1.6 1997/04/30 08:52:06 quinn
72 * Revision 1.5 1996/10/11 15:00:24 adam
73 * CCL parser from Europagate Email gateway 1.0.
75 * Revision 1.16 1996/01/08 08:41:13 adam
76 * Removed unused function.
78 * Revision 1.15 1995/07/20 08:14:34 adam
79 * Qualifiers were observed too often. Instead tokens are treated as
80 * qualifiers only when separated by comma.
82 * Revision 1.14 1995/05/16 09:39:26 adam
85 * Revision 1.13 1995/04/17 09:31:42 adam
86 * Improved handling of qualifiers. Aliases or reserved words.
88 * Revision 1.12 1995/03/20 15:27:43 adam
91 * Revision 1.11 1995/02/23 08:31:59 adam
94 * Revision 1.9 1995/02/16 13:20:06 adam
97 * Revision 1.8 1995/02/14 19:59:42 adam
98 * Removed a syntax error.
100 * Revision 1.7 1995/02/14 19:55:10 adam
101 * Header files ccl.h/cclp.h are gone! They have been merged an
102 * moved to ../include/ccl.h.
103 * Node kind(s) in ccl_rpn_node have changed names.
105 * Revision 1.6 1995/02/14 16:20:55 adam
106 * Qualifiers are read from a file now.
108 * Revision 1.5 1995/02/14 14:12:41 adam
109 * Ranges for ordered qualfiers implemented (e.g. pd=1980-1990).
111 * Revision 1.4 1995/02/14 13:16:29 adam
112 * Left and/or right truncation implemented.
114 * Revision 1.3 1995/02/14 10:25:56 adam
115 * The constructions 'qualifier rel term ...' implemented.
117 * Revision 1.2 1995/02/13 15:15:07 adam
118 * Added handling of qualifiers. Not finished yet.
120 * Revision 1.1 1995/02/13 12:35:20 adam
121 * First version of CCL. Qualifiers aren't handled yet.
132 /* returns type of current lookahead */
133 #define KIND (cclp->look_token->kind)
135 /* move one token forward */
136 #define ADVANCE cclp->look_token = cclp->look_token->next
139 * qual_val_type: test for existance of attribute type/value pair.
140 * qa: Attribute array
141 * type: Type of attribute to search for
142 * value: Value of attribute to seach for
143 * return: 1 if found; 0 otherwise.
145 static int qual_val_type (struct ccl_rpn_attr **qa, int type, int value)
148 struct ccl_rpn_attr *q;
152 for (i = 0; (q=qa[i]); i++)
155 if (q->type == type && q->value == value)
163 * strxcat: concatenate strings.
164 * n: Null-terminated Destination string
165 * src: Source string to be appended (not null-terminated)
166 * len: Length of source string.
168 static void strxcat (char *n, const char *src, int len)
178 * copy_token_name: Return copy of CCL token name
179 * tp: Pointer to token info.
180 * return: malloc(3) allocated copy of token name.
182 static char *copy_token_name (struct ccl_token *tp)
184 char *str = (char *)malloc (tp->len + 1);
186 memcpy (str, tp->name, tp->len);
192 * mk_node: Create RPN node.
193 * kind: Type of node.
194 * return: pointer to allocated node.
196 static struct ccl_rpn_node *mk_node (int kind)
198 struct ccl_rpn_node *p;
199 p = (struct ccl_rpn_node *)malloc (sizeof(*p));
206 * ccl_rpn_delete: Delete RPN tree.
207 * rpn: Pointer to tree.
209 void ccl_rpn_delete (struct ccl_rpn_node *rpn)
211 struct ccl_rpn_attr *attr, *attr1;
219 ccl_rpn_delete (rpn->u.p[0]);
220 ccl_rpn_delete (rpn->u.p[1]);
223 free (rpn->u.t.term);
224 for (attr = rpn->u.t.attr_list; attr; attr = attr1)
231 free (rpn->u.setname);
234 ccl_rpn_delete (rpn->u.p[0]);
235 ccl_rpn_delete (rpn->u.p[1]);
241 static struct ccl_rpn_node *find_spec (CCL_parser cclp,
242 struct ccl_rpn_attr **qa);
243 static struct ccl_rpn_node *search_terms (CCL_parser cclp,
244 struct ccl_rpn_attr **qa);
247 * add_attr: Add attribute (type/value) to RPN term node.
248 * p: RPN node of type term.
249 * type: Type of attribute
250 * value: Value of attribute
252 static void add_attr (struct ccl_rpn_node *p, int type, int value)
254 struct ccl_rpn_attr *n;
256 n = (struct ccl_rpn_attr *)malloc (sizeof(*n));
260 n->next = p->u.t.attr_list;
261 p->u.t.attr_list = n;
265 * search_term: Parse CCL search term.
267 * qa: Qualifier attributes already applied.
268 * return: pointer to node(s); NULL on error.
270 static struct ccl_rpn_node *search_term (CCL_parser cclp,
271 struct ccl_rpn_attr **qa)
273 struct ccl_rpn_node *p;
274 struct ccl_token *lookahead = cclp->look_token;
280 int relation_value = -1;
281 int position_value = -1;
282 int structure_value = -1;
283 int truncation_value = -1;
284 int completeness_value = -1;
286 if (KIND != CCL_TOK_TERM)
288 cclp->error_code = CCL_ERR_TERM_EXPECTED;
291 /* create the term node, but wait a moment before adding the term */
292 p = mk_node (CCL_RPN_TERM);
293 p->u.t.attr_list = NULL;
298 /* no qualifier(s) applied. Use 'term' if it is defined */
300 qa = (struct ccl_rpn_attr **)malloc (2*sizeof(*qa));
302 qa[0] = ccl_qual_search (cclp, "term", 4);
306 /* go through all attributes and add them to the attribute list */
307 for (i=0; qa && qa[i]; i++)
309 struct ccl_rpn_attr *attr;
311 for (attr = qa[i]; attr; attr = attr->next)
313 { /* deal only with REAL attributes (positive) */
317 if (relation_value != -1)
319 relation_value = attr->value;
322 if (position_value != -1)
324 position_value = attr->value;
327 if (structure_value != -1)
329 structure_value = attr->value;
332 if (truncation_value != -1)
334 truncation_value = attr->value;
337 if (completeness_value != -1)
339 completeness_value = attr->value;
342 add_attr (p, attr->type, attr->value);
345 /* go through each TERM token. If no truncation attribute is yet
346 met, then look for left/right truncation markers (?) and
347 set left_trunc/right_trunc/mid_trunc accordingly */
348 for (no = 0; lookahead->kind == CCL_TOK_TERM; no++)
350 for (i = 0; i<lookahead->len; i++)
351 if (truncation_value == -1 && lookahead->name[i] == '?')
353 if (no == 0 && i == 0 && lookahead->len >= 1)
355 else if (lookahead->next->kind != CCL_TOK_TERM &&
356 i == lookahead->len-1 && i >= 1)
361 len += 1+lookahead->len;
362 lookahead = lookahead->next;
364 /* len now holds the number of characters in the RPN term */
365 /* no holds the number of CCL tokens (1 or more) */
367 if (structure_value == -1 &&
368 qual_val_type (qa, CCL_BIB1_STR, CCL_BIB1_STR_WP))
369 { /* no structure attribute met. Apply either structure attribute
370 WORD or PHRASE depending on number of CCL tokens */
372 add_attr (p, CCL_BIB1_STR, 2);
374 add_attr (p, CCL_BIB1_STR, 1);
377 /* make the RPN token */
378 p->u.t.term = (char *)malloc (len);
379 assert (p->u.t.term);
380 p->u.t.term[0] = '\0';
381 for (i = 0; i<no; i++)
383 const char *src_str = cclp->look_token->name;
384 int src_len = cclp->look_token->len;
386 if (i == 0 && left_trunc)
391 else if (i == no-1 && right_trunc)
394 strcat (p->u.t.term, " ");
395 strxcat (p->u.t.term, src_str, src_len);
398 if (left_trunc && right_trunc)
400 if (!qual_val_type (qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_BOTH))
402 cclp->error_code = CCL_ERR_TRUNC_NOT_BOTH;
407 add_attr (p, CCL_BIB1_TRU, 3);
409 else if (right_trunc)
411 if (!qual_val_type (qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_RIGHT))
413 cclp->error_code = CCL_ERR_TRUNC_NOT_RIGHT;
418 add_attr (p, CCL_BIB1_TRU, 1);
422 if (!qual_val_type (qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_LEFT))
424 cclp->error_code = CCL_ERR_TRUNC_NOT_LEFT;
429 add_attr (p, CCL_BIB1_TRU, 2);
433 if (qual_val_type (qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_NONE))
434 add_attr (p, CCL_BIB1_TRU, 100);
440 * qualifiers: Parse CCL qualifiers and search terms.
442 * la: Token pointer to RELATION token.
443 * qa: Qualifier attributes already applied.
444 * return: pointer to node(s); NULL on error.
446 static struct ccl_rpn_node *qualifiers (CCL_parser cclp, struct ccl_token *la,
447 struct ccl_rpn_attr **qa)
449 struct ccl_token *lookahead = cclp->look_token;
450 struct ccl_rpn_attr **ap;
456 cclp->error_code = CCL_ERR_DOUBLE_QUAL;
460 for (lookahead = cclp->look_token; lookahead != la;
461 lookahead=lookahead->next)
464 for (i=0; qa[i]; i++)
466 ap = (struct ccl_rpn_attr **)malloc ((no+1) * sizeof(*ap));
468 for (i = 0; cclp->look_token != la; i++)
470 ap[i] = ccl_qual_search (cclp, cclp->look_token->name,
471 cclp->look_token->len);
474 cclp->error_code = CCL_ERR_UNKNOWN_QUAL;
479 if (KIND == CCL_TOK_COMMA)
486 if (!qual_val_type (ap, CCL_BIB1_REL, CCL_BIB1_REL_ORDER))
488 /* unordered relation */
489 struct ccl_rpn_node *p;
490 if (KIND != CCL_TOK_EQ)
492 cclp->error_code = CCL_ERR_EQ_EXPECTED;
497 if (KIND == CCL_TOK_LP)
500 if (!(p = find_spec (cclp, ap)))
505 if (KIND != CCL_TOK_RP)
507 cclp->error_code = CCL_ERR_RP_EXPECTED;
515 p = search_terms (cclp, ap);
520 if (cclp->look_token->len == 1)
522 if (cclp->look_token->name[0] == '<')
524 else if (cclp->look_token->name[0] == '=')
526 else if (cclp->look_token->name[0] == '>')
529 else if (cclp->look_token->len == 2)
531 if (!memcmp (cclp->look_token->name, "<=", 2))
533 else if (!memcmp (cclp->look_token->name, ">=", 2))
535 else if (!memcmp (cclp->look_token->name, "<>", 2))
539 cclp->error_code = CCL_ERR_BAD_RELATION;
542 struct ccl_rpn_node *p;
544 ADVANCE; /* skip relation */
545 if (KIND == CCL_TOK_TERM &&
546 cclp->look_token->next->kind == CCL_TOK_MINUS)
548 struct ccl_rpn_node *p1;
549 if (!(p1 = search_term (cclp, ap)))
554 ADVANCE; /* skip '-' */
555 if (KIND == CCL_TOK_TERM) /* = term - term ? */
557 struct ccl_rpn_node *p2;
559 if (!(p2 = search_term (cclp, ap)))
565 p = mk_node (CCL_RPN_AND);
567 add_attr (p1, CCL_BIB1_REL, 4);
569 add_attr (p2, CCL_BIB1_REL, 2);
575 add_attr (p1, CCL_BIB1_REL, 4);
580 else if (KIND == CCL_TOK_MINUS) /* = - term ? */
583 if (!(p = search_term (cclp, ap)))
588 add_attr (p, CCL_BIB1_REL, 2);
592 else if (KIND == CCL_TOK_LP)
595 if (!(p = find_spec (cclp, ap)))
600 if (KIND != CCL_TOK_RP)
602 cclp->error_code = CCL_ERR_RP_EXPECTED;
613 if (!(p = search_terms (cclp, ap)))
618 add_attr (p, CCL_BIB1_REL, rel);
622 cclp->error_code = CCL_ERR_TERM_EXPECTED;
629 * search_terms: Parse CCL search terms - including proximity.
631 * qa: Qualifier attributes already applied.
632 * return: pointer to node(s); NULL on error.
634 static struct ccl_rpn_node *search_terms (CCL_parser cclp,
635 struct ccl_rpn_attr **qa)
637 struct ccl_rpn_node *p1, *p2, *pn;
638 p1 = search_term (cclp, qa);
643 if (KIND == CCL_TOK_PROX)
646 p2 = search_term (cclp, qa);
652 pn = mk_node (CCL_RPN_PROX);
657 else if (KIND == CCL_TOK_TERM)
659 p2 = search_term (cclp, qa);
665 pn = mk_node (CCL_RPN_PROX);
677 * search_elements: Parse CCL search elements
679 * qa: Qualifier attributes already applied.
680 * return: pointer to node(s); NULL on error.
682 static struct ccl_rpn_node *search_elements (CCL_parser cclp,
683 struct ccl_rpn_attr **qa)
685 struct ccl_rpn_node *p1;
686 struct ccl_token *lookahead;
687 if (KIND == CCL_TOK_LP)
690 p1 = find_spec (cclp, qa);
693 if (KIND != CCL_TOK_RP)
695 cclp->error_code = CCL_ERR_RP_EXPECTED;
702 else if (KIND == CCL_TOK_SET)
705 if (KIND == CCL_TOK_EQ)
707 if (KIND != CCL_TOK_TERM)
709 cclp->error_code = CCL_ERR_SETNAME_EXPECTED;
712 p1 = mk_node (CCL_RPN_SET);
713 p1->u.setname = copy_token_name (cclp->look_token);
717 lookahead = cclp->look_token;
719 while (lookahead->kind==CCL_TOK_TERM)
721 lookahead = lookahead->next;
722 if (lookahead->kind == CCL_TOK_REL || lookahead->kind == CCL_TOK_EQ)
723 return qualifiers (cclp, lookahead, qa);
724 if (lookahead->kind != CCL_TOK_COMMA)
726 lookahead = lookahead->next;
728 return search_terms (cclp, qa);
732 * find_spec: Parse CCL find specification
734 * qa: Qualifier attributes already applied.
735 * return: pointer to node(s); NULL on error.
737 static struct ccl_rpn_node *find_spec (CCL_parser cclp,
738 struct ccl_rpn_attr **qa)
740 struct ccl_rpn_node *p1, *p2, *pn;
741 if (!(p1 = search_elements (cclp, qa)))
749 p2 = search_elements (cclp, qa);
755 pn = mk_node (CCL_RPN_AND);
762 p2 = search_elements (cclp, qa);
768 pn = mk_node (CCL_RPN_OR);
775 p2 = search_elements (cclp, qa);
781 pn = mk_node (CCL_RPN_NOT);
792 struct ccl_rpn_node *ccl_parser_find (CCL_parser cclp, struct ccl_token *list)
794 struct ccl_rpn_node *p;
796 cclp->look_token = list;
797 p = find_spec (cclp, NULL);
798 if (p && KIND != CCL_TOK_EOL)
800 if (KIND == CCL_TOK_RP)
801 cclp->error_code = CCL_ERR_BAD_RP;
803 cclp->error_code = CCL_ERR_OP_EXPECTED;
807 cclp->error_pos = cclp->look_token->name;
809 cclp->error_code = CCL_ERR_OK;
811 cclp->error_code = cclp->error_code;
816 * ccl_find: Parse CCL find - token representation
817 * bibset: Bibset to be used for the parsing
818 * list: List of tokens
819 * error: Pointer to integer. Holds error no. on completion.
820 * pos: Pointer to char position. Holds approximate error position.
821 * return: RPN tree on successful completion; NULL otherwise.
823 struct ccl_rpn_node *ccl_find (CCL_bibset bibset, struct ccl_token *list,
824 int *error, const char **pos)
826 struct ccl_rpn_node *p;
827 CCL_parser cclp = ccl_parser_create ();
829 cclp->bibset = bibset;
831 p = ccl_parser_find (cclp, list);
833 *error = cclp->error_code;
834 *pos = cclp->error_pos;
836 ccl_parser_destroy (cclp);
842 * ccl_find_str: Parse CCL find - string representation
843 * bibset: Bibset to be used for the parsing
844 * str: String to be parsed
845 * error: Pointer to integer. Holds error no. on completion.
846 * pos: Pointer to char position. Holds approximate error position.
847 * return: RPN tree on successful completion; NULL otherwise.
849 struct ccl_rpn_node *ccl_find_str (CCL_bibset bibset, const char *str,
850 int *error, int *pos)
852 CCL_parser cclp = ccl_parser_create ();
853 struct ccl_token *list;
854 struct ccl_rpn_node *p;
856 cclp->bibset = bibset;
858 list = ccl_parser_tokenize (cclp, str);
859 p = ccl_parser_find (cclp, list);
861 *error = cclp->error_code;
863 *pos = cclp->error_pos - str;
864 ccl_parser_destroy (cclp);
865 ccl_token_del (list);