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_ident.h"
9 #include "graph_list.h"
10 #include "utils_cgi.h"
12 #include <fcgiapp.h>
13 #include <fcgi_stdio.h>
15 static int print_graph_inst_json (__attribute__((unused)) graph_config_t *cfg, /* {{{ */
16 graph_instance_t *inst,
17 void *user_data)
18 {
19 _Bool *first;
20 graph_ident_t *ident;
21 char *json;
23 first = user_data;
25 ident = inst_get_selector (inst);
26 if (ident == NULL)
27 return (-1);
29 json = ident_to_json (ident);
30 if (json == NULL)
31 {
32 ident_destroy (ident);
33 return (ENOMEM);
34 }
36 if (*first)
37 printf ("%s", json);
38 else
39 printf (",\n%s", json);
41 *first = 0;
43 ident_destroy (ident);
44 return (0);
45 } /* }}} int print_graph_inst_json */
47 static int print_graph_json (graph_config_t *cfg, /* {{{ */
48 void *user_data)
49 {
50 return (gl_graph_instance_get_all (cfg, print_graph_inst_json, user_data));
51 } /* }}} int print_graph_json */
53 static int list_graphs_json (void) /* {{{ */
54 {
55 _Bool first = 1;
57 printf ("Content-Type: application/json\n\n");
59 printf ("[\n");
60 gl_graph_get_all (print_graph_json, /* user_data = */ &first);
61 printf ("\n]");
63 return (0);
64 } /* }}} int list_graphs_json */
66 struct callback_data_s
67 {
68 graph_config_t *cfg;
69 int limit;
70 };
71 typedef struct callback_data_s callback_data_t;
73 static int print_graph_inst_html (graph_config_t *cfg, /* {{{ */
74 graph_instance_t *inst,
75 void *user_data)
76 {
77 callback_data_t *data = user_data;
78 char params[1024];
79 char desc[1024];
81 if (data->cfg != cfg)
82 {
83 if (data->cfg != NULL)
84 printf (" </ul></li>\n");
86 memset (desc, 0, sizeof (desc));
87 graph_get_title (cfg, desc, sizeof (desc));
88 html_escape_buffer (desc, sizeof (desc));
90 printf (" <li>%s\n <ul>\n", desc);
92 data->cfg = cfg;
93 }
95 memset (params, 0, sizeof (params));
96 inst_get_params (cfg, inst, params, sizeof (params));
97 html_escape_buffer (params, sizeof (params));
99 memset (desc, 0, sizeof (desc));
100 inst_describe (cfg, inst, desc, sizeof (desc));
101 html_escape_buffer (desc, sizeof (desc));
103 printf (" <li><a href=\"%s?action=graph;%s\">%s</a></li>\n",
104 script_name (), params, desc);
106 if (data->limit > 0)
107 data->limit--;
109 /* Abort scan if limit is reached. */
110 if (data->limit == 0)
111 return (1);
113 return (0);
114 } /* }}} int print_graph_inst_html */
116 static int list_graphs_html (const char *term) /* {{{ */
117 {
118 callback_data_t data = { NULL, /* limit = */ 20 };
119 char *term_html;
121 term_html = NULL;
122 if (term != NULL)
123 term_html = html_escape (term);
125 printf ("Content-Type: text/html\n\n");
127 printf ("<html>\n <head>\n");
128 if (term != NULL)
129 printf (" <title>c4: Graphs matching "%s"</title>\n", term);
130 else
131 printf (" <title>c4: List of all graphs</title>\n");
132 printf (" </head>\n <body>\n");
134 printf ("<form action=\"%s\" method=\"get\">\n"
135 " <input type=\"hidden\" name=\"action\" value=\"list_graphs\" />\n"
136 " <input type=\"text\" name=\"search\" value=\"%s\" />\n"
137 " <input type=\"submit\" name=\"button\" value=\"Search\" />\n"
138 "</form>\n",
139 script_name (), (term_html != NULL) ? term_html : "");
141 free (term_html);
143 printf (" <ul>\n");
144 if (term == NULL)
145 gl_instance_get_all (print_graph_inst_html, /* user_data = */ &data);
146 else
147 gl_search (term, print_graph_inst_html, /* user_data = */ &data);
149 if (data.cfg != NULL)
150 printf (" </ul></li>\n");
152 printf (" </ul>\n");
154 printf (" </body>\n</html>\n");
156 return (0);
157 } /* }}} int list_graphs_html */
159 int action_list_graphs (void) /* {{{ */
160 {
161 const char *format;
163 gl_update ();
165 format = param ("format");
166 if (format == NULL)
167 format = "html";
169 if (strcmp ("json", format) == 0)
170 return (list_graphs_json ());
171 else
172 return (list_graphs_html (param ("search")));
173 } /* }}} int action_list_graphs */
175 /* vim: set sw=2 sts=2 et fdm=marker : */