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