#include "plugin.h"
#include "common.h"
#include "utils_cache.h"
-#include "utils_parse_option.h"
/*
* Private variables
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;
*/
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
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 : */
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 : */
#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); \
#include "plugin.h"
#include "common.h"
#include "utils_cache.h"
-#include "utils_parse_option.h"
#include "utils_format_json.h"
#if HAVE_PTHREAD_H
#include "configfile.h"
#include "utils_cache.h"
-#include "utils_parse_option.h"
#include <pthread.h>
#include <sys/socket.h>