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.17 2001-10-03 23:54:41 adam
49 * Fixes for numeric ranges (date=1980-1990).
51 * Revision 1.16 2001/03/07 13:24:40 adam
52 * Member and_not in Z_Operator is kept for backwards compatibility.
53 * Added support for definition of CCL operators in field spec file.
55 * Revision 1.15 2000/05/01 09:36:50 adam
56 * Range operator only treated in ordered ranges so that minus (-) can be
57 * used for, say, the and-not operator.
59 * Revision 1.14 2000/03/14 09:06:11 adam
60 * Added POSIX threads support for frontend server.
62 * Revision 1.13 2000/02/08 10:39:53 adam
63 * Added a few functions to set name of operands, etc.
65 * Revision 1.12 2000/01/31 13:15:21 adam
66 * Removed uses of assert(3). Cleanup of ODR. CCL parser update so
67 * that some characters are not surrounded by spaces in resulting term.
70 * Revision 1.11 1999/11/30 13:47:11 adam
71 * Improved installation. Moved header files to include/yaz.
73 * Revision 1.10 1998/07/07 15:49:41 adam
74 * Added braces to avoid warning.
76 * Revision 1.9 1998/02/11 11:53:33 adam
77 * Changed code so that it compiles as C++.
79 * Revision 1.8 1997/09/29 08:56:38 adam
80 * Changed CCL parser to be thread safe. New type, CCL_parser, declared
81 * and a create/destructers ccl_parser_create/ccl_parser/destory has
84 * Revision 1.7 1997/09/01 08:48:12 adam
85 * New windows NT/95 port using MSV5.0. Only a few changes made
88 * Revision 1.6 1997/04/30 08:52:07 quinn
91 * Revision 1.5 1996/10/11 15:00:26 adam
92 * CCL parser from Europagate Email gateway 1.0.
94 * Revision 1.10 1995/07/11 12:28:31 adam
95 * New function: ccl_token_simple (split into simple tokens) and
96 * ccl_token_del (delete tokens).
98 * Revision 1.9 1995/05/16 09:39:28 adam
101 * Revision 1.8 1995/05/11 14:03:57 adam
102 * Changes in the reading of qualifier(s). New function: ccl_qual_fitem.
103 * New variable ccl_case_sensitive, which controls whether reserved
104 * words and field names are case sensitive or not.
106 * Revision 1.7 1995/04/19 12:11:24 adam
109 * Revision 1.6 1995/04/17 09:31:48 adam
110 * Improved handling of qualifiers. Aliases or reserved words.
112 * Revision 1.5 1995/02/23 08:32:00 adam
115 * Revision 1.3 1995/02/15 17:42:16 adam
116 * Minor changes of the api of this module. FILE* argument added
119 * Revision 1.2 1995/02/14 19:55:13 adam
120 * Header files ccl.h/cclp.h are gone! They have been merged an
121 * moved to ../include/ccl.h.
122 * Node kind(s) in ccl_rpn_node have changed names.
124 * Revision 1.1 1995/02/13 12:35:21 adam
125 * First version of CCL. Qualifiers aren't handled yet.
135 * token_cmp: Compare token with keyword(s)
136 * kw: Keyword list. Each keyword is separated by space.
138 * return: 1 if token string matches one of the keywords in list;
141 static int token_cmp (CCL_parser cclp, const char *kw, struct ccl_token *token)
143 const char *cp1 = kw;
146 int case_sensitive = cclp->ccl_case_sensitive;
148 aliases = ccl_qual_search_special(cclp->bibset, "case");
150 case_sensitive = atoi(aliases);
153 while ((cp2 = strchr (cp1, ' ')))
155 if (token->len == (size_t) (cp2-cp1))
159 if (!memcmp (cp1, token->name, token->len))
164 if (!ccl_memicmp (cp1, token->name, token->len))
171 return token->len == strlen(cp1)
172 && !memcmp (cp1, token->name, token->len);
173 return token->len == strlen(cp1) &&
174 !ccl_memicmp (cp1, token->name, token->len);
178 * ccl_token_simple: tokenize CCL raw tokens
180 struct ccl_token *ccl_token_simple (const char *command)
182 const char *cp = command;
183 struct ccl_token *first = NULL;
184 struct ccl_token *last = NULL;
188 while (*cp && strchr (" \t\r\n", *cp))
195 first = last = (struct ccl_token *)malloc (sizeof (*first));
201 last->next = (struct ccl_token *)malloc (sizeof(*first));
202 ccl_assert (last->next);
203 last->next->prev = last;
212 last->kind = CCL_TOK_EOL;
215 last->kind = CCL_TOK_TERM;
218 while (*cp && *cp != '\"')
227 while (*cp && !strchr (" \t\n\r", *cp))
232 last->kind = CCL_TOK_TERM;
240 * ccl_tokenize: tokenize CCL command string.
241 * return: CCL token list.
243 struct ccl_token *ccl_parser_tokenize (CCL_parser cclp, const char *command)
246 const char *cp = command;
247 struct ccl_token *first = NULL;
248 struct ccl_token *last = NULL;
252 while (*cp && strchr (" \t\r\n", *cp))
259 first = last = (struct ccl_token *)malloc (sizeof (*first));
265 last->next = (struct ccl_token *)malloc (sizeof(*first));
266 ccl_assert (last->next);
267 last->next->prev = last;
276 last->kind = CCL_TOK_EOL;
279 last->kind = CCL_TOK_LP;
282 last->kind = CCL_TOK_RP;
285 last->kind = CCL_TOK_COMMA;
289 last->kind = CCL_TOK_PROX;
290 while (*cp == '%' || *cp == '!')
299 if (*cp == '=' || *cp == '<' || *cp == '>')
302 last->kind = CCL_TOK_REL;
305 else if (cp[-1] == '=')
306 last->kind = CCL_TOK_EQ;
308 last->kind = CCL_TOK_REL;
311 last->kind = CCL_TOK_TERM;
314 while (*cp && *cp != '\"')
323 if (!strchr ("(),%!><=- \t\n\r", cp[-1]))
325 while (*cp && !strchr ("(),%!><=- \t\n\r", *cp))
331 last->kind = CCL_TOK_TERM;
333 aliases = ccl_qual_search_special(cclp->bibset, "and");
335 aliases = cclp->ccl_token_and;
336 if (token_cmp (cclp, aliases, last))
337 last->kind = CCL_TOK_AND;
339 aliases = ccl_qual_search_special(cclp->bibset, "or");
341 aliases = cclp->ccl_token_or;
342 if (token_cmp (cclp, aliases, last))
343 last->kind = CCL_TOK_OR;
345 aliases = ccl_qual_search_special(cclp->bibset, "not");
347 aliases = cclp->ccl_token_not;
348 if (token_cmp (cclp, aliases, last))
349 last->kind = CCL_TOK_NOT;
351 aliases = ccl_qual_search_special(cclp->bibset, "set");
353 aliases = cclp->ccl_token_set;
355 if (token_cmp (cclp, aliases, last))
356 last->kind = CCL_TOK_SET;
362 struct ccl_token *ccl_tokenize (const char *command)
364 CCL_parser cclp = ccl_parser_create ();
365 struct ccl_token *list;
367 list = ccl_parser_tokenize (cclp, command);
369 ccl_parser_destroy (cclp);
374 * ccl_token_del: delete CCL tokens
376 void ccl_token_del (struct ccl_token *list)
378 struct ccl_token *list1;
388 char *ccl_strdup (const char *str)
390 int len = strlen(str);
391 char *p = (char*) malloc (len+1);
396 CCL_parser ccl_parser_create (void)
398 CCL_parser p = (CCL_parser)malloc (sizeof(*p));
401 p->look_token = NULL;
406 p->ccl_token_and = ccl_strdup("and");
407 p->ccl_token_or = ccl_strdup("or");
408 p->ccl_token_not = ccl_strdup("not andnot");
409 p->ccl_token_set = ccl_strdup("set");
410 p->ccl_case_sensitive = 1;
415 void ccl_parser_destroy (CCL_parser p)
419 free (p->ccl_token_and);
420 free (p->ccl_token_or);
421 free (p->ccl_token_not);
422 free (p->ccl_token_set);
426 void ccl_parser_set_op_and (CCL_parser p, const char *op)
430 if (p->ccl_token_and)
431 free (p->ccl_token_and);
432 p->ccl_token_and = ccl_strdup (op);
436 void ccl_parser_set_op_or (CCL_parser p, const char *op)
441 free (p->ccl_token_or);
442 p->ccl_token_or = ccl_strdup (op);
445 void ccl_parser_set_op_not (CCL_parser p, const char *op)
449 if (p->ccl_token_not)
450 free (p->ccl_token_not);
451 p->ccl_token_not = ccl_strdup (op);
454 void ccl_parser_set_op_set (CCL_parser p, const char *op)
458 if (p->ccl_token_set)
459 free (p->ccl_token_set);
460 p->ccl_token_set = ccl_strdup (op);
464 void ccl_parser_set_case (CCL_parser p, int case_sensitivity_flag)
467 p->ccl_case_sensitive = case_sensitivity_flag;