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