3 * $Id: selbuf.c,v 1.1 1999/05/14 21:01: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.1 1999/05/14 21:01:15 mdw
34 * Integrated `select' handling bits from the background resolver project.
38 /*----- Header files ------------------------------------------------------*/
45 #include <sys/types.h>
53 /*----- Main code ---------------------------------------------------------*/
55 /* --- @selbuf_enable@ --- *
57 * Arguments: @selbuf *b@ = pointer to buffer block
61 * Use: Enables a buffer for reading, and emits any queued lines
62 * to the buffer's owner.
65 void selbuf_enable(selbuf *b)
67 if (!(b->b.f & lbuf_enable)) {
68 b->b.f |= lbuf_enable;
69 sel_addfile(&b->reader);
70 lbuf_flush(&b->b, 0, 0);
74 /* --- @selbuf_disable@ --- *
76 * Arguments: @selbuf *b@ = pointer to a buffer block
80 * Use: Disables a buffer. It won't be read from until it's
84 void selbuf_disable(selbuf *b)
86 if (b->b.f & lbuf_enable) {
87 b->b.f &= ~lbuf_enable;
88 sel_rmfile(&b->reader);
92 /* --- @selbuf_read@ --- *
94 * Arguments: @int fd@ = file descriptor to read from
95 * @int mode@ = what we can do to the file
96 * @void *vp@ = pointer to buffer context
100 * Use: Acts on the result of a @select@ call.
103 static void selbuf_read(int fd, unsigned mode, void *vp)
110 sz = lbuf_free(&b->b, &p);
118 #if EAGAIN != EWOULDBLOCK
127 lbuf_flush(&b->b, p, n);
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 * @void (*func)(char *s, void *p)@ = function to call
136 * @void *p@ = argument for function
140 * Use: Initializes a buffer block.
143 void selbuf_init(selbuf *b,
146 void (*func)(char */*s*/, void */*p*/),
149 lbuf_init(&b->b, func, p);
150 b->b.f &= ~lbuf_enable;
151 sel_initfile(s, &b->reader, fd, SEL_READ, selbuf_read, b);
155 /*----- That's all, folks -------------------------------------------------*/