Fixed for compiled ASN.1.
[yaz-moved-to-github.git] / zutil / query.c
1 /*
2  * Copyright (c) 1995-1999, Index Data
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: query.c,v $
7  * Revision 1.2  1999-06-09 14:01:33  adam
8  * Fixed for compiled ASN.1.
9  *
10  * Revision 1.1  1999/06/08 10:10:16  adam
11  * New sub directory zutil. Moved YAZ Compiler to be part of YAZ tree.
12  *
13  * Revision 1.3  1996/01/02 11:46:56  quinn
14  * Changed 'operator' to 'roperator' to avoid C++ conflict.
15  *
16  * Revision 1.2  1995/05/16  08:51:14  quinn
17  * License, documentation, and memory fixes
18  *
19  * Revision 1.1  1995/04/10  10:28:47  quinn
20  * Added copy of CCL and MARC display
21  *
22  *
23  */
24
25 #include <stdio.h>
26 #include <ctype.h>
27 #include <string.h>
28
29 #include <odr.h>
30 #include <proto.h>
31
32 static Z_Complex *makecomplex(ODR o, char **buf);
33 static Z_Operand *makesimple(ODR o, char **buf);
34 Z_RPNStructure *makerpn(ODR o, char **buf);
35
36 void skip_spaces(char**p)
37 {
38     while (**p && isspace(**p))
39         (*p)++;
40 }
41
42 static Z_Operand *makesimple(ODR o, char **buf)
43 {
44     Z_Operand *r;
45     Z_AttributesPlusTerm *t;
46     char *b;
47
48     r = odr_malloc(o, sizeof(*r));
49     if (**buf == 's' && *((*buf) + 1) == '=')
50     {
51         char *b = odr_malloc(o, 100);
52
53         r->which = Z_Operand_resultSetId;
54         r->u.resultSetId = b;
55         (*buf)++;
56         (*buf)++;
57         while (**buf && !isspace(**buf))
58             *(b++) = *((*buf)++);
59         *b = 0;
60         return r;
61     }
62     else if (**buf != '"')
63         return 0;
64     (*buf)++;
65     r->which = Z_Operand_APT;
66     r->u.attributesPlusTerm = t = odr_malloc(o, sizeof(*t));
67 #ifdef ASN_COMPILED
68     t->attributes = odr_malloc(o, sizeof(*t));
69     t->attributes->num_attributes = 0;
70     t->attributes->attributes = 0;
71 #else
72     t->num_attributes = 0;
73     t->attributeList = 0;
74 #endif
75     t->term = odr_malloc(o, sizeof(*t->term));
76     t->term->which = Z_Term_general;
77     t->term->u.general = odr_malloc(o, sizeof(Odr_oct));
78     t->term->u.general->buf = odr_malloc(o, 100);
79     t->term->u.general->size = 100;
80     t->term->u.general->len = 0;
81     b = (char*) t->term->u.general->buf;
82     while (**buf && **buf != '"')
83     {
84         *(b++) = *((*buf)++);
85         t->term->u.general->len++;
86     }
87     if (**buf != '"')
88         return 0;
89     (*buf)++;
90     return r;
91 }
92
93 static Z_Complex *makecomplex(ODR o, char **buf)
94 {
95     Z_Complex *r;
96     char op[100], *b;
97
98     r = odr_malloc(o, sizeof(*r));
99     r->roperator = odr_malloc(o, sizeof(*r->roperator));
100
101     b = op;
102     while (**buf && !isspace(**buf))
103         *(b++) = *((*buf)++);
104     *b = 0;
105     if (!strcmp(op, "and"))
106         r->roperator->which = Z_Operator_and;
107     else if (!strcmp(op, "or"))
108         r->roperator->which = Z_Operator_or;
109     else if (!strcmp(op, "not"))
110         r->roperator->which = Z_Operator_and_not;
111     r->roperator->u.and = "";
112     while (**buf && !isspace(**buf))
113         (*buf)++;
114     if (!(r->s1 = makerpn(o, buf)))
115         return 0;
116     if (!(r->s2 = makerpn(o, buf)))
117         return 0;
118     return r;
119 }
120     
121 Z_RPNStructure *makerpn(ODR o, char **buf)
122 {
123     Z_RPNStructure *r;
124
125     r = odr_malloc(o, sizeof(*r));
126     skip_spaces(buf);
127     if (**buf == '"' || **buf == 's')
128     {
129         r->which = Z_RPNStructure_simple;
130         if (!(r->u.simple = makesimple(o, buf)))
131             return 0;
132         return r;
133     }
134     r->which = Z_RPNStructure_complex;
135     if (!(r->u.complex = makecomplex(o, buf)))
136         return 0;
137     return r;
138 }