1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <errno.h>
6 #include "action_list_graphs.h"
7 #include "graph.h"
8 #include "graph_list.h"
9 #include "utils_params.h"
11 #include <fcgiapp.h>
12 #include <fcgi_stdio.h>
14 static int print_graph_inst_json (__attribute__((unused)) graph_config_t *cfg, /* {{{ */
15 graph_instance_t *inst,
16 void *user_data)
17 {
18 _Bool *first;
19 graph_ident_t *ident;
20 char *json;
22 first = user_data;
24 ident = inst_get_selector (inst);
25 if (ident == NULL)
26 return (-1);
28 json = ident_to_json (ident);
29 if (json == NULL)
30 {
31 ident_destroy (ident);
32 return (ENOMEM);
33 }
35 if (*first)
36 printf ("%s", json);
37 else
38 printf (",\n%s", json);
40 *first = 0;
42 ident_destroy (ident);
43 return (0);
44 } /* }}} int print_graph_inst_json */
46 static int print_graph_json (graph_config_t *cfg, /* {{{ */
47 void *user_data)
48 {
49 return (gl_graph_instance_get_all (cfg, print_graph_inst_json, user_data));
50 } /* }}} int print_graph_json */
52 static int list_graphs_json (void) /* {{{ */
53 {
54 _Bool first = 1;
56 printf ("Content-Type: application/json\n\n");
58 printf ("[\n");
59 gl_graph_get_all (print_graph_json, /* user_data = */ &first);
60 printf ("\n]");
62 return (0);
63 } /* }}} int list_graphs_json */
65 static int print_graph_inst_html (graph_config_t *cfg, /* {{{ */
66 graph_instance_t *inst,
67 __attribute__((unused)) void *user_data)
68 {
69 char params[1024];
70 char desc[1024];
72 memset (params, 0, sizeof (params));
73 inst_get_params (cfg, inst, params, sizeof (params));
75 memset (desc, 0, sizeof (desc));
76 inst_describe (cfg, inst, desc, sizeof (desc));
78 printf ("<li><a href=\"test.fcgi?action=graph;%s\">%s</a></li>\n", params, desc);
80 return (0);
81 } /* }}} int print_graph_inst_html */
83 static int print_graph_html (graph_config_t *cfg, /* {{{ */
84 __attribute__((unused)) void *user_data)
85 {
86 char buffer[1024];
88 memset (buffer, 0, sizeof (buffer));
89 graph_get_title (cfg, buffer, sizeof (buffer));
91 printf ("<li>%s\n<ul>\n", buffer);
92 gl_graph_instance_get_all (cfg, print_graph_inst_html, /* user_data = */ NULL);
93 printf ("</ul>\n");
95 return (0);
96 } /* }}} int print_graph_html */
98 static int list_graphs_html (void) /* {{{ */
99 {
100 printf ("Content-Type: text/html\n\n");
102 printf ("<ul>\n");
103 gl_graph_get_all (print_graph_html, /* user_data = */ NULL);
104 printf ("</ul>\n");
106 return (0);
107 } /* }}} int list_graphs_html */
109 int action_list_graphs (void) /* {{{ */
110 {
111 const char *format;
113 gl_update ();
115 format = param ("format");
116 if (format == NULL)
117 format = "html";
119 if (strcmp ("json", format) == 0)
120 return (list_graphs_json ());
121 else
122 return (list_graphs_html ());
123 } /* }}} int action_list_graphs */
125 /* vim: set sw=2 sts=2 et fdm=marker : */