chiark / gitweb /
cleanup: All the whitespace fixes, all at once.
[mLib] / selbuf.c
CommitLineData
97f65b00 1/* -*-c-*-
2 *
8656dc50 3 * $Id: selbuf.c,v 1.6 2004/04/08 01:36:13 mdw Exp $
97f65b00 4 *
5 * Line-buffering select handler
6 *
7 * (c) 1999 Straylight/Edgeware
8 */
9
d4efbcd9 10/*----- Licensing notice --------------------------------------------------*
97f65b00 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.
d4efbcd9 18 *
97f65b00 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.
d4efbcd9 23 *
97f65b00 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
97f65b00 30/*----- Header files ------------------------------------------------------*/
31
32#include <errno.h>
33#include <stdio.h>
34#include <stdlib.h>
35#include <string.h>
36
37#include <sys/types.h>
38#include <sys/time.h>
39#include <unistd.h>
40
41#include "lbuf.h"
42#include "sel.h"
43#include "selbuf.h"
44
45/*----- Main code ---------------------------------------------------------*/
46
47/* --- @selbuf_enable@ --- *
48 *
49 * Arguments: @selbuf *b@ = pointer to buffer block
50 *
51 * Returns: ---
52 *
53 * Use: Enables a buffer for reading, and emits any queued lines
54 * to the buffer's owner.
55 */
56
57void selbuf_enable(selbuf *b)
58{
1ef7279c 59 if (!(b->b.f & LBUF_ENABLE)) {
60 b->b.f |= LBUF_ENABLE;
97f65b00 61 sel_addfile(&b->reader);
62 lbuf_flush(&b->b, 0, 0);
63 }
64}
65
66/* --- @selbuf_disable@ --- *
67 *
68 * Arguments: @selbuf *b@ = pointer to a buffer block
69 *
70 * Returns: ---
71 *
72 * Use: Disables a buffer. It won't be read from until it's
73 * enabled again.
74 */
75
76void selbuf_disable(selbuf *b)
77{
1ef7279c 78 if (b->b.f & LBUF_ENABLE) {
79 b->b.f &= ~LBUF_ENABLE;
97f65b00 80 sel_rmfile(&b->reader);
81 }
82}
83
84/* --- @selbuf_read@ --- *
85 *
86 * Arguments: @int fd@ = file descriptor to read from
87 * @int mode@ = what we can do to the file
88 * @void *vp@ = pointer to buffer context
89 *
90 * Returns: ---
91 *
92 * Use: Acts on the result of a @select@ call.
93 */
94
95static void selbuf_read(int fd, unsigned mode, void *vp)
96{
97 selbuf *b = vp;
98 char *p;
99 size_t sz;
100 int n;
101
102 sz = lbuf_free(&b->b, &p);
97f65b00 103 n = read(fd, p, sz);
693c44ef 104 if (n == 0)
105 lbuf_close(&b->b);
106 else if (n > 0)
107 lbuf_flush(&b->b, p, n);
108 else switch (errno) {
109 case EINTR:
110 case EAGAIN:
97f65b00 111#if EAGAIN != EWOULDBLOCK
693c44ef 112 case EWOULDBLOCK:
97f65b00 113#endif
693c44ef 114 return;
115 default:
116 lbuf_close(&b->b);
97f65b00 117 }
97f65b00 118}
119
e03be5f4 120/* --- @selbuf_setsize@ --- *
121 *
122 * Arguments: @selbuf *b@ = pointer to buffer block
123 * @size_t sz@ = size of buffer
124 *
125 * Returns: ---
126 *
127 * Use: Sets the size of the buffer used for reading lines.
128 */
129
130void selbuf_setsize(selbuf *b, size_t sz)
131{
132 lbuf_setsize(&b->b, sz);
133}
134
97f65b00 135/* --- @selbuf_init@ --- *
136 *
137 * Arguments: @selbuf *b@ = pointer to buffer block
138 * @sel_state *s@ = pointer to select state to attach to
139 * @int fd@ = file descriptor to listen to
b342b114 140 * @lbuf_func *func@ = function to call
97f65b00 141 * @void *p@ = argument for function
142 *
143 * Returns: ---
144 *
145 * Use: Initializes a buffer block.
146 */
147
b342b114 148void selbuf_init(selbuf *b, sel_state *s, int fd, lbuf_func *func, void *p)
97f65b00 149{
150 lbuf_init(&b->b, func, p);
1ef7279c 151 b->b.f &= ~LBUF_ENABLE;
97f65b00 152 sel_initfile(s, &b->reader, fd, SEL_READ, selbuf_read, b);
153 selbuf_enable(b);
154}
155
e03be5f4 156/* --- @selbuf_destroy@ --- *
157 *
158 * Arguments: @selbuf *b@ = pointer to buffer block
159 *
160 * Returns: ---
161 *
162 * Use: Deallocates a line buffer and frees any resources it owned.
163 */
164
165void selbuf_destroy(selbuf *b)
166{
167 selbuf_disable(b);
168 lbuf_destroy(&b->b);
169}
170
97f65b00 171/*----- That's all, folks -------------------------------------------------*/