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>
This commit is contained in:
David Lamparter 2017-07-13 17:34:08 +02:00
parent 8867927f0e
commit 83eba583d7
5 changed files with 74 additions and 44 deletions

View File

@ -199,6 +199,50 @@ buffer_putstr (struct buffer *b, const char *c)
buffer_put(b, c, strlen(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 /* Keep flushing data to the fd until the buffer is empty or an error is
encountered or the operation would block. */ encountered or the operation would block. */
buffer_status_t buffer_status_t

View File

@ -41,6 +41,8 @@ extern void buffer_put (struct buffer *, const void *, size_t);
extern void buffer_putc (struct buffer *, u_char); extern void buffer_putc (struct buffer *, u_char);
/* Add a NUL-terminated string to the end of the buffer. */ /* Add a NUL-terminated string to the end of the buffer. */
extern void buffer_putstr (struct buffer *, const char *); extern void buffer_putstr (struct buffer *, const char *);
/* Add given data, inline-expanding \n to \r\n */
extern void buffer_put_crlf(struct buffer *b, const void *p, size_t size);
/* Combine all accumulated (and unflushed) data inside the buffer into a /* Combine all accumulated (and unflushed) data inside the buffer into a
single NUL-terminated string allocated using XMALLOC(MTYPE_TMP). Note single NUL-terminated string allocated using XMALLOC(MTYPE_TMP). Note

View File

@ -92,23 +92,28 @@ char integrate_default[] = SYSCONFDIR INTEGRATE_DEFAULT_CONFIG;
static int do_log_commands = 0; static int do_log_commands = 0;
static int /* VTY standard output function. */
vty_out_variadic (struct vty *vty, const char *format, va_list args) int
vty_out (struct vty *vty, const char *format, ...)
{ {
va_list args;
int len = 0; int len = 0;
int size = 1024; int size = 1024;
char buf[1024]; char buf[1024];
char *p = NULL; char *p = NULL;
va_list cp;
if (vty_shell (vty)) if (vty_shell (vty))
{
va_start (args, format);
vprintf (format, args); vprintf (format, args);
va_end (args);
}
else else
{ {
/* Try to write to initial buffer. */ /* Try to write to initial buffer. */
va_copy (cp, args); va_start (args, format);
len = vsnprintf (buf, sizeof(buf), format, cp); len = vsnprintf (buf, sizeof(buf), format, args);
va_end (cp); va_end (args);
/* Initial buffer is not enough. */ /* Initial buffer is not enough. */
if (len < 0 || len >= size) if (len < 0 || len >= size)
@ -124,9 +129,9 @@ vty_out_variadic (struct vty *vty, const char *format, va_list args)
if (! p) if (! p)
return -1; return -1;
va_copy (cp, args); va_start (args, format);
len = vsnprintf (p, size, format, cp); len = vsnprintf (p, size, format, args);
va_end (cp); va_end (args);
if (len > -1 && len < size) if (len > -1 && len < size)
break; break;
@ -138,7 +143,10 @@ vty_out_variadic (struct vty *vty, const char *format, va_list args)
p = buf; p = buf;
/* Pointer p must point out buffer. */ /* Pointer p must point out buffer. */
if (vty->type != VTY_TERM)
buffer_put (vty->obuf, (u_char *) p, len); buffer_put (vty->obuf, (u_char *) p, len);
else
buffer_put_crlf (vty->obuf, (u_char *) p, len);
/* If p is not different with buf, it is allocated buffer. */ /* If p is not different with buf, it is allocated buffer. */
if (p != buf) if (p != buf)
@ -147,32 +155,6 @@ vty_out_variadic (struct vty *vty, const char *format, va_list args)
return len; return len;
} }
/* VTY standard output function. */
int
vty_out (struct vty *vty, const char *format, ...)
{
int len;
va_list args;
va_start (args, format);
len = vty_out_variadic (vty, format, args);
va_end (args);
return len;
}
int
vty_outln (struct vty *vty, const char *format, ...)
{
int len;
va_list args;
va_start (args, format);
len = vty_out_variadic (vty, format, args);
va_end (args);
return len + vty_out (vty, "%s", VTYNL);
}
static int static int
vty_log_out (struct vty *vty, const char *level, const char *proto_str, vty_log_out (struct vty *vty, const char *level, const char *proto_str,

View File

@ -179,9 +179,6 @@ struct vty_arg
/* Integrated configuration file. */ /* Integrated configuration file. */
#define INTEGRATE_DEFAULT_CONFIG "frr.conf" #define INTEGRATE_DEFAULT_CONFIG "frr.conf"
/* Small macro to determine newline is newline only or linefeed needed. */
#define VTYNL ((vty->type == VTY_TERM) ? "\r\n" : "\n")
/* for compatibility */ /* for compatibility */
#if defined(__ICC) #if defined(__ICC)
#define CPP_WARN_STR(X) #X #define CPP_WARN_STR(X) #X
@ -198,8 +195,12 @@ struct vty_arg
#define CPP_WARN(text) #define CPP_WARN(text)
#endif #endif
#define VTY_NEWLINE VTYNL \ #define VNL "\n" \
CPP_WARN("VTY_NEWLINE has been replaced with VTYNL and/or vty_outln().") /* CPP_WARN("VNL has been replaced with \\n.") */
#define VTYNL "\n" \
/* CPP_WARN("VTYNL has been replaced with \\n.") */
#define VTY_NEWLINE "\n" \
CPP_WARN("VTY_NEWLINE has been replaced with \\n.")
#define VTY_GET_INTEGER(desc,v,str) {(v)=strtoul ((str), NULL, 10);} \ #define VTY_GET_INTEGER(desc,v,str) {(v)=strtoul ((str), NULL, 10);} \
CPP_WARN("VTY_GET_INTEGER is no longer useful, use strtoul() or DEFPY.") CPP_WARN("VTY_GET_INTEGER is no longer useful, use strtoul() or DEFPY.")
#define VTY_GET_INTEGER_RANGE(desc,v,str,min,max) {(v)=strtoul ((str), NULL, 10);} \ #define VTY_GET_INTEGER_RANGE(desc,v,str,min,max) {(v)=strtoul ((str), NULL, 10);} \
@ -212,6 +213,9 @@ struct vty_arg
CPP_WARN("VTY_GET_IPV4_ADDRESS is no longer useful, use inet_aton() or DEFPY.") CPP_WARN("VTY_GET_IPV4_ADDRESS is no longer useful, use inet_aton() or DEFPY.")
#define VTY_GET_IPV4_PREFIX(desc,v,str) str2prefix_ipv4 ((str), &(v)) \ #define VTY_GET_IPV4_PREFIX(desc,v,str) str2prefix_ipv4 ((str), &(v)) \
CPP_WARN("VTY_GET_IPV4_PREFIX is no longer useful, use str2prefix_ipv4() or DEFPY.") CPP_WARN("VTY_GET_IPV4_PREFIX is no longer useful, use str2prefix_ipv4() or DEFPY.")
#define vty_outln(vty, str, ...) \
vty_out(vty, str "\n", ## __VA_ARGS__) \
/* CPP_WARN("vty_outln is no longer useful, use vty_out(...\\n...)") */
/* Default time out value */ /* Default time out value */
#define VTY_TIMEOUT_DEFAULT 600 #define VTY_TIMEOUT_DEFAULT 600
@ -239,7 +243,6 @@ extern void vty_reset (void);
extern struct vty *vty_new (void); extern struct vty *vty_new (void);
extern struct vty *vty_stdio (void (*atclose)(void)); extern struct vty *vty_stdio (void (*atclose)(void));
extern int vty_out (struct vty *, const char *, ...) PRINTF_ATTRIBUTE(2, 3); extern int vty_out (struct vty *, const char *, ...) PRINTF_ATTRIBUTE(2, 3);
extern int vty_outln (struct vty *, const char *, ...) PRINTF_ATTRIBUTE(2, 3);
extern void vty_read_config (const char *, char *); extern void vty_read_config (const char *, char *);
extern void vty_time_print (struct vty *, int); extern void vty_time_print (struct vty *, int);
extern void vty_serv_sock (const char *, unsigned short, const char *); extern void vty_serv_sock (const char *, unsigned short, const char *);

View File

@ -90,7 +90,6 @@ extern struct thread_master *master;
#define OSPF6_ROUTER_ID_STR "Specify Router-ID\n" #define OSPF6_ROUTER_ID_STR "Specify Router-ID\n"
#define OSPF6_LS_ID_STR "Specify Link State ID\n" #define OSPF6_LS_ID_STR "Specify Link State ID\n"
#define VNL VTYNL
#define OSPF6_CMD_CHECK_RUNNING() \ #define OSPF6_CMD_CHECK_RUNNING() \
if (ospf6 == NULL) \ if (ospf6 == NULL) \
{ \ { \