2 * collectd - src/utils_cmds.h
3 * Copyright (C) 2016 Sebastian 'tokkee' Harl
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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.
24 * Sebastian 'tokkee' Harl <sh at tokkee.org>
28 #define UTILS_CMDS_H 1
39 #define CMD_TO_STRING(type) \
40 ((type) == CMD_FLUSH) ? "FLUSH" \
41 : ((type) == CMD_PUTVAL) ? "PUTVAL" \
49 identifier_t *identifiers;
50 size_t identifiers_num;
54 /* The raw identifier as provided by the user. */
57 /* An array of the fully parsed identifier and all value lists, and their
58 * options as provided by the user. */
68 * The representation of a fully parsed command.
83 * Status codes describing the parse result.
89 CMD_UNKNOWN_COMMAND = -3,
91 /* Not necessarily fatal errors. */
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.
105 void (*cb) (void *, cmd_status_t, const char *, va_list);
107 } cmd_error_handler_t;
114 * Reports an error via the specified error handler (if set).
116 void cmd_error (cmd_status_t status, cmd_error_handler_t *err,
117 const char *format, ...);
124 * Parse a command string and populate a command object.
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.
132 * CMD_OK on success or the respective error code otherwise.
134 cmd_status_t cmd_parse (char *buffer,
135 cmd_t *ret_cmd, cmd_error_handler_t *err);
137 cmd_status_t cmd_parsev (size_t argc, char **argv,
138 cmd_t *ret_cmd, cmd_error_handler_t *err);
140 void cmd_destroy (cmd_t *cmd);
147 * Parses a command option which must be of the form:
148 * name=value with \ and spaces
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.
157 * CMD_OK on success or an error code otherwise.
158 * CMD_NO_OPTION if `field' does not represent an option at all (missing
161 cmd_status_t cmd_parse_option (char *field,
162 char **ret_key, char **ret_value, cmd_error_handler_t *err);
169 * An error callback writing the message to an open file handle using the
170 * format expected by the unixsock or exec plugins.
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
180 void cmd_error_fh (void *ud, cmd_status_t status,
181 const char *format, va_list ap);
183 #endif /* UTILS_CMDS_H */