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.21 2001-02-21 13:46:53 adam
51 * Revision 1.20 2000/11/16 13:03:12 adam
52 * Function ccl_rpn_query sets attributeSet to Bib-1.
54 * Revision 1.19 2000/11/16 09:58:02 adam
55 * Implemented local AttributeSet setting for CCL field maps.
57 * Revision 1.18 2000/10/17 19:50:28 adam
58 * Implemented and-list and or-list for CCL module.
60 * Revision 1.17 2000/05/01 09:36:50 adam
61 * Range operator only treated in ordered ranges so that minus (-) can be
62 * used for, say, the and-not operator.
64 * Revision 1.16 2000/03/14 09:06:11 adam
65 * Added POSIX threads support for frontend server.
67 * Revision 1.15 2000/02/24 23:49:13 adam
68 * Fixed memory allocation problem.
70 * Revision 1.14 2000/01/31 13:15:21 adam
71 * Removed uses of assert(3). Cleanup of ODR. CCL parser update so
72 * that some characters are not surrounded by spaces in resulting term.
75 * Revision 1.13 1999/12/22 13:13:32 adam
76 * Search terms may include "operators" without causing error.
78 * Revision 1.12 1999/11/30 13:47:11 adam
79 * Improved installation. Moved header files to include/yaz.
81 * Revision 1.11 1999/03/31 11:15:37 adam
82 * Fixed memory leaks in ccl_find_str and ccl_qual_rm.
84 * Revision 1.10 1998/02/11 11:53:33 adam
85 * Changed code so that it compiles as C++.
87 * Revision 1.9 1997/09/29 08:56:37 adam
88 * Changed CCL parser to be thread safe. New type, CCL_parser, declared
89 * and a create/destructers ccl_parser_create/ccl_parser/destory has
92 * Revision 1.8 1997/09/01 08:48:11 adam
93 * New windows NT/95 port using MSV5.0. Only a few changes made
96 * Revision 1.7 1997/05/14 06:53:26 adam
99 * Revision 1.6 1997/04/30 08:52:06 quinn
102 * Revision 1.5 1996/10/11 15:00:24 adam
103 * CCL parser from Europagate Email gateway 1.0.
105 * Revision 1.16 1996/01/08 08:41:13 adam
106 * Removed unused function.
108 * Revision 1.15 1995/07/20 08:14:34 adam
109 * Qualifiers were observed too often. Instead tokens are treated as
110 * qualifiers only when separated by comma.
112 * Revision 1.14 1995/05/16 09:39:26 adam
115 * Revision 1.13 1995/04/17 09:31:42 adam
116 * Improved handling of qualifiers. Aliases or reserved words.
118 * Revision 1.12 1995/03/20 15:27:43 adam
121 * Revision 1.11 1995/02/23 08:31:59 adam
124 * Revision 1.9 1995/02/16 13:20:06 adam
127 * Revision 1.8 1995/02/14 19:59:42 adam
128 * Removed a syntax error.
130 * Revision 1.7 1995/02/14 19:55:10 adam
131 * Header files ccl.h/cclp.h are gone! They have been merged an
132 * moved to ../include/ccl.h.
133 * Node kind(s) in ccl_rpn_node have changed names.
135 * Revision 1.6 1995/02/14 16:20:55 adam
136 * Qualifiers are read from a file now.
138 * Revision 1.5 1995/02/14 14:12:41 adam
139 * Ranges for ordered qualfiers implemented (e.g. pd=1980-1990).
141 * Revision 1.4 1995/02/14 13:16:29 adam
142 * Left and/or right truncation implemented.
144 * Revision 1.3 1995/02/14 10:25:56 adam
145 * The constructions 'qualifier rel term ...' implemented.
147 * Revision 1.2 1995/02/13 15:15:07 adam
148 * Added handling of qualifiers. Not finished yet.
150 * Revision 1.1 1995/02/13 12:35:20 adam
151 * First version of CCL. Qualifiers aren't handled yet.
160 /* returns type of current lookahead */
161 #define KIND (cclp->look_token->kind)
163 /* move one token forward */
164 #define ADVANCE cclp->look_token = cclp->look_token->next
167 * qual_val_type: test for existance of attribute type/value pair.
168 * qa: Attribute array
169 * type: Type of attribute to search for
170 * value: Value of attribute to seach for
171 * return: 1 if found; 0 otherwise.
173 static int qual_val_type (struct ccl_rpn_attr **qa, int type, int value,
177 struct ccl_rpn_attr *q;
181 for (i = 0; (q=qa[i]); i++)
184 if (q->type == type && q->value == value)
196 * strxcat: concatenate strings.
197 * n: Null-terminated Destination string
198 * src: Source string to be appended (not null-terminated)
199 * len: Length of source string.
201 static void strxcat (char *n, const char *src, int len)
211 * copy_token_name: Return copy of CCL token name
212 * tp: Pointer to token info.
213 * return: malloc(3) allocated copy of token name.
215 static char *copy_token_name (struct ccl_token *tp)
217 char *str = (char *)malloc (tp->len + 1);
219 memcpy (str, tp->name, tp->len);
225 * mk_node: Create RPN node.
226 * kind: Type of node.
227 * return: pointer to allocated node.
229 static struct ccl_rpn_node *mk_node (int kind)
231 struct ccl_rpn_node *p;
232 p = (struct ccl_rpn_node *)malloc (sizeof(*p));
239 * ccl_rpn_delete: Delete RPN tree.
240 * rpn: Pointer to tree.
242 void ccl_rpn_delete (struct ccl_rpn_node *rpn)
244 struct ccl_rpn_attr *attr, *attr1;
252 ccl_rpn_delete (rpn->u.p[0]);
253 ccl_rpn_delete (rpn->u.p[1]);
256 free (rpn->u.t.term);
257 for (attr = rpn->u.t.attr_list; attr; attr = attr1)
266 free (rpn->u.setname);
269 ccl_rpn_delete (rpn->u.p[0]);
270 ccl_rpn_delete (rpn->u.p[1]);
276 static struct ccl_rpn_node *find_spec (CCL_parser cclp,
277 struct ccl_rpn_attr **qa);
279 static int is_term_ok (int look, int *list)
281 for (;*list >= 0; list++)
287 static struct ccl_rpn_node *search_terms (CCL_parser cclp,
288 struct ccl_rpn_attr **qa);
291 * add_attr: Add attribute (type/value) to RPN term node.
292 * p: RPN node of type term.
293 * type: Type of attribute
294 * value: Value of attribute
295 * set: Attribute set name
297 static void add_attr (struct ccl_rpn_node *p, const char *set,
300 struct ccl_rpn_attr *n;
302 n = (struct ccl_rpn_attr *)malloc (sizeof(*n));
306 n->set = (char*) malloc (strlen(set)+1);
307 strcpy (n->set, set);
313 n->next = p->u.t.attr_list;
314 p->u.t.attr_list = n;
318 * search_term: Parse CCL search term.
320 * qa: Qualifier attributes already applied.
321 * return: pointer to node(s); NULL on error.
323 static struct ccl_rpn_node *search_term_x (CCL_parser cclp,
324 struct ccl_rpn_attr **qa,
327 struct ccl_rpn_attr *qa_tmp[2];
328 struct ccl_rpn_node *p_top = 0;
329 struct ccl_token *lookahead = cclp->look_token;
336 /* no qualifier(s) applied. Use 'term' if it is defined */
340 qa[0] = ccl_qual_search (cclp, "term", 4);
343 if (qual_val_type (qa, CCL_BIB1_STR, CCL_BIB1_STR_AND_LIST, 0))
345 if (qual_val_type (qa, CCL_BIB1_STR, CCL_BIB1_STR_OR_LIST, 0))
349 struct ccl_rpn_node *p;
354 int relation_value = -1;
355 int position_value = -1;
356 int structure_value = -1;
357 int truncation_value = -1;
358 int completeness_value = -1;
361 if (and_list || or_list)
364 /* go through each TERM token. If no truncation attribute is yet
365 met, then look for left/right truncation markers (?) and
366 set left_trunc/right_trunc/mid_trunc accordingly */
367 for (no = 0; no < max && is_term_ok(lookahead->kind, term_list); no++)
369 for (i = 0; i<lookahead->len; i++)
370 if (truncation_value == -1 && lookahead->name[i] == '?')
372 if (no == 0 && i == 0 && lookahead->len >= 1)
374 else if (!is_term_ok(lookahead->next->kind, term_list) &&
375 i == lookahead->len-1 && i >= 1)
380 len += 1+lookahead->len;
381 lookahead = lookahead->next;
385 break; /* no more terms . stop . */
390 p = mk_node (CCL_RPN_OR);
392 p = mk_node (CCL_RPN_AND);
394 p = mk_node (CCL_RPN_AND);
399 /* create the term node, but wait a moment before adding the term */
400 p = mk_node (CCL_RPN_TERM);
401 p->u.t.attr_list = NULL;
404 /* make the top node point to us.. */
411 /* go through all attributes and add them to the attribute list */
412 for (i=0; qa && qa[i]; i++)
414 struct ccl_rpn_attr *attr;
416 for (attr = qa[i]; attr; attr = attr->next)
418 { /* deal only with REAL attributes (positive) */
422 if (relation_value != -1)
424 relation_value = attr->value;
427 if (position_value != -1)
429 position_value = attr->value;
432 if (structure_value != -1)
434 structure_value = attr->value;
437 if (truncation_value != -1)
439 truncation_value = attr->value;
440 left_trunc = right_trunc = mid_trunc = 0;
443 if (completeness_value != -1)
445 completeness_value = attr->value;
448 add_attr (p, attr->set, attr->type, attr->value);
451 /* len now holds the number of characters in the RPN term */
452 /* no holds the number of CCL tokens (1 or more) */
454 if (structure_value == -1 &&
455 qual_val_type (qa, CCL_BIB1_STR, CCL_BIB1_STR_WP, &attset))
456 { /* no structure attribute met. Apply either structure attribute
457 WORD or PHRASE depending on number of CCL tokens */
459 add_attr (p, attset, CCL_BIB1_STR, 2);
461 add_attr (p, attset, CCL_BIB1_STR, 1);
464 /* make the RPN token */
465 p->u.t.term = (char *)malloc (len);
466 ccl_assert (p->u.t.term);
467 p->u.t.term[0] = '\0';
468 for (i = 0; i<no; i++)
470 const char *src_str = cclp->look_token->name;
471 int src_len = cclp->look_token->len;
473 if (i == 0 && left_trunc)
478 else if (i == no-1 && right_trunc)
482 int len = strlen(p->u.t.term);
484 !strchr("-+", *src_str) &&
485 !strchr("-+", p->u.t.term[len-1]))
487 strcat (p->u.t.term, " ");
490 strxcat (p->u.t.term, src_str, src_len);
493 if (left_trunc && right_trunc)
495 if (!qual_val_type (qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_BOTH,
498 cclp->error_code = CCL_ERR_TRUNC_NOT_BOTH;
502 add_attr (p, attset, CCL_BIB1_TRU, 3);
504 else if (right_trunc)
506 if (!qual_val_type (qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_RIGHT,
509 cclp->error_code = CCL_ERR_TRUNC_NOT_RIGHT;
513 add_attr (p, attset, CCL_BIB1_TRU, 1);
517 if (!qual_val_type (qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_LEFT,
520 cclp->error_code = CCL_ERR_TRUNC_NOT_LEFT;
524 add_attr (p, attset, CCL_BIB1_TRU, 2);
528 if (qual_val_type (qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_NONE,
530 add_attr (p, attset, CCL_BIB1_TRU, 100);
534 cclp->error_code = CCL_ERR_TERM_EXPECTED;
538 static struct ccl_rpn_node *search_term (CCL_parser cclp,
539 struct ccl_rpn_attr **qa)
541 static int list[] = {CCL_TOK_TERM, CCL_TOK_COMMA, -1};
542 return search_term_x(cclp, qa, list);
546 * qualifiers: Parse CCL qualifiers and search terms.
548 * la: Token pointer to RELATION token.
549 * qa: Qualifier attributes already applied.
550 * return: pointer to node(s); NULL on error.
552 static struct ccl_rpn_node *qualifiers (CCL_parser cclp, struct ccl_token *la,
553 struct ccl_rpn_attr **qa)
555 struct ccl_token *lookahead = cclp->look_token;
556 struct ccl_rpn_attr **ap;
563 cclp->error_code = CCL_ERR_DOUBLE_QUAL;
567 for (lookahead = cclp->look_token; lookahead != la;
568 lookahead=lookahead->next)
571 for (i=0; qa[i]; i++)
573 ap = (struct ccl_rpn_attr **)malloc ((no+1) * sizeof(*ap));
575 for (i = 0; cclp->look_token != la; i++)
577 ap[i] = ccl_qual_search (cclp, cclp->look_token->name,
578 cclp->look_token->len);
581 cclp->error_code = CCL_ERR_UNKNOWN_QUAL;
586 if (KIND == CCL_TOK_COMMA)
593 if (!qual_val_type(ap, CCL_BIB1_REL, CCL_BIB1_REL_ORDER, &attset))
595 /* unordered relation */
596 struct ccl_rpn_node *p;
597 if (KIND != CCL_TOK_EQ)
599 cclp->error_code = CCL_ERR_EQ_EXPECTED;
604 if (KIND == CCL_TOK_LP)
607 if (!(p = find_spec (cclp, ap)))
612 if (KIND != CCL_TOK_RP)
614 cclp->error_code = CCL_ERR_RP_EXPECTED;
622 p = search_terms (cclp, ap);
626 /* ordered relation ... */
628 if (cclp->look_token->len == 1)
630 if (cclp->look_token->name[0] == '<')
632 else if (cclp->look_token->name[0] == '=')
634 else if (cclp->look_token->name[0] == '>')
637 else if (cclp->look_token->len == 2)
639 if (!memcmp (cclp->look_token->name, "<=", 2))
641 else if (!memcmp (cclp->look_token->name, ">=", 2))
643 else if (!memcmp (cclp->look_token->name, "<>", 2))
647 cclp->error_code = CCL_ERR_BAD_RELATION;
650 struct ccl_rpn_node *p;
652 ADVANCE; /* skip relation */
653 if (KIND == CCL_TOK_TERM &&
654 cclp->look_token->next->len == 1 &&
655 cclp->look_token->next->name[0] == '-')
657 struct ccl_rpn_node *p1;
658 if (!(p1 = search_term (cclp, ap)))
663 ADVANCE; /* skip '-' */
664 if (KIND == CCL_TOK_TERM) /* = term - term ? */
666 struct ccl_rpn_node *p2;
668 if (!(p2 = search_term (cclp, ap)))
674 p = mk_node (CCL_RPN_AND);
676 add_attr (p1, attset, CCL_BIB1_REL, 4);
678 add_attr (p2, attset, CCL_BIB1_REL, 2);
684 add_attr (p1, attset, CCL_BIB1_REL, 4);
689 else if (cclp->look_token->len == 1 &&
690 cclp->look_token->name[0] == '"') /* = - term ? */
693 if (!(p = search_term (cclp, ap)))
698 add_attr (p, attset, CCL_BIB1_REL, 2);
702 else if (KIND == CCL_TOK_LP)
705 if (!(p = find_spec (cclp, ap)))
710 if (KIND != CCL_TOK_RP)
712 cclp->error_code = CCL_ERR_RP_EXPECTED;
723 if (!(p = search_terms (cclp, ap)))
728 add_attr (p, attset, CCL_BIB1_REL, rel);
732 cclp->error_code = CCL_ERR_TERM_EXPECTED;
739 * search_terms: Parse CCL search terms - including proximity.
741 * qa: Qualifier attributes already applied.
742 * return: pointer to node(s); NULL on error.
744 static struct ccl_rpn_node *search_terms (CCL_parser cclp,
745 struct ccl_rpn_attr **qa)
747 static int list[] = {
748 CCL_TOK_TERM, CCL_TOK_COMMA,CCL_TOK_EQ, CCL_TOK_REL, -1};
749 struct ccl_rpn_node *p1, *p2, *pn;
750 p1 = search_term_x (cclp, qa, list);
755 if (KIND == CCL_TOK_PROX)
758 p2 = search_term_x (cclp, qa, list);
764 pn = mk_node (CCL_RPN_PROX);
769 else if (is_term_ok(KIND, list))
771 p2 = search_term_x (cclp, qa, list);
777 pn = mk_node (CCL_RPN_PROX);
789 * search_elements: Parse CCL search elements
791 * qa: Qualifier attributes already applied.
792 * return: pointer to node(s); NULL on error.
794 static struct ccl_rpn_node *search_elements (CCL_parser cclp,
795 struct ccl_rpn_attr **qa)
797 struct ccl_rpn_node *p1;
798 struct ccl_token *lookahead;
799 if (KIND == CCL_TOK_LP)
802 p1 = find_spec (cclp, qa);
805 if (KIND != CCL_TOK_RP)
807 cclp->error_code = CCL_ERR_RP_EXPECTED;
814 else if (KIND == CCL_TOK_SET)
817 if (KIND == CCL_TOK_EQ)
819 if (KIND != CCL_TOK_TERM)
821 cclp->error_code = CCL_ERR_SETNAME_EXPECTED;
824 p1 = mk_node (CCL_RPN_SET);
825 p1->u.setname = copy_token_name (cclp->look_token);
829 lookahead = cclp->look_token;
831 while (lookahead->kind==CCL_TOK_TERM)
833 lookahead = lookahead->next;
834 if (lookahead->kind == CCL_TOK_REL || lookahead->kind == CCL_TOK_EQ)
835 return qualifiers (cclp, lookahead, qa);
836 if (lookahead->kind != CCL_TOK_COMMA)
838 lookahead = lookahead->next;
840 return search_terms (cclp, qa);
844 * find_spec: Parse CCL find specification
846 * qa: Qualifier attributes already applied.
847 * return: pointer to node(s); NULL on error.
849 static struct ccl_rpn_node *find_spec (CCL_parser cclp,
850 struct ccl_rpn_attr **qa)
852 struct ccl_rpn_node *p1, *p2, *pn;
853 if (!(p1 = search_elements (cclp, qa)))
861 p2 = search_elements (cclp, qa);
867 pn = mk_node (CCL_RPN_AND);
874 p2 = search_elements (cclp, qa);
880 pn = mk_node (CCL_RPN_OR);
887 p2 = search_elements (cclp, qa);
893 pn = mk_node (CCL_RPN_NOT);
904 struct ccl_rpn_node *ccl_parser_find (CCL_parser cclp, struct ccl_token *list)
906 struct ccl_rpn_node *p;
908 cclp->look_token = list;
909 p = find_spec (cclp, NULL);
910 if (p && KIND != CCL_TOK_EOL)
912 if (KIND == CCL_TOK_RP)
913 cclp->error_code = CCL_ERR_BAD_RP;
915 cclp->error_code = CCL_ERR_OP_EXPECTED;
919 cclp->error_pos = cclp->look_token->name;
921 cclp->error_code = CCL_ERR_OK;
923 cclp->error_code = cclp->error_code;
928 * ccl_find: Parse CCL find - token representation
929 * bibset: Bibset to be used for the parsing
930 * list: List of tokens
931 * error: Pointer to integer. Holds error no. on completion.
932 * pos: Pointer to char position. Holds approximate error position.
933 * return: RPN tree on successful completion; NULL otherwise.
935 struct ccl_rpn_node *ccl_find (CCL_bibset bibset, struct ccl_token *list,
936 int *error, const char **pos)
938 struct ccl_rpn_node *p;
939 CCL_parser cclp = ccl_parser_create ();
941 cclp->bibset = bibset;
943 p = ccl_parser_find (cclp, list);
945 *error = cclp->error_code;
946 *pos = cclp->error_pos;
948 ccl_parser_destroy (cclp);
954 * ccl_find_str: Parse CCL find - string representation
955 * bibset: Bibset to be used for the parsing
956 * str: String to be parsed
957 * error: Pointer to integer. Holds error no. on completion.
958 * pos: Pointer to char position. Holds approximate error position.
959 * return: RPN tree on successful completion; NULL otherwise.
961 struct ccl_rpn_node *ccl_find_str (CCL_bibset bibset, const char *str,
962 int *error, int *pos)
964 CCL_parser cclp = ccl_parser_create ();
965 struct ccl_token *list;
966 struct ccl_rpn_node *p;
968 cclp->bibset = bibset;
970 list = ccl_parser_tokenize (cclp, str);
971 p = ccl_parser_find (cclp, list);
973 *error = cclp->error_code;
975 *pos = cclp->error_pos - str;
976 ccl_parser_destroy (cclp);
977 ccl_token_del (list);