New Z39.50 OID utilities: yaz_oidval_to_z3950oid, yaz_str_to_z3950oid
[yaz-moved-to-github.git] / zutil / yaz-ccl.c
1 /*
2  * Copyright (c) 1996-2001, Index Data.
3  * See the file LICENSE for details.
4  *
5  * $Log: yaz-ccl.c,v $
6  * Revision 1.14  2001-09-24 21:51:56  adam
7  * New Z39.50 OID utilities: yaz_oidval_to_z3950oid, yaz_str_to_z3950oid
8  * and yaz_z3950oid_to_str.
9  *
10  * Revision 1.13  2001/05/09 23:31:35  adam
11  * String attribute values for PQF. Proper C-backslash escaping for PQF.
12  *
13  * Revision 1.12  2001/03/07 13:24:40  adam
14  * Member and_not in Z_Operator is kept for backwards compatibility.
15  * Added support for definition of CCL operators in field spec file.
16  *
17  * Revision 1.11  2001/02/21 13:46:54  adam
18  * C++ fixes.
19  *
20  * Revision 1.10  2001/02/20 11:23:50  adam
21  * Updated ccl_pquery to consider local attribute set too.
22  *
23  * Revision 1.9  2000/11/27 14:16:55  adam
24  * Fixed bug in ccl_rpn_simple regarding resultSetId's.
25  *
26  * Revision 1.8  2000/11/16 13:03:13  adam
27  * Function ccl_rpn_query sets attributeSet to Bib-1.
28  *
29  * Revision 1.7  2000/11/16 09:58:02  adam
30  * Implemented local AttributeSet setting for CCL field maps.
31  *
32  * Revision 1.6  2000/02/02 15:13:23  adam
33  * Minor change.
34  *
35  * Revision 1.5  2000/01/31 13:15:22  adam
36  * Removed uses of assert(3). Cleanup of ODR. CCL parser update so
37  * that some characters are not surrounded by spaces in resulting term.
38  * ILL-code updates.
39  *
40  * Revision 1.4  1999/12/20 15:20:13  adam
41  * Implemented ccl_pquery to convert from CCL tree to prefix query.
42  *
43  * Revision 1.3  1999/11/30 13:47:12  adam
44  * Improved installation. Moved header files to include/yaz.
45  *
46  * Revision 1.2  1999/06/16 12:00:08  adam
47  * Added proximity.
48  *
49  * Revision 1.1  1999/06/08 10:12:43  adam
50  * Moved file to be part of zutil (instead of util).
51  *
52  * Revision 1.13  1998/03/31 15:13:20  adam
53  * Development towards compiled ASN.1.
54  *
55  * Revision 1.12  1998/02/11 11:53:36  adam
56  * Changed code so that it compiles as C++.
57  *
58  * Revision 1.11  1997/11/24 11:33:57  adam
59  * Using function odr_nullval() instead of global ODR_NULLVAL when
60  * appropriate.
61  *
62  * Revision 1.10  1997/09/29 08:58:25  adam
63  * Fixed conversion of trees so that true copy is made.
64  *
65  * Revision 1.9  1997/06/23 10:31:25  adam
66  * Added ODR argument to ccl_rpn_query and ccl_scan_query.
67  *
68  * Revision 1.8  1996/10/29 13:36:27  adam
69  * Added header.
70  *
71  */
72
73 #include <stdio.h>
74 #include <stdlib.h>
75 #include <string.h>
76
77 #include <yaz/yaz-ccl.h>
78
79 static Z_RPNStructure *ccl_rpn_structure (ODR o, struct ccl_rpn_node *p);
80
81 static Z_AttributesPlusTerm *ccl_rpn_term (ODR o, struct ccl_rpn_node *p)
82 {
83     struct ccl_rpn_attr *attr;
84     int num = 0;
85     Z_AttributesPlusTerm *zapt;
86     Odr_oct *term_octet;
87     Z_Term *term;
88     Z_AttributeElement **elements;
89
90     zapt = (Z_AttributesPlusTerm *)odr_malloc (o, sizeof(*zapt));
91
92     term_octet = (Odr_oct *)odr_malloc (o, sizeof(*term_octet));
93
94     term = (Z_Term *)odr_malloc (o, sizeof(*term));
95
96     for (attr = p->u.t.attr_list; attr; attr = attr->next)
97         num++;
98     if (!num)
99         elements = (Z_AttributeElement**)odr_nullval();
100     else
101     {
102         int i = 0;
103         elements = (Z_AttributeElement **)
104             odr_malloc (o, num*sizeof(*elements));
105         for (attr = p->u.t.attr_list; attr; attr = attr->next, i++)
106         {
107             elements[i] = (Z_AttributeElement *)
108                 odr_malloc (o, sizeof(**elements));
109             elements[i]->attributeType =
110                 (int *)odr_malloc(o, sizeof(int));
111             *elements[i]->attributeType = attr->type;
112             elements[i]->attributeSet = 0;
113             if (attr->set && *attr->set)
114             {
115                 int value = oid_getvalbyname (attr->set);
116
117                 if (value != VAL_NONE)
118                 {
119                     elements[i]->attributeSet =
120                         yaz_oidval_to_z3950oid(o, CLASS_ATTSET, value);
121                 }
122             }
123             elements[i]->which = Z_AttributeValue_numeric;
124             elements[i]->value.numeric =
125                 (int *)odr_malloc (o, sizeof(int));
126             *elements[i]->value.numeric = attr->value;
127         }
128     }
129 #ifdef ASN_COMPILED
130     zapt->attributes = (Z_AttributeList *)
131         odr_malloc (o, sizeof(*zapt->attributes));
132     zapt->attributes->num_attributes = num;
133     zapt->attributes->attributes = elements;
134 #else
135     zapt->num_attributes = num;
136     zapt->attributeList = elements;
137 #endif    
138     zapt->term = term;
139     term->which = Z_Term_general;
140     term->u.general = term_octet;
141     term_octet->len = term_octet->size = strlen (p->u.t.term);
142     term_octet->buf = (unsigned char *)odr_malloc (o, term_octet->len+1);
143     strcpy ((char*) term_octet->buf, p->u.t.term);
144     return zapt;
145 }
146
147 static Z_Operand *ccl_rpn_simple (ODR o, struct ccl_rpn_node *p)
148 {
149     Z_Operand *zo;
150
151     zo = (Z_Operand *)odr_malloc (o, sizeof(*zo));
152
153     switch (p->kind)
154     {
155     case CCL_RPN_TERM:
156         zo->which = Z_Operand_APT;
157         zo->u.attributesPlusTerm = ccl_rpn_term (o, p);
158         break;
159     case CCL_RPN_SET:
160         zo->which = Z_Operand_resultSetId;
161         zo->u.resultSetId = odr_strdup (o, p->u.setname);
162         break;
163     default:
164         return 0;
165     }
166     return zo;
167 }
168
169 static Z_Complex *ccl_rpn_complex (ODR o, struct ccl_rpn_node *p)
170 {
171     Z_Complex *zc;
172     Z_Operator *zo;
173
174     zc = (Z_Complex *)odr_malloc (o, sizeof(*zc));
175     zo = (Z_Operator *)odr_malloc (o, sizeof(*zo));
176
177     zc->roperator = zo;
178     switch (p->kind)
179     {
180     case CCL_RPN_AND:
181         zo->which = Z_Operator_and;
182         zo->u.and_not = odr_nullval();
183         break;
184     case CCL_RPN_OR:
185         zo->which = Z_Operator_or;
186         zo->u.and_not = odr_nullval();
187         break;
188     case CCL_RPN_NOT:
189         zo->which = Z_Operator_and_not;
190         zo->u.and_not = odr_nullval();
191         break;
192     case CCL_RPN_PROX:
193         zo->which = Z_Operator_prox;
194         zo->u.prox = (Z_ProximityOperator *)
195             odr_malloc (o, sizeof(*zo->u.prox));
196         zo->u.prox->exclusion = 0;
197
198         zo->u.prox->distance = (int *)
199             odr_malloc (o, sizeof(*zo->u.prox->distance));
200         *zo->u.prox->distance = 2;
201
202         zo->u.prox->ordered = (bool_t *)
203             odr_malloc (o, sizeof(*zo->u.prox->ordered));
204         *zo->u.prox->ordered = 0;
205
206         zo->u.prox->relationType = (int *)
207             odr_malloc (o, sizeof(*zo->u.prox->relationType));
208 #ifdef ASN_COMPILED
209         *zo->u.prox->relationType = Z_ProximityOperator_Prox_lessThan;
210         zo->u.prox->which = Z_ProximityOperator_known;
211         zo->u.prox->u.known = 
212             (Z_ProxUnit *) odr_malloc (o, sizeof(*zo->u.prox->u.known));
213         *zo->u.prox->u.known = Z_ProxUnit_word;
214 #else
215         *zo->u.prox->relationType = Z_Prox_lessThan;
216         zo->u.prox->which = Z_ProxCode_known;
217         zo->u.prox->proximityUnitCode = (int*)
218             odr_malloc (o, sizeof(*zo->u.prox->proximityUnitCode));
219         *zo->u.prox->proximityUnitCode = Z_ProxUnit_word;
220 #endif
221         break;
222     default:
223         return 0;
224     }
225     zc->s1 = ccl_rpn_structure (o, p->u.p[0]);
226     zc->s2 = ccl_rpn_structure (o, p->u.p[1]);
227     return zc;
228 }
229
230 static Z_RPNStructure *ccl_rpn_structure (ODR o, struct ccl_rpn_node *p)
231 {
232     Z_RPNStructure *zs;
233
234     zs = (Z_RPNStructure *)odr_malloc (o, sizeof(*zs));
235     switch (p->kind)
236     {
237     case CCL_RPN_AND:
238     case CCL_RPN_OR:
239     case CCL_RPN_NOT:
240     case CCL_RPN_PROX:
241         zs->which = Z_RPNStructure_complex;
242         zs->u.complex = ccl_rpn_complex (o, p);
243         break;
244     case CCL_RPN_TERM:
245     case CCL_RPN_SET:
246         zs->which = Z_RPNStructure_simple;
247         zs->u.simple = ccl_rpn_simple (o, p);
248         break;
249     default:
250         return 0;
251     }
252     return zs;
253 }
254
255 Z_RPNQuery *ccl_rpn_query (ODR o, struct ccl_rpn_node *p)
256 {
257     Z_RPNQuery *zq = (Z_RPNQuery *)odr_malloc (o, sizeof(*zq));
258     zq->attributeSetId = yaz_oidval_to_z3950oid (o, CLASS_ATTSET, VAL_BIB1);
259     zq->RPNStructure = ccl_rpn_structure (o, p);
260     return zq;
261 }
262
263 Z_AttributesPlusTerm *ccl_scan_query (ODR o, struct ccl_rpn_node *p)
264 {
265     if (p->kind != CCL_RPN_TERM)
266         return NULL;
267     return ccl_rpn_term (o, p);
268 }
269
270 static void ccl_pquery_complex (WRBUF w, struct ccl_rpn_node *p)
271 {
272     switch (p->kind)
273     {
274     case CCL_RPN_AND:
275         wrbuf_puts (w, "@and ");
276         break;
277     case CCL_RPN_OR:
278         wrbuf_puts(w, "@or ");
279         break;
280     case CCL_RPN_NOT:
281         wrbuf_puts(w, "@not ");
282         break;
283     case CCL_RPN_PROX:
284         wrbuf_puts(w, "@prox 0 2 0 1 known 2 ");
285         break;
286     default:
287         wrbuf_puts(w, "@ bad op (unknown) ");
288     };
289     ccl_pquery(w, p->u.p[0]);
290     ccl_pquery(w, p->u.p[1]);
291 }
292         
293 void ccl_pquery (WRBUF w, struct ccl_rpn_node *p)
294 {
295     struct ccl_rpn_attr *att;
296     const char *cp;
297
298     switch (p->kind)
299     {
300     case CCL_RPN_AND:
301     case CCL_RPN_OR:
302     case CCL_RPN_NOT:
303     case CCL_RPN_PROX:
304         ccl_pquery_complex (w, p);
305         break;
306     case CCL_RPN_SET:
307         wrbuf_puts (w, "@set ");
308         wrbuf_puts (w, p->u.setname);
309         wrbuf_puts (w, " ");
310         break;
311     case CCL_RPN_TERM:
312         for (att = p->u.t.attr_list; att; att = att->next)
313         {
314             char tmpattr[128];
315             wrbuf_puts (w, "@attr ");
316             if (att->set)
317             {
318                 wrbuf_puts (w, att->set);
319                 wrbuf_puts (w, " ");
320             }
321             sprintf(tmpattr, "%d=%d ", att->type, att->value);
322             wrbuf_puts (w, tmpattr);
323         }
324         for (cp = p->u.t.term; *cp; cp++)
325         {
326             if (*cp == ' ' || *cp == '\\')
327                 wrbuf_putc (w, '\\');
328             wrbuf_putc (w, *cp);
329         }
330         wrbuf_puts (w, " ");
331         break;
332     }
333 }