CCL parser from Europagate Email gateway 1.0.
[yaz-moved-to-github.git] / ccl / ccltoken.c
1 /*
2  * Copyright (c) 1995, the EUROPAGATE consortium (see below).
3  *
4  * The EUROPAGATE consortium members are:
5  *
6  *    University College Dublin
7  *    Danmarks Teknologiske Videnscenter
8  *    An Chomhairle Leabharlanna
9  *    Consejo Superior de Investigaciones Cientificas
10  *
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,
13  * provided that:
14  *
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.
18  *
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.
22  *
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.
28  * 
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.
32  *
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.
42  *
43  */
44 /* CCL - lexical analysis
45  * Europagate, 1995
46  *
47  * $Log: ccltoken.c,v $
48  * Revision 1.5  1996-10-11 15:00:26  adam
49  * CCL parser from Europagate Email gateway 1.0.
50  *
51  * Revision 1.10  1995/07/11  12:28:31  adam
52  * New function: ccl_token_simple (split into simple tokens) and
53  *  ccl_token_del (delete tokens).
54  *
55  * Revision 1.9  1995/05/16  09:39:28  adam
56  * LICENSE.
57  *
58  * Revision 1.8  1995/05/11  14:03:57  adam
59  * Changes in the reading of qualifier(s). New function: ccl_qual_fitem.
60  * New variable ccl_case_sensitive, which controls whether reserved
61  * words and field names are case sensitive or not.
62  *
63  * Revision 1.7  1995/04/19  12:11:24  adam
64  * Minor change.
65  *
66  * Revision 1.6  1995/04/17  09:31:48  adam
67  * Improved handling of qualifiers. Aliases or reserved words.
68  *
69  * Revision 1.5  1995/02/23  08:32:00  adam
70  * Changed header.
71  *
72  * Revision 1.3  1995/02/15  17:42:16  adam
73  * Minor changes of the api of this module. FILE* argument added
74  * to ccl_pr_tree.
75  *
76  * Revision 1.2  1995/02/14  19:55:13  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.
80  *
81  * Revision 1.1  1995/02/13  12:35:21  adam
82  * First version of CCL. Qualifiers aren't handled yet.
83  *
84  */
85
86 #include <stdio.h>
87 #include <string.h>
88 #include <stdlib.h>
89 #include <assert.h>
90
91 #include <ccl.h>
92
93 const char *ccl_token_and = "and";
94 const char *ccl_token_or = "or";
95 const char *ccl_token_not = "not andnot";
96 const char *ccl_token_set = "set";
97 int ccl_case_sensitive = 1;
98
99 /*
100  * token_cmp: Compare token with keyword(s)
101  * kw:     Keyword list. Each keyword is separated by space.
102  * token:  CCL token.
103  * return: 1 if token string matches one of the keywords in list;
104  *         0 otherwise.
105  */
106 static int token_cmp (const char *kw, struct ccl_token *token)
107 {
108     const char *cp1 = kw;
109     const char *cp2;
110     if (!kw)
111         return 0;
112     while ((cp2 = strchr (cp1, ' ')))
113     {
114         if (token->len == cp2-cp1)
115             if (ccl_case_sensitive)
116             {
117                 if (!memcmp (cp1, token->name, token->len))
118                     return 1;
119             }
120             else
121             {
122                 if (!ccl_memicmp (cp1, token->name, token->len))
123                     return 1;
124             }
125         cp1 = cp2+1;
126     }
127     if (ccl_case_sensitive)
128         return token->len == strlen(cp1) 
129             && !memcmp (cp1, token->name, token->len);
130     return token->len == strlen(cp1) &&
131         !ccl_memicmp (cp1, token->name, token->len);
132 }
133
134 /*
135  * ccl_token_simple: tokenize CCL raw tokens
136  */
137 struct ccl_token *ccl_token_simple (const char *command)
138 {
139     const char *cp = command;
140     struct ccl_token *first = NULL;
141     struct ccl_token *last = NULL;
142
143     while (1)
144     {
145         while (*cp && strchr (" \t\r\n", *cp))
146         {
147             cp++;
148             continue;
149         }
150         if (!first)
151         {
152             first = last = malloc (sizeof (*first));
153             assert (first);
154             last->prev = NULL;
155         }
156         else
157         {
158             last->next = malloc (sizeof(*first));
159             assert (last->next);
160             last->next->prev = last;
161             last = last->next;
162         }
163         last->next = NULL;
164         last->name = cp;
165         last->len = 1;
166         switch (*cp++)
167         {
168         case '\0':
169             last->kind = CCL_TOK_EOL;
170             return first;
171         case '\"':
172             last->kind = CCL_TOK_TERM;
173             last->name = cp;
174             last->len = 0;
175             while (*cp && *cp != '\"')
176             {
177                 cp++;
178                 ++ last->len;
179             }
180             if (*cp == '\"')
181                 cp++;
182             break;
183         default:
184             while (*cp && !strchr (" \t\n\r", *cp))
185             {
186                 cp++;
187                 ++ last->len;
188             }
189             last->kind = CCL_TOK_TERM;
190         }
191     }
192     return first;
193 }
194
195 /*
196  * ccl_tokenize: tokenize CCL command string.
197  * return: CCL token list.
198  */
199 struct ccl_token *ccl_tokenize (const char *command)
200 {
201     const char *cp = command;
202     struct ccl_token *first = NULL;
203     struct ccl_token *last = NULL;
204
205     while (1)
206     {
207         while (*cp && strchr (" \t\r\n", *cp))
208         {
209             cp++;
210             continue;
211         }
212         if (!first)
213         {
214             first = last = malloc (sizeof (*first));
215             assert (first);
216             last->prev = NULL;
217         }
218         else
219         {
220             last->next = malloc (sizeof(*first));
221             assert (last->next);
222             last->next->prev = last;
223             last = last->next;
224         }
225         last->next = NULL;
226         last->name = cp;
227         last->len = 1;
228         switch (*cp++)
229         {
230         case '\0':
231             last->kind = CCL_TOK_EOL;
232             return first;
233         case '(':
234             last->kind = CCL_TOK_LP;
235             break;
236         case ')':
237             last->kind = CCL_TOK_RP;
238             break;
239         case ',':
240             last->kind = CCL_TOK_COMMA;
241             break;
242         case '%':
243         case '!':
244             last->kind = CCL_TOK_PROX;
245             while (*cp == '%' || *cp == '!')
246             {
247                 ++ last->len;
248                 cp++;
249             }
250             break;
251         case '>':
252         case '<':
253         case '=':
254             if (*cp == '=' || *cp == '<' || *cp == '>')
255             {
256                 cp++;
257                 last->kind = CCL_TOK_REL;
258                 ++ last->len;
259             }
260             else if (cp[-1] == '=')
261                 last->kind = CCL_TOK_EQ;
262             else
263                 last->kind = CCL_TOK_REL;
264             break;
265         case '-':
266             last->kind = CCL_TOK_MINUS;
267             break;
268         case '\"':
269             last->kind = CCL_TOK_TERM;
270             last->name = cp;
271             last->len = 0;
272             while (*cp && *cp != '\"')
273             {
274                 cp++;
275                 ++ last->len;
276             }
277             if (*cp == '\"')
278                 cp++;
279             break;
280         default:
281             while (*cp && !strchr ("(),%!><=- \t\n\r", *cp))
282             {
283                 cp++;
284                 ++ last->len;
285             }
286             if (token_cmp (ccl_token_and, last))
287                 last->kind = CCL_TOK_AND;
288             else if (token_cmp (ccl_token_or, last))
289                 last->kind = CCL_TOK_OR;
290             else if (token_cmp (ccl_token_not, last))
291                 last->kind = CCL_TOK_NOT;
292             else if (token_cmp (ccl_token_set, last))
293                 last->kind = CCL_TOK_SET;
294             else
295                 last->kind = CCL_TOK_TERM;
296         }
297     }
298     return first;
299 }
300
301 /*
302  * ccl_token_del: delete CCL tokens
303  */
304 void ccl_token_del (struct ccl_token *list)
305 {
306     struct ccl_token *list1;
307
308     while (list) 
309     {
310         list1 = list->next;
311         free (list);
312         list = list1;
313     }
314 }