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