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