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