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