Add the "list_graphs" action.
authorFlorian Forster <ff@octo.it>
Fri, 30 Apr 2010 13:35:40 +0000 (15:35 +0200)
committerFlorian Forster <octo@leeloo.lan.home.verplant.org>
Fri, 30 Apr 2010 13:35:40 +0000 (15:35 +0200)
Makefile
action_list_graphs.c [new file with mode: 0644]
action_list_graphs.h [new file with mode: 0644]
fcgi_test.c

index 970d85f..7360c5a 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -15,10 +15,12 @@ graph_list.o: graph_list.c graph_list.h
 
 utils_params.o: utils_params.c utils_params.h
 
+action_list_graphs.o: action_list_graphs.c action_list_graphs.h
+
 test: test.c utils_params.o
 
 fcgi_test: LDLIBS = -lfcgi
-fcgi_test: fcgi_test.c common.o graph_list.o utils_params.o
+fcgi_test: fcgi_test.c common.o graph_list.o utils_params.o action_list_graphs.o
 
 .PHONY: clean
 
diff --git a/action_list_graphs.c b/action_list_graphs.c
new file mode 100644 (file)
index 0000000..8c78ac1
--- /dev/null
@@ -0,0 +1,57 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+
+#include <fcgiapp.h>
+#include <fcgi_stdio.h>
+
+#include "graph_list.h"
+
+static int print_graph (const graph_list_t *gl, void *user_data) /* {{{ */
+{
+  _Bool *first;
+
+  if ((gl == NULL) || (user_data == NULL))
+    return (EINVAL);
+
+  first = (_Bool *) user_data;
+  if (!*first)
+    printf (",\n");
+  *first = 0;
+
+  printf (" {");
+
+  printf ("\"host\":\"%s\"", gl->host);
+      
+  printf (",\"plugin\":\"%s\"", gl->plugin);
+  if (gl->plugin_instance != NULL)
+    printf (",\"plugin_instance\":\"%s\"", gl->plugin_instance);
+  else
+    printf (",\"plugin_instance\":null");
+
+  printf (",\"type\":\"%s\"", gl->type);
+  if (gl->type_instance != NULL)
+    printf (",\"type_instance\":\"%s\"", gl->type_instance);
+  else
+    printf (",\"type_instance\":null");
+
+  printf ("}");
+
+  return (0);
+} /* }}} int print_graph */
+
+int action_list_graphs (void) /* {{{ */
+{
+  _Bool first = 1;
+
+  printf ("Content-Type: text/plain\n\n");
+
+  gl_update ();
+
+  printf ("[\n");
+  gl_foreach (print_graph, /* user_data = */ &first);
+  printf ("\n]");
+} /* }}} int action_list_graphs */
+
+/* vim: set sw=2 sts=2 et fdm=marker : */
diff --git a/action_list_graphs.h b/action_list_graphs.h
new file mode 100644 (file)
index 0000000..dd7edf9
--- /dev/null
@@ -0,0 +1,7 @@
+#ifndef ACTION_LIST_GRAPHS_H
+#define ACTION_LIST_GRAPHS_H 1
+
+int action_list_graphs (void);
+
+#endif /* ACTION_LIST_GRAPHS_H */
+/* vim: set sw=2 sts=2 et fdm=marker : */
index e0d3035..a6ca107 100644 (file)
@@ -16,6 +16,8 @@
 #include "graph_list.h"
 #include "utils_params.h"
 
+#include "action_list_graphs.h"
+
 struct str_array_s
 {
   char **ptr;
@@ -72,7 +74,7 @@ static int array_add (const char *entry, void *user_data) /* {{{ */
   return (0);
 } /* }}} int array_add */
 
-static int print_graph (const graph_list_t *gl, void *user_data)
+static int print_graph (const graph_list_t *gl, void *user_data) /* {{{ */
 {
   if (gl == NULL)
     return (EINVAL);
@@ -131,6 +133,10 @@ 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 ());