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