Merge pull request #2680 from masterwishx/work2471-eco_addon
[networkupstools.git] / server / desc.c
blobe6b08f0d914b62bcd71b67964ecf409c17a98a52
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 */
22 #include <string.h>
24 #include "common.h"
25 #include "parseconf.h"
27 #include "desc.h"
29 extern const char *datapath;
31 typedef struct dlist_s {
32 char *name;
33 char *desc;
34 struct dlist_s *next;
35 } dlist_t;
37 static dlist_t *cmd_list = NULL, *var_list = NULL;
39 static void list_free(dlist_t *ptr)
41 dlist_t *next;
43 while (ptr) {
44 next = ptr->next;
46 free(ptr->name);
47 free(ptr->desc);
48 free(ptr);
50 ptr = next;
54 static const char *list_get(const dlist_t *list, const char *name)
56 const dlist_t *temp;
58 for (temp = list; temp != NULL; temp = temp->next) {
60 if (!strcasecmp(temp->name, name)) {
61 return temp->desc;
65 return NULL;
68 static void desc_add(dlist_t **list, const char *name, const char *desc)
70 dlist_t *temp;
72 for (temp = *list; temp != NULL; temp = temp->next) {
73 if (!strcasecmp(temp->name, name)) {
74 break;
78 if (temp == NULL) {
79 temp = xcalloc(1, sizeof(*temp));
80 temp->name = xstrdup(name);
81 temp->next = *list;
82 *list = temp;
85 free(temp->desc);
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);
94 /* interface */
96 void desc_load(void)
98 char fn[SMALLBUF];
99 PCONF_CTX_t ctx;
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);
108 pconf_finish(&ctx);
109 return;
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);
115 continue;
118 if (ctx.numargs < 3) {
119 continue;
122 if (!strcmp(ctx.arglist[0], "CMDDESC")) {
123 desc_add(&cmd_list, ctx.arglist[1], ctx.arglist[2]);
124 continue;
127 if (!strcmp(ctx.arglist[0], "VARDESC")) {
128 desc_add(&var_list, ctx.arglist[1], ctx.arglist[2]);
129 continue;
132 /* unknown */
135 pconf_finish(&ctx);
138 void desc_free(void)
140 list_free(cmd_list);
141 list_free(var_list);
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);