Autotoolization.
[collection4.git] / src / action_list_graphs.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <errno.h>
5
6 #include "action_list_graphs.h"
7 #include "graph.h"
8 #include "graph_list.h"
9 #include "utils_params.h"
10
11 #include <fcgiapp.h>
12 #include <fcgi_stdio.h>
13
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;
21
22   first = user_data;
23
24   ident = inst_get_selector (inst);
25   if (ident == NULL)
26     return (-1);
27
28   json = ident_to_json (ident);
29   if (json == NULL)
30   {
31     ident_destroy (ident);
32     return (ENOMEM);
33   }
34
35   if (*first)
36     printf ("%s", json);
37   else
38     printf (",\n%s", json);
39
40   *first = 0;
41
42   ident_destroy (ident);
43   return (0);
44 } /* }}} int print_graph_inst_json */
45
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 */
51
52 static int list_graphs_json (void) /* {{{ */
53 {
54   _Bool first = 1;
55
56   printf ("Content-Type: application/json\n\n");
57
58   printf ("[\n");
59   gl_graph_get_all (print_graph_json, /* user_data = */ &first);
60   printf ("\n]");
61
62   return (0);
63 } /* }}} int list_graphs_json */
64
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];
71
72   memset (params, 0, sizeof (params));
73   inst_get_params (cfg, inst, params, sizeof (params));
74
75   memset (desc, 0, sizeof (desc));
76   inst_describe (cfg, inst, desc, sizeof (desc));
77
78   printf ("    <li><a href=\"test.fcgi?action=graph;%s\">%s</a></li>\n",
79       params, desc);
80
81   return (0);
82 } /* }}} int print_graph_inst_html */
83
84 static int print_graph_html (graph_config_t *cfg, /* {{{ */
85     __attribute__((unused)) void *user_data)
86 {
87   char buffer[1024];
88
89   memset (buffer, 0, sizeof (buffer));
90   graph_get_title (cfg, buffer, sizeof (buffer));
91
92   printf ("  <li>%s\n  <ul>\n", buffer);
93   gl_graph_instance_get_all (cfg, print_graph_inst_html, /* user_data = */ NULL);
94   printf ("  </ul></li>\n");
95
96   return (0);
97 } /* }}} int print_graph_html */
98
99 static int list_graphs_html (void) /* {{{ */
100 {
101   printf ("Content-Type: text/html\n\n");
102
103   printf ("<ul>\n");
104   gl_graph_get_all (print_graph_html, /* user_data = */ NULL);
105   printf ("</ul>\n");
106
107   return (0);
108 } /* }}} int list_graphs_html */
109
110 int action_list_graphs (void) /* {{{ */
111 {
112   const char *format;
113
114   gl_update ();
115
116   format = param ("format");
117   if (format == NULL)
118     format = "html";
119
120   if (strcmp ("json", format) == 0)
121     return (list_graphs_json ());
122   else
123     return (list_graphs_html ());
124 } /* }}} int action_list_graphs */
125
126 /* vim: set sw=2 sts=2 et fdm=marker : */