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