From 8fa1cf1c6e1ff5337fdda19446aa42ee81c3d816 Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Thu, 19 Feb 2009 13:14:35 +0100 Subject: [PATCH] table plugin, src/common: Un-escape '\t', '\n' and '\r' in column separators. For this purpose, the function strunescape() has been added to the "common" module. Currently, any escape sequence that's not '\t', '\n' or '\r' will be expanded to the literal escaped character. --- src/collectd.conf.pod | 4 ++++ src/common.c | 34 ++++++++++++++++++++++++++++++++++ src/common.h | 23 +++++++++++++++++++++++ src/table.c | 1 + 4 files changed, 62 insertions(+) diff --git a/src/collectd.conf.pod b/src/collectd.conf.pod index a8831b92..962fcfe7 100644 --- a/src/collectd.conf.pod +++ b/src/collectd.conf.pod @@ -2486,6 +2486,10 @@ table is considered to be a single delimiter, i.Ee. there cannot be any empty columns. The plugin uses the L function to parse the lines of a table - see its documentation for more details. This option is mandatory. +A horizontal tab, newline and carriage return may be specified by C<\\t>, +C<\\n> and C<\\r> respectively. Please note that the double backslashes are +required because of collectd's config parsing. + =back The following options are available inside a B block: diff --git a/src/common.c b/src/common.c index c4994a2c..2286088a 100644 --- a/src/common.c +++ b/src/common.c @@ -315,6 +315,40 @@ int strsubstitute (char *str, char c_from, char c_to) return (ret); } /* int strsubstitute */ +int strunescape (char *buf, size_t buf_len) +{ + size_t i; + + for (i = 0; (i < buf_len) && (buf[i] != '\0'); ++i) + { + if (buf[i] != '\\') + continue; + + if ((i >= buf_len) || (buf[i + 1] == '\0')) { + ERROR ("string unescape: backslash found at end of string."); + return (-1); + } + + switch (buf[i + 1]) { + case 't': + buf[i] = '\t'; + break; + case 'n': + buf[i] = '\n'; + break; + case 'r': + buf[i] = '\r'; + break; + default: + buf[i] = buf[i + 1]; + break; + } + + memmove (buf + i + 1, buf + i + 2, buf_len - i - 2); + } + return (0); +} /* int strunescape */ + int escape_slashes (char *buf, int buf_len) { int i; diff --git a/src/common.h b/src/common.h index 5b9f02cc..72098083 100644 --- a/src/common.h +++ b/src/common.h @@ -177,6 +177,29 @@ void replace_special (char *buffer, size_t buffer_size); int strsubstitute (char *str, char c_from, char c_to); +/* + * NAME + * strunescape + * + * DESCRIPTION + * Replaces any escaped characters in a string with the appropriate special + * characters. The following escaped characters are recognized: + * + * \t -> + * \n -> + * \r -> + * + * For all other escacped characters only the backslash will be removed. + * + * PARAMETERS + * `buf' String to be unescaped. + * `buf_len' Length of the string, including the terminating null-byte. + * + * RETURN VALUE + * Returns zero upon success, a value less than zero else. + */ +int strunescape (char *buf, size_t buf_len); + /* * NAME * timeval_cmp diff --git a/src/table.c b/src/table.c index d48a2ef6..2911bf02 100644 --- a/src/table.c +++ b/src/table.c @@ -279,6 +279,7 @@ static int tbl_config_table (oconfig_item_t *ci) log_err ("Table \"%s\" does not specify any separator.", tbl->file); status = 1; } + strunescape (tbl->sep, strlen (tbl->sep) + 1); if (NULL == tbl->instance) { tbl->instance = sstrdup (tbl->file); -- 2.30.2