~ubuntu-branches/ubuntu/saucy/nut/saucy

« back to all changes in this revision

Viewing changes to server/desc.c

  • Committer: Bazaar Package Importer
  • Author(s): Arnaud Quette
  • Date: 2004-05-28 13:10:01 UTC
  • mto: (16.1.1 squeeze)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20040528131001-yj2m9qcez4ya2w14
Tags: upstream-1.4.2
ImportĀ upstreamĀ versionĀ 1.4.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
extern const char *datapath;
 
26
 
 
27
struct dlist_t {
 
28
        char    *name;
 
29
        char    *desc;
 
30
        struct  dlist_t *next;
 
31
};
 
32
 
 
33
        static  struct  dlist_t *cmd_list = NULL, *var_list = NULL;
 
34
 
 
35
static void list_free(struct dlist_t *ptr)
 
36
{
 
37
        struct  dlist_t *next;
 
38
 
 
39
        while (ptr) {
 
40
                next = ptr->next;
 
41
 
 
42
                if (ptr->name)
 
43
                        free(ptr->name);
 
44
                if (ptr->desc)
 
45
                        free(ptr->desc);
 
46
                free(ptr);
 
47
 
 
48
                ptr = next;
 
49
        }
 
50
}
 
51
 
 
52
static const char *list_get(const struct dlist_t *list, const char *name)
 
53
{
 
54
        const   struct  dlist_t *tmp;
 
55
 
 
56
        tmp = list;
 
57
 
 
58
        while (tmp) {
 
59
                if (!strcasecmp(tmp->name, name))
 
60
                        return tmp->desc;
 
61
 
 
62
                tmp = tmp->next;
 
63
        }
 
64
 
 
65
        return NULL;
 
66
}
 
67
 
 
68
static void desc_add(struct dlist_t **list, const char *name, const char *desc)
 
69
{
 
70
        struct  dlist_t *tmp, *last;
 
71
 
 
72
        tmp = last = *list;
 
73
 
 
74
        while (tmp) {
 
75
                last = tmp;
 
76
 
 
77
                /* replace duplicates */
 
78
                if (!strcasecmp(tmp->name, name)) {
 
79
                        if (tmp->desc)
 
80
                                free(tmp->desc);
 
81
 
 
82
                        tmp->desc = xstrdup(desc);
 
83
                        return;
 
84
                }
 
85
 
 
86
                tmp = tmp->next;
 
87
        }
 
88
 
 
89
        tmp = xmalloc(sizeof(struct dlist_t));
 
90
 
 
91
        tmp->name = xstrdup(name);
 
92
        tmp->desc = xstrdup(desc);
 
93
        tmp->next = NULL;
 
94
 
 
95
        if (last)
 
96
                last->next = tmp;
 
97
        else
 
98
                *list = tmp;
 
99
}
 
100
 
 
101
void desc_file_err(const char *errmsg)
 
102
{
 
103
        upslogx(LOG_ERR, "Fatal error in parseconf (cmdvartab): %s", errmsg);
 
104
}
 
105
 
 
106
/* interface */
 
107
 
 
108
void desc_load(void)
 
109
{
 
110
        char    fn[SMALLBUF];
 
111
        PCONF_CTX       ctx;
 
112
 
 
113
        snprintf(fn, sizeof(fn), "%s/cmdvartab", datapath);
 
114
 
 
115
        pconf_init(&ctx, desc_file_err);
 
116
 
 
117
        /* this file is not required */
 
118
        if (!pconf_file_begin(&ctx, fn)) {
 
119
                upslogx(LOG_INFO, "%s not found - disabling descriptions", fn);
 
120
                pconf_finish(&ctx);
 
121
                return;
 
122
        }
 
123
 
 
124
        while (pconf_file_next(&ctx)) {
 
125
                if (pconf_parse_error(&ctx)) {
 
126
                        upslogx(LOG_ERR, "Parse error: %s:%d: %s",
 
127
                                fn, ctx.linenum, ctx.errmsg);
 
128
                        
 
129
                        continue;
 
130
                }
 
131
                        
 
132
                if (ctx.numargs < 3)
 
133
                        continue;
 
134
 
 
135
                if (!strcmp(ctx.arglist[0], "CMDDESC")) {
 
136
                        desc_add(&cmd_list, ctx.arglist[1], ctx.arglist[2]);
 
137
                        continue;
 
138
                }
 
139
 
 
140
                if (!strcmp(ctx.arglist[0], "VARDESC")) {
 
141
                        desc_add(&var_list, ctx.arglist[1], ctx.arglist[2]);
 
142
                        continue;
 
143
                }
 
144
 
 
145
                /* unknown */
 
146
        }
 
147
 
 
148
        pconf_finish(&ctx);
 
149
}
 
150
 
 
151
void desc_free(void)
 
152
{
 
153
        list_free(cmd_list);
 
154
        list_free(var_list);
 
155
 
 
156
        cmd_list = var_list = NULL;
 
157
}
 
158
 
 
159
const char *desc_get_cmd(const char *name)
 
160
{
 
161
        return list_get(cmd_list, name);
 
162
}
 
163
 
 
164
const char *desc_get_var(const char *name)
 
165
{
 
166
        return list_get(var_list, name);
 
167
}