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