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.5 1996-10-11 15:00:24 adam
49 * CCL parser from Europagate Email gateway 1.0.
51 * Revision 1.16 1996/01/08 08:41:13 adam
52 * Removed unused function.
54 * Revision 1.15 1995/07/20 08:14:34 adam
55 * Qualifiers were observed too often. Instead tokens are treated as
56 * qualifiers only when separated by comma.
58 * Revision 1.14 1995/05/16 09:39:26 adam
61 * Revision 1.13 1995/04/17 09:31:42 adam
62 * Improved handling of qualifiers. Aliases or reserved words.
64 * Revision 1.12 1995/03/20 15:27:43 adam
67 * Revision 1.11 1995/02/23 08:31:59 adam
70 * Revision 1.9 1995/02/16 13:20:06 adam
73 * Revision 1.8 1995/02/14 19:59:42 adam
74 * Removed a syntax error.
76 * Revision 1.7 1995/02/14 19:55:10 adam
77 * Header files ccl.h/cclp.h are gone! They have been merged an
78 * moved to ../include/ccl.h.
79 * Node kind(s) in ccl_rpn_node have changed names.
81 * Revision 1.6 1995/02/14 16:20:55 adam
82 * Qualifiers are read from a file now.
84 * Revision 1.5 1995/02/14 14:12:41 adam
85 * Ranges for ordered qualfiers implemented (e.g. pd=1980-1990).
87 * Revision 1.4 1995/02/14 13:16:29 adam
88 * Left and/or right truncation implemented.
90 * Revision 1.3 1995/02/14 10:25:56 adam
91 * The constructions 'qualifier rel term ...' implemented.
93 * Revision 1.2 1995/02/13 15:15:07 adam
94 * Added handling of qualifiers. Not finished yet.
96 * Revision 1.1 1995/02/13 12:35:20 adam
97 * First version of CCL. Qualifiers aren't handled yet.
108 /* current lookahead token */
109 static struct ccl_token *look_token;
111 /* holds error no if error occur */
112 static int ccl_error;
115 static CCL_bibset bibset;
117 /* returns type of current lookahead */
118 #define KIND (look_token->kind)
120 /* move one token forward */
121 #define ADVANCE look_token = look_token->next
124 * qual_val_type: test for existance of attribute type/value pair.
125 * qa: Attribute array
126 * type: Type of attribute to search for
127 * value: Value of attribute to seach for
128 * return: 1 if found; 0 otherwise.
130 static int qual_val_type (struct ccl_rpn_attr **qa, int type, int value)
133 struct ccl_rpn_attr *q;
137 for (i = 0; (q=qa[i]); i++)
140 if (q->type == type && q->value == value)
148 * strxcat: concatenate strings.
149 * n: Null-terminated Destination string
150 * src: Source string to be appended (not null-terminated)
151 * len: Length of source string.
153 static void strxcat (char *n, const char *src, int len)
163 * copy_token_name: Return copy of CCL token name
164 * tp: Pointer to token info.
165 * return: malloc(3) allocated copy of token name.
167 static char *copy_token_name (struct ccl_token *tp)
169 char *str = malloc (tp->len + 1);
171 memcpy (str, tp->name, tp->len);
177 * mk_node: Create RPN node.
178 * kind: Type of node.
179 * return: pointer to allocated node.
181 static struct ccl_rpn_node *mk_node (enum rpn_node_kind kind)
183 struct ccl_rpn_node *p;
184 p = malloc (sizeof(*p));
191 * ccl_rpn_delete: Delete RPN tree.
192 * rpn: Pointer to tree.
194 void ccl_rpn_delete (struct ccl_rpn_node *rpn)
196 struct ccl_rpn_attr *attr, *attr1;
204 ccl_rpn_delete (rpn->u.p[0]);
205 ccl_rpn_delete (rpn->u.p[1]);
208 free (rpn->u.t.term);
209 for (attr = rpn->u.t.attr_list; attr; attr = attr1)
216 free (rpn->u.setname);
219 ccl_rpn_delete (rpn->u.p[0]);
220 ccl_rpn_delete (rpn->u.p[1]);
226 static struct ccl_rpn_node *find_spec (struct ccl_rpn_attr **qa);
227 static struct ccl_rpn_node *search_terms (struct ccl_rpn_attr **qa);
230 * add_attr: Add attribute (type/value) to RPN term node.
231 * p: RPN node of type term.
232 * type: Type of attribute
233 * value: Value of attribute
235 static void add_attr (struct ccl_rpn_node *p, int type, int value)
237 struct ccl_rpn_attr *n;
239 n = malloc (sizeof(*n));
243 n->next = p->u.t.attr_list;
244 p->u.t.attr_list = n;
248 * search_term: Parse CCL search term.
249 * qa: Qualifier attributes already applied.
250 * return: pointer to node(s); NULL on error.
252 static struct ccl_rpn_node *search_term (struct ccl_rpn_attr **qa)
254 struct ccl_rpn_node *p;
255 struct ccl_token *lookahead = look_token;
261 int relation_value = -1;
262 int position_value = -1;
263 int structure_value = -1;
264 int truncation_value = -1;
265 int completeness_value = -1;
267 if (KIND != CCL_TOK_TERM)
269 ccl_error = CCL_ERR_TERM_EXPECTED;
272 /* create the term node, but wait a moment before adding the term */
273 p = mk_node (CCL_RPN_TERM);
274 p->u.t.attr_list = NULL;
279 /* no qualifier(s) applied. Use 'term' if it is defined */
281 qa = malloc (2*sizeof(*qa));
283 qa[0] = ccl_qual_search (bibset, "term", 4);
287 /* go through all attributes and add them to the attribute list */
288 for (i=0; qa && qa[i]; i++)
290 struct ccl_rpn_attr *attr;
292 for (attr = qa[i]; attr; attr = attr->next)
294 { /* deal only with REAL attributes (positive) */
298 if (relation_value != -1)
300 relation_value = attr->value;
303 if (position_value != -1)
305 position_value = attr->value;
308 if (structure_value != -1)
310 structure_value = attr->value;
313 if (truncation_value != -1)
315 truncation_value = attr->value;
318 if (completeness_value != -1)
320 completeness_value = attr->value;
323 add_attr (p, attr->type, attr->value);
326 /* go through each TERM token. If no truncation attribute is yet
327 met, then look for left/right truncation markers (?) and
328 set left_trunc/right_trunc/mid_trunc accordingly */
329 for (no = 0; lookahead->kind == CCL_TOK_TERM; no++)
331 for (i = 0; i<lookahead->len; i++)
332 if (truncation_value == -1 && lookahead->name[i] == '?')
334 if (no == 0 && i == 0 && lookahead->len >= 1)
336 else if (lookahead->next->kind != CCL_TOK_TERM &&
337 i == lookahead->len-1 && i >= 1)
342 len += 1+lookahead->len;
343 lookahead = lookahead->next;
345 /* len now holds the number of characters in the RPN term */
346 /* no holds the number of CCL tokens (1 or more) */
348 if (structure_value == -1 &&
349 qual_val_type (qa, CCL_BIB1_STR, CCL_BIB1_STR_WP))
350 { /* no structure attribute met. Apply either structure attribute
351 WORD or PHRASE depending on number of CCL tokens */
353 add_attr (p, CCL_BIB1_STR, 2);
355 add_attr (p, CCL_BIB1_STR, 1);
358 /* make the RPN token */
359 p->u.t.term = malloc (len);
360 assert (p->u.t.term);
361 p->u.t.term[0] = '\0';
362 for (i = 0; i<no; i++)
364 const char *src_str = look_token->name;
365 int src_len = look_token->len;
367 if (i == 0 && left_trunc)
372 else if (i == no-1 && right_trunc)
375 strcat (p->u.t.term, " ");
376 strxcat (p->u.t.term, src_str, src_len);
379 if (left_trunc && right_trunc)
381 if (!qual_val_type (qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_BOTH))
383 ccl_error = CCL_ERR_TRUNC_NOT_BOTH;
388 add_attr (p, CCL_BIB1_TRU, 3);
390 else if (right_trunc)
392 if (!qual_val_type (qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_RIGHT))
394 ccl_error = CCL_ERR_TRUNC_NOT_RIGHT;
399 add_attr (p, CCL_BIB1_TRU, 1);
403 if (!qual_val_type (qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_LEFT))
405 ccl_error = CCL_ERR_TRUNC_NOT_LEFT;
410 add_attr (p, CCL_BIB1_TRU, 2);
414 if (qual_val_type (qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_NONE))
415 add_attr (p, CCL_BIB1_TRU, 100);
421 * qualifiers: Parse CCL qualifiers and search terms.
422 * la: Token pointer to RELATION token.
423 * qa: Qualifier attributes already applied.
424 * return: pointer to node(s); NULL on error.
426 static struct ccl_rpn_node *qualifiers (struct ccl_token *la,
427 struct ccl_rpn_attr **qa)
429 struct ccl_token *lookahead = look_token;
430 struct ccl_rpn_attr **ap;
436 ccl_error = CCL_ERR_DOUBLE_QUAL;
440 for (lookahead = look_token; lookahead != la; lookahead=lookahead->next)
443 for (i=0; qa[i]; i++)
445 ap = malloc ((no+1) * sizeof(*ap));
447 for (i = 0; look_token != la; i++)
449 ap[i] = ccl_qual_search (bibset, look_token->name, look_token->len);
452 ccl_error = CCL_ERR_UNKNOWN_QUAL;
457 if (KIND == CCL_TOK_COMMA)
464 if (!qual_val_type (ap, CCL_BIB1_REL, CCL_BIB1_REL_ORDER))
466 /* unordered relation */
467 struct ccl_rpn_node *p;
468 if (KIND != CCL_TOK_EQ)
470 ccl_error = CCL_ERR_EQ_EXPECTED;
475 if (KIND == CCL_TOK_LP)
478 if (!(p = find_spec (ap)))
483 if (KIND != CCL_TOK_RP)
485 ccl_error = CCL_ERR_RP_EXPECTED;
493 p = search_terms (ap);
498 if (look_token->len == 1)
500 if (look_token->name[0] == '<')
502 else if (look_token->name[0] == '=')
504 else if (look_token->name[0] == '>')
507 else if (look_token->len == 2)
509 if (!memcmp (look_token->name, "<=", 2))
511 else if (!memcmp (look_token->name, ">=", 2))
513 else if (!memcmp (look_token->name, "<>", 2))
517 ccl_error = CCL_ERR_BAD_RELATION;
520 struct ccl_rpn_node *p;
522 ADVANCE; /* skip relation */
523 if (KIND == CCL_TOK_TERM && look_token->next->kind == CCL_TOK_MINUS)
525 struct ccl_rpn_node *p1;
526 if (!(p1 = search_term (ap)))
531 ADVANCE; /* skip '-' */
532 if (KIND == CCL_TOK_TERM) /* = term - term ? */
534 struct ccl_rpn_node *p2;
536 if (!(p2 = search_term (ap)))
542 p = mk_node (CCL_RPN_AND);
544 add_attr (p1, CCL_BIB1_REL, 4);
546 add_attr (p2, CCL_BIB1_REL, 2);
552 add_attr (p1, CCL_BIB1_REL, 4);
557 else if (KIND == CCL_TOK_MINUS) /* = - term ? */
560 if (!(p = search_term (ap)))
565 add_attr (p, CCL_BIB1_REL, 2);
569 else if (KIND == CCL_TOK_LP)
572 if (!(p = find_spec (ap)))
577 if (KIND != CCL_TOK_RP)
579 ccl_error = CCL_ERR_RP_EXPECTED;
590 if (!(p = search_terms (ap)))
595 add_attr (p, CCL_BIB1_REL, rel);
599 ccl_error = CCL_ERR_TERM_EXPECTED;
606 * search_terms: Parse CCL search terms - including proximity.
607 * qa: Qualifier attributes already applied.
608 * return: pointer to node(s); NULL on error.
610 static struct ccl_rpn_node *search_terms (struct ccl_rpn_attr **qa)
612 struct ccl_rpn_node *p1, *p2, *pn;
613 p1 = search_term (qa);
618 if (KIND == CCL_TOK_PROX)
621 p2 = search_term (qa);
627 pn = mk_node (CCL_RPN_PROX);
632 else if (KIND == CCL_TOK_TERM)
634 p2 = search_term (qa);
640 pn = mk_node (CCL_RPN_PROX);
652 * search_elements: Parse CCL search elements
653 * qa: Qualifier attributes already applied.
654 * return: pointer to node(s); NULL on error.
656 static struct ccl_rpn_node *search_elements (struct ccl_rpn_attr **qa)
658 struct ccl_rpn_node *p1;
659 struct ccl_token *lookahead;
660 if (KIND == CCL_TOK_LP)
666 if (KIND != CCL_TOK_RP)
668 ccl_error = CCL_ERR_RP_EXPECTED;
675 else if (KIND == CCL_TOK_SET)
678 if (KIND == CCL_TOK_EQ)
680 if (KIND != CCL_TOK_TERM)
682 ccl_error = CCL_ERR_SETNAME_EXPECTED;
685 p1 = mk_node (CCL_RPN_SET);
686 p1->u.setname = copy_token_name (look_token);
690 lookahead = look_token;
692 while (lookahead->kind==CCL_TOK_TERM)
694 lookahead = lookahead->next;
695 if (lookahead->kind == CCL_TOK_REL || lookahead->kind == CCL_TOK_EQ)
696 return qualifiers (lookahead, qa);
697 if (lookahead->kind != CCL_TOK_COMMA)
699 lookahead = lookahead->next;
701 return search_terms (qa);
705 * find_spec: Parse CCL find specification
706 * qa: Qualifier attributes already applied.
707 * return: pointer to node(s); NULL on error.
709 static struct ccl_rpn_node *find_spec (struct ccl_rpn_attr **qa)
711 struct ccl_rpn_node *p1, *p2, *pn;
712 if (!(p1 = search_elements (qa)))
720 p2 = search_elements (qa);
726 pn = mk_node (CCL_RPN_AND);
733 p2 = search_elements (qa);
739 pn = mk_node (CCL_RPN_OR);
746 p2 = search_elements (qa);
752 pn = mk_node (CCL_RPN_NOT);
764 * ccl_find: Parse CCL find - token representation
765 * abibset: Bibset to be used for the parsing
766 * list: List of tokens
767 * error: Pointer to integer. Holds error no. on completion.
768 * pos: Pointer to char position. Holds approximate error position.
769 * return: RPN tree on successful completion; NULL otherwise.
771 struct ccl_rpn_node *ccl_find (CCL_bibset abibset, struct ccl_token *list,
772 int *error, const char **pos)
774 struct ccl_rpn_node *p;
778 p = find_spec (NULL);
779 if (p && KIND != CCL_TOK_EOL)
781 if (KIND == CCL_TOK_RP)
782 ccl_error = CCL_ERR_BAD_RP;
784 ccl_error = CCL_ERR_OP_EXPECTED;
788 *pos = look_token->name;
797 * ccl_find_str: Parse CCL find - string representation
798 * bibset: Bibset to be used for the parsing
799 * str: String to be parsed
800 * error: Pointer to integer. Holds error no. on completion.
801 * pos: Pointer to char position. Holds approximate error position.
802 * return: RPN tree on successful completion; NULL otherwise.
804 struct ccl_rpn_node *ccl_find_str (CCL_bibset bibset, const char *str,
805 int *error, int *pos)
807 struct ccl_token *list;
808 struct ccl_rpn_node *rpn;
809 const char *char_pos;
811 list = ccl_tokenize (str);
812 rpn = ccl_find (bibset, list, error, &char_pos);
814 *pos = char_pos - str;