1 /**
2 * collectd - src/utils_parse_option.c
3 * Copyright (C) 2008 Florian Forster
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Florian octo Forster <octo at collectd.org>
25 **/
27 #include "collectd.h"
28 #include "common.h"
29 #include "plugin.h"
30 #include "utils_parse_option.h"
32 int parse_string (char **ret_buffer, char **ret_string)
33 {
34 char *buffer;
35 char *string;
37 buffer = *ret_buffer;
39 /* Eat up leading spaces. */
40 string = buffer;
41 while (isspace ((int) *string))
42 string++;
43 if (*string == 0)
44 return (1);
46 /* A quoted string */
47 if (*string == '"')
48 {
49 char *dst;
51 string++;
52 if (*string == 0)
53 return (1);
55 dst = string;
56 buffer = string;
57 while ((*buffer != '"') && (*buffer != 0))
58 {
59 /* Un-escape backslashes */
60 if (*buffer == '\\')
61 {
62 buffer++;
63 /* Catch a backslash at the end of buffer */
64 if (*buffer == 0)
65 return (-1);
66 }
67 *dst = *buffer;
68 buffer++;
69 dst++;
70 }
71 /* No quote sign has been found */
72 if (*buffer == 0)
73 return (-1);
75 *dst = 0;
76 dst++;
77 *buffer = 0;
78 buffer++;
80 /* Check for trailing spaces. */
81 if ((*buffer != 0) && !isspace ((int) *buffer))
82 return (-1);
83 }
84 else /* an unquoted string */
85 {
86 buffer = string;
87 while ((*buffer != 0) && !isspace ((int) *buffer))
88 buffer++;
89 if (*buffer != 0)
90 {
91 *buffer = 0;
92 buffer++;
93 }
94 }
96 /* Eat up trailing spaces */
97 while (isspace ((int) *buffer))
98 buffer++;
100 *ret_buffer = buffer;
101 *ret_string = string;
103 return (0);
104 } /* int parse_string */
106 /*
107 * parse_option
108 * ------------
109 * Parses an ``option'' as used with the unixsock and exec commands. An
110 * option is of the form:
111 * name0="value"
112 * name1="value with \"quotes\""
113 * name2="value \\ backslash"
114 * However, if the value does *not* contain a space character, you can skip
115 * the quotes.
116 */
117 int parse_option (char **ret_buffer, char **ret_key, char **ret_value)
118 {
119 char *buffer;
120 char *key;
121 char *value;
122 int status;
124 buffer = *ret_buffer;
126 /* Eat up leading spaces */
127 key = buffer;
128 while (isspace ((int) *key))
129 key++;
130 if (*key == 0)
131 return (1);
133 /* Look for the equal sign */
134 buffer = key;
135 while (isalnum ((int) *buffer) || *buffer == '_' || *buffer == ':')
136 buffer++;
137 if ((*buffer != '=') || (buffer == key))
138 return (1);
139 *buffer = 0;
140 buffer++;
141 /* Empty values must be written as "" */
142 if (isspace ((int) *buffer) || (*buffer == 0))
143 return (-1);
145 status = parse_string (&buffer, &value);
146 if (status != 0)
147 return (-1);
149 /* NB: parse_string will have eaten up all trailing spaces. */
151 *ret_buffer = buffer;
152 *ret_key = key;
153 *ret_value = value;
155 return (0);
156 } /* int parse_option */
158 /* vim: set sw=2 ts=8 tw=78 et : */