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 - lexical analysis
47 * $Log: ccltoken.c,v $
48 * Revision 1.16 2001-03-07 13:24:40 adam
49 * Member and_not in Z_Operator is kept for backwards compatibility.
50 * Added support for definition of CCL operators in field spec file.
52 * Revision 1.15 2000/05/01 09:36:50 adam
53 * Range operator only treated in ordered ranges so that minus (-) can be
54 * used for, say, the and-not operator.
56 * Revision 1.14 2000/03/14 09:06:11 adam
57 * Added POSIX threads support for frontend server.
59 * Revision 1.13 2000/02/08 10:39:53 adam
60 * Added a few functions to set name of operands, etc.
62 * Revision 1.12 2000/01/31 13:15:21 adam
63 * Removed uses of assert(3). Cleanup of ODR. CCL parser update so
64 * that some characters are not surrounded by spaces in resulting term.
67 * Revision 1.11 1999/11/30 13:47:11 adam
68 * Improved installation. Moved header files to include/yaz.
70 * Revision 1.10 1998/07/07 15:49:41 adam
71 * Added braces to avoid warning.
73 * Revision 1.9 1998/02/11 11:53:33 adam
74 * Changed code so that it compiles as C++.
76 * Revision 1.8 1997/09/29 08:56:38 adam
77 * Changed CCL parser to be thread safe. New type, CCL_parser, declared
78 * and a create/destructers ccl_parser_create/ccl_parser/destory has
81 * Revision 1.7 1997/09/01 08:48:12 adam
82 * New windows NT/95 port using MSV5.0. Only a few changes made
85 * Revision 1.6 1997/04/30 08:52:07 quinn
88 * Revision 1.5 1996/10/11 15:00:26 adam
89 * CCL parser from Europagate Email gateway 1.0.
91 * Revision 1.10 1995/07/11 12:28:31 adam
92 * New function: ccl_token_simple (split into simple tokens) and
93 * ccl_token_del (delete tokens).
95 * Revision 1.9 1995/05/16 09:39:28 adam
98 * Revision 1.8 1995/05/11 14:03:57 adam
99 * Changes in the reading of qualifier(s). New function: ccl_qual_fitem.
100 * New variable ccl_case_sensitive, which controls whether reserved
101 * words and field names are case sensitive or not.
103 * Revision 1.7 1995/04/19 12:11:24 adam
106 * Revision 1.6 1995/04/17 09:31:48 adam
107 * Improved handling of qualifiers. Aliases or reserved words.
109 * Revision 1.5 1995/02/23 08:32:00 adam
112 * Revision 1.3 1995/02/15 17:42:16 adam
113 * Minor changes of the api of this module. FILE* argument added
116 * Revision 1.2 1995/02/14 19:55:13 adam
117 * Header files ccl.h/cclp.h are gone! They have been merged an
118 * moved to ../include/ccl.h.
119 * Node kind(s) in ccl_rpn_node have changed names.
121 * Revision 1.1 1995/02/13 12:35:21 adam
122 * First version of CCL. Qualifiers aren't handled yet.
132 * token_cmp: Compare token with keyword(s)
133 * kw: Keyword list. Each keyword is separated by space.
135 * return: 1 if token string matches one of the keywords in list;
138 static int token_cmp (CCL_parser cclp, const char *kw, struct ccl_token *token)
140 const char *cp1 = kw;
143 int case_sensitive = cclp->ccl_case_sensitive;
145 aliases = ccl_qual_search_special(cclp->bibset, "case");
147 case_sensitive = atoi(aliases);
150 while ((cp2 = strchr (cp1, ' ')))
152 if (token->len == (size_t) (cp2-cp1))
156 if (!memcmp (cp1, token->name, token->len))
161 if (!ccl_memicmp (cp1, token->name, token->len))
168 return token->len == strlen(cp1)
169 && !memcmp (cp1, token->name, token->len);
170 return token->len == strlen(cp1) &&
171 !ccl_memicmp (cp1, token->name, token->len);
175 * ccl_token_simple: tokenize CCL raw tokens
177 struct ccl_token *ccl_token_simple (const char *command)
179 const char *cp = command;
180 struct ccl_token *first = NULL;
181 struct ccl_token *last = NULL;
185 while (*cp && strchr (" \t\r\n", *cp))
192 first = last = (struct ccl_token *)malloc (sizeof (*first));
198 last->next = (struct ccl_token *)malloc (sizeof(*first));
199 ccl_assert (last->next);
200 last->next->prev = last;
209 last->kind = CCL_TOK_EOL;
212 last->kind = CCL_TOK_TERM;
215 while (*cp && *cp != '\"')
224 while (*cp && !strchr (" \t\n\r", *cp))
229 last->kind = CCL_TOK_TERM;
237 * ccl_tokenize: tokenize CCL command string.
238 * return: CCL token list.
240 struct ccl_token *ccl_parser_tokenize (CCL_parser cclp, const char *command)
243 const char *cp = command;
244 struct ccl_token *first = NULL;
245 struct ccl_token *last = NULL;
249 while (*cp && strchr (" \t\r\n", *cp))
256 first = last = (struct ccl_token *)malloc (sizeof (*first));
262 last->next = (struct ccl_token *)malloc (sizeof(*first));
263 ccl_assert (last->next);
264 last->next->prev = last;
273 last->kind = CCL_TOK_EOL;
276 last->kind = CCL_TOK_LP;
279 last->kind = CCL_TOK_RP;
282 last->kind = CCL_TOK_COMMA;
286 last->kind = CCL_TOK_PROX;
287 while (*cp == '%' || *cp == '!')
296 if (*cp == '=' || *cp == '<' || *cp == '>')
299 last->kind = CCL_TOK_REL;
302 else if (cp[-1] == '=')
303 last->kind = CCL_TOK_EQ;
305 last->kind = CCL_TOK_REL;
308 last->kind = CCL_TOK_TERM;
311 while (*cp && *cp != '\"')
320 while (*cp && !strchr ("(),%!><=- \t\n\r", *cp))
325 last->kind = CCL_TOK_TERM;
327 aliases = ccl_qual_search_special(cclp->bibset, "and");
329 aliases = cclp->ccl_token_and;
330 if (token_cmp (cclp, aliases, last))
331 last->kind = CCL_TOK_AND;
333 aliases = ccl_qual_search_special(cclp->bibset, "or");
335 aliases = cclp->ccl_token_or;
336 if (token_cmp (cclp, aliases, last))
337 last->kind = CCL_TOK_OR;
339 aliases = ccl_qual_search_special(cclp->bibset, "not");
341 aliases = cclp->ccl_token_not;
342 if (token_cmp (cclp, aliases, last))
343 last->kind = CCL_TOK_NOT;
345 aliases = ccl_qual_search_special(cclp->bibset, "set");
347 aliases = cclp->ccl_token_set;
349 if (token_cmp (cclp, aliases, last))
350 last->kind = CCL_TOK_SET;
356 struct ccl_token *ccl_tokenize (const char *command)
358 CCL_parser cclp = ccl_parser_create ();
359 struct ccl_token *list;
361 list = ccl_parser_tokenize (cclp, command);
363 ccl_parser_destroy (cclp);
368 * ccl_token_del: delete CCL tokens
370 void ccl_token_del (struct ccl_token *list)
372 struct ccl_token *list1;
382 char *ccl_strdup (const char *str)
384 int len = strlen(str);
385 char *p = (char*) malloc (len+1);
390 CCL_parser ccl_parser_create (void)
392 CCL_parser p = (CCL_parser)malloc (sizeof(*p));
395 p->look_token = NULL;
400 p->ccl_token_and = ccl_strdup("and");
401 p->ccl_token_or = ccl_strdup("or");
402 p->ccl_token_not = ccl_strdup("not andnot");
403 p->ccl_token_set = ccl_strdup("set");
404 p->ccl_case_sensitive = 1;
409 void ccl_parser_destroy (CCL_parser p)
413 free (p->ccl_token_and);
414 free (p->ccl_token_or);
415 free (p->ccl_token_not);
416 free (p->ccl_token_set);
420 void ccl_parser_set_op_and (CCL_parser p, const char *op)
424 if (p->ccl_token_and)
425 free (p->ccl_token_and);
426 p->ccl_token_and = ccl_strdup (op);
430 void ccl_parser_set_op_or (CCL_parser p, const char *op)
435 free (p->ccl_token_or);
436 p->ccl_token_or = ccl_strdup (op);
439 void ccl_parser_set_op_not (CCL_parser p, const char *op)
443 if (p->ccl_token_not)
444 free (p->ccl_token_not);
445 p->ccl_token_not = ccl_strdup (op);
448 void ccl_parser_set_op_set (CCL_parser p, const char *op)
452 if (p->ccl_token_set)
453 free (p->ccl_token_set);
454 p->ccl_token_set = ccl_strdup (op);
458 void ccl_parser_set_case (CCL_parser p, int case_sensitivity_flag)
461 p->ccl_case_sensitive = case_sensitivity_flag;