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.15 2000-02-24 23:49:13 adam
49 * Fixed memory allocation problem.
51 * Revision 1.14 2000/01/31 13:15:21 adam
52 * Removed uses of assert(3). Cleanup of ODR. CCL parser update so
53 * that some characters are not surrounded by spaces in resulting term.
56 * Revision 1.13 1999/12/22 13:13:32 adam
57 * Search terms may include "operators" without causing error.
59 * Revision 1.12 1999/11/30 13:47:11 adam
60 * Improved installation. Moved header files to include/yaz.
62 * Revision 1.11 1999/03/31 11:15:37 adam
63 * Fixed memory leaks in ccl_find_str and ccl_qual_rm.
65 * Revision 1.10 1998/02/11 11:53:33 adam
66 * Changed code so that it compiles as C++.
68 * Revision 1.9 1997/09/29 08:56:37 adam
69 * Changed CCL parser to be thread safe. New type, CCL_parser, declared
70 * and a create/destructers ccl_parser_create/ccl_parser/destory has
73 * Revision 1.8 1997/09/01 08:48:11 adam
74 * New windows NT/95 port using MSV5.0. Only a few changes made
77 * Revision 1.7 1997/05/14 06:53:26 adam
80 * Revision 1.6 1997/04/30 08:52:06 quinn
83 * Revision 1.5 1996/10/11 15:00:24 adam
84 * CCL parser from Europagate Email gateway 1.0.
86 * Revision 1.16 1996/01/08 08:41:13 adam
87 * Removed unused function.
89 * Revision 1.15 1995/07/20 08:14:34 adam
90 * Qualifiers were observed too often. Instead tokens are treated as
91 * qualifiers only when separated by comma.
93 * Revision 1.14 1995/05/16 09:39:26 adam
96 * Revision 1.13 1995/04/17 09:31:42 adam
97 * Improved handling of qualifiers. Aliases or reserved words.
99 * Revision 1.12 1995/03/20 15:27:43 adam
102 * Revision 1.11 1995/02/23 08:31:59 adam
105 * Revision 1.9 1995/02/16 13:20:06 adam
108 * Revision 1.8 1995/02/14 19:59:42 adam
109 * Removed a syntax error.
111 * Revision 1.7 1995/02/14 19:55:10 adam
112 * Header files ccl.h/cclp.h are gone! They have been merged an
113 * moved to ../include/ccl.h.
114 * Node kind(s) in ccl_rpn_node have changed names.
116 * Revision 1.6 1995/02/14 16:20:55 adam
117 * Qualifiers are read from a file now.
119 * Revision 1.5 1995/02/14 14:12:41 adam
120 * Ranges for ordered qualfiers implemented (e.g. pd=1980-1990).
122 * Revision 1.4 1995/02/14 13:16:29 adam
123 * Left and/or right truncation implemented.
125 * Revision 1.3 1995/02/14 10:25:56 adam
126 * The constructions 'qualifier rel term ...' implemented.
128 * Revision 1.2 1995/02/13 15:15:07 adam
129 * Added handling of qualifiers. Not finished yet.
131 * Revision 1.1 1995/02/13 12:35:20 adam
132 * First version of CCL. Qualifiers aren't handled yet.
142 /* returns type of current lookahead */
143 #define KIND (cclp->look_token->kind)
145 /* move one token forward */
146 #define ADVANCE cclp->look_token = cclp->look_token->next
149 * qual_val_type: test for existance of attribute type/value pair.
150 * qa: Attribute array
151 * type: Type of attribute to search for
152 * value: Value of attribute to seach for
153 * return: 1 if found; 0 otherwise.
155 static int qual_val_type (struct ccl_rpn_attr **qa, int type, int value)
158 struct ccl_rpn_attr *q;
162 for (i = 0; (q=qa[i]); i++)
165 if (q->type == type && q->value == value)
173 * strxcat: concatenate strings.
174 * n: Null-terminated Destination string
175 * src: Source string to be appended (not null-terminated)
176 * len: Length of source string.
178 static void strxcat (char *n, const char *src, int len)
188 * copy_token_name: Return copy of CCL token name
189 * tp: Pointer to token info.
190 * return: malloc(3) allocated copy of token name.
192 static char *copy_token_name (struct ccl_token *tp)
194 char *str = (char *)malloc (tp->len + 1);
196 memcpy (str, tp->name, tp->len);
202 * mk_node: Create RPN node.
203 * kind: Type of node.
204 * return: pointer to allocated node.
206 static struct ccl_rpn_node *mk_node (int kind)
208 struct ccl_rpn_node *p;
209 p = (struct ccl_rpn_node *)malloc (sizeof(*p));
216 * ccl_rpn_delete: Delete RPN tree.
217 * rpn: Pointer to tree.
219 void ccl_rpn_delete (struct ccl_rpn_node *rpn)
221 struct ccl_rpn_attr *attr, *attr1;
229 ccl_rpn_delete (rpn->u.p[0]);
230 ccl_rpn_delete (rpn->u.p[1]);
233 free (rpn->u.t.term);
234 for (attr = rpn->u.t.attr_list; attr; attr = attr1)
241 free (rpn->u.setname);
244 ccl_rpn_delete (rpn->u.p[0]);
245 ccl_rpn_delete (rpn->u.p[1]);
251 static struct ccl_rpn_node *find_spec (CCL_parser cclp,
252 struct ccl_rpn_attr **qa);
254 static int is_term_ok (int look, int *list)
256 for (;*list >= 0; list++)
262 static struct ccl_rpn_node *search_terms (CCL_parser cclp,
263 struct ccl_rpn_attr **qa);
266 * add_attr: Add attribute (type/value) to RPN term node.
267 * p: RPN node of type term.
268 * type: Type of attribute
269 * value: Value of attribute
271 static void add_attr (struct ccl_rpn_node *p, int type, int value)
273 struct ccl_rpn_attr *n;
275 n = (struct ccl_rpn_attr *)malloc (sizeof(*n));
279 n->next = p->u.t.attr_list;
280 p->u.t.attr_list = n;
284 * search_term: Parse CCL search term.
286 * qa: Qualifier attributes already applied.
287 * return: pointer to node(s); NULL on error.
289 static struct ccl_rpn_node *search_term_x (CCL_parser cclp,
290 struct ccl_rpn_attr **qa,
293 struct ccl_rpn_attr *qa_tmp[2];
294 struct ccl_rpn_node *p;
295 struct ccl_token *lookahead = cclp->look_token;
301 int relation_value = -1;
302 int position_value = -1;
303 int structure_value = -1;
304 int truncation_value = -1;
305 int completeness_value = -1;
307 if (!is_term_ok(KIND, term_list))
309 cclp->error_code = CCL_ERR_TERM_EXPECTED;
312 /* create the term node, but wait a moment before adding the term */
313 p = mk_node (CCL_RPN_TERM);
314 p->u.t.attr_list = NULL;
319 /* no qualifier(s) applied. Use 'term' if it is defined */
323 qa[0] = ccl_qual_search (cclp, "term", 4);
327 /* go through all attributes and add them to the attribute list */
328 for (i=0; qa && qa[i]; i++)
330 struct ccl_rpn_attr *attr;
332 for (attr = qa[i]; attr; attr = attr->next)
334 { /* deal only with REAL attributes (positive) */
338 if (relation_value != -1)
340 relation_value = attr->value;
343 if (position_value != -1)
345 position_value = attr->value;
348 if (structure_value != -1)
350 structure_value = attr->value;
353 if (truncation_value != -1)
355 truncation_value = attr->value;
358 if (completeness_value != -1)
360 completeness_value = attr->value;
363 add_attr (p, attr->type, attr->value);
366 /* go through each TERM token. If no truncation attribute is yet
367 met, then look for left/right truncation markers (?) and
368 set left_trunc/right_trunc/mid_trunc accordingly */
369 for (no = 0; is_term_ok(lookahead->kind, term_list); no++)
371 for (i = 0; i<lookahead->len; i++)
372 if (truncation_value == -1 && lookahead->name[i] == '?')
374 if (no == 0 && i == 0 && lookahead->len >= 1)
376 else if (!is_term_ok(lookahead->next->kind, term_list) &&
377 i == lookahead->len-1 && i >= 1)
382 len += 1+lookahead->len;
383 lookahead = lookahead->next;
385 /* len now holds the number of characters in the RPN term */
386 /* no holds the number of CCL tokens (1 or more) */
388 if (structure_value == -1 &&
389 qual_val_type (qa, CCL_BIB1_STR, CCL_BIB1_STR_WP))
390 { /* no structure attribute met. Apply either structure attribute
391 WORD or PHRASE depending on number of CCL tokens */
393 add_attr (p, CCL_BIB1_STR, 2);
395 add_attr (p, CCL_BIB1_STR, 1);
398 /* make the RPN token */
399 p->u.t.term = (char *)malloc (len);
400 ccl_assert (p->u.t.term);
401 p->u.t.term[0] = '\0';
402 for (i = 0; i<no; i++)
404 const char *src_str = cclp->look_token->name;
405 int src_len = cclp->look_token->len;
407 if (i == 0 && left_trunc)
412 else if (i == no-1 && right_trunc)
416 int len = strlen(p->u.t.term);
418 !strchr("-+", *src_str) &&
419 !strchr("-+", p->u.t.term[len-1]))
421 strcat (p->u.t.term, " ");
424 strxcat (p->u.t.term, src_str, src_len);
427 if (left_trunc && right_trunc)
429 if (!qual_val_type (qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_BOTH))
431 cclp->error_code = CCL_ERR_TRUNC_NOT_BOTH;
435 add_attr (p, CCL_BIB1_TRU, 3);
437 else if (right_trunc)
439 if (!qual_val_type (qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_RIGHT))
441 cclp->error_code = CCL_ERR_TRUNC_NOT_RIGHT;
445 add_attr (p, CCL_BIB1_TRU, 1);
449 if (!qual_val_type (qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_LEFT))
451 cclp->error_code = CCL_ERR_TRUNC_NOT_LEFT;
455 add_attr (p, CCL_BIB1_TRU, 2);
459 if (qual_val_type (qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_NONE))
460 add_attr (p, CCL_BIB1_TRU, 100);
465 static struct ccl_rpn_node *search_term (CCL_parser cclp,
466 struct ccl_rpn_attr **qa)
468 static int list[] = {CCL_TOK_TERM, CCL_TOK_COMMA, -1};
469 return search_term_x(cclp, qa, list);
473 * qualifiers: Parse CCL qualifiers and search terms.
475 * la: Token pointer to RELATION token.
476 * qa: Qualifier attributes already applied.
477 * return: pointer to node(s); NULL on error.
479 static struct ccl_rpn_node *qualifiers (CCL_parser cclp, struct ccl_token *la,
480 struct ccl_rpn_attr **qa)
482 struct ccl_token *lookahead = cclp->look_token;
483 struct ccl_rpn_attr **ap;
489 cclp->error_code = CCL_ERR_DOUBLE_QUAL;
493 for (lookahead = cclp->look_token; lookahead != la;
494 lookahead=lookahead->next)
497 for (i=0; qa[i]; i++)
499 ap = (struct ccl_rpn_attr **)malloc ((no+1) * sizeof(*ap));
501 for (i = 0; cclp->look_token != la; i++)
503 ap[i] = ccl_qual_search (cclp, cclp->look_token->name,
504 cclp->look_token->len);
507 cclp->error_code = CCL_ERR_UNKNOWN_QUAL;
512 if (KIND == CCL_TOK_COMMA)
519 if (!qual_val_type (ap, CCL_BIB1_REL, CCL_BIB1_REL_ORDER))
521 /* unordered relation */
522 struct ccl_rpn_node *p;
523 if (KIND != CCL_TOK_EQ)
525 cclp->error_code = CCL_ERR_EQ_EXPECTED;
530 if (KIND == CCL_TOK_LP)
533 if (!(p = find_spec (cclp, ap)))
538 if (KIND != CCL_TOK_RP)
540 cclp->error_code = CCL_ERR_RP_EXPECTED;
548 p = search_terms (cclp, ap);
552 /* ordered relation ... */
554 if (cclp->look_token->len == 1)
556 if (cclp->look_token->name[0] == '<')
558 else if (cclp->look_token->name[0] == '=')
560 else if (cclp->look_token->name[0] == '>')
563 else if (cclp->look_token->len == 2)
565 if (!memcmp (cclp->look_token->name, "<=", 2))
567 else if (!memcmp (cclp->look_token->name, ">=", 2))
569 else if (!memcmp (cclp->look_token->name, "<>", 2))
573 cclp->error_code = CCL_ERR_BAD_RELATION;
576 struct ccl_rpn_node *p;
578 ADVANCE; /* skip relation */
579 if (KIND == CCL_TOK_TERM &&
580 cclp->look_token->next->kind == CCL_TOK_MINUS)
582 struct ccl_rpn_node *p1;
583 if (!(p1 = search_term (cclp, ap)))
588 ADVANCE; /* skip '-' */
589 if (KIND == CCL_TOK_TERM) /* = term - term ? */
591 struct ccl_rpn_node *p2;
593 if (!(p2 = search_term (cclp, ap)))
599 p = mk_node (CCL_RPN_AND);
601 add_attr (p1, CCL_BIB1_REL, 4);
603 add_attr (p2, CCL_BIB1_REL, 2);
609 add_attr (p1, CCL_BIB1_REL, 4);
614 else if (KIND == CCL_TOK_MINUS) /* = - term ? */
617 if (!(p = search_term (cclp, ap)))
622 add_attr (p, CCL_BIB1_REL, 2);
626 else if (KIND == CCL_TOK_LP)
629 if (!(p = find_spec (cclp, ap)))
634 if (KIND != CCL_TOK_RP)
636 cclp->error_code = CCL_ERR_RP_EXPECTED;
647 if (!(p = search_terms (cclp, ap)))
652 add_attr (p, CCL_BIB1_REL, rel);
656 cclp->error_code = CCL_ERR_TERM_EXPECTED;
663 * search_terms: Parse CCL search terms - including proximity.
665 * qa: Qualifier attributes already applied.
666 * return: pointer to node(s); NULL on error.
668 static struct ccl_rpn_node *search_terms (CCL_parser cclp,
669 struct ccl_rpn_attr **qa)
671 static int list[] = {
672 CCL_TOK_TERM, CCL_TOK_COMMA,CCL_TOK_EQ,
673 CCL_TOK_REL, CCL_TOK_MINUS, -1};
674 struct ccl_rpn_node *p1, *p2, *pn;
675 p1 = search_term_x (cclp, qa, list);
680 if (KIND == CCL_TOK_PROX)
683 p2 = search_term_x (cclp, qa, list);
689 pn = mk_node (CCL_RPN_PROX);
694 else if (is_term_ok(KIND, list))
696 p2 = search_term_x (cclp, qa, list);
702 pn = mk_node (CCL_RPN_PROX);
714 * search_elements: Parse CCL search elements
716 * qa: Qualifier attributes already applied.
717 * return: pointer to node(s); NULL on error.
719 static struct ccl_rpn_node *search_elements (CCL_parser cclp,
720 struct ccl_rpn_attr **qa)
722 struct ccl_rpn_node *p1;
723 struct ccl_token *lookahead;
724 if (KIND == CCL_TOK_LP)
727 p1 = find_spec (cclp, qa);
730 if (KIND != CCL_TOK_RP)
732 cclp->error_code = CCL_ERR_RP_EXPECTED;
739 else if (KIND == CCL_TOK_SET)
742 if (KIND == CCL_TOK_EQ)
744 if (KIND != CCL_TOK_TERM)
746 cclp->error_code = CCL_ERR_SETNAME_EXPECTED;
749 p1 = mk_node (CCL_RPN_SET);
750 p1->u.setname = copy_token_name (cclp->look_token);
754 lookahead = cclp->look_token;
756 while (lookahead->kind==CCL_TOK_TERM)
758 lookahead = lookahead->next;
759 if (lookahead->kind == CCL_TOK_REL || lookahead->kind == CCL_TOK_EQ)
760 return qualifiers (cclp, lookahead, qa);
761 if (lookahead->kind != CCL_TOK_COMMA)
763 lookahead = lookahead->next;
765 return search_terms (cclp, qa);
769 * find_spec: Parse CCL find specification
771 * qa: Qualifier attributes already applied.
772 * return: pointer to node(s); NULL on error.
774 static struct ccl_rpn_node *find_spec (CCL_parser cclp,
775 struct ccl_rpn_attr **qa)
777 struct ccl_rpn_node *p1, *p2, *pn;
778 if (!(p1 = search_elements (cclp, qa)))
786 p2 = search_elements (cclp, qa);
792 pn = mk_node (CCL_RPN_AND);
799 p2 = search_elements (cclp, qa);
805 pn = mk_node (CCL_RPN_OR);
812 p2 = search_elements (cclp, qa);
818 pn = mk_node (CCL_RPN_NOT);
829 struct ccl_rpn_node *ccl_parser_find (CCL_parser cclp, struct ccl_token *list)
831 struct ccl_rpn_node *p;
833 cclp->look_token = list;
834 p = find_spec (cclp, NULL);
835 if (p && KIND != CCL_TOK_EOL)
837 if (KIND == CCL_TOK_RP)
838 cclp->error_code = CCL_ERR_BAD_RP;
840 cclp->error_code = CCL_ERR_OP_EXPECTED;
844 cclp->error_pos = cclp->look_token->name;
846 cclp->error_code = CCL_ERR_OK;
848 cclp->error_code = cclp->error_code;
853 * ccl_find: Parse CCL find - token representation
854 * bibset: Bibset to be used for the parsing
855 * list: List of tokens
856 * error: Pointer to integer. Holds error no. on completion.
857 * pos: Pointer to char position. Holds approximate error position.
858 * return: RPN tree on successful completion; NULL otherwise.
860 struct ccl_rpn_node *ccl_find (CCL_bibset bibset, struct ccl_token *list,
861 int *error, const char **pos)
863 struct ccl_rpn_node *p;
864 CCL_parser cclp = ccl_parser_create ();
866 cclp->bibset = bibset;
868 p = ccl_parser_find (cclp, list);
870 *error = cclp->error_code;
871 *pos = cclp->error_pos;
873 ccl_parser_destroy (cclp);
879 * ccl_find_str: Parse CCL find - string representation
880 * bibset: Bibset to be used for the parsing
881 * str: String to be parsed
882 * error: Pointer to integer. Holds error no. on completion.
883 * pos: Pointer to char position. Holds approximate error position.
884 * return: RPN tree on successful completion; NULL otherwise.
886 struct ccl_rpn_node *ccl_find_str (CCL_bibset bibset, const char *str,
887 int *error, int *pos)
889 CCL_parser cclp = ccl_parser_create ();
890 struct ccl_token *list;
891 struct ccl_rpn_node *p;
893 cclp->bibset = bibset;
895 list = ccl_parser_tokenize (cclp, str);
896 p = ccl_parser_find (cclp, list);
898 *error = cclp->error_code;
900 *pos = cclp->error_pos - str;
901 ccl_parser_destroy (cclp);
902 ccl_token_del (list);