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