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