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