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