1 /**
2 * collectd - src/utils_parse_option.c
3 * Copyright (C) 2008 Florian Forster
4 *
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.
8 *
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.
13 *
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
17 *
18 * Author:
19 * Florian octo Forster <octo at verplant.org>
20 **/
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25 #include "utils_parse_option.h"
27 int parse_string (char **ret_buffer, char **ret_string)
28 {
29 char *buffer;
30 char *string;
32 buffer = *ret_buffer;
34 /* Eat up leading spaces. */
35 string = buffer;
36 while (isspace ((int) *string))
37 string++;
38 if (*string == 0)
39 return (1);
41 /* A quoted string */
42 if (*string == '"')
43 {
44 char *dst;
46 string++;
47 if (*string == 0)
48 return (1);
50 dst = string;
51 buffer = string;
52 while ((*buffer != '"') && (*buffer != 0))
53 {
54 /* Un-escape backslashes */
55 if (*buffer == '\\')
56 {
57 buffer++;
58 /* Catch a backslash at the end of buffer */
59 if (*buffer == 0)
60 return (-1);
61 }
62 *dst = *buffer;
63 buffer++;
64 dst++;
65 }
66 /* No quote sign has been found */
67 if (*buffer == 0)
68 return (-1);
69 *buffer = 0;
70 buffer++;
72 /* Check for trailing spaces. */
73 if ((*buffer != 0) && !isspace ((int) *buffer))
74 return (-1);
75 }
76 else /* an unquoted string */
77 {
78 buffer = string;
79 while ((*buffer != 0) && !isspace ((int) *buffer))
80 buffer++;
81 if (*buffer != 0)
82 {
83 *buffer = 0;
84 buffer++;
85 }
86 }
88 /* Eat up trailing spaces */
89 while (isspace ((int) *buffer))
90 buffer++;
92 *ret_buffer = buffer;
93 *ret_string = string;
95 return (0);
96 } /* int parse_string */
98 /*
99 * parse_option
100 * ------------
101 * Parses an ``option'' as used with the unixsock and exec commands. An
102 * option is of the form:
103 * name0="value"
104 * name1="value with \"quotes\""
105 * name2="value \\ backslash"
106 * However, if the value does *not* contain a space character, you can skip
107 * the quotes.
108 */
109 int parse_option (char **ret_buffer, char **ret_key, char **ret_value)
110 {
111 char *buffer;
112 char *key;
113 char *value;
114 int status;
116 buffer = *ret_buffer;
118 /* Eat up leading spaces */
119 key = buffer;
120 while (isspace ((int) *key))
121 key++;
122 if (*key == 0)
123 return (1);
125 /* Look for the equal sign */
126 buffer = key;
127 while (isalnum ((int) *buffer))
128 buffer++;
129 if ((*buffer != '=') || (buffer == key))
130 return (1);
131 *buffer = 0;
132 buffer++;
133 /* Empty values must be written as "" */
134 if (isspace ((int) *buffer) || (*buffer == 0))
135 return (-1);
137 status = parse_string (&buffer, &value);
138 if (status != 0)
139 return (-1);
141 /* NB: parse_string will have eaten up all trailing spaces. */
143 *ret_buffer = buffer;
144 *ret_key = key;
145 *ret_value = value;
147 return (0);
148 } /* int parse_option */
150 /* vim: set sw=2 ts=8 tw=78 et : */