1
/* desc.c - variable/command description handling for upsd
3
Copyright (C) 2003 Russell Kroll <rkroll@exploits.org>
5
This program is free software; you can redistribute it and/or modify
6
it under the terms of the GNU General Public License as published by
7
the Free Software Foundation; either version 2 of the License, or
8
(at your option) any later version.
10
This program is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
GNU General Public License for more details.
15
You should have received a copy of the GNU General Public License
16
along with this program; if not, write to the Free Software
17
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23
#include "parseconf.h"
25
extern const char *datapath;
33
static struct dlist_t *cmd_list = NULL, *var_list = NULL;
35
static void list_free(struct dlist_t *ptr)
52
static const char *list_get(const struct dlist_t *list, const char *name)
54
const struct dlist_t *tmp;
59
if (!strcasecmp(tmp->name, name))
68
static void desc_add(struct dlist_t **list, const char *name, const char *desc)
70
struct dlist_t *tmp, *last;
77
/* replace duplicates */
78
if (!strcasecmp(tmp->name, name)) {
82
tmp->desc = xstrdup(desc);
89
tmp = xmalloc(sizeof(struct dlist_t));
91
tmp->name = xstrdup(name);
92
tmp->desc = xstrdup(desc);
101
void desc_file_err(const char *errmsg)
103
upslogx(LOG_ERR, "Fatal error in parseconf (cmdvartab): %s", errmsg);
113
snprintf(fn, sizeof(fn), "%s/cmdvartab", datapath);
115
pconf_init(&ctx, desc_file_err);
117
/* this file is not required */
118
if (!pconf_file_begin(&ctx, fn)) {
119
upslogx(LOG_INFO, "%s not found - disabling descriptions", fn);
124
while (pconf_file_next(&ctx)) {
125
if (pconf_parse_error(&ctx)) {
126
upslogx(LOG_ERR, "Parse error: %s:%d: %s",
127
fn, ctx.linenum, ctx.errmsg);
135
if (!strcmp(ctx.arglist[0], "CMDDESC")) {
136
desc_add(&cmd_list, ctx.arglist[1], ctx.arglist[2]);
140
if (!strcmp(ctx.arglist[0], "VARDESC")) {
141
desc_add(&var_list, ctx.arglist[1], ctx.arglist[2]);
156
cmd_list = var_list = NULL;
159
const char *desc_get_cmd(const char *name)
161
return list_get(cmd_list, name);
164
const char *desc_get_var(const char *name)
166
return list_get(var_list, name);