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