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 ------------------------------------------------------*/
32 #include <sys/types.h>
40 /*----- Main code ---------------------------------------------------------*/
42 /* --- @selbuf_enable@ --- *
44 * Arguments: @selbuf *b@ = pointer to buffer block
48 * Use: Enables a buffer for reading, and emits any queued lines
49 * to the buffer's owner.
52 void selbuf_enable(selbuf *b)
54 if (!(b->b.f & LBUF_ENABLE)) {
55 b->b.f |= LBUF_ENABLE;
56 sel_addfile(&b->reader);
57 lbuf_flush(&b->b, 0, 0);
61 /* --- @selbuf_disable@ --- *
63 * Arguments: @selbuf *b@ = pointer to a buffer block
67 * Use: Disables a buffer. It won't be read from until it's
71 void selbuf_disable(selbuf *b)
73 if (b->b.f & LBUF_ENABLE) {
74 b->b.f &= ~LBUF_ENABLE;
75 sel_rmfile(&b->reader);
79 /* --- @selbuf_read@ --- *
81 * Arguments: @int fd@ = file descriptor to read from
82 * @int mode@ = what we can do to the file
83 * @void *vp@ = pointer to buffer context
87 * Use: Acts on the result of a @select@ call.
90 static void selbuf_read(int fd, unsigned mode, void *vp)
97 sz = lbuf_free(&b->b, &p);
102 lbuf_flush(&b->b, p, n);
103 else switch (errno) {
106 #if EAGAIN != EWOULDBLOCK
115 /* --- @selbuf_setsize@ --- *
117 * Arguments: @selbuf *b@ = pointer to buffer block
118 * @size_t sz@ = size of buffer
122 * Use: Sets the size of the buffer used for reading lines.
125 void selbuf_setsize(selbuf *b, size_t sz)
127 lbuf_setsize(&b->b, sz);
130 /* --- @selbuf_init@ --- *
132 * Arguments: @selbuf *b@ = pointer to buffer block
133 * @sel_state *s@ = pointer to select state to attach to
134 * @int fd@ = file descriptor to listen to
135 * @lbuf_func *func@ = function to call
136 * @void *p@ = argument for function
140 * Use: Initializes a buffer block.
143 void selbuf_init(selbuf *b, sel_state *s, int fd, lbuf_func *func, void *p)
145 lbuf_init(&b->b, func, p);
146 b->b.f &= ~LBUF_ENABLE;
147 sel_initfile(s, &b->reader, fd, SEL_READ, selbuf_read, b);
151 /* --- @selbuf_destroy@ --- *
153 * Arguments: @selbuf *b@ = pointer to buffer block
157 * Use: Deallocates a line buffer and frees any resources it owned.
160 void selbuf_destroy(selbuf *b)
166 /*----- That's all, folks -------------------------------------------------*/