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