From: Florian Forster Date: Mon, 14 Jun 2010 21:46:37 +0000 (+0200) Subject: graph_config.[ch]: Add forgotten files. X-Git-Tag: v4.0.0~266 X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=4568880c50d790912fb4823d0ae785e95f65cad2;p=collection4.git graph_config.[ch]: Add forgotten files. --- diff --git a/graph_config.c b/graph_config.c new file mode 100644 index 0000000..1b612c6 --- /dev/null +++ b/graph_config.c @@ -0,0 +1,116 @@ +#include +#include +#include +#include +#include +#include +#include + +#include "graph_config.h" +#include "graph_list.h" +#include "oconfig.h" +#include "common.h" + +#define CONFIG_FILE "/usr/lib/cgi-bin/octo/collection.conf" + +time_t last_read_mtime = 0; + +static int dispatch_config (const oconfig_item_t *ci) /* {{{ */ +{ + int i; + + for (i = 0; i < ci->children_num; i++) + { + oconfig_item_t *child; + + child = ci->children + i; + if (strcasecmp ("Graph", child->key) == 0) + graph_config_add (child); + else + { + DEBUG ("Unknown config option: %s", child->key); + } + } + + return (0); +} /* }}} int dispatch_config */ + +static int internal_read_config (void) /* {{{ */ +{ + oconfig_item_t *ci; + + ci = oconfig_parse_file (CONFIG_FILE); + if (ci == NULL) + return (-1); + + dispatch_config (ci); + + oconfig_free (ci); + + graph_config_submit (); + + return (0); +} /* }}} int internal_read_config */ + +static time_t get_config_mtime (void) /* {{{ */ +{ + struct stat statbuf; + int status; + + memset (&statbuf, 0, sizeof (statbuf)); + status = stat (CONFIG_FILE, &statbuf); + if (status != 0) + return (0); + + return (statbuf.st_mtime); +} /* }}} time_t get_config_mtime */ + +int graph_read_config (void) /* {{{ */ +{ + time_t mtime; + + mtime = get_config_mtime (); + + if (mtime <= last_read_mtime) + return (0); + + internal_read_config (); + + last_read_mtime = mtime; + + return (0); +} /* }}} int graph_read_config */ + +int graph_config_get_string (const oconfig_item_t *ci, /* {{{ */ + char **ret_str) +{ + char *tmp; + + if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) + return (EINVAL); + + tmp = strdup (ci->values[0].value.string); + if (tmp == NULL) + return (ENOMEM); + + free (*ret_str); + *ret_str = tmp; + + return (0); +} /* }}} int graph_config_get_string */ + +int graph_config_get_bool (const oconfig_item_t *ci, /* {{{ */ + _Bool *ret_bool) +{ + if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN)) + return (EINVAL); + + if (ci->values[0].value.boolean) + *ret_bool = 1; + else + *ret_bool = 0; + + return (0); +} /* }}} int graph_config_get_bool */ + +/* vim: set sw=2 sts=2 et fdm=marker : */ diff --git a/graph_config.h b/graph_config.h new file mode 100644 index 0000000..c468eec --- /dev/null +++ b/graph_config.h @@ -0,0 +1,12 @@ +#ifndef GRAPH_CONFIG_H +#define GRAPH_CONFIG_H 1 + +#include "oconfig.h" + +int graph_read_config (void); + +int graph_config_get_string (const oconfig_item_t *ci, char **ret_str); +int graph_config_get_bool (const oconfig_item_t *ci, _Bool *ret_bool); + +/* vim: set sw=2 sts=2 et fdm=marker : */ +#endif /* GRAPH_CONFIG_H */