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"
19 /* Include this last, so the macro magic of <fcgi_stdio.h> doesn't interfere
20 * with our own header files. */
21 #include <fcgiapp.h>
22 #include <fcgi_stdio.h>
24 struct action_s
25 {
26 const char *name;
27 int (*callback) (void);
28 };
29 typedef struct action_s action_t;
31 static int action_usage (void);
33 static const action_t actions[] =
34 {
35 { "graph", action_graph },
36 { "list_graphs", action_list_graphs },
37 { "usage", action_usage }
38 };
39 static const size_t actions_num = sizeof (actions) / sizeof (actions[0]);
42 static int action_usage (void) /* {{{ */
43 {
44 size_t i;
46 printf ("Content-Type: text/plain\n\n");
48 printf ("Usage:\n"
49 "\n"
50 " Available actions:\n"
51 "\n");
53 for (i = 0; i < actions_num; i++)
54 printf (" * %s\n", actions[i].name);
56 printf ("\n");
58 return (0);
59 } /* }}} int action_usage */
61 static int handle_request (void) /* {{{ */
62 {
63 const char *action;
65 param_init ();
67 action = param ("action");
68 if (action == NULL)
69 {
70 return (action_usage ());
71 }
72 else
73 {
74 size_t i;
76 for (i = 0; i < actions_num; i++)
77 {
78 if (strcmp (action, actions[i].name) == 0)
79 return ((*actions[i].callback) ());
80 }
82 return (action_usage ());
83 }
84 } /* }}} int handle_request */
86 static int run (void) /* {{{ */
87 {
88 while (FCGI_Accept() >= 0)
89 {
90 handle_request ();
91 param_finish ();
92 }
94 return (0);
95 } /* }}} int run */
97 int main (int argc, char **argv) /* {{{ */
98 {
99 int status;
101 argc = 0;
102 argv = NULL;
104 if (FCGX_IsCGI ())
105 status = handle_request ();
106 else
107 status = run ();
109 exit ((status == 0) ? EXIT_SUCCESS : EXIT_FAILURE);
110 } /* }}} int main */
112 /* vim: set sw=2 sts=2 et fdm=marker : */