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.14 1995/05/16 09:39:26 adam
51 * Revision 1.13 1995/04/17 09:31:42 adam
52 * Improved handling of qualifiers. Aliases or reserved words.
54 * Revision 1.12 1995/03/20 15:27:43 adam
57 * Revision 1.11 1995/02/23 08:31:59 adam
60 * Revision 1.9 1995/02/16 13:20:06 adam
63 * Revision 1.8 1995/02/14 19:59:42 adam
64 * Removed a syntax error.
66 * Revision 1.7 1995/02/14 19:55:10 adam
67 * Header files ccl.h/cclp.h are gone! They have been merged an
68 * moved to ../include/ccl.h.
69 * Node kind(s) in ccl_rpn_node have changed names.
71 * Revision 1.6 1995/02/14 16:20:55 adam
72 * Qualifiers are read from a file now.
74 * Revision 1.5 1995/02/14 14:12:41 adam
75 * Ranges for ordered qualfiers implemented (e.g. pd=1980-1990).
77 * Revision 1.4 1995/02/14 13:16:29 adam
78 * Left and/or right truncation implemented.
80 * Revision 1.3 1995/02/14 10:25:56 adam
81 * The constructions 'qualifier rel term ...' implemented.
83 * Revision 1.2 1995/02/13 15:15:07 adam
84 * Added handling of qualifiers. Not finished yet.
86 * Revision 1.1 1995/02/13 12:35:20 adam
87 * First version of CCL. Qualifiers aren't handled yet.
98 /* current lookahead token */
99 static struct ccl_token *look_token;
101 /* holds error no if error occur */
102 static int ccl_error;
105 static CCL_bibset bibset;
107 /* returns type of current lookahead */
108 #define KIND (look_token->kind)
110 /* move one token forward */
111 #define ADVANCE look_token = look_token->next
114 * qual_val_range: search for attribute of type with range
115 * qa: Attribute array
116 * type: Type of attribute to search for
117 * vmin: Lower bound of value to search for
118 * vmax: Upper bound of value to search for
119 * return: Return pointer to integer of attribute value found; NULL
122 static int *qual_val_range (struct ccl_rpn_attr **qa, int type,
126 struct ccl_rpn_attr *q;
130 for (i = 0; (q=qa[i]); i++)
133 if (q->type == type && q->value >= vmin && q->value <= vmax)
141 * qual_val_type: test for existance of attribute type/value pair.
142 * qa: Attribute array
143 * type: Type of attribute to search for
144 * value: Value of attribute to seach for
145 * return: 1 if found; 0 otherwise.
147 static int qual_val_type (struct ccl_rpn_attr **qa, int type, int value)
150 struct ccl_rpn_attr *q;
154 for (i = 0; (q=qa[i]); i++)
157 if (q->type == type && q->value == value)
165 * strxcat: concatenate strings.
166 * n: Null-terminated Destination string
167 * src: Source string to be appended (not null-terminated)
168 * len: Length of source string.
170 static void strxcat (char *n, const char *src, int len)
180 * copy_token_name: Return copy of CCL token name
181 * tp: Pointer to token info.
182 * return: malloc(3) allocated copy of token name.
184 static char *copy_token_name (struct ccl_token *tp)
186 char *str = malloc (tp->len + 1);
188 memcpy (str, tp->name, tp->len);
194 * mk_node: Create RPN node.
195 * kind: Type of node.
196 * return: pointer to allocated node.
198 static struct ccl_rpn_node *mk_node (enum rpn_node_kind kind)
200 struct ccl_rpn_node *p;
201 p = malloc (sizeof(*p));
208 * ccl_rpn_delete: Delete RPN tree.
209 * rpn: Pointer to tree.
211 void ccl_rpn_delete (struct ccl_rpn_node *rpn)
213 struct ccl_rpn_attr *attr, *attr1;
221 ccl_rpn_delete (rpn->u.p[0]);
222 ccl_rpn_delete (rpn->u.p[1]);
225 free (rpn->u.t.term);
226 for (attr = rpn->u.t.attr_list; attr; attr = attr1)
233 free (rpn->u.setname);
236 ccl_rpn_delete (rpn->u.p[0]);
237 ccl_rpn_delete (rpn->u.p[1]);
243 static struct ccl_rpn_node *find_spec (struct ccl_rpn_attr **qa);
244 static struct ccl_rpn_node *search_terms (struct ccl_rpn_attr **qa);
247 * add_attr: Add attribute (type/value) to RPN term node.
248 * p: RPN node of type term.
249 * type: Type of attribute
250 * value: Value of attribute
252 static void add_attr (struct ccl_rpn_node *p, int type, int value)
254 struct ccl_rpn_attr *n;
256 n = malloc (sizeof(*n));
260 n->next = p->u.t.attr_list;
261 p->u.t.attr_list = n;
265 * search_term: Parse CCL search term.
266 * qa: Qualifier attributes already applied.
267 * return: pointer to node(s); NULL on error.
269 static struct ccl_rpn_node *search_term (struct ccl_rpn_attr **qa)
271 struct ccl_rpn_node *p;
272 struct ccl_token *lookahead = look_token;
278 int relation_value = -1;
279 int position_value = -1;
280 int structure_value = -1;
281 int truncation_value = -1;
282 int completeness_value = -1;
284 if (KIND != CCL_TOK_TERM)
286 ccl_error = CCL_ERR_TERM_EXPECTED;
289 /* create the term node, but wait a moment before adding the term */
290 p = mk_node (CCL_RPN_TERM);
291 p->u.t.attr_list = NULL;
296 /* no qualifier(s) applied. Use 'term' if it is defined */
298 qa = malloc (2*sizeof(*qa));
300 qa[0] = ccl_qual_search (bibset, "term", 4);
304 /* go through all attributes and add them to the attribute list */
305 for (i=0; qa && qa[i]; i++)
307 struct ccl_rpn_attr *attr;
309 for (attr = qa[i]; attr; attr = attr->next)
311 { /* deal only with REAL attributes (positive) */
315 if (relation_value != -1)
317 relation_value = attr->value;
320 if (position_value != -1)
322 position_value = attr->value;
325 if (structure_value != -1)
327 structure_value = attr->value;
330 if (truncation_value != -1)
332 truncation_value = attr->value;
335 if (completeness_value != -1)
337 completeness_value = attr->value;
340 add_attr (p, attr->type, attr->value);
343 /* go through each TERM token. If no truncation attribute is yet
344 met, then look for left/right truncation markers (?) and
345 set left_trunc/right_trunc/mid_trunc accordingly */
346 for (no = 0; lookahead->kind == CCL_TOK_TERM; no++)
348 for (i = 0; i<lookahead->len; i++)
349 if (truncation_value == -1 && lookahead->name[i] == '?')
351 if (no == 0 && i == 0 && lookahead->len >= 1)
353 else if (lookahead->next->kind != CCL_TOK_TERM &&
354 i == lookahead->len-1 && i >= 1)
359 len += 1+lookahead->len;
360 lookahead = lookahead->next;
362 /* len now holds the number of characters in the RPN term */
363 /* no holds the number of CCL tokens (1 or more) */
365 if (structure_value == -1 &&
366 qual_val_type (qa, CCL_BIB1_STR, CCL_BIB1_STR_WP))
367 { /* no structure attribute met. Apply either structure attribute
368 WORD or PHRASE depending on number of CCL tokens */
370 add_attr (p, CCL_BIB1_STR, 2);
372 add_attr (p, CCL_BIB1_STR, 1);
375 /* make the RPN token */
376 p->u.t.term = malloc (len);
377 assert (p->u.t.term);
378 p->u.t.term[0] = '\0';
379 for (i = 0; i<no; i++)
381 const char *src_str = look_token->name;
382 int src_len = look_token->len;
384 if (i == 0 && left_trunc)
389 else if (i == no-1 && right_trunc)
392 strcat (p->u.t.term, " ");
393 strxcat (p->u.t.term, src_str, src_len);
396 if (left_trunc && right_trunc)
398 if (!qual_val_type (qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_BOTH))
400 ccl_error = CCL_ERR_TRUNC_NOT_BOTH;
405 add_attr (p, CCL_BIB1_TRU, 3);
407 else if (right_trunc)
409 if (!qual_val_type (qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_RIGHT))
411 ccl_error = CCL_ERR_TRUNC_NOT_RIGHT;
416 add_attr (p, CCL_BIB1_TRU, 1);
420 if (!qual_val_type (qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_LEFT))
422 ccl_error = CCL_ERR_TRUNC_NOT_LEFT;
427 add_attr (p, CCL_BIB1_TRU, 2);
431 if (qual_val_type (qa, CCL_BIB1_TRU, CCL_BIB1_TRU_CAN_NONE))
432 add_attr (p, CCL_BIB1_TRU, 100);
438 * 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 (struct ccl_token *la,
444 struct ccl_rpn_attr **qa)
446 struct ccl_token *lookahead = look_token;
447 struct ccl_rpn_attr **ap;
453 ccl_error = CCL_ERR_DOUBLE_QUAL;
457 for (lookahead = look_token; lookahead != la; lookahead=lookahead->next)
460 for (i=0; qa[i]; i++)
462 ap = malloc ((no+1) * sizeof(*ap));
464 for (i = 0; look_token != la; i++)
466 ap[i] = ccl_qual_search (bibset, look_token->name, look_token->len);
469 ccl_error = CCL_ERR_UNKNOWN_QUAL;
474 if (KIND == CCL_TOK_COMMA)
481 if (!qual_val_type (ap, CCL_BIB1_REL, CCL_BIB1_REL_ORDER))
483 /* unordered relation */
484 struct ccl_rpn_node *p;
485 if (KIND != CCL_TOK_EQ)
487 ccl_error = CCL_ERR_EQ_EXPECTED;
492 if (KIND == CCL_TOK_LP)
495 if (!(p = find_spec (ap)))
500 if (KIND != CCL_TOK_RP)
502 ccl_error = CCL_ERR_RP_EXPECTED;
510 p = search_terms (ap);
515 if (look_token->len == 1)
517 if (look_token->name[0] == '<')
519 else if (look_token->name[0] == '=')
521 else if (look_token->name[0] == '>')
524 else if (look_token->len == 2)
526 if (!memcmp (look_token->name, "<=", 2))
528 else if (!memcmp (look_token->name, ">=", 2))
530 else if (!memcmp (look_token->name, "<>", 2))
534 ccl_error = CCL_ERR_BAD_RELATION;
537 struct ccl_rpn_node *p;
539 ADVANCE; /* skip relation */
540 if (KIND == CCL_TOK_TERM && look_token->next->kind == CCL_TOK_MINUS)
542 struct ccl_rpn_node *p1;
543 if (!(p1 = search_term (ap)))
548 ADVANCE; /* skip '-' */
549 if (KIND == CCL_TOK_TERM) /* = term - term ? */
551 struct ccl_rpn_node *p2;
553 if (!(p2 = search_term (ap)))
559 p = mk_node (CCL_RPN_AND);
561 add_attr (p1, CCL_BIB1_REL, 4);
563 add_attr (p2, CCL_BIB1_REL, 2);
569 add_attr (p1, CCL_BIB1_REL, 4);
574 else if (KIND == CCL_TOK_MINUS) /* = - term ? */
577 if (!(p = search_term (ap)))
582 add_attr (p, CCL_BIB1_REL, 2);
586 else if (KIND == CCL_TOK_LP)
589 if (!(p = find_spec (ap)))
594 if (KIND != CCL_TOK_RP)
596 ccl_error = CCL_ERR_RP_EXPECTED;
607 if (!(p = search_terms (ap)))
612 add_attr (p, CCL_BIB1_REL, rel);
616 ccl_error = CCL_ERR_TERM_EXPECTED;
623 * search_terms: Parse CCL search terms - including proximity.
624 * qa: Qualifier attributes already applied.
625 * return: pointer to node(s); NULL on error.
627 static struct ccl_rpn_node *search_terms (struct ccl_rpn_attr **qa)
629 struct ccl_rpn_node *p1, *p2, *pn;
630 p1 = search_term (qa);
635 if (KIND == CCL_TOK_PROX)
638 p2 = search_term (qa);
644 pn = mk_node (CCL_RPN_PROX);
649 else if (KIND == CCL_TOK_TERM)
651 p2 = search_term (qa);
657 pn = mk_node (CCL_RPN_PROX);
669 * 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 (struct ccl_rpn_attr **qa)
675 struct ccl_rpn_node *p1;
676 struct ccl_token *lookahead;
677 if (KIND == CCL_TOK_LP)
683 if (KIND != CCL_TOK_RP)
685 ccl_error = CCL_ERR_RP_EXPECTED;
692 else if (KIND == CCL_TOK_SET)
695 if (KIND == CCL_TOK_EQ)
697 if (KIND != CCL_TOK_TERM)
699 ccl_error = CCL_ERR_SETNAME_EXPECTED;
702 p1 = mk_node (CCL_RPN_SET);
703 p1->u.setname = copy_token_name (look_token);
707 lookahead = look_token;
709 while (lookahead->kind==CCL_TOK_TERM || lookahead->kind==CCL_TOK_COMMA)
710 lookahead = lookahead->next;
711 if (lookahead->kind == CCL_TOK_REL || lookahead->kind == CCL_TOK_EQ)
712 return qualifiers (lookahead, qa);
713 return search_terms (qa);
717 * find_spec: Parse CCL find specification
718 * qa: Qualifier attributes already applied.
719 * return: pointer to node(s); NULL on error.
721 static struct ccl_rpn_node *find_spec (struct ccl_rpn_attr **qa)
723 struct ccl_rpn_node *p1, *p2, *pn;
724 if (!(p1 = search_elements (qa)))
732 p2 = search_elements (qa);
738 pn = mk_node (CCL_RPN_AND);
745 p2 = search_elements (qa);
751 pn = mk_node (CCL_RPN_OR);
758 p2 = search_elements (qa);
764 pn = mk_node (CCL_RPN_NOT);
776 * ccl_find: Parse CCL find - token representation
777 * abibset: Bibset to be used for the parsing
778 * list: List of tokens
779 * error: Pointer to integer. Holds error no. on completion.
780 * pos: Pointer to char position. Holds approximate error position.
781 * return: RPN tree on successful completion; NULL otherwise.
783 struct ccl_rpn_node *ccl_find (CCL_bibset abibset, struct ccl_token *list,
784 int *error, const char **pos)
786 struct ccl_rpn_node *p;
790 p = find_spec (NULL);
791 if (p && KIND != CCL_TOK_EOL)
793 if (KIND == CCL_TOK_RP)
794 ccl_error = CCL_ERR_BAD_RP;
796 ccl_error = CCL_ERR_OP_EXPECTED;
800 *pos = look_token->name;
809 * ccl_find_str: Parse CCL find - string representation
810 * bibset: Bibset to be used for the parsing
811 * str: String to be parsed
812 * error: Pointer to integer. Holds error no. on completion.
813 * pos: Pointer to char position. Holds approximate error position.
814 * return: RPN tree on successful completion; NULL otherwise.
816 struct ccl_rpn_node *ccl_find_str (CCL_bibset bibset, const char *str,
817 int *error, int *pos)
819 struct ccl_token *list;
820 struct ccl_rpn_node *rpn;
821 const char *char_pos;
823 list = ccl_tokenize (str);
824 rpn = ccl_find (bibset, list, error, &char_pos);
826 *pos = char_pos - str;