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