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.17 2000-05-01 09:36:50 adam
49 * Range operator only treated in ordered ranges so that minus (-) can be
50 * used for, say, the and-not operator.
52 * Revision 1.16 2000/03/14 09:06:11 adam
53 * Added POSIX threads support for frontend server.
55 * Revision 1.15 2000/02/24 23:49:13 adam
56 * Fixed memory allocation problem.
58 * Revision 1.14 2000/01/31 13:15:21 adam
59 * Removed uses of assert(3). Cleanup of ODR. CCL parser update so
60 * that some characters are not surrounded by spaces in resulting term.
63 * Revision 1.13 1999/12/22 13:13:32 adam
64 * Search terms may include "operators" without causing error.
66 * Revision 1.12 1999/11/30 13:47:11 adam
67 * Improved installation. Moved header files to include/yaz.
69 * Revision 1.11 1999/03/31 11:15:37 adam
70 * Fixed memory leaks in ccl_find_str and ccl_qual_rm.
72 * Revision 1.10 1998/02/11 11:53:33 adam
73 * Changed code so that it compiles as C++.
75 * Revision 1.9 1997/09/29 08:56:37 adam
76 * Changed CCL parser to be thread safe. New type, CCL_parser, declared
77 * and a create/destructers ccl_parser_create/ccl_parser/destory has
80 * Revision 1.8 1997/09/01 08:48:11 adam
81 * New windows NT/95 port using MSV5.0. Only a few changes made
84 * Revision 1.7 1997/05/14 06:53:26 adam
87 * Revision 1.6 1997/04/30 08:52:06 quinn
90 * Revision 1.5 1996/10/11 15:00:24 adam
91 * CCL parser from Europagate Email gateway 1.0.
93 * Revision 1.16 1996/01/08 08:41:13 adam
94 * Removed unused function.
96 * Revision 1.15 1995/07/20 08:14:34 adam
97 * Qualifiers were observed too often. Instead tokens are treated as
98 * qualifiers only when separated by comma.
100 * Revision 1.14 1995/05/16 09:39:26 adam
103 * Revision 1.13 1995/04/17 09:31:42 adam
104 * Improved handling of qualifiers. Aliases or reserved words.
106 * Revision 1.12 1995/03/20 15:27:43 adam
109 * Revision 1.11 1995/02/23 08:31:59 adam
112 * Revision 1.9 1995/02/16 13:20:06 adam
115 * Revision 1.8 1995/02/14 19:59:42 adam
116 * Removed a syntax error.
118 * Revision 1.7 1995/02/14 19:55:10 adam
119 * Header files ccl.h/cclp.h are gone! They have been merged an
120 * moved to ../include/ccl.h.
121 * Node kind(s) in ccl_rpn_node have changed names.
123 * Revision 1.6 1995/02/14 16:20:55 adam
124 * Qualifiers are read from a file now.
126 * Revision 1.5 1995/02/14 14:12:41 adam
127 * Ranges for ordered qualfiers implemented (e.g. pd=1980-1990).
129 * Revision 1.4 1995/02/14 13:16:29 adam
130 * Left and/or right truncation implemented.
132 * Revision 1.3 1995/02/14 10:25:56 adam
133 * The constructions 'qualifier rel term ...' implemented.
135 * Revision 1.2 1995/02/13 15:15:07 adam
136 * Added handling of qualifiers. Not finished yet.
138 * Revision 1.1 1995/02/13 12:35:20 adam
139 * First version of CCL. Qualifiers aren't handled yet.
148 /* returns type of current lookahead */
149 #define KIND (cclp->look_token->kind)
151 /* move one token forward */
152 #define ADVANCE cclp->look_token = cclp->look_token->next
155 * qual_val_type: test for existance of attribute type/value pair.
156 * qa: Attribute array
157 * type: Type of attribute to search for
158 * value: Value of attribute to seach for
159 * return: 1 if found; 0 otherwise.
161 static int qual_val_type (struct ccl_rpn_attr **qa, int type, int value)
164 struct ccl_rpn_attr *q;
168 for (i = 0; (q=qa[i]); i++)
171 if (q->type == type && q->value == value)
179 * strxcat: concatenate strings.
180 * n: Null-terminated Destination string
181 * src: Source string to be appended (not null-terminated)
182 * len: Length of source string.
184 static void strxcat (char *n, const char *src, int len)
194 * copy_token_name: Return copy of CCL token name
195 * tp: Pointer to token info.
196 * return: malloc(3) allocated copy of token name.
198 static char *copy_token_name (struct ccl_token *tp)
200 char *str = (char *)malloc (tp->len + 1);
202 memcpy (str, tp->name, tp->len);
208 * mk_node: Create RPN node.
209 * kind: Type of node.
210 * return: pointer to allocated node.
212 static struct ccl_rpn_node *mk_node (int kind)
214 struct ccl_rpn_node *p;
215 p = (struct ccl_rpn_node *)malloc (sizeof(*p));
222 * ccl_rpn_delete: Delete RPN tree.
223 * rpn: Pointer to tree.
225 void ccl_rpn_delete (struct ccl_rpn_node *rpn)
227 struct ccl_rpn_attr *attr, *attr1;
235 ccl_rpn_delete (rpn->u.p[0]);
236 ccl_rpn_delete (rpn->u.p[1]);
239 free (rpn->u.t.term);
240 for (attr = rpn->u.t.attr_list; attr; attr = attr1)
247 free (rpn->u.setname);
250 ccl_rpn_delete (rpn->u.p[0]);
251 ccl_rpn_delete (rpn->u.p[1]);
257 static struct ccl_rpn_node *find_spec (CCL_parser cclp,
258 struct ccl_rpn_attr **qa);
260 static int is_term_ok (int look, int *list)
262 for (;*list >= 0; list++)
268 static struct ccl_rpn_node *search_terms (CCL_parser cclp,
269 struct ccl_rpn_attr **qa);
272 * add_attr: Add attribute (type/value) to RPN term node.
273 * p: RPN node of type term.
274 * type: Type of attribute
275 * value: Value of attribute
277 static void add_attr (struct ccl_rpn_node *p, int type, int value)
279 struct ccl_rpn_attr *n;
281 n = (struct ccl_rpn_attr *)malloc (sizeof(*n));
285 n->next = p->u.t.attr_list;
286 p->u.t.attr_list = n;
290 * search_term: Parse CCL search term.
292 * qa: Qualifier attributes already applied.
293 * return: pointer to node(s); NULL on error.
295 static struct ccl_rpn_node *search_term_x (CCL_parser cclp,
296 struct ccl_rpn_attr **qa,
299 struct ccl_rpn_attr *qa_tmp[2];
300 struct ccl_rpn_node *p;
301 struct ccl_token *lookahead = cclp->look_token;
307 int relation_value = -1;
308 int position_value = -1;
309 int structure_value = -1;
310 int truncation_value = -1;
311 int completeness_value = -1;
313 if (!is_term_ok(KIND, term_list))
315 cclp->error_code = CCL_ERR_TERM_EXPECTED;
318 /* create the term node, but wait a moment before adding the term */
319 p = mk_node (CCL_RPN_TERM);
320 p->u.t.attr_list = NULL;
325 /* no qualifier(s) applied. Use 'term' if it is defined */
329 qa[0] = ccl_qual_search (cclp, "term", 4);
333 /* go through all attributes and add them to the attribute list */
334 for (i=0; qa && qa[i]; i++)
336 struct ccl_rpn_attr *attr;
338 for (attr = qa[i]; attr; attr = attr->next)
340 { /* deal only with REAL attributes (positive) */
344 if (relation_value != -1)
346 relation_value = attr->value;
349 if (position_value != -1)
351 position_value = attr->value;
354 if (structure_value != -1)
356 structure_value = attr->value;
359 if (truncation_value != -1)
361 truncation_value = attr->value;
364 if (completeness_value != -1)
366 completeness_value = attr->value;
369 add_attr (p, attr->type, attr->value);
372 /* go through each TERM token. If no truncation attribute is yet
373 met, then look for left/right truncation markers (?) and
374 set left_trunc/right_trunc/mid_trunc accordingly */
375 for (no = 0; is_term_ok(lookahead->kind, term_list); no++)
377 for (i = 0; i<lookahead->len; i++)
378 if (truncation_value == -1 && lookahead->name[i] == '?')
380 if (no == 0 && i == 0 && lookahead->len >= 1)
382 else if (!is_term_ok(lookahead->next->kind, term_list) &&
383 i == lookahead->len-1 && i >= 1)
388 len += 1+lookahead->len;
389 lookahead = lookahead->next;
391 /* len now holds the number of characters in the RPN term */
392 /* no holds the number of CCL tokens (1 or more) */
394 if (structure_value == -1 &&
395 qual_val_type (qa, CCL_BIB1_STR, CCL_BIB1_STR_WP))
396 { /* no structure attribute met. Apply either structure attribute
397 WORD or PHRASE depending on number of CCL tokens */
399 add_attr (p, CCL_BIB1_STR, 2);
401 add_attr (p, CCL_BIB1_STR, 1);
404 /* make the RPN token */
405 p->u.t.term = (char *)malloc (len);
406 ccl_assert (p->u.t.term);
407 p->u.t.term[0] = '\0';
408 for (i = 0; i<no; i++)
410 const char *src_str = cclp->look_token->name;
411 int src_len = cclp->look_token->len;
413 if (i == 0 && left_trunc)
418 else if (i == no-1 && right_trunc)
422 int len = strlen(p->u.t.term);
424 !strchr("-+", *src_str) &&
425 !strchr("-+", p->u.t.term[len-1]))
427 strcat (p->u.t.term, " ");
430 strxcat (p->u.t.term, src_str, src_len);
433 if (left_trunc && right_trunc)
435 if (!qual_val_type (qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_BOTH))
437 cclp->error_code = CCL_ERR_TRUNC_NOT_BOTH;
441 add_attr (p, CCL_BIB1_TRU, 3);
443 else if (right_trunc)
445 if (!qual_val_type (qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_RIGHT))
447 cclp->error_code = CCL_ERR_TRUNC_NOT_RIGHT;
451 add_attr (p, CCL_BIB1_TRU, 1);
455 if (!qual_val_type (qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_LEFT))
457 cclp->error_code = CCL_ERR_TRUNC_NOT_LEFT;
461 add_attr (p, CCL_BIB1_TRU, 2);
465 if (qual_val_type (qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_NONE))
466 add_attr (p, CCL_BIB1_TRU, 100);
471 static struct ccl_rpn_node *search_term (CCL_parser cclp,
472 struct ccl_rpn_attr **qa)
474 static int list[] = {CCL_TOK_TERM, CCL_TOK_COMMA, -1};
475 return search_term_x(cclp, qa, list);
479 * qualifiers: Parse CCL qualifiers and search terms.
481 * la: Token pointer to RELATION token.
482 * qa: Qualifier attributes already applied.
483 * return: pointer to node(s); NULL on error.
485 static struct ccl_rpn_node *qualifiers (CCL_parser cclp, struct ccl_token *la,
486 struct ccl_rpn_attr **qa)
488 struct ccl_token *lookahead = cclp->look_token;
489 struct ccl_rpn_attr **ap;
495 cclp->error_code = CCL_ERR_DOUBLE_QUAL;
499 for (lookahead = cclp->look_token; lookahead != la;
500 lookahead=lookahead->next)
503 for (i=0; qa[i]; i++)
505 ap = (struct ccl_rpn_attr **)malloc ((no+1) * sizeof(*ap));
507 for (i = 0; cclp->look_token != la; i++)
509 ap[i] = ccl_qual_search (cclp, cclp->look_token->name,
510 cclp->look_token->len);
513 cclp->error_code = CCL_ERR_UNKNOWN_QUAL;
518 if (KIND == CCL_TOK_COMMA)
525 if (!qual_val_type (ap, CCL_BIB1_REL, CCL_BIB1_REL_ORDER))
527 /* unordered relation */
528 struct ccl_rpn_node *p;
529 if (KIND != CCL_TOK_EQ)
531 cclp->error_code = CCL_ERR_EQ_EXPECTED;
536 if (KIND == CCL_TOK_LP)
539 if (!(p = find_spec (cclp, ap)))
544 if (KIND != CCL_TOK_RP)
546 cclp->error_code = CCL_ERR_RP_EXPECTED;
554 p = search_terms (cclp, ap);
558 /* ordered relation ... */
560 if (cclp->look_token->len == 1)
562 if (cclp->look_token->name[0] == '<')
564 else if (cclp->look_token->name[0] == '=')
566 else if (cclp->look_token->name[0] == '>')
569 else if (cclp->look_token->len == 2)
571 if (!memcmp (cclp->look_token->name, "<=", 2))
573 else if (!memcmp (cclp->look_token->name, ">=", 2))
575 else if (!memcmp (cclp->look_token->name, "<>", 2))
579 cclp->error_code = CCL_ERR_BAD_RELATION;
582 struct ccl_rpn_node *p;
584 ADVANCE; /* skip relation */
585 if (KIND == CCL_TOK_TERM &&
586 cclp->look_token->next->len == 1 &&
587 cclp->look_token->next->name[0] == '-')
589 struct ccl_rpn_node *p1;
590 if (!(p1 = search_term (cclp, ap)))
595 ADVANCE; /* skip '-' */
596 if (KIND == CCL_TOK_TERM) /* = term - term ? */
598 struct ccl_rpn_node *p2;
600 if (!(p2 = search_term (cclp, ap)))
606 p = mk_node (CCL_RPN_AND);
608 add_attr (p1, CCL_BIB1_REL, 4);
610 add_attr (p2, CCL_BIB1_REL, 2);
616 add_attr (p1, CCL_BIB1_REL, 4);
621 else if (cclp->look_token->len == 1 &&
622 cclp->look_token->name[0] == '"') /* = - term ? */
625 if (!(p = search_term (cclp, ap)))
630 add_attr (p, CCL_BIB1_REL, 2);
634 else if (KIND == CCL_TOK_LP)
637 if (!(p = find_spec (cclp, ap)))
642 if (KIND != CCL_TOK_RP)
644 cclp->error_code = CCL_ERR_RP_EXPECTED;
655 if (!(p = search_terms (cclp, ap)))
660 add_attr (p, CCL_BIB1_REL, rel);
664 cclp->error_code = CCL_ERR_TERM_EXPECTED;
671 * search_terms: Parse CCL search terms - including proximity.
673 * qa: Qualifier attributes already applied.
674 * return: pointer to node(s); NULL on error.
676 static struct ccl_rpn_node *search_terms (CCL_parser cclp,
677 struct ccl_rpn_attr **qa)
679 static int list[] = {
680 CCL_TOK_TERM, CCL_TOK_COMMA,CCL_TOK_EQ, CCL_TOK_REL, -1};
681 struct ccl_rpn_node *p1, *p2, *pn;
682 p1 = search_term_x (cclp, qa, list);
687 if (KIND == CCL_TOK_PROX)
690 p2 = search_term_x (cclp, qa, list);
696 pn = mk_node (CCL_RPN_PROX);
701 else if (is_term_ok(KIND, list))
703 p2 = search_term_x (cclp, qa, list);
709 pn = mk_node (CCL_RPN_PROX);
721 * search_elements: Parse CCL search elements
723 * qa: Qualifier attributes already applied.
724 * return: pointer to node(s); NULL on error.
726 static struct ccl_rpn_node *search_elements (CCL_parser cclp,
727 struct ccl_rpn_attr **qa)
729 struct ccl_rpn_node *p1;
730 struct ccl_token *lookahead;
731 if (KIND == CCL_TOK_LP)
734 p1 = find_spec (cclp, qa);
737 if (KIND != CCL_TOK_RP)
739 cclp->error_code = CCL_ERR_RP_EXPECTED;
746 else if (KIND == CCL_TOK_SET)
749 if (KIND == CCL_TOK_EQ)
751 if (KIND != CCL_TOK_TERM)
753 cclp->error_code = CCL_ERR_SETNAME_EXPECTED;
756 p1 = mk_node (CCL_RPN_SET);
757 p1->u.setname = copy_token_name (cclp->look_token);
761 lookahead = cclp->look_token;
763 while (lookahead->kind==CCL_TOK_TERM)
765 lookahead = lookahead->next;
766 if (lookahead->kind == CCL_TOK_REL || lookahead->kind == CCL_TOK_EQ)
767 return qualifiers (cclp, lookahead, qa);
768 if (lookahead->kind != CCL_TOK_COMMA)
770 lookahead = lookahead->next;
772 return search_terms (cclp, qa);
776 * find_spec: Parse CCL find specification
778 * qa: Qualifier attributes already applied.
779 * return: pointer to node(s); NULL on error.
781 static struct ccl_rpn_node *find_spec (CCL_parser cclp,
782 struct ccl_rpn_attr **qa)
784 struct ccl_rpn_node *p1, *p2, *pn;
785 if (!(p1 = search_elements (cclp, qa)))
793 p2 = search_elements (cclp, qa);
799 pn = mk_node (CCL_RPN_AND);
806 p2 = search_elements (cclp, qa);
812 pn = mk_node (CCL_RPN_OR);
819 p2 = search_elements (cclp, qa);
825 pn = mk_node (CCL_RPN_NOT);
836 struct ccl_rpn_node *ccl_parser_find (CCL_parser cclp, struct ccl_token *list)
838 struct ccl_rpn_node *p;
840 cclp->look_token = list;
841 p = find_spec (cclp, NULL);
842 if (p && KIND != CCL_TOK_EOL)
844 if (KIND == CCL_TOK_RP)
845 cclp->error_code = CCL_ERR_BAD_RP;
847 cclp->error_code = CCL_ERR_OP_EXPECTED;
851 cclp->error_pos = cclp->look_token->name;
853 cclp->error_code = CCL_ERR_OK;
855 cclp->error_code = cclp->error_code;
860 * ccl_find: Parse CCL find - token representation
861 * bibset: Bibset to be used for the parsing
862 * list: List of tokens
863 * error: Pointer to integer. Holds error no. on completion.
864 * pos: Pointer to char position. Holds approximate error position.
865 * return: RPN tree on successful completion; NULL otherwise.
867 struct ccl_rpn_node *ccl_find (CCL_bibset bibset, struct ccl_token *list,
868 int *error, const char **pos)
870 struct ccl_rpn_node *p;
871 CCL_parser cclp = ccl_parser_create ();
873 cclp->bibset = bibset;
875 p = ccl_parser_find (cclp, list);
877 *error = cclp->error_code;
878 *pos = cclp->error_pos;
880 ccl_parser_destroy (cclp);
886 * ccl_find_str: Parse CCL find - string representation
887 * bibset: Bibset to be used for the parsing
888 * str: String to be parsed
889 * error: Pointer to integer. Holds error no. on completion.
890 * pos: Pointer to char position. Holds approximate error position.
891 * return: RPN tree on successful completion; NULL otherwise.
893 struct ccl_rpn_node *ccl_find_str (CCL_bibset bibset, const char *str,
894 int *error, int *pos)
896 CCL_parser cclp = ccl_parser_create ();
897 struct ccl_token *list;
898 struct ccl_rpn_node *p;
900 cclp->bibset = bibset;
902 list = ccl_parser_tokenize (cclp, str);
903 p = ccl_parser_find (cclp, list);
905 *error = cclp->error_code;
907 *pos = cclp->error_pos - str;
908 ccl_parser_destroy (cclp);
909 ccl_token_del (list);