1 /**
2 * collection4 - main.c
3 * Copyright (C) 2010 Florian octo Forster
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301 USA
19 *
20 * Authors:
21 * Florian octo Forster <ff at octo.it>
22 **/
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <strings.h>
28 #include <errno.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <unistd.h>
33 #include <dirent.h>
35 #include "common.h"
36 #include "graph_list.h"
37 #include "utils_cgi.h"
39 #include "action_graph.h"
40 #include "action_list_graphs.h"
41 #include "action_search_json.h"
42 #include "action_show_graph.h"
43 #include "action_show_graph_json.h"
44 #include "action_show_instance.h"
46 /* Include this last, so the macro magic of <fcgi_stdio.h> doesn't interfere
47 * with our own header files. */
48 #include <fcgiapp.h>
49 #include <fcgi_stdio.h>
51 struct action_s
52 {
53 const char *name;
54 int (*callback) (void);
55 };
56 typedef struct action_s action_t;
58 static int action_usage (void);
60 static const action_t actions[] =
61 {
62 { "graph", action_graph },
63 { "list_graphs", action_list_graphs },
64 { "search_json", action_search_json },
65 { "show_graph", action_show_graph },
66 { "show_graph_json", action_show_graph_json },
67 { "show_instance", action_show_instance },
68 { "usage", action_usage }
69 };
70 static const size_t actions_num = sizeof (actions) / sizeof (actions[0]);
73 static int action_usage (void) /* {{{ */
74 {
75 size_t i;
77 printf ("Content-Type: text/plain\n\n");
79 printf ("Usage:\n"
80 "\n"
81 " Available actions:\n"
82 "\n");
84 for (i = 0; i < actions_num; i++)
85 printf (" * %s\n", actions[i].name);
87 printf ("\n");
89 return (0);
90 } /* }}} int action_usage */
92 static int handle_request (void) /* {{{ */
93 {
94 const char *action;
96 param_init ();
98 action = param ("action");
99 if (action == NULL)
100 {
101 return (action_usage ());
102 }
103 else
104 {
105 size_t i;
107 for (i = 0; i < actions_num; i++)
108 {
109 if (strcmp (action, actions[i].name) == 0)
110 return ((*actions[i].callback) ());
111 }
113 return (action_usage ());
114 }
115 } /* }}} int handle_request */
117 static int run (void) /* {{{ */
118 {
119 while (FCGI_Accept() >= 0)
120 {
121 handle_request ();
122 param_finish ();
123 }
125 return (0);
126 } /* }}} int run */
128 int main (int argc, char **argv) /* {{{ */
129 {
130 int status;
132 argc = 0;
133 argv = NULL;
135 if (FCGX_IsCGI ())
136 status = handle_request ();
137 else
138 status = run ();
140 exit ((status == 0) ? EXIT_SUCCESS : EXIT_FAILURE);
141 } /* }}} int main */
143 /* vim: set sw=2 sts=2 et fdm=marker : */