oconfig.c: Fix compiler warning.
[collection4.git] / test.fcgi.c
index a6ca107..097c55c 100644 (file)
 #include <unistd.h>
 #include <dirent.h>
 
-#include <fcgiapp.h>
-#include <fcgi_stdio.h>
-
 #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 <fcgi_stdio.h> doesn't interfere
+ * with our own header files. */
+#include <fcgiapp.h>
+#include <fcgi_stdio.h>
 
-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 */