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