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