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