chiark / gitweb /
Hack if there is no @_SC_OPEN_MAX@ or @OPEN_MAX@.
[mLib] / selbuf.c
1 /* -*-c-*-
2  *
3  * $Id: selbuf.c,v 1.4 2000/06/17 10:38:14 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.4  2000/06/17 10:38:14  mdw
34  * Add support for variable buffer sizes.
35  *
36  * Revision 1.3  1999/05/22 13:41:00  mdw
37  * Fix end-of-file detection and error handling.
38  *
39  * Revision 1.2  1999/05/17 20:36:50  mdw
40  * Make the magical constants for the buffer flags uppercase.
41  *
42  * Revision 1.1  1999/05/14 21:01:15  mdw
43  * Integrated `select' handling bits from the background resolver project.
44  *
45  */
46
47 /*----- Header files ------------------------------------------------------*/
48
49 #include <errno.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53
54 #include <sys/types.h>
55 #include <sys/time.h>
56 #include <unistd.h>
57
58 #include "lbuf.h"
59 #include "sel.h"
60 #include "selbuf.h"
61
62 /*----- Main code ---------------------------------------------------------*/
63
64 /* --- @selbuf_enable@ --- *
65  *
66  * Arguments:   @selbuf *b@ = pointer to buffer block
67  *
68  * Returns:     ---
69  *
70  * Use:         Enables a buffer for reading, and emits any queued lines
71  *              to the buffer's owner.
72  */
73
74 void selbuf_enable(selbuf *b)
75 {
76   if (!(b->b.f & LBUF_ENABLE)) {
77     b->b.f |= LBUF_ENABLE;
78     sel_addfile(&b->reader);
79     lbuf_flush(&b->b, 0, 0);
80   }
81 }
82
83 /* --- @selbuf_disable@ --- *
84  *
85  * Arguments:   @selbuf *b@ = pointer to a buffer block
86  *
87  * Returns:     ---
88  *
89  * Use:         Disables a buffer.  It won't be read from until it's
90  *              enabled again.
91  */
92
93 void selbuf_disable(selbuf *b)
94 {
95   if (b->b.f & LBUF_ENABLE) {
96     b->b.f &= ~LBUF_ENABLE;
97     sel_rmfile(&b->reader);
98   }
99 }
100
101 /* --- @selbuf_read@ --- *
102  *
103  * Arguments:   @int fd@ = file descriptor to read from
104  *              @int mode@ = what we can do to the file
105  *              @void *vp@ = pointer to buffer context
106  *
107  * Returns:     ---
108  *
109  * Use:         Acts on the result of a @select@ call.
110  */
111
112 static void selbuf_read(int fd, unsigned mode, void *vp)
113 {
114   selbuf *b = vp;
115   char *p;
116   size_t sz;
117   int n;
118
119   sz = lbuf_free(&b->b, &p);
120   n = read(fd, p, sz);
121   if (n == 0)
122     lbuf_close(&b->b);
123   else if (n > 0)
124     lbuf_flush(&b->b, p, n);
125   else switch (errno) {
126     case EINTR:
127     case EAGAIN:
128 #if EAGAIN != EWOULDBLOCK
129     case EWOULDBLOCK:
130 #endif
131       return;
132     default:
133       lbuf_close(&b->b);
134   }
135 }
136
137 /* --- @selbuf_setsize@ --- *
138  *
139  * Arguments:   @selbuf *b@ = pointer to buffer block
140  *              @size_t sz@ = size of buffer
141  *
142  * Returns:     ---
143  *
144  * Use:         Sets the size of the buffer used for reading lines.
145  */
146
147 void selbuf_setsize(selbuf *b, size_t sz)
148 {
149   lbuf_setsize(&b->b, sz);
150 }
151
152 /* --- @selbuf_init@ --- *
153  *
154  * Arguments:   @selbuf *b@ = pointer to buffer block
155  *              @sel_state *s@ = pointer to select state to attach to
156  *              @int fd@ = file descriptor to listen to
157  *              @void (*func)(char *s, void *p)@ = function to call
158  *              @void *p@ = argument for function
159  *
160  * Returns:     ---
161  *
162  * Use:         Initializes a buffer block.
163  */
164
165 void selbuf_init(selbuf *b,
166                  sel_state *s,
167                  int fd,
168                  void (*func)(char */*s*/, void */*p*/),
169                  void *p)
170 {
171   lbuf_init(&b->b, func, p);
172   b->b.f &= ~LBUF_ENABLE;
173   sel_initfile(s, &b->reader, fd, SEL_READ, selbuf_read, b);
174   selbuf_enable(b);
175 }
176
177 /* --- @selbuf_destroy@ --- *
178  *
179  * Arguments:   @selbuf *b@ = pointer to buffer block
180  *
181  * Returns:     ---
182  *
183  * Use:         Deallocates a line buffer and frees any resources it owned.
184  */
185
186 void selbuf_destroy(selbuf *b)
187 {
188   selbuf_disable(b);
189   lbuf_destroy(&b->b);
190 }
191
192 /*----- That's all, folks -------------------------------------------------*/