3 * $Id: selbuf.c,v 1.6 2004/04/08 01:36:13 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 /*----- Header files ------------------------------------------------------*/
37 #include <sys/types.h>
45 /*----- Main code ---------------------------------------------------------*/
47 /* --- @selbuf_enable@ --- *
49 * Arguments: @selbuf *b@ = pointer to buffer block
53 * Use: Enables a buffer for reading, and emits any queued lines
54 * to the buffer's owner.
57 void selbuf_enable(selbuf *b)
59 if (!(b->b.f & LBUF_ENABLE)) {
60 b->b.f |= LBUF_ENABLE;
61 sel_addfile(&b->reader);
62 lbuf_flush(&b->b, 0, 0);
66 /* --- @selbuf_disable@ --- *
68 * Arguments: @selbuf *b@ = pointer to a buffer block
72 * Use: Disables a buffer. It won't be read from until it's
76 void selbuf_disable(selbuf *b)
78 if (b->b.f & LBUF_ENABLE) {
79 b->b.f &= ~LBUF_ENABLE;
80 sel_rmfile(&b->reader);
84 /* --- @selbuf_read@ --- *
86 * Arguments: @int fd@ = file descriptor to read from
87 * @int mode@ = what we can do to the file
88 * @void *vp@ = pointer to buffer context
92 * Use: Acts on the result of a @select@ call.
95 static void selbuf_read(int fd, unsigned mode, void *vp)
102 sz = lbuf_free(&b->b, &p);
107 lbuf_flush(&b->b, p, n);
108 else switch (errno) {
111 #if EAGAIN != EWOULDBLOCK
120 /* --- @selbuf_setsize@ --- *
122 * Arguments: @selbuf *b@ = pointer to buffer block
123 * @size_t sz@ = size of buffer
127 * Use: Sets the size of the buffer used for reading lines.
130 void selbuf_setsize(selbuf *b, size_t sz)
132 lbuf_setsize(&b->b, sz);
135 /* --- @selbuf_init@ --- *
137 * Arguments: @selbuf *b@ = pointer to buffer block
138 * @sel_state *s@ = pointer to select state to attach to
139 * @int fd@ = file descriptor to listen to
140 * @lbuf_func *func@ = function to call
141 * @void *p@ = argument for function
145 * Use: Initializes a buffer block.
148 void selbuf_init(selbuf *b, sel_state *s, int fd, lbuf_func *func, void *p)
150 lbuf_init(&b->b, func, p);
151 b->b.f &= ~LBUF_ENABLE;
152 sel_initfile(s, &b->reader, fd, SEL_READ, selbuf_read, b);
156 /* --- @selbuf_destroy@ --- *
158 * Arguments: @selbuf *b@ = pointer to buffer block
162 * Use: Deallocates a line buffer and frees any resources it owned.
165 void selbuf_destroy(selbuf *b)
171 /*----- That's all, folks -------------------------------------------------*/