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.20 2000-11-16 13:03:12 adam
49 * Function ccl_rpn_query sets attributeSet to Bib-1.
51 * Revision 1.19 2000/11/16 09:58:02 adam
52 * Implemented local AttributeSet setting for CCL field maps.
54 * Revision 1.18 2000/10/17 19:50:28 adam
55 * Implemented and-list and or-list for CCL module.
57 * Revision 1.17 2000/05/01 09:36:50 adam
58 * Range operator only treated in ordered ranges so that minus (-) can be
59 * used for, say, the and-not operator.
61 * Revision 1.16 2000/03/14 09:06:11 adam
62 * Added POSIX threads support for frontend server.
64 * Revision 1.15 2000/02/24 23:49:13 adam
65 * Fixed memory allocation problem.
67 * Revision 1.14 2000/01/31 13:15:21 adam
68 * Removed uses of assert(3). Cleanup of ODR. CCL parser update so
69 * that some characters are not surrounded by spaces in resulting term.
72 * Revision 1.13 1999/12/22 13:13:32 adam
73 * Search terms may include "operators" without causing error.
75 * Revision 1.12 1999/11/30 13:47:11 adam
76 * Improved installation. Moved header files to include/yaz.
78 * Revision 1.11 1999/03/31 11:15:37 adam
79 * Fixed memory leaks in ccl_find_str and ccl_qual_rm.
81 * Revision 1.10 1998/02/11 11:53:33 adam
82 * Changed code so that it compiles as C++.
84 * Revision 1.9 1997/09/29 08:56:37 adam
85 * Changed CCL parser to be thread safe. New type, CCL_parser, declared
86 * and a create/destructers ccl_parser_create/ccl_parser/destory has
89 * Revision 1.8 1997/09/01 08:48:11 adam
90 * New windows NT/95 port using MSV5.0. Only a few changes made
93 * Revision 1.7 1997/05/14 06:53:26 adam
96 * Revision 1.6 1997/04/30 08:52:06 quinn
99 * Revision 1.5 1996/10/11 15:00:24 adam
100 * CCL parser from Europagate Email gateway 1.0.
102 * Revision 1.16 1996/01/08 08:41:13 adam
103 * Removed unused function.
105 * Revision 1.15 1995/07/20 08:14:34 adam
106 * Qualifiers were observed too often. Instead tokens are treated as
107 * qualifiers only when separated by comma.
109 * Revision 1.14 1995/05/16 09:39:26 adam
112 * Revision 1.13 1995/04/17 09:31:42 adam
113 * Improved handling of qualifiers. Aliases or reserved words.
115 * Revision 1.12 1995/03/20 15:27:43 adam
118 * Revision 1.11 1995/02/23 08:31:59 adam
121 * Revision 1.9 1995/02/16 13:20:06 adam
124 * Revision 1.8 1995/02/14 19:59:42 adam
125 * Removed a syntax error.
127 * Revision 1.7 1995/02/14 19:55:10 adam
128 * Header files ccl.h/cclp.h are gone! They have been merged an
129 * moved to ../include/ccl.h.
130 * Node kind(s) in ccl_rpn_node have changed names.
132 * Revision 1.6 1995/02/14 16:20:55 adam
133 * Qualifiers are read from a file now.
135 * Revision 1.5 1995/02/14 14:12:41 adam
136 * Ranges for ordered qualfiers implemented (e.g. pd=1980-1990).
138 * Revision 1.4 1995/02/14 13:16:29 adam
139 * Left and/or right truncation implemented.
141 * Revision 1.3 1995/02/14 10:25:56 adam
142 * The constructions 'qualifier rel term ...' implemented.
144 * Revision 1.2 1995/02/13 15:15:07 adam
145 * Added handling of qualifiers. Not finished yet.
147 * Revision 1.1 1995/02/13 12:35:20 adam
148 * First version of CCL. Qualifiers aren't handled yet.
157 /* returns type of current lookahead */
158 #define KIND (cclp->look_token->kind)
160 /* move one token forward */
161 #define ADVANCE cclp->look_token = cclp->look_token->next
164 * qual_val_type: test for existance of attribute type/value pair.
165 * qa: Attribute array
166 * type: Type of attribute to search for
167 * value: Value of attribute to seach for
168 * return: 1 if found; 0 otherwise.
170 static int qual_val_type (struct ccl_rpn_attr **qa, int type, int value,
174 struct ccl_rpn_attr *q;
178 for (i = 0; (q=qa[i]); i++)
181 if (q->type == type && q->value == value)
193 * strxcat: concatenate strings.
194 * n: Null-terminated Destination string
195 * src: Source string to be appended (not null-terminated)
196 * len: Length of source string.
198 static void strxcat (char *n, const char *src, int len)
208 * copy_token_name: Return copy of CCL token name
209 * tp: Pointer to token info.
210 * return: malloc(3) allocated copy of token name.
212 static char *copy_token_name (struct ccl_token *tp)
214 char *str = (char *)malloc (tp->len + 1);
216 memcpy (str, tp->name, tp->len);
222 * mk_node: Create RPN node.
223 * kind: Type of node.
224 * return: pointer to allocated node.
226 static struct ccl_rpn_node *mk_node (int kind)
228 struct ccl_rpn_node *p;
229 p = (struct ccl_rpn_node *)malloc (sizeof(*p));
236 * ccl_rpn_delete: Delete RPN tree.
237 * rpn: Pointer to tree.
239 void ccl_rpn_delete (struct ccl_rpn_node *rpn)
241 struct ccl_rpn_attr *attr, *attr1;
249 ccl_rpn_delete (rpn->u.p[0]);
250 ccl_rpn_delete (rpn->u.p[1]);
253 free (rpn->u.t.term);
254 for (attr = rpn->u.t.attr_list; attr; attr = attr1)
263 free (rpn->u.setname);
266 ccl_rpn_delete (rpn->u.p[0]);
267 ccl_rpn_delete (rpn->u.p[1]);
273 static struct ccl_rpn_node *find_spec (CCL_parser cclp,
274 struct ccl_rpn_attr **qa);
276 static int is_term_ok (int look, int *list)
278 for (;*list >= 0; list++)
284 static struct ccl_rpn_node *search_terms (CCL_parser cclp,
285 struct ccl_rpn_attr **qa);
288 * add_attr: Add attribute (type/value) to RPN term node.
289 * p: RPN node of type term.
290 * type: Type of attribute
291 * value: Value of attribute
292 * set: Attribute set name
294 static void add_attr (struct ccl_rpn_node *p, const char *set,
297 struct ccl_rpn_attr *n;
299 n = (struct ccl_rpn_attr *)malloc (sizeof(*n));
303 n->set = malloc (strlen(set)+1);
304 strcpy (n->set, set);
310 n->next = p->u.t.attr_list;
311 p->u.t.attr_list = n;
315 * search_term: Parse CCL search term.
317 * qa: Qualifier attributes already applied.
318 * return: pointer to node(s); NULL on error.
320 static struct ccl_rpn_node *search_term_x (CCL_parser cclp,
321 struct ccl_rpn_attr **qa,
324 struct ccl_rpn_attr *qa_tmp[2];
325 struct ccl_rpn_node *p_top = 0;
326 struct ccl_token *lookahead = cclp->look_token;
333 /* no qualifier(s) applied. Use 'term' if it is defined */
337 qa[0] = ccl_qual_search (cclp, "term", 4);
340 if (qual_val_type (qa, CCL_BIB1_STR, CCL_BIB1_STR_AND_LIST, 0))
342 if (qual_val_type (qa, CCL_BIB1_STR, CCL_BIB1_STR_OR_LIST, 0))
346 struct ccl_rpn_node *p;
351 int relation_value = -1;
352 int position_value = -1;
353 int structure_value = -1;
354 int truncation_value = -1;
355 int completeness_value = -1;
358 if (and_list || or_list)
361 /* go through each TERM token. If no truncation attribute is yet
362 met, then look for left/right truncation markers (?) and
363 set left_trunc/right_trunc/mid_trunc accordingly */
364 for (no = 0; no < max && is_term_ok(lookahead->kind, term_list); no++)
366 for (i = 0; i<lookahead->len; i++)
367 if (truncation_value == -1 && lookahead->name[i] == '?')
369 if (no == 0 && i == 0 && lookahead->len >= 1)
371 else if (!is_term_ok(lookahead->next->kind, term_list) &&
372 i == lookahead->len-1 && i >= 1)
377 len += 1+lookahead->len;
378 lookahead = lookahead->next;
382 break; /* no more terms . stop . */
387 p = mk_node (CCL_RPN_OR);
389 p = mk_node (CCL_RPN_AND);
391 p = mk_node (CCL_RPN_AND);
396 /* create the term node, but wait a moment before adding the term */
397 p = mk_node (CCL_RPN_TERM);
398 p->u.t.attr_list = NULL;
401 /* make the top node point to us.. */
408 /* go through all attributes and add them to the attribute list */
409 for (i=0; qa && qa[i]; i++)
411 struct ccl_rpn_attr *attr;
413 for (attr = qa[i]; attr; attr = attr->next)
415 { /* deal only with REAL attributes (positive) */
419 if (relation_value != -1)
421 relation_value = attr->value;
424 if (position_value != -1)
426 position_value = attr->value;
429 if (structure_value != -1)
431 structure_value = attr->value;
434 if (truncation_value != -1)
436 truncation_value = attr->value;
437 left_trunc = right_trunc = mid_trunc = 0;
440 if (completeness_value != -1)
442 completeness_value = attr->value;
445 add_attr (p, attr->set, attr->type, attr->value);
448 /* len now holds the number of characters in the RPN term */
449 /* no holds the number of CCL tokens (1 or more) */
451 if (structure_value == -1 &&
452 qual_val_type (qa, CCL_BIB1_STR, CCL_BIB1_STR_WP, &attset))
453 { /* no structure attribute met. Apply either structure attribute
454 WORD or PHRASE depending on number of CCL tokens */
456 add_attr (p, attset, CCL_BIB1_STR, 2);
458 add_attr (p, attset, CCL_BIB1_STR, 1);
461 /* make the RPN token */
462 p->u.t.term = (char *)malloc (len);
463 ccl_assert (p->u.t.term);
464 p->u.t.term[0] = '\0';
465 for (i = 0; i<no; i++)
467 const char *src_str = cclp->look_token->name;
468 int src_len = cclp->look_token->len;
470 if (i == 0 && left_trunc)
475 else if (i == no-1 && right_trunc)
479 int len = strlen(p->u.t.term);
481 !strchr("-+", *src_str) &&
482 !strchr("-+", p->u.t.term[len-1]))
484 strcat (p->u.t.term, " ");
487 strxcat (p->u.t.term, src_str, src_len);
490 if (left_trunc && right_trunc)
492 if (!qual_val_type (qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_BOTH,
495 cclp->error_code = CCL_ERR_TRUNC_NOT_BOTH;
499 add_attr (p, attset, CCL_BIB1_TRU, 3);
501 else if (right_trunc)
503 if (!qual_val_type (qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_RIGHT,
506 cclp->error_code = CCL_ERR_TRUNC_NOT_RIGHT;
510 add_attr (p, attset, CCL_BIB1_TRU, 1);
514 if (!qual_val_type (qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_LEFT,
517 cclp->error_code = CCL_ERR_TRUNC_NOT_LEFT;
521 add_attr (p, attset, CCL_BIB1_TRU, 2);
525 if (qual_val_type (qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_NONE,
527 add_attr (p, attset, CCL_BIB1_TRU, 100);
531 cclp->error_code = CCL_ERR_TERM_EXPECTED;
535 static struct ccl_rpn_node *search_term (CCL_parser cclp,
536 struct ccl_rpn_attr **qa)
538 static int list[] = {CCL_TOK_TERM, CCL_TOK_COMMA, -1};
539 return search_term_x(cclp, qa, list);
543 * qualifiers: Parse CCL qualifiers and search terms.
545 * la: Token pointer to RELATION token.
546 * qa: Qualifier attributes already applied.
547 * return: pointer to node(s); NULL on error.
549 static struct ccl_rpn_node *qualifiers (CCL_parser cclp, struct ccl_token *la,
550 struct ccl_rpn_attr **qa)
552 struct ccl_token *lookahead = cclp->look_token;
553 struct ccl_rpn_attr **ap;
560 cclp->error_code = CCL_ERR_DOUBLE_QUAL;
564 for (lookahead = cclp->look_token; lookahead != la;
565 lookahead=lookahead->next)
568 for (i=0; qa[i]; i++)
570 ap = (struct ccl_rpn_attr **)malloc ((no+1) * sizeof(*ap));
572 for (i = 0; cclp->look_token != la; i++)
574 ap[i] = ccl_qual_search (cclp, cclp->look_token->name,
575 cclp->look_token->len);
578 cclp->error_code = CCL_ERR_UNKNOWN_QUAL;
583 if (KIND == CCL_TOK_COMMA)
590 if (!qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_ORDER, &attset))
592 /* unordered relation */
593 struct ccl_rpn_node *p;
594 if (KIND != CCL_TOK_EQ)
596 cclp->error_code = CCL_ERR_EQ_EXPECTED;
601 if (KIND == CCL_TOK_LP)
604 if (!(p = find_spec (cclp, ap)))
609 if (KIND != CCL_TOK_RP)
611 cclp->error_code = CCL_ERR_RP_EXPECTED;
619 p = search_terms (cclp, ap);
623 /* ordered relation ... */
625 if (cclp->look_token->len == 1)
627 if (cclp->look_token->name[0] == '<')
629 else if (cclp->look_token->name[0] == '=')
631 else if (cclp->look_token->name[0] == '>')
634 else if (cclp->look_token->len == 2)
636 if (!memcmp (cclp->look_token->name, "<=", 2))
638 else if (!memcmp (cclp->look_token->name, ">=", 2))
640 else if (!memcmp (cclp->look_token->name, "<>", 2))
644 cclp->error_code = CCL_ERR_BAD_RELATION;
647 struct ccl_rpn_node *p;
649 ADVANCE; /* skip relation */
650 if (KIND == CCL_TOK_TERM &&
651 cclp->look_token->next->len == 1 &&
652 cclp->look_token->next->name[0] == '-')
654 struct ccl_rpn_node *p1;
655 if (!(p1 = search_term (cclp, ap)))
660 ADVANCE; /* skip '-' */
661 if (KIND == CCL_TOK_TERM) /* = term - term ? */
663 struct ccl_rpn_node *p2;
665 if (!(p2 = search_term (cclp, ap)))
671 p = mk_node (CCL_RPN_AND);
673 add_attr (p1, attset, CCL_BIB1_REL, 4);
675 add_attr (p2, attset, CCL_BIB1_REL, 2);
681 add_attr (p1, attset, CCL_BIB1_REL, 4);
686 else if (cclp->look_token->len == 1 &&
687 cclp->look_token->name[0] == '"') /* = - term ? */
690 if (!(p = search_term (cclp, ap)))
695 add_attr (p, attset, CCL_BIB1_REL, 2);
699 else if (KIND == CCL_TOK_LP)
702 if (!(p = find_spec (cclp, ap)))
707 if (KIND != CCL_TOK_RP)
709 cclp->error_code = CCL_ERR_RP_EXPECTED;
720 if (!(p = search_terms (cclp, ap)))
725 add_attr (p, attset, CCL_BIB1_REL, rel);
729 cclp->error_code = CCL_ERR_TERM_EXPECTED;
736 * search_terms: Parse CCL search terms - including proximity.
738 * qa: Qualifier attributes already applied.
739 * return: pointer to node(s); NULL on error.
741 static struct ccl_rpn_node *search_terms (CCL_parser cclp,
742 struct ccl_rpn_attr **qa)
744 static int list[] = {
745 CCL_TOK_TERM, CCL_TOK_COMMA,CCL_TOK_EQ, CCL_TOK_REL, -1};
746 struct ccl_rpn_node *p1, *p2, *pn;
747 p1 = search_term_x (cclp, qa, list);
752 if (KIND == CCL_TOK_PROX)
755 p2 = search_term_x (cclp, qa, list);
761 pn = mk_node (CCL_RPN_PROX);
766 else if (is_term_ok(KIND, list))
768 p2 = search_term_x (cclp, qa, list);
774 pn = mk_node (CCL_RPN_PROX);
786 * search_elements: Parse CCL search elements
788 * qa: Qualifier attributes already applied.
789 * return: pointer to node(s); NULL on error.
791 static struct ccl_rpn_node *search_elements (CCL_parser cclp,
792 struct ccl_rpn_attr **qa)
794 struct ccl_rpn_node *p1;
795 struct ccl_token *lookahead;
796 if (KIND == CCL_TOK_LP)
799 p1 = find_spec (cclp, qa);
802 if (KIND != CCL_TOK_RP)
804 cclp->error_code = CCL_ERR_RP_EXPECTED;
811 else if (KIND == CCL_TOK_SET)
814 if (KIND == CCL_TOK_EQ)
816 if (KIND != CCL_TOK_TERM)
818 cclp->error_code = CCL_ERR_SETNAME_EXPECTED;
821 p1 = mk_node (CCL_RPN_SET);
822 p1->u.setname = copy_token_name (cclp->look_token);
826 lookahead = cclp->look_token;
828 while (lookahead->kind==CCL_TOK_TERM)
830 lookahead = lookahead->next;
831 if (lookahead->kind == CCL_TOK_REL || lookahead->kind == CCL_TOK_EQ)
832 return qualifiers (cclp, lookahead, qa);
833 if (lookahead->kind != CCL_TOK_COMMA)
835 lookahead = lookahead->next;
837 return search_terms (cclp, qa);
841 * find_spec: Parse CCL find specification
843 * qa: Qualifier attributes already applied.
844 * return: pointer to node(s); NULL on error.
846 static struct ccl_rpn_node *find_spec (CCL_parser cclp,
847 struct ccl_rpn_attr **qa)
849 struct ccl_rpn_node *p1, *p2, *pn;
850 if (!(p1 = search_elements (cclp, qa)))
858 p2 = search_elements (cclp, qa);
864 pn = mk_node (CCL_RPN_AND);
871 p2 = search_elements (cclp, qa);
877 pn = mk_node (CCL_RPN_OR);
884 p2 = search_elements (cclp, qa);
890 pn = mk_node (CCL_RPN_NOT);
901 struct ccl_rpn_node *ccl_parser_find (CCL_parser cclp, struct ccl_token *list)
903 struct ccl_rpn_node *p;
905 cclp->look_token = list;
906 p = find_spec (cclp, NULL);
907 if (p && KIND != CCL_TOK_EOL)
909 if (KIND == CCL_TOK_RP)
910 cclp->error_code = CCL_ERR_BAD_RP;
912 cclp->error_code = CCL_ERR_OP_EXPECTED;
916 cclp->error_pos = cclp->look_token->name;
918 cclp->error_code = CCL_ERR_OK;
920 cclp->error_code = cclp->error_code;
925 * ccl_find: Parse CCL find - token representation
926 * bibset: Bibset to be used for the parsing
927 * list: List of tokens
928 * error: Pointer to integer. Holds error no. on completion.
929 * pos: Pointer to char position. Holds approximate error position.
930 * return: RPN tree on successful completion; NULL otherwise.
932 struct ccl_rpn_node *ccl_find (CCL_bibset bibset, struct ccl_token *list,
933 int *error, const char **pos)
935 struct ccl_rpn_node *p;
936 CCL_parser cclp = ccl_parser_create ();
938 cclp->bibset = bibset;
940 p = ccl_parser_find (cclp, list);
942 *error = cclp->error_code;
943 *pos = cclp->error_pos;
945 ccl_parser_destroy (cclp);
951 * ccl_find_str: Parse CCL find - string representation
952 * bibset: Bibset to be used for the parsing
953 * str: String to be parsed
954 * error: Pointer to integer. Holds error no. on completion.
955 * pos: Pointer to char position. Holds approximate error position.
956 * return: RPN tree on successful completion; NULL otherwise.
958 struct ccl_rpn_node *ccl_find_str (CCL_bibset bibset, const char *str,
959 int *error, int *pos)
961 CCL_parser cclp = ccl_parser_create ();
962 struct ccl_token *list;
963 struct ccl_rpn_node *p;
965 cclp->bibset = bibset;
967 list = ccl_parser_tokenize (cclp, str);
968 p = ccl_parser_find (cclp, list);
970 *error = cclp->error_code;
972 *pos = cclp->error_pos - str;
973 ccl_parser_destroy (cclp);
974 ccl_token_del (list);