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