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