8 #include "graph_list.h"
11 #define UPDATE_INTERVAL 10
13 static graph_list_t *graph_list = NULL;
14 static size_t graph_list_length = 0;
15 static time_t gl_last_update = 0;
17 /* "Safe" version of strcmp(3): Either or both pointers may be NULL. */
18 static int strcmp_s (const char *s1, const char *s2) /* {{{ */
20 if ((s1 == NULL) && (s2 == NULL))
26 assert ((s1 != NULL) && (s2 != NULL));
28 return (strcmp (s1, s2));
29 } /* }}} int strcmp_s */
31 static int gl_compare (const void *p0, const void *p1) /* {{{ */
33 const graph_list_t *gl0 = p0;
34 const graph_list_t *gl1 = p1;
37 status = strcmp (gl0->host, gl1->host);
41 status = strcmp (gl0->plugin, gl1->plugin);
45 status = strcmp_s (gl0->plugin_instance, gl1->plugin_instance);
49 status = strcmp (gl0->type, gl1->type);
53 return (strcmp_s (gl0->type_instance, gl1->type_instance));
54 } /* }}} int gl_compare */
56 static void gl_clear_entry (graph_list_t *gl) /* {{{ */
63 free (gl->plugin_instance);
65 free (gl->type_instance);
69 gl->plugin_instance = NULL;
71 gl->type_instance = NULL;
72 } /* }}} void gl_clear_entry */
74 static void gl_clear (void) /* {{{ */
78 for (i = 0; i < graph_list_length; i++)
79 gl_clear_entry (graph_list + i);
83 graph_list_length = 0;
85 } /* }}} void gl_clear */
87 static int gl_add_copy (graph_list_t *gl) /* {{{ */
95 ptr = realloc (graph_list, sizeof (*graph_list) * (graph_list_length + 1));
100 ptr = graph_list + graph_list_length;
101 memset (ptr, 0, sizeof (*ptr));
104 ptr->plugin_instance = NULL;
106 ptr->type_instance = NULL;
108 #define DUP_OR_BREAK(member) do { \
109 ptr->member = NULL; \
110 if (gl->member != NULL) \
112 ptr->member = strdup (gl->member); \
113 if (ptr->member == NULL) \
122 DUP_OR_BREAK(plugin);
123 DUP_OR_BREAK(plugin_instance);
125 DUP_OR_BREAK(type_instance);
136 free (ptr->plugin_instance);
138 free (ptr->type_instance);
144 } /* }}} int gl_add_copy */
146 static int callback_type (const char *type, void *user_data) /* {{{ */
151 if ((type == NULL) || (user_data == NULL))
155 if ((gl->type != NULL) || (gl->type_instance != NULL))
158 gl->type = strdup (type);
159 if (gl->type == NULL)
162 gl->type_instance = strchr (gl->type, '-');
163 if (gl->type_instance != NULL)
165 *gl->type_instance = 0;
169 status = gl_add_copy (gl);
173 gl->type_instance = NULL;
176 } /* }}} int callback_type */
178 static int callback_plugin (const char *plugin, void *user_data) /* {{{ */
183 if ((plugin == NULL) || (user_data == NULL))
187 if ((gl->plugin != NULL) || (gl->plugin_instance != NULL))
190 gl->plugin = strdup (plugin);
191 if (gl->plugin == NULL)
194 gl->plugin_instance = strchr (gl->plugin, '-');
195 if (gl->plugin_instance != NULL)
197 *gl->plugin_instance = 0;
198 gl->plugin_instance++;
201 status = foreach_type (gl->host, plugin, callback_type, gl);
205 gl->plugin_instance = NULL;
208 } /* }}} int callback_plugin */
210 static int callback_host (const char *host, void *user_data) /* {{{ */
215 if ((host == NULL) || (user_data == NULL))
219 if (gl->host != NULL)
222 gl->host = strdup (host);
223 if (gl->host == NULL)
226 status = foreach_plugin (host, callback_plugin, gl);
232 } /* }}} int callback_host */
234 int gl_update (void) /* {{{ */
242 if ((gl_last_update + UPDATE_INTERVAL) >= now)
247 memset (&gl, 0, sizeof (gl));
250 gl.plugin_instance = NULL;
252 gl.type_instance = NULL;
254 status = foreach_host (callback_host, &gl);
256 if (graph_list_length > 1)
257 qsort (graph_list, graph_list_length, sizeof (*graph_list), gl_compare);
260 } /* }}} int gl_update */
262 int gl_foreach (gl_callback callback, void *user_data) /* {{{ */
266 for (i = 0; i < graph_list_length; i++)
270 status = (*callback) (graph_list + i, user_data);
276 } /* }}} int gl_foreach */
278 /* vim: set sw=2 sts=2 et fdm=marker : */