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