chiark / gitweb /
Integrated `select' handling bits from the background resolver project.
[mLib] / selbuf.c
1 /* -*-c-*-
2  *
3  * $Id: selbuf.c,v 1.1 1999/05/14 21:01:15 mdw Exp $
4  *
5  * Line-buffering select handler
6  *
7  * (c) 1999 Straylight/Edgeware
8  */
9
10 /*----- Licensing notice --------------------------------------------------* 
11  *
12  * This file is part of the mLib utilities library.
13  *
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.
18  * 
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.
23  * 
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,
27  * MA 02111-1307, USA.
28  */
29
30 /*----- Revision history --------------------------------------------------* 
31  *
32  * $Log: selbuf.c,v $
33  * Revision 1.1  1999/05/14 21:01:15  mdw
34  * Integrated `select' handling bits from the background resolver project.
35  *
36  */
37
38 /*----- Header files ------------------------------------------------------*/
39
40 #include <errno.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44
45 #include <sys/types.h>
46 #include <sys/time.h>
47 #include <unistd.h>
48
49 #include "lbuf.h"
50 #include "sel.h"
51 #include "selbuf.h"
52
53 /*----- Main code ---------------------------------------------------------*/
54
55 /* --- @selbuf_enable@ --- *
56  *
57  * Arguments:   @selbuf *b@ = pointer to buffer block
58  *
59  * Returns:     ---
60  *
61  * Use:         Enables a buffer for reading, and emits any queued lines
62  *              to the buffer's owner.
63  */
64
65 void selbuf_enable(selbuf *b)
66 {
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);
71   }
72 }
73
74 /* --- @selbuf_disable@ --- *
75  *
76  * Arguments:   @selbuf *b@ = pointer to a buffer block
77  *
78  * Returns:     ---
79  *
80  * Use:         Disables a buffer.  It won't be read from until it's
81  *              enabled again.
82  */
83
84 void selbuf_disable(selbuf *b)
85 {
86   if (b->b.f & lbuf_enable) {
87     b->b.f &= ~lbuf_enable;
88     sel_rmfile(&b->reader);
89   }
90 }
91
92 /* --- @selbuf_read@ --- *
93  *
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
97  *
98  * Returns:     ---
99  *
100  * Use:         Acts on the result of a @select@ call.
101  */
102
103 static void selbuf_read(int fd, unsigned mode, void *vp)
104 {
105   selbuf *b = vp;
106   char *p;
107   size_t sz;
108   int n;
109
110   sz = lbuf_free(&b->b, &p);
111 again:
112   n = read(fd, p, sz);
113   if (n <= 0) {
114     switch (errno) {
115       case EINTR:
116         goto again;
117       case EAGAIN:
118 #if EAGAIN != EWOULDBLOCK
119       case EWOULDBLOCK:
120 #endif
121         return;
122       default:
123         lbuf_close(&b->b);
124     }
125   }
126   else
127     lbuf_flush(&b->b, p, n);
128 }
129
130 /* --- @selbuf_init@ --- *
131  *
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
137  *
138  * Returns:     ---
139  *
140  * Use:         Initializes a buffer block.
141  */
142
143 void selbuf_init(selbuf *b,
144                  sel_state *s,
145                  int fd,
146                  void (*func)(char */*s*/, void */*p*/),
147                  void *p)
148 {
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);
152   selbuf_enable(b);
153 }
154
155 /*----- That's all, folks -------------------------------------------------*/