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