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