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