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