1
1
/* assuan-buffer.c - read and send data
2
* Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc.
2
* Copyright (C) 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
4
4
* This file is part of Assuan.
49
52
/* Read an entire line. */
51
54
readline (ASSUAN_CONTEXT ctx, char *buf, size_t buflen,
52
int *r_nread, int *eof)
55
int *r_nread, int *r_eof)
54
57
size_t nleft = buflen;
61
ssize_t n = ctx->io->read (ctx, buf, nleft);
64
ssize_t n = ctx->io->readfnc (ctx, buf, nleft);
128
fprintf (ctx->log_fp, "%s[%p] <- [Error: %s]\n",
129
assuan_get_assuan_log_prefix (), ctx, strerror (errno));
131
fprintf (ctx->log_fp, "%s[%u.%p] DBG: <- [Error: %s]\n",
132
assuan_get_assuan_log_prefix (),
133
(unsigned int)getpid (), ctx, strerror (errno));
130
134
return ASSUAN_Read_Error;
134
138
assert (ctx->inbound.eof);
136
fprintf (ctx->log_fp, "%s[%p] <- [EOF]\n",
137
assuan_get_assuan_log_prefix (), ctx);
140
fprintf (ctx->log_fp, "%s[%u.%p] DBG: <- [EOF]\n",
141
assuan_get_assuan_log_prefix (),
142
(unsigned int)getpid (), ctx);
165
170
ctx->inbound.linelen = endp - line;
168
fprintf (ctx->log_fp, "%s[%p] <- ",
169
assuan_get_assuan_log_prefix (), ctx);
173
fprintf (ctx->log_fp, "%s[%u.%p] DBG: <- ",
174
assuan_get_assuan_log_prefix (),
175
(unsigned int)getpid (), ctx);
170
176
if (ctx->confidential)
171
177
fputs ("[Confidential data not shown]", ctx->log_fp);
183
fprintf (ctx->log_fp, "%s[%p] <- [Invalid line]\n",
184
assuan_get_assuan_log_prefix (), ctx);
189
fprintf (ctx->log_fp, "%s[%u.%p] DBG: <- [Invalid line]\n",
190
assuan_get_assuan_log_prefix (),
191
(unsigned int)getpid (), ctx);
186
193
ctx->inbound.linelen = 0;
187
194
return ctx->inbound.eof ? ASSUAN_Line_Not_Terminated
199
206
Returns 0 on success or an assuan error code.
200
207
See also: assuan_pending_line().
203
210
assuan_read_line (ASSUAN_CONTEXT ctx, char **line, size_t *linelen)
208
215
return ASSUAN_Invalid_Value;
234
_assuan_write_line (assuan_context_t ctx, const char *prefix,
235
const char *line, size_t len)
238
size_t prefixlen = prefix? strlen (prefix):0;
240
/* Make sure that the line is short enough. */
241
if (len + prefixlen + 2 > ASSUAN_LINELENGTH)
244
fprintf (ctx->log_fp, "%s[%u.%p] DBG: -> "
245
"[supplied line too long -truncated]\n",
246
assuan_get_assuan_log_prefix (),
247
(unsigned int)getpid (), ctx);
250
if (len > ASSUAN_LINELENGTH - prefixlen - 2)
251
len = ASSUAN_LINELENGTH - prefixlen - 2 - 1;
254
/* Fixme: we should do some kind of line buffering. */
257
fprintf (ctx->log_fp, "%s[%u.%p] DBG: -> ",
258
assuan_get_assuan_log_prefix (),
259
(unsigned int)getpid (), ctx);
260
if (ctx->confidential)
261
fputs ("[Confidential data not shown]", ctx->log_fp);
263
_assuan_log_print_buffer (ctx->log_fp, line, len);
264
putc ('\n', ctx->log_fp);
269
rc = writen (ctx, prefix, prefixlen);
271
rc = ASSUAN_Write_Error;
275
rc = writen (ctx, line, len);
277
rc = ASSUAN_Write_Error;
280
rc = writen (ctx, "\n", 1);
282
rc = ASSUAN_Write_Error;
227
290
assuan_write_line (ASSUAN_CONTEXT ctx, const char *line)
238
300
s = strchr (line, '\n');
239
301
len = s? (s-line) : strlen (line);
241
if (len > LINELENGTH - 2)
242
return ASSUAN_Line_Too_Long;
244
/* fixme: we should do some kind of line buffering. */
247
fprintf (ctx->log_fp, "%s[%p] -> ",
248
assuan_get_assuan_log_prefix (), ctx);
250
fputs ("[supplied line contained a LF]", ctx->log_fp);
251
if (ctx->confidential)
252
fputs ("[Confidential data not shown]", ctx->log_fp);
254
_assuan_log_print_buffer (ctx->log_fp, line, len);
255
putc ('\n', ctx->log_fp);
258
rc = writen (ctx, line, len);
260
rc = ASSUAN_Write_Error;
263
rc = writen (ctx, "\n", 1);
265
rc = ASSUAN_Write_Error;
303
if (ctx->log_fp && s)
304
fprintf (ctx->log_fp, "%s[%u.%p] DBG: -> "
305
"[supplied line contained a LF -truncated]\n",
306
assuan_get_assuan_log_prefix (),
307
(unsigned int)getpid (), ctx);
309
return _assuan_write_line (ctx, NULL, line, len);
273
314
/* Write out the data in buffer as datalines with line wrapping and
274
percent escaping. This fucntion is used for GNU's custom streams */
315
percent escaping. This function is used for GNU's custom streams */
276
_assuan_cookie_write_data (void *cookie, const char *buffer, size_t size)
317
_assuan_cookie_write_data (void *cookie, const char *buffer, size_t orig_size)
278
319
ASSUAN_CONTEXT ctx = cookie;
320
size_t size = orig_size;
320
fprintf (ctx->log_fp, "%s[%p] -> ",
321
assuan_get_assuan_log_prefix (), ctx);
362
fprintf (ctx->log_fp, "%s[%u.%p] DBG: -> ",
363
assuan_get_assuan_log_prefix (),
364
(unsigned int)getpid (), ctx);
323
366
if (ctx->confidential)
324
367
fputs ("[Confidential data not shown]", ctx->log_fp);
343
386
ctx->outbound.data.linelen = linelen;
387
return (int)orig_size;
348
391
/* Write out any buffered data
349
This fucntion is used for GNU's custom streams */
392
This function is used for GNU's custom streams */
351
394
_assuan_cookie_write_flush (void *cookie)
367
fprintf (ctx->log_fp, "%s[%p] -> ",
368
assuan_get_assuan_log_prefix (), ctx);
410
fprintf (ctx->log_fp, "%s[%u.%p] DBG: -> ",
411
assuan_get_assuan_log_prefix (),
412
(unsigned int)getpid (), ctx);
369
413
if (ctx->confidential)
370
414
fputs ("[Confidential data not shown]", ctx->log_fp);