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