summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 216c624)
raw | patch | inline | side by side (parent: 216c624)
author | Florian Forster <octo@collectd.org> | |
Sat, 20 Sep 2014 05:11:17 +0000 (07:11 +0200) | ||
committer | Florian Forster <octo@collectd.org> | |
Sat, 20 Sep 2014 05:11:17 +0000 (07:11 +0200) |
diff --git a/src/csv.c b/src/csv.c
index e9a409d8c1cdf7643ab0025e6458dfdacaadba68..b6e535ee6b4c9b44eae4cde70052e877c1c5e2ee 100644 (file)
--- a/src/csv.c
+++ b/src/csv.c
#include "plugin.h"
#include "common.h"
#include "utils_cache.h"
-#include "utils_parse_option.h"
/*
* Private variables
diff --git a/src/daemon/common.c b/src/daemon/common.c
index 5386739fbc372e96638ac55c79a91e8ca3af3ca9..535dfad33d51febe2bb40507a8a0c6f866f4648f 100644 (file)
--- a/src/daemon/common.c
+++ b/src/daemon/common.c
return (ret);
} /* int strsubstitute */
+int escape_string (char *buffer, size_t buffer_size)
+{
+ char *temp;
+ size_t i;
+ size_t j;
+
+ /* Check if we need to escape at all first */
+ temp = strpbrk (buffer, " \t\"\\");
+ if (temp == NULL)
+ return (0);
+
+ if (buffer_size < 3)
+ return (EINVAL);
+
+ temp = (char *) malloc (buffer_size);
+ if (temp == NULL)
+ return (ENOMEM);
+ memset (temp, 0, buffer_size);
+
+ temp[0] = '"';
+ j = 1;
+
+ for (i = 0; i < buffer_size; i++)
+ {
+ if (buffer[i] == 0)
+ {
+ break;
+ }
+ else if ((buffer[i] == '"') || (buffer[i] == '\\'))
+ {
+ if (j > (buffer_size - 4))
+ break;
+ temp[j] = '\\';
+ temp[j + 1] = buffer[i];
+ j += 2;
+ }
+ else
+ {
+ if (j > (buffer_size - 3))
+ break;
+ temp[j] = buffer[i];
+ j++;
+ }
+ }
+
+ assert ((j + 1) < buffer_size);
+ temp[j] = '"';
+ temp[j + 1] = 0;
+
+ sstrncpy (buffer, temp, buffer_size);
+ sfree (temp);
+ return (0);
+} /* int escape_string */
+
int strunescape (char *buf, size_t buf_len)
{
size_t i;
diff --git a/src/daemon/common.h b/src/daemon/common.h
index 434ed019bc2d1a2359db6bbcf8d982fba98e091c..da21cad9a6f3a81f1120b466740a669ec009c231 100644 (file)
--- a/src/daemon/common.h
+++ b/src/daemon/common.h
@@ -186,6 +186,27 @@ int strjoin (char *dst, size_t dst_len, char **fields, size_t fields_num, const
*/
int escape_slashes (char *buffer, size_t buffer_size);
+/**
+ * NAME
+ * escape_string
+ *
+ * DESCRIPTION
+ * escape_string quotes and escapes a string to be usable with collectd's
+ * plain text protocol. "simple" strings are left as they are, for example if
+ * buffer is 'simple' before the call, it will remain 'simple'. However, if
+ * buffer contains 'more "complex"' before the call, the returned buffer will
+ * contain '"more \"complex\""'.
+ *
+ * If the buffer is too small to contain the escaped string, the string will
+ * be truncated. However, leading and trailing double quotes, as well as an
+ * ending null byte are guaranteed.
+ *
+ * RETURN VALUE
+ * Returns zero on success, even if the string was truncated. Non-zero on
+ * failure.
+ */
+int escape_string (char *buffer, size_t buffer_size);
+
/*
* NAME
* replace_special
index 56e65ea54f127e456c660b5774e7e229fdd42726..7f06f2959133fe46724344184d890a5d52befd9a 100644 (file)
return (0);
} /* int parse_option */
-int escape_string (char *buffer, size_t buffer_size)
-{
- char *temp;
- size_t i;
- size_t j;
-
- /* Check if we need to escape at all first */
- temp = strpbrk (buffer, " \t\"\\");
- if (temp == NULL)
- return (0);
-
- temp = (char *) malloc (buffer_size);
- if (temp == NULL)
- return (-1);
- memset (temp, 0, buffer_size);
-
- temp[0] = '"';
- j = 1;
-
- for (i = 0; i < buffer_size; i++)
- {
- if (buffer[i] == 0)
- {
- break;
- }
- else if ((buffer[i] == '"') || (buffer[i] == '\\'))
- {
- if (j > (buffer_size - 4))
- break;
- temp[j] = '\\';
- temp[j + 1] = buffer[i];
- j += 2;
- }
- else
- {
- if (j > (buffer_size - 3))
- break;
- temp[j] = buffer[i];
- j++;
- }
- }
-
- assert ((j + 1) < buffer_size);
- temp[j] = '"';
- temp[j + 1] = 0;
-
- sstrncpy (buffer, temp, buffer_size);
- sfree (temp);
- return (0);
-} /* int escape_string */
-
/* vim: set sw=2 ts=8 tw=78 et : */
index 01b73d16c7c0092a7cffd8c838472bbd76e615d2..885a6a33d263dfe1dd3429e209dc4484bf7a307d 100644 (file)
int parse_string (char **ret_buffer, char **ret_string);
int parse_option (char **ret_buffer, char **ret_key, char **ret_value);
-int escape_string (char *buffer, size_t buffer_size);
-
#endif /* UTILS_PARSE_OPTION */
/* vim: set sw=2 ts=8 tw=78 et : */
index 31b83c37406c49864d668bdb2930211c928ef857..699c74e2718706157777096598038132194bd621 100644 (file)
--- a/src/utils_format_json.c
+++ b/src/utils_format_json.c
#include "utils_cache.h"
#include "utils_format_json.h"
-static int escape_string (char *buffer, size_t buffer_size, /* {{{ */
+static int json_escape_string (char *buffer, size_t buffer_size, /* {{{ */
const char *string)
{
size_t src_pos;
#undef BUFFER_ADD
return (0);
-} /* }}} int escape_string */
+} /* }}} int json_escape_string */
static int values_to_json (char *buffer, size_t buffer_size, /* {{{ */
const data_set_t *ds, const value_list_t *vl, int store_rates)
if (meta_data_get_string (meta, key, &value) == 0)
{
char temp[512] = "";
- escape_string (temp, sizeof (temp), value);
+ json_escape_string (temp, sizeof (temp), value);
sfree (value);
BUFFER_ADD (",\"%s\":%s", key, temp);
}
BUFFER_ADD (",\"interval\":%.3f", CDTIME_T_TO_DOUBLE (vl->interval));
#define BUFFER_ADD_KEYVAL(key, value) do { \
- status = escape_string (temp, sizeof (temp), (value)); \
+ status = json_escape_string (temp, sizeof (temp), (value)); \
if (status != 0) \
return (status); \
BUFFER_ADD (",\"%s\":%s", (key), temp); \
diff --git a/src/write_http.c b/src/write_http.c
index 7a1fbd04fd823bca1fae6609d28f6ad65cbeab64..d97bc9b52ce9fde8b736d80d31bb0ede208f9f70 100644 (file)
--- a/src/write_http.c
+++ b/src/write_http.c
#include "plugin.h"
#include "common.h"
#include "utils_cache.h"
-#include "utils_parse_option.h"
#include "utils_format_json.h"
#if HAVE_PTHREAD_H
diff --git a/src/write_tsdb.c b/src/write_tsdb.c
index 2eca77e1b038fd9a658864917d616f31ccfa55bc..aeed635097bff8ad183cade11e07baaafb831304 100644 (file)
--- a/src/write_tsdb.c
+++ b/src/write_tsdb.c
#include "configfile.h"
#include "utils_cache.h"
-#include "utils_parse_option.h"
#include <pthread.h>
#include <sys/socket.h>