1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
|
/* upsconf.c - code for handling ups.conf ini-style parsing
Copyright (C) 2001 Russell Kroll <rkroll@exploits.org>
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 2 of the License, 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, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "config.h" /* must be the first header */
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include "upsconf.h"
#include "common.h"
#include "parseconf.h"
static char *ups_section;
/* handle arguments separated by parseconf */
static void conf_args(size_t numargs, char **arg)
{
if (numargs < 1)
return;
/* look for section headers - [upsname] */
if ((arg[0][0] == '[') && (arg[0][strlen(arg[0])-1] == ']')) {
free(ups_section);
arg[0][strlen(arg[0])-1] = '\0';
ups_section = xstrdup(&arg[0][1]);
return;
}
/* handle 'foo' (flag) */
if (numargs == 1) {
do_upsconf_args(ups_section, arg[0], NULL);
return;
}
if (numargs < 3)
return;
/* handle 'foo = bar', 'foo=bar', 'foo =bar' or 'foo= bar' forms */
if (!strcmp(arg[1], "=")) {
do_upsconf_args(ups_section, arg[0], arg[2]);
return;
}
}
/* called for fatal errors in parseconf like malloc failures */
static void upsconf_err(const char *errmsg)
{
upslogx(LOG_ERR, "Fatal error in parseconf(ups.conf): %s", errmsg);
}
/* open the ups.conf, parse it, and call back do_upsconf_args()
* returns -1 (or aborts the program) in case of errors;
* returns 1 if processing finished successfully
* See also reload_flag support in main.c for live-reload feature
*/
int read_upsconf(int fatal_errors)
{
char fn[NUT_PATH_MAX + 1];
PCONF_CTX_t ctx;
ups_section = NULL;
snprintf(fn, sizeof(fn), "%s/ups.conf", confpath());
pconf_init(&ctx, upsconf_err);
if (!pconf_file_begin(&ctx, fn)) {
if (fatal_errors) {
fatalx(EXIT_FAILURE, "Can't open %s: %s", fn, ctx.errmsg);
} else {
upslogx(LOG_WARNING, "Can't open %s: %s", fn, ctx.errmsg);
return -1;
}
}
while (pconf_file_next(&ctx)) {
if (pconf_parse_error(&ctx)) {
upslogx(LOG_ERR, "Parse error: %s:%d: %s",
fn, ctx.linenum, ctx.errmsg);
continue;
}
conf_args(ctx.numargs, ctx.arglist);
}
pconf_finish(&ctx);
free(ups_section);
return 1; /* Handled OK */
}
|