]> code.delx.au - pulseaudio/blob - src/polypcore/ioline.c
Move xmalloc to the public side (libpolyp).
[pulseaudio] / src / polypcore / ioline.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
10
11 polypaudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <errno.h>
27 #include <stdio.h>
28 #include <assert.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include <polyp/xmalloc.h>
33
34 #include <polypcore/log.h>
35
36 #include "ioline.h"
37
38 #define BUFFER_LIMIT (64*1024)
39 #define READ_SIZE (1024)
40
41 struct pa_ioline {
42 pa_iochannel *io;
43 pa_defer_event *defer_event;
44 pa_mainloop_api *mainloop;
45 int ref;
46 int dead;
47
48 char *wbuf;
49 size_t wbuf_length, wbuf_index, wbuf_valid_length;
50
51 char *rbuf;
52 size_t rbuf_length, rbuf_index, rbuf_valid_length;
53
54 void (*callback)(pa_ioline*io, const char *s, void *userdata);
55 void *userdata;
56
57 int defer_close;
58 };
59
60 static void io_callback(pa_iochannel*io, void *userdata);
61 static void defer_callback(pa_mainloop_api*m, pa_defer_event*e, void *userdata);
62
63 pa_ioline* pa_ioline_new(pa_iochannel *io) {
64 pa_ioline *l;
65 assert(io);
66
67 l = pa_xmalloc(sizeof(pa_ioline));
68 l->io = io;
69 l->dead = 0;
70
71 l->wbuf = NULL;
72 l->wbuf_length = l->wbuf_index = l->wbuf_valid_length = 0;
73
74 l->rbuf = NULL;
75 l->rbuf_length = l->rbuf_index = l->rbuf_valid_length = 0;
76
77 l->callback = NULL;
78 l->userdata = NULL;
79 l->ref = 1;
80
81 l->mainloop = pa_iochannel_get_mainloop_api(io);
82
83 l->defer_event = l->mainloop->defer_new(l->mainloop, defer_callback, l);
84 l->mainloop->defer_enable(l->defer_event, 0);
85
86 l->defer_close = 0;
87
88 pa_iochannel_set_callback(io, io_callback, l);
89
90 return l;
91 }
92
93 static void ioline_free(pa_ioline *l) {
94 assert(l);
95
96 if (l->io)
97 pa_iochannel_free(l->io);
98
99 if (l->defer_event)
100 l->mainloop->defer_free(l->defer_event);
101
102 pa_xfree(l->wbuf);
103 pa_xfree(l->rbuf);
104 pa_xfree(l);
105 }
106
107 void pa_ioline_unref(pa_ioline *l) {
108 assert(l && l->ref >= 1);
109
110 if ((--l->ref) <= 0)
111 ioline_free(l);
112 }
113
114 pa_ioline* pa_ioline_ref(pa_ioline *l) {
115 assert(l && l->ref >= 1);
116
117 l->ref++;
118 return l;
119 }
120
121 void pa_ioline_close(pa_ioline *l) {
122 assert(l && l->ref >= 1);
123
124 l->dead = 1;
125 if (l->io) {
126 pa_iochannel_free(l->io);
127 l->io = NULL;
128 }
129
130 if (l->defer_event) {
131 l->mainloop->defer_free(l->defer_event);
132 l->defer_event = NULL;
133 }
134 }
135
136 void pa_ioline_puts(pa_ioline *l, const char *c) {
137 size_t len;
138 assert(l && c && l->ref >= 1 && !l->dead);
139
140 pa_ioline_ref(l);
141
142 len = strlen(c);
143 if (len > BUFFER_LIMIT - l->wbuf_valid_length)
144 len = BUFFER_LIMIT - l->wbuf_valid_length;
145
146 if (len) {
147 assert(l->wbuf_length >= l->wbuf_valid_length);
148
149 /* In case the allocated buffer is too small, enlarge it. */
150 if (l->wbuf_valid_length + len > l->wbuf_length) {
151 size_t n = l->wbuf_valid_length+len;
152 char *new = pa_xmalloc(n);
153 if (l->wbuf) {
154 memcpy(new, l->wbuf+l->wbuf_index, l->wbuf_valid_length);
155 pa_xfree(l->wbuf);
156 }
157 l->wbuf = new;
158 l->wbuf_length = n;
159 l->wbuf_index = 0;
160 } else if (l->wbuf_index + l->wbuf_valid_length + len > l->wbuf_length) {
161
162 /* In case the allocated buffer fits, but the current index is too far from the start, move it to the front. */
163 memmove(l->wbuf, l->wbuf+l->wbuf_index, l->wbuf_valid_length);
164 l->wbuf_index = 0;
165 }
166
167 assert(l->wbuf_index + l->wbuf_valid_length + len <= l->wbuf_length);
168
169 /* Append the new string */
170 memcpy(l->wbuf + l->wbuf_index + l->wbuf_valid_length, c, len);
171 l->wbuf_valid_length += len;
172
173 l->mainloop->defer_enable(l->defer_event, 1);
174 }
175
176 pa_ioline_unref(l);
177 }
178
179 void pa_ioline_set_callback(pa_ioline*l, void (*callback)(pa_ioline*io, const char *s, void *userdata), void *userdata) {
180 assert(l && l->ref >= 1);
181 l->callback = callback;
182 l->userdata = userdata;
183 }
184
185 static void failure(pa_ioline *l) {
186 assert(l && l->ref >= 1 && !l->dead);
187
188 pa_ioline_close(l);
189
190 if (l->callback) {
191 l->callback(l, NULL, l->userdata);
192 l->callback = NULL;
193 }
194 }
195
196 static void scan_for_lines(pa_ioline *l, size_t skip) {
197 assert(l && l->ref >= 1 && skip < l->rbuf_valid_length);
198
199 while (!l->dead && l->rbuf_valid_length > skip) {
200 char *e, *p;
201 size_t m;
202
203 if (!(e = memchr(l->rbuf + l->rbuf_index + skip, '\n', l->rbuf_valid_length - skip)))
204 break;
205
206 *e = 0;
207
208 p = l->rbuf + l->rbuf_index;
209 m = strlen(p);
210
211 l->rbuf_index += m+1;
212 l->rbuf_valid_length -= m+1;
213
214 /* A shortcut for the next time */
215 if (l->rbuf_valid_length == 0)
216 l->rbuf_index = 0;
217
218 if (l->callback)
219 l->callback(l, p, l->userdata);
220
221 skip = 0;
222 }
223
224 /* If the buffer became too large and still no newline was found, drop it. */
225 if (l->rbuf_valid_length >= BUFFER_LIMIT)
226 l->rbuf_index = l->rbuf_valid_length = 0;
227 }
228
229 static int do_write(pa_ioline *l);
230
231 static int do_read(pa_ioline *l) {
232 assert(l && l->ref >= 1);
233
234 while (!l->dead && pa_iochannel_is_readable(l->io)) {
235 ssize_t r;
236 size_t len;
237
238 len = l->rbuf_length - l->rbuf_index - l->rbuf_valid_length;
239
240 /* Check if we have to enlarge the read buffer */
241 if (len < READ_SIZE) {
242 size_t n = l->rbuf_valid_length+READ_SIZE;
243
244 if (n >= BUFFER_LIMIT)
245 n = BUFFER_LIMIT;
246
247 if (l->rbuf_length >= n) {
248 /* The current buffer is large enough, let's just move the data to the front */
249 if (l->rbuf_valid_length)
250 memmove(l->rbuf, l->rbuf+l->rbuf_index, l->rbuf_valid_length);
251 } else {
252 /* Enlarge the buffer */
253 char *new = pa_xmalloc(n);
254 if (l->rbuf_valid_length)
255 memcpy(new, l->rbuf+l->rbuf_index, l->rbuf_valid_length);
256 pa_xfree(l->rbuf);
257 l->rbuf = new;
258 l->rbuf_length = n;
259 }
260
261 l->rbuf_index = 0;
262 }
263
264 len = l->rbuf_length - l->rbuf_index - l->rbuf_valid_length;
265
266 assert(len >= READ_SIZE);
267
268 /* Read some data */
269 r = pa_iochannel_read(l->io, l->rbuf+l->rbuf_index+l->rbuf_valid_length, len);
270 if (r == 0) {
271 /* Got an EOF, so fake an exit command. */
272 l->rbuf_index = 0;
273 snprintf (l->rbuf, l->rbuf_length, "exit\n");
274 r = 5;
275 pa_ioline_puts(l, "\nExiting.\n");
276 do_write(l);
277 } else if (r < 0) {
278 pa_log(__FILE__": read() failed: %s", strerror(errno));
279 failure(l);
280 return -1;
281 }
282
283 l->rbuf_valid_length += r;
284
285 /* Look if a line has been terminated in the newly read data */
286 scan_for_lines(l, l->rbuf_valid_length - r);
287 }
288
289 return 0;
290 }
291
292 /* Try to flush the buffer */
293 static int do_write(pa_ioline *l) {
294 ssize_t r;
295 assert(l && l->ref >= 1);
296
297 while (!l->dead && pa_iochannel_is_writable(l->io) && l->wbuf_valid_length) {
298
299 if ((r = pa_iochannel_write(l->io, l->wbuf+l->wbuf_index, l->wbuf_valid_length)) < 0) {
300 pa_log(__FILE__": write() failed: %s", r < 0 ? strerror(errno) : "EOF");
301 failure(l);
302 return -1;
303 }
304
305 l->wbuf_index += r;
306 l->wbuf_valid_length -= r;
307
308 /* A shortcut for the next time */
309 if (l->wbuf_valid_length == 0)
310 l->wbuf_index = 0;
311 }
312
313 return 0;
314 }
315
316 /* Try to flush read/write data */
317 static void do_work(pa_ioline *l) {
318 assert(l && l->ref >= 1);
319
320 pa_ioline_ref(l);
321
322 l->mainloop->defer_enable(l->defer_event, 0);
323
324 if (!l->dead)
325 do_write(l);
326
327 if (!l->dead)
328 do_read(l);
329
330 if (l->defer_close && !l->wbuf_valid_length)
331 failure(l);
332
333 pa_ioline_unref(l);
334 }
335
336 static void io_callback(pa_iochannel*io, void *userdata) {
337 pa_ioline *l = userdata;
338 assert(io && l && l->ref >= 1);
339
340 do_work(l);
341 }
342
343 static void defer_callback(pa_mainloop_api*m, pa_defer_event*e, void *userdata) {
344 pa_ioline *l = userdata;
345 assert(l && l->ref >= 1 && l->mainloop == m && l->defer_event == e);
346
347 do_work(l);
348 }
349
350 void pa_ioline_defer_close(pa_ioline *l) {
351 assert(l);
352
353 l->defer_close = 1;
354
355 if (!l->wbuf_valid_length)
356 l->mainloop->defer_enable(l->defer_event, 1);
357 }
358
359 void pa_ioline_printf(pa_ioline *s, const char *format, ...) {
360 char *t;
361 va_list ap;
362
363
364 va_start(ap, format);
365 t = pa_vsprintf_malloc(format, ap);
366 va_end(ap);
367
368 pa_ioline_puts(s, t);
369 pa_xfree(t);
370 }