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.13 2000-02-08 10:39:53 adam
49 * Added a few functions to set name of operands, etc.
51 * Revision 1.12 2000/01/31 13:15:21 adam
52 * Removed uses of assert(3). Cleanup of ODR. CCL parser update so
53 * that some characters are not surrounded by spaces in resulting term.
56 * Revision 1.11 1999/11/30 13:47:11 adam
57 * Improved installation. Moved header files to include/yaz.
59 * Revision 1.10 1998/07/07 15:49:41 adam
60 * Added braces to avoid warning.
62 * Revision 1.9 1998/02/11 11:53:33 adam
63 * Changed code so that it compiles as C++.
65 * Revision 1.8 1997/09/29 08:56:38 adam
66 * Changed CCL parser to be thread safe. New type, CCL_parser, declared
67 * and a create/destructers ccl_parser_create/ccl_parser/destory has
70 * Revision 1.7 1997/09/01 08:48:12 adam
71 * New windows NT/95 port using MSV5.0. Only a few changes made
74 * Revision 1.6 1997/04/30 08:52:07 quinn
77 * Revision 1.5 1996/10/11 15:00:26 adam
78 * CCL parser from Europagate Email gateway 1.0.
80 * Revision 1.10 1995/07/11 12:28:31 adam
81 * New function: ccl_token_simple (split into simple tokens) and
82 * ccl_token_del (delete tokens).
84 * Revision 1.9 1995/05/16 09:39:28 adam
87 * Revision 1.8 1995/05/11 14:03:57 adam
88 * Changes in the reading of qualifier(s). New function: ccl_qual_fitem.
89 * New variable ccl_case_sensitive, which controls whether reserved
90 * words and field names are case sensitive or not.
92 * Revision 1.7 1995/04/19 12:11:24 adam
95 * Revision 1.6 1995/04/17 09:31:48 adam
96 * Improved handling of qualifiers. Aliases or reserved words.
98 * Revision 1.5 1995/02/23 08:32:00 adam
101 * Revision 1.3 1995/02/15 17:42:16 adam
102 * Minor changes of the api of this module. FILE* argument added
105 * Revision 1.2 1995/02/14 19:55:13 adam
106 * Header files ccl.h/cclp.h are gone! They have been merged an
107 * moved to ../include/ccl.h.
108 * Node kind(s) in ccl_rpn_node have changed names.
110 * Revision 1.1 1995/02/13 12:35:21 adam
111 * First version of CCL. Qualifiers aren't handled yet.
122 * token_cmp: Compare token with keyword(s)
123 * kw: Keyword list. Each keyword is separated by space.
125 * return: 1 if token string matches one of the keywords in list;
128 static int token_cmp (CCL_parser cclp, const char *kw, struct ccl_token *token)
130 const char *cp1 = kw;
134 while ((cp2 = strchr (cp1, ' ')))
136 if (token->len == (size_t) (cp2-cp1))
138 if (cclp->ccl_case_sensitive)
140 if (!memcmp (cp1, token->name, token->len))
145 if (!ccl_memicmp (cp1, token->name, token->len))
151 if (cclp->ccl_case_sensitive)
152 return token->len == strlen(cp1)
153 && !memcmp (cp1, token->name, token->len);
154 return token->len == strlen(cp1) &&
155 !ccl_memicmp (cp1, token->name, token->len);
159 * ccl_token_simple: tokenize CCL raw tokens
161 struct ccl_token *ccl_token_simple (const char *command)
163 const char *cp = command;
164 struct ccl_token *first = NULL;
165 struct ccl_token *last = NULL;
169 while (*cp && strchr (" \t\r\n", *cp))
176 first = last = (struct ccl_token *)malloc (sizeof (*first));
182 last->next = (struct ccl_token *)malloc (sizeof(*first));
183 ccl_assert (last->next);
184 last->next->prev = last;
193 last->kind = CCL_TOK_EOL;
196 last->kind = CCL_TOK_TERM;
199 while (*cp && *cp != '\"')
208 while (*cp && !strchr (" \t\n\r", *cp))
213 last->kind = CCL_TOK_TERM;
221 * ccl_tokenize: tokenize CCL command string.
222 * return: CCL token list.
224 struct ccl_token *ccl_parser_tokenize (CCL_parser cclp, const char *command)
226 const char *cp = command;
227 struct ccl_token *first = NULL;
228 struct ccl_token *last = NULL;
232 while (*cp && strchr (" \t\r\n", *cp))
239 first = last = (struct ccl_token *)malloc (sizeof (*first));
245 last->next = (struct ccl_token *)malloc (sizeof(*first));
246 ccl_assert (last->next);
247 last->next->prev = last;
256 last->kind = CCL_TOK_EOL;
259 last->kind = CCL_TOK_LP;
262 last->kind = CCL_TOK_RP;
265 last->kind = CCL_TOK_COMMA;
269 last->kind = CCL_TOK_PROX;
270 while (*cp == '%' || *cp == '!')
279 if (*cp == '=' || *cp == '<' || *cp == '>')
282 last->kind = CCL_TOK_REL;
285 else if (cp[-1] == '=')
286 last->kind = CCL_TOK_EQ;
288 last->kind = CCL_TOK_REL;
291 last->kind = CCL_TOK_MINUS;
294 last->kind = CCL_TOK_TERM;
297 while (*cp && *cp != '\"')
306 while (*cp && !strchr ("(),%!><=- \t\n\r", *cp))
311 if (token_cmp (cclp, cclp->ccl_token_and, last))
312 last->kind = CCL_TOK_AND;
313 else if (token_cmp (cclp, cclp->ccl_token_or, last))
314 last->kind = CCL_TOK_OR;
315 else if (token_cmp (cclp, cclp->ccl_token_not, last))
316 last->kind = CCL_TOK_NOT;
317 else if (token_cmp (cclp, cclp->ccl_token_set, last))
318 last->kind = CCL_TOK_SET;
320 last->kind = CCL_TOK_TERM;
326 struct ccl_token *ccl_tokenize (const char *command)
328 CCL_parser cclp = ccl_parser_create ();
329 struct ccl_token *list;
331 list = ccl_parser_tokenize (cclp, command);
333 ccl_parser_destroy (cclp);
338 * ccl_token_del: delete CCL tokens
340 void ccl_token_del (struct ccl_token *list)
342 struct ccl_token *list1;
352 static char *ccl_strdup (const char *str)
354 int len = strlen(str);
355 char *p = (char*) malloc (len+1);
360 CCL_parser ccl_parser_create (void)
362 CCL_parser p = (CCL_parser)malloc (sizeof(*p));
365 p->look_token = NULL;
370 p->ccl_token_and = ccl_strdup("and");
371 p->ccl_token_or = ccl_strdup("or");
372 p->ccl_token_not = ccl_strdup("not andnot");
373 p->ccl_token_set = ccl_strdup("set");
374 p->ccl_case_sensitive = 1;
379 void ccl_parser_destroy (CCL_parser p)
383 free (p->ccl_token_and);
384 free (p->ccl_token_or);
385 free (p->ccl_token_not);
386 free (p->ccl_token_set);
390 void ccl_parser_set_op_and (CCL_parser p, const char *op)
393 p->ccl_token_and = ccl_strdup (op);
396 void ccl_parser_set_op_or (CCL_parser p, const char *op)
399 p->ccl_token_or = ccl_strdup (op);
401 void ccl_parser_set_op_not (CCL_parser p, const char *op)
404 p->ccl_token_not = ccl_strdup (op);
406 void ccl_parser_set_op_set (CCL_parser p, const char *op)
409 p->ccl_token_set = ccl_strdup (op);
412 void ccl_parser_set_case (CCL_parser p, int case_sensitivity_flag)
415 p->ccl_case_sensitive = case_sensitivity_flag;