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);
70 *dst = 0;
71 dst++;
72 *buffer = 0;
73 buffer++;
75 /* Check for trailing spaces. */
76 if ((*buffer != 0) && !isspace ((int) *buffer))
77 return (-1);
78 }
79 else /* an unquoted string */
80 {
81 buffer = string;
82 while ((*buffer != 0) && !isspace ((int) *buffer))
83 buffer++;
84 if (*buffer != 0)
85 {
86 *buffer = 0;
87 buffer++;
88 }
89 }
91 /* Eat up trailing spaces */
92 while (isspace ((int) *buffer))
93 buffer++;
95 *ret_buffer = buffer;
96 *ret_string = string;
98 return (0);
99 } /* int parse_string */
101 /*
102 * parse_option
103 * ------------
104 * Parses an ``option'' as used with the unixsock and exec commands. An
105 * option is of the form:
106 * name0="value"
107 * name1="value with \"quotes\""
108 * name2="value \\ backslash"
109 * However, if the value does *not* contain a space character, you can skip
110 * the quotes.
111 */
112 int parse_option (char **ret_buffer, char **ret_key, char **ret_value)
113 {
114 char *buffer;
115 char *key;
116 char *value;
117 int status;
119 buffer = *ret_buffer;
121 /* Eat up leading spaces */
122 key = buffer;
123 while (isspace ((int) *key))
124 key++;
125 if (*key == 0)
126 return (1);
128 /* Look for the equal sign */
129 buffer = key;
130 while (isalnum ((int) *buffer))
131 buffer++;
132 if ((*buffer != '=') || (buffer == key))
133 return (1);
134 *buffer = 0;
135 buffer++;
136 /* Empty values must be written as "" */
137 if (isspace ((int) *buffer) || (*buffer == 0))
138 return (-1);
140 status = parse_string (&buffer, &value);
141 if (status != 0)
142 return (-1);
144 /* NB: parse_string will have eaten up all trailing spaces. */
146 *ret_buffer = buffer;
147 *ret_key = key;
148 *ret_value = value;
150 return (0);
151 } /* int parse_option */
153 /* vim: set sw=2 ts=8 tw=78 et : */