1 /**
2 * collectd - src/utils_cmds.h
3 * Copyright (C) 2016 Sebastian 'tokkee' Harl
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 * Sebastian 'tokkee' Harl <sh at tokkee.org>
25 **/
27 #ifndef UTILS_CMDS_H
28 #define UTILS_CMDS_H 1
30 #include "plugin.h"
32 #include <stdarg.h>
34 typedef enum {
35 CMD_UNKNOWN = 0,
36 CMD_FLUSH = 1,
37 CMD_GETVAL = 2,
38 CMD_LISTVAL = 3,
39 CMD_PUTVAL = 4,
40 } cmd_type_t;
41 #define CMD_TO_STRING(type) \
42 ((type) == CMD_FLUSH) ? "FLUSH" : ((type) == CMD_GETVAL) \
43 ? "GETVAL" \
44 : ((type) == CMD_LISTVAL) \
45 ? "LISTVAL" \
46 : ((type) == CMD_PUTVAL) \
47 ? "PUTVAL" \
48 : "UNKNOWN"
50 typedef struct {
51 double timeout;
53 char **plugins;
54 size_t plugins_num;
55 identifier_t *identifiers;
56 size_t identifiers_num;
57 } cmd_flush_t;
59 typedef struct {
60 char *raw_identifier;
61 identifier_t identifier;
62 } cmd_getval_t;
64 typedef struct {
65 } cmd_listval_t;
67 typedef struct {
68 /* The raw identifier as provided by the user. */
69 char *raw_identifier;
71 /* An array of the fully parsed identifier and all value lists, and their
72 * options as provided by the user. */
73 value_list_t *vl;
74 size_t vl_num;
75 } cmd_putval_t;
77 /*
78 * NAME
79 * cmd_t
80 *
81 * DESCRIPTION
82 * The representation of a fully parsed command.
83 */
84 typedef struct {
85 cmd_type_t type;
86 union {
87 cmd_flush_t flush;
88 cmd_getval_t getval;
89 cmd_listval_t listval;
90 cmd_putval_t putval;
91 } cmd;
92 } cmd_t;
94 /*
95 * NAME
96 * cmd_options_t
97 *
98 * DESCRIPTIONS
99 * Optional settings for tuning the parser behavior.
100 */
101 typedef struct {
102 /* identifier_default_host: If non-NULL, the hostname is optional and will
103 * default to the specified value. */
104 char *identifier_default_host;
105 } cmd_options_t;
107 /*
108 * NAME
109 * cmd_status_t
110 *
111 * DESCRIPTION
112 * Status codes describing the parse result.
113 */
114 typedef enum {
115 CMD_OK = 0,
116 CMD_ERROR = -1,
117 CMD_PARSE_ERROR = -2,
118 CMD_UNKNOWN_COMMAND = -3,
120 /* Not necessarily fatal errors. */
121 CMD_NO_OPTION = 1,
122 } cmd_status_t;
124 /*
125 * NAME
126 * cmd_error_handler_t
127 *
128 * DESCRIPTION
129 * An error handler describes a callback to be invoked when the parser
130 * encounters an error. The user data pointer will be passed to the callback
131 * as the first argument.
132 */
133 typedef struct {
134 void (*cb)(void *, cmd_status_t, const char *, va_list);
135 void *ud;
136 } cmd_error_handler_t;
138 /*
139 * NAME:
140 * cmd_error
141 *
142 * DESCRIPTION
143 * Reports an error via the specified error handler (if set).
144 */
145 void cmd_error(cmd_status_t status, cmd_error_handler_t *err,
146 const char *format, ...);
148 /*
149 * NAME
150 * cmd_parse
151 *
152 * DESCRIPTION
153 * Parse a command string and populate a command object.
154 *
155 * PARAMETERS
156 * `buffer' The command string to be parsed.
157 * `ret_cmd' The parse result will be stored at this location.
158 * `opts' Parser options. If NULL, defaults will be used.
159 * `err' An optional error handler to invoke on error.
160 *
161 * RETURN VALUE
162 * CMD_OK on success or the respective error code otherwise.
163 */
164 cmd_status_t cmd_parse(char *buffer, cmd_t *ret_cmd, const cmd_options_t *opts,
165 cmd_error_handler_t *err);
167 cmd_status_t cmd_parsev(size_t argc, char **argv, cmd_t *ret_cmd,
168 const cmd_options_t *opts, cmd_error_handler_t *err);
170 void cmd_destroy(cmd_t *cmd);
172 /*
173 * NAME
174 * cmd_parse_option
175 *
176 * DESCRIPTION
177 * Parses a command option which must be of the form:
178 * name=value with \ and spaces
179 *
180 * PARAMETERS
181 * `field' The parsed input field with any quotes removed and special
182 * characters unescaped.
183 * `ret_key' The parsed key will be stored at this location.
184 * `ret_value' The parsed value will be stored at this location.
185 *
186 * RETURN VALUE
187 * CMD_OK on success or an error code otherwise.
188 * CMD_NO_OPTION if `field' does not represent an option at all (missing
189 * equal sign).
190 */
191 cmd_status_t cmd_parse_option(char *field, char **ret_key, char **ret_value,
192 cmd_error_handler_t *err);
194 /*
195 * NAME
196 * cmd_error_fh
197 *
198 * DESCRIPTION
199 * An error callback writing the message to an open file handle using the
200 * format expected by the unixsock or exec plugins.
201 *
202 * PARAMETERS
203 * `ud' Error handler user-data pointer. This must be an open
204 * file-handle (FILE *).
205 * `status' The error status code.
206 * `format' Printf-style format string.
207 * `ap' Variable argument list providing the arguments for the format
208 * string.
209 */
210 void cmd_error_fh(void *ud, cmd_status_t status, const char *format,
211 va_list ap);
213 #endif /* UTILS_CMDS_H */