diff options
| author | David Lamparter <equinox@opensourcerouting.org> | 2017-07-13 17:34:08 +0200 |
|---|---|---|
| committer | David Lamparter <equinox@opensourcerouting.org> | 2017-07-13 20:29:20 +0200 |
| commit | 83eba583d7be5afaf2eba35ce4d391f645af4bfa (patch) | |
| tree | 1dc80b60d845022589a592405e2d4bb7c1a392bd /lib/buffer.c | |
| parent | 8867927f0ea63a0a76ac75592da554da995ed9de (diff) | |
lib: move \n vs. \r\n handling into vty code
Instead of having an ?: expression embedded in every single caller of
vty_out, just expand \n to \r\n in the vty code if neccessary.
(Deprecation warnings will be enabled in the next commits which will do
the search-and-replace over the codebase.)
[This reverts commit 4d5f445 "lib: add vty_outln()"]
Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
Diffstat (limited to 'lib/buffer.c')
| -rw-r--r-- | lib/buffer.c | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/lib/buffer.c b/lib/buffer.c index 649677fc93..5d458901ee 100644 --- a/lib/buffer.c +++ b/lib/buffer.c @@ -199,6 +199,50 @@ buffer_putstr (struct buffer *b, const char *c) buffer_put(b, c, strlen(c)); } +/* Expand \n to \r\n */ +void +buffer_put_crlf(struct buffer *b, const void *origp, size_t origsize) +{ + struct buffer_data *data = b->tail; + const char *p = origp, *end = p + origsize, *lf; + size_t size; + + lf = memchr(p, '\n', end - p); + + /* We use even last one byte of data buffer. */ + while (p < end) + { + size_t avail, chunk; + + /* If there is no data buffer add it. */ + if (data == NULL || data->cp == b->size) + data = buffer_add (b); + + size = (lf ? lf : end) - p; + avail = b->size - data->cp; + + chunk = (size <= avail) ? size : avail; + memcpy (data->data + data->cp, p, chunk); + + p += chunk; + data->cp += chunk; + + if (lf && size <= avail) + { + /* we just copied up to (including) a '\n' */ + if (data->cp == b->size) + data = buffer_add (b); + data->data[data->cp++] = '\r'; + if (data->cp == b->size) + data = buffer_add (b); + data->data[data->cp++] = '\n'; + + p++; + lf = memchr(p, '\n', end - p); + } + } +} + /* Keep flushing data to the fd until the buffer is empty or an error is encountered or the operation would block. */ buffer_status_t |
