3 * $Id: selbuf.c,v 1.5 2002/01/13 13:33:15 mdw Exp $
5 * Line-buffering select handler
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 2002/01/13 13:33:15 mdw
34 * Track interface change for @lbuf@.
36 * Revision 1.4 2000/06/17 10:38:14 mdw
37 * Add support for variable buffer sizes.
39 * Revision 1.3 1999/05/22 13:41:00 mdw
40 * Fix end-of-file detection and error handling.
42 * Revision 1.2 1999/05/17 20:36:50 mdw
43 * Make the magical constants for the buffer flags uppercase.
45 * Revision 1.1 1999/05/14 21:01:15 mdw
46 * Integrated `select' handling bits from the background resolver project.
50 /*----- Header files ------------------------------------------------------*/
57 #include <sys/types.h>
65 /*----- Main code ---------------------------------------------------------*/
67 /* --- @selbuf_enable@ --- *
69 * Arguments: @selbuf *b@ = pointer to buffer block
73 * Use: Enables a buffer for reading, and emits any queued lines
74 * to the buffer's owner.
77 void selbuf_enable(selbuf
*b
)
79 if (!(b
->b
.f
& LBUF_ENABLE
)) {
80 b
->b
.f
|= LBUF_ENABLE
;
81 sel_addfile(&b
->reader
);
82 lbuf_flush(&b
->b
, 0, 0);
86 /* --- @selbuf_disable@ --- *
88 * Arguments: @selbuf *b@ = pointer to a buffer block
92 * Use: Disables a buffer. It won't be read from until it's
96 void selbuf_disable(selbuf
*b
)
98 if (b
->b
.f
& LBUF_ENABLE
) {
99 b
->b
.f
&= ~LBUF_ENABLE
;
100 sel_rmfile(&b
->reader
);
104 /* --- @selbuf_read@ --- *
106 * Arguments: @int fd@ = file descriptor to read from
107 * @int mode@ = what we can do to the file
108 * @void *vp@ = pointer to buffer context
112 * Use: Acts on the result of a @select@ call.
115 static void selbuf_read(int fd
, unsigned mode
, void *vp
)
122 sz
= lbuf_free(&b
->b
, &p
);
127 lbuf_flush(&b
->b
, p
, n
);
128 else switch (errno
) {
131 #if EAGAIN != EWOULDBLOCK
140 /* --- @selbuf_setsize@ --- *
142 * Arguments: @selbuf *b@ = pointer to buffer block
143 * @size_t sz@ = size of buffer
147 * Use: Sets the size of the buffer used for reading lines.
150 void selbuf_setsize(selbuf
*b
, size_t sz
)
152 lbuf_setsize(&b
->b
, sz
);
155 /* --- @selbuf_init@ --- *
157 * Arguments: @selbuf *b@ = pointer to buffer block
158 * @sel_state *s@ = pointer to select state to attach to
159 * @int fd@ = file descriptor to listen to
160 * @lbuf_func *func@ = function to call
161 * @void *p@ = argument for function
165 * Use: Initializes a buffer block.
168 void selbuf_init(selbuf
*b
, sel_state
*s
, int fd
, lbuf_func
*func
, void *p
)
170 lbuf_init(&b
->b
, func
, p
);
171 b
->b
.f
&= ~LBUF_ENABLE
;
172 sel_initfile(s
, &b
->reader
, fd
, SEL_READ
, selbuf_read
, b
);
176 /* --- @selbuf_destroy@ --- *
178 * Arguments: @selbuf *b@ = pointer to buffer block
182 * Use: Deallocates a line buffer and frees any resources it owned.
185 void selbuf_destroy(selbuf
*b
)
191 /*----- That's all, folks -------------------------------------------------*/