chiark / gitweb /
@@@ fltfmt fettling
[mLib] / sel / selbuf.c
1 /* -*-c-*-
2  *
3  * Line-buffering select handler
4  *
5  * (c) 1999 Straylight/Edgeware
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
9  *
10  * This file is part of the mLib utilities library.
11  *
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.
16  *
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.
21  *
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,
25  * MA 02111-1307, USA.
26  */
27
28 /*----- Header files ------------------------------------------------------*/
29
30 #include <errno.h>
31
32 #include <sys/types.h>
33 #include <sys/time.h>
34 #include <unistd.h>
35
36 #include "lbuf.h"
37 #include "sel.h"
38 #include "selbuf.h"
39
40 /*----- Main code ---------------------------------------------------------*/
41
42 /* --- @selbuf_enable@ --- *
43  *
44  * Arguments:   @selbuf *b@ = pointer to buffer block
45  *
46  * Returns:     ---
47  *
48  * Use:         Enables a buffer for reading, and emits any queued lines
49  *              to the buffer's owner.
50  */
51
52 void selbuf_enable(selbuf *b)
53 {
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);
58   }
59 }
60
61 /* --- @selbuf_disable@ --- *
62  *
63  * Arguments:   @selbuf *b@ = pointer to a buffer block
64  *
65  * Returns:     ---
66  *
67  * Use:         Disables a buffer.  It won't be read from until it's
68  *              enabled again.
69  */
70
71 void selbuf_disable(selbuf *b)
72 {
73   if (b->b.f & LBUF_ENABLE) {
74     b->b.f &= ~LBUF_ENABLE;
75     sel_rmfile(&b->reader);
76   }
77 }
78
79 /* --- @selbuf_read@ --- *
80  *
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
84  *
85  * Returns:     ---
86  *
87  * Use:         Acts on the result of a @select@ call.
88  */
89
90 static void selbuf_read(int fd, unsigned mode, void *vp)
91 {
92   selbuf *b = vp;
93   char *p;
94   size_t sz;
95   int n;
96
97   sz = lbuf_free(&b->b, &p);
98   n = read(fd, p, sz);
99   if (n == 0)
100     lbuf_close(&b->b);
101   else if (n > 0)
102     lbuf_flush(&b->b, p, n);
103   else switch (errno) {
104     case EINTR:
105     case EAGAIN:
106 #if EAGAIN != EWOULDBLOCK
107     case EWOULDBLOCK:
108 #endif
109       return;
110     default:
111       lbuf_close(&b->b);
112   }
113 }
114
115 /* --- @selbuf_setsize@ --- *
116  *
117  * Arguments:   @selbuf *b@ = pointer to buffer block
118  *              @size_t sz@ = size of buffer
119  *
120  * Returns:     ---
121  *
122  * Use:         Sets the size of the buffer used for reading lines.
123  */
124
125 void selbuf_setsize(selbuf *b, size_t sz)
126 {
127   lbuf_setsize(&b->b, sz);
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  *              @lbuf_func *func@ = 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, sel_state *s, int fd, lbuf_func *func, void *p)
144 {
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);
148   selbuf_enable(b);
149 }
150
151 /* --- @selbuf_destroy@ --- *
152  *
153  * Arguments:   @selbuf *b@ = pointer to buffer block
154  *
155  * Returns:     ---
156  *
157  * Use:         Deallocates a line buffer and frees any resources it owned.
158  */
159
160 void selbuf_destroy(selbuf *b)
161 {
162   selbuf_disable(b);
163   lbuf_destroy(&b->b);
164 }
165
166 /*----- That's all, folks -------------------------------------------------*/