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