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