1
/* assuan-client.c - client functions
2
* Copyright (C) 2001, 2002 Free Software Foundation, Inc.
4
* This file is part of Assuan.
6
* Assuan is free software; you can redistribute it and/or modify it
7
* under the terms of the GNU Lesser General Public License as
8
* published by the Free Software Foundation; either version 2.1 of
9
* the License, or (at your option) any later version.
11
* Assuan is distributed in the hope that it will be useful, but
12
* WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
* Lesser General Public License for more details.
16
* You should have received a copy of the GNU Lesser General Public
17
* License along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
28
#include "assuan-defs.h"
30
#define xtoi_1(p) (*(p) <= '9'? (*(p)- '0'): \
31
*(p) <= 'F'? (*(p)-'A'+10):(*(p)-'a'+10))
32
#define xtoi_2(p) ((xtoi_1(p) * 16) + xtoi_1((p)+1))
36
_assuan_read_from_server (ASSUAN_CONTEXT ctx, int *okay, int *off)
46
rc = _assuan_read_line (ctx);
49
line = ctx->inbound.line;
50
linelen = ctx->inbound.linelen;
52
while (*line == '#' || !linelen);
55
&& line[0] == 'D' && line[1] == ' ')
57
*okay = 2; /* data line */
62
&& (line[1] == '\0' || line[1] == ' '))
66
while (line[*off] == ' ')
70
&& line[0] == 'O' && line[1] == 'K'
71
&& (line[2] == '\0' || line[2] == ' '))
75
while (line[*off] == ' ')
79
&& line[0] == 'E' && line[1] == 'R' && line[2] == 'R'
80
&& (line[3] == '\0' || line[3] == ' '))
84
while (line[*off] == ' ')
88
&& line[0] == 'I' && line[1] == 'N' && line[2] == 'Q'
89
&& line[3] == 'U' && line[4] == 'I' && line[5] == 'R'
91
&& (line[7] == '\0' || line[7] == ' '))
95
while (line[*off] == ' ')
99
&& line[0] == 'E' && line[1] == 'N' && line[2] == 'D'
100
&& (line[3] == '\0' || line[3] == ' '))
102
*okay = 5; /* end line */
106
rc = ASSUAN_Invalid_Response;
114
* @ctx: The Assuan context
115
* @command: Coimmand line to be send to server
116
* @data_cb: Callback function for data lines
117
* @data_cb_arg: first argument passed to @data_cb
118
* @inquire_cb: Callback function for a inquire response
119
* @inquire_cb_arg: first argument passed to @inquire_cb
120
* @status_cb: Callback function for a status response
121
* @status_cb_arg: first argument passed to @status_cb
123
* FIXME: Write documentation
125
* Return value: 0 on success or error code. The error code may be
126
* the one one returned by the server in error lines or from the
127
* callback functions.
130
assuan_transact (ASSUAN_CONTEXT ctx,
132
AssuanError (*data_cb)(void *, const void *, size_t),
134
AssuanError (*inquire_cb)(void*, const char *),
135
void *inquire_cb_arg,
136
AssuanError (*status_cb)(void*, const char *),
143
rc = assuan_write_line (ctx, command);
148
rc = _assuan_read_from_server (ctx, &okay, &off);
150
return rc; /* error reading from server */
152
line = ctx->inbound.line + off;
153
linelen = ctx->inbound.linelen - off;
159
rc = ASSUAN_Server_Fault;
164
rc = ASSUAN_No_Data_Callback;
167
unsigned char *s, *d;
169
for (s=d=line; linelen; linelen--)
171
if (*s == '%' && linelen > 2)
172
{ /* handle escaping */
181
*d = 0; /* add a hidden string terminator */
182
rc = data_cb (data_cb_arg, line, d - line);
191
assuan_write_line (ctx, "END"); /* get out of inquire mode */
192
_assuan_read_from_server (ctx, &okay, &off); /* dummy read */
193
rc = ASSUAN_No_Inquire_Callback;
197
rc = inquire_cb (inquire_cb_arg, line);
199
rc = assuan_send_data (ctx, NULL, 0); /* flush and send END */
207
rc = status_cb (status_cb_arg, line);
214
rc = ASSUAN_No_Data_Callback;
217
rc = data_cb (data_cb_arg, NULL, 0);