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