diff options
author | Lennart Poettering <lennart@poettering.net> | 2004-11-17 01:04:52 +0000 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2004-11-17 01:04:52 +0000 |
commit | 5ea2783dda66ebf343f72350aaa99d22f35f6d0e (patch) | |
tree | 77f38fc40632428dad7524556e55db8c2d5fd20a /polyp | |
parent | 0a2bbc528b7865b08139155e0316738a717c4e42 (diff) |
* fix a long standing bug in ioline.c (large prints failed)
* fix a bug regarding ipv6 binding
git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@287 fefdeb5f-60dc-0310-8127-8f9354f1896f
Diffstat (limited to 'polyp')
-rw-r--r-- | polyp/cli-command.c | 2 | ||||
-rw-r--r-- | polyp/cli.c | 12 | ||||
-rw-r--r-- | polyp/ioline.c | 109 | ||||
-rw-r--r-- | polyp/socket-server.c | 4 | ||||
-rw-r--r-- | polyp/strbuf.c | 27 |
5 files changed, 83 insertions, 71 deletions
diff --git a/polyp/cli-command.c b/polyp/cli-command.c index dec877fb..c11647fc 100644 --- a/polyp/cli-command.c +++ b/polyp/cli-command.c @@ -242,8 +242,6 @@ static int pa_cli_command_stat(struct pa_core *c, struct pa_tokenizer *t, struct pa_namereg_get_default_sink_name(c), pa_namereg_get_default_source_name(c)); - - return 0; } diff --git a/polyp/cli.c b/polyp/cli.c index 15142f35..2ff35992 100644 --- a/polyp/cli.c +++ b/polyp/cli.c @@ -44,6 +44,8 @@ #include "xmalloc.h" #include "log.h" +#define PROMPT ">>> " + struct pa_cli { struct pa_core *core; struct pa_ioline *line; @@ -57,9 +59,6 @@ struct pa_cli { }; 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) { @@ -83,8 +82,7 @@ struct pa_cli* pa_cli_new(struct pa_core *core, struct pa_iochannel *io, struct 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; @@ -139,11 +137,11 @@ static void line_callback(struct pa_ioline *line, const char *s, void *userdata) 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; } diff --git a/polyp/ioline.c b/polyp/ioline.c index b1c980d8..f3d17b58 100644 --- a/polyp/ioline.c +++ b/polyp/ioline.c @@ -92,8 +92,11 @@ void pa_ioline_puts(struct pa_ioline *l, const char *c) { 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) { @@ -103,27 +106,64 @@ void pa_ioline_puts(struct pa_ioline *l, const char *c) { 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)) @@ -131,6 +171,7 @@ static int do_read(struct pa_ioline *l) { 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; @@ -138,9 +179,11 @@ static int do_read(struct pa_ioline *l) { 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); @@ -153,37 +196,22 @@ static int do_read(struct pa_ioline *l) { } 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); @@ -194,27 +222,26 @@ static int do_write(struct pa_ioline *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); + } } diff --git a/polyp/socket-server.c b/polyp/socket-server.c index 7268ccfa..e67a9daa 100644 --- a/polyp/socket-server.c +++ b/polyp/socket-server.c @@ -202,7 +202,7 @@ struct pa_socket_server* pa_socket_server_new_ipv4(struct pa_mainloop_api *m, ui pa_socket_tcp_low_delay(fd); - memset(&sa, sizeof(sa), 0); + memset(&sa, 0, sizeof(sa)); sa.sin_family = AF_INET; sa.sin_port = htons(port); sa.sin_addr.s_addr = htonl(address); @@ -251,7 +251,7 @@ struct pa_socket_server* pa_socket_server_new_ipv6(struct pa_mainloop_api *m, ui pa_socket_tcp_low_delay(fd); - memset(&sa, sizeof(sa), 0); + memset(&sa, 0, sizeof(sa)); sa.sin6_family = AF_INET6; sa.sin6_port = htons(port); memcpy(sa.sin6_addr.s6_addr, address, 16); diff --git a/polyp/strbuf.c b/polyp/strbuf.c index a6521651..1aa710ea 100644 --- a/polyp/strbuf.c +++ b/polyp/strbuf.c @@ -33,26 +33,14 @@ #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; @@ -83,17 +71,18 @@ char *pa_strbuf_tostring(struct pa_strbuf *sb) { 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; } @@ -140,7 +129,7 @@ void pa_strbuf_putsn(struct pa_strbuf *sb, const char *t, size_t l) { 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); } @@ -160,7 +149,7 @@ int pa_strbuf_printf(struct pa_strbuf *sb, const char *format, ...) { 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) { |