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"
27
extern const char *datapath;
29
typedef struct dlist_s {
35
static dlist_t *cmd_list = NULL, *var_list = NULL;
37
static void list_free(dlist_t *ptr)
52
static const char *list_get(const dlist_t *list, const char *name)
56
for (temp = list; temp != NULL; temp = temp->next) {
58
if (!strcasecmp(temp->name, name)) {
66
static void desc_add(dlist_t **list, const char *name, const char *desc)
70
for (temp = *list; temp != NULL; temp = temp->next) {
71
if (!strcasecmp(temp->name, name)) {
77
temp = xcalloc(1, sizeof(*temp));
78
temp->name = xstrdup(name);
84
temp->desc = xstrdup(desc);
87
static void desc_file_err(const char *errmsg)
89
upslogx(LOG_ERR, "Fatal error in parseconf (cmdvartab): %s", errmsg);
99
snprintf(fn, sizeof(fn), "%s/cmdvartab", datapath);
101
pconf_init(&ctx, desc_file_err);
103
/* this file is not required */
104
if (!pconf_file_begin(&ctx, fn)) {
105
upslogx(LOG_INFO, "%s not found - disabling descriptions", fn);
110
while (pconf_file_next(&ctx)) {
111
if (pconf_parse_error(&ctx)) {
112
upslogx(LOG_ERR, "Parse error: %s:%d: %s", fn, ctx.linenum, ctx.errmsg);
116
if (ctx.numargs < 3) {
120
if (!strcmp(ctx.arglist[0], "CMDDESC")) {
121
desc_add(&cmd_list, ctx.arglist[1], ctx.arglist[2]);
125
if (!strcmp(ctx.arglist[0], "VARDESC")) {
126
desc_add(&var_list, ctx.arglist[1], ctx.arglist[2]);
141
cmd_list = var_list = NULL;
144
const char *desc_get_cmd(const char *name)
146
return list_get(cmd_list, name);
149
const char *desc_get_var(const char *name)
151
return list_get(var_list, name);