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_instance.h"
44 /* Include this last, so the macro magic of <fcgi_stdio.h> doesn't interfere
45 * with our own header files. */
46 #include <fcgiapp.h>
47 #include <fcgi_stdio.h>
49 struct action_s
50 {
51 const char *name;
52 int (*callback) (void);
53 };
54 typedef struct action_s action_t;
56 static int action_usage (void);
58 static const action_t actions[] =
59 {
60 { "graph", action_graph },
61 { "list_graphs", action_list_graphs },
62 { "search_json", action_search_json },
63 { "show_instance", action_show_instance },
64 { "usage", action_usage }
65 };
66 static const size_t actions_num = sizeof (actions) / sizeof (actions[0]);
69 static int action_usage (void) /* {{{ */
70 {
71 size_t i;
73 printf ("Content-Type: text/plain\n\n");
75 printf ("Usage:\n"
76 "\n"
77 " Available actions:\n"
78 "\n");
80 for (i = 0; i < actions_num; i++)
81 printf (" * %s\n", actions[i].name);
83 printf ("\n");
85 return (0);
86 } /* }}} int action_usage */
88 static int handle_request (void) /* {{{ */
89 {
90 const char *action;
92 param_init ();
94 action = param ("action");
95 if (action == NULL)
96 {
97 return (action_usage ());
98 }
99 else
100 {
101 size_t i;
103 for (i = 0; i < actions_num; i++)
104 {
105 if (strcmp (action, actions[i].name) == 0)
106 return ((*actions[i].callback) ());
107 }
109 return (action_usage ());
110 }
111 } /* }}} int handle_request */
113 static int run (void) /* {{{ */
114 {
115 while (FCGI_Accept() >= 0)
116 {
117 handle_request ();
118 param_finish ();
119 }
121 return (0);
122 } /* }}} int run */
124 int main (int argc, char **argv) /* {{{ */
125 {
126 int status;
128 argc = 0;
129 argv = NULL;
131 if (FCGX_IsCGI ())
132 status = handle_request ();
133 else
134 status = run ();
136 exit ((status == 0) ? EXIT_SUCCESS : EXIT_FAILURE);
137 } /* }}} int main */
139 /* vim: set sw=2 sts=2 et fdm=marker : */