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.9 1997-09-29 08:56:37 adam
49 * Changed CCL parser to be thread safe. New type, CCL_parser, declared
50 * and a create/destructers ccl_parser_create/ccl_parser/destory has
53 * Revision 1.8 1997/09/01 08:48:11 adam
54 * New windows NT/95 port using MSV5.0. Only a few changes made
57 * Revision 1.7 1997/05/14 06:53:26 adam
60 * Revision 1.6 1997/04/30 08:52:06 quinn
63 * Revision 1.5 1996/10/11 15:00:24 adam
64 * CCL parser from Europagate Email gateway 1.0.
66 * Revision 1.16 1996/01/08 08:41:13 adam
67 * Removed unused function.
69 * Revision 1.15 1995/07/20 08:14:34 adam
70 * Qualifiers were observed too often. Instead tokens are treated as
71 * qualifiers only when separated by comma.
73 * Revision 1.14 1995/05/16 09:39:26 adam
76 * Revision 1.13 1995/04/17 09:31:42 adam
77 * Improved handling of qualifiers. Aliases or reserved words.
79 * Revision 1.12 1995/03/20 15:27:43 adam
82 * Revision 1.11 1995/02/23 08:31:59 adam
85 * Revision 1.9 1995/02/16 13:20:06 adam
88 * Revision 1.8 1995/02/14 19:59:42 adam
89 * Removed a syntax error.
91 * Revision 1.7 1995/02/14 19:55:10 adam
92 * Header files ccl.h/cclp.h are gone! They have been merged an
93 * moved to ../include/ccl.h.
94 * Node kind(s) in ccl_rpn_node have changed names.
96 * Revision 1.6 1995/02/14 16:20:55 adam
97 * Qualifiers are read from a file now.
99 * Revision 1.5 1995/02/14 14:12:41 adam
100 * Ranges for ordered qualfiers implemented (e.g. pd=1980-1990).
102 * Revision 1.4 1995/02/14 13:16:29 adam
103 * Left and/or right truncation implemented.
105 * Revision 1.3 1995/02/14 10:25:56 adam
106 * The constructions 'qualifier rel term ...' implemented.
108 * Revision 1.2 1995/02/13 15:15:07 adam
109 * Added handling of qualifiers. Not finished yet.
111 * Revision 1.1 1995/02/13 12:35:20 adam
112 * First version of CCL. Qualifiers aren't handled yet.
123 /* returns type of current lookahead */
124 #define KIND (cclp->look_token->kind)
126 /* move one token forward */
127 #define ADVANCE cclp->look_token = cclp->look_token->next
130 * qual_val_type: test for existance of attribute type/value pair.
131 * qa: Attribute array
132 * type: Type of attribute to search for
133 * value: Value of attribute to seach for
134 * return: 1 if found; 0 otherwise.
136 static int qual_val_type (struct ccl_rpn_attr **qa, int type, int value)
139 struct ccl_rpn_attr *q;
143 for (i = 0; (q=qa[i]); i++)
146 if (q->type == type && q->value == value)
154 * strxcat: concatenate strings.
155 * n: Null-terminated Destination string
156 * src: Source string to be appended (not null-terminated)
157 * len: Length of source string.
159 static void strxcat (char *n, const char *src, int len)
169 * copy_token_name: Return copy of CCL token name
170 * tp: Pointer to token info.
171 * return: malloc(3) allocated copy of token name.
173 static char *copy_token_name (struct ccl_token *tp)
175 char *str = malloc (tp->len + 1);
177 memcpy (str, tp->name, tp->len);
183 * mk_node: Create RPN node.
184 * kind: Type of node.
185 * return: pointer to allocated node.
187 static struct ccl_rpn_node *mk_node (int kind)
189 struct ccl_rpn_node *p;
190 p = malloc (sizeof(*p));
197 * ccl_rpn_delete: Delete RPN tree.
198 * rpn: Pointer to tree.
200 void ccl_rpn_delete (struct ccl_rpn_node *rpn)
202 struct ccl_rpn_attr *attr, *attr1;
210 ccl_rpn_delete (rpn->u.p[0]);
211 ccl_rpn_delete (rpn->u.p[1]);
214 free (rpn->u.t.term);
215 for (attr = rpn->u.t.attr_list; attr; attr = attr1)
222 free (rpn->u.setname);
225 ccl_rpn_delete (rpn->u.p[0]);
226 ccl_rpn_delete (rpn->u.p[1]);
232 static struct ccl_rpn_node *find_spec (CCL_parser cclp,
233 struct ccl_rpn_attr **qa);
234 static struct ccl_rpn_node *search_terms (CCL_parser cclp,
235 struct ccl_rpn_attr **qa);
238 * add_attr: Add attribute (type/value) to RPN term node.
239 * p: RPN node of type term.
240 * type: Type of attribute
241 * value: Value of attribute
243 static void add_attr (struct ccl_rpn_node *p, int type, int value)
245 struct ccl_rpn_attr *n;
247 n = malloc (sizeof(*n));
251 n->next = p->u.t.attr_list;
252 p->u.t.attr_list = n;
256 * search_term: Parse CCL search term.
258 * qa: Qualifier attributes already applied.
259 * return: pointer to node(s); NULL on error.
261 static struct ccl_rpn_node *search_term (CCL_parser cclp,
262 struct ccl_rpn_attr **qa)
264 struct ccl_rpn_node *p;
265 struct ccl_token *lookahead = cclp->look_token;
271 int relation_value = -1;
272 int position_value = -1;
273 int structure_value = -1;
274 int truncation_value = -1;
275 int completeness_value = -1;
277 if (KIND != CCL_TOK_TERM)
279 cclp->error_code = CCL_ERR_TERM_EXPECTED;
282 /* create the term node, but wait a moment before adding the term */
283 p = mk_node (CCL_RPN_TERM);
284 p->u.t.attr_list = NULL;
289 /* no qualifier(s) applied. Use 'term' if it is defined */
291 qa = malloc (2*sizeof(*qa));
293 qa[0] = ccl_qual_search (cclp, "term", 4);
297 /* go through all attributes and add them to the attribute list */
298 for (i=0; qa && qa[i]; i++)
300 struct ccl_rpn_attr *attr;
302 for (attr = qa[i]; attr; attr = attr->next)
304 { /* deal only with REAL attributes (positive) */
308 if (relation_value != -1)
310 relation_value = attr->value;
313 if (position_value != -1)
315 position_value = attr->value;
318 if (structure_value != -1)
320 structure_value = attr->value;
323 if (truncation_value != -1)
325 truncation_value = attr->value;
328 if (completeness_value != -1)
330 completeness_value = attr->value;
333 add_attr (p, attr->type, attr->value);
336 /* go through each TERM token. If no truncation attribute is yet
337 met, then look for left/right truncation markers (?) and
338 set left_trunc/right_trunc/mid_trunc accordingly */
339 for (no = 0; lookahead->kind == CCL_TOK_TERM; no++)
341 for (i = 0; i<lookahead->len; i++)
342 if (truncation_value == -1 && lookahead->name[i] == '?')
344 if (no == 0 && i == 0 && lookahead->len >= 1)
346 else if (lookahead->next->kind != CCL_TOK_TERM &&
347 i == lookahead->len-1 && i >= 1)
352 len += 1+lookahead->len;
353 lookahead = lookahead->next;
355 /* len now holds the number of characters in the RPN term */
356 /* no holds the number of CCL tokens (1 or more) */
358 if (structure_value == -1 &&
359 qual_val_type (qa, CCL_BIB1_STR, CCL_BIB1_STR_WP))
360 { /* no structure attribute met. Apply either structure attribute
361 WORD or PHRASE depending on number of CCL tokens */
363 add_attr (p, CCL_BIB1_STR, 2);
365 add_attr (p, CCL_BIB1_STR, 1);
368 /* make the RPN token */
369 p->u.t.term = malloc (len);
370 assert (p->u.t.term);
371 p->u.t.term[0] = '\0';
372 for (i = 0; i<no; i++)
374 const char *src_str = cclp->look_token->name;
375 int src_len = cclp->look_token->len;
377 if (i == 0 && left_trunc)
382 else if (i == no-1 && right_trunc)
385 strcat (p->u.t.term, " ");
386 strxcat (p->u.t.term, src_str, src_len);
389 if (left_trunc && right_trunc)
391 if (!qual_val_type (qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_BOTH))
393 cclp->error_code = CCL_ERR_TRUNC_NOT_BOTH;
398 add_attr (p, CCL_BIB1_TRU, 3);
400 else if (right_trunc)
402 if (!qual_val_type (qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_RIGHT))
404 cclp->error_code = CCL_ERR_TRUNC_NOT_RIGHT;
409 add_attr (p, CCL_BIB1_TRU, 1);
413 if (!qual_val_type (qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_LEFT))
415 cclp->error_code = CCL_ERR_TRUNC_NOT_LEFT;
420 add_attr (p, CCL_BIB1_TRU, 2);
424 if (qual_val_type (qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_NONE))
425 add_attr (p, CCL_BIB1_TRU, 100);
431 * qualifiers: Parse CCL qualifiers and search terms.
433 * la: Token pointer to RELATION token.
434 * qa: Qualifier attributes already applied.
435 * return: pointer to node(s); NULL on error.
437 static struct ccl_rpn_node *qualifiers (CCL_parser cclp, struct ccl_token *la,
438 struct ccl_rpn_attr **qa)
440 struct ccl_token *lookahead = cclp->look_token;
441 struct ccl_rpn_attr **ap;
447 cclp->error_code = CCL_ERR_DOUBLE_QUAL;
451 for (lookahead = cclp->look_token; lookahead != la;
452 lookahead=lookahead->next)
455 for (i=0; qa[i]; i++)
457 ap = malloc ((no+1) * sizeof(*ap));
459 for (i = 0; cclp->look_token != la; i++)
461 ap[i] = ccl_qual_search (cclp, cclp->look_token->name,
462 cclp->look_token->len);
465 cclp->error_code = CCL_ERR_UNKNOWN_QUAL;
470 if (KIND == CCL_TOK_COMMA)
477 if (!qual_val_type (ap, CCL_BIB1_REL, CCL_BIB1_REL_ORDER))
479 /* unordered relation */
480 struct ccl_rpn_node *p;
481 if (KIND != CCL_TOK_EQ)
483 cclp->error_code = CCL_ERR_EQ_EXPECTED;
488 if (KIND == CCL_TOK_LP)
491 if (!(p = find_spec (cclp, ap)))
496 if (KIND != CCL_TOK_RP)
498 cclp->error_code = CCL_ERR_RP_EXPECTED;
506 p = search_terms (cclp, ap);
511 if (cclp->look_token->len == 1)
513 if (cclp->look_token->name[0] == '<')
515 else if (cclp->look_token->name[0] == '=')
517 else if (cclp->look_token->name[0] == '>')
520 else if (cclp->look_token->len == 2)
522 if (!memcmp (cclp->look_token->name, "<=", 2))
524 else if (!memcmp (cclp->look_token->name, ">=", 2))
526 else if (!memcmp (cclp->look_token->name, "<>", 2))
530 cclp->error_code = CCL_ERR_BAD_RELATION;
533 struct ccl_rpn_node *p;
535 ADVANCE; /* skip relation */
536 if (KIND == CCL_TOK_TERM &&
537 cclp->look_token->next->kind == CCL_TOK_MINUS)
539 struct ccl_rpn_node *p1;
540 if (!(p1 = search_term (cclp, ap)))
545 ADVANCE; /* skip '-' */
546 if (KIND == CCL_TOK_TERM) /* = term - term ? */
548 struct ccl_rpn_node *p2;
550 if (!(p2 = search_term (cclp, ap)))
556 p = mk_node (CCL_RPN_AND);
558 add_attr (p1, CCL_BIB1_REL, 4);
560 add_attr (p2, CCL_BIB1_REL, 2);
566 add_attr (p1, CCL_BIB1_REL, 4);
571 else if (KIND == CCL_TOK_MINUS) /* = - term ? */
574 if (!(p = search_term (cclp, ap)))
579 add_attr (p, CCL_BIB1_REL, 2);
583 else if (KIND == CCL_TOK_LP)
586 if (!(p = find_spec (cclp, ap)))
591 if (KIND != CCL_TOK_RP)
593 cclp->error_code = CCL_ERR_RP_EXPECTED;
604 if (!(p = search_terms (cclp, ap)))
609 add_attr (p, CCL_BIB1_REL, rel);
613 cclp->error_code = CCL_ERR_TERM_EXPECTED;
620 * search_terms: Parse CCL search terms - including proximity.
622 * qa: Qualifier attributes already applied.
623 * return: pointer to node(s); NULL on error.
625 static struct ccl_rpn_node *search_terms (CCL_parser cclp,
626 struct ccl_rpn_attr **qa)
628 struct ccl_rpn_node *p1, *p2, *pn;
629 p1 = search_term (cclp, qa);
634 if (KIND == CCL_TOK_PROX)
637 p2 = search_term (cclp, qa);
643 pn = mk_node (CCL_RPN_PROX);
648 else if (KIND == CCL_TOK_TERM)
650 p2 = search_term (cclp, qa);
656 pn = mk_node (CCL_RPN_PROX);
668 * search_elements: Parse CCL search elements
670 * qa: Qualifier attributes already applied.
671 * return: pointer to node(s); NULL on error.
673 static struct ccl_rpn_node *search_elements (CCL_parser cclp,
674 struct ccl_rpn_attr **qa)
676 struct ccl_rpn_node *p1;
677 struct ccl_token *lookahead;
678 if (KIND == CCL_TOK_LP)
681 p1 = find_spec (cclp, qa);
684 if (KIND != CCL_TOK_RP)
686 cclp->error_code = CCL_ERR_RP_EXPECTED;
693 else if (KIND == CCL_TOK_SET)
696 if (KIND == CCL_TOK_EQ)
698 if (KIND != CCL_TOK_TERM)
700 cclp->error_code = CCL_ERR_SETNAME_EXPECTED;
703 p1 = mk_node (CCL_RPN_SET);
704 p1->u.setname = copy_token_name (cclp->look_token);
708 lookahead = cclp->look_token;
710 while (lookahead->kind==CCL_TOK_TERM)
712 lookahead = lookahead->next;
713 if (lookahead->kind == CCL_TOK_REL || lookahead->kind == CCL_TOK_EQ)
714 return qualifiers (cclp, lookahead, qa);
715 if (lookahead->kind != CCL_TOK_COMMA)
717 lookahead = lookahead->next;
719 return search_terms (cclp, qa);
723 * find_spec: Parse CCL find specification
725 * qa: Qualifier attributes already applied.
726 * return: pointer to node(s); NULL on error.
728 static struct ccl_rpn_node *find_spec (CCL_parser cclp,
729 struct ccl_rpn_attr **qa)
731 struct ccl_rpn_node *p1, *p2, *pn;
732 if (!(p1 = search_elements (cclp, qa)))
740 p2 = search_elements (cclp, qa);
746 pn = mk_node (CCL_RPN_AND);
753 p2 = search_elements (cclp, qa);
759 pn = mk_node (CCL_RPN_OR);
766 p2 = search_elements (cclp, qa);
772 pn = mk_node (CCL_RPN_NOT);
783 struct ccl_rpn_node *ccl_parser_find (CCL_parser cclp, struct ccl_token *list)
785 struct ccl_rpn_node *p;
787 cclp->look_token = list;
788 p = find_spec (cclp, NULL);
789 if (p && KIND != CCL_TOK_EOL)
791 if (KIND == CCL_TOK_RP)
792 cclp->error_code = CCL_ERR_BAD_RP;
794 cclp->error_code = CCL_ERR_OP_EXPECTED;
798 cclp->error_pos = cclp->look_token->name;
800 cclp->error_code = CCL_ERR_OK;
802 cclp->error_code = cclp->error_code;
807 * ccl_find: Parse CCL find - token representation
808 * bibset: Bibset to be used for the parsing
809 * list: List of tokens
810 * error: Pointer to integer. Holds error no. on completion.
811 * pos: Pointer to char position. Holds approximate error position.
812 * return: RPN tree on successful completion; NULL otherwise.
814 struct ccl_rpn_node *ccl_find (CCL_bibset bibset, struct ccl_token *list,
815 int *error, const char **pos)
817 struct ccl_rpn_node *p;
818 CCL_parser cclp = ccl_parser_create ();
820 cclp->bibset = bibset;
822 p = ccl_parser_find (cclp, list);
824 *error = cclp->error_code;
825 *pos = cclp->error_pos;
827 ccl_parser_destroy (cclp);
833 * ccl_find_str: Parse CCL find - string representation
834 * bibset: Bibset to be used for the parsing
835 * str: String to be parsed
836 * error: Pointer to integer. Holds error no. on completion.
837 * pos: Pointer to char position. Holds approximate error position.
838 * return: RPN tree on successful completion; NULL otherwise.
840 struct ccl_rpn_node *ccl_find_str (CCL_bibset bibset, const char *str,
841 int *error, int *pos)
843 CCL_parser cclp = ccl_parser_create ();
844 struct ccl_token *list;
845 struct ccl_rpn_node *p;
847 cclp->bibset = bibset;
849 list = ccl_parser_tokenize (cclp, str);
850 p = ccl_parser_find (cclp, list);
852 *error = cclp->error_code;
854 *pos = cclp->error_pos - str;
855 ccl_parser_destroy (cclp);