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