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