blob: 4728986cba0b368647b3d11e89d056f6f380381d [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
Willy Tarreauc7e42382012-08-24 19:22:53 +02002 * Channel management functions.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003 *
Willy Tarreaua27dc192014-11-27 22:10:04 +01004 * Copyright 2000-2014 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Krzysztof Piotr Oledzkiba8d7d32009-10-10 21:06:03 +020013#include <ctype.h>
Willy Tarreauc0dde7a2007-01-01 21:38:07 +010014#include <stdarg.h>
15#include <stdio.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020016#include <string.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020017
18#include <common/config.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020019#include <common/buffer.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020020
Willy Tarreau9b28e032012-10-12 23:49:43 +020021#include <proto/channel.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020022
Willy Tarreau7341d942007-05-13 19:56:02 +020023
Willy Tarreau8263d2b2012-08-28 00:06:31 +020024/* Schedule up to <bytes> more bytes to be forwarded via the channel without
25 * notifying the owner task. Any data pending in the buffer are scheduled to be
26 * sent as well, in the limit of the number of bytes to forward. This must be
27 * the only method to use to schedule bytes to be forwarded. If the requested
28 * number is too large, it is automatically adjusted. The number of bytes taken
29 * into account is returned. Directly touching ->to_forward will cause lockups
30 * when buf->o goes down to zero if nobody is ready to push the remaining data.
Willy Tarreau0bc34932011-03-28 16:25:58 +020031 */
Willy Tarreau55a69062012-10-26 00:21:52 +020032unsigned long long __channel_forward(struct channel *chn, unsigned long long bytes)
Willy Tarreau0bc34932011-03-28 16:25:58 +020033{
Willy Tarreau0bc34932011-03-28 16:25:58 +020034 unsigned int new_forward;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +010035 unsigned int forwarded;
Willy Tarreau0bc34932011-03-28 16:25:58 +020036
Willy Tarreau9b28e032012-10-12 23:49:43 +020037 forwarded = chn->buf->i;
38 b_adv(chn->buf, chn->buf->i);
Willy Tarreau0bc34932011-03-28 16:25:58 +020039
Willy Tarreau0bc34932011-03-28 16:25:58 +020040 /* Note: the case below is the only case where we may return
41 * a byte count that does not fit into a 32-bit number.
42 */
Willy Tarreau974ced62012-10-12 23:11:02 +020043 if (likely(chn->to_forward == CHN_INFINITE_FORWARD))
Willy Tarreau0bc34932011-03-28 16:25:58 +020044 return bytes;
45
Willy Tarreau03cdb7c2012-08-27 23:14:58 +020046 if (likely(bytes == CHN_INFINITE_FORWARD)) {
Willy Tarreau974ced62012-10-12 23:11:02 +020047 chn->to_forward = bytes;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +010048 return bytes;
49 }
50
Willy Tarreau974ced62012-10-12 23:11:02 +020051 new_forward = chn->to_forward + bytes - forwarded;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +010052 bytes = forwarded; /* at least those bytes were scheduled */
Willy Tarreau0bc34932011-03-28 16:25:58 +020053
Willy Tarreau974ced62012-10-12 23:11:02 +020054 if (new_forward <= chn->to_forward) {
Willy Tarreau0bc34932011-03-28 16:25:58 +020055 /* integer overflow detected, let's assume no more than 2G at once */
56 new_forward = MID_RANGE(new_forward);
57 }
58
Willy Tarreau974ced62012-10-12 23:11:02 +020059 if (new_forward > chn->to_forward) {
60 bytes += new_forward - chn->to_forward;
61 chn->to_forward = new_forward;
Willy Tarreau0bc34932011-03-28 16:25:58 +020062 }
63 return bytes;
64}
Willy Tarreaubaaee002006-06-26 02:48:02 +020065
Willy Tarreau8263d2b2012-08-28 00:06:31 +020066/* writes <len> bytes from message <msg> to the channel's buffer. Returns -1 in
67 * case of success, -2 if the message is larger than the buffer size, or the
68 * number of bytes available otherwise. The send limit is automatically
69 * adjusted to the amount of data written. FIXME-20060521: handle unaligned
70 * data. Note: this function appends data to the buffer's output and possibly
71 * overwrites any pending input data which are assumed not to exist.
Willy Tarreaubaaee002006-06-26 02:48:02 +020072 */
Willy Tarreau974ced62012-10-12 23:11:02 +020073int bo_inject(struct channel *chn, const char *msg, int len)
Willy Tarreaubaaee002006-06-26 02:48:02 +020074{
75 int max;
76
Willy Tarreauaeac3192009-08-31 08:09:57 +020077 if (len == 0)
78 return -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +020079
Willy Tarreau320ec2a2016-02-25 16:15:19 +010080 if (len < 0 || len > chn->buf->size) {
Willy Tarreau078e2942009-08-18 07:19:39 +020081 /* we can't write this chunk and will never be able to, because
82 * it is larger than the buffer. This must be reported as an
83 * error. Then we return -2 so that writers that don't care can
84 * ignore it and go on, and others can check for this value.
85 */
86 return -2;
87 }
88
Willy Tarreau9b28e032012-10-12 23:49:43 +020089 max = buffer_realign(chn->buf);
Willy Tarreauaeac3192009-08-31 08:09:57 +020090
Willy Tarreaubaaee002006-06-26 02:48:02 +020091 if (len > max)
92 return max;
93
Willy Tarreau9b28e032012-10-12 23:49:43 +020094 memcpy(chn->buf->p, msg, len);
95 chn->buf->o += len;
96 chn->buf->p = b_ptr(chn->buf, len);
Willy Tarreau974ced62012-10-12 23:11:02 +020097 chn->total += len;
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +020098 return -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +020099}
100
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200101/* Tries to copy character <c> into the channel's buffer after some length
Willy Tarreau974ced62012-10-12 23:11:02 +0200102 * controls. The chn->o and to_forward pointers are updated. If the channel
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200103 * input is closed, -2 is returned. If there is not enough room left in the
104 * buffer, -1 is returned. Otherwise the number of bytes copied is returned
105 * (1). Channel flag READ_PARTIAL is updated if some data can be transferred.
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100106 */
Willy Tarreau974ced62012-10-12 23:11:02 +0200107int bi_putchr(struct channel *chn, char c)
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100108{
Willy Tarreau974ced62012-10-12 23:11:02 +0200109 if (unlikely(channel_input_closed(chn)))
Willy Tarreau74b08c92010-09-08 17:04:31 +0200110 return -2;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100111
Willy Tarreaubc18da12015-03-13 14:00:47 +0100112 if (!channel_may_recv(chn))
Krzysztof Piotr Oledzki8e4b21d2008-04-20 21:34:47 +0200113 return -1;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100114
Willy Tarreau9b28e032012-10-12 23:49:43 +0200115 *bi_end(chn->buf) = c;
Willy Tarreau74b08c92010-09-08 17:04:31 +0200116
Willy Tarreau9b28e032012-10-12 23:49:43 +0200117 chn->buf->i++;
Willy Tarreau974ced62012-10-12 23:11:02 +0200118 chn->flags |= CF_READ_PARTIAL;
Willy Tarreau74b08c92010-09-08 17:04:31 +0200119
Willy Tarreau974ced62012-10-12 23:11:02 +0200120 if (chn->to_forward >= 1) {
121 if (chn->to_forward != CHN_INFINITE_FORWARD)
122 chn->to_forward--;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200123 b_adv(chn->buf, 1);
Willy Tarreau74b08c92010-09-08 17:04:31 +0200124 }
125
Willy Tarreau974ced62012-10-12 23:11:02 +0200126 chn->total++;
Willy Tarreau74b08c92010-09-08 17:04:31 +0200127 return 1;
128}
129
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200130/* Tries to copy block <blk> at once into the channel's buffer after length
Willy Tarreau974ced62012-10-12 23:11:02 +0200131 * controls. The chn->o and to_forward pointers are updated. If the channel
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200132 * input is closed, -2 is returned. If the block is too large for this buffer,
133 * -3 is returned. If there is not enough room left in the buffer, -1 is
134 * returned. Otherwise the number of bytes copied is returned (0 being a valid
135 * number). Channel flag READ_PARTIAL is updated if some data can be
Willy Tarreaubc18da12015-03-13 14:00:47 +0100136 * transferred.
Willy Tarreau74b08c92010-09-08 17:04:31 +0200137 */
Willy Tarreau974ced62012-10-12 23:11:02 +0200138int bi_putblk(struct channel *chn, const char *blk, int len)
Willy Tarreau74b08c92010-09-08 17:04:31 +0200139{
140 int max;
141
Willy Tarreau974ced62012-10-12 23:11:02 +0200142 if (unlikely(channel_input_closed(chn)))
Willy Tarreau74b08c92010-09-08 17:04:31 +0200143 return -2;
144
Willy Tarreau320ec2a2016-02-25 16:15:19 +0100145 if (len < 0)
146 return -3;
147
Willy Tarreau3f5096d2015-01-14 20:21:43 +0100148 max = channel_recv_limit(chn);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200149 if (unlikely(len > max - buffer_len(chn->buf))) {
Willy Tarreau591fedc2010-08-10 15:28:21 +0200150 /* we can't write this chunk right now because the buffer is
151 * almost full or because the block is too large. Return the
152 * available space or -2 if impossible.
Willy Tarreau078e2942009-08-18 07:19:39 +0200153 */
Willy Tarreau591fedc2010-08-10 15:28:21 +0200154 if (len > max)
Willy Tarreau74b08c92010-09-08 17:04:31 +0200155 return -3;
Willy Tarreau078e2942009-08-18 07:19:39 +0200156
Willy Tarreau74b08c92010-09-08 17:04:31 +0200157 return -1;
Willy Tarreau591fedc2010-08-10 15:28:21 +0200158 }
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100159
Willy Tarreau74b08c92010-09-08 17:04:31 +0200160 if (unlikely(len == 0))
161 return 0;
162
Willy Tarreau591fedc2010-08-10 15:28:21 +0200163 /* OK so the data fits in the buffer in one or two blocks */
Willy Tarreau285ff0f2014-04-24 17:02:57 +0200164 max = buffer_contig_space(chn->buf);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200165 memcpy(bi_end(chn->buf), blk, MIN(len, max));
Willy Tarreauaeac3192009-08-31 08:09:57 +0200166 if (len > max)
Willy Tarreau9b28e032012-10-12 23:49:43 +0200167 memcpy(chn->buf->data, blk + max, len - max);
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100168
Willy Tarreau9b28e032012-10-12 23:49:43 +0200169 chn->buf->i += len;
Willy Tarreau974ced62012-10-12 23:11:02 +0200170 chn->total += len;
171 if (chn->to_forward) {
Willy Tarreau31971e52009-09-20 12:07:52 +0200172 unsigned long fwd = len;
Willy Tarreau974ced62012-10-12 23:11:02 +0200173 if (chn->to_forward != CHN_INFINITE_FORWARD) {
174 if (fwd > chn->to_forward)
175 fwd = chn->to_forward;
176 chn->to_forward -= fwd;
Willy Tarreau31971e52009-09-20 12:07:52 +0200177 }
Willy Tarreau9b28e032012-10-12 23:49:43 +0200178 b_adv(chn->buf, fwd);
Willy Tarreauaeac3192009-08-31 08:09:57 +0200179 }
180
Willy Tarreaufb0e9202009-09-23 23:47:55 +0200181 /* notify that some data was read from the SI into the buffer */
Willy Tarreau974ced62012-10-12 23:11:02 +0200182 chn->flags |= CF_READ_PARTIAL;
Willy Tarreau74b08c92010-09-08 17:04:31 +0200183 return len;
Willy Tarreauc0dde7a2007-01-01 21:38:07 +0100184}
185
Willy Tarreaub034b252014-12-08 18:14:53 +0100186/* Tries to copy the whole buffer <buf> into the channel's buffer after length
187 * controls. It will only succeed if the target buffer is empty, in which case
188 * it will simply swap the buffers. The buffer not attached to the channel is
189 * returned so that the caller can store it locally. The chn->buf->o and
190 * to_forward pointers are updated. If the output buffer is a dummy buffer or
191 * if it still contains data <buf> is returned, indicating that nothing could
192 * be done. Channel flag READ_PARTIAL is updated if some data can be transferred.
193 * The chunk's length is updated with the number of bytes sent. On errors, NULL
194 * is returned. Note that only buf->i is considered.
195 */
196struct buffer *bi_swpbuf(struct channel *chn, struct buffer *buf)
197{
198 struct buffer *old;
199
200 if (unlikely(channel_input_closed(chn)))
201 return NULL;
202
Willy Tarreaubc18da12015-03-13 14:00:47 +0100203 if (!chn->buf->size || !buffer_empty(chn->buf))
Willy Tarreaub034b252014-12-08 18:14:53 +0100204 return buf;
Willy Tarreaub034b252014-12-08 18:14:53 +0100205
206 old = chn->buf;
207 chn->buf = buf;
208
209 if (!buf->i)
210 return old;
211
212 chn->total += buf->i;
213
214 if (chn->to_forward) {
215 unsigned long fwd = buf->i;
216 if (chn->to_forward != CHN_INFINITE_FORWARD) {
217 if (fwd > chn->to_forward)
218 fwd = chn->to_forward;
219 chn->to_forward -= fwd;
220 }
221 b_adv(chn->buf, fwd);
222 }
223
224 /* notify that some data was read from the SI into the buffer */
225 chn->flags |= CF_READ_PARTIAL;
226 return old;
227}
228
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200229/* Gets one text line out of a channel's buffer from a stream interface.
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200230 * Return values :
231 * >0 : number of bytes read. Includes the \n if present before len or end.
Willy Tarreau74b08c92010-09-08 17:04:31 +0200232 * =0 : no '\n' before end found. <str> is left undefined.
233 * <0 : no more bytes readable because output is shut.
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200234 * The channel status is not changed. The caller must call bo_skip() to
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200235 * update it. The '\n' is waited for as long as neither the buffer nor the
236 * output are full. If either of them is full, the string may be returned
237 * as is, without the '\n'.
238 */
Willy Tarreau974ced62012-10-12 23:11:02 +0200239int bo_getline(struct channel *chn, char *str, int len)
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200240{
241 int ret, max;
242 char *p;
243
244 ret = 0;
245 max = len;
Willy Tarreau74b08c92010-09-08 17:04:31 +0200246
247 /* closed or empty + imminent close = -1; empty = 0 */
Willy Tarreau974ced62012-10-12 23:11:02 +0200248 if (unlikely((chn->flags & CF_SHUTW) || channel_is_empty(chn))) {
249 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200250 ret = -1;
251 goto out;
252 }
253
Willy Tarreau9b28e032012-10-12 23:49:43 +0200254 p = bo_ptr(chn->buf);
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200255
Willy Tarreau9b28e032012-10-12 23:49:43 +0200256 if (max > chn->buf->o) {
257 max = chn->buf->o;
Willy Tarreau2e1dd3d2009-09-23 22:56:07 +0200258 str[max-1] = 0;
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200259 }
260 while (max) {
261 *str++ = *p;
262 ret++;
263 max--;
264
265 if (*p == '\n')
266 break;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200267 p = buffer_wrap_add(chn->buf, p + 1);
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200268 }
Willy Tarreau82de2b62013-12-10 18:58:23 +0100269 if (ret > 0 && ret < len &&
Willy Tarreau3889fff2015-01-13 20:20:10 +0100270 (ret < chn->buf->o || channel_may_recv(chn)) &&
Willy Tarreau2e1dd3d2009-09-23 22:56:07 +0200271 *(str-1) != '\n' &&
Willy Tarreau974ced62012-10-12 23:11:02 +0200272 !(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
Willy Tarreau4fe7a2e2009-09-01 06:41:32 +0200273 ret = 0;
274 out:
275 if (max)
276 *str = 0;
277 return ret;
278}
279
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200280/* Gets one full block of data at once from a channel's buffer, optionally from
281 * a specific offset. Return values :
Willy Tarreau74b08c92010-09-08 17:04:31 +0200282 * >0 : number of bytes read, equal to requested size.
283 * =0 : not enough data available. <blk> is left undefined.
284 * <0 : no more bytes readable because output is shut.
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200285 * The channel status is not changed. The caller must call bo_skip() to
Willy Tarreau74b08c92010-09-08 17:04:31 +0200286 * update it.
287 */
Willy Tarreau974ced62012-10-12 23:11:02 +0200288int bo_getblk(struct channel *chn, char *blk, int len, int offset)
Willy Tarreau74b08c92010-09-08 17:04:31 +0200289{
290 int firstblock;
291
Willy Tarreau974ced62012-10-12 23:11:02 +0200292 if (chn->flags & CF_SHUTW)
Willy Tarreau74b08c92010-09-08 17:04:31 +0200293 return -1;
294
Willy Tarreau9b28e032012-10-12 23:49:43 +0200295 if (len + offset > chn->buf->o) {
Willy Tarreau974ced62012-10-12 23:11:02 +0200296 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreau74b08c92010-09-08 17:04:31 +0200297 return -1;
298 return 0;
299 }
300
Willy Tarreau9b28e032012-10-12 23:49:43 +0200301 firstblock = chn->buf->data + chn->buf->size - bo_ptr(chn->buf);
Willy Tarreau74b08c92010-09-08 17:04:31 +0200302 if (firstblock > offset) {
303 if (firstblock >= len + offset) {
Willy Tarreau9b28e032012-10-12 23:49:43 +0200304 memcpy(blk, bo_ptr(chn->buf) + offset, len);
Willy Tarreau74b08c92010-09-08 17:04:31 +0200305 return len;
306 }
307
Willy Tarreau9b28e032012-10-12 23:49:43 +0200308 memcpy(blk, bo_ptr(chn->buf) + offset, firstblock - offset);
309 memcpy(blk + firstblock - offset, chn->buf->data, len - firstblock + offset);
Willy Tarreau74b08c92010-09-08 17:04:31 +0200310 return len;
311 }
312
Willy Tarreau9b28e032012-10-12 23:49:43 +0200313 memcpy(blk, chn->buf->data + offset - firstblock, len);
Willy Tarreau74b08c92010-09-08 17:04:31 +0200314 return len;
315}
316
Thierry FOURNIERca16b032015-02-16 19:26:48 +0100317/* Gets one or two blocks of data at once from a channel's output buffer.
318 * Return values :
319 * >0 : number of blocks filled (1 or 2). blk1 is always filled before blk2.
320 * =0 : not enough data available. <blk*> are left undefined.
321 * <0 : no more bytes readable because output is shut.
322 * The channel status is not changed. The caller must call bo_skip() to
323 * update it. Unused buffers are left in an undefined state.
324 */
325int bo_getblk_nc(struct channel *chn, char **blk1, int *len1, char **blk2, int *len2)
326{
327 if (unlikely(chn->buf->o == 0)) {
328 if (chn->flags & CF_SHUTW)
329 return -1;
330 return 0;
331 }
332
333 if (unlikely(chn->buf->p - chn->buf->o < chn->buf->data)) {
334 *blk1 = chn->buf->p - chn->buf->o + chn->buf->size;
335 *len1 = chn->buf->data + chn->buf->size - *blk1;
336 *blk2 = chn->buf->data;
337 *len2 = chn->buf->p - chn->buf->data;
338 return 2;
339 }
340
341 *blk1 = chn->buf->p - chn->buf->o;
342 *len1 = chn->buf->o;
343 return 1;
344}
345
346/* Gets one text line out of a channel's output buffer from a stream interface.
347 * Return values :
348 * >0 : number of blocks returned (1 or 2). blk1 is always filled before blk2.
349 * =0 : not enough data available.
350 * <0 : no more bytes readable because output is shut.
351 * The '\n' is waited for as long as neither the buffer nor the output are
352 * full. If either of them is full, the string may be returned as is, without
353 * the '\n'. Unused buffers are left in an undefined state.
354 */
355int bo_getline_nc(struct channel *chn,
356 char **blk1, int *len1,
357 char **blk2, int *len2)
358{
359 int retcode;
360 int l;
361
362 retcode = bo_getblk_nc(chn, blk1, len1, blk2, len2);
363 if (unlikely(retcode) <= 0)
364 return retcode;
365
366 for (l = 0; l < *len1 && (*blk1)[l] != '\n'; l++);
367 if (l < *len1 && (*blk1)[l] == '\n') {
368 *len1 = l + 1;
369 return 1;
370 }
371
372 if (retcode >= 2) {
373 for (l = 0; l < *len2 && (*blk2)[l] != '\n'; l++);
374 if (l < *len2 && (*blk2)[l] == '\n') {
375 *len2 = l + 1;
376 return 2;
377 }
378 }
379
380 if (chn->flags & CF_SHUTW) {
381 /* If we have found no LF and the buffer is shut, then
382 * the resulting string is made of the concatenation of
383 * the pending blocks (1 or 2).
384 */
385 return retcode;
386 }
387
388 /* No LF yet and not shut yet */
389 return 0;
390}
391
392/* Gets one full block of data at once from a channel's input buffer.
393 * This function can return the data slitted in one or two blocks.
394 * Return values :
395 * >0 : number of blocks returned (1 or 2). blk1 is always filled before blk2.
396 * =0 : not enough data available.
397 * <0 : no more bytes readable because input is shut.
398 */
399int bi_getblk_nc(struct channel *chn,
400 char **blk1, int *len1,
401 char **blk2, int *len2)
402{
403 if (unlikely(chn->buf->i == 0)) {
404 if (chn->flags & CF_SHUTR)
405 return -1;
406 return 0;
407 }
408
409 if (unlikely(chn->buf->p + chn->buf->i > chn->buf->data + chn->buf->size)) {
410 *blk1 = chn->buf->p;
411 *len1 = chn->buf->data + chn->buf->size - chn->buf->p;
412 *blk2 = chn->buf->data;
413 *len2 = chn->buf->i - *len1;
414 return 2;
415 }
416
417 *blk1 = chn->buf->p;
418 *len1 = chn->buf->i;
419 return 1;
420}
421
422/* Gets one text line out of a channel's input buffer from a stream interface.
423 * Return values :
424 * >0 : number of blocks returned (1 or 2). blk1 is always filled before blk2.
425 * =0 : not enough data available.
426 * <0 : no more bytes readable because output is shut.
427 * The '\n' is waited for as long as neither the buffer nor the input are
428 * full. If either of them is full, the string may be returned as is, without
429 * the '\n'. Unused buffers are left in an undefined state.
430 */
431int bi_getline_nc(struct channel *chn,
432 char **blk1, int *len1,
433 char **blk2, int *len2)
434{
435 int retcode;
436 int l;
437
438 retcode = bi_getblk_nc(chn, blk1, len1, blk2, len2);
439 if (unlikely(retcode) <= 0)
440 return retcode;
441
442 for (l = 0; l < *len1 && (*blk1)[l] != '\n'; l++);
443 if (l < *len1 && (*blk1)[l] == '\n') {
444 *len1 = l + 1;
445 return 1;
446 }
447
448 if (retcode >= 2) {
449 for (l = 0; l < *len2 && (*blk2)[l] != '\n'; l++);
450 if (l < *len2 && (*blk2)[l] == '\n') {
451 *len2 = l + 1;
452 return 2;
453 }
454 }
455
456 if (chn->flags & CF_SHUTW) {
457 /* If we have found no LF and the buffer is shut, then
458 * the resulting string is made of the concatenation of
459 * the pending blocks (1 or 2).
460 */
461 return retcode;
462 }
463
464 /* No LF yet and not shut yet */
465 return 0;
466}
467
Krzysztof Piotr Oledzkiba8d7d32009-10-10 21:06:03 +0200468/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200469 * Local variables:
470 * c-indent-level: 8
471 * c-basic-offset: 8
472 * End:
473 */