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