CCL proximity support
[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.21 2002-12-28 12:13:03 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
89 #include <yaz/ccl.h>
90
91 /*
92  * token_cmp: Compare token with keyword(s)
93  * kw:     Keyword list. Each keyword is separated by space.
94  * token:  CCL token.
95  * return: 1 if token string matches one of the keywords in list;
96  *         0 otherwise.
97  */
98 static int token_cmp (CCL_parser cclp, const char *kw, struct ccl_token *token)
99 {
100     const char *cp1 = kw;
101     const char *cp2;
102     const char *aliases;
103     int case_sensitive = cclp->ccl_case_sensitive;
104
105     aliases = ccl_qual_search_special(cclp->bibset, "case");
106     if (aliases)
107         case_sensitive = atoi(aliases);
108     if (!kw)
109         return 0;
110     while ((cp2 = strchr (cp1, ' ')))
111     {
112         if (token->len == (size_t) (cp2-cp1))
113         {
114             if (case_sensitive)
115             {
116                 if (!memcmp (cp1, token->name, token->len))
117                     return 1;
118             }
119             else
120             {
121                 if (!ccl_memicmp (cp1, token->name, token->len))
122                     return 1;
123             }
124         }
125         cp1 = cp2+1;
126     }
127     if (case_sensitive)
128         return token->len == strlen(cp1) 
129             && !memcmp (cp1, token->name, token->len);
130     return token->len == strlen(cp1) &&
131         !ccl_memicmp (cp1, token->name, token->len);
132 }
133
134 /*
135  * ccl_token_simple: tokenize CCL raw tokens
136  */
137 struct ccl_token *ccl_token_simple (const char *command)
138 {
139     const char *cp = command;
140     struct ccl_token *first = NULL;
141     struct ccl_token *last = NULL;
142
143     while (1)
144     {
145         while (*cp && strchr (" \t\r\n", *cp))
146         {
147             cp++;
148             continue;
149         }
150         if (!first)
151         {
152             first = last = (struct ccl_token *)xmalloc (sizeof (*first));
153             ccl_assert (first);
154             last->prev = NULL;
155         }
156         else
157         {
158             last->next = (struct ccl_token *)xmalloc (sizeof(*first));
159             ccl_assert (last->next);
160             last->next->prev = last;
161             last = last->next;
162         }
163         last->next = NULL;
164         last->name = cp;
165         last->len = 1;
166         switch (*cp++)
167         {
168         case '\0':
169             last->kind = CCL_TOK_EOL;
170             return first;
171         case '\"':
172             last->kind = CCL_TOK_TERM;
173             last->name = cp;
174             last->len = 0;
175             while (*cp && *cp != '\"')
176             {
177                 cp++;
178                 ++ last->len;
179             }
180             if (*cp == '\"')
181                 cp++;
182             break;
183         default:
184             while (*cp && !strchr (" \t\n\r", *cp))
185             {
186                 cp++;
187                 ++ last->len;
188             }
189             last->kind = CCL_TOK_TERM;
190         }
191     }
192     return first;
193 }
194
195
196 /*
197  * ccl_tokenize: tokenize CCL command string.
198  * return: CCL token list.
199  */
200 struct ccl_token *ccl_parser_tokenize (CCL_parser cclp, const char *command)
201 {
202     const char *aliases;
203     const char *cp = command;
204     struct ccl_token *first = NULL;
205     struct ccl_token *last = NULL;
206
207     while (1)
208     {
209         while (*cp && strchr (" \t\r\n", *cp))
210         {
211             cp++;
212             continue;
213         }
214         if (!first)
215         {
216             first = last = (struct ccl_token *)xmalloc (sizeof (*first));
217             ccl_assert (first);
218             last->prev = NULL;
219         }
220         else
221         {
222             last->next = (struct ccl_token *)xmalloc (sizeof(*first));
223             ccl_assert (last->next);
224             last->next->prev = last;
225             last = last->next;
226         }
227         last->next = NULL;
228         last->name = cp;
229         last->len = 1;
230         switch (*cp++)
231         {
232         case '\0':
233             last->kind = CCL_TOK_EOL;
234             return first;
235         case '(':
236             last->kind = CCL_TOK_LP;
237             break;
238         case ')':
239             last->kind = CCL_TOK_RP;
240             break;
241         case ',':
242             last->kind = CCL_TOK_COMMA;
243             break;
244         case '%':
245         case '!':
246             last->kind = CCL_TOK_PROX;
247             while (isdigit(*cp))
248             {
249                 ++ last->len;
250                 cp++;
251             }
252             break;
253         case '>':
254         case '<':
255         case '=':
256             if (*cp == '=' || *cp == '<' || *cp == '>')
257             {
258                 cp++;
259                 last->kind = CCL_TOK_REL;
260                 ++ last->len;
261             }
262             else if (cp[-1] == '=')
263                 last->kind = CCL_TOK_EQ;
264             else
265                 last->kind = CCL_TOK_REL;
266             break;
267         case '\"':
268             last->kind = CCL_TOK_TERM;
269             last->name = cp;
270             last->len = 0;
271             while (*cp && *cp != '\"')
272             {
273                 cp++;
274                 ++ last->len;
275             }
276             if (*cp == '\"')
277                 cp++;
278             break;
279         default:
280             if (!strchr ("(),%!><= \t\n\r", cp[-1]))
281             {
282                 while (*cp && !strchr ("(),%!><= \t\n\r", *cp))
283                 {
284                     cp++;
285                     ++ last->len;
286                 }
287             }
288             last->kind = CCL_TOK_TERM;
289
290             aliases = ccl_qual_search_special(cclp->bibset, "and");
291             if (!aliases)
292                 aliases = cclp->ccl_token_and;
293             if (token_cmp (cclp, aliases, last))
294                 last->kind = CCL_TOK_AND;
295
296             aliases = ccl_qual_search_special(cclp->bibset, "or");
297             if (!aliases)
298                 aliases = cclp->ccl_token_or;
299             if (token_cmp (cclp, aliases, last))
300                 last->kind = CCL_TOK_OR;
301
302             aliases = ccl_qual_search_special(cclp->bibset, "not");
303             if (!aliases)
304                 aliases = cclp->ccl_token_not;
305             if (token_cmp (cclp, aliases, last))
306                 last->kind = CCL_TOK_NOT;
307
308             aliases = ccl_qual_search_special(cclp->bibset, "set");
309             if (!aliases)
310                 aliases = cclp->ccl_token_set;
311
312             if (token_cmp (cclp, aliases, last))
313                 last->kind = CCL_TOK_SET;
314         }
315     }
316     return first;
317 }
318
319 struct ccl_token *ccl_tokenize (const char *command)
320 {
321     CCL_parser cclp = ccl_parser_create ();
322     struct ccl_token *list;
323
324     list = ccl_parser_tokenize (cclp, command);
325
326     ccl_parser_destroy (cclp);
327     return list;
328 }
329
330 /*
331  * ccl_token_del: delete CCL tokens
332  */
333 void ccl_token_del (struct ccl_token *list)
334 {
335     struct ccl_token *list1;
336
337     while (list) 
338     {
339         list1 = list->next;
340         xfree (list);
341         list = list1;
342     }
343 }
344
345 char *ccl_strdup (const char *str)
346 {
347     int len = strlen(str);
348     char *p = (char*) xmalloc (len+1);
349     strcpy (p, str);
350     return p;
351 }
352
353 CCL_parser ccl_parser_create (void)
354 {
355     CCL_parser p = (CCL_parser)xmalloc (sizeof(*p));
356     if (!p)
357         return p;
358     p->look_token = NULL;
359     p->error_code = 0;
360     p->error_pos = NULL;
361     p->bibset = NULL;
362
363     p->ccl_token_and = ccl_strdup("and");
364     p->ccl_token_or = ccl_strdup("or");
365     p->ccl_token_not = ccl_strdup("not andnot");
366     p->ccl_token_set = ccl_strdup("set");
367     p->ccl_case_sensitive = 1;
368
369     return p;
370 }
371
372 void ccl_parser_destroy (CCL_parser p)
373 {
374     if (!p)
375         return;
376     xfree (p->ccl_token_and);
377     xfree (p->ccl_token_or);
378     xfree (p->ccl_token_not);
379     xfree (p->ccl_token_set);
380     xfree (p);
381 }
382
383 void ccl_parser_set_op_and (CCL_parser p, const char *op)
384 {
385     if (p && op)
386     {
387         if (p->ccl_token_and)
388             xfree (p->ccl_token_and);
389         p->ccl_token_and = ccl_strdup (op);
390     }
391 }
392
393 void ccl_parser_set_op_or (CCL_parser p, const char *op)
394 {
395     if (p && op)
396     {
397         if (p->ccl_token_or)
398             xfree (p->ccl_token_or);
399         p->ccl_token_or = ccl_strdup (op);
400     }
401 }
402 void ccl_parser_set_op_not (CCL_parser p, const char *op)
403 {
404     if (p && op)
405     {
406         if (p->ccl_token_not)
407             xfree (p->ccl_token_not);
408         p->ccl_token_not = ccl_strdup (op);
409     }
410 }
411 void ccl_parser_set_op_set (CCL_parser p, const char *op)
412 {
413     if (p && op)
414     {
415         if (p->ccl_token_set)
416             xfree (p->ccl_token_set);
417         p->ccl_token_set = ccl_strdup (op);
418     }
419 }
420
421 void ccl_parser_set_case (CCL_parser p, int case_sensitivity_flag)
422 {
423     if (p)
424         p->ccl_case_sensitive = case_sensitivity_flag;
425 }