test.fcgi.c: Introduce an array of actions.
[collection4.git] / test.fcgi.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 <fcgiapp.h>
13 #include <fcgi_stdio.h>
14
15 #include "common.h"
16 #include "graph_list.h"
17 #include "utils_params.h"
18
19 #include "action_list_graphs.h"
20
21 struct action_s
22 {
23   const char *name;
24   int (*callback) (void);
25 };
26 typedef struct action_s action_t;
27
28 #if 0
29 struct str_array_s
30 {
31   char **ptr;
32   size_t size;
33 };
34 typedef struct str_array_s str_array_t;
35 #endif
36
37 static int action_usage (void);
38
39 static const action_t actions[] =
40 {
41   { "list_graphs", action_list_graphs },
42   { "usage",       action_usage }
43 };
44 static const size_t actions_num = sizeof (actions) / sizeof (actions[0]);
45
46 #if 0
47 static str_array_t *array_alloc (void) /* {{{ */
48 {
49   str_array_t *a;
50
51   a = malloc (sizeof (*a));
52   if (a == NULL)
53     return (NULL);
54
55   memset (a, 0, sizeof (*a));
56   a->ptr = NULL;
57   a->size = 0;
58
59   return (a);
60 } /* }}} str_array_t *array_alloc */
61
62 static void array_free (str_array_t *a) /* {{{ */
63 {
64   if (a == NULL)
65     return;
66
67   free (a->ptr);
68   a->ptr = NULL;
69   a->size = 0;
70
71   free (a);
72 } /* }}} void array_free */
73
74 static int array_add (const char *entry, void *user_data) /* {{{ */
75 {
76   str_array_t *a = user_data;
77   char **ptr;
78
79   if ((entry == NULL) || (a == NULL))
80     return (EINVAL);
81
82   ptr = realloc (a->ptr, sizeof (*a->ptr) * (a->size + 1));
83   if (ptr == NULL)
84     return (ENOMEM);
85   a->ptr = ptr;
86   ptr = a->ptr + a->size;
87
88   *ptr = strdup (entry);
89   if (*ptr == NULL)
90     return (ENOMEM);
91
92   a->size++;
93   return (0);
94 } /* }}} int array_add */
95 #endif
96
97 static int action_usage (void) /* {{{ */
98 {
99   size_t i;
100
101   printf ("Content-Type: text/plain\n\n");
102
103   printf ("Usage:\n"
104       "\n"
105       "  Available actions:\n"
106       "\n");
107
108   for (i = 0; i < actions_num; i++)
109     printf ("  * %s\n", actions[i].name);
110
111   printf ("\n");
112
113   return (0);
114 } /* }}} int action_usage */
115
116 static int handle_request (void) /* {{{ */
117 {
118   const char *action;
119
120   param_init ();
121
122   action = param ("action");
123   if (action == NULL)
124   {
125     return (action_usage ());
126   }
127   else
128   {
129     size_t i;
130
131     for (i = 0; i < actions_num; i++)
132     {
133       if (strcmp (action, actions[i].name) == 0)
134         return ((*actions[i].callback) ());
135     }
136
137     return (action_usage ());
138   }
139 } /* }}} int handle_request */
140
141 static int run (void) /* {{{ */
142 {
143   while (FCGI_Accept() >= 0)
144   {
145     handle_request ();
146     param_finish ();
147   }
148
149   return (0);
150 } /* }}} int run */
151
152 int main (int argc, char **argv) /* {{{ */
153 {
154   int status;
155
156   argc = 0;
157   argv = NULL;
158
159   if (FCGX_IsCGI ())
160     status = handle_request ();
161   else
162     status = run ();
163
164   exit ((status == 0) ? EXIT_SUCCESS : EXIT_FAILURE);
165 } /* }}} int main */
166
167 /* vim: set sw=2 sts=2 et fdm=marker : */