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