2 * collectd - src/utils_cmds.c
3 * Copyright (C) 2016 Sebastian 'tokkee' Harl
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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.
24 * Sebastian 'tokkee' Harl <sh at tokkee.org>
27 #include "utils_cmds.h"
28 #include "utils_cmd_putval.h"
29 #include "utils_parse_option.h"
30 #include "daemon/common.h"
39 void cmd_error (cmd_status_t status, cmd_error_handler_t *err,
40 const char *format, ...)
44 if ((err == NULL) || (err->cb == NULL))
47 va_start (ap, format);
48 err->cb (err->ud, status, format, ap);
50 } /* void cmd_error */
52 cmd_status_t cmd_parse (char *buffer,
53 cmd_t *ret_cmd, cmd_error_handler_t *err)
58 if ((buffer == NULL) || (ret_cmd == NULL))
61 cmd_error (CMD_ERROR, err, "Invalid arguments to cmd_parse.");
65 if ((status = parse_string (&buffer, &command)) != 0)
67 cmd_error (CMD_PARSE_ERROR, err,
68 "Failed to extract command from `%s'.", buffer);
69 return (CMD_PARSE_ERROR);
71 assert (command != NULL);
73 memset (ret_cmd, 0, sizeof (*ret_cmd));
74 if (strcasecmp ("PUTVAL", command) == 0)
76 ret_cmd->type = CMD_PUTVAL;
77 return cmd_parse_putval (buffer, &ret_cmd->cmd.putval, err);
81 ret_cmd->type = CMD_UNKNOWN;
82 cmd_error (CMD_UNKNOWN_COMMAND, err,
83 "Unknown command `%s'.", command);
84 return (CMD_UNKNOWN_COMMAND);
88 } /* cmd_status_t cmd_parse */
90 void cmd_destroy (cmd_t *cmd)
101 cmd_destroy_putval (&cmd->cmd.putval);
104 } /* void cmd_destroy */
106 void cmd_error_fh (void *ud, cmd_status_t status,
107 const char *format, va_list ap)
113 if (status == CMD_OK)
116 vsnprintf (buf, sizeof(buf), format, ap);
117 buf[sizeof (buf) - 1] = '\0';
118 if (fprintf (fh, "%i %s\n", code, buf) < 0)
121 WARNING ("utils_cmds: failed to write to file-handle #%i: %s",
122 fileno (fh), sstrerror (errno, errbuf, sizeof (errbuf)));
127 } /* void cmd_error_fh */
129 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */