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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
/* 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
*/
#include <string.h>
#include "common.h"
#include "parseconf.h"
#include "desc.h"
extern const char *datapath;
struct dlist_t {
char *name;
char *desc;
struct dlist_t *next;
};
static struct dlist_t *cmd_list = NULL, *var_list = NULL;
static void list_free(struct dlist_t *ptr)
{
struct dlist_t *next;
while (ptr) {
next = ptr->next;
free(ptr->name);
free(ptr->desc);
free(ptr);
ptr = next;
}
}
static const char *list_get(const struct dlist_t *list, const char *name)
{
const struct dlist_t *tmp;
tmp = list;
while (tmp) {
if (!strcasecmp(tmp->name, name))
return tmp->desc;
tmp = tmp->next;
}
return NULL;
}
static void desc_add(struct dlist_t **list, const char *name, const char *desc)
{
struct dlist_t *tmp, *last;
tmp = last = *list;
while (tmp) {
last = tmp;
/* replace duplicates */
if (!strcasecmp(tmp->name, name)) {
free(tmp->desc);
tmp->desc = xstrdup(desc);
return;
}
tmp = tmp->next;
}
tmp = xmalloc(sizeof(struct dlist_t));
tmp->name = xstrdup(name);
tmp->desc = xstrdup(desc);
tmp->next = NULL;
if (last)
last->next = tmp;
else
*list = tmp;
}
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)) {
upslogx(LOG_ERR, "Parse error: %s:%d: %s",
fn, ctx.linenum, ctx.errmsg);
continue;
}
if (ctx.numargs < 3)
continue;
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);
}
|