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