Fixed handling of default/inherited attributes.
[yaz-moved-to-github.git] / zutil / pquery.c
1 /*
2  * Copyright (c) 1995-1998, Index Data.
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: pquery.c,v $
7  * Revision 1.4  1999-12-21 16:25:20  adam
8  * Fixed handling of default/inherited attributes.
9  *
10  * Revision 1.3  1999/12/20 15:20:13  adam
11  * Implemented ccl_pquery to convert from CCL tree to prefix query.
12  *
13  * Revision 1.2  1999/11/30 13:47:12  adam
14  * Improved installation. Moved header files to include/yaz.
15  *
16  * Revision 1.1  1999/06/08 10:10:16  adam
17  * New sub directory zutil. Moved YAZ Compiler to be part of YAZ tree.
18  *
19  * Revision 1.22  1999/04/20 09:56:49  adam
20  * Added 'name' paramter to encoder/decoder routines (typedef Odr_fun).
21  * Modified all encoders/decoders to reflect this change.
22  *
23  * Revision 1.21  1998/10/13 16:03:37  adam
24  * Better checking for invalid OID's in p_query_rpn.
25  *
26  * Revision 1.20  1998/03/31 15:13:20  adam
27  * Development towards compiled ASN.1.
28  *
29  * Revision 1.19  1998/03/05 08:09:03  adam
30  * Minor change to make C++ happy.
31  *
32  * Revision 1.18  1998/02/11 11:53:36  adam
33  * Changed code so that it compiles as C++.
34  *
35  * Revision 1.17  1997/11/24 11:33:57  adam
36  * Using function odr_nullval() instead of global ODR_NULLVAL when
37  * appropriate.
38  *
39  * Revision 1.16  1997/09/29 13:19:00  adam
40  * Added function, oid_ent_to_oid, to replace the function
41  * oid_getoidbyent, which is not thread safe.
42  *
43  * Revision 1.15  1997/09/29 07:13:43  adam
44  * Changed type of a few variables to avoid warnings.
45  *
46  * Revision 1.14  1997/09/22 12:33:41  adam
47  * Fixed bug introduced by previous commit.
48  *
49  * Revision 1.13  1997/09/17 12:10:42  adam
50  * YAZ version 1.4.
51  *
52  * Revision 1.12  1997/09/01 08:54:13  adam
53  * New windows NT/95 port using MSV5.0. Made prefix query handling
54  * thread safe. The function options ignores empty arguments when met.
55  *
56  * Revision 1.11  1996/11/11 13:15:29  adam
57  * Added proximity operator.
58  *
59  * Revision 1.10  1996/08/12 14:10:35  adam
60  * New function p_query_attset to define default attribute set.
61  *
62  * Revision 1.9  1996/03/15  11:03:46  adam
63  * Attribute set can be set globally for a query with the @attrset
64  * operator. The @attr operator has an optional attribute-set specifier
65  * that sets the attribute set locally.
66  *
67  * Revision 1.8  1996/01/02  11:46:56  quinn
68  * Changed 'operator' to 'roperator' to avoid C++ conflict.
69  *
70  * Revision 1.7  1995/09/29  17:12:36  quinn
71  * Smallish
72  *
73  * Revision 1.6  1995/09/27  15:03:03  quinn
74  * Modified function heads & prototypes.
75  *
76  * Revision 1.5  1995/06/15  12:31:02  quinn
77  * *** empty log message ***
78  *
79  * Revision 1.4  1995/06/15  07:45:19  quinn
80  * Moving to v3.
81  *
82  * Revision 1.3  1995/06/14  11:06:35  adam
83  * Bug fix: Attributes wasn't interpreted correctly!
84  *
85  * Revision 1.2  1995/05/26  08:56:11  adam
86  * New function: p_query_scan.
87  *
88  * Revision 1.1  1995/05/22  15:31:49  adam
89  * New function, p_query_rpn, to convert from prefix (ascii) to rpn (asn).
90  *
91  */
92
93 #include <stdio.h>
94 #include <string.h>
95 #include <stdlib.h>
96
97 #include <yaz/proto.h>
98 #include <yaz/oid.h>
99 #include <yaz/pquery.h>
100
101 static oid_value p_query_dfset = VAL_NONE;
102
103 struct lex_info {
104     const char *query_buf;
105     const char *lex_buf;
106     size_t lex_len;
107     int query_look;
108     char *left_sep;
109     char *right_sep;
110     int escape_char;
111     int term_type;
112 };
113
114 static Z_RPNStructure *rpn_structure (struct lex_info *li, ODR o, oid_proto, 
115                                       int num_attr, int max_attr, 
116                                       int *attr_list, oid_value *attr_set);
117
118 static enum oid_value query_oid_getvalbyname (struct lex_info *li)
119 {
120     enum oid_value value;
121     char buf[32];
122
123     if (li->lex_len > 31)
124         return VAL_NONE;
125     memcpy (buf, li->lex_buf, li->lex_len);
126     buf[li->lex_len] = '\0';
127     value = oid_getvalbyname (buf);
128     return value;
129 }
130
131 static int compare_term (struct lex_info *li, const char *src, size_t off)
132 {
133     size_t len=strlen(src);
134
135     if (li->lex_len == len+off && !memcmp (li->lex_buf+off, src, len-off))
136         return 1;
137     return 0;
138 }
139
140 static int query_token (struct lex_info *li)
141 {
142     const char *sep_match;
143     const char **qptr = &li->query_buf;
144
145     while (**qptr == ' ')
146         (*qptr)++;
147     if (**qptr == '\0')
148         return 0;
149     li->lex_len = 0;
150     if ((sep_match = strchr (li->left_sep, **qptr)))
151     {
152         int sep_index = sep_match - li->left_sep;
153         
154         ++(*qptr);
155         li->lex_buf = *qptr;
156         while (**qptr && **qptr != li->right_sep[sep_index])
157         {
158             ++(li->lex_len);
159             ++(*qptr);
160         }
161         if (**qptr)
162             ++(*qptr);
163     }
164     else
165     {
166         li->lex_buf = *qptr;
167         while (**qptr && **qptr != ' ')
168         {
169             ++(li->lex_len);
170             ++(*qptr);
171         }
172     }
173     if (li->lex_len >= 1 && li->lex_buf[0] == li->escape_char)
174     {
175         if (compare_term (li, "and", 1))
176             return 'a';
177         if (compare_term (li, "or", 1))
178             return 'o';
179         if (compare_term (li, "not", 1))
180             return 'n';
181         if (compare_term (li, "attr", 1))
182             return 'l';
183         if (compare_term (li, "set", 1))
184             return 's';
185         if (compare_term (li, "attrset", 1))
186             return 'r';
187         if (compare_term (li, "prox", 1))
188             return 'p';
189         if (compare_term (li, "term", 1))
190             return 'y';
191     }
192     return 't';
193 }
194
195 static int lex (struct lex_info *li)
196 {
197     return li->query_look = query_token (li);
198 }
199
200 static Z_AttributesPlusTerm *rpn_term (struct lex_info *li, ODR o,
201                                        oid_proto proto, 
202                                        int num_attr, int *attr_list,
203                                        oid_value *attr_set)
204 {
205     Z_AttributesPlusTerm *zapt;
206     Odr_oct *term_octet;
207     Z_Term *term;
208     Z_AttributeElement **elements;
209
210     zapt = (Z_AttributesPlusTerm *)odr_malloc (o, sizeof(*zapt));
211     term_octet = (Odr_oct *)odr_malloc (o, sizeof(*term_octet));
212     term = (Z_Term *)odr_malloc (o, sizeof(*term));
213
214     if (!num_attr)
215         elements = (Z_AttributeElement**)odr_nullval();
216     else
217     {
218         int i, k = 0;
219         int *attr_tmp;
220
221         elements = (Z_AttributeElement**)
222             odr_malloc (o, num_attr * sizeof(*elements));
223
224         attr_tmp = (int *)odr_malloc (o, num_attr * 2 * sizeof(int));
225         memcpy (attr_tmp, attr_list, num_attr * 2 * sizeof(int));
226         for (i = num_attr; --i >= 0; )
227         {
228             int j;
229             for (j = i+1; j<num_attr; j++)
230                 if (attr_tmp[2*j] == attr_tmp[2*i])
231                     break;
232             if (j < num_attr)
233                 continue;
234             elements[k] =
235                 (Z_AttributeElement*)odr_malloc (o,sizeof(**elements));
236             elements[k]->attributeType = &attr_tmp[2*i];
237             if (attr_set[i] == VAL_NONE)
238                 elements[k]->attributeSet = 0;
239             else
240             {
241                 oident attrid;
242                 int oid[OID_SIZE];
243                 
244                 attrid.proto = PROTO_Z3950;
245                 attrid.oclass = CLASS_ATTSET;
246                 attrid.value = attr_set[i];
247                    
248                 elements[k]->attributeSet =
249                     odr_oiddup (o, oid_ent_to_oid (&attrid, oid));
250             }
251             elements[k]->which = Z_AttributeValue_numeric;
252             elements[k]->value.numeric = &attr_tmp[2*i+1];
253             k++;
254         }
255         num_attr = k;
256     }
257 #ifdef ASN_COMPILED
258     zapt->attributes = (Z_AttributeList *)
259         odr_malloc (o, sizeof(*zapt->attributes));
260     zapt->attributes->num_attributes = num_attr;
261     zapt->attributes->attributes = elements;
262 #else
263     zapt->num_attributes = num_attr;
264     zapt->attributeList = elements;
265 #endif    
266
267     zapt->term = term;
268     term->which = Z_Term_general;
269     term->u.general = term_octet;
270     term_octet->buf = (unsigned char *)odr_malloc (o, li->lex_len);
271     term_octet->size = term_octet->len = li->lex_len;
272     memcpy (term_octet->buf, li->lex_buf, li->lex_len);
273     return zapt;
274 }
275
276 static Z_Operand *rpn_simple (struct lex_info *li, ODR o, oid_proto proto,
277                               int num_attr, int *attr_list,
278                               oid_value *attr_set)
279 {
280     Z_Operand *zo;
281
282     zo = (Z_Operand *)odr_malloc (o, sizeof(*zo));
283     switch (li->query_look)
284     {
285     case 't':
286         zo->which = Z_Operand_APT;
287         if (!(zo->u.attributesPlusTerm =
288               rpn_term (li, o, proto, num_attr, attr_list, attr_set)))
289             return NULL;
290         lex (li);
291         break;
292     case 's':
293         lex (li);
294         if (!li->query_look)
295             return NULL;
296         zo->which = Z_Operand_resultSetId;
297         zo->u.resultSetId = (char *)odr_malloc (o, li->lex_len+1);
298         memcpy (zo->u.resultSetId, li->lex_buf, li->lex_len);
299         zo->u.resultSetId[li->lex_len] = '\0';
300         lex (li);
301         break;
302     default:
303         return NULL;
304     }
305     return zo;
306 }
307
308 static Z_ProximityOperator *rpn_proximity (struct lex_info *li, ODR o)
309 {
310     Z_ProximityOperator *p = (Z_ProximityOperator *)odr_malloc (o, sizeof(*p));
311
312     if (!lex (li))
313         return NULL;
314     if (*li->lex_buf == '1')
315     {
316         p->exclusion = (int *)odr_malloc (o, sizeof(*p->exclusion));
317         *p->exclusion = 1;
318     } 
319     else if (*li->lex_buf == '0')
320     {
321         p->exclusion = (int *)odr_malloc (o, sizeof(*p->exclusion));
322         *p->exclusion = 0;
323     }
324     else
325         p->exclusion = NULL;
326
327     if (!lex (li))
328         return NULL;
329     p->distance = (int *)odr_malloc (o, sizeof(*p->distance));
330     *p->distance = atoi (li->lex_buf);
331
332     if (!lex (li))
333         return NULL;
334     p->ordered = (int *)odr_malloc (o, sizeof(*p->ordered));
335     *p->ordered = atoi (li->lex_buf);
336     
337     if (!lex (li))
338         return NULL;
339     p->relationType = (int *)odr_malloc (o, sizeof(*p->relationType));
340     *p->relationType = atoi (li->lex_buf);
341
342     if (!lex (li))
343         return NULL;
344     if (*li->lex_buf == 'k')
345         p->which = 0;
346     else if (*li->lex_buf == 'p')
347         p->which = 1;
348     else
349         p->which = atoi (li->lex_buf);
350
351     if (!lex (li))
352         return NULL;
353 #ifdef ASN_COMPILED
354     p->which = Z_ProximityOperator_known;
355     p->u.known = (int *)odr_malloc (o, sizeof(*p->u.known));
356     *p->u.known = atoi (li->lex_buf);
357 #else
358     p->proximityUnitCode = (int *)odr_malloc (o, sizeof(*p->proximityUnitCode));
359     *p->proximityUnitCode = atoi (li->lex_buf);
360 #endif
361     return p;
362 }
363
364 static Z_Complex *rpn_complex (struct lex_info *li, ODR o, oid_proto proto,
365                                int num_attr, int max_attr, 
366                                int *attr_list, oid_value *attr_set)
367 {
368     Z_Complex *zc;
369     Z_Operator *zo;
370
371     zc = (Z_Complex *)odr_malloc (o, sizeof(*zc));
372     zo = (Z_Operator *)odr_malloc (o, sizeof(*zo));
373     zc->roperator = zo;
374     switch (li->query_look)
375     {
376     case 'a':
377         zo->which = Z_Operator_and;
378         zo->u.and = odr_nullval();
379         break;
380     case 'o':
381         zo->which = Z_Operator_or;
382         zo->u.and = odr_nullval();
383         break;
384     case 'n':
385         zo->which = Z_Operator_and_not;
386         zo->u.and = odr_nullval();
387         break;
388     case 'p':
389         zo->which = Z_Operator_prox;
390         zo->u.prox = rpn_proximity (li, o);
391         if (!zo->u.prox)
392             return NULL;
393         break;
394     default:
395         return NULL;
396     }
397     lex (li);
398     if (!(zc->s1 =
399           rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
400                          attr_set)))
401         return NULL;
402     if (!(zc->s2 =
403           rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
404                          attr_set)))
405         return NULL;
406     return zc;
407 }
408
409 static Z_RPNStructure *rpn_structure (struct lex_info *li, ODR o,
410                                       oid_proto proto, 
411                                       int num_attr, int max_attr, 
412                                       int *attr_list, oid_value *attr_set)
413 {
414     Z_RPNStructure *sz;
415     const char *cp;
416
417     sz = (Z_RPNStructure *)odr_malloc (o, sizeof(*sz));
418     switch (li->query_look)
419     {
420     case 'a':
421     case 'o':
422     case 'n':
423     case 'p':
424         sz->which = Z_RPNStructure_complex;
425         if (!(sz->u.complex =
426               rpn_complex (li, o, proto, num_attr, max_attr, attr_list,
427                            attr_set)))
428             return NULL;
429         break;
430     case 't':
431     case 's':
432         sz->which = Z_RPNStructure_simple;
433         if (!(sz->u.simple =
434               rpn_simple (li, o, proto, num_attr, attr_list,
435                           attr_set)))
436             return NULL;
437         break;
438     case 'l':
439         lex (li);
440         if (!li->query_look)
441             return NULL;
442         if (num_attr >= max_attr)
443             return NULL;
444         if (!(cp = strchr (li->lex_buf, '=')) ||
445             (size_t) (cp-li->lex_buf) > li->lex_len)
446         {
447             attr_set[num_attr] = query_oid_getvalbyname (li);
448             if (attr_set[num_attr] == VAL_NONE)
449                 return NULL;
450             lex (li);
451
452             if (!(cp = strchr (li->lex_buf, '=')))
453                 return NULL;
454         }
455         else 
456         {
457             if (num_attr > 0)
458                 attr_set[num_attr] = attr_set[num_attr-1];
459             else
460                 attr_set[num_attr] = VAL_NONE;
461         }
462         attr_list[2*num_attr] = atoi(li->lex_buf); 
463         attr_list[2*num_attr+1] = atoi (cp+1);
464         num_attr++;
465         lex (li);
466         return
467             rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
468                            attr_set);
469     case 'y':
470         lex (li);
471         if (!li->query_look)
472             return NULL;
473         if (compare_term (li, "general", 0))
474             li->term_type = Z_Term_general;
475         else if (compare_term (li, "numeric", 0))
476             li->term_type = Z_Term_numeric;
477         else if (compare_term (li, "string", 0))
478             li->term_type = Z_Term_characterString;
479         else if (compare_term (li, "oid", 0))
480             li->term_type = Z_Term_oid;
481         else if (compare_term (li, "datetime", 0))
482             li->term_type = Z_Term_dateTime;
483         else if (compare_term (li, "null", 0))
484             li->term_type = Z_Term_null;
485         lex (li);
486         return
487             rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
488                            attr_set);
489     case 0:                /* operator/operand expected! */
490         return NULL;
491     }
492     return sz;
493 }
494
495 Z_RPNQuery *p_query_rpn_mk (ODR o, struct lex_info *li, oid_proto proto,
496                             const char *qbuf)
497 {
498     Z_RPNQuery *zq;
499     int attr_array[1024];
500     oid_value attr_set[512];
501     oid_value topSet = VAL_NONE;
502     oident oset;
503     int oid[OID_SIZE];
504
505     zq = (Z_RPNQuery *)odr_malloc (o, sizeof(*zq));
506     lex (li);
507     if (li->query_look == 'r')
508     {
509         lex (li);
510         topSet = query_oid_getvalbyname (li);
511         if (topSet == VAL_NONE)
512             return NULL;
513
514         lex (li);
515     }
516     if (topSet == VAL_NONE)
517         topSet = p_query_dfset;
518     if (topSet == VAL_NONE)
519         topSet = VAL_BIB1;
520     oset.proto = proto;
521     oset.oclass = CLASS_ATTSET;
522     oset.value = topSet;
523
524     if (!oid_ent_to_oid (&oset, oid))
525         return NULL;
526     zq->attributeSetId = odr_oiddup (o, oid);
527
528     if (!(zq->RPNStructure = rpn_structure (li, o, proto, 0, 512,
529                                             attr_array, attr_set)))
530         return NULL;
531     return zq;
532 }
533
534 Z_RPNQuery *p_query_rpn (ODR o, oid_proto proto,
535                          const char *qbuf)
536 {
537     struct lex_info li;
538     
539     li.left_sep = "{\"";
540     li.right_sep = "}\"";
541     li.escape_char = '@';
542     li.term_type = Z_Term_general;
543     li.query_buf = qbuf;
544     return p_query_rpn_mk (o, &li, proto, qbuf);
545 }
546
547 Z_AttributesPlusTerm *p_query_scan_mk (struct lex_info *li,
548                                        ODR o, oid_proto proto,
549                                        Odr_oid **attributeSetP,
550                                        const char *qbuf)
551 {
552     int attr_list[1024];
553     oid_value attr_set[512];
554     int num_attr = 0;
555     int max_attr = 512;
556     const char *cp;
557     oid_value topSet = VAL_NONE;
558     oident oset;
559     int oid[OID_SIZE];
560
561     lex (li);
562     if (li->query_look == 'r')
563     {
564         lex (li);
565         topSet = query_oid_getvalbyname (li);
566
567         lex (li);
568     }
569     if (topSet == VAL_NONE)
570         topSet = p_query_dfset;
571     if (topSet == VAL_NONE)
572         topSet = VAL_BIB1;
573     oset.proto = proto;
574     oset.oclass = CLASS_ATTSET;
575     oset.value = topSet;
576
577     *attributeSetP = odr_oiddup (o, oid_ent_to_oid (&oset, oid));
578
579     while (li->query_look == 'l')
580     {
581         lex (li);
582         if (!li->query_look)
583             return NULL;
584         if (num_attr >= max_attr)
585             return NULL;
586
587         if (!(cp = strchr (li->lex_buf, '=')) ||
588             (size_t) (cp-li->lex_buf) > li->lex_len)
589         {
590             attr_set[num_attr] = query_oid_getvalbyname (li);
591             lex (li);
592
593             if (!(cp = strchr (li->lex_buf, '=')))
594                 return NULL;
595         }
596         else
597         {
598             if (num_attr > 0)
599                 attr_set[num_attr] = attr_set[num_attr-1];
600             else
601                 attr_set[num_attr] = VAL_NONE;
602         }
603         attr_list[2*num_attr] = atoi (li->lex_buf);
604         attr_list[2*num_attr+1] = atoi (cp+1);
605         num_attr++;
606         lex (li);
607     }
608     if (!li->query_look)
609         return NULL;
610     return rpn_term (li, o, proto, num_attr, attr_list, attr_set);
611 }
612
613 Z_AttributesPlusTerm *p_query_scan (ODR o, oid_proto proto,
614                                     Odr_oid **attributeSetP,
615                                     const char *qbuf)
616 {
617     struct lex_info li;
618
619     li.left_sep = "{\"";
620     li.right_sep = "}\"";
621     li.escape_char = '@';
622     li.term_type = Z_Term_general;
623     li.query_buf = qbuf;
624
625     return p_query_scan_mk (&li, o, proto, attributeSetP, qbuf);
626 }
627
628 int p_query_attset (const char *arg)
629 {
630     p_query_dfset = oid_getvalbyname (arg);
631     return (p_query_dfset == VAL_NONE) ? -1 : 0;
632 }
633