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.25 2001-10-03 23:54:41 adam
49 * Fixes for numeric ranges (date=1980-1990).
51 * Revision 1.24 2001/03/22 21:23:30 adam
52 * Directive s=pw sets structure to phrase if term includes blank(s).
54 * Revision 1.23 2001/03/20 11:22:58 adam
55 * CCL Truncation character may be defined.
57 * Revision 1.22 2001/03/07 13:24:40 adam
58 * Member and_not in Z_Operator is kept for backwards compatibility.
59 * Added support for definition of CCL operators in field spec file.
61 * Revision 1.21 2001/02/21 13:46:53 adam
64 * Revision 1.20 2000/11/16 13:03:12 adam
65 * Function ccl_rpn_query sets attributeSet to Bib-1.
67 * Revision 1.19 2000/11/16 09:58:02 adam
68 * Implemented local AttributeSet setting for CCL field maps.
70 * Revision 1.18 2000/10/17 19:50:28 adam
71 * Implemented and-list and or-list for CCL module.
73 * Revision 1.17 2000/05/01 09:36:50 adam
74 * Range operator only treated in ordered ranges so that minus (-) can be
75 * used for, say, the and-not operator.
77 * Revision 1.16 2000/03/14 09:06:11 adam
78 * Added POSIX threads support for frontend server.
80 * Revision 1.15 2000/02/24 23:49:13 adam
81 * Fixed memory allocation problem.
83 * Revision 1.14 2000/01/31 13:15:21 adam
84 * Removed uses of assert(3). Cleanup of ODR. CCL parser update so
85 * that some characters are not surrounded by spaces in resulting term.
88 * Revision 1.13 1999/12/22 13:13:32 adam
89 * Search terms may include "operators" without causing error.
91 * Revision 1.12 1999/11/30 13:47:11 adam
92 * Improved installation. Moved header files to include/yaz.
94 * Revision 1.11 1999/03/31 11:15:37 adam
95 * Fixed memory leaks in ccl_find_str and ccl_qual_rm.
97 * Revision 1.10 1998/02/11 11:53:33 adam
98 * Changed code so that it compiles as C++.
100 * Revision 1.9 1997/09/29 08:56:37 adam
101 * Changed CCL parser to be thread safe. New type, CCL_parser, declared
102 * and a create/destructers ccl_parser_create/ccl_parser/destory has
105 * Revision 1.8 1997/09/01 08:48:11 adam
106 * New windows NT/95 port using MSV5.0. Only a few changes made
109 * Revision 1.7 1997/05/14 06:53:26 adam
112 * Revision 1.6 1997/04/30 08:52:06 quinn
115 * Revision 1.5 1996/10/11 15:00:24 adam
116 * CCL parser from Europagate Email gateway 1.0.
118 * Revision 1.16 1996/01/08 08:41:13 adam
119 * Removed unused function.
121 * Revision 1.15 1995/07/20 08:14:34 adam
122 * Qualifiers were observed too often. Instead tokens are treated as
123 * qualifiers only when separated by comma.
125 * Revision 1.14 1995/05/16 09:39:26 adam
128 * Revision 1.13 1995/04/17 09:31:42 adam
129 * Improved handling of qualifiers. Aliases or reserved words.
131 * Revision 1.12 1995/03/20 15:27:43 adam
134 * Revision 1.11 1995/02/23 08:31:59 adam
137 * Revision 1.9 1995/02/16 13:20:06 adam
140 * Revision 1.8 1995/02/14 19:59:42 adam
141 * Removed a syntax error.
143 * Revision 1.7 1995/02/14 19:55:10 adam
144 * Header files ccl.h/cclp.h are gone! They have been merged an
145 * moved to ../include/ccl.h.
146 * Node kind(s) in ccl_rpn_node have changed names.
148 * Revision 1.6 1995/02/14 16:20:55 adam
149 * Qualifiers are read from a file now.
151 * Revision 1.5 1995/02/14 14:12:41 adam
152 * Ranges for ordered qualfiers implemented (e.g. pd=1980-1990).
154 * Revision 1.4 1995/02/14 13:16:29 adam
155 * Left and/or right truncation implemented.
157 * Revision 1.3 1995/02/14 10:25:56 adam
158 * The constructions 'qualifier rel term ...' implemented.
160 * Revision 1.2 1995/02/13 15:15:07 adam
161 * Added handling of qualifiers. Not finished yet.
163 * Revision 1.1 1995/02/13 12:35:20 adam
164 * First version of CCL. Qualifiers aren't handled yet.
173 /* returns type of current lookahead */
174 #define KIND (cclp->look_token->kind)
176 /* move one token forward */
177 #define ADVANCE cclp->look_token = cclp->look_token->next
180 * qual_val_type: test for existance of attribute type/value pair.
181 * qa: Attribute array
182 * type: Type of attribute to search for
183 * value: Value of attribute to seach for
184 * return: 1 if found; 0 otherwise.
186 static int qual_val_type (struct ccl_rpn_attr **qa, int type, int value,
190 struct ccl_rpn_attr *q;
194 for (i = 0; (q=qa[i]); i++)
197 if (q->type == type && q->value == value)
209 * strxcat: concatenate strings.
210 * n: Null-terminated Destination string
211 * src: Source string to be appended (not null-terminated)
212 * len: Length of source string.
214 static void strxcat (char *n, const char *src, int len)
224 * copy_token_name: Return copy of CCL token name
225 * tp: Pointer to token info.
226 * return: malloc(3) allocated copy of token name.
228 static char *copy_token_name (struct ccl_token *tp)
230 char *str = (char *)malloc (tp->len + 1);
232 memcpy (str, tp->name, tp->len);
238 * mk_node: Create RPN node.
239 * kind: Type of node.
240 * return: pointer to allocated node.
242 static struct ccl_rpn_node *mk_node (int kind)
244 struct ccl_rpn_node *p;
245 p = (struct ccl_rpn_node *)malloc (sizeof(*p));
252 * ccl_rpn_delete: Delete RPN tree.
253 * rpn: Pointer to tree.
255 void ccl_rpn_delete (struct ccl_rpn_node *rpn)
257 struct ccl_rpn_attr *attr, *attr1;
265 ccl_rpn_delete (rpn->u.p[0]);
266 ccl_rpn_delete (rpn->u.p[1]);
269 free (rpn->u.t.term);
270 for (attr = rpn->u.t.attr_list; attr; attr = attr1)
279 free (rpn->u.setname);
282 ccl_rpn_delete (rpn->u.p[0]);
283 ccl_rpn_delete (rpn->u.p[1]);
289 static struct ccl_rpn_node *find_spec (CCL_parser cclp,
290 struct ccl_rpn_attr **qa);
292 static int is_term_ok (int look, int *list)
294 for (;*list >= 0; list++)
300 static struct ccl_rpn_node *search_terms (CCL_parser cclp,
301 struct ccl_rpn_attr **qa);
304 * add_attr: Add attribute (type/value) to RPN term node.
305 * p: RPN node of type term.
306 * type: Type of attribute
307 * value: Value of attribute
308 * set: Attribute set name
310 static void add_attr (struct ccl_rpn_node *p, const char *set,
313 struct ccl_rpn_attr *n;
315 n = (struct ccl_rpn_attr *)malloc (sizeof(*n));
319 n->set = (char*) malloc (strlen(set)+1);
320 strcpy (n->set, set);
326 n->next = p->u.t.attr_list;
327 p->u.t.attr_list = n;
331 * search_term: Parse CCL search term.
333 * qa: Qualifier attributes already applied.
334 * term_list: tokens we accept as terms in context
335 * multi: whether we accept "multiple" tokens
336 * return: pointer to node(s); NULL on error.
338 static struct ccl_rpn_node *search_term_x (CCL_parser cclp,
339 struct ccl_rpn_attr **qa,
340 int *term_list, int multi)
342 struct ccl_rpn_attr *qa_tmp[2];
343 struct ccl_rpn_node *p_top = 0;
344 struct ccl_token *lookahead = cclp->look_token;
348 const char *truncation_aliases;
351 ccl_qual_search_special(cclp->bibset, "truncation");
352 if (!truncation_aliases)
353 truncation_aliases = "?";
357 /* no qualifier(s) applied. Use 'term' if it is defined */
361 qa[0] = ccl_qual_search (cclp, "term", 4);
364 if (qual_val_type (qa, CCL_BIB1_STR, CCL_BIB1_STR_AND_LIST, 0))
366 if (qual_val_type (qa, CCL_BIB1_STR, CCL_BIB1_STR_OR_LIST, 0))
370 struct ccl_rpn_node *p;
376 int relation_value = -1;
377 int position_value = -1;
378 int structure_value = -1;
379 int truncation_value = -1;
380 int completeness_value = -1;
383 if (and_list || or_list || !multi)
386 /* go through each TERM token. If no truncation attribute is yet
387 met, then look for left/right truncation markers (?) and
388 set left_trunc/right_trunc/mid_trunc accordingly */
389 for (no = 0; no < max && is_term_ok(lookahead->kind, term_list); no++)
391 for (i = 0; i<lookahead->len; i++)
392 if (lookahead->name[i] == ' ')
394 else if (strchr(truncation_aliases, lookahead->name[i]))
396 if (no == 0 && i == 0 && lookahead->len >= 1)
398 else if (!is_term_ok(lookahead->next->kind, term_list) &&
399 i == lookahead->len-1 && i >= 1)
404 len += 1+lookahead->len;
405 lookahead = lookahead->next;
409 break; /* no more terms . stop . */
414 p = mk_node (CCL_RPN_OR);
416 p = mk_node (CCL_RPN_AND);
418 p = mk_node (CCL_RPN_AND);
423 /* create the term node, but wait a moment before adding the term */
424 p = mk_node (CCL_RPN_TERM);
425 p->u.t.attr_list = NULL;
428 /* make the top node point to us.. */
435 /* go through all attributes and add them to the attribute list */
436 for (i=0; qa && qa[i]; i++)
438 struct ccl_rpn_attr *attr;
440 for (attr = qa[i]; attr; attr = attr->next)
442 { /* deal only with REAL attributes (positive) */
446 if (relation_value != -1)
448 relation_value = attr->value;
451 if (position_value != -1)
453 position_value = attr->value;
456 if (structure_value != -1)
458 structure_value = attr->value;
461 if (truncation_value != -1)
463 truncation_value = attr->value;
464 left_trunc = right_trunc = mid_trunc = 0;
467 if (completeness_value != -1)
469 completeness_value = attr->value;
472 add_attr (p, attr->set, attr->type, attr->value);
475 /* len now holds the number of characters in the RPN term */
476 /* no holds the number of CCL tokens (1 or more) */
478 if (structure_value == -1 &&
479 qual_val_type (qa, CCL_BIB1_STR, CCL_BIB1_STR_WP, &attset))
480 { /* no structure attribute met. Apply either structure attribute
481 WORD or PHRASE depending on number of CCL tokens */
482 if (no == 1 && no_spaces == 0)
483 add_attr (p, attset, CCL_BIB1_STR, 2);
485 add_attr (p, attset, CCL_BIB1_STR, 1);
488 /* make the RPN token */
489 p->u.t.term = (char *)malloc (len);
490 ccl_assert (p->u.t.term);
491 p->u.t.term[0] = '\0';
492 for (i = 0; i<no; i++)
494 const char *src_str = cclp->look_token->name;
495 int src_len = cclp->look_token->len;
497 if (i == 0 && left_trunc)
502 else if (i == no-1 && right_trunc)
506 int len = strlen(p->u.t.term);
508 !strchr("-+", *src_str) &&
509 !strchr("-+", p->u.t.term[len-1]))
511 strcat (p->u.t.term, " ");
514 strxcat (p->u.t.term, src_str, src_len);
517 if (left_trunc && right_trunc)
519 if (!qual_val_type (qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_BOTH,
522 cclp->error_code = CCL_ERR_TRUNC_NOT_BOTH;
526 add_attr (p, attset, CCL_BIB1_TRU, 3);
528 else if (right_trunc)
530 if (!qual_val_type (qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_RIGHT,
533 cclp->error_code = CCL_ERR_TRUNC_NOT_RIGHT;
537 add_attr (p, attset, CCL_BIB1_TRU, 1);
541 if (!qual_val_type (qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_LEFT,
544 cclp->error_code = CCL_ERR_TRUNC_NOT_LEFT;
548 add_attr (p, attset, CCL_BIB1_TRU, 2);
552 if (qual_val_type (qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_NONE,
554 add_attr (p, attset, CCL_BIB1_TRU, 100);
560 cclp->error_code = CCL_ERR_TERM_EXPECTED;
564 static struct ccl_rpn_node *search_term (CCL_parser cclp,
565 struct ccl_rpn_attr **qa)
567 static int list[] = {CCL_TOK_TERM, CCL_TOK_COMMA, -1};
568 return search_term_x(cclp, qa, list, 0);
572 * qualifiers: Parse CCL qualifiers and search terms.
574 * la: Token pointer to RELATION token.
575 * qa: Qualifier attributes already applied.
576 * return: pointer to node(s); NULL on error.
578 static struct ccl_rpn_node *qualifiers (CCL_parser cclp, struct ccl_token *la,
579 struct ccl_rpn_attr **qa)
581 struct ccl_token *lookahead = cclp->look_token;
582 struct ccl_rpn_attr **ap;
589 cclp->error_code = CCL_ERR_DOUBLE_QUAL;
593 for (lookahead = cclp->look_token; lookahead != la;
594 lookahead=lookahead->next)
597 for (i=0; qa[i]; i++)
599 ap = (struct ccl_rpn_attr **)malloc ((no+1) * sizeof(*ap));
601 for (i = 0; cclp->look_token != la; i++)
603 ap[i] = ccl_qual_search (cclp, cclp->look_token->name,
604 cclp->look_token->len);
607 cclp->error_code = CCL_ERR_UNKNOWN_QUAL;
612 if (KIND == CCL_TOK_COMMA)
619 if (!qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_ORDER, &attset))
621 /* unordered relation */
622 struct ccl_rpn_node *p;
623 if (KIND != CCL_TOK_EQ)
625 cclp->error_code = CCL_ERR_EQ_EXPECTED;
630 if (KIND == CCL_TOK_LP)
633 if (!(p = find_spec (cclp, ap)))
638 if (KIND != CCL_TOK_RP)
640 cclp->error_code = CCL_ERR_RP_EXPECTED;
648 p = search_terms (cclp, ap);
652 /* ordered relation ... */
654 if (cclp->look_token->len == 1)
656 if (cclp->look_token->name[0] == '<')
658 else if (cclp->look_token->name[0] == '=')
660 else if (cclp->look_token->name[0] == '>')
663 else if (cclp->look_token->len == 2)
665 if (!memcmp (cclp->look_token->name, "<=", 2))
667 else if (!memcmp (cclp->look_token->name, ">=", 2))
669 else if (!memcmp (cclp->look_token->name, "<>", 2))
673 cclp->error_code = CCL_ERR_BAD_RELATION;
676 struct ccl_rpn_node *p;
678 ADVANCE; /* skip relation */
679 if (KIND == CCL_TOK_TERM &&
680 cclp->look_token->next && cclp->look_token->next->len == 1 &&
681 cclp->look_token->next->name[0] == '-')
683 struct ccl_rpn_node *p1;
684 if (!(p1 = search_term (cclp, ap)))
689 ADVANCE; /* skip '-' */
690 if (KIND == CCL_TOK_TERM) /* = term - term ? */
692 struct ccl_rpn_node *p2;
694 if (!(p2 = search_term (cclp, ap)))
700 p = mk_node (CCL_RPN_AND);
702 add_attr (p1, attset, CCL_BIB1_REL, 4);
704 add_attr (p2, attset, CCL_BIB1_REL, 2);
710 add_attr (p1, attset, CCL_BIB1_REL, 4);
715 else if (cclp->look_token->len == 1 &&
716 cclp->look_token->name[0] == '"') /* = - term ? */
719 if (!(p = search_term (cclp, ap)))
724 add_attr (p, attset, CCL_BIB1_REL, 2);
728 else if (KIND == CCL_TOK_LP)
731 if (!(p = find_spec (cclp, ap)))
736 if (KIND != CCL_TOK_RP)
738 cclp->error_code = CCL_ERR_RP_EXPECTED;
749 if (!(p = search_terms (cclp, ap)))
754 add_attr (p, attset, CCL_BIB1_REL, rel);
758 cclp->error_code = CCL_ERR_TERM_EXPECTED;
765 * search_terms: Parse CCL search terms - including proximity.
767 * qa: Qualifier attributes already applied.
768 * return: pointer to node(s); NULL on error.
770 static struct ccl_rpn_node *search_terms (CCL_parser cclp,
771 struct ccl_rpn_attr **qa)
773 static int list[] = {
774 CCL_TOK_TERM, CCL_TOK_COMMA,CCL_TOK_EQ, CCL_TOK_REL, -1};
775 struct ccl_rpn_node *p1, *p2, *pn;
776 p1 = search_term_x (cclp, qa, list, 1);
781 if (KIND == CCL_TOK_PROX)
784 p2 = search_term_x (cclp, qa, list, 1);
790 pn = mk_node (CCL_RPN_PROX);
795 else if (is_term_ok(KIND, list))
797 p2 = search_term_x (cclp, qa, list, 1);
803 pn = mk_node (CCL_RPN_PROX);
815 * search_elements: Parse CCL search elements
817 * qa: Qualifier attributes already applied.
818 * return: pointer to node(s); NULL on error.
820 static struct ccl_rpn_node *search_elements (CCL_parser cclp,
821 struct ccl_rpn_attr **qa)
823 struct ccl_rpn_node *p1;
824 struct ccl_token *lookahead;
825 if (KIND == CCL_TOK_LP)
828 p1 = find_spec (cclp, qa);
831 if (KIND != CCL_TOK_RP)
833 cclp->error_code = CCL_ERR_RP_EXPECTED;
840 else if (KIND == CCL_TOK_SET)
843 if (KIND == CCL_TOK_EQ)
845 if (KIND != CCL_TOK_TERM)
847 cclp->error_code = CCL_ERR_SETNAME_EXPECTED;
850 p1 = mk_node (CCL_RPN_SET);
851 p1->u.setname = copy_token_name (cclp->look_token);
855 lookahead = cclp->look_token;
857 while (lookahead->kind==CCL_TOK_TERM)
859 lookahead = lookahead->next;
860 if (lookahead->kind == CCL_TOK_REL || lookahead->kind == CCL_TOK_EQ)
861 return qualifiers (cclp, lookahead, qa);
862 if (lookahead->kind != CCL_TOK_COMMA)
864 lookahead = lookahead->next;
866 return search_terms (cclp, qa);
870 * find_spec: Parse CCL find specification
872 * qa: Qualifier attributes already applied.
873 * return: pointer to node(s); NULL on error.
875 static struct ccl_rpn_node *find_spec (CCL_parser cclp,
876 struct ccl_rpn_attr **qa)
878 struct ccl_rpn_node *p1, *p2, *pn;
879 if (!(p1 = search_elements (cclp, qa)))
887 p2 = search_elements (cclp, qa);
893 pn = mk_node (CCL_RPN_AND);
900 p2 = search_elements (cclp, qa);
906 pn = mk_node (CCL_RPN_OR);
913 p2 = search_elements (cclp, qa);
919 pn = mk_node (CCL_RPN_NOT);
930 struct ccl_rpn_node *ccl_parser_find (CCL_parser cclp, struct ccl_token *list)
932 struct ccl_rpn_node *p;
936 cclp->look_token = list;
937 p = find_spec (cclp, NULL);
938 if (p && KIND != CCL_TOK_EOL)
940 if (KIND == CCL_TOK_RP)
941 cclp->error_code = CCL_ERR_BAD_RP;
943 cclp->error_code = CCL_ERR_OP_EXPECTED;
947 cclp->error_pos = cclp->look_token->name;
949 cclp->error_code = CCL_ERR_OK;
951 cclp->error_code = cclp->error_code;
956 * ccl_find: Parse CCL find - token representation
957 * bibset: Bibset to be used for the parsing
958 * list: List of tokens
959 * error: Pointer to integer. Holds error no. on completion.
960 * pos: Pointer to char position. Holds approximate error position.
961 * return: RPN tree on successful completion; NULL otherwise.
963 struct ccl_rpn_node *ccl_find (CCL_bibset bibset, struct ccl_token *list,
964 int *error, const char **pos)
966 struct ccl_rpn_node *p;
967 CCL_parser cclp = ccl_parser_create ();
969 cclp->bibset = bibset;
971 p = ccl_parser_find (cclp, list);
973 *error = cclp->error_code;
974 *pos = cclp->error_pos;
976 ccl_parser_destroy (cclp);
982 * ccl_find_str: Parse CCL find - string representation
983 * bibset: Bibset to be used for the parsing
984 * str: String to be parsed
985 * error: Pointer to integer. Holds error no. on completion.
986 * pos: Pointer to char position. Holds approximate error position.
987 * return: RPN tree on successful completion; NULL otherwise.
989 struct ccl_rpn_node *ccl_find_str (CCL_bibset bibset, const char *str,
990 int *error, int *pos)
992 CCL_parser cclp = ccl_parser_create ();
993 struct ccl_token *list;
994 struct ccl_rpn_node *p;
996 cclp->bibset = bibset;
998 list = ccl_parser_tokenize (cclp, str);
999 p = ccl_parser_find (cclp, list);
1001 *error = cclp->error_code;
1003 *pos = cclp->error_pos - str;
1004 ccl_parser_destroy (cclp);
1005 ccl_token_del (list);