diff options
Diffstat (limited to 'lib/config.c')
-rw-r--r-- | lib/config.c | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/lib/config.c b/lib/config.c new file mode 100644 index 0000000..8fcb926 --- /dev/null +++ b/lib/config.c @@ -0,0 +1,115 @@ +/* This file is part of Pies. + Copyright (C) 2007, 2008, 2009 Sergey Poznyakoff + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. */ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include <libpies.h> + + +char ** +config_array_to_argv (mu_config_value_t *val, mu_debug_t debug) +{ + int i, j; + int argc; + char **argv; + + argc = val->v.arg.c; + argv = xcalloc (argc + 1, sizeof (argv[0])); + for (i = j = 0; i < argc; i++) + { + if (mu_cfg_assert_value_type (&val->v.arg.v[i], MU_CFG_STRING, + debug) == 0) + argv[j++] = xstrdup (val->v.arg.v[i].v.string); + } + argv[j] = NULL; + return argv; +} + +char * +config_array_to_string (mu_config_value_t *val, mu_debug_t debug) +{ + size_t len = 0; + int i; + char *str, *p; + + for (i = 0; i < val->v.arg.c; i++) + { + if (mu_cfg_assert_value_type (&val->v.arg.v[i], MU_CFG_STRING, debug)) + return NULL; + len += strlen (val->v.arg.v[i].v.string) + 1; + } + + str = xmalloc (len); + p = str; + for (i = 0; i < val->v.arg.c; i++) + { + size_t n = strlen (val->v.arg.v[i].v.string); + memcpy (p, val->v.arg.v[i].v.string, n); + p += n; + *p++ = ' '; + } + str[len-1] = 0; + return str; +} + + +int +config_cb_timeout (struct timeval *pt, mu_debug_t debug, + mu_config_value_t *val) +{ + int rc; + const char *endp; + time_t t; + const char *str; + char *alloc_str = NULL; + + switch (val->type) + { + case MU_CFG_STRING: + str = val->v.string; + break; + + case MU_CFG_ARRAY: + str = alloc_str = config_array_to_string (val, debug); + if (!str) + return 1; + break; + + case MU_CFG_LIST: + mu_cfg_format_error (debug, MU_DEBUG_ERROR, + _("unexpected list")); + return 1; + } + + rc = parse_time_interval (str, &t, &endp); + if (rc) + mu_cfg_format_error (debug, MU_DEBUG_ERROR, + _("unrecognized time format (near `%s')"), + endp); + else + { + pt->tv_usec = 0; + pt->tv_sec = t; + } + free (alloc_str); + return 0; +} + + + + |