#include "xmalloc.h"
#include "log.h"
+#define PROMPT ">>> "
+
struct pa_cli {
struct pa_core *core;
struct pa_ioline *line;
};
static void line_callback(struct pa_ioline *line, const char *s, void *userdata);
-
-static const char prompt[] = ">>> ";
-
static void client_kill(struct pa_client *c);
struct pa_cli* pa_cli_new(struct pa_core *core, struct pa_iochannel *io, struct pa_module *m) {
c->client->owner = m;
pa_ioline_set_callback(c->line, line_callback, c);
- pa_ioline_puts(c->line, "Welcome to polypaudio! Use \"help\" for usage information.\n");
- pa_ioline_puts(c->line, prompt);
+ pa_ioline_puts(c->line, "Welcome to polypaudio! Use \"help\" for usage information.\n"PROMPT);
c->fail = c->kill_requested = c->defer_kill = 0;
c->verbose = 1;
if (c->eof_callback)
c->eof_callback(c, c->userdata);
} else
- pa_ioline_puts(line, prompt);
+ pa_ioline_puts(line, PROMPT);
}
void pa_cli_set_eof_callback(struct pa_cli *c, void (*cb)(struct pa_cli*c, void *userdata), void *userdata) {
- assert(c && cb);
+ assert(c);
c->eof_callback = cb;
c->userdata = userdata;
}
if (!len)
return;
-
- if (len > l->wbuf_length - l->wbuf_valid_length) {
+
+ assert(l->wbuf_length >= l->wbuf_valid_length);
+
+ /* In case the allocated buffer is too small, enlarge it. */
+ if (l->wbuf_valid_length + len > l->wbuf_length) {
size_t n = l->wbuf_valid_length+len;
char *new = pa_xmalloc(n);
if (l->wbuf) {
l->wbuf = new;
l->wbuf_length = n;
l->wbuf_index = 0;
- } else if (len > l->wbuf_length - l->wbuf_valid_length - l->wbuf_index) {
+ } else if (l->wbuf_index + l->wbuf_valid_length + len > l->wbuf_length) {
+
+ /* In case the allocated buffer fits, but the current index is too far from the start, move it to the front. */
memmove(l->wbuf, l->wbuf+l->wbuf_index, l->wbuf_valid_length);
l->wbuf_index = 0;
}
- memcpy(l->wbuf+l->wbuf_index+l->wbuf_valid_length, c, len);
+ assert(l->wbuf_index + l->wbuf_valid_length + len <= l->wbuf_length);
+
+ /* Append the new string */
+ memcpy(l->wbuf + l->wbuf_index + l->wbuf_valid_length, c, len);
l->wbuf_valid_length += len;
do_write(l);
}
void pa_ioline_set_callback(struct pa_ioline*l, void (*callback)(struct pa_ioline*io, const char *s, void *userdata), void *userdata) {
- assert(l && callback);
+ assert(l);
l->callback = callback;
l->userdata = userdata;
}
+static void scan_for_lines(struct pa_ioline *l, size_t skip) {
+ assert(l && skip < l->rbuf_valid_length);
+
+ while (!l->dead && l->rbuf_valid_length > skip) {
+ char *e, *p;
+ size_t m;
+
+ if (!(e = memchr(l->rbuf + l->rbuf_index + skip, '\n', l->rbuf_valid_length - skip)))
+ break;
+
+ *e = 0;
+
+ p = l->rbuf + l->rbuf_index;
+ m = strlen(p);
+
+ l->rbuf_index += m+1;
+ l->rbuf_valid_length -= m+1;
+
+ /* A shortcut for the next time */
+ if (l->rbuf_valid_length == 0)
+ l->rbuf_index = 0;
+
+ if (l->callback)
+ l->callback(l, p, l->userdata);
+
+ skip = 0;
+ }
+
+ /* If the buffer became too large and still no newline was found, drop it. */
+ if (l->rbuf_valid_length >= BUFFER_LIMIT)
+ l->rbuf_index = l->rbuf_valid_length = 0;
+}
+
static int do_read(struct pa_ioline *l) {
ssize_t r;
- size_t m, len;
- char *e;
+ size_t len;
assert(l);
if (!pa_iochannel_is_readable(l->io))
len = l->rbuf_length - l->rbuf_index - l->rbuf_valid_length;
+ /* Check if we have to enlarge the read buffer */
if (len < READ_SIZE) {
size_t n = l->rbuf_valid_length+READ_SIZE;
n = BUFFER_LIMIT;
if (l->rbuf_length >= n) {
+ /* The current buffer is large enough, let's just move the data to the front */
if (l->rbuf_valid_length)
memmove(l->rbuf, l->rbuf+l->rbuf_index, l->rbuf_valid_length);
} else {
+ /* Enlarge the buffer */
char *new = pa_xmalloc(n);
if (l->rbuf_valid_length)
memcpy(new, l->rbuf+l->rbuf_index, l->rbuf_valid_length);
}
len = l->rbuf_length - l->rbuf_index - l->rbuf_valid_length;
-
+
+ assert(len >= READ_SIZE);
+
+ /* Read some data */
if ((r = pa_iochannel_read(l->io, l->rbuf+l->rbuf_index+l->rbuf_valid_length, len)) <= 0)
return -1;
- e = memchr(l->rbuf+l->rbuf_index+l->rbuf_valid_length, '\n', r);
l->rbuf_valid_length += r;
- if (!e &&l->rbuf_valid_length >= BUFFER_LIMIT)
- e = l->rbuf+BUFFER_LIMIT-1;
-
- if (e) {
- char *p;
-
- *e = 0;
-
- p = l->rbuf+l->rbuf_index;
- m = strlen(p);
-
- l->rbuf_index += m+1;
- l->rbuf_valid_length -= m+1;
-
- if (l->rbuf_valid_length == 0)
- l->rbuf_index = 0;
-
- if (l->callback)
- l->callback(l, p, l->userdata);
- }
+ /* Look if a line has been terminated in the newly read data */
+ scan_for_lines(l, l->rbuf_valid_length - r);
return 0;
}
+/* Try to flush the buffer */
static int do_write(struct pa_ioline *l) {
ssize_t r;
assert(l);
if ((r = pa_iochannel_write(l->io, l->wbuf+l->wbuf_index, l->wbuf_valid_length)) < 0)
return -1;
+ l->wbuf_index += r;
l->wbuf_valid_length -= r;
+
+ /* A shortcut for the next time */
if (l->wbuf_valid_length == 0)
l->wbuf_index = 0;
return 0;
}
+/* Try to flush read/write data */
static void io_callback(struct pa_iochannel*io, void *userdata) {
struct pa_ioline *l = userdata;
assert(io && l);
- if (!l->dead && do_write(l) < 0)
- goto fail;
-
- if (!l->dead && do_read(l) < 0)
- goto fail;
-
- return;
-
-fail:
- l->dead = 1;
- if (l->callback)
- l->callback(l, NULL, l->userdata);
+ if ((!l->dead && do_write(l) < 0) ||
+ (!l->dead && do_read(l) < 0)) {
+
+ l->dead = 1;
+ if (l->callback)
+ l->callback(l, NULL, l->userdata);
+ }
}
#include "strbuf.h"
-/* Some magic for zero-length arrays */
-#ifdef __STDC_VERSION__
-#if __STDC_VERSION__ >= 199901L
-#ifndef STDC99
-#define STDC99
-#endif
-#endif
-#endif
-
/* A chunk of the linked list that makes up the string */
struct chunk {
struct chunk *next;
size_t length;
-#ifdef STDC99
- char text[];
-#else
- char text[0];
-#endif
};
+#define CHUNK_TO_TEXT(c) ((char*) (c) + sizeof(struct chunk))
+
struct pa_strbuf {
size_t length;
struct chunk *head, *tail;
struct chunk *c;
assert(sb);
- t = pa_xmalloc(sb->length+1);
+ e = t = pa_xmalloc(sb->length+1);
- e = t;
for (c = sb->head; c; c = c->next) {
assert((size_t) (e-t) <= sb->length);
- memcpy(e, c->text, c->length);
+ memcpy(e, CHUNK_TO_TEXT(c), c->length);
e += c->length;
}
/* Trailing NUL */
*e = 0;
+
+ assert(e == t+sb->length);
return t;
}
c = pa_xmalloc(sizeof(struct chunk)+l);
c->length = l;
- memcpy(c->text, t, l);
+ memcpy(CHUNK_TO_TEXT(c), t, l);
append(sb, c);
}
c = pa_xrealloc(c, sizeof(struct chunk)+size);
va_start(ap, format);
- r = vsnprintf(c->text, size, format, ap);
+ r = vsnprintf(CHUNK_TO_TEXT(c), size, format, ap);
va_end(ap);
if (r > -1 && r < size) {