action_list_graphs.c: Implement real simple HTML output.
authorFlorian Forster <ff@octo.it>
Sun, 2 May 2010 07:11:29 +0000 (09:11 +0200)
committerFlorian Forster <octo@leeloo.lan.home.verplant.org>
Sun, 2 May 2010 07:11:29 +0000 (09:11 +0200)
action_list_graphs.c

index 8c78ac1..9fc8f45 100644 (file)
@@ -7,8 +7,9 @@
 #include <fcgi_stdio.h>
 
 #include "graph_list.h"
+#include "utils_params.h"
 
-static int print_graph (const graph_list_t *gl, void *user_data) /* {{{ */
+static int print_graph_json (const graph_list_t *gl, void *user_data) /* {{{ */
 {
   _Bool *first;
 
@@ -39,19 +40,63 @@ static int print_graph (const graph_list_t *gl, void *user_data) /* {{{ */
   printf ("}");
 
   return (0);
-} /* }}} int print_graph */
+} /* }}} int print_graph_json */
 
-int action_list_graphs (void) /* {{{ */
+static int print_graph_html (const graph_list_t *gl,
+    void __attribute__((unused)) *user_data)
 {
-  _Bool first = 1;
+  if (gl == NULL)
+    return (EINVAL);
 
-  printf ("Content-Type: text/plain\n\n");
+  printf ("<li>%s/%s", gl->host, gl->plugin);
+  if (gl->plugin_instance != NULL)
+    printf ("-%s", gl->plugin_instance);
+  printf ("/%s", gl->type);
+  if (gl->type_instance != NULL)
+    printf ("-%s", gl->type_instance);
+  printf ("</li>\n");
 
-  gl_update ();
+  return (0);
+}
+
+static int list_graphs_json (void) /* {{{ */
+{
+  _Bool first = 1;
+
+  printf ("Content-Type: application/json\n\n");
 
   printf ("[\n");
-  gl_foreach (print_graph, /* user_data = */ &first);
+  gl_foreach (print_graph_json, /* user_data = */ &first);
   printf ("\n]");
+
+  return (0);
+} /* }}} int list_graphs_json */
+
+static int list_graphs_html (void) /* {{{ */
+{
+  printf ("Content-Type: text/html\n\n");
+
+  printf ("<ul>\n");
+  gl_foreach (print_graph_html, /* user_data = */ NULL);
+  printf ("</ul>\n");
+
+  return (0);
+} /* }}} int list_graphs_html */
+
+int action_list_graphs (void) /* {{{ */
+{
+  const char *format;
+
+  gl_update ();
+
+  format = param ("format");
+  if (format == NULL)
+    format = "html";
+
+  if (strcmp ("json", format) == 0)
+    return (list_graphs_json ());
+  else
+    return (list_graphs_html ());
 } /* }}} int action_list_graphs */
 
 /* vim: set sw=2 sts=2 et fdm=marker : */