1 #include <stdlib.h>
2 #include <unistd.h>
3 #include <string.h>
4 #include <strings.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <errno.h>
9 #include "graph_config.h"
10 #include "graph_list.h"
11 #include "oconfig.h"
12 #include "common.h"
14 #define CONFIG_FILE "/usr/lib/cgi-bin/octo/collection.conf"
16 time_t last_read_mtime = 0;
18 static int dispatch_config (const oconfig_item_t *ci) /* {{{ */
19 {
20 int i;
22 for (i = 0; i < ci->children_num; i++)
23 {
24 oconfig_item_t *child;
26 child = ci->children + i;
27 if (strcasecmp ("Graph", child->key) == 0)
28 graph_config_add (child);
29 else
30 {
31 DEBUG ("Unknown config option: %s", child->key);
32 }
33 }
35 return (0);
36 } /* }}} int dispatch_config */
38 static int internal_read_config (void) /* {{{ */
39 {
40 oconfig_item_t *ci;
42 ci = oconfig_parse_file (CONFIG_FILE);
43 if (ci == NULL)
44 return (-1);
46 dispatch_config (ci);
48 oconfig_free (ci);
50 gl_config_submit ();
52 return (0);
53 } /* }}} int internal_read_config */
55 static time_t get_config_mtime (void) /* {{{ */
56 {
57 struct stat statbuf;
58 int status;
60 memset (&statbuf, 0, sizeof (statbuf));
61 status = stat (CONFIG_FILE, &statbuf);
62 if (status != 0)
63 return (0);
65 return (statbuf.st_mtime);
66 } /* }}} time_t get_config_mtime */
68 int graph_read_config (void) /* {{{ */
69 {
70 time_t mtime;
72 mtime = get_config_mtime ();
74 if (mtime <= last_read_mtime)
75 return (0);
77 internal_read_config ();
79 last_read_mtime = mtime;
81 return (0);
82 } /* }}} int graph_read_config */
84 int graph_config_get_string (const oconfig_item_t *ci, /* {{{ */
85 char **ret_str)
86 {
87 char *tmp;
89 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
90 return (EINVAL);
92 tmp = strdup (ci->values[0].value.string);
93 if (tmp == NULL)
94 return (ENOMEM);
96 free (*ret_str);
97 *ret_str = tmp;
99 return (0);
100 } /* }}} int graph_config_get_string */
102 int graph_config_get_bool (const oconfig_item_t *ci, /* {{{ */
103 _Bool *ret_bool)
104 {
105 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
106 return (EINVAL);
108 if (ci->values[0].value.boolean)
109 *ret_bool = 1;
110 else
111 *ret_bool = 0;
113 return (0);
114 } /* }}} int graph_config_get_bool */
116 /* vim: set sw=2 sts=2 et fdm=marker : */