3 * $Id: lbuf.h,v 1.5 2001/01/20 12:06:01 mdw Exp $
5 * Block-to-line buffering
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the mLib utilities library.
14 * mLib is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * mLib is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with mLib; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.5 2001/01/20 12:06:01 mdw
34 * Define flags with macros, to ensure unsignedness.
36 * Revision 1.4 2000/06/17 10:38:14 mdw
37 * Add support for variable buffer sizes.
39 * Revision 1.3 1999/12/10 23:42:04 mdw
40 * Change header file guard names.
42 * Revision 1.2 1999/05/17 20:36:08 mdw
43 * Make the magical constants for the buffer flags uppercase.
45 * Revision 1.1 1999/05/14 21:01:14 mdw
46 * Integrated `select' handling bits from the background resolver project.
57 /*----- Line buffering ----------------------------------------------------*
59 * The line buffer accepts as input arbitrary-sized lumps of data and
60 * converts them, by passing them to a client-supplied function, into a
61 * sequence of lines. It's particularly useful when performing multiplexed
62 * network I/O. It's not normally acceptable to block while waiting for the
63 * rest of a text line to arrive, for example. The line buffer stores the
64 * start of the line until the rest of it arrives later.
66 * A line is a piece of text terminated by either a linefeed or a carriage-
67 * return/linefeed pair. (The former is there to cope with Unix; the latter
68 * copes with Internet-format line ends.)
70 * There's a limit to the size of lines that the buffer can cope with. It's
71 * not hard to remove this limit, but it's probably a bad idea in a lot of
72 * cases, because it'd allow a remote user to gobble arbitrary amounts of
73 * your memory. If a line exceeds the limit, it is truncated: the initial
74 * portion of the line is processed normally, and the remaining portion is
77 * Lines extracted from the input data are passed, one at a time, to a
78 * `handler function', along with a caller-supplied pointer argument to
79 * provide the handler with some context. The line read is null-terminated
80 * and does not include the trailing newline characters. It is legal for a
81 * handler function to modify the string it is passed. However, writing
82 * beyond the terminating null byte is not allowed. An end-of-file condition
83 * is signalled to the handler by passing it a null pointer rather than the
84 * address of a string.
86 * A complexity arises because of the concept of a `disabled' buffer.
87 * Disablement is really a higher-level concept, but it turns out to be
88 * important to implement it here. It's useful for a line handler function
89 * to `disable' itself, so that it doesn't get called any more. For example,
90 * this might happen if it encouters an error, or when it finishes reading
91 * everything it wanted to read. The line buffer needs to be `in the loop'
92 * so that it stops attempting to flush any further lines stored in its
93 * buffer towards a handler function which isn't ready to accept them.
94 * Buffers are initially enabled, although higher- level buffering systems
95 * might well disable them immediately for their own purposes.
98 /*----- Header files ------------------------------------------------------*/
106 /*----- Data structures ---------------------------------------------------*/
108 /* --- The buffer structure --- *
110 * The only thing that's safe to fiddle with in here is the @lbuf_enable@
111 * flag. Only higher-level buffering systems should be playing with even
115 typedef struct lbuf
{
116 void (*func
)(char */
*s*/
, void */
*p*/
); /* Handler function */
117 void *p
; /* Argument for handler */
118 size_t len
; /* Length of data in buffer */
119 size_t sz
; /* Buffer size */
120 unsigned f
; /* Various useful state flags */
121 arena
*a
; /* Memory allocation arena */
122 char *buf
; /* The actual buffer */
125 #define LBUF_CR 1u /* Read a carriage return */
126 #define LBUF_ENABLE 2u /* Buffer is currently enabled */
128 /*----- Functions provided ------------------------------------------------*/
130 /* --- @lbuf_flush@ --- *
132 * Arguments: @lbuf *b@ = pointer to buffer block
133 * @char *p@ = pointer to where to start searching
134 * @size_t len@ = length of new material added
138 * Use: Flushes any complete lines in a line buffer. New material
139 * is assumed to have been added starting at @p@. If @p@ is
140 * null, then the scan starts at the beginning of the buffer,
141 * and the size of data already in the buffer is used in place
144 * It is assumed that the buffer is initially enabled. You
145 * shouldn't be contributing data to a disabled buffer anyway.
146 * However, the buffer handler may at some point disable itself,
147 * and @lbuf_flush@ can cope with this eventuality. Any pending
148 * data is left at the start of the buffer and can be flushed
149 * out by calling @lbuf_flush(b, 0, 0)@ if the buffer is ever
153 extern void lbuf_flush(lbuf */
*b*/
, char */
*p*/
, size_t /*len*/);
155 /* --- @lbuf_close@ --- *
157 * Arguments: @lbuf *b@ = pointer to buffer block
161 * Use: Empties the buffer of any data currently lurking in it, and
162 * informs the client that this has happened. It's assumed that
163 * the buffer is enabled: you shouldn't be reading close events
164 * on disabled buffers.
167 extern void lbuf_close(lbuf */
*b*/
);
169 /* --- @lbuf_free@ --- *
171 * Arguments: @lbuf *b@ = pointer to buffer block
172 * @char **p@ = output pointer to free space
174 * Returns: Free buffer size.
176 * Use: Returns the free portion of a line buffer. Data can then be
177 * written to this portion, and split out into lines by calling
181 extern size_t lbuf_free(lbuf */
*b*/
, char **/
*p*/
);
183 /* --- @lbuf_snarf@ --- *
185 * Arguments: @lbuf *b@ = pointer to buffer block
186 * @const void *p@ = pointer to input data buffer
187 * @size_t sz@ = size of data in input buffer
191 * Use: Snarfs the data from the input buffer and spits it out as
192 * lines. This interface ignores the complexities of dealing
193 * with disablement: you should be using @lbuf_free@ to
194 * contribute data if you want to cope with that.
197 extern void lbuf_snarf(lbuf */
*b*/
, const void */
*p*/
, size_t /*sz*/);
199 /* --- @lbuf_setsize@ --- *
201 * Arguments: @lbuf *b@ = pointer to buffer block
202 * @size_t sz@ = requested maximum line size
206 * Use: Allocates a buffer of the requested size reading lines.
209 extern void lbuf_setsize(lbuf */
*b*/
, size_t /*sz*/);
211 /* --- @lbuf_init@ --- *
213 * Arguments: @lbuf *b@ = pointer to buffer block
214 * @void (*func)(char *s, void *p)@ = handler function
215 * @void *p@ = argument pointer for @func@
219 * Use: Initializes a line buffer block. Any recognized lines are
220 * passed to @func@ for processing.
223 extern void lbuf_init(lbuf */
*b*/
,
224 void (*/
*func*/
)(char */
*s*/
, void */
*p*/
),
227 /* --- @lbuf_destroy@ --- *
229 * Arguments: @lbuf *b@ = pointer to buffer block
233 * Use: Deallocates a line buffer and frees any resources it owned.
236 extern void lbuf_destroy(lbuf */
*b*/
);
238 /*----- That's all, folks -------------------------------------------------*/