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