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