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"
29 #include "utils_parse_option.h"
31 int parse_string (char **ret_buffer, char **ret_string)
32 {
33 char *buffer;
34 char *string;
36 buffer = *ret_buffer;
38 /* Eat up leading spaces. */
39 string = buffer;
40 while (isspace ((int) *string))
41 string++;
42 if (*string == 0)
43 return (1);
45 /* A quoted string */
46 if (*string == '"')
47 {
48 char *dst;
50 string++;
51 if (*string == 0)
52 return (1);
54 dst = string;
55 buffer = string;
56 while ((*buffer != '"') && (*buffer != 0))
57 {
58 /* Un-escape backslashes */
59 if (*buffer == '\\')
60 {
61 buffer++;
62 /* Catch a backslash at the end of buffer */
63 if (*buffer == 0)
64 return (-1);
65 }
66 *dst = *buffer;
67 buffer++;
68 dst++;
69 }
70 /* No quote sign has been found */
71 if (*buffer == 0)
72 return (-1);
74 *dst = 0;
75 dst++;
76 *buffer = 0;
77 buffer++;
79 /* Check for trailing spaces. */
80 if ((*buffer != 0) && !isspace ((int) *buffer))
81 return (-1);
82 }
83 else /* an unquoted string */
84 {
85 buffer = string;
86 while ((*buffer != 0) && !isspace ((int) *buffer))
87 buffer++;
88 if (*buffer != 0)
89 {
90 *buffer = 0;
91 buffer++;
92 }
93 }
95 /* Eat up trailing spaces */
96 while (isspace ((int) *buffer))
97 buffer++;
99 *ret_buffer = buffer;
100 *ret_string = string;
102 return (0);
103 } /* int parse_string */
105 /*
106 * parse_option
107 * ------------
108 * Parses an ``option'' as used with the unixsock and exec commands. An
109 * option is of the form:
110 * name0="value"
111 * name1="value with \"quotes\""
112 * name2="value \\ backslash"
113 * However, if the value does *not* contain a space character, you can skip
114 * the quotes.
115 */
116 int parse_option (char **ret_buffer, char **ret_key, char **ret_value)
117 {
118 char *buffer;
119 char *key;
120 char *value;
121 int status;
123 buffer = *ret_buffer;
125 /* Eat up leading spaces */
126 key = buffer;
127 while (isspace ((int) *key))
128 key++;
129 if (*key == 0)
130 return (1);
132 /* Look for the equal sign */
133 buffer = key;
134 while (isalnum ((int) *buffer) || *buffer == '_' || *buffer == ':')
135 buffer++;
136 if ((*buffer != '=') || (buffer == key))
137 return (1);
138 *buffer = 0;
139 buffer++;
140 /* Empty values must be written as "" */
141 if (isspace ((int) *buffer) || (*buffer == 0))
142 return (-1);
144 status = parse_string (&buffer, &value);
145 if (status != 0)
146 return (-1);
148 /* NB: parse_string will have eaten up all trailing spaces. */
150 *ret_buffer = buffer;
151 *ret_key = key;
152 *ret_value = value;
154 return (0);
155 } /* int parse_option */
157 /* vim: set sw=2 ts=8 tw=78 et : */