Member and_not in Z_Operator is kept for backwards compatibility.
[yaz-moved-to-github.git] / include / yaz / ccl.h
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 /*
46  * CCL - header file
47  *
48  * $Log: ccl.h,v $
49  * Revision 1.9  2001-03-07 13:24:40  adam
50  * Member and_not in Z_Operator is kept for backwards compatibility.
51  * Added support for definition of CCL operators in field spec file.
52  *
53  * Revision 1.8  2000/11/16 09:58:02  adam
54  * Implemented local AttributeSet setting for CCL field maps.
55  *
56  * Revision 1.7  2000/11/01 14:47:00  adam
57  * Added CCL support for WIN32.
58  *
59  * Revision 1.6  2000/10/17 19:50:28  adam
60  * Implemented and-list and or-list for CCL module.
61  *
62  * Revision 1.5  2000/05/02 17:19:58  adam
63  * Removed MINUS token.
64  *
65  * Revision 1.4  2000/03/14 09:06:11  adam
66  * Added POSIX threads support for frontend server.
67  *
68  * Revision 1.3  2000/02/08 10:39:53  adam
69  * Added a few functions to set name of operands, etc.
70  *
71  * Revision 1.2  2000/01/31 13:15:21  adam
72  * Removed uses of assert(3). Cleanup of ODR. CCL parser update so
73  * that some characters are not surrounded by spaces in resulting term.
74  * ILL-code updates.
75  *
76  * Revision 1.1  1999/11/30 13:47:11  adam
77  * Improved installation. Moved header files to include/yaz.
78  *
79  * Revision 1.9  1998/02/11 11:53:33  adam
80  * Changed code so that it compiles as C++.
81  *
82  * Revision 1.8  1997/09/29 09:01:19  adam
83  * Changed CCL parser to be thread safe. New type, CCL-parser, declared
84  * and a create/destructor ccl_parser_create/ccl_parser_destroy has been
85  * added.
86  *
87  * Revision 1.7  1997/09/01 08:49:47  adam
88  * New windows NT/95 port using MSV5.0. To export DLL functions the
89  * YAZ_EXPORT modifier was added. Defined in yconfig.h.
90  *
91  * Revision 1.6  1997/05/14 06:53:37  adam
92  * C++ support.
93  *
94  * Revision 1.5  1997/04/30 08:52:08  quinn
95  * Null
96  *
97  * Revision 1.4  1996/10/11  15:02:26  adam
98  * CCL parser from Europagate Email gateway 1.0.
99  *
100  * Revision 1.10  1996/01/08  08:41:22  adam
101  * Minor changes.
102  *
103  * Revision 1.9  1995/07/20  08:15:16  adam
104  * Bug fix: Token value for comma and OR were the same!
105  *
106  * Revision 1.8  1995/07/11  12:28:34  adam
107  * New function: ccl_token_simple (split into simple tokens) and
108  *  ccl_token_del (delete tokens).
109  *
110  * Revision 1.7  1995/05/16  09:39:38  adam
111  * LICENSE.
112  *
113  * Revision 1.6  1995/05/11  14:04:03  adam
114  * Changes in the reading of qualifier(s). New function: ccl_qual_fitem.
115  * New variable ccl_case_sensitive, which controls whether reserved
116  * words and field names are case sensitive or not.
117  *
118  * Revision 1.5  1995/02/23  08:32:11  adam
119  * Changed header.
120  *
121  * Revision 1.3  1995/02/16  13:20:10  adam
122  * Spell fix.
123  *
124  * Revision 1.2  1995/02/15  17:43:08  adam
125  * Minor changes to the ccl interface. Bug fix in iso2709 module.
126  *
127  * Revision 1.1  1995/02/14  19:55:21  adam
128  * Header files ccl.h/cclp.h are gone! They have been merged an
129  * moved to ../include/ccl.h.
130  *
131  */
132
133 #ifndef CCL_H
134 #define CCL_H
135
136 #include <yaz/yconfig.h>
137 #include <stdio.h>
138
139 YAZ_BEGIN_CDECL
140     
141 /* CCL error numbers */
142 #define CCL_ERR_OK                0
143 #define CCL_ERR_TERM_EXPECTED     1
144 #define CCL_ERR_RP_EXPECTED       2
145 #define CCL_ERR_SETNAME_EXPECTED  3
146 #define CCL_ERR_OP_EXPECTED       4
147 #define CCL_ERR_BAD_RP            5
148 #define CCL_ERR_UNKNOWN_QUAL      6
149 #define CCL_ERR_DOUBLE_QUAL       7
150 #define CCL_ERR_EQ_EXPECTED       8
151 #define CCL_ERR_BAD_RELATION      9
152 #define CCL_ERR_TRUNC_NOT_LEFT   10
153 #define CCL_ERR_TRUNC_NOT_BOTH   11
154 #define CCL_ERR_TRUNC_NOT_RIGHT  12
155     
156 /* attribute pair (type, value) */
157 struct ccl_rpn_attr {
158     struct ccl_rpn_attr *next;
159     char *set;
160     int type;
161     int value;
162 };
163
164 #define CCL_RPN_AND 1
165 #define CCL_RPN_OR 2
166 #define CCL_RPN_NOT 3
167 #define CCL_RPN_TERM 4
168 #define CCL_RPN_SET 5
169 #define CCL_RPN_PROX 6
170
171 /* RPN tree structure */
172 struct ccl_rpn_node {
173     int kind;
174     union {
175         struct ccl_rpn_node *p[2];
176         struct {
177             char *term;
178             struct ccl_rpn_attr *attr_list;
179         } t;
180         char *setname;
181     } u;
182 };
183
184 typedef struct ccl_qualifiers *CCL_bibset;
185
186 /* use (1)
187
188    relation (2)
189                             -1  none
190                              0  ordered
191                            1-6  relation (<, <=, =, >=, >, <>)
192
193    position (3)
194                             -1  none
195                              1  first in field
196                              2  first in sub field
197                              3  any position in field
198    structure (4)
199                             -1  none
200                              0  word/phrase auto select
201                              1  phrase
202                              2  word
203                              3  key
204                              4  year
205                              5  date (normalized)
206                              6  word list 
207                            100  date (un-normalized)
208                            101  name (normalized)
209                            102  name (un-normalized)
210    truncation (5)                            
211    completeness (6)
212 */
213
214 #define CCL_BIB1_USE 1
215 #define CCL_BIB1_REL 2
216 #define CCL_BIB1_POS 3
217 #define CCL_BIB1_STR 4
218 #define CCL_BIB1_TRU 5
219 #define CCL_BIB1_COM 6
220
221 #define CCL_BIB1_STR_WP (-1)
222 #define CCL_BIB1_STR_AND_LIST (-2)
223 #define CCL_BIB1_STR_OR_LIST (-3)
224 #define CCL_BIB1_REL_ORDER (-1)
225
226 #define CCL_BIB1_TRU_CAN_LEFT (-1)
227 #define CCL_BIB1_TRU_CAN_RIGHT (-2)
228 #define CCL_BIB1_TRU_CAN_BOTH  (-3)
229 #define CCL_BIB1_TRU_CAN_NONE  (-4)
230
231 #define CCL_TOK_EOL   0
232 #define CCL_TOK_TERM  1
233 #define CCL_TOK_REL   2
234 #define CCL_TOK_EQ    3
235 #define CCL_TOK_PROX  4
236 #define CCL_TOK_LP    5
237 #define CCL_TOK_RP    6
238 #define CCL_TOK_COMMA 7
239 #define CCL_TOK_AND   8
240 #define CCL_TOK_OR    9
241 #define CCL_TOK_NOT   10
242 #define CCL_TOK_SET   11
243
244 /* CCL token */
245 struct ccl_token {
246     char kind;
247     size_t len;
248     const char *name;
249     struct ccl_token *next;
250     struct ccl_token *prev;
251 };
252
253 /* CCL Qualifier */
254 struct ccl_qualifier {
255     char *name;
256     struct ccl_rpn_attr *attr_list;
257     struct ccl_qualifier *next;
258 };
259
260 struct ccl_parser {
261 /* current lookahead token */
262     struct ccl_token *look_token;
263     
264 /* holds error code if error occur (and approx position of error) */
265     int error_code;
266     const char *error_pos;
267     
268 /* current bibset */
269     CCL_bibset bibset;
270
271     char *ccl_token_and;
272     char *ccl_token_or;
273     char *ccl_token_not;
274     char *ccl_token_set;
275     int ccl_case_sensitive;
276 };
277     
278 typedef struct ccl_parser *CCL_parser;
279     
280 /* Generate tokens from command string - obeys all CCL opererators */
281 YAZ_EXPORT struct ccl_token *ccl_parser_tokenize (CCL_parser cclp,
282                                        const char *command);
283 YAZ_EXPORT struct ccl_token *ccl_tokenize (const char *command);
284     
285 /* Generate tokens from command string - oebeys only simple tokens and 
286    quoted strings */
287 YAZ_EXPORT struct ccl_token *ccl_token_simple (const char *command);
288
289 /* Delete token list */
290 YAZ_EXPORT void ccl_token_del (struct ccl_token *list);
291
292 /* Parse CCL Find command - NULL-terminated string */
293 YAZ_EXPORT struct ccl_rpn_node *ccl_find_str (CCL_bibset bibset,
294                                    const char *str, int *error, int *pos);
295
296 /* Parse CCL Find command - Tokens read by ccl_tokenize */
297 YAZ_EXPORT struct ccl_rpn_node *ccl_find (CCL_bibset abibset, struct ccl_token *list,
298                                int *error, const char **pos);
299
300 /* Parse CCL Find command */
301 YAZ_EXPORT struct ccl_rpn_node *ccl_parser_find (CCL_parser cclp, struct ccl_token *list);
302
303 /* Set various OPs */
304 YAZ_EXPORT void ccl_parser_set_op_and (CCL_parser p, const char *op);
305 YAZ_EXPORT void ccl_parser_set_op_or (CCL_parser p, const char *op);
306 YAZ_EXPORT void ccl_parser_set_op_not (CCL_parser p, const char *op);
307 YAZ_EXPORT void ccl_parser_set_op_set (CCL_parser p, const char *op);
308
309 YAZ_EXPORT void ccl_parser_set_case (CCL_parser p, int case_sensitivity_flag);
310
311 /* Return english-readable error message */
312 YAZ_EXPORT const char *ccl_err_msg (int ccl_errno);
313
314 /* Delete RPN tree returned by ccl_find */
315 YAZ_EXPORT void ccl_rpn_delete (struct ccl_rpn_node *rpn);
316
317 /* Dump RPN tree in readable format to fd_out */
318 YAZ_EXPORT void ccl_pr_tree (struct ccl_rpn_node *rpn, FILE *fd_out);
319
320 /* Add CCL qualifier */
321 YAZ_EXPORT void ccl_qual_add (CCL_bibset b, const char *name, int no,
322                               int *attr);
323
324 YAZ_EXPORT void ccl_qual_add_set (CCL_bibset b, const char *name, int no,
325                                   int *attr, char **attsets);
326
327 /* Read CCL qualifier list spec from file inf */
328 YAZ_EXPORT void ccl_qual_file (CCL_bibset bibset, FILE *inf);
329
330 /* Read CCL qualifier list spec from file inf */
331 YAZ_EXPORT int ccl_qual_fname (CCL_bibset bibset, const char *fname);
332
333 /* Add CCL qualifier by using single-line spec */
334 YAZ_EXPORT void ccl_qual_fitem (CCL_bibset bibset, const char *cp, const char *qual_name);
335
336 /* Make CCL qualifier set */
337 YAZ_EXPORT CCL_bibset ccl_qual_mk (void);
338
339 /* Delete CCL qualifier set */
340 YAZ_EXPORT void ccl_qual_rm (CCL_bibset *b);
341
342 /* Char-to-upper function */
343 extern int (*ccl_toupper)(int c);
344
345 /* String utilities */
346 YAZ_EXPORT int ccl_stricmp (const char *s1, const char *s2);
347 YAZ_EXPORT int ccl_memicmp (const char *s1, const char *s2, size_t n);
348
349 /* Search for qualifier 'name' in set 'b'. */
350 YAZ_EXPORT struct ccl_rpn_attr *ccl_qual_search (CCL_parser cclp, const char *name,
351                                       size_t len);
352
353 /* Create CCL parser */
354 YAZ_EXPORT CCL_parser ccl_parser_create (void);
355
356 /* Destroy CCL parser */
357 YAZ_EXPORT void ccl_parser_destroy (CCL_parser p);
358
359 YAZ_EXPORT char *ccl_strdup (const char *str);
360
361 YAZ_EXPORT const char *ccl_qual_search_special (CCL_bibset b,
362                                                 const char *name);
363
364 #ifndef ccl_assert
365 #define ccl_assert(x) ;
366 #endif
367
368 YAZ_END_CDECL
369
370 #endif
371