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