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