Merge branch 'search'
[collection4.git] / src / main.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 "common.h"
13 #include "graph_list.h"
14 #include "utils_params.h"
15
16 #include "action_graph.h"
17 #include "action_list_graphs.h"
18
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>
23
24 struct action_s
25 {
26   const char *name;
27   int (*callback) (void);
28 };
29 typedef struct action_s action_t;
30
31 static int action_usage (void);
32
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]);
40
41
42 static int action_usage (void) /* {{{ */
43 {
44   size_t i;
45
46   printf ("Content-Type: text/plain\n\n");
47
48   printf ("Usage:\n"
49       "\n"
50       "  Available actions:\n"
51       "\n");
52
53   for (i = 0; i < actions_num; i++)
54     printf ("  * %s\n", actions[i].name);
55
56   printf ("\n");
57
58   return (0);
59 } /* }}} int action_usage */
60
61 static int handle_request (void) /* {{{ */
62 {
63   const char *action;
64
65   param_init ();
66
67   action = param ("action");
68   if (action == NULL)
69   {
70     return (action_usage ());
71   }
72   else
73   {
74     size_t i;
75
76     for (i = 0; i < actions_num; i++)
77     {
78       if (strcmp (action, actions[i].name) == 0)
79         return ((*actions[i].callback) ());
80     }
81
82     return (action_usage ());
83   }
84 } /* }}} int handle_request */
85
86 static int run (void) /* {{{ */
87 {
88   while (FCGI_Accept() >= 0)
89   {
90     handle_request ();
91     param_finish ();
92   }
93
94   return (0);
95 } /* }}} int run */
96
97 int main (int argc, char **argv) /* {{{ */
98 {
99   int status;
100
101   argc = 0;
102   argv = NULL;
103
104   if (FCGX_IsCGI ())
105     status = handle_request ();
106   else
107     status = run ();
108
109   exit ((status == 0) ? EXIT_SUCCESS : EXIT_FAILURE);
110 } /* }}} int main */
111
112 /* vim: set sw=2 sts=2 et fdm=marker : */