chiark / gitweb /
Track interface change for @lbuf@.
[mLib] / lbuf.h
1 /* -*-c-*-
2  *
3  * $Id: lbuf.h,v 1.7 2002/01/13 13:32:52 mdw Exp $
4  *
5  * Block-to-line buffering
6  *
7  * (c) 1999 Straylight/Edgeware
8  */
9
10 /*----- Licensing notice --------------------------------------------------* 
11  *
12  * This file is part of the mLib utilities library.
13  *
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.
18  * 
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.
23  * 
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,
27  * MA 02111-1307, USA.
28  */
29
30 /*----- Revision history --------------------------------------------------* 
31  *
32  * $Log: lbuf.h,v $
33  * Revision 1.7  2002/01/13 13:32:52  mdw
34  * Pass line length to line handler function.  Provide a @typedef@ for
35  * handler functions.  Allow run-time configuration of line delimiters.
36  *
37  * Revision 1.6  2001/02/03 16:23:33  mdw
38  * Bug fix: handle a disable during a close-induced flush without dumping
39  * core.
40  *
41  * Revision 1.5  2001/01/20 12:06:01  mdw
42  * Define flags with macros, to ensure unsignedness.
43  *
44  * Revision 1.4  2000/06/17 10:38:14  mdw
45  * Add support for variable buffer sizes.
46  *
47  * Revision 1.3  1999/12/10 23:42:04  mdw
48  * Change header file guard names.
49  *
50  * Revision 1.2  1999/05/17 20:36:08  mdw
51  * Make the magical constants for the buffer flags uppercase.
52  *
53  * Revision 1.1  1999/05/14 21:01:14  mdw
54  * Integrated `select' handling bits from the background resolver project.
55  *
56  */
57
58 #ifndef MLIB_LBUF_H
59 #define MLIB_LBUF_H
60
61 #ifdef __cplusplus
62   extern "C" {
63 #endif
64
65 /*----- Line buffering ----------------------------------------------------*
66  *
67  * The line buffer accepts as input arbitrary-sized lumps of data and
68  * converts them, by passing them to a client-supplied function, into a
69  * sequence of lines.  It's particularly useful when performing multiplexed
70  * network I/O.  It's not normally acceptable to block while waiting for the
71  * rest of a text line to arrive, for example.  The line buffer stores the
72  * start of the line until the rest of it arrives later.
73  *
74  * A line is a piece of text terminated by either a linefeed or a carriage-
75  * return/linefeed pair.  (The former is there to cope with Unix; the latter
76  * copes with Internet-format line ends.)
77  *
78  * There's a limit to the size of lines that the buffer can cope with.  It's
79  * not hard to remove this limit, but it's probably a bad idea in a lot of
80  * cases, because it'd allow a remote user to gobble arbitrary amounts of
81  * your memory.  If a line exceeds the limit, it is truncated: the initial
82  * portion of the line is processed normally, and the remaining portion is
83  * simply discarded.
84  *
85  * Lines extracted from the input data are passed, one at a time, to a
86  * `handler function', along with a caller-supplied pointer argument to
87  * provide the handler with some context.  The line read is null-terminated
88  * and does not include the trailing newline characters.  It is legal for a
89  * handler function to modify the string it is passed.  However, writing
90  * beyond the terminating null byte is not allowed.  An end-of-file condition
91  * is signalled to the handler by passing it a null pointer rather than the
92  * address of a string.
93  *
94  * A complexity arises because of the concept of a `disabled' buffer.
95  * Disablement is really a higher-level concept, but it turns out to be
96  * important to implement it here.  It's useful for a line handler function
97  * to `disable' itself, so that it doesn't get called any more.  For example,
98  * this might happen if it encouters an error, or when it finishes reading
99  * everything it wanted to read.  The line buffer needs to be `in the loop'
100  * so that it stops attempting to flush any further lines stored in its
101  * buffer towards a handler function which isn't ready to accept them.
102  * Buffers are initially enabled, although higher- level buffering systems
103  * might well disable them immediately for their own purposes.
104  */
105
106 /*----- Header files ------------------------------------------------------*/
107
108 #include <stddef.h>
109
110 #ifndef MLIB_ARENA_H
111 #  include "arena.h"
112 #endif
113
114 /*----- Data structures ---------------------------------------------------*/
115
116 /* --- The buffer structure --- *
117  *
118  * The only thing that's safe to fiddle with in here is the @lbuf_enable@
119  * flag.  Only higher-level buffering systems should be playing with even
120  * that.
121  */
122
123 struct lbuf;
124
125 typedef void lbuf_func(char */*s*/, size_t /*len*/, void */*p*/);
126
127 typedef struct lbuf {
128   lbuf_func *func;                      /* Handler function */
129   void *p;                              /* Argument for handler */
130   size_t len;                           /* Length of data in buffer */
131   size_t sz;                            /* Buffer size */
132   unsigned delim;                       /* Delimiter to look for */
133   unsigned f;                           /* Various useful state flags */
134   arena *a;                             /* Memory allocation arena */
135   char *buf;                            /* The actual buffer */
136 } lbuf;
137
138 #define LBUF_CR 1u                      /* Read a carriage return */
139 #define LBUF_ENABLE 2u                  /* Buffer is currently enabled */
140 #define LBUF_CLOSE 4u                   /* Buffer is now closed */
141
142 enum {
143   LBUF_CRLF = 256,
144   LBUF_STRICTCRLF = 257
145 };
146
147 /*----- Functions provided ------------------------------------------------*/
148
149 /* --- @lbuf_flush@ --- *
150  *
151  * Arguments:   @lbuf *b@ = pointer to buffer block
152  *              @char *p@ = pointer to where to start searching
153  *              @size_t len@ = length of new material added
154  *
155  * Returns:     ---
156  *
157  * Use:         Flushes any complete lines in a line buffer.  New material
158  *              is assumed to have been added starting at @p@.  If @p@ is
159  *              null, then the scan starts at the beginning of the buffer,
160  *              and the size of data already in the buffer is used in place
161  *              of @len@.
162  *
163  *              It is assumed that the buffer is initially enabled.  You
164  *              shouldn't be contributing data to a disabled buffer anyway.
165  *              However, the buffer handler may at some point disable itself,
166  *              and @lbuf_flush@ can cope with this eventuality.  Any pending
167  *              data is left at the start of the buffer and can be flushed
168  *              out by calling @lbuf_flush(b, 0, 0)@ if the buffer is ever
169  *              re-enabled.
170  */
171
172 extern void lbuf_flush(lbuf */*b*/, char */*p*/, size_t /*len*/);
173
174 /* --- @lbuf_close@ --- *
175  *
176  * Arguments:   @lbuf *b@ = pointer to buffer block
177  *
178  * Returns:     ---
179  *
180  * Use:         Empties the buffer of any data currently lurking in it, and
181  *              informs the client that this has happened.  It's assumed that
182  *              the buffer is enabled: you shouldn't be reading close events
183  *              on disabled buffers.
184  */
185
186 extern void lbuf_close(lbuf */*b*/);
187
188 /* --- @lbuf_free@ --- *
189  *
190  * Arguments:   @lbuf *b@ = pointer to buffer block
191  *              @char **p@ = output pointer to free space
192  *
193  * Returns:     Free buffer size.
194  *
195  * Use:         Returns the free portion of a line buffer.  Data can then be
196  *              written to this portion, and split out into lines by calling
197  *              @lbuf_flush@.
198  */
199
200 extern size_t lbuf_free(lbuf */*b*/, char **/*p*/);
201
202 /* --- @lbuf_snarf@ --- *
203  *
204  * Arguments:   @lbuf *b@ = pointer to buffer block
205  *              @const void *p@ = pointer to input data buffer
206  *              @size_t sz@ = size of data in input buffer
207  *
208  * Returns:     ---
209  *
210  * Use:         Snarfs the data from the input buffer and spits it out as
211  *              lines.  This interface ignores the complexities of dealing
212  *              with disablement: you should be using @lbuf_free@ to
213  *              contribute data if you want to cope with that.
214  */
215
216 extern void lbuf_snarf(lbuf */*b*/, const void */*p*/, size_t /*sz*/);
217
218 /* --- @lbuf_setsize@ --- *
219  *
220  * Arguments:   @lbuf *b@ = pointer to buffer block
221  *              @size_t sz@ = requested maximum line size
222  *
223  * Returns:     ---
224  *
225  * Use:         Allocates a buffer of the requested size reading lines.
226  */
227
228 extern void lbuf_setsize(lbuf */*b*/, size_t /*sz*/);
229
230 /* --- @lbuf_init@ --- *
231  *
232  * Arguments:   @lbuf *b@ = pointer to buffer block
233  *              @lbuf_func *func@ = handler function
234  *              @void *p@ = argument pointer for @func@
235  *
236  * Returns:     ---
237  *
238  * Use:         Initializes a line buffer block.  Any recognized lines are
239  *              passed to @func@ for processing.
240  */
241
242 extern void lbuf_init(lbuf */*b*/, lbuf_func */*func*/, void */*p*/);
243
244 /* --- @lbuf_destroy@ --- *
245  *
246  * Arguments:   @lbuf *b@ = pointer to buffer block
247  *
248  * Returns:     ---
249  *
250  * Use:         Deallocates a line buffer and frees any resources it owned.
251  */
252
253 extern void lbuf_destroy(lbuf */*b*/);
254
255 /*----- That's all, folks -------------------------------------------------*/
256
257 #ifdef __cplusplus
258   }
259 #endif
260
261 #endif