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