nut-debian/server/desc.c

155 lines
3.0 KiB
C
Raw Permalink Normal View History

2010-03-26 01:20:59 +02:00
/* desc.c - variable/command description handling for upsd
Copyright (C) 2003 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
*/
2022-07-10 10:23:45 +03:00
#include "config.h" /* must be the first header */
2010-03-26 01:20:59 +02:00
#include <string.h>
#include "common.h"
#include "parseconf.h"
#include "desc.h"
extern const char *datapath;
2011-01-26 11:35:08 +02:00
typedef struct dlist_s {
2010-03-26 01:20:59 +02:00
char *name;
char *desc;
2011-01-26 11:35:08 +02:00
struct dlist_s *next;
} dlist_t;
2010-03-26 01:20:59 +02:00
2011-01-26 11:35:08 +02:00
static dlist_t *cmd_list = NULL, *var_list = NULL;
2010-03-26 01:20:59 +02:00
2011-01-26 11:35:08 +02:00
static void list_free(dlist_t *ptr)
2010-03-26 01:20:59 +02:00
{
2011-01-26 11:35:08 +02:00
dlist_t *next;
2010-03-26 01:20:59 +02:00
while (ptr) {
next = ptr->next;
free(ptr->name);
free(ptr->desc);
free(ptr);
ptr = next;
}
}
2011-01-26 11:35:08 +02:00
static const char *list_get(const dlist_t *list, const char *name)
2010-03-26 01:20:59 +02:00
{
2011-01-26 11:35:08 +02:00
const dlist_t *temp;
2010-03-26 01:20:59 +02:00
2011-01-26 11:35:08 +02:00
for (temp = list; temp != NULL; temp = temp->next) {
2010-03-26 01:20:59 +02:00
2011-01-26 11:35:08 +02:00
if (!strcasecmp(temp->name, name)) {
return temp->desc;
}
2010-03-26 01:20:59 +02:00
}
return NULL;
}
2011-01-26 11:35:08 +02:00
static void desc_add(dlist_t **list, const char *name, const char *desc)
2010-03-26 01:20:59 +02:00
{
2011-01-26 11:35:08 +02:00
dlist_t *temp;
2010-03-26 01:20:59 +02:00
2011-01-26 11:35:08 +02:00
for (temp = *list; temp != NULL; temp = temp->next) {
if (!strcasecmp(temp->name, name)) {
break;
2010-03-26 01:20:59 +02:00
}
}
2011-01-26 11:35:08 +02:00
if (temp == NULL) {
temp = xcalloc(1, sizeof(*temp));
temp->name = xstrdup(name);
temp->next = *list;
*list = temp;
}
2010-03-26 01:20:59 +02:00
2011-01-26 11:35:08 +02:00
free(temp->desc);
temp->desc = xstrdup(desc);
2010-03-26 01:20:59 +02:00
}
static void desc_file_err(const char *errmsg)
{
upslogx(LOG_ERR, "Fatal error in parseconf (cmdvartab): %s", errmsg);
}
/* interface */
void desc_load(void)
{
char fn[SMALLBUF];
PCONF_CTX_t ctx;
snprintf(fn, sizeof(fn), "%s/cmdvartab", datapath);
pconf_init(&ctx, desc_file_err);
/* this file is not required */
if (!pconf_file_begin(&ctx, fn)) {
upslogx(LOG_INFO, "%s not found - disabling descriptions", fn);
pconf_finish(&ctx);
return;
}
while (pconf_file_next(&ctx)) {
if (pconf_parse_error(&ctx)) {
2011-01-26 11:35:08 +02:00
upslogx(LOG_ERR, "Parse error: %s:%d: %s", fn, ctx.linenum, ctx.errmsg);
2010-03-26 01:20:59 +02:00
continue;
}
2011-01-26 11:35:08 +02:00
if (ctx.numargs < 3) {
2010-03-26 01:20:59 +02:00
continue;
2011-01-26 11:35:08 +02:00
}
2010-03-26 01:20:59 +02:00
if (!strcmp(ctx.arglist[0], "CMDDESC")) {
desc_add(&cmd_list, ctx.arglist[1], ctx.arglist[2]);
continue;
}
if (!strcmp(ctx.arglist[0], "VARDESC")) {
desc_add(&var_list, ctx.arglist[1], ctx.arglist[2]);
continue;
}
/* unknown */
}
pconf_finish(&ctx);
}
void desc_free(void)
{
list_free(cmd_list);
list_free(var_list);
cmd_list = var_list = NULL;
}
const char *desc_get_cmd(const char *name)
{
return list_get(cmd_list, name);
}
const char *desc_get_var(const char *name)
{
return list_get(var_list, name);
}