2 * collectd - src/utils_parse_option.c
3 * Copyright (C) 2008 Florian Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Florian octo Forster <octo at verplant.org>
25 #include "utils_parse_option.h"
27 int parse_string (char **ret_buffer, char **ret_string)
34 /* Eat up leading spaces. */
36 while (isspace ((int) *string))
52 while ((*buffer != '"') && (*buffer != 0))
54 /* Un-escape backslashes */
58 /* Catch a backslash at the end of buffer */
66 /* No quote sign has been found */
75 /* Check for trailing spaces. */
76 if ((*buffer != 0) && !isspace ((int) *buffer))
79 else /* an unquoted string */
82 while ((*buffer != 0) && !isspace ((int) *buffer))
91 /* Eat up trailing spaces */
92 while (isspace ((int) *buffer))
99 } /* int parse_string */
104 * Parses an ``option'' as used with the unixsock and exec commands. An
105 * option is of the form:
107 * name1="value with \"quotes\""
108 * name2="value \\ backslash"
109 * However, if the value does *not* contain a space character, you can skip
112 int parse_option (char **ret_buffer, char **ret_key, char **ret_value)
119 buffer = *ret_buffer;
121 /* Eat up leading spaces */
123 while (isspace ((int) *key))
128 /* Look for the equal sign */
130 while (isalnum ((int) *buffer) || *buffer == '_')
132 if ((*buffer != '=') || (buffer == key))
136 /* Empty values must be written as "" */
137 if (isspace ((int) *buffer) || (*buffer == 0))
140 status = parse_string (&buffer, &value);
144 /* NB: parse_string will have eaten up all trailing spaces. */
146 *ret_buffer = buffer;
151 } /* int parse_option */
153 int escape_string (char *buffer, size_t buffer_size)
159 /* Check if we need to escape at all first */
160 temp = strpbrk (buffer, " \t\"\\");
164 temp = (char *) malloc (buffer_size);
167 memset (temp, 0, buffer_size);
172 for (i = 0; i < buffer_size; i++)
178 else if ((buffer[i] == '"') || (buffer[i] == '\\'))
180 if (j > (buffer_size - 4))
183 temp[j + 1] = buffer[i];
188 if (j > (buffer_size - 3))
195 assert ((j + 1) < buffer_size);
199 sstrncpy (buffer, temp, buffer_size);
202 } /* int escape_string */
204 /* vim: set sw=2 ts=8 tw=78 et : */