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.11 1999-03-31 11:15:37 adam
49 * Fixed memory leaks in ccl_find_str and ccl_qual_rm.
51 * Revision 1.10 1998/02/11 11:53:33 adam
52 * Changed code so that it compiles as C++.
54 * Revision 1.9 1997/09/29 08:56:37 adam
55 * Changed CCL parser to be thread safe. New type, CCL_parser, declared
56 * and a create/destructers ccl_parser_create/ccl_parser/destory has
59 * Revision 1.8 1997/09/01 08:48:11 adam
60 * New windows NT/95 port using MSV5.0. Only a few changes made
63 * Revision 1.7 1997/05/14 06:53:26 adam
66 * Revision 1.6 1997/04/30 08:52:06 quinn
69 * Revision 1.5 1996/10/11 15:00:24 adam
70 * CCL parser from Europagate Email gateway 1.0.
72 * Revision 1.16 1996/01/08 08:41:13 adam
73 * Removed unused function.
75 * Revision 1.15 1995/07/20 08:14:34 adam
76 * Qualifiers were observed too often. Instead tokens are treated as
77 * qualifiers only when separated by comma.
79 * Revision 1.14 1995/05/16 09:39:26 adam
82 * Revision 1.13 1995/04/17 09:31:42 adam
83 * Improved handling of qualifiers. Aliases or reserved words.
85 * Revision 1.12 1995/03/20 15:27:43 adam
88 * Revision 1.11 1995/02/23 08:31:59 adam
91 * Revision 1.9 1995/02/16 13:20:06 adam
94 * Revision 1.8 1995/02/14 19:59:42 adam
95 * Removed a syntax error.
97 * Revision 1.7 1995/02/14 19:55:10 adam
98 * Header files ccl.h/cclp.h are gone! They have been merged an
99 * moved to ../include/ccl.h.
100 * Node kind(s) in ccl_rpn_node have changed names.
102 * Revision 1.6 1995/02/14 16:20:55 adam
103 * Qualifiers are read from a file now.
105 * Revision 1.5 1995/02/14 14:12:41 adam
106 * Ranges for ordered qualfiers implemented (e.g. pd=1980-1990).
108 * Revision 1.4 1995/02/14 13:16:29 adam
109 * Left and/or right truncation implemented.
111 * Revision 1.3 1995/02/14 10:25:56 adam
112 * The constructions 'qualifier rel term ...' implemented.
114 * Revision 1.2 1995/02/13 15:15:07 adam
115 * Added handling of qualifiers. Not finished yet.
117 * Revision 1.1 1995/02/13 12:35:20 adam
118 * First version of CCL. Qualifiers aren't handled yet.
129 /* returns type of current lookahead */
130 #define KIND (cclp->look_token->kind)
132 /* move one token forward */
133 #define ADVANCE cclp->look_token = cclp->look_token->next
136 * qual_val_type: test for existance of attribute type/value pair.
137 * qa: Attribute array
138 * type: Type of attribute to search for
139 * value: Value of attribute to seach for
140 * return: 1 if found; 0 otherwise.
142 static int qual_val_type (struct ccl_rpn_attr **qa, int type, int value)
145 struct ccl_rpn_attr *q;
149 for (i = 0; (q=qa[i]); i++)
152 if (q->type == type && q->value == value)
160 * strxcat: concatenate strings.
161 * n: Null-terminated Destination string
162 * src: Source string to be appended (not null-terminated)
163 * len: Length of source string.
165 static void strxcat (char *n, const char *src, int len)
175 * copy_token_name: Return copy of CCL token name
176 * tp: Pointer to token info.
177 * return: malloc(3) allocated copy of token name.
179 static char *copy_token_name (struct ccl_token *tp)
181 char *str = (char *)malloc (tp->len + 1);
183 memcpy (str, tp->name, tp->len);
189 * mk_node: Create RPN node.
190 * kind: Type of node.
191 * return: pointer to allocated node.
193 static struct ccl_rpn_node *mk_node (int kind)
195 struct ccl_rpn_node *p;
196 p = (struct ccl_rpn_node *)malloc (sizeof(*p));
203 * ccl_rpn_delete: Delete RPN tree.
204 * rpn: Pointer to tree.
206 void ccl_rpn_delete (struct ccl_rpn_node *rpn)
208 struct ccl_rpn_attr *attr, *attr1;
216 ccl_rpn_delete (rpn->u.p[0]);
217 ccl_rpn_delete (rpn->u.p[1]);
220 free (rpn->u.t.term);
221 for (attr = rpn->u.t.attr_list; attr; attr = attr1)
228 free (rpn->u.setname);
231 ccl_rpn_delete (rpn->u.p[0]);
232 ccl_rpn_delete (rpn->u.p[1]);
238 static struct ccl_rpn_node *find_spec (CCL_parser cclp,
239 struct ccl_rpn_attr **qa);
240 static struct ccl_rpn_node *search_terms (CCL_parser cclp,
241 struct ccl_rpn_attr **qa);
244 * add_attr: Add attribute (type/value) to RPN term node.
245 * p: RPN node of type term.
246 * type: Type of attribute
247 * value: Value of attribute
249 static void add_attr (struct ccl_rpn_node *p, int type, int value)
251 struct ccl_rpn_attr *n;
253 n = (struct ccl_rpn_attr *)malloc (sizeof(*n));
257 n->next = p->u.t.attr_list;
258 p->u.t.attr_list = n;
262 * search_term: Parse CCL search term.
264 * qa: Qualifier attributes already applied.
265 * return: pointer to node(s); NULL on error.
267 static struct ccl_rpn_node *search_term (CCL_parser cclp,
268 struct ccl_rpn_attr **qa)
270 struct ccl_rpn_node *p;
271 struct ccl_token *lookahead = cclp->look_token;
277 int relation_value = -1;
278 int position_value = -1;
279 int structure_value = -1;
280 int truncation_value = -1;
281 int completeness_value = -1;
283 if (KIND != CCL_TOK_TERM)
285 cclp->error_code = CCL_ERR_TERM_EXPECTED;
288 /* create the term node, but wait a moment before adding the term */
289 p = mk_node (CCL_RPN_TERM);
290 p->u.t.attr_list = NULL;
295 /* no qualifier(s) applied. Use 'term' if it is defined */
297 qa = (struct ccl_rpn_attr **)malloc (2*sizeof(*qa));
299 qa[0] = ccl_qual_search (cclp, "term", 4);
303 /* go through all attributes and add them to the attribute list */
304 for (i=0; qa && qa[i]; i++)
306 struct ccl_rpn_attr *attr;
308 for (attr = qa[i]; attr; attr = attr->next)
310 { /* deal only with REAL attributes (positive) */
314 if (relation_value != -1)
316 relation_value = attr->value;
319 if (position_value != -1)
321 position_value = attr->value;
324 if (structure_value != -1)
326 structure_value = attr->value;
329 if (truncation_value != -1)
331 truncation_value = attr->value;
334 if (completeness_value != -1)
336 completeness_value = attr->value;
339 add_attr (p, attr->type, attr->value);
342 /* go through each TERM token. If no truncation attribute is yet
343 met, then look for left/right truncation markers (?) and
344 set left_trunc/right_trunc/mid_trunc accordingly */
345 for (no = 0; lookahead->kind == CCL_TOK_TERM; no++)
347 for (i = 0; i<lookahead->len; i++)
348 if (truncation_value == -1 && lookahead->name[i] == '?')
350 if (no == 0 && i == 0 && lookahead->len >= 1)
352 else if (lookahead->next->kind != CCL_TOK_TERM &&
353 i == lookahead->len-1 && i >= 1)
358 len += 1+lookahead->len;
359 lookahead = lookahead->next;
361 /* len now holds the number of characters in the RPN term */
362 /* no holds the number of CCL tokens (1 or more) */
364 if (structure_value == -1 &&
365 qual_val_type (qa, CCL_BIB1_STR, CCL_BIB1_STR_WP))
366 { /* no structure attribute met. Apply either structure attribute
367 WORD or PHRASE depending on number of CCL tokens */
369 add_attr (p, CCL_BIB1_STR, 2);
371 add_attr (p, CCL_BIB1_STR, 1);
374 /* make the RPN token */
375 p->u.t.term = (char *)malloc (len);
376 assert (p->u.t.term);
377 p->u.t.term[0] = '\0';
378 for (i = 0; i<no; i++)
380 const char *src_str = cclp->look_token->name;
381 int src_len = cclp->look_token->len;
383 if (i == 0 && left_trunc)
388 else if (i == no-1 && right_trunc)
391 strcat (p->u.t.term, " ");
392 strxcat (p->u.t.term, src_str, src_len);
395 if (left_trunc && right_trunc)
397 if (!qual_val_type (qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_BOTH))
399 cclp->error_code = CCL_ERR_TRUNC_NOT_BOTH;
404 add_attr (p, CCL_BIB1_TRU, 3);
406 else if (right_trunc)
408 if (!qual_val_type (qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_RIGHT))
410 cclp->error_code = CCL_ERR_TRUNC_NOT_RIGHT;
415 add_attr (p, CCL_BIB1_TRU, 1);
419 if (!qual_val_type (qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_LEFT))
421 cclp->error_code = CCL_ERR_TRUNC_NOT_LEFT;
426 add_attr (p, CCL_BIB1_TRU, 2);
430 if (qual_val_type (qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_NONE))
431 add_attr (p, CCL_BIB1_TRU, 100);
437 * qualifiers: Parse CCL qualifiers and search terms.
439 * la: Token pointer to RELATION token.
440 * qa: Qualifier attributes already applied.
441 * return: pointer to node(s); NULL on error.
443 static struct ccl_rpn_node *qualifiers (CCL_parser cclp, struct ccl_token *la,
444 struct ccl_rpn_attr **qa)
446 struct ccl_token *lookahead = cclp->look_token;
447 struct ccl_rpn_attr **ap;
453 cclp->error_code = CCL_ERR_DOUBLE_QUAL;
457 for (lookahead = cclp->look_token; lookahead != la;
458 lookahead=lookahead->next)
461 for (i=0; qa[i]; i++)
463 ap = (struct ccl_rpn_attr **)malloc ((no+1) * sizeof(*ap));
465 for (i = 0; cclp->look_token != la; i++)
467 ap[i] = ccl_qual_search (cclp, cclp->look_token->name,
468 cclp->look_token->len);
471 cclp->error_code = CCL_ERR_UNKNOWN_QUAL;
476 if (KIND == CCL_TOK_COMMA)
483 if (!qual_val_type (ap, CCL_BIB1_REL, CCL_BIB1_REL_ORDER))
485 /* unordered relation */
486 struct ccl_rpn_node *p;
487 if (KIND != CCL_TOK_EQ)
489 cclp->error_code = CCL_ERR_EQ_EXPECTED;
494 if (KIND == CCL_TOK_LP)
497 if (!(p = find_spec (cclp, ap)))
502 if (KIND != CCL_TOK_RP)
504 cclp->error_code = CCL_ERR_RP_EXPECTED;
512 p = search_terms (cclp, ap);
517 if (cclp->look_token->len == 1)
519 if (cclp->look_token->name[0] == '<')
521 else if (cclp->look_token->name[0] == '=')
523 else if (cclp->look_token->name[0] == '>')
526 else if (cclp->look_token->len == 2)
528 if (!memcmp (cclp->look_token->name, "<=", 2))
530 else if (!memcmp (cclp->look_token->name, ">=", 2))
532 else if (!memcmp (cclp->look_token->name, "<>", 2))
536 cclp->error_code = CCL_ERR_BAD_RELATION;
539 struct ccl_rpn_node *p;
541 ADVANCE; /* skip relation */
542 if (KIND == CCL_TOK_TERM &&
543 cclp->look_token->next->kind == CCL_TOK_MINUS)
545 struct ccl_rpn_node *p1;
546 if (!(p1 = search_term (cclp, ap)))
551 ADVANCE; /* skip '-' */
552 if (KIND == CCL_TOK_TERM) /* = term - term ? */
554 struct ccl_rpn_node *p2;
556 if (!(p2 = search_term (cclp, ap)))
562 p = mk_node (CCL_RPN_AND);
564 add_attr (p1, CCL_BIB1_REL, 4);
566 add_attr (p2, CCL_BIB1_REL, 2);
572 add_attr (p1, CCL_BIB1_REL, 4);
577 else if (KIND == CCL_TOK_MINUS) /* = - term ? */
580 if (!(p = search_term (cclp, ap)))
585 add_attr (p, CCL_BIB1_REL, 2);
589 else if (KIND == CCL_TOK_LP)
592 if (!(p = find_spec (cclp, ap)))
597 if (KIND != CCL_TOK_RP)
599 cclp->error_code = CCL_ERR_RP_EXPECTED;
610 if (!(p = search_terms (cclp, ap)))
615 add_attr (p, CCL_BIB1_REL, rel);
619 cclp->error_code = CCL_ERR_TERM_EXPECTED;
626 * search_terms: Parse CCL search terms - including proximity.
628 * qa: Qualifier attributes already applied.
629 * return: pointer to node(s); NULL on error.
631 static struct ccl_rpn_node *search_terms (CCL_parser cclp,
632 struct ccl_rpn_attr **qa)
634 struct ccl_rpn_node *p1, *p2, *pn;
635 p1 = search_term (cclp, qa);
640 if (KIND == CCL_TOK_PROX)
643 p2 = search_term (cclp, qa);
649 pn = mk_node (CCL_RPN_PROX);
654 else if (KIND == CCL_TOK_TERM)
656 p2 = search_term (cclp, qa);
662 pn = mk_node (CCL_RPN_PROX);
674 * search_elements: Parse CCL search elements
676 * qa: Qualifier attributes already applied.
677 * return: pointer to node(s); NULL on error.
679 static struct ccl_rpn_node *search_elements (CCL_parser cclp,
680 struct ccl_rpn_attr **qa)
682 struct ccl_rpn_node *p1;
683 struct ccl_token *lookahead;
684 if (KIND == CCL_TOK_LP)
687 p1 = find_spec (cclp, qa);
690 if (KIND != CCL_TOK_RP)
692 cclp->error_code = CCL_ERR_RP_EXPECTED;
699 else if (KIND == CCL_TOK_SET)
702 if (KIND == CCL_TOK_EQ)
704 if (KIND != CCL_TOK_TERM)
706 cclp->error_code = CCL_ERR_SETNAME_EXPECTED;
709 p1 = mk_node (CCL_RPN_SET);
710 p1->u.setname = copy_token_name (cclp->look_token);
714 lookahead = cclp->look_token;
716 while (lookahead->kind==CCL_TOK_TERM)
718 lookahead = lookahead->next;
719 if (lookahead->kind == CCL_TOK_REL || lookahead->kind == CCL_TOK_EQ)
720 return qualifiers (cclp, lookahead, qa);
721 if (lookahead->kind != CCL_TOK_COMMA)
723 lookahead = lookahead->next;
725 return search_terms (cclp, qa);
729 * find_spec: Parse CCL find specification
731 * qa: Qualifier attributes already applied.
732 * return: pointer to node(s); NULL on error.
734 static struct ccl_rpn_node *find_spec (CCL_parser cclp,
735 struct ccl_rpn_attr **qa)
737 struct ccl_rpn_node *p1, *p2, *pn;
738 if (!(p1 = search_elements (cclp, qa)))
746 p2 = search_elements (cclp, qa);
752 pn = mk_node (CCL_RPN_AND);
759 p2 = search_elements (cclp, qa);
765 pn = mk_node (CCL_RPN_OR);
772 p2 = search_elements (cclp, qa);
778 pn = mk_node (CCL_RPN_NOT);
789 struct ccl_rpn_node *ccl_parser_find (CCL_parser cclp, struct ccl_token *list)
791 struct ccl_rpn_node *p;
793 cclp->look_token = list;
794 p = find_spec (cclp, NULL);
795 if (p && KIND != CCL_TOK_EOL)
797 if (KIND == CCL_TOK_RP)
798 cclp->error_code = CCL_ERR_BAD_RP;
800 cclp->error_code = CCL_ERR_OP_EXPECTED;
804 cclp->error_pos = cclp->look_token->name;
806 cclp->error_code = CCL_ERR_OK;
808 cclp->error_code = cclp->error_code;
813 * ccl_find: Parse CCL find - token representation
814 * bibset: Bibset to be used for the parsing
815 * list: List of tokens
816 * error: Pointer to integer. Holds error no. on completion.
817 * pos: Pointer to char position. Holds approximate error position.
818 * return: RPN tree on successful completion; NULL otherwise.
820 struct ccl_rpn_node *ccl_find (CCL_bibset bibset, struct ccl_token *list,
821 int *error, const char **pos)
823 struct ccl_rpn_node *p;
824 CCL_parser cclp = ccl_parser_create ();
826 cclp->bibset = bibset;
828 p = ccl_parser_find (cclp, list);
830 *error = cclp->error_code;
831 *pos = cclp->error_pos;
833 ccl_parser_destroy (cclp);
839 * ccl_find_str: Parse CCL find - string representation
840 * bibset: Bibset to be used for the parsing
841 * str: String to be parsed
842 * error: Pointer to integer. Holds error no. on completion.
843 * pos: Pointer to char position. Holds approximate error position.
844 * return: RPN tree on successful completion; NULL otherwise.
846 struct ccl_rpn_node *ccl_find_str (CCL_bibset bibset, const char *str,
847 int *error, int *pos)
849 CCL_parser cclp = ccl_parser_create ();
850 struct ccl_token *list;
851 struct ccl_rpn_node *p;
853 cclp->bibset = bibset;
855 list = ccl_parser_tokenize (cclp, str);
856 p = ccl_parser_find (cclp, list);
858 *error = cclp->error_code;
860 *pos = cclp->error_pos - str;
861 ccl_parser_destroy (cclp);
862 ccl_token_del (list);