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