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