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