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.24 2001-03-22 21:23:30 adam
49 * Directive s=pw sets structure to phrase if term includes blank(s).
51 * Revision 1.23 2001/03/20 11:22:58 adam
52 * CCL Truncation character may be defined.
54 * Revision 1.22 2001/03/07 13:24:40 adam
55 * Member and_not in Z_Operator is kept for backwards compatibility.
56 * Added support for definition of CCL operators in field spec file.
58 * Revision 1.21 2001/02/21 13:46:53 adam
61 * Revision 1.20 2000/11/16 13:03:12 adam
62 * Function ccl_rpn_query sets attributeSet to Bib-1.
64 * Revision 1.19 2000/11/16 09:58:02 adam
65 * Implemented local AttributeSet setting for CCL field maps.
67 * Revision 1.18 2000/10/17 19:50:28 adam
68 * Implemented and-list and or-list for CCL module.
70 * Revision 1.17 2000/05/01 09:36:50 adam
71 * Range operator only treated in ordered ranges so that minus (-) can be
72 * used for, say, the and-not operator.
74 * Revision 1.16 2000/03/14 09:06:11 adam
75 * Added POSIX threads support for frontend server.
77 * Revision 1.15 2000/02/24 23:49:13 adam
78 * Fixed memory allocation problem.
80 * Revision 1.14 2000/01/31 13:15:21 adam
81 * Removed uses of assert(3). Cleanup of ODR. CCL parser update so
82 * that some characters are not surrounded by spaces in resulting term.
85 * Revision 1.13 1999/12/22 13:13:32 adam
86 * Search terms may include "operators" without causing error.
88 * Revision 1.12 1999/11/30 13:47:11 adam
89 * Improved installation. Moved header files to include/yaz.
91 * Revision 1.11 1999/03/31 11:15:37 adam
92 * Fixed memory leaks in ccl_find_str and ccl_qual_rm.
94 * Revision 1.10 1998/02/11 11:53:33 adam
95 * Changed code so that it compiles as C++.
97 * Revision 1.9 1997/09/29 08:56:37 adam
98 * Changed CCL parser to be thread safe. New type, CCL_parser, declared
99 * and a create/destructers ccl_parser_create/ccl_parser/destory has
102 * Revision 1.8 1997/09/01 08:48:11 adam
103 * New windows NT/95 port using MSV5.0. Only a few changes made
106 * Revision 1.7 1997/05/14 06:53:26 adam
109 * Revision 1.6 1997/04/30 08:52:06 quinn
112 * Revision 1.5 1996/10/11 15:00:24 adam
113 * CCL parser from Europagate Email gateway 1.0.
115 * Revision 1.16 1996/01/08 08:41:13 adam
116 * Removed unused function.
118 * Revision 1.15 1995/07/20 08:14:34 adam
119 * Qualifiers were observed too often. Instead tokens are treated as
120 * qualifiers only when separated by comma.
122 * Revision 1.14 1995/05/16 09:39:26 adam
125 * Revision 1.13 1995/04/17 09:31:42 adam
126 * Improved handling of qualifiers. Aliases or reserved words.
128 * Revision 1.12 1995/03/20 15:27:43 adam
131 * Revision 1.11 1995/02/23 08:31:59 adam
134 * Revision 1.9 1995/02/16 13:20:06 adam
137 * Revision 1.8 1995/02/14 19:59:42 adam
138 * Removed a syntax error.
140 * Revision 1.7 1995/02/14 19:55:10 adam
141 * Header files ccl.h/cclp.h are gone! They have been merged an
142 * moved to ../include/ccl.h.
143 * Node kind(s) in ccl_rpn_node have changed names.
145 * Revision 1.6 1995/02/14 16:20:55 adam
146 * Qualifiers are read from a file now.
148 * Revision 1.5 1995/02/14 14:12:41 adam
149 * Ranges for ordered qualfiers implemented (e.g. pd=1980-1990).
151 * Revision 1.4 1995/02/14 13:16:29 adam
152 * Left and/or right truncation implemented.
154 * Revision 1.3 1995/02/14 10:25:56 adam
155 * The constructions 'qualifier rel term ...' implemented.
157 * Revision 1.2 1995/02/13 15:15:07 adam
158 * Added handling of qualifiers. Not finished yet.
160 * Revision 1.1 1995/02/13 12:35:20 adam
161 * First version of CCL. Qualifiers aren't handled yet.
170 /* returns type of current lookahead */
171 #define KIND (cclp->look_token->kind)
173 /* move one token forward */
174 #define ADVANCE cclp->look_token = cclp->look_token->next
177 * qual_val_type: test for existance of attribute type/value pair.
178 * qa: Attribute array
179 * type: Type of attribute to search for
180 * value: Value of attribute to seach for
181 * return: 1 if found; 0 otherwise.
183 static int qual_val_type (struct ccl_rpn_attr **qa, int type, int value,
187 struct ccl_rpn_attr *q;
191 for (i = 0; (q=qa[i]); i++)
194 if (q->type == type && q->value == value)
206 * strxcat: concatenate strings.
207 * n: Null-terminated Destination string
208 * src: Source string to be appended (not null-terminated)
209 * len: Length of source string.
211 static void strxcat (char *n, const char *src, int len)
221 * copy_token_name: Return copy of CCL token name
222 * tp: Pointer to token info.
223 * return: malloc(3) allocated copy of token name.
225 static char *copy_token_name (struct ccl_token *tp)
227 char *str = (char *)malloc (tp->len + 1);
229 memcpy (str, tp->name, tp->len);
235 * mk_node: Create RPN node.
236 * kind: Type of node.
237 * return: pointer to allocated node.
239 static struct ccl_rpn_node *mk_node (int kind)
241 struct ccl_rpn_node *p;
242 p = (struct ccl_rpn_node *)malloc (sizeof(*p));
249 * ccl_rpn_delete: Delete RPN tree.
250 * rpn: Pointer to tree.
252 void ccl_rpn_delete (struct ccl_rpn_node *rpn)
254 struct ccl_rpn_attr *attr, *attr1;
262 ccl_rpn_delete (rpn->u.p[0]);
263 ccl_rpn_delete (rpn->u.p[1]);
266 free (rpn->u.t.term);
267 for (attr = rpn->u.t.attr_list; attr; attr = attr1)
276 free (rpn->u.setname);
279 ccl_rpn_delete (rpn->u.p[0]);
280 ccl_rpn_delete (rpn->u.p[1]);
286 static struct ccl_rpn_node *find_spec (CCL_parser cclp,
287 struct ccl_rpn_attr **qa);
289 static int is_term_ok (int look, int *list)
291 for (;*list >= 0; list++)
297 static struct ccl_rpn_node *search_terms (CCL_parser cclp,
298 struct ccl_rpn_attr **qa);
301 * add_attr: Add attribute (type/value) to RPN term node.
302 * p: RPN node of type term.
303 * type: Type of attribute
304 * value: Value of attribute
305 * set: Attribute set name
307 static void add_attr (struct ccl_rpn_node *p, const char *set,
310 struct ccl_rpn_attr *n;
312 n = (struct ccl_rpn_attr *)malloc (sizeof(*n));
316 n->set = (char*) malloc (strlen(set)+1);
317 strcpy (n->set, set);
323 n->next = p->u.t.attr_list;
324 p->u.t.attr_list = n;
328 * search_term: Parse CCL search term.
330 * qa: Qualifier attributes already applied.
331 * return: pointer to node(s); NULL on error.
333 static struct ccl_rpn_node *search_term_x (CCL_parser cclp,
334 struct ccl_rpn_attr **qa,
337 struct ccl_rpn_attr *qa_tmp[2];
338 struct ccl_rpn_node *p_top = 0;
339 struct ccl_token *lookahead = cclp->look_token;
343 const char *truncation_aliases;
346 ccl_qual_search_special(cclp->bibset, "truncation");
347 if (!truncation_aliases)
348 truncation_aliases = "?";
352 /* no qualifier(s) applied. Use 'term' if it is defined */
356 qa[0] = ccl_qual_search (cclp, "term", 4);
359 if (qual_val_type (qa, CCL_BIB1_STR, CCL_BIB1_STR_AND_LIST, 0))
361 if (qual_val_type (qa, CCL_BIB1_STR, CCL_BIB1_STR_OR_LIST, 0))
365 struct ccl_rpn_node *p;
371 int relation_value = -1;
372 int position_value = -1;
373 int structure_value = -1;
374 int truncation_value = -1;
375 int completeness_value = -1;
378 if (and_list || or_list)
381 /* go through each TERM token. If no truncation attribute is yet
382 met, then look for left/right truncation markers (?) and
383 set left_trunc/right_trunc/mid_trunc accordingly */
384 for (no = 0; no < max && is_term_ok(lookahead->kind, term_list); no++)
386 for (i = 0; i<lookahead->len; i++)
387 if (lookahead->name[i] == ' ')
389 else if (strchr(truncation_aliases, lookahead->name[i]))
391 if (no == 0 && i == 0 && lookahead->len >= 1)
393 else if (!is_term_ok(lookahead->next->kind, term_list) &&
394 i == lookahead->len-1 && i >= 1)
399 len += 1+lookahead->len;
400 lookahead = lookahead->next;
404 break; /* no more terms . stop . */
409 p = mk_node (CCL_RPN_OR);
411 p = mk_node (CCL_RPN_AND);
413 p = mk_node (CCL_RPN_AND);
418 /* create the term node, but wait a moment before adding the term */
419 p = mk_node (CCL_RPN_TERM);
420 p->u.t.attr_list = NULL;
423 /* make the top node point to us.. */
430 /* go through all attributes and add them to the attribute list */
431 for (i=0; qa && qa[i]; i++)
433 struct ccl_rpn_attr *attr;
435 for (attr = qa[i]; attr; attr = attr->next)
437 { /* deal only with REAL attributes (positive) */
441 if (relation_value != -1)
443 relation_value = attr->value;
446 if (position_value != -1)
448 position_value = attr->value;
451 if (structure_value != -1)
453 structure_value = attr->value;
456 if (truncation_value != -1)
458 truncation_value = attr->value;
459 left_trunc = right_trunc = mid_trunc = 0;
462 if (completeness_value != -1)
464 completeness_value = attr->value;
467 add_attr (p, attr->set, attr->type, attr->value);
470 /* len now holds the number of characters in the RPN term */
471 /* no holds the number of CCL tokens (1 or more) */
473 if (structure_value == -1 &&
474 qual_val_type (qa, CCL_BIB1_STR, CCL_BIB1_STR_WP, &attset))
475 { /* no structure attribute met. Apply either structure attribute
476 WORD or PHRASE depending on number of CCL tokens */
477 if (no == 1 && no_spaces == 0)
478 add_attr (p, attset, CCL_BIB1_STR, 2);
480 add_attr (p, attset, CCL_BIB1_STR, 1);
483 /* make the RPN token */
484 p->u.t.term = (char *)malloc (len);
485 ccl_assert (p->u.t.term);
486 p->u.t.term[0] = '\0';
487 for (i = 0; i<no; i++)
489 const char *src_str = cclp->look_token->name;
490 int src_len = cclp->look_token->len;
492 if (i == 0 && left_trunc)
497 else if (i == no-1 && right_trunc)
501 int len = strlen(p->u.t.term);
503 !strchr("-+", *src_str) &&
504 !strchr("-+", p->u.t.term[len-1]))
506 strcat (p->u.t.term, " ");
509 strxcat (p->u.t.term, src_str, src_len);
512 if (left_trunc && right_trunc)
514 if (!qual_val_type (qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_BOTH,
517 cclp->error_code = CCL_ERR_TRUNC_NOT_BOTH;
521 add_attr (p, attset, CCL_BIB1_TRU, 3);
523 else if (right_trunc)
525 if (!qual_val_type (qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_RIGHT,
528 cclp->error_code = CCL_ERR_TRUNC_NOT_RIGHT;
532 add_attr (p, attset, CCL_BIB1_TRU, 1);
536 if (!qual_val_type (qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_LEFT,
539 cclp->error_code = CCL_ERR_TRUNC_NOT_LEFT;
543 add_attr (p, attset, CCL_BIB1_TRU, 2);
547 if (qual_val_type (qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_NONE,
549 add_attr (p, attset, CCL_BIB1_TRU, 100);
553 cclp->error_code = CCL_ERR_TERM_EXPECTED;
557 static struct ccl_rpn_node *search_term (CCL_parser cclp,
558 struct ccl_rpn_attr **qa)
560 static int list[] = {CCL_TOK_TERM, CCL_TOK_COMMA, -1};
561 return search_term_x(cclp, qa, list);
565 * qualifiers: Parse CCL qualifiers and search terms.
567 * la: Token pointer to RELATION token.
568 * qa: Qualifier attributes already applied.
569 * return: pointer to node(s); NULL on error.
571 static struct ccl_rpn_node *qualifiers (CCL_parser cclp, struct ccl_token *la,
572 struct ccl_rpn_attr **qa)
574 struct ccl_token *lookahead = cclp->look_token;
575 struct ccl_rpn_attr **ap;
582 cclp->error_code = CCL_ERR_DOUBLE_QUAL;
586 for (lookahead = cclp->look_token; lookahead != la;
587 lookahead=lookahead->next)
590 for (i=0; qa[i]; i++)
592 ap = (struct ccl_rpn_attr **)malloc ((no+1) * sizeof(*ap));
594 for (i = 0; cclp->look_token != la; i++)
596 ap[i] = ccl_qual_search (cclp, cclp->look_token->name,
597 cclp->look_token->len);
600 cclp->error_code = CCL_ERR_UNKNOWN_QUAL;
605 if (KIND == CCL_TOK_COMMA)
612 if (!qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_ORDER, &attset))
614 /* unordered relation */
615 struct ccl_rpn_node *p;
616 if (KIND != CCL_TOK_EQ)
618 cclp->error_code = CCL_ERR_EQ_EXPECTED;
623 if (KIND == CCL_TOK_LP)
626 if (!(p = find_spec (cclp, ap)))
631 if (KIND != CCL_TOK_RP)
633 cclp->error_code = CCL_ERR_RP_EXPECTED;
641 p = search_terms (cclp, ap);
645 /* ordered relation ... */
647 if (cclp->look_token->len == 1)
649 if (cclp->look_token->name[0] == '<')
651 else if (cclp->look_token->name[0] == '=')
653 else if (cclp->look_token->name[0] == '>')
656 else if (cclp->look_token->len == 2)
658 if (!memcmp (cclp->look_token->name, "<=", 2))
660 else if (!memcmp (cclp->look_token->name, ">=", 2))
662 else if (!memcmp (cclp->look_token->name, "<>", 2))
666 cclp->error_code = CCL_ERR_BAD_RELATION;
669 struct ccl_rpn_node *p;
671 ADVANCE; /* skip relation */
672 if (KIND == CCL_TOK_TERM &&
673 cclp->look_token->next->len == 1 &&
674 cclp->look_token->next->name[0] == '-')
676 struct ccl_rpn_node *p1;
677 if (!(p1 = search_term (cclp, ap)))
682 ADVANCE; /* skip '-' */
683 if (KIND == CCL_TOK_TERM) /* = term - term ? */
685 struct ccl_rpn_node *p2;
687 if (!(p2 = search_term (cclp, ap)))
693 p = mk_node (CCL_RPN_AND);
695 add_attr (p1, attset, CCL_BIB1_REL, 4);
697 add_attr (p2, attset, CCL_BIB1_REL, 2);
703 add_attr (p1, attset, CCL_BIB1_REL, 4);
708 else if (cclp->look_token->len == 1 &&
709 cclp->look_token->name[0] == '"') /* = - term ? */
712 if (!(p = search_term (cclp, ap)))
717 add_attr (p, attset, CCL_BIB1_REL, 2);
721 else if (KIND == CCL_TOK_LP)
724 if (!(p = find_spec (cclp, ap)))
729 if (KIND != CCL_TOK_RP)
731 cclp->error_code = CCL_ERR_RP_EXPECTED;
742 if (!(p = search_terms (cclp, ap)))
747 add_attr (p, attset, CCL_BIB1_REL, rel);
751 cclp->error_code = CCL_ERR_TERM_EXPECTED;
758 * search_terms: Parse CCL search terms - including proximity.
760 * qa: Qualifier attributes already applied.
761 * return: pointer to node(s); NULL on error.
763 static struct ccl_rpn_node *search_terms (CCL_parser cclp,
764 struct ccl_rpn_attr **qa)
766 static int list[] = {
767 CCL_TOK_TERM, CCL_TOK_COMMA,CCL_TOK_EQ, CCL_TOK_REL, -1};
768 struct ccl_rpn_node *p1, *p2, *pn;
769 p1 = search_term_x (cclp, qa, list);
774 if (KIND == CCL_TOK_PROX)
777 p2 = search_term_x (cclp, qa, list);
783 pn = mk_node (CCL_RPN_PROX);
788 else if (is_term_ok(KIND, list))
790 p2 = search_term_x (cclp, qa, list);
796 pn = mk_node (CCL_RPN_PROX);
808 * search_elements: Parse CCL search elements
810 * qa: Qualifier attributes already applied.
811 * return: pointer to node(s); NULL on error.
813 static struct ccl_rpn_node *search_elements (CCL_parser cclp,
814 struct ccl_rpn_attr **qa)
816 struct ccl_rpn_node *p1;
817 struct ccl_token *lookahead;
818 if (KIND == CCL_TOK_LP)
821 p1 = find_spec (cclp, qa);
824 if (KIND != CCL_TOK_RP)
826 cclp->error_code = CCL_ERR_RP_EXPECTED;
833 else if (KIND == CCL_TOK_SET)
836 if (KIND == CCL_TOK_EQ)
838 if (KIND != CCL_TOK_TERM)
840 cclp->error_code = CCL_ERR_SETNAME_EXPECTED;
843 p1 = mk_node (CCL_RPN_SET);
844 p1->u.setname = copy_token_name (cclp->look_token);
848 lookahead = cclp->look_token;
850 while (lookahead->kind==CCL_TOK_TERM)
852 lookahead = lookahead->next;
853 if (lookahead->kind == CCL_TOK_REL || lookahead->kind == CCL_TOK_EQ)
854 return qualifiers (cclp, lookahead, qa);
855 if (lookahead->kind != CCL_TOK_COMMA)
857 lookahead = lookahead->next;
859 return search_terms (cclp, qa);
863 * find_spec: Parse CCL find specification
865 * qa: Qualifier attributes already applied.
866 * return: pointer to node(s); NULL on error.
868 static struct ccl_rpn_node *find_spec (CCL_parser cclp,
869 struct ccl_rpn_attr **qa)
871 struct ccl_rpn_node *p1, *p2, *pn;
872 if (!(p1 = search_elements (cclp, qa)))
880 p2 = search_elements (cclp, qa);
886 pn = mk_node (CCL_RPN_AND);
893 p2 = search_elements (cclp, qa);
899 pn = mk_node (CCL_RPN_OR);
906 p2 = search_elements (cclp, qa);
912 pn = mk_node (CCL_RPN_NOT);
923 struct ccl_rpn_node *ccl_parser_find (CCL_parser cclp, struct ccl_token *list)
925 struct ccl_rpn_node *p;
929 cclp->look_token = list;
930 p = find_spec (cclp, NULL);
931 if (p && KIND != CCL_TOK_EOL)
933 if (KIND == CCL_TOK_RP)
934 cclp->error_code = CCL_ERR_BAD_RP;
936 cclp->error_code = CCL_ERR_OP_EXPECTED;
940 cclp->error_pos = cclp->look_token->name;
942 cclp->error_code = CCL_ERR_OK;
944 cclp->error_code = cclp->error_code;
949 * ccl_find: Parse CCL find - token representation
950 * bibset: Bibset to be used for the parsing
951 * list: List of tokens
952 * error: Pointer to integer. Holds error no. on completion.
953 * pos: Pointer to char position. Holds approximate error position.
954 * return: RPN tree on successful completion; NULL otherwise.
956 struct ccl_rpn_node *ccl_find (CCL_bibset bibset, struct ccl_token *list,
957 int *error, const char **pos)
959 struct ccl_rpn_node *p;
960 CCL_parser cclp = ccl_parser_create ();
962 cclp->bibset = bibset;
964 p = ccl_parser_find (cclp, list);
966 *error = cclp->error_code;
967 *pos = cclp->error_pos;
969 ccl_parser_destroy (cclp);
975 * ccl_find_str: Parse CCL find - string representation
976 * bibset: Bibset to be used for the parsing
977 * str: String to be parsed
978 * error: Pointer to integer. Holds error no. on completion.
979 * pos: Pointer to char position. Holds approximate error position.
980 * return: RPN tree on successful completion; NULL otherwise.
982 struct ccl_rpn_node *ccl_find_str (CCL_bibset bibset, const char *str,
983 int *error, int *pos)
985 CCL_parser cclp = ccl_parser_create ();
986 struct ccl_token *list;
987 struct ccl_rpn_node *p;
989 cclp->bibset = bibset;
991 list = ccl_parser_tokenize (cclp, str);
992 p = ccl_parser_find (cclp, list);
994 *error = cclp->error_code;
996 *pos = cclp->error_pos - str;
997 ccl_parser_destroy (cclp);
998 ccl_token_del (list);