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
20 #include "config.h" /* must be the first header */
25 #include "parseconf.h"
29 extern const char *datapath
;
31 typedef struct dlist_s
{
37 static dlist_t
*cmd_list
= NULL
, *var_list
= NULL
;
39 static void list_free(dlist_t
*ptr
)
54 static const char *list_get(const dlist_t
*list
, const char *name
)
58 for (temp
= list
; temp
!= NULL
; temp
= temp
->next
) {
60 if (!strcasecmp(temp
->name
, name
)) {
68 static void desc_add(dlist_t
**list
, const char *name
, const char *desc
)
72 for (temp
= *list
; temp
!= NULL
; temp
= temp
->next
) {
73 if (!strcasecmp(temp
->name
, name
)) {
79 temp
= xcalloc(1, sizeof(*temp
));
80 temp
->name
= xstrdup(name
);
86 temp
->desc
= xstrdup(desc
);
89 static void desc_file_err(const char *errmsg
)
91 upslogx(LOG_ERR
, "Fatal error in parseconf (cmdvartab): %s", errmsg
);
101 snprintf(fn
, sizeof(fn
), "%s/cmdvartab", datapath
);
103 pconf_init(&ctx
, desc_file_err
);
105 /* this file is not required */
106 if (!pconf_file_begin(&ctx
, fn
)) {
107 upslogx(LOG_INFO
, "%s not found - disabling descriptions", fn
);
112 while (pconf_file_next(&ctx
)) {
113 if (pconf_parse_error(&ctx
)) {
114 upslogx(LOG_ERR
, "Parse error: %s:%d: %s", fn
, ctx
.linenum
, ctx
.errmsg
);
118 if (ctx
.numargs
< 3) {
122 if (!strcmp(ctx
.arglist
[0], "CMDDESC")) {
123 desc_add(&cmd_list
, ctx
.arglist
[1], ctx
.arglist
[2]);
127 if (!strcmp(ctx
.arglist
[0], "VARDESC")) {
128 desc_add(&var_list
, ctx
.arglist
[1], ctx
.arglist
[2]);
143 cmd_list
= var_list
= NULL
;
146 const char *desc_get_cmd(const char *name
)
148 return list_get(cmd_list
, name
);
151 const char *desc_get_var(const char *name
)
153 return list_get(var_list
, name
);