Uhm, lots of stuff changed. Forgot to check in to be honest ;)
[collection4.git] / test.fcgi.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <strings.h>
5 #include <errno.h>
6
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <unistd.h>
10 #include <dirent.h>
11
12 #include <fcgiapp.h>
13 #include <fcgi_stdio.h>
14
15 #include "common.h"
16 #include "graph_list.h"
17 #include "utils_params.h"
18
19 #include "action_graph.h"
20 #include "action_list_graphs.h"
21
22 struct action_s
23 {
24   const char *name;
25   int (*callback) (void);
26 };
27 typedef struct action_s action_t;
28
29 static int action_usage (void);
30
31 static const action_t actions[] =
32 {
33   { "graph",       action_graph },
34   { "list_graphs", action_list_graphs },
35   { "usage",       action_usage }
36 };
37 static const size_t actions_num = sizeof (actions) / sizeof (actions[0]);
38
39
40 static int action_usage (void) /* {{{ */
41 {
42   size_t i;
43
44   printf ("Content-Type: text/plain\n\n");
45
46   printf ("Usage:\n"
47       "\n"
48       "  Available actions:\n"
49       "\n");
50
51   for (i = 0; i < actions_num; i++)
52     printf ("  * %s\n", actions[i].name);
53
54   printf ("\n");
55
56   return (0);
57 } /* }}} int action_usage */
58
59 static int handle_request (void) /* {{{ */
60 {
61   const char *action;
62
63   param_init ();
64
65   action = param ("action");
66   if (action == NULL)
67   {
68     return (action_usage ());
69   }
70   else
71   {
72     size_t i;
73
74     for (i = 0; i < actions_num; i++)
75     {
76       if (strcmp (action, actions[i].name) == 0)
77         return ((*actions[i].callback) ());
78     }
79
80     return (action_usage ());
81   }
82 } /* }}} int handle_request */
83
84 static int run (void) /* {{{ */
85 {
86   while (FCGI_Accept() >= 0)
87   {
88     handle_request ();
89     param_finish ();
90   }
91
92   return (0);
93 } /* }}} int run */
94
95 int main (int argc, char **argv) /* {{{ */
96 {
97   int status;
98
99   argc = 0;
100   argv = NULL;
101
102   if (FCGX_IsCGI ())
103     status = handle_request ();
104   else
105     status = run ();
106
107   exit ((status == 0) ? EXIT_SUCCESS : EXIT_FAILURE);
108 } /* }}} int main */
109
110 /* vim: set sw=2 sts=2 et fdm=marker : */