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