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.18 2000-10-17 19:50:28 adam
49 * Implemented and-list and or-list for CCL module.
51 * Revision 1.17 2000/05/01 09:36:50 adam
52 * Range operator only treated in ordered ranges so that minus (-) can be
53 * used for, say, the and-not operator.
55 * Revision 1.16 2000/03/14 09:06:11 adam
56 * Added POSIX threads support for frontend server.
58 * Revision 1.15 2000/02/24 23:49:13 adam
59 * Fixed memory allocation problem.
61 * Revision 1.14 2000/01/31 13:15:21 adam
62 * Removed uses of assert(3). Cleanup of ODR. CCL parser update so
63 * that some characters are not surrounded by spaces in resulting term.
66 * Revision 1.13 1999/12/22 13:13:32 adam
67 * Search terms may include "operators" without causing error.
69 * Revision 1.12 1999/11/30 13:47:11 adam
70 * Improved installation. Moved header files to include/yaz.
72 * Revision 1.11 1999/03/31 11:15:37 adam
73 * Fixed memory leaks in ccl_find_str and ccl_qual_rm.
75 * Revision 1.10 1998/02/11 11:53:33 adam
76 * Changed code so that it compiles as C++.
78 * Revision 1.9 1997/09/29 08:56:37 adam
79 * Changed CCL parser to be thread safe. New type, CCL_parser, declared
80 * and a create/destructers ccl_parser_create/ccl_parser/destory has
83 * Revision 1.8 1997/09/01 08:48:11 adam
84 * New windows NT/95 port using MSV5.0. Only a few changes made
87 * Revision 1.7 1997/05/14 06:53:26 adam
90 * Revision 1.6 1997/04/30 08:52:06 quinn
93 * Revision 1.5 1996/10/11 15:00:24 adam
94 * CCL parser from Europagate Email gateway 1.0.
96 * Revision 1.16 1996/01/08 08:41:13 adam
97 * Removed unused function.
99 * Revision 1.15 1995/07/20 08:14:34 adam
100 * Qualifiers were observed too often. Instead tokens are treated as
101 * qualifiers only when separated by comma.
103 * Revision 1.14 1995/05/16 09:39:26 adam
106 * Revision 1.13 1995/04/17 09:31:42 adam
107 * Improved handling of qualifiers. Aliases or reserved words.
109 * Revision 1.12 1995/03/20 15:27:43 adam
112 * Revision 1.11 1995/02/23 08:31:59 adam
115 * Revision 1.9 1995/02/16 13:20:06 adam
118 * Revision 1.8 1995/02/14 19:59:42 adam
119 * Removed a syntax error.
121 * Revision 1.7 1995/02/14 19:55:10 adam
122 * Header files ccl.h/cclp.h are gone! They have been merged an
123 * moved to ../include/ccl.h.
124 * Node kind(s) in ccl_rpn_node have changed names.
126 * Revision 1.6 1995/02/14 16:20:55 adam
127 * Qualifiers are read from a file now.
129 * Revision 1.5 1995/02/14 14:12:41 adam
130 * Ranges for ordered qualfiers implemented (e.g. pd=1980-1990).
132 * Revision 1.4 1995/02/14 13:16:29 adam
133 * Left and/or right truncation implemented.
135 * Revision 1.3 1995/02/14 10:25:56 adam
136 * The constructions 'qualifier rel term ...' implemented.
138 * Revision 1.2 1995/02/13 15:15:07 adam
139 * Added handling of qualifiers. Not finished yet.
141 * Revision 1.1 1995/02/13 12:35:20 adam
142 * First version of CCL. Qualifiers aren't handled yet.
151 /* returns type of current lookahead */
152 #define KIND (cclp->look_token->kind)
154 /* move one token forward */
155 #define ADVANCE cclp->look_token = cclp->look_token->next
158 * qual_val_type: test for existance of attribute type/value pair.
159 * qa: Attribute array
160 * type: Type of attribute to search for
161 * value: Value of attribute to seach for
162 * return: 1 if found; 0 otherwise.
164 static int qual_val_type (struct ccl_rpn_attr **qa, int type, int value)
167 struct ccl_rpn_attr *q;
171 for (i = 0; (q=qa[i]); i++)
174 if (q->type == type && q->value == value)
182 * strxcat: concatenate strings.
183 * n: Null-terminated Destination string
184 * src: Source string to be appended (not null-terminated)
185 * len: Length of source string.
187 static void strxcat (char *n, const char *src, int len)
197 * copy_token_name: Return copy of CCL token name
198 * tp: Pointer to token info.
199 * return: malloc(3) allocated copy of token name.
201 static char *copy_token_name (struct ccl_token *tp)
203 char *str = (char *)malloc (tp->len + 1);
205 memcpy (str, tp->name, tp->len);
211 * mk_node: Create RPN node.
212 * kind: Type of node.
213 * return: pointer to allocated node.
215 static struct ccl_rpn_node *mk_node (int kind)
217 struct ccl_rpn_node *p;
218 p = (struct ccl_rpn_node *)malloc (sizeof(*p));
225 * ccl_rpn_delete: Delete RPN tree.
226 * rpn: Pointer to tree.
228 void ccl_rpn_delete (struct ccl_rpn_node *rpn)
230 struct ccl_rpn_attr *attr, *attr1;
238 ccl_rpn_delete (rpn->u.p[0]);
239 ccl_rpn_delete (rpn->u.p[1]);
242 free (rpn->u.t.term);
243 for (attr = rpn->u.t.attr_list; attr; attr = attr1)
250 free (rpn->u.setname);
253 ccl_rpn_delete (rpn->u.p[0]);
254 ccl_rpn_delete (rpn->u.p[1]);
260 static struct ccl_rpn_node *find_spec (CCL_parser cclp,
261 struct ccl_rpn_attr **qa);
263 static int is_term_ok (int look, int *list)
265 for (;*list >= 0; list++)
271 static struct ccl_rpn_node *search_terms (CCL_parser cclp,
272 struct ccl_rpn_attr **qa);
275 * add_attr: Add attribute (type/value) to RPN term node.
276 * p: RPN node of type term.
277 * type: Type of attribute
278 * value: Value of attribute
280 static void add_attr (struct ccl_rpn_node *p, int type, int value)
282 struct ccl_rpn_attr *n;
284 n = (struct ccl_rpn_attr *)malloc (sizeof(*n));
288 n->next = p->u.t.attr_list;
289 p->u.t.attr_list = n;
293 * search_term: Parse CCL search term.
295 * qa: Qualifier attributes already applied.
296 * return: pointer to node(s); NULL on error.
298 static struct ccl_rpn_node *search_term_x (CCL_parser cclp,
299 struct ccl_rpn_attr **qa,
302 struct ccl_rpn_attr *qa_tmp[2];
303 struct ccl_rpn_node *p_top = 0;
304 struct ccl_token *lookahead = cclp->look_token;
310 /* no qualifier(s) applied. Use 'term' if it is defined */
314 qa[0] = ccl_qual_search (cclp, "term", 4);
317 if (qual_val_type (qa, CCL_BIB1_STR, CCL_BIB1_STR_AND_LIST))
319 if (qual_val_type (qa, CCL_BIB1_STR, CCL_BIB1_STR_OR_LIST))
323 struct ccl_rpn_node *p;
328 int relation_value = -1;
329 int position_value = -1;
330 int structure_value = -1;
331 int truncation_value = -1;
332 int completeness_value = -1;
335 if (and_list || or_list)
338 /* go through each TERM token. If no truncation attribute is yet
339 met, then look for left/right truncation markers (?) and
340 set left_trunc/right_trunc/mid_trunc accordingly */
341 for (no = 0; no < max && is_term_ok(lookahead->kind, term_list); no++)
343 for (i = 0; i<lookahead->len; i++)
344 if (truncation_value == -1 && lookahead->name[i] == '?')
346 if (no == 0 && i == 0 && lookahead->len >= 1)
348 else if (!is_term_ok(lookahead->next->kind, term_list) &&
349 i == lookahead->len-1 && i >= 1)
354 len += 1+lookahead->len;
355 lookahead = lookahead->next;
359 break; /* no more terms . stop . */
364 p = mk_node (CCL_RPN_OR);
366 p = mk_node (CCL_RPN_AND);
368 p = mk_node (CCL_RPN_AND);
373 /* create the term node, but wait a moment before adding the term */
374 p = mk_node (CCL_RPN_TERM);
375 p->u.t.attr_list = NULL;
378 /* make the top node point to us.. */
385 /* go through all attributes and add them to the attribute list */
386 for (i=0; qa && qa[i]; i++)
388 struct ccl_rpn_attr *attr;
390 for (attr = qa[i]; attr; attr = attr->next)
392 { /* deal only with REAL attributes (positive) */
396 if (relation_value != -1)
398 relation_value = attr->value;
401 if (position_value != -1)
403 position_value = attr->value;
406 if (structure_value != -1)
408 structure_value = attr->value;
411 if (truncation_value != -1)
413 truncation_value = attr->value;
414 left_trunc = right_trunc = mid_trunc = 0;
417 if (completeness_value != -1)
419 completeness_value = attr->value;
422 add_attr (p, attr->type, attr->value);
425 /* len now holds the number of characters in the RPN term */
426 /* no holds the number of CCL tokens (1 or more) */
428 if (structure_value == -1 &&
429 qual_val_type (qa, CCL_BIB1_STR, CCL_BIB1_STR_WP))
430 { /* no structure attribute met. Apply either structure attribute
431 WORD or PHRASE depending on number of CCL tokens */
433 add_attr (p, CCL_BIB1_STR, 2);
435 add_attr (p, CCL_BIB1_STR, 1);
438 /* make the RPN token */
439 p->u.t.term = (char *)malloc (len);
440 ccl_assert (p->u.t.term);
441 p->u.t.term[0] = '\0';
442 for (i = 0; i<no; i++)
444 const char *src_str = cclp->look_token->name;
445 int src_len = cclp->look_token->len;
447 if (i == 0 && left_trunc)
452 else if (i == no-1 && right_trunc)
456 int len = strlen(p->u.t.term);
458 !strchr("-+", *src_str) &&
459 !strchr("-+", p->u.t.term[len-1]))
461 strcat (p->u.t.term, " ");
464 strxcat (p->u.t.term, src_str, src_len);
467 if (left_trunc && right_trunc)
469 if (!qual_val_type (qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_BOTH))
471 cclp->error_code = CCL_ERR_TRUNC_NOT_BOTH;
475 add_attr (p, CCL_BIB1_TRU, 3);
477 else if (right_trunc)
479 if (!qual_val_type (qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_RIGHT))
481 cclp->error_code = CCL_ERR_TRUNC_NOT_RIGHT;
485 add_attr (p, CCL_BIB1_TRU, 1);
489 if (!qual_val_type (qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_LEFT))
491 cclp->error_code = CCL_ERR_TRUNC_NOT_LEFT;
495 add_attr (p, CCL_BIB1_TRU, 2);
499 if (qual_val_type (qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_NONE))
500 add_attr (p, CCL_BIB1_TRU, 100);
504 cclp->error_code = CCL_ERR_TERM_EXPECTED;
508 static struct ccl_rpn_node *search_term (CCL_parser cclp,
509 struct ccl_rpn_attr **qa)
511 static int list[] = {CCL_TOK_TERM, CCL_TOK_COMMA, -1};
512 return search_term_x(cclp, qa, list);
516 * qualifiers: Parse CCL qualifiers and search terms.
518 * la: Token pointer to RELATION token.
519 * qa: Qualifier attributes already applied.
520 * return: pointer to node(s); NULL on error.
522 static struct ccl_rpn_node *qualifiers (CCL_parser cclp, struct ccl_token *la,
523 struct ccl_rpn_attr **qa)
525 struct ccl_token *lookahead = cclp->look_token;
526 struct ccl_rpn_attr **ap;
532 cclp->error_code = CCL_ERR_DOUBLE_QUAL;
536 for (lookahead = cclp->look_token; lookahead != la;
537 lookahead=lookahead->next)
540 for (i=0; qa[i]; i++)
542 ap = (struct ccl_rpn_attr **)malloc ((no+1) * sizeof(*ap));
544 for (i = 0; cclp->look_token != la; i++)
546 ap[i] = ccl_qual_search (cclp, cclp->look_token->name,
547 cclp->look_token->len);
550 cclp->error_code = CCL_ERR_UNKNOWN_QUAL;
555 if (KIND == CCL_TOK_COMMA)
562 if (!qual_val_type (ap, CCL_BIB1_REL, CCL_BIB1_REL_ORDER))
564 /* unordered relation */
565 struct ccl_rpn_node *p;
566 if (KIND != CCL_TOK_EQ)
568 cclp->error_code = CCL_ERR_EQ_EXPECTED;
573 if (KIND == CCL_TOK_LP)
576 if (!(p = find_spec (cclp, ap)))
581 if (KIND != CCL_TOK_RP)
583 cclp->error_code = CCL_ERR_RP_EXPECTED;
591 p = search_terms (cclp, ap);
595 /* ordered relation ... */
597 if (cclp->look_token->len == 1)
599 if (cclp->look_token->name[0] == '<')
601 else if (cclp->look_token->name[0] == '=')
603 else if (cclp->look_token->name[0] == '>')
606 else if (cclp->look_token->len == 2)
608 if (!memcmp (cclp->look_token->name, "<=", 2))
610 else if (!memcmp (cclp->look_token->name, ">=", 2))
612 else if (!memcmp (cclp->look_token->name, "<>", 2))
616 cclp->error_code = CCL_ERR_BAD_RELATION;
619 struct ccl_rpn_node *p;
621 ADVANCE; /* skip relation */
622 if (KIND == CCL_TOK_TERM &&
623 cclp->look_token->next->len == 1 &&
624 cclp->look_token->next->name[0] == '-')
626 struct ccl_rpn_node *p1;
627 if (!(p1 = search_term (cclp, ap)))
632 ADVANCE; /* skip '-' */
633 if (KIND == CCL_TOK_TERM) /* = term - term ? */
635 struct ccl_rpn_node *p2;
637 if (!(p2 = search_term (cclp, ap)))
643 p = mk_node (CCL_RPN_AND);
645 add_attr (p1, CCL_BIB1_REL, 4);
647 add_attr (p2, CCL_BIB1_REL, 2);
653 add_attr (p1, CCL_BIB1_REL, 4);
658 else if (cclp->look_token->len == 1 &&
659 cclp->look_token->name[0] == '"') /* = - term ? */
662 if (!(p = search_term (cclp, ap)))
667 add_attr (p, CCL_BIB1_REL, 2);
671 else if (KIND == CCL_TOK_LP)
674 if (!(p = find_spec (cclp, ap)))
679 if (KIND != CCL_TOK_RP)
681 cclp->error_code = CCL_ERR_RP_EXPECTED;
692 if (!(p = search_terms (cclp, ap)))
697 add_attr (p, CCL_BIB1_REL, rel);
701 cclp->error_code = CCL_ERR_TERM_EXPECTED;
708 * search_terms: Parse CCL search terms - including proximity.
710 * qa: Qualifier attributes already applied.
711 * return: pointer to node(s); NULL on error.
713 static struct ccl_rpn_node *search_terms (CCL_parser cclp,
714 struct ccl_rpn_attr **qa)
716 static int list[] = {
717 CCL_TOK_TERM, CCL_TOK_COMMA,CCL_TOK_EQ, CCL_TOK_REL, -1};
718 struct ccl_rpn_node *p1, *p2, *pn;
719 p1 = search_term_x (cclp, qa, list);
724 if (KIND == CCL_TOK_PROX)
727 p2 = search_term_x (cclp, qa, list);
733 pn = mk_node (CCL_RPN_PROX);
738 else if (is_term_ok(KIND, list))
740 p2 = search_term_x (cclp, qa, list);
746 pn = mk_node (CCL_RPN_PROX);
758 * search_elements: Parse CCL search elements
760 * qa: Qualifier attributes already applied.
761 * return: pointer to node(s); NULL on error.
763 static struct ccl_rpn_node *search_elements (CCL_parser cclp,
764 struct ccl_rpn_attr **qa)
766 struct ccl_rpn_node *p1;
767 struct ccl_token *lookahead;
768 if (KIND == CCL_TOK_LP)
771 p1 = find_spec (cclp, qa);
774 if (KIND != CCL_TOK_RP)
776 cclp->error_code = CCL_ERR_RP_EXPECTED;
783 else if (KIND == CCL_TOK_SET)
786 if (KIND == CCL_TOK_EQ)
788 if (KIND != CCL_TOK_TERM)
790 cclp->error_code = CCL_ERR_SETNAME_EXPECTED;
793 p1 = mk_node (CCL_RPN_SET);
794 p1->u.setname = copy_token_name (cclp->look_token);
798 lookahead = cclp->look_token;
800 while (lookahead->kind==CCL_TOK_TERM)
802 lookahead = lookahead->next;
803 if (lookahead->kind == CCL_TOK_REL || lookahead->kind == CCL_TOK_EQ)
804 return qualifiers (cclp, lookahead, qa);
805 if (lookahead->kind != CCL_TOK_COMMA)
807 lookahead = lookahead->next;
809 return search_terms (cclp, qa);
813 * find_spec: Parse CCL find specification
815 * qa: Qualifier attributes already applied.
816 * return: pointer to node(s); NULL on error.
818 static struct ccl_rpn_node *find_spec (CCL_parser cclp,
819 struct ccl_rpn_attr **qa)
821 struct ccl_rpn_node *p1, *p2, *pn;
822 if (!(p1 = search_elements (cclp, qa)))
830 p2 = search_elements (cclp, qa);
836 pn = mk_node (CCL_RPN_AND);
843 p2 = search_elements (cclp, qa);
849 pn = mk_node (CCL_RPN_OR);
856 p2 = search_elements (cclp, qa);
862 pn = mk_node (CCL_RPN_NOT);
873 struct ccl_rpn_node *ccl_parser_find (CCL_parser cclp, struct ccl_token *list)
875 struct ccl_rpn_node *p;
877 cclp->look_token = list;
878 p = find_spec (cclp, NULL);
879 if (p && KIND != CCL_TOK_EOL)
881 if (KIND == CCL_TOK_RP)
882 cclp->error_code = CCL_ERR_BAD_RP;
884 cclp->error_code = CCL_ERR_OP_EXPECTED;
888 cclp->error_pos = cclp->look_token->name;
890 cclp->error_code = CCL_ERR_OK;
892 cclp->error_code = cclp->error_code;
897 * ccl_find: Parse CCL find - token representation
898 * bibset: Bibset to be used for the parsing
899 * list: List of tokens
900 * error: Pointer to integer. Holds error no. on completion.
901 * pos: Pointer to char position. Holds approximate error position.
902 * return: RPN tree on successful completion; NULL otherwise.
904 struct ccl_rpn_node *ccl_find (CCL_bibset bibset, struct ccl_token *list,
905 int *error, const char **pos)
907 struct ccl_rpn_node *p;
908 CCL_parser cclp = ccl_parser_create ();
910 cclp->bibset = bibset;
912 p = ccl_parser_find (cclp, list);
914 *error = cclp->error_code;
915 *pos = cclp->error_pos;
917 ccl_parser_destroy (cclp);
923 * ccl_find_str: Parse CCL find - string representation
924 * bibset: Bibset to be used for the parsing
925 * str: String to be parsed
926 * error: Pointer to integer. Holds error no. on completion.
927 * pos: Pointer to char position. Holds approximate error position.
928 * return: RPN tree on successful completion; NULL otherwise.
930 struct ccl_rpn_node *ccl_find_str (CCL_bibset bibset, const char *str,
931 int *error, int *pos)
933 CCL_parser cclp = ccl_parser_create ();
934 struct ccl_token *list;
935 struct ccl_rpn_node *p;
937 cclp->bibset = bibset;
939 list = ccl_parser_tokenize (cclp, str);
940 p = ccl_parser_find (cclp, list);
942 *error = cclp->error_code;
944 *pos = cclp->error_pos - str;
945 ccl_parser_destroy (cclp);
946 ccl_token_del (list);