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"
20 /* Include this last, so the macro magic of <fcgi_stdio.h> doesn't interfere
21 * with our own header files. */
22 #include <fcgiapp.h>
23 #include <fcgi_stdio.h>
25 struct action_s
26 {
27 const char *name;
28 int (*callback) (void);
29 };
30 typedef struct action_s action_t;
32 static int action_usage (void);
34 static const action_t actions[] =
35 {
36 { "graph", action_graph },
37 { "list_graphs", action_list_graphs },
38 { "search_json", action_search_json },
39 { "usage", action_usage }
40 };
41 static const size_t actions_num = sizeof (actions) / sizeof (actions[0]);
44 static int action_usage (void) /* {{{ */
45 {
46 size_t i;
48 printf ("Content-Type: text/plain\n\n");
50 printf ("Usage:\n"
51 "\n"
52 " Available actions:\n"
53 "\n");
55 for (i = 0; i < actions_num; i++)
56 printf (" * %s\n", actions[i].name);
58 printf ("\n");
60 return (0);
61 } /* }}} int action_usage */
63 static int handle_request (void) /* {{{ */
64 {
65 const char *action;
67 param_init ();
69 action = param ("action");
70 if (action == NULL)
71 {
72 return (action_usage ());
73 }
74 else
75 {
76 size_t i;
78 for (i = 0; i < actions_num; i++)
79 {
80 if (strcmp (action, actions[i].name) == 0)
81 return ((*actions[i].callback) ());
82 }
84 return (action_usage ());
85 }
86 } /* }}} int handle_request */
88 static int run (void) /* {{{ */
89 {
90 while (FCGI_Accept() >= 0)
91 {
92 handle_request ();
93 param_finish ();
94 }
96 return (0);
97 } /* }}} int run */
99 int main (int argc, char **argv) /* {{{ */
100 {
101 int status;
103 argc = 0;
104 argv = NULL;
106 if (FCGX_IsCGI ())
107 status = handle_request ();
108 else
109 status = run ();
111 exit ((status == 0) ? EXIT_SUCCESS : EXIT_FAILURE);
112 } /* }}} int main */
114 /* vim: set sw=2 sts=2 et fdm=marker : */