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