basic cli interface
[profile/ivi/pulseaudio.git] / src / ioline.c
1 #include <errno.h>
2 #include <stdio.h>
3 #include <assert.h>
4 #include <stdlib.h>
5 #include <string.h>
6
7 #include "ioline.h"
8
9 #define BUFFER_LIMIT (64*1024)
10 #define READ_SIZE (1024)
11
12 struct ioline {
13     struct iochannel *io;
14     int dead;
15
16     char *wbuf;
17     size_t wbuf_length, wbuf_index, wbuf_valid_length;
18
19     char *rbuf;
20     size_t rbuf_length, rbuf_index, rbuf_valid_length;
21
22     void (*callback)(struct ioline*io, const char *s, void *userdata);
23     void *userdata;
24 };
25
26 static void io_callback(struct iochannel*io, void *userdata);
27 static int do_write(struct ioline *l);
28
29 struct ioline* ioline_new(struct iochannel *io) {
30     struct ioline *l;
31     assert(io);
32     
33     l = malloc(sizeof(struct ioline));
34     assert(l);
35     l->io = io;
36     l->dead = 0;
37
38     l->wbuf = NULL;
39     l->wbuf_length = l->wbuf_index = l->wbuf_valid_length = 0;
40
41     l->rbuf = NULL;
42     l->rbuf_length = l->rbuf_index = l->rbuf_valid_length = 0;
43
44     l->callback = NULL;
45     l->userdata = NULL;
46
47     iochannel_set_callback(io, io_callback, l);
48     
49     return l;
50 }
51
52 void ioline_free(struct ioline *l) {
53     assert(l);
54     iochannel_free(l->io);
55     free(l->wbuf);
56     free(l->rbuf);
57     free(l);
58 }
59
60 void ioline_puts(struct ioline *l, const char *c) {
61     size_t len;
62     assert(l && c);
63     
64     len = strlen(c);
65     if (len > BUFFER_LIMIT - l->wbuf_valid_length)
66         len = BUFFER_LIMIT - l->wbuf_valid_length;
67
68     if (!len)
69         return;
70     
71     if (len > l->wbuf_length - l->wbuf_valid_length) {
72         size_t n = l->wbuf_valid_length+len;
73         char *new = malloc(n);
74         if (l->wbuf) {
75             memcpy(new, l->wbuf+l->wbuf_index, l->wbuf_valid_length);
76             free(l->wbuf);
77         }
78         l->wbuf = new;
79         l->wbuf_length = n;
80         l->wbuf_index = 0;
81     } else if (len > l->wbuf_length - l->wbuf_valid_length - l->wbuf_index) {
82         memmove(l->wbuf, l->wbuf+l->wbuf_index, l->wbuf_valid_length);
83         l->wbuf_index = 0;
84     }
85
86     memcpy(l->wbuf+l->wbuf_index+l->wbuf_valid_length, c, len);
87     l->wbuf_valid_length += len;
88
89     do_write(l);
90 }
91
92 void ioline_set_callback(struct ioline*l, void (*callback)(struct ioline*io, const char *s, void *userdata), void *userdata) {
93     assert(l && callback);
94     l->callback = callback;
95     l->userdata = userdata;
96 }
97
98 static int do_read(struct ioline *l) {
99     ssize_t r;
100     size_t m, len;
101     char *p, *e;
102     assert(l);
103
104     if (!iochannel_is_readable(l->io))
105         return 0;
106
107     len = l->rbuf_length - l->rbuf_index - l->rbuf_valid_length;
108
109     if (len < READ_SIZE) {
110         size_t n = l->rbuf_valid_length+READ_SIZE;
111
112         if (n >= BUFFER_LIMIT)
113             n = BUFFER_LIMIT;
114         
115         if (l->rbuf_length >= n) {
116             if (l->rbuf_valid_length)
117                 memmove(l->rbuf, l->rbuf+l->rbuf_index, l->rbuf_valid_length);
118         } else {
119             char *new = malloc(n);
120             if (l->rbuf_valid_length)
121                 memcpy(new, l->rbuf+l->rbuf_index, l->rbuf_valid_length);
122             free(l->rbuf);
123             l->rbuf = new;
124             l->rbuf_length = n;
125         }
126         
127         l->rbuf_index = 0;
128     }
129
130     len = l->rbuf_length - l->rbuf_index - l->rbuf_valid_length;
131     
132     if ((r = iochannel_read(l->io, l->rbuf+l->rbuf_index+l->rbuf_valid_length, len)) <= 0)
133         return -1;
134
135     e = memchr(l->rbuf+l->rbuf_index+l->rbuf_valid_length, '\n', r);
136     l->rbuf_valid_length += r;
137
138     if (!e && l->rbuf_valid_length >= BUFFER_LIMIT)
139         e = l->rbuf+BUFFER_LIMIT-1;
140         
141     *e = 0;
142     p = l->rbuf+l->rbuf_index;
143     m = strlen(p);
144
145     if (l->callback)
146         l->callback(l, p, l->userdata);
147
148     l->rbuf_index += m+1;
149     l->rbuf_valid_length -= m+1;
150
151     if (l->rbuf_valid_length == 0)
152         l->rbuf_index = 0;
153
154     return 0;
155 }
156
157 static int do_write(struct ioline *l) {
158     ssize_t r;
159     assert(l);
160
161     if (!l->wbuf_valid_length || !iochannel_is_writable(l->io))
162         return 0;
163     
164     if ((r = iochannel_write(l->io, l->wbuf+l->wbuf_index, l->wbuf_valid_length)) < 0)
165         return -1;
166
167     l->wbuf_valid_length -= r;
168     if (l->wbuf_valid_length == 0)
169         l->wbuf_index = 0;
170
171     return 0;
172 }
173
174 static void io_callback(struct iochannel*io, void *userdata) {
175     struct ioline *l = userdata;
176     assert(io && l);
177     
178     if (!l->dead && do_read(l) < 0)
179         goto fail;
180     
181     if (!l->dead && do_write(l) < 0)
182         goto fail;
183     
184     return;
185
186 fail:
187     if (l->callback)
188         l->callback(l, NULL, l->userdata);
189     l->dead = 1;
190 }