1d1ba0a1d24d31dbb0b75bc93b1d03f2f3ac818d
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <strings.h>
5 #include <errno.h>
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <unistd.h>
10 #include <dirent.h>
12 #include "common.h"
13 #include "graph_list.h"
14 #include "utils_cgi.h"
16 #include "action_graph.h"
17 #include "action_list_graphs.h"
18 #include "action_search_json.h"
19 #include "action_show_instance.h"
21 /* Include this last, so the macro magic of <fcgi_stdio.h> doesn't interfere
22 * with our own header files. */
23 #include <fcgiapp.h>
24 #include <fcgi_stdio.h>
26 struct action_s
27 {
28 const char *name;
29 int (*callback) (void);
30 };
31 typedef struct action_s action_t;
33 static int action_usage (void);
35 static const action_t actions[] =
36 {
37 { "graph", action_graph },
38 { "list_graphs", action_list_graphs },
39 { "search_json", action_search_json },
40 { "show_instance", action_show_instance },
41 { "usage", action_usage }
42 };
43 static const size_t actions_num = sizeof (actions) / sizeof (actions[0]);
46 static int action_usage (void) /* {{{ */
47 {
48 size_t i;
50 printf ("Content-Type: text/plain\n\n");
52 printf ("Usage:\n"
53 "\n"
54 " Available actions:\n"
55 "\n");
57 for (i = 0; i < actions_num; i++)
58 printf (" * %s\n", actions[i].name);
60 printf ("\n");
62 return (0);
63 } /* }}} int action_usage */
65 static int handle_request (void) /* {{{ */
66 {
67 const char *action;
69 param_init ();
71 action = param ("action");
72 if (action == NULL)
73 {
74 return (action_usage ());
75 }
76 else
77 {
78 size_t i;
80 for (i = 0; i < actions_num; i++)
81 {
82 if (strcmp (action, actions[i].name) == 0)
83 return ((*actions[i].callback) ());
84 }
86 return (action_usage ());
87 }
88 } /* }}} int handle_request */
90 static int run (void) /* {{{ */
91 {
92 while (FCGI_Accept() >= 0)
93 {
94 handle_request ();
95 param_finish ();
96 }
98 return (0);
99 } /* }}} int run */
101 int main (int argc, char **argv) /* {{{ */
102 {
103 int status;
105 argc = 0;
106 argv = NULL;
108 if (FCGX_IsCGI ())
109 status = handle_request ();
110 else
111 status = run ();
113 exit ((status == 0) ? EXIT_SUCCESS : EXIT_FAILURE);
114 } /* }}} int main */
116 /* vim: set sw=2 sts=2 et fdm=marker : */