Debian Codegraph @

src/utils/cmds/cmds.h

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