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

« back to all changes in this revision

Viewing changes to common/state.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
/* state.c - Network UPS Tools common state management functions
 
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 <stdio.h>
 
21
#include <stdarg.h>
 
22
#include <sys/stat.h>
 
23
#include <sys/types.h>
 
24
#include <sys/socket.h>
 
25
#include <sys/un.h>
 
26
 
 
27
#include "common.h"
 
28
#include "state.h"
 
29
#include "parseconf.h"
 
30
 
 
31
static void val_escape(struct st_tree_t *node)
 
32
{
 
33
        char    etmp[ST_MAX_VALUE_LEN];
 
34
 
 
35
        /* escape any tricky stuff like \ and " */
 
36
        pconf_encode(node->val, etmp, sizeof(etmp));
 
37
 
 
38
        /* if nothing was escaped, we don't need to do anything else */
 
39
        if (!strcmp(node->raw, etmp)) {
 
40
                node->val = node->raw;
 
41
                return;
 
42
        }
 
43
 
 
44
        /* first time: set a good starting place */
 
45
        if (node->safesize == 0) {
 
46
                node->safesize = strlen(etmp) + 1;
 
47
                node->safe = xmalloc(node->safesize);
 
48
        }
 
49
 
 
50
        /* if the escaped value grew, deal with it */
 
51
        if (strlen(etmp) > (node->safesize - 1)) {
 
52
                node->safesize = strlen(etmp) + 1;
 
53
                node->safe = xrealloc(node->safe, node->safesize);
 
54
        }
 
55
 
 
56
        snprintf(node->safe, node->safesize, "%s", etmp);
 
57
        node->val = node->safe;
 
58
}
 
59
 
 
60
/* free all memory associated with a node */
 
61
static void st_tree_node_free(struct st_tree_t *node)
 
62
{
 
63
        struct  enum_t  *tmp, *next;
 
64
 
 
65
        if (node->var)
 
66
                free(node->var);
 
67
        if (node->raw)
 
68
                free(node->raw);
 
69
        if (node->safe)
 
70
                free(node->safe);
 
71
 
 
72
        /* never free node->val, since it's just a pointer to raw or safe */
 
73
 
 
74
        /* blow away the list of enums */
 
75
        tmp = node->enum_list;
 
76
        while (tmp) {
 
77
                next = tmp->next;
 
78
 
 
79
                free(tmp->val);
 
80
                free(tmp);
 
81
 
 
82
                tmp = next;
 
83
        }
 
84
 
 
85
        /* now finally kill the node itself */
 
86
        free(node);
 
87
}
 
88
 
 
89
/* add a subtree to another subtree */
 
90
static void st_tree_node_add(struct st_tree_t **rptr, struct st_tree_t *node)
 
91
{
 
92
        struct  st_tree_t *root = *rptr;
 
93
 
 
94
        if (!root) {
 
95
                *rptr = node;
 
96
                return;
 
97
        }
 
98
 
 
99
        if (strcmp(node->var, root->var) < 0) {
 
100
                st_tree_node_add(&root->left, node);
 
101
                return;
 
102
        }
 
103
 
 
104
        st_tree_node_add(&root->right, node);
 
105
}
 
106
 
 
107
static int st_tree_delete(struct st_tree_t **nptr, struct st_tree_t **lptr,
 
108
        const char *var)
 
109
{
 
110
        int     cmp, ret;
 
111
        struct  st_tree_t       *node, *last;
 
112
 
 
113
        if (!nptr)
 
114
                return 0;
 
115
 
 
116
        node = *nptr;
 
117
 
 
118
        if (!node)
 
119
                return 0;
 
120
 
 
121
        if (lptr)
 
122
                last = *lptr;
 
123
        else
 
124
                last = NULL;
 
125
 
 
126
        cmp = strcasecmp(var, node->var);
 
127
 
 
128
        if (cmp == 0) {         /* found the right one */
 
129
 
 
130
                /* deleting the root? */
 
131
                if (!last) {
 
132
 
 
133
                        /* root with two children? */
 
134
                        if ((node->left) && (node->right)) {
 
135
 
 
136
                                /* hang current left off current right */
 
137
                                st_tree_node_add(&node->right, node->left);
 
138
 
 
139
                                /* now point the root at the old right child */
 
140
                                *nptr = node->right;
 
141
 
 
142
                                st_tree_node_free(node);
 
143
                                return 1;
 
144
                        }
 
145
 
 
146
                        /* root with one child (left) */
 
147
                        if (node->left) {
 
148
 
 
149
                                /* point root at left child */
 
150
                                *nptr = node->left;
 
151
 
 
152
                                st_tree_node_free(node);
 
153
                                return 1;
 
154
                        }
 
155
 
 
156
                        /* root with one child (right) */
 
157
                        if (node->right) {
 
158
 
 
159
                                /* point root at right child */
 
160
                                *nptr = node->right;
 
161
 
 
162
                                st_tree_node_free(node);
 
163
                                return 1;
 
164
                        }
 
165
 
 
166
                        /* root with no children */
 
167
 
 
168
                        /* point root at an empty tree */
 
169
                        *nptr = NULL;
 
170
 
 
171
                        st_tree_node_free(node);
 
172
                        return 1;
 
173
                }
 
174
 
 
175
                /* leaf */
 
176
                if ((!node->left) && (!node->right)) {
 
177
 
 
178
                        if (last->right == node)
 
179
                                last->right = NULL;
 
180
                        else
 
181
                                last->left = NULL;
 
182
 
 
183
                        st_tree_node_free(node);
 
184
                        return 1;
 
185
                }
 
186
 
 
187
                /* node with two children */
 
188
                if ((node->left) && (node->right)) {
 
189
 
 
190
                        /* hang the current left off the current right */
 
191
                        st_tree_node_add(&node->right, node->left);
 
192
 
 
193
                        if (last->left == node)
 
194
                                last->left = node->right;
 
195
                        else
 
196
                                last->right = node->right;
 
197
 
 
198
 
 
199
                        st_tree_node_free(node);
 
200
                        return 1;
 
201
                }
 
202
 
 
203
                /* node with one child (left) */
 
204
                if (node->left) {
 
205
 
 
206
                        if (last->right == node)
 
207
                                last->right = node->left;
 
208
                        else
 
209
                                last->left = node->left;
 
210
 
 
211
                        st_tree_node_free(node);
 
212
                        return 1;
 
213
                }
 
214
 
 
215
                /* node with one child (right) */
 
216
 
 
217
                if (last->right == node)
 
218
                        last->right = node->right;
 
219
                else
 
220
                        last->left = node->right;
 
221
 
 
222
                st_tree_node_free(node);
 
223
                return 1;
 
224
        }
 
225
 
 
226
        if (cmp < 0) {
 
227
                ret = st_tree_delete(&node->left, nptr, var);
 
228
 
 
229
                if (ret != 0)
 
230
                        return 1;
 
231
        }
 
232
 
 
233
        return st_tree_delete(&node->right, nptr, var);
 
234
}       
 
235
 
 
236
/* interface */
 
237
 
 
238
int state_setinfo(struct st_tree_t **nptr, const char *var, const char *val)
 
239
{
 
240
        struct  st_tree_t       *node = *nptr;
 
241
 
 
242
        if (!node) {
 
243
                *nptr = xmalloc(sizeof(struct st_tree_t));
 
244
 
 
245
                node = *nptr;
 
246
                node->var = xstrdup(var);
 
247
 
 
248
                node->rawsize = strlen(val) + 1;
 
249
                node->raw = xmalloc(node->rawsize);
 
250
                snprintf(node->raw, node->rawsize, "%s", val);
 
251
 
 
252
                /* this is usually sufficient if nothing gets escaped */
 
253
                node->val = node->raw;
 
254
                node->safesize = 0;
 
255
                node->safe = NULL;
 
256
 
 
257
                /* but see if it needs escaping anyway */
 
258
                val_escape(node);
 
259
 
 
260
                /* these are updated by other functions */
 
261
                node->flags = 0;
 
262
                node->aux = 0;
 
263
                node->enum_list = NULL;
 
264
 
 
265
                node->left = NULL;
 
266
                node->right = NULL;
 
267
 
 
268
                return 1;       /* added */
 
269
        }
 
270
 
 
271
        if (strcasecmp(var, node->var) < 0)
 
272
                return state_setinfo(&node->left, var, val);
 
273
 
 
274
        if (strcasecmp(var, node->var) > 0)
 
275
                return state_setinfo(&node->right, var, val);
 
276
 
 
277
        /* var must equal node->var - updating an existing entry */
 
278
 
 
279
        if (!strcasecmp(node->raw, val))
 
280
                return 0;               /* no change */
 
281
 
 
282
        /* expand the buffer if the value grows */
 
283
        if (strlen(val) > (node->rawsize - 1)) {
 
284
                node->rawsize = strlen(val) + 1;
 
285
                node->raw = xrealloc(node->raw, node->rawsize);
 
286
                node->val = node->raw;
 
287
        }
 
288
 
 
289
        /* store the literal value for later comparisons */
 
290
        snprintf(node->raw, node->rawsize, "%s", val);
 
291
 
 
292
        val_escape(node);
 
293
 
 
294
        return 1;       /* added */
 
295
}
 
296
 
 
297
int state_addenum(struct st_tree_t *root, const char *var, const char *value)
 
298
{
 
299
        struct  st_tree_t       *sttmp;
 
300
        struct  enum_t  *etmp, *elast;
 
301
        char    enc[ST_MAX_VALUE_LEN];
 
302
 
 
303
        /* find the tree node for var */
 
304
        sttmp = state_tree_find(root, var);
 
305
 
 
306
        if (!sttmp) {
 
307
                upslogx(LOG_ERR, "dstate_addenum: base variable (%s) "
 
308
                        "does not exist", var);
 
309
                return 0;       /* failed */
 
310
        }
 
311
 
 
312
        /* smooth over any oddities in the enum value */
 
313
        pconf_encode(value, enc, sizeof(enc));
 
314
 
 
315
        etmp = sttmp->enum_list;
 
316
        elast = NULL;
 
317
 
 
318
        while (etmp) {
 
319
                elast = etmp;
 
320
 
 
321
                /* don't add duplicates - silently ignore them */
 
322
                if (!strcmp(etmp->val, enc))
 
323
                        return 1;
 
324
 
 
325
                etmp = etmp->next;
 
326
        }
 
327
 
 
328
        etmp = xmalloc(sizeof(struct enum_t));
 
329
        etmp->val = xstrdup(enc);
 
330
        etmp->next = NULL;
 
331
 
 
332
        if (!elast)
 
333
                sttmp->enum_list = etmp;
 
334
        else
 
335
                elast->next = etmp;
 
336
 
 
337
        return 1;
 
338
}
 
339
 
 
340
int state_setaux(struct st_tree_t *root, const char *var, const char *auxs)
 
341
{
 
342
        struct  st_tree_t       *sttmp;
 
343
        int     aux;
 
344
 
 
345
        /* find the tree node for var */
 
346
        sttmp = state_tree_find(root, var);
 
347
 
 
348
        if (!sttmp) {
 
349
                upslogx(LOG_ERR, "dstate_addenum: base variable (%s) "
 
350
                        "does not exist", var);
 
351
                return -1;      /* failed */
 
352
        }
 
353
 
 
354
        aux = strtol(auxs, (char **) NULL, 10);
 
355
 
 
356
        /* silently ignore matches */
 
357
        if (sttmp->aux == aux)
 
358
                return 0;
 
359
 
 
360
        sttmp->aux = aux;
 
361
 
 
362
        return 1;
 
363
}
 
364
 
 
365
const char *state_getinfo(struct st_tree_t *root, const char *var)
 
366
{
 
367
        struct  st_tree_t       *sttmp;
 
368
 
 
369
        /* find the tree node for var */
 
370
        sttmp = state_tree_find(root, var);
 
371
 
 
372
        if (!sttmp)
 
373
                return NULL;
 
374
 
 
375
        return sttmp->val;
 
376
}
 
377
 
 
378
int state_getflags(struct st_tree_t *root, const char *var)
 
379
{
 
380
        struct  st_tree_t       *sttmp;
 
381
 
 
382
        /* find the tree node for var */
 
383
        sttmp = state_tree_find(root, var);
 
384
 
 
385
        if (!sttmp)
 
386
                return -1;
 
387
 
 
388
        return sttmp->flags;
 
389
}
 
390
 
 
391
int state_getaux(struct st_tree_t *root, const char *var)
 
392
{
 
393
        struct  st_tree_t       *sttmp;
 
394
 
 
395
        /* find the tree node for var */
 
396
        sttmp = state_tree_find(root, var);
 
397
 
 
398
        if (!sttmp)
 
399
                return -1;
 
400
 
 
401
        return sttmp->aux;
 
402
}
 
403
 
 
404
const struct enum_t *state_getenumlist(struct st_tree_t *root, const char *var)
 
405
{
 
406
        struct  st_tree_t       *sttmp;
 
407
 
 
408
        /* find the tree node for var */
 
409
        sttmp = state_tree_find(root, var);
 
410
 
 
411
        if (!sttmp)
 
412
                return NULL;
 
413
 
 
414
        return sttmp->enum_list;
 
415
}
 
416
 
 
417
void state_setflags(struct st_tree_t *root, const char *var, int numflags,
 
418
        char **flag)
 
419
{       
 
420
        int     i;
 
421
        struct  st_tree_t       *sttmp;
 
422
 
 
423
        /* find the tree node for var */
 
424
        sttmp = state_tree_find(root, var);
 
425
 
 
426
        if (!sttmp) {
 
427
                upslogx(LOG_ERR, "dstate_setflags: base variable (%s) "
 
428
                        "does not exist", var);
 
429
                return;
 
430
        }
 
431
 
 
432
        sttmp->flags = 0;
 
433
 
 
434
        for (i = 0; i < numflags; i++) {
 
435
 
 
436
                if (!strcasecmp(flag[i], "RW")) {
 
437
                        sttmp->flags |= ST_FLAG_RW;
 
438
                        continue;
 
439
                }
 
440
 
 
441
                if (!strcasecmp(flag[i], "STRING")) {
 
442
                        sttmp->flags |= ST_FLAG_STRING;
 
443
                        continue;
 
444
                }
 
445
 
 
446
                upsdebugx(2, "Unrecognized flag [%s]", flag[i]);
 
447
        }
 
448
}
 
449
                
 
450
void state_addcmd(struct cmdlist_t **list, const char *cmdname)
 
451
{
 
452
        struct  cmdlist_t       *tmp, *last;
 
453
 
 
454
        tmp = last = *list;
 
455
 
 
456
        while (tmp) {
 
457
                last = tmp;
 
458
 
 
459
                /* ignore duplicates */
 
460
                if (!strcasecmp(tmp->name, cmdname))
 
461
                        return;
 
462
 
 
463
                tmp = tmp->next;
 
464
        }
 
465
 
 
466
        tmp = xmalloc(sizeof(struct cmdlist_t));
 
467
        tmp->name = xstrdup(cmdname);
 
468
        tmp->next = NULL;
 
469
 
 
470
        if (last)
 
471
                last->next = tmp;
 
472
        else
 
473
                *list = tmp;
 
474
}
 
475
 
 
476
void state_infofree(struct st_tree_t *node)
 
477
{
 
478
        if (!node)
 
479
                return;
 
480
 
 
481
        if (node->left)
 
482
                state_infofree(node->left);
 
483
 
 
484
        if (node->right)
 
485
                state_infofree(node->right);
 
486
 
 
487
        st_tree_node_free(node);
 
488
}
 
489
 
 
490
int state_delcmd(struct cmdlist_t **list, const char *cmd)
 
491
{
 
492
        struct  cmdlist_t       *tmp, *last;
 
493
 
 
494
        tmp = *list;
 
495
        last = NULL;
 
496
 
 
497
        while (tmp) {
 
498
                if (!strcmp(tmp->name, cmd)) {
 
499
 
 
500
                        if (last)
 
501
                                last->next = tmp->next;
 
502
                        else
 
503
                                *list = tmp->next;
 
504
 
 
505
                        free(tmp->name);
 
506
                        free(tmp);
 
507
 
 
508
                        return 1;       /* deleted */
 
509
                }
 
510
 
 
511
                tmp = tmp->next;
 
512
        }
 
513
 
 
514
        return 0;       /* not found */
 
515
}
 
516
 
 
517
int state_delinfo(struct st_tree_t **root, const char *var)
 
518
{
 
519
        return st_tree_delete(root, NULL, var);
 
520
}
 
521
 
 
522
int state_delenum(struct st_tree_t *root, const char *var, const char *val)
 
523
{
 
524
        struct  st_tree_t       *sttmp;
 
525
        struct  enum_t *etmp, *elast;
 
526
 
 
527
        /* find the tree node for var */
 
528
        sttmp = state_tree_find(root, var);
 
529
 
 
530
        if (!sttmp)
 
531
                return 0;
 
532
 
 
533
        /* look for val in enum_list */
 
534
        etmp = sttmp->enum_list;
 
535
        elast = NULL;   
 
536
 
 
537
        while (etmp) {
 
538
                if (!strcmp(etmp->val, val)) {
 
539
 
 
540
                        if (elast)
 
541
                                elast->next = etmp->next;
 
542
                        else
 
543
                                sttmp->enum_list = etmp->next;
 
544
 
 
545
                        free(etmp->val);
 
546
                        free(etmp);
 
547
 
 
548
                        return 1;       /* deleted */
 
549
                }
 
550
 
 
551
                elast = etmp;
 
552
                etmp = etmp->next;
 
553
        }
 
554
 
 
555
        return 0;       /* not found */
 
556
}
 
557
 
 
558
struct st_tree_t *state_tree_find(struct st_tree_t *node, const char *var)
 
559
{
 
560
        int     cmp;
 
561
 
 
562
        if (!node)
 
563
                return NULL;
 
564
 
 
565
        cmp = strcasecmp(var, node->var);
 
566
 
 
567
        if (cmp == 0)
 
568
                return node;
 
569
 
 
570
        if (cmp < 0)
 
571
                return state_tree_find(node->left, var);
 
572
 
 
573
        return state_tree_find(node->right, var);
 
574
}