2 * collectd - src/utils_cmds.c
3 * Copyright (C) 2008 Florian Forster
4 * Copyright (C) 2016 Sebastian 'tokkee' Harl
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
25 * Florian octo Forster <octo at collectd.org>
26 * Sebastian 'tokkee' Harl <sh at tokkee.org>
29 #include "utils_cmds.h"
30 #include "utils_cmd_flush.h"
31 #include "utils_cmd_getval.h"
32 #include "utils_cmd_listval.h"
33 #include "utils_cmd_putval.h"
34 #include "utils_parse_option.h"
35 #include "daemon/common.h"
40 static cmd_options_t default_options = {
41 /* identifier_default_host = */ NULL,
45 * private helper functions
48 static cmd_status_t cmd_split (char *buffer,
49 size_t *ret_len, char ***ret_fields,
50 cmd_error_handler_t *err)
53 bool in_field, in_quotes;
60 for (char *string = buffer; *string != '\0'; ++string)
62 /* Make a quick worst-case estimate of the number of fields by
63 * counting spaces and ignoring quotation marks. */
64 if (!isspace ((int)*string))
78 /* fields will be NULL-terminated */
79 fields = malloc ((estimate + 1) * sizeof (*fields));
81 cmd_error (CMD_ERROR, err, "malloc failed.");
95 assert (len < estimate); \
96 fields[len] = field; \
105 for (char *string = buffer; *string != '\0'; string++)
107 if (isspace ((int)string[0]))
118 else if (string[0] == '"')
120 /* Note: Two consecutive quoted fields not separated by space are
121 * treated as different fields. This is the collectd 5.x behavior
122 * around splitting fields. */
126 /* end of quoted field */
127 if (! in_field) /* empty quoted string */
135 /* if (! in_field): add new field on next iteration
136 * else: quoted string following an unquoted string (one field)
137 * in either case: skip quotation mark */
140 else if ((string[0] == '\\') && in_quotes)
142 /* Outside of quotes, a backslash is a regular character (mostly
143 * for backward compatibility). */
145 if (string[1] == '\0')
148 cmd_error (CMD_PARSE_ERROR, err,
149 "Backslash at end of string.");
150 return (CMD_PARSE_ERROR);
153 /* un-escape the next character; skip backslash */
168 cmd_error (CMD_PARSE_ERROR, err, "Unterminated quoted string.");
169 return (CMD_PARSE_ERROR);
178 if (ret_fields != NULL)
179 *ret_fields = fields;
183 } /* int cmd_split */
189 void cmd_error (cmd_status_t status, cmd_error_handler_t *err,
190 const char *format, ...)
194 if ((err == NULL) || (err->cb == NULL))
197 va_start (ap, format);
198 err->cb (err->ud, status, format, ap);
200 } /* void cmd_error */
202 cmd_status_t cmd_parsev (size_t argc, char **argv, cmd_t *ret_cmd,
203 const cmd_options_t *opts, cmd_error_handler_t *err)
205 char *command = NULL;
208 if ((argc < 1) || (argv == NULL) || (ret_cmd == NULL))
211 cmd_error (CMD_ERROR, err, "Missing command.");
216 opts = &default_options;
218 memset (ret_cmd, 0, sizeof (*ret_cmd));
220 if (strcasecmp ("FLUSH", command) == 0)
222 ret_cmd->type = CMD_FLUSH;
223 status = cmd_parse_flush (argc - 1, argv + 1,
224 &ret_cmd->cmd.flush, opts, err);
226 else if (strcasecmp ("GETVAL", command) == 0)
228 ret_cmd->type = CMD_GETVAL;
229 status = cmd_parse_getval (argc - 1, argv + 1,
230 &ret_cmd->cmd.getval, opts, err);
232 else if (strcasecmp ("LISTVAL", command) == 0)
234 ret_cmd->type = CMD_LISTVAL;
235 status = cmd_parse_listval (argc - 1, argv + 1,
236 &ret_cmd->cmd.listval, opts, err);
238 else if (strcasecmp ("PUTVAL", command) == 0)
240 ret_cmd->type = CMD_PUTVAL;
241 status = cmd_parse_putval (argc - 1, argv + 1,
242 &ret_cmd->cmd.putval, opts, err);
246 ret_cmd->type = CMD_UNKNOWN;
247 cmd_error (CMD_UNKNOWN_COMMAND, err,
248 "Unknown command `%s'.", command);
249 return (CMD_UNKNOWN_COMMAND);
252 if (status != CMD_OK)
253 ret_cmd->type = CMD_UNKNOWN;
255 } /* cmd_status_t cmd_parsev */
257 cmd_status_t cmd_parse (char *buffer, cmd_t *ret_cmd,
258 const cmd_options_t *opts, cmd_error_handler_t *err)
260 char **fields = NULL;
261 size_t fields_num = 0;
264 if ((status = cmd_split (buffer, &fields_num, &fields, err)) != CMD_OK)
267 status = cmd_parsev (fields_num, fields, ret_cmd, opts, err);
270 } /* cmd_status_t cmd_parse */
272 void cmd_destroy (cmd_t *cmd)
283 cmd_destroy_flush (&cmd->cmd.flush);
286 cmd_destroy_getval (&cmd->cmd.getval);
289 cmd_destroy_listval (&cmd->cmd.listval);
292 cmd_destroy_putval (&cmd->cmd.putval);
295 } /* void cmd_destroy */
297 cmd_status_t cmd_parse_option (char *field,
298 char **ret_key, char **ret_value, cmd_error_handler_t *err)
305 cmd_error (CMD_ERROR, err, "Invalid argument to cmd_parse_option.");
310 /* Look for the equal sign. */
311 while (isalnum ((int)value[0]) || (value[0] == '_') || (value[0] == ':'))
313 if ((value[0] != '=') || (value == key))
315 /* Whether this is a fatal error is up to the caller. */
316 return (CMD_NO_OPTION);
323 if (ret_value != NULL)
327 } /* cmd_status_t cmd_parse_option */
329 void cmd_error_fh (void *ud, cmd_status_t status,
330 const char *format, va_list ap)
336 if (status == CMD_OK)
339 vsnprintf (buf, sizeof(buf), format, ap);
340 buf[sizeof (buf) - 1] = '\0';
341 if (fprintf (fh, "%i %s\n", code, buf) < 0)
344 WARNING ("utils_cmds: failed to write to file-handle #%i: %s",
345 fileno (fh), sstrerror (errno, errbuf, sizeof (errbuf)));
350 } /* void cmd_error_fh */
352 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */