3 * Line-buffering select handler
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the mLib utilities library.
12 * mLib is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * mLib is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with mLib; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
35 #include <sys/types.h>
43 /*----- Main code ---------------------------------------------------------*/
45 /* --- @selbuf_enable@ --- *
47 * Arguments: @selbuf *b@ = pointer to buffer block
51 * Use: Enables a buffer for reading, and emits any queued lines
52 * to the buffer's owner.
55 void selbuf_enable(selbuf *b)
57 if (!(b->b.f & LBUF_ENABLE)) {
58 b->b.f |= LBUF_ENABLE;
59 sel_addfile(&b->reader);
60 lbuf_flush(&b->b, 0, 0);
64 /* --- @selbuf_disable@ --- *
66 * Arguments: @selbuf *b@ = pointer to a buffer block
70 * Use: Disables a buffer. It won't be read from until it's
74 void selbuf_disable(selbuf *b)
76 if (b->b.f & LBUF_ENABLE) {
77 b->b.f &= ~LBUF_ENABLE;
78 sel_rmfile(&b->reader);
82 /* --- @selbuf_read@ --- *
84 * Arguments: @int fd@ = file descriptor to read from
85 * @int mode@ = what we can do to the file
86 * @void *vp@ = pointer to buffer context
90 * Use: Acts on the result of a @select@ call.
93 static void selbuf_read(int fd, unsigned mode, void *vp)
100 sz = lbuf_free(&b->b, &p);
105 lbuf_flush(&b->b, p, n);
106 else switch (errno) {
109 #if EAGAIN != EWOULDBLOCK
118 /* --- @selbuf_setsize@ --- *
120 * Arguments: @selbuf *b@ = pointer to buffer block
121 * @size_t sz@ = size of buffer
125 * Use: Sets the size of the buffer used for reading lines.
128 void selbuf_setsize(selbuf *b, size_t sz)
130 lbuf_setsize(&b->b, sz);
133 /* --- @selbuf_init@ --- *
135 * Arguments: @selbuf *b@ = pointer to buffer block
136 * @sel_state *s@ = pointer to select state to attach to
137 * @int fd@ = file descriptor to listen to
138 * @lbuf_func *func@ = function to call
139 * @void *p@ = argument for function
143 * Use: Initializes a buffer block.
146 void selbuf_init(selbuf *b, sel_state *s, int fd, lbuf_func *func, void *p)
148 lbuf_init(&b->b, func, p);
149 b->b.f &= ~LBUF_ENABLE;
150 sel_initfile(s, &b->reader, fd, SEL_READ, selbuf_read, b);
154 /* --- @selbuf_destroy@ --- *
156 * Arguments: @selbuf *b@ = pointer to buffer block
160 * Use: Deallocates a line buffer and frees any resources it owned.
163 void selbuf_destroy(selbuf *b)
169 /*----- That's all, folks -------------------------------------------------*/