~iheino+ub/+junk/nut-upsconf-docfix

« back to all changes in this revision

Viewing changes to server/desc.c

  • Committer: Tuomas Heino
  • Author(s): Laurent Bigonville
  • Date: 2014-04-22 20:46:12 UTC
  • Revision ID: iheino+ub@cc.hut.fi-20140422204612-1x2gh3nkezfsdao4
Tags: upstream-2.7.2
ImportĀ upstreamĀ versionĀ 2.7.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* desc.c - variable/command description handling for upsd
 
2
 
 
3
   Copyright (C) 2003  Russell Kroll <rkroll@exploits.org>
 
4
 
 
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.
 
9
 
 
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.
 
14
 
 
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
 
18
*/
 
19
 
 
20
#include <string.h>
 
21
 
 
22
#include "common.h"
 
23
#include "parseconf.h"
 
24
 
 
25
#include "desc.h"
 
26
 
 
27
extern const char *datapath;
 
28
 
 
29
typedef struct dlist_s {
 
30
        char    *name;
 
31
        char    *desc;
 
32
        struct dlist_s  *next;
 
33
} dlist_t;
 
34
 
 
35
static dlist_t  *cmd_list = NULL, *var_list = NULL;
 
36
 
 
37
static void list_free(dlist_t *ptr)
 
38
{
 
39
        dlist_t *next;
 
40
 
 
41
        while (ptr) {
 
42
                next = ptr->next;
 
43
 
 
44
                free(ptr->name);
 
45
                free(ptr->desc);
 
46
                free(ptr);
 
47
 
 
48
                ptr = next;
 
49
        }
 
50
}
 
51
 
 
52
static const char *list_get(const dlist_t *list, const char *name)
 
53
{
 
54
        const dlist_t   *temp;
 
55
 
 
56
        for (temp = list; temp != NULL; temp = temp->next) {
 
57
 
 
58
                if (!strcasecmp(temp->name, name)) {
 
59
                        return temp->desc;
 
60
                }
 
61
        }
 
62
 
 
63
        return NULL;
 
64
}
 
65
 
 
66
static void desc_add(dlist_t **list, const char *name, const char *desc)
 
67
{
 
68
        dlist_t *temp;
 
69
 
 
70
        for (temp = *list; temp != NULL; temp = temp->next) {
 
71
                if (!strcasecmp(temp->name, name)) {
 
72
                        break;
 
73
                }
 
74
        }
 
75
 
 
76
        if (temp == NULL) {
 
77
                temp = xcalloc(1, sizeof(*temp));
 
78
                temp->name = xstrdup(name);
 
79
                temp->next = *list;
 
80
                *list = temp;
 
81
        }
 
82
 
 
83
        free(temp->desc);
 
84
        temp->desc = xstrdup(desc);
 
85
}
 
86
 
 
87
static void desc_file_err(const char *errmsg)
 
88
{
 
89
        upslogx(LOG_ERR, "Fatal error in parseconf (cmdvartab): %s", errmsg);
 
90
}
 
91
 
 
92
/* interface */
 
93
 
 
94
void desc_load(void)
 
95
{
 
96
        char    fn[SMALLBUF];
 
97
        PCONF_CTX_t     ctx;
 
98
 
 
99
        snprintf(fn, sizeof(fn), "%s/cmdvartab", datapath);
 
100
 
 
101
        pconf_init(&ctx, desc_file_err);
 
102
 
 
103
        /* this file is not required */
 
104
        if (!pconf_file_begin(&ctx, fn)) {
 
105
                upslogx(LOG_INFO, "%s not found - disabling descriptions", fn);
 
106
                pconf_finish(&ctx);
 
107
                return;
 
108
        }
 
109
 
 
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);
 
113
                        continue;
 
114
                }
 
115
 
 
116
                if (ctx.numargs < 3) {
 
117
                        continue;
 
118
                }
 
119
 
 
120
                if (!strcmp(ctx.arglist[0], "CMDDESC")) {
 
121
                        desc_add(&cmd_list, ctx.arglist[1], ctx.arglist[2]);
 
122
                        continue;
 
123
                }
 
124
 
 
125
                if (!strcmp(ctx.arglist[0], "VARDESC")) {
 
126
                        desc_add(&var_list, ctx.arglist[1], ctx.arglist[2]);
 
127
                        continue;
 
128
                }
 
129
 
 
130
                /* unknown */
 
131
        }
 
132
 
 
133
        pconf_finish(&ctx);
 
134
}
 
135
 
 
136
void desc_free(void)
 
137
{
 
138
        list_free(cmd_list);
 
139
        list_free(var_list);
 
140
 
 
141
        cmd_list = var_list = NULL;
 
142
}
 
143
 
 
144
const char *desc_get_cmd(const char *name)
 
145
{
 
146
        return list_get(cmd_list, name);
 
147
}
 
148
 
 
149
const char *desc_get_var(const char *name)
 
150
{
 
151
        return list_get(var_list, name);
 
152
}