Fix compiler warnings.
[collection4.git] / graph_ident.c
1 #include <stdlib.h>
2 #include <string.h>
3 #include <strings.h>
4 #include <errno.h>
5 #include <limits.h> /* PATH_MAX */
6
7 #include "graph_ident.h"
8 #include "common.h"
9 #include "filesystem.h"
10
11 #define IS_ANY(str) (((str) != NULL) && (strcasecmp (ANY_TOKEN, (str)) == 0))
12 #define IS_ALL(str) (((str) != NULL) && (strcasecmp (ALL_TOKEN, (str)) == 0))
13
14 /*
15  * Data types
16  */
17 struct graph_ident_s /* {{{ */
18 {
19   char *host;
20   char *plugin;
21   char *plugin_instance;
22   char *type;
23   char *type_instance;
24 }; /* }}} struct graph_ident_s */
25
26 /*
27  * Private functions
28  */
29 static char *part_copy_with_selector (const char *selector, /* {{{ */
30     const char *part, unsigned int flags)
31 {
32   if ((selector == NULL) || (part == NULL))
33     return (NULL);
34
35   if ((flags & IDENT_FLAG_REPLACE_ANY) && IS_ANY (part))
36     return (NULL);
37
38   if ((flags & IDENT_FLAG_REPLACE_ALL) && IS_ALL (part))
39     return (NULL);
40
41   /* Replace the ANY and ALL flags if requested and if the selecter actually
42    * *is* that flag. */
43   if (IS_ANY (selector))
44   {
45     if (flags & IDENT_FLAG_REPLACE_ANY)
46       return (strdup (part));
47     else
48       return (strdup (selector));
49   }
50
51   if (IS_ALL (selector))
52   {
53     if (flags & IDENT_FLAG_REPLACE_ALL)
54       return (strdup (part));
55     else
56       return (strdup (selector));
57   }
58
59   if (strcmp (selector, part) != 0)
60     return (NULL);
61
62   /* Otherwise (no replacement), return a copy of the selector. */
63   return (strdup (selector));
64 } /* }}} char *part_copy_with_selector */
65
66 static _Bool part_matches (const char *selector, /* {{{ */
67     const char *part)
68 {
69   if ((selector == NULL) && (part == NULL))
70     return (1);
71
72   if (selector == NULL) /* && (part != NULL) */
73     return (0);
74
75   if (IS_ANY(selector) || IS_ALL(selector))
76     return (1);
77
78   if (part == NULL) /* && (selector != NULL) */
79     return (0);
80
81   if (strcmp (selector, part) == 0)
82     return (1);
83
84   return (0);
85 } /* }}} _Bool part_matches */
86
87 /*
88  * Public functions
89  */
90 graph_ident_t *ident_create (const char *host, /* {{{ */
91     const char *plugin, const char *plugin_instance,
92     const char *type, const char *type_instance)
93 {
94   graph_ident_t *ret;
95
96   if ((host == NULL)
97       || (plugin == NULL) || (plugin_instance == NULL)
98       || (type == NULL) || (type_instance == NULL))
99     return (NULL);
100
101   ret = malloc (sizeof (*ret));
102   if (ret == NULL)
103     return (NULL);
104   memset (ret, 0, sizeof (*ret));
105
106   ret->host = NULL;
107   ret->host = NULL;
108   ret->plugin = NULL;
109   ret->plugin_instance = NULL;
110   ret->type = NULL;
111   ret->type_instance = NULL;
112
113 #define COPY_PART(p) do {        \
114   ret->p = strdup (p);           \
115   if (ret->p == NULL)            \
116   {                              \
117     free (ret->host);            \
118     free (ret->plugin);          \
119     free (ret->plugin_instance); \
120     free (ret->type);            \
121     free (ret->type_instance);   \
122     free (ret);                  \
123     return (NULL);               \
124   }                              \
125 } while (0)
126
127   COPY_PART(host);
128   COPY_PART(plugin);
129   COPY_PART(plugin_instance);
130   COPY_PART(type);
131   COPY_PART(type_instance);
132
133 #undef COPY_PART
134
135   return (ret);
136 } /* }}} graph_ident_t *ident_create */
137
138 graph_ident_t *ident_clone (const graph_ident_t *ident) /* {{{ */
139 {
140   return (ident_create (ident->host,
141         ident->plugin, ident->plugin_instance,
142         ident->type, ident->type_instance));
143 } /* }}} graph_ident_t *ident_clone */
144
145 graph_ident_t *ident_copy_with_selector (const graph_ident_t *selector, /* {{{ */
146     const graph_ident_t *ident, unsigned int flags)
147 {
148   graph_ident_t *ret;
149
150   if ((selector == NULL) || (ident == NULL))
151     return (NULL);
152
153   ret = malloc (sizeof (*ret));
154   if (ret == NULL)
155     return (NULL);
156   memset (ret, 0, sizeof (*ret));
157   ret->host = NULL;
158   ret->plugin = NULL;
159   ret->plugin_instance = NULL;
160   ret->type = NULL;
161   ret->type_instance = NULL;
162
163 #define COPY_PART(p) do {                                  \
164   ret->p = part_copy_with_selector (selector->p, ident->p, flags); \
165   if (ret->p == NULL)                                      \
166   {                                                        \
167     free (ret->host);                                      \
168     free (ret->plugin);                                    \
169     free (ret->plugin_instance);                           \
170     free (ret->type);                                      \
171     free (ret->type_instance);                             \
172     return (NULL);                                         \
173   }                                                        \
174 } while (0)
175
176   COPY_PART (host);
177   COPY_PART (plugin);
178   COPY_PART (plugin_instance);
179   COPY_PART (type);
180   COPY_PART (type_instance);
181
182 #undef COPY_PART
183
184   return (ret);
185 } /* }}} graph_ident_t *ident_copy_with_selector */
186
187 void ident_destroy (graph_ident_t *ident) /* {{{ */
188 {
189   if (ident == NULL)
190     return;
191
192   free (ident->host);
193   free (ident->plugin);
194   free (ident->plugin_instance);
195   free (ident->type);
196   free (ident->type_instance);
197
198   free (ident);
199 } /* }}} void ident_destroy */
200
201 /* ident_get_* methods {{{ */
202 const char *ident_get_host (graph_ident_t *ident) /* {{{ */
203 {
204   if (ident == NULL)
205     return (NULL);
206
207   return (ident->host);
208 } /* }}} char *ident_get_host */
209
210 const char *ident_get_plugin (graph_ident_t *ident) /* {{{ */
211 {
212   if (ident == NULL)
213     return (NULL);
214
215   return (ident->plugin);
216 } /* }}} char *ident_get_plugin */
217
218 const char *ident_get_plugin_instance (graph_ident_t *ident) /* {{{ */
219 {
220   if (ident == NULL)
221     return (NULL);
222
223   return (ident->plugin_instance);
224 } /* }}} char *ident_get_plugin_instance */
225
226 const char *ident_get_type (graph_ident_t *ident) /* {{{ */
227 {
228   if (ident == NULL)
229     return (NULL);
230
231   return (ident->type);
232 } /* }}} char *ident_get_type */
233
234 const char *ident_get_type_instance (graph_ident_t *ident) /* {{{ */
235 {
236   if (ident == NULL)
237     return (NULL);
238
239   return (ident->type_instance);
240 } /* }}} char *ident_get_type_instance */
241 /* }}} ident_get_* methods */
242
243 /* ident_set_* methods {{{ */
244 int ident_set_host (graph_ident_t *ident, const char *host) /* {{{ */
245 {
246   char *tmp;
247
248   if ((ident == NULL) || (host == NULL))
249     return (EINVAL);
250
251   tmp = strdup (host);
252   if (tmp == NULL)
253     return (ENOMEM);
254
255   free (ident->host);
256   ident->host = tmp;
257
258   return (0);
259 } /* }}} int ident_set_host */
260
261 int ident_set_plugin (graph_ident_t *ident, const char *plugin) /* {{{ */
262 {
263   char *tmp;
264
265   if ((ident == NULL) || (plugin == NULL))
266     return (EINVAL);
267
268   tmp = strdup (plugin);
269   if (tmp == NULL)
270     return (ENOMEM);
271
272   free (ident->plugin);
273   ident->plugin = tmp;
274
275   return (0);
276 } /* }}} int ident_set_plugin */
277
278 int ident_set_plugin_instance (graph_ident_t *ident, const char *plugin_instance) /* {{{ */
279 {
280   char *tmp;
281
282   if ((ident == NULL) || (plugin_instance == NULL))
283     return (EINVAL);
284
285   tmp = strdup (plugin_instance);
286   if (tmp == NULL)
287     return (ENOMEM);
288
289   free (ident->plugin_instance);
290   ident->plugin_instance = tmp;
291
292   return (0);
293 } /* }}} int ident_set_plugin_instance */
294
295 int ident_set_type (graph_ident_t *ident, const char *type) /* {{{ */
296 {
297   char *tmp;
298
299   if ((ident == NULL) || (type == NULL))
300     return (EINVAL);
301
302   tmp = strdup (type);
303   if (tmp == NULL)
304     return (ENOMEM);
305
306   free (ident->type);
307   ident->type = tmp;
308
309   return (0);
310 } /* }}} int ident_set_type */
311
312 int ident_set_type_instance (graph_ident_t *ident, const char *type_instance) /* {{{ */
313 {
314   char *tmp;
315
316   if ((ident == NULL) || (type_instance == NULL))
317     return (EINVAL);
318
319   tmp = strdup (type_instance);
320   if (tmp == NULL)
321     return (ENOMEM);
322
323   free (ident->type_instance);
324   ident->type_instance = tmp;
325
326   return (0);
327 } /* }}} int ident_set_type_instance */
328
329 /* }}} ident_set_* methods */
330
331 int ident_compare (const graph_ident_t *i0, /* {{{ */
332     const graph_ident_t *i1)
333 {
334   int status;
335
336 #define COMPARE_PART(p) do {       \
337   status = strcmp (i0->p, i1->p);  \
338   if (status != 0)                 \
339     return (status);               \
340 } while (0)
341
342   COMPARE_PART (host);
343   COMPARE_PART (plugin);
344   COMPARE_PART (plugin_instance);
345   COMPARE_PART (type);
346   COMPARE_PART (type_instance);
347
348 #undef COMPARE_PART
349
350   return (0);
351 } /* }}} int ident_compare */
352
353 _Bool ident_matches (const graph_ident_t *selector, /* {{{ */
354     const graph_ident_t *ident)
355 {
356   if ((selector == NULL) && (ident == NULL))
357     return (0);
358   else if (selector == NULL)
359     return (-1);
360   else if (ident == NULL)
361     return (1);
362
363   if (!part_matches (selector->host, ident->host))
364     return (0);
365
366   if (!part_matches (selector->plugin, ident->plugin))
367     return (0);
368
369   if (!part_matches (selector->plugin_instance, ident->plugin_instance))
370     return (0);
371
372   if (!part_matches (selector->type, ident->type))
373     return (0);
374
375   if (!part_matches (selector->type_instance, ident->type_instance))
376     return (0);
377
378   return (1);
379 } /* }}} _Bool ident_matches */
380
381 char *ident_to_string (const graph_ident_t *ident) /* {{{ */
382 {
383   char buffer[PATH_MAX];
384
385   buffer[0] = 0;
386
387   strlcat (buffer, ident->host, sizeof (buffer));
388   strlcat (buffer, "/", sizeof (buffer));
389   strlcat (buffer, ident->plugin, sizeof (buffer));
390   if (ident->plugin_instance[0] != 0)
391   {
392     strlcat (buffer, "-", sizeof (buffer));
393     strlcat (buffer, ident->plugin_instance, sizeof (buffer));
394   }
395   strlcat (buffer, "/", sizeof (buffer));
396   strlcat (buffer, ident->type, sizeof (buffer));
397   if (ident->type_instance[0] != 0)
398   {
399     strlcat (buffer, "-", sizeof (buffer));
400     strlcat (buffer, ident->type_instance, sizeof (buffer));
401   }
402
403   return (strdup (buffer));
404 } /* }}} char *ident_to_string */
405
406 char *ident_to_file (const graph_ident_t *ident) /* {{{ */
407 {
408   char buffer[PATH_MAX];
409
410   buffer[0] = 0;
411
412   strlcat (buffer, DATA_DIR, sizeof (buffer));
413   strlcat (buffer, "/", sizeof (buffer));
414
415   strlcat (buffer, ident->host, sizeof (buffer));
416   strlcat (buffer, "/", sizeof (buffer));
417   strlcat (buffer, ident->plugin, sizeof (buffer));
418   if (ident->plugin_instance[0] != 0)
419   {
420     strlcat (buffer, "-", sizeof (buffer));
421     strlcat (buffer, ident->plugin_instance, sizeof (buffer));
422   }
423   strlcat (buffer, "/", sizeof (buffer));
424   strlcat (buffer, ident->type, sizeof (buffer));
425   if (ident->type_instance[0] != 0)
426   {
427     strlcat (buffer, "-", sizeof (buffer));
428     strlcat (buffer, ident->type_instance, sizeof (buffer));
429   }
430
431   strlcat (buffer, ".rrd", sizeof (buffer));
432
433   return (strdup (buffer));
434 } /* }}} char *ident_to_file */
435
436 char *ident_to_json (const graph_ident_t *ident) /* {{{ */
437 {
438   char buffer[4096];
439
440   buffer[0] = 0;
441
442   strlcat (buffer, "{\"host\":\"", sizeof (buffer));
443   strlcat (buffer, ident->host, sizeof (buffer));
444   strlcat (buffer, "\",\"plugin\":\"", sizeof (buffer));
445   strlcat (buffer, ident->plugin, sizeof (buffer));
446   strlcat (buffer, "\",\"plugin_instance\":\"", sizeof (buffer));
447   strlcat (buffer, ident->plugin_instance, sizeof (buffer));
448   strlcat (buffer, "\",\"type\":\"", sizeof (buffer));
449   strlcat (buffer, ident->type, sizeof (buffer));
450   strlcat (buffer, "\",\"type_instance\":\"", sizeof (buffer));
451   strlcat (buffer, ident->type_instance, sizeof (buffer));
452   strlcat (buffer, "\"}", sizeof (buffer));
453
454   return (strdup (buffer));
455 } /* }}} char *ident_to_json */
456
457 /* vim: set sw=2 sts=2 et fdm=marker : */
458