X-Git-Url: https://git.octo.it/?p=collection4.git;a=blobdiff_plain;f=test.fcgi.c;h=097c55cfee70700ea48e6622686f2bacc445c36f;hp=a6ca107217399ca4bc7009de77dfe1dc72bf5246;hb=af2ed61b280e1ec169aba2bcb9df30ac76b006d2;hpb=5a31f034b6c6e0056109f9f6766ccc674a7d1b95 diff --git a/test.fcgi.c b/test.fcgi.c index a6ca107..097c55c 100644 --- a/test.fcgi.c +++ b/test.fcgi.c @@ -9,115 +9,51 @@ #include #include -#include -#include - #include "common.h" #include "graph_list.h" #include "utils_params.h" +#include "action_graph.h" #include "action_list_graphs.h" -struct str_array_s -{ - char **ptr; - size_t size; -}; -typedef struct str_array_s str_array_t; - -static str_array_t *array_alloc (void) /* {{{ */ -{ - str_array_t *a; - - a = malloc (sizeof (*a)); - if (a == NULL) - return (NULL); - - memset (a, 0, sizeof (*a)); - a->ptr = NULL; - a->size = 0; - - return (a); -} /* }}} str_array_t *array_alloc */ - -static void array_free (str_array_t *a) /* {{{ */ -{ - if (a == NULL) - return; - - free (a->ptr); - a->ptr = NULL; - a->size = 0; - - free (a); -} /* }}} void array_free */ - -static int array_add (const char *entry, void *user_data) /* {{{ */ -{ - str_array_t *a = user_data; - char **ptr; - - if ((entry == NULL) || (a == NULL)) - return (EINVAL); - - ptr = realloc (a->ptr, sizeof (*a->ptr) * (a->size + 1)); - if (ptr == NULL) - return (ENOMEM); - a->ptr = ptr; - ptr = a->ptr + a->size; - - *ptr = strdup (entry); - if (*ptr == NULL) - return (ENOMEM); - - a->size++; - return (0); -} /* }}} int array_add */ - -static int print_graph (const graph_list_t *gl, void *user_data) /* {{{ */ -{ - if (gl == NULL) - return (EINVAL); - - printf ("host = %s; plugin = %s;", gl->host, gl->plugin); - if (gl->plugin_instance != NULL) - printf (" plugin_instance = %s;", gl->plugin_instance); - printf (" type = %s;", gl->type); - if (gl->type_instance != NULL) - printf (" type_instance = %s;", gl->type_instance); - printf ("\n"); - - return (0); -} /* }}} int print_graph */ +/* Include this last, so the macro magic of doesn't interfere + * with our own header files. */ +#include +#include -static int get_graphs_list (char ***ret_graphs, /* {{{ */ - size_t *ret_graphs_num) +struct action_s { - gl_update (); - gl_foreach (print_graph, /* user_data = */ NULL); + const char *name; + int (*callback) (void); +}; +typedef struct action_s action_t; - return (0); -} /* }}} int get_graphs_list */ +static int action_usage (void); -static int action_hello (void) /* {{{ */ +static const action_t actions[] = { - printf ("Content-Type: text/plain\n\n"); - - get_graphs_list (NULL, NULL); + { "graph", action_graph }, + { "list_graphs", action_list_graphs }, + { "usage", action_usage } +}; +static const size_t actions_num = sizeof (actions) / sizeof (actions[0]); - return (0); -} /* }}} int action_hello */ static int action_usage (void) /* {{{ */ { + size_t i; + printf ("Content-Type: text/plain\n\n"); - fputs ("Usage:\n" + printf ("Usage:\n" "\n" " Available actions:\n" - "\n" - " * hello\n" - "\n", stdout); + "\n"); + + for (i = 0; i < actions_num; i++) + printf (" * %s\n", actions[i].name); + + printf ("\n"); return (0); } /* }}} int action_usage */ @@ -133,16 +69,16 @@ static int handle_request (void) /* {{{ */ { return (action_usage ()); } - else if (strcmp ("list_graphs", action) == 0) - { - return (action_list_graphs ()); - } - else if (strcmp ("hello", action) == 0) - { - return (action_hello ()); - } else { + size_t i; + + for (i = 0; i < actions_num; i++) + { + if (strcmp (action, actions[i].name) == 0) + return ((*actions[i].callback) ()); + } + return (action_usage ()); } } /* }}} int handle_request */