2 * collectd - src/utils_cmd_listval.c
3 * Copyright (C) 2008 Florian octo Forster
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 * Florian octo Forster <octo at collectd.org>
32 #include "utils_cmd_listval.h"
33 #include "utils_cache.h"
34 #include "utils_parse_option.h"
36 cmd_status_t cmd_parse_listval (size_t argc, char **argv,
37 cmd_listval_t *ret_listval __attribute__((unused)),
38 const cmd_options_t *opts __attribute__((unused)),
39 cmd_error_handler_t *err)
43 cmd_error (CMD_PARSE_ERROR, err,
44 "Garbage after end of command: `%s'.", argv[0]);
45 return (CMD_PARSE_ERROR);
49 } /* cmd_status_t cmd_parse_listval */
51 #define free_everything_and_return(status) do { \
52 for (size_t j = 0; j < number; j++) { \
61 #define print_to_socket(fh, ...) \
63 if (fprintf (fh, __VA_ARGS__) < 0) { \
65 WARNING ("handle_listval: failed to write to socket #%i: %s", \
66 fileno (fh), sstrerror (errno, errbuf, sizeof (errbuf))); \
67 free_everything_and_return (CMD_ERROR); \
72 cmd_status_t cmd_handle_listval (FILE *fh, char *buffer)
74 cmd_error_handler_t err = { cmd_error_fh, fh };
79 cdtime_t *times = NULL;
82 DEBUG ("utils_cmd_listval: handle_listval (fh = %p, buffer = %s);",
85 if ((status = cmd_parse (buffer, &cmd, NULL, &err)) != CMD_OK)
87 if (cmd.type != CMD_LISTVAL)
89 cmd_error (CMD_UNKNOWN_COMMAND, &err,
90 "Unexpected command: `%s'.", CMD_TO_STRING (cmd.type));
91 free_everything_and_return (CMD_UNKNOWN_COMMAND);
94 status = uc_get_names (&names, ×, &number);
97 DEBUG ("command listval: uc_get_names failed with status %i", status);
98 cmd_error (CMD_ERROR, &err, "uc_get_names failed.");
99 free_everything_and_return (CMD_ERROR);
102 print_to_socket (fh, "%i Value%s found\n",
103 (int) number, (number == 1) ? "" : "s");
104 for (size_t i = 0; i < number; i++)
105 print_to_socket (fh, "%.3f %s\n", CDTIME_T_TO_DOUBLE (times[i]),
108 free_everything_and_return (CMD_OK);
109 } /* cmd_status_t cmd_handle_listval */
111 void cmd_destroy_listval (cmd_listval_t *listval __attribute__((unused)))
114 } /* void cmd_destroy_listval */
116 /* vim: set sw=2 sts=2 ts=8 : */