Fixed bug #272: Preserve spacing for CCL terms. The spacing is
[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 /** \file ccl.h
46     \brief Header with public definitions for CCL.
47 */
48
49 /*
50  * CCL - header file
51  *
52  * $Id: ccl.h,v 1.21 2005-03-15 16:32:52 adam Exp $
53  *
54  * Old Europagate Log:
55  *
56  * Revision 1.10  1996/01/08  08:41:22  adam
57  * Minor changes.
58  *
59  * Revision 1.9  1995/07/20  08:15:16  adam
60  * Bug fix: Token value for comma and OR were the same!
61  *
62  * Revision 1.8  1995/07/11  12:28:34  adam
63  * New function: ccl_token_simple (split into simple tokens) and
64  *  ccl_token_del (delete tokens).
65  *
66  * Revision 1.7  1995/05/16  09:39:38  adam
67  * LICENSE.
68  *
69  * Revision 1.6  1995/05/11  14:04:03  adam
70  * Changes in the reading of qualifier(s). New function: ccl_qual_fitem.
71  * New variable ccl_case_sensitive, which controls whether reserved
72  * words and field names are case sensitive or not.
73  *
74  * Revision 1.5  1995/02/23  08:32:11  adam
75  * Changed header.
76  *
77  * Revision 1.3  1995/02/16  13:20:10  adam
78  * Spell fix.
79  *
80  * Revision 1.2  1995/02/15  17:43:08  adam
81  * Minor changes to the ccl interface. Bug fix in iso2709 module.
82  *
83  * Revision 1.1  1995/02/14  19:55:21  adam
84  * Header files ccl.h/cclp.h are gone! They have been merged an
85  * moved to ../include/ccl.h.
86  *
87  */
88
89 #ifndef CCL_H
90 #define CCL_H
91
92 #include <yaz/yconfig.h>
93 #include <stdio.h>
94 #include <yaz/xmalloc.h>
95 #include <yaz/wrbuf.h>
96
97 YAZ_BEGIN_CDECL
98     
99 /* CCL error numbers */
100 #define CCL_ERR_OK                0
101 #define CCL_ERR_TERM_EXPECTED     1
102 #define CCL_ERR_RP_EXPECTED       2
103 #define CCL_ERR_SETNAME_EXPECTED  3
104 #define CCL_ERR_OP_EXPECTED       4
105 #define CCL_ERR_BAD_RP            5
106 #define CCL_ERR_UNKNOWN_QUAL      6
107 #define CCL_ERR_DOUBLE_QUAL       7
108 #define CCL_ERR_EQ_EXPECTED       8
109 #define CCL_ERR_BAD_RELATION      9
110 #define CCL_ERR_TRUNC_NOT_LEFT   10
111 #define CCL_ERR_TRUNC_NOT_BOTH   11
112 #define CCL_ERR_TRUNC_NOT_RIGHT  12
113     
114 /** attribute node (type, value) pair as used in RPN */
115 struct ccl_rpn_attr {
116     /** next node */
117     struct ccl_rpn_attr *next;
118     /** attribute set */
119     char *set;
120     /** attribute type, Bib-1: 1=use, 2=relation, 3=position, .. */
121     int type;
122     /** attribute value type (numeric or string) */
123     int kind;
124 #define CCL_RPN_ATTR_NUMERIC 1
125 #define CCL_RPN_ATTR_STRING 2
126     union {
127         /** numeric attribute value */
128         int numeric;
129         /** string attribute value */
130         char *str;
131     } value;
132 };
133
134 #define CCL_RPN_AND 1
135 #define CCL_RPN_OR 2
136 #define CCL_RPN_NOT 3
137 #define CCL_RPN_TERM 4
138 #define CCL_RPN_SET 5
139 #define CCL_RPN_PROX 6
140
141 /** RPN tree structure node */
142 struct ccl_rpn_node {
143     /** RPN tree node type */
144     int kind;
145     union {
146         /** Boolean including proximity 0=left, 2=right, 3=prox parms */
147         struct ccl_rpn_node *p[3];
148         /** Attributes + Term */
149         struct {
150             char *term;
151             struct ccl_rpn_attr *attr_list;
152         } t;
153         /** Result set */
154         char *setname;
155     } u;
156 };
157
158 typedef struct ccl_qualifiers *CCL_bibset;
159
160 /* use (1)
161
162    relation (2)
163                             -1  none
164                              0  ordered
165                            1-6  relation (<, <=, =, >=, >, <>)
166
167    position (3)
168                             -1  none
169                              1  first in field
170                              2  first in sub field
171                              3  any position in field
172    structure (4)
173                             -1  none
174                              0  word/phrase auto select
175                              1  phrase
176                              2  word
177                              3  key
178                              4  year
179                              5  date (normalized)
180                              6  word list 
181                            100  date (un-normalized)
182                            101  name (normalized)
183                            102  name (un-normalized)
184    truncation (5)                            
185    completeness (6)
186 */
187
188 #define CCL_BIB1_USE 1
189 #define CCL_BIB1_REL 2
190 #define CCL_BIB1_POS 3
191 #define CCL_BIB1_STR 4
192 #define CCL_BIB1_TRU 5
193 #define CCL_BIB1_COM 6
194
195 #define CCL_BIB1_STR_WP (-1)
196 #define CCL_BIB1_STR_AND_LIST (-2)
197 #define CCL_BIB1_STR_OR_LIST (-3)
198 #define CCL_BIB1_REL_ORDER (-1)
199 #define CCL_BIB1_REL_PORDER (-2)
200
201 #define CCL_BIB1_TRU_CAN_LEFT (-1)
202 #define CCL_BIB1_TRU_CAN_RIGHT (-2)
203 #define CCL_BIB1_TRU_CAN_BOTH  (-3)
204 #define CCL_BIB1_TRU_CAN_NONE  (-4)
205
206 #define CCL_TOK_EOL   0
207 #define CCL_TOK_TERM  1
208 #define CCL_TOK_REL   2
209 #define CCL_TOK_EQ    3
210 #define CCL_TOK_PROX  4
211 #define CCL_TOK_LP    5
212 #define CCL_TOK_RP    6
213 #define CCL_TOK_COMMA 7
214 #define CCL_TOK_AND   8
215 #define CCL_TOK_OR    9
216 #define CCL_TOK_NOT   10
217 #define CCL_TOK_SET   11
218
219 /** CCL token */
220 struct ccl_token {
221     char kind;
222     size_t len;                 /* length of name below */
223     const char *name;           /* string / name of token */
224     struct ccl_token *next;
225     struct ccl_token *prev;
226     const char *ws_prefix_buf;  /* leading white space buf */
227     size_t ws_prefix_len;       /* leading white space len */
228 };
229
230 /** CCL Qualifier */
231 struct ccl_qualifier {
232     char *name;
233     int no_sub;
234     struct ccl_qualifier **sub;
235     struct ccl_rpn_attr *attr_list;
236     struct ccl_qualifier *next;
237 };
238
239 /** CCL parser structure */
240 struct ccl_parser {
241     /** current lookahead token */
242     struct ccl_token *look_token;
243     
244     /** holds error code if error occur */
245     int error_code;
246     /** if error occurs, this holds position (starting from 0). */
247     const char *error_pos;
248     
249     /** current bibset */
250     CCL_bibset bibset;
251
252     /** names of and operator */
253     char *ccl_token_and;
254     /** names of or operator */
255     char *ccl_token_or;
256     /** names of not operator */
257     char *ccl_token_not;
258     /** names of set operator */
259     char *ccl_token_set;
260     /** 1=CCL parser is case sensitive, 0=case insensitive */
261     int ccl_case_sensitive;
262 };
263     
264 typedef struct ccl_parser *CCL_parser;
265     
266 /**
267  * Splits CCL command string into individual tokens using
268  * a CCL parser.
269  */
270 YAZ_EXPORT
271 struct ccl_token *ccl_parser_tokenize (CCL_parser cclp, const char *command);
272
273 /**
274  * Splits CCL command string into tokens using temporary parser.
275  *
276  * Use ccl_parser_tokenize instead and provide a parser - it is
277  * more flexible and efficient.
278  */
279 YAZ_EXPORT
280 struct ccl_token *ccl_tokenize (const char *command);
281     
282 /**
283  * Splits CCL command into tokens. This function is obsolete. Use
284  * ccl_parser_tokenize instead.
285  */
286 YAZ_EXPORT
287 struct ccl_token *ccl_token_simple (const char *command);
288
289 /** 
290  * Deletes token list
291  */
292 YAZ_EXPORT
293 void ccl_token_del (struct ccl_token *list);
294
295 /**
296  * Add single token after a given onde.
297  */
298 YAZ_EXPORT
299 struct ccl_token *ccl_token_add (struct ccl_token *at);
300
301 /**
302  * Parses a CCL Find command in a simple C string. Returns CCL parse
303  * tree node describing RPN if parsing is successful. If parsing is
304  * unsuccesful, NULL is returned and error and pos is set accordingly.
305  */
306 YAZ_EXPORT
307 struct ccl_rpn_node *ccl_find_str (CCL_bibset bibset,
308                                    const char *str, int *error, int *pos);
309
310 /**
311  * Parses CCL Find command from a list of CCL tokens. Otherwise similar to
312  * ccl_find_str.
313  */
314 YAZ_EXPORT
315 struct ccl_rpn_node *ccl_find (CCL_bibset abibset, struct ccl_token *list,
316                                int *error, const char **pos);
317
318 /**
319  * Parses a CCL Find command from a list of CCL tokens and given a CCL
320  * parser. Otherwise similar to ccl_find_str.
321  */
322 YAZ_EXPORT
323 struct ccl_rpn_node *ccl_parser_find (CCL_parser cclp, struct ccl_token *list);
324
325 /** Set names for AND operator in parser */
326 YAZ_EXPORT
327 void ccl_parser_set_op_and (CCL_parser p, const char *op);
328
329 /** Set names for OR operator in parser */
330 YAZ_EXPORT
331 void ccl_parser_set_op_or (CCL_parser p, const char *op);
332
333 /** Set names for ANDNOT operator in parser */
334 YAZ_EXPORT
335 void ccl_parser_set_op_not (CCL_parser p, const char *op);
336
337 /** Set names for ResultSet in parser */
338 YAZ_EXPORT
339 void ccl_parser_set_op_set (CCL_parser p, const char *op);
340
341 /** Set case sensitivity for parser */
342 YAZ_EXPORT
343 void ccl_parser_set_case (CCL_parser p, int case_sensitivity_flag);
344
345 /** Return english-readable error message for CCL parser error number */
346 YAZ_EXPORT
347 const char *ccl_err_msg (int ccl_errno);
348
349 /** Delete RPN tree returned by ccl_find */
350 YAZ_EXPORT
351 void ccl_rpn_delete (struct ccl_rpn_node *rpn);
352
353 /** Dump RPN tree in readable format to fd_out */
354 YAZ_EXPORT
355 void ccl_pr_tree (struct ccl_rpn_node *rpn, FILE *fd_out);
356
357 /** Add qualifier and supply attribute pairs for it */
358 YAZ_EXPORT
359 void ccl_qual_add (CCL_bibset b, const char *name, int no, int *attr);
360
361 /** Add qualifier and supply attributes pairs+attribute set for it */
362 YAZ_EXPORT
363 void ccl_qual_add_set (CCL_bibset b, const char *name, int no,
364                        int *type, int *value, char **svalue, char **attsets);
365
366 /** Add special qualifier */
367 YAZ_EXPORT
368 void ccl_qual_add_special (CCL_bibset bibset, const char *n, const char *v);
369
370 /** Add combo qualifier */
371 YAZ_EXPORT
372 void ccl_qual_add_combi (CCL_bibset b, const char *n, const char *names);
373
374 /** Read CCL qualifier list spec from file inf */
375 YAZ_EXPORT
376 void ccl_qual_file (CCL_bibset bibset, FILE *inf);
377
378 /** Read CCL qualifier list spec from file inf */
379 YAZ_EXPORT
380 int ccl_qual_fname (CCL_bibset bibset, const char *fname);
381
382 /** Add CCL qualifier as buf spec (multiple lines). */
383 YAZ_EXPORT
384 void ccl_qual_buf(CCL_bibset bibset, const char *buf);
385
386 /** Add CCL qualifier as line spec. Note: line is _modified_ */
387 YAZ_EXPORT
388 void ccl_qual_line(CCL_bibset bibset, char *line);
389
390 /* Add CCL qualifier by using qual_name + value pair */
391 YAZ_EXPORT
392 void ccl_qual_fitem (CCL_bibset bibset, const char *value,
393                      const char *qual_name);
394
395 /** Make CCL qualifier set */
396 YAZ_EXPORT
397 CCL_bibset ccl_qual_mk (void);
398
399 /** Delete CCL qualifier set */
400 YAZ_EXPORT
401 void ccl_qual_rm (CCL_bibset *b);
402
403 /** Char-to-upper function */
404 extern int (*ccl_toupper)(int c);
405
406 /** CCL version of ccl_stricmp */
407 YAZ_EXPORT
408 int ccl_stricmp (const char *s1, const char *s2);
409
410 /** CCL version of ccl_memicmp */
411 YAZ_EXPORT
412 int ccl_memicmp (const char *s1, const char *s2, size_t n);
413
414 /** Search for qualifier 'name' in set 'b'. */
415 YAZ_EXPORT
416 struct ccl_rpn_attr *ccl_qual_search (CCL_parser cclp, const char *name,
417                                       size_t len, int seq);
418
419 /** Create CCL parser */
420 YAZ_EXPORT
421 CCL_parser ccl_parser_create (void);
422
423 /** Destroy CCL parser */
424 YAZ_EXPORT
425 void ccl_parser_destroy (CCL_parser p);
426
427 /** String dup utility (ala strdup) */
428 YAZ_EXPORT
429 char *ccl_strdup (const char *str);
430
431 /** Search for special qualifier */
432 YAZ_EXPORT
433 const char *ccl_qual_search_special (CCL_bibset b,
434                                                 const char *name);
435 /** Pretty-print CCL RPN node tree to WRBUF */
436 YAZ_EXPORT
437 void ccl_pquery (WRBUF w, struct ccl_rpn_node *p);
438
439 #ifndef ccl_assert
440 #define ccl_assert(x) ;
441 #endif
442
443 YAZ_END_CDECL
444
445 #endif
446