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