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