Fix missing #includes.
[collection4.git] / src / action_search_json.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <errno.h>
5
6 #include "action_search_json.h"
7 #include "common.h"
8 #include "graph.h"
9 #include "graph_ident.h"
10 #include "graph_instance.h"
11 #include "graph_list.h"
12 #include "utils_cgi.h"
13
14 #include <fcgiapp.h>
15 #include <fcgi_stdio.h>
16
17 #define RESULT_LIMIT 10
18
19 struct callback_data_s
20 {
21   graph_config_t *cfg;
22   int limit;
23   _Bool first;
24 };
25 typedef struct callback_data_s callback_data_t;
26
27 static int json_begin_graph (graph_config_t *cfg) /* {{{ */
28 {
29   char desc[1024];
30
31   if (cfg == NULL)
32     return (EINVAL);
33
34   graph_get_title (cfg, desc, sizeof (desc));
35
36   printf ("{\"title\":\"%s\",\"instances\":[", desc);
37
38   return (0);
39 } /* }}} int json_begin_graph */
40
41 static int json_end_graph (void) /* {{{ */
42 {
43   printf ("]}");
44
45   return (0);
46 } /* }}} int json_end_graph */
47
48 static int json_print_instance (graph_config_t *cfg, /* {{{ */
49     graph_instance_t *inst)
50 {
51   char params[1024];
52   char desc[1024];
53
54   if ((cfg == NULL) || (inst == NULL))
55     return (EINVAL);
56
57   memset (desc, 0, sizeof (desc));
58   inst_describe (cfg, inst, desc, sizeof (desc));
59
60   memset (params, 0, sizeof (params));
61   inst_get_params (cfg, inst, params, sizeof (params));
62
63   printf ("{\"description\":\"%s\",\"params\":\"%s\"}",
64       desc, params);
65
66   return (0);
67 } /* }}} int json_print_instance */
68
69 static int json_print_graph_instance (graph_config_t *cfg, /* {{{ */
70     graph_instance_t *inst,
71     void *user_data)
72 {
73   callback_data_t *data = user_data;
74
75   if (data->cfg != cfg)
76   {
77     if (!data->first)
78     {
79       json_end_graph ();
80       printf (",\n");
81     }
82     json_begin_graph (cfg);
83
84     data->cfg = cfg;
85     data->first = 0;
86   }
87   else /* if (not first instance) */
88   {
89     printf (",\n");
90   }
91
92   json_print_instance (cfg, inst);
93
94   if (data->limit > 0)
95     data->limit--;
96
97   if (data->limit == 0)
98     return (1);
99
100   return (0);
101 } /* }}} int json_print_graph_instance */
102
103 static int list_graphs_json (const char *term) /* {{{ */
104 {
105   callback_data_t data;
106
107   time_t now;
108   char time_buffer[128];
109   int status;
110
111   printf ("Content-Type: application/json\n");
112
113   now = time (NULL);
114   status = time_to_rfc1123 (now + 300, time_buffer, sizeof (time_buffer));
115   if (status == 0)
116     printf ("Expires: %s\n"
117         "Cache-Control: public\n",
118         time_buffer);
119   printf ("\n");
120
121   data.cfg = NULL;
122   data.limit = RESULT_LIMIT;
123   data.first = 1;
124
125   printf ("[\n");
126   if (term == NULL)
127     gl_instance_get_all (json_print_graph_instance, /* user_data = */ &data);
128   else
129     gl_search (term, json_print_graph_instance, /* user_data = */ &data);
130
131   if (!data.first)
132     json_end_graph ();
133
134   printf ("\n]");
135
136   return (0);
137 } /* }}} int list_graphs_json */
138
139 int action_search_json (void) /* {{{ */
140 {
141   char *search;
142   int status;
143
144   gl_update ();
145
146   search = strtolower_copy (param ("q"));
147
148   status = list_graphs_json (search);
149
150   free (search);
151
152   return (status);
153 } /* }}} int action_search_json */
154
155 /* vim: set sw=2 sts=2 et fdm=marker : */