Added cs_get_SSL. yaz-client-ssl prints peer info
[yaz-moved-to-github.git] / src / cqlutil.c
1 /* $Id: cqlutil.c,v 1.4 2004-03-16 13:22:16 adam Exp $
2    Copyright (C) 2002-2004
3    Index Data Aps
4
5 This file is part of the YAZ toolkit.
6
7 See the file LICENSE.
8 */
9
10 #include <stdlib.h>
11 #include <string.h>
12
13 #include <yaz/xmalloc.h>
14 #include <yaz/cql.h>
15
16 void cql_fputs(const char *buf, void *client_data)
17 {
18     FILE *f = (FILE *) client_data;
19     fputs(buf, f);
20 }
21
22 struct cql_node *cql_node_dup (struct cql_node *cp)
23 {
24     struct cql_node *cn = 0;
25
26     if (!cp)
27         return 0;
28     switch (cp->which)
29     {
30     case CQL_NODE_ST:
31         cn = cql_node_mk_sc(cp->u.st.index,
32                             cp->u.st.relation,
33                             cp->u.st.term);
34         cn->u.st.modifiers = cql_node_dup(cp->u.st.modifiers);
35         cn->u.st.index_uri = cp->u.st.index_uri ? 
36             xstrdup(cp->u.st.index_uri) : 0;
37         cn->u.st.relation_uri = cp->u.st.relation_uri ?
38             xstrdup(cp->u.st.relation_uri) : 0;
39         break;
40     case CQL_NODE_BOOL:
41         cn = cql_node_mk_boolean(cp->u.boolean.value);
42         cn->u.boolean.left = cql_node_dup(cp->u.boolean.left);
43         cn->u.boolean.right = cql_node_dup(cp->u.boolean.right);
44     }
45     return cn;
46 }
47
48 struct cql_node *cql_node_mk_sc(const char *index,
49                                 const char *relation,
50                                 const char *term)
51 {
52     struct cql_node *p = (struct cql_node *) xmalloc(sizeof(*p));
53     p->which = CQL_NODE_ST;
54     p->u.st.index = 0;
55     if (index)
56         p->u.st.index = xstrdup(index);
57     p->u.st.index_uri = 0;
58     p->u.st.term = 0;
59     if (term)
60         p->u.st.term = xstrdup(term);
61     p->u.st.relation = 0;
62     if (relation)
63         p->u.st.relation = xstrdup(relation);
64     p->u.st.relation_uri = 0;
65     p->u.st.modifiers = 0;
66     return p;
67 }
68
69 struct cql_node *cql_node_mk_boolean(const char *op)
70 {
71     struct cql_node *p = (struct cql_node *) xmalloc(sizeof(*p));
72     p->which = CQL_NODE_BOOL;
73     p->u.boolean.value = 0;
74     if (op)
75         p->u.boolean.value = xstrdup(op);
76     p->u.boolean.left = 0;
77     p->u.boolean.right = 0;
78     p->u.boolean.modifiers = 0;
79     return p;
80 }
81
82 const char *cql_uri()
83 {
84     return "info:srw/cql-context-set/1/cql-v1.1";
85 }
86
87 struct cql_node *cql_apply_prefix(struct cql_node *n, const char *prefix,
88                                   const char *uri)
89 {
90     if (n->which == CQL_NODE_ST)
91     {
92         if (!n->u.st.index_uri && n->u.st.index)
93         {   /* not yet resolved.. */
94             const char *cp = strchr(n->u.st.index, '.');
95             if (prefix && cp && 
96                 strlen(prefix) == (size_t) (cp - n->u.st.index) &&
97                 !memcmp(n->u.st.index, prefix, strlen(prefix)))
98             {
99                 char *nval = xstrdup(cp+1);
100                 n->u.st.index_uri = xstrdup(uri);
101                 xfree (n->u.st.index);
102                 n->u.st.index = nval;
103             }
104             else if (!prefix && !cp)
105             {
106                 n->u.st.index_uri = xstrdup(uri);
107             }
108         }
109         if (!n->u.st.relation_uri && n->u.st.relation)
110         {
111             const char *cp = strchr(n->u.st.relation, '.');
112             if (prefix && cp &&
113                 strlen(prefix) == (size_t)(cp - n->u.st.relation) &&
114                 !memcmp(n->u.st.relation, prefix, strlen(prefix)))
115             {
116                 char *nval = xstrdup(cp+1);
117                 n->u.st.relation_uri = xstrdup(uri);
118                 xfree (n->u.st.relation);
119                 n->u.st.relation = nval;
120             }
121         }
122     }
123     else if (n->which == CQL_NODE_BOOL)
124     {
125         cql_apply_prefix(n->u.boolean.left, prefix, uri);
126         cql_apply_prefix(n->u.boolean.right, prefix, uri);
127     }
128     return n;
129 }
130
131 void cql_node_destroy(struct cql_node *cn)
132 {
133     if (!cn)
134         return;
135     switch (cn->which)
136     {
137     case CQL_NODE_ST:
138         xfree (cn->u.st.index);
139         xfree (cn->u.st.relation);
140         xfree (cn->u.st.term);
141         xfree (cn->u.st.index_uri);
142         xfree (cn->u.st.relation_uri);
143         cql_node_destroy(cn->u.st.modifiers);
144         break;
145     case CQL_NODE_BOOL:
146         xfree (cn->u.boolean.value);
147         cql_node_destroy(cn->u.boolean.left);
148         cql_node_destroy(cn->u.boolean.right);
149         cql_node_destroy(cn->u.boolean.modifiers);
150     }
151     xfree (cn);
152 }