String attributes for CCL parser
[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.14 2003-06-23 10:22:21 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
92 YAZ_BEGIN_CDECL
93     
94 /* CCL error numbers */
95 #define CCL_ERR_OK                0
96 #define CCL_ERR_TERM_EXPECTED     1
97 #define CCL_ERR_RP_EXPECTED       2
98 #define CCL_ERR_SETNAME_EXPECTED  3
99 #define CCL_ERR_OP_EXPECTED       4
100 #define CCL_ERR_BAD_RP            5
101 #define CCL_ERR_UNKNOWN_QUAL      6
102 #define CCL_ERR_DOUBLE_QUAL       7
103 #define CCL_ERR_EQ_EXPECTED       8
104 #define CCL_ERR_BAD_RELATION      9
105 #define CCL_ERR_TRUNC_NOT_LEFT   10
106 #define CCL_ERR_TRUNC_NOT_BOTH   11
107 #define CCL_ERR_TRUNC_NOT_RIGHT  12
108     
109 /* attribute pair (type, value) */
110 struct ccl_rpn_attr {
111     struct ccl_rpn_attr *next;
112     char *set;
113     int type;
114     int kind;
115 #define CCL_RPN_ATTR_NUMERIC 1
116 #define CCL_RPN_ATTR_STRING 2
117     union {
118         int numeric;
119         char *str;
120     } value;
121 };
122
123 #define CCL_RPN_AND 1
124 #define CCL_RPN_OR 2
125 #define CCL_RPN_NOT 3
126 #define CCL_RPN_TERM 4
127 #define CCL_RPN_SET 5
128 #define CCL_RPN_PROX 6
129
130 /* RPN tree structure */
131 struct ccl_rpn_node {
132     int kind;
133     union {
134         struct ccl_rpn_node *p[3];
135         struct {
136             char *term;
137             struct ccl_rpn_attr *attr_list;
138         } t;
139         char *setname;
140     } u;
141 };
142
143 typedef struct ccl_qualifiers *CCL_bibset;
144
145 /* use (1)
146
147    relation (2)
148                             -1  none
149                              0  ordered
150                            1-6  relation (<, <=, =, >=, >, <>)
151
152    position (3)
153                             -1  none
154                              1  first in field
155                              2  first in sub field
156                              3  any position in field
157    structure (4)
158                             -1  none
159                              0  word/phrase auto select
160                              1  phrase
161                              2  word
162                              3  key
163                              4  year
164                              5  date (normalized)
165                              6  word list 
166                            100  date (un-normalized)
167                            101  name (normalized)
168                            102  name (un-normalized)
169    truncation (5)                            
170    completeness (6)
171 */
172
173 #define CCL_BIB1_USE 1
174 #define CCL_BIB1_REL 2
175 #define CCL_BIB1_POS 3
176 #define CCL_BIB1_STR 4
177 #define CCL_BIB1_TRU 5
178 #define CCL_BIB1_COM 6
179
180 #define CCL_BIB1_STR_WP (-1)
181 #define CCL_BIB1_STR_AND_LIST (-2)
182 #define CCL_BIB1_STR_OR_LIST (-3)
183 #define CCL_BIB1_REL_ORDER (-1)
184
185 #define CCL_BIB1_TRU_CAN_LEFT (-1)
186 #define CCL_BIB1_TRU_CAN_RIGHT (-2)
187 #define CCL_BIB1_TRU_CAN_BOTH  (-3)
188 #define CCL_BIB1_TRU_CAN_NONE  (-4)
189
190 #define CCL_TOK_EOL   0
191 #define CCL_TOK_TERM  1
192 #define CCL_TOK_REL   2
193 #define CCL_TOK_EQ    3
194 #define CCL_TOK_PROX  4
195 #define CCL_TOK_LP    5
196 #define CCL_TOK_RP    6
197 #define CCL_TOK_COMMA 7
198 #define CCL_TOK_AND   8
199 #define CCL_TOK_OR    9
200 #define CCL_TOK_NOT   10
201 #define CCL_TOK_SET   11
202
203 /* CCL token */
204 struct ccl_token {
205     char kind;
206     size_t len;
207     const char *name;
208     struct ccl_token *next;
209     struct ccl_token *prev;
210 };
211
212 /* CCL Qualifier */
213 struct ccl_qualifier {
214     char *name;
215     int no_sub;
216     struct ccl_qualifier **sub;
217     struct ccl_rpn_attr *attr_list;
218     struct ccl_qualifier *next;
219 };
220
221 struct ccl_parser {
222 /* current lookahead token */
223     struct ccl_token *look_token;
224     
225 /* holds error code if error occur (and approx position of error) */
226     int error_code;
227     const char *error_pos;
228     
229 /* current bibset */
230     CCL_bibset bibset;
231
232     char *ccl_token_and;
233     char *ccl_token_or;
234     char *ccl_token_not;
235     char *ccl_token_set;
236     int ccl_case_sensitive;
237 };
238     
239 typedef struct ccl_parser *CCL_parser;
240     
241 /* Generate tokens from command string - obeys all CCL opererators */
242 YAZ_EXPORT struct ccl_token *ccl_parser_tokenize (CCL_parser cclp,
243                                        const char *command);
244 YAZ_EXPORT struct ccl_token *ccl_tokenize (const char *command);
245     
246 /* Generate tokens from command string - oebeys only simple tokens and 
247    quoted strings */
248 YAZ_EXPORT struct ccl_token *ccl_token_simple (const char *command);
249
250 /* Delete token list */
251 YAZ_EXPORT void ccl_token_del (struct ccl_token *list);
252
253 /* Parse CCL Find command - NULL-terminated string */
254 YAZ_EXPORT struct ccl_rpn_node *ccl_find_str (CCL_bibset bibset,
255                                    const char *str, int *error, int *pos);
256
257 /* Parse CCL Find command - Tokens read by ccl_tokenize */
258 YAZ_EXPORT struct ccl_rpn_node *ccl_find (CCL_bibset abibset, struct ccl_token *list,
259                                int *error, const char **pos);
260
261 /* Parse CCL Find command */
262 YAZ_EXPORT struct ccl_rpn_node *ccl_parser_find (CCL_parser cclp, struct ccl_token *list);
263
264 /* Set various OPs */
265 YAZ_EXPORT void ccl_parser_set_op_and (CCL_parser p, const char *op);
266 YAZ_EXPORT void ccl_parser_set_op_or (CCL_parser p, const char *op);
267 YAZ_EXPORT void ccl_parser_set_op_not (CCL_parser p, const char *op);
268 YAZ_EXPORT void ccl_parser_set_op_set (CCL_parser p, const char *op);
269
270 YAZ_EXPORT void ccl_parser_set_case (CCL_parser p, int case_sensitivity_flag);
271
272 /* Return english-readable error message */
273 YAZ_EXPORT const char *ccl_err_msg (int ccl_errno);
274
275 /* Delete RPN tree returned by ccl_find */
276 YAZ_EXPORT void ccl_rpn_delete (struct ccl_rpn_node *rpn);
277
278 /* Dump RPN tree in readable format to fd_out */
279 YAZ_EXPORT void ccl_pr_tree (struct ccl_rpn_node *rpn, FILE *fd_out);
280
281 /* Add CCL qualifier */
282 YAZ_EXPORT void ccl_qual_add (CCL_bibset b, const char *name, int no,
283                               int *attr);
284
285 YAZ_EXPORT void ccl_qual_add_set (CCL_bibset b, const char *name, int no,
286                                   int *type, int *value, char **svalue,
287                                   char **attsets);
288
289 YAZ_EXPORT void ccl_qual_add_special (CCL_bibset bibset,
290                                       const char *n, const char *v);
291
292 YAZ_EXPORT void ccl_qual_add_combi (CCL_bibset b, const char *n,
293                                     const char *names);
294
295 /* Read CCL qualifier list spec from file inf */
296 YAZ_EXPORT void ccl_qual_file (CCL_bibset bibset, FILE *inf);
297
298 /* Read CCL qualifier list spec from file inf */
299 YAZ_EXPORT int ccl_qual_fname (CCL_bibset bibset, const char *fname);
300
301 /* Add CCL qualifier by using single-line spec */
302 YAZ_EXPORT void ccl_qual_fitem (CCL_bibset bibset, const char *cp,
303                                 const char *qual_name);
304
305 /* Make CCL qualifier set */
306 YAZ_EXPORT CCL_bibset ccl_qual_mk (void);
307
308 /* Delete CCL qualifier set */
309 YAZ_EXPORT void ccl_qual_rm (CCL_bibset *b);
310
311 /* Char-to-upper function */
312 extern int (*ccl_toupper)(int c);
313
314 /* String utilities */
315 YAZ_EXPORT int ccl_stricmp (const char *s1, const char *s2);
316 YAZ_EXPORT int ccl_memicmp (const char *s1, const char *s2, size_t n);
317
318 /* Search for qualifier 'name' in set 'b'. */
319 YAZ_EXPORT struct ccl_rpn_attr *ccl_qual_search (CCL_parser cclp,
320                                                  const char *name,
321                                                  size_t len,
322                                                  int seq);
323
324 /* Create CCL parser */
325 YAZ_EXPORT CCL_parser ccl_parser_create (void);
326
327 /* Destroy CCL parser */
328 YAZ_EXPORT void ccl_parser_destroy (CCL_parser p);
329
330 YAZ_EXPORT char *ccl_strdup (const char *str);
331
332 YAZ_EXPORT const char *ccl_qual_search_special (CCL_bibset b,
333                                                 const char *name);
334
335 #ifndef ccl_assert
336 #define ccl_assert(x) ;
337 #endif
338
339 YAZ_END_CDECL
340
341 #endif
342