33
33
#include "sstate.h"
36
/* until old-style vars are gone */
37
#include "shared-tables.h"
41
/* upsd.c pulls in the initializers, so just reference those */
42
extern struct netvars_t netvars[];
43
extern struct instcmds_t instcmds[];
44
extern struct var_map_t var_map[];
45
extern struct cmd_map_t cmd_map[];
47
static const char *newcmd_to_old(const char *new)
51
for (i = 0; cmd_map[i].name != NULL; i++)
52
if (!strcasecmp(cmd_map[i].name, new))
53
return cmd_map[i].old;
58
/* convert temporary hex numerics to the old-style cmdnames */
59
static void append_oldcmdname(char *buf, size_t bufsize, const char *cmdname)
63
/* only handle old-style stuff (no periods, and 4 chars long) */
64
if ((strchr(cmdname, '.')) || (strlen(cmdname) != 4)) {
67
old = newcmd_to_old(cmdname);
70
upsdebugx(2, "Unhandled new command name [%s]",
75
snprintfcat(buf, bufsize, " %s", old);
79
cmd = strtol(cmdname, (char **) NULL, 16);
84
/* yeah, it's ugly, but it's going away, so ignore it for now */
85
for (i = 0; instcmds[i].name != NULL; i++) {
86
if (instcmds[i].cmd == cmd) {
87
snprintfcat(buf, bufsize, " %s", instcmds[i].name);
94
upslogx(LOG_NOTICE, "Failed to find %04X in instcmds (%d)\n", cmd, i);
97
static const char *newvar_to_old(const char *new)
101
for (i = 0; var_map[i].name != NULL; i++)
102
if (!strcasecmp(var_map[i].name, new))
103
return var_map[i].old;
108
/* convert temporary hex numerics to the old-style varnames */
109
static void append_oldinfoname(char *buf, size_t bufsize, const char *varname)
113
/* only handle old-style stuff (no periods, and 4 chars long) */
114
if ((strchr(varname, '.')) || (strlen(varname) != 4)) {
117
old = newvar_to_old(varname);
120
upsdebugx(2, "Unhandled new variable name [%s]",
125
snprintfcat(buf, bufsize, " %s", old);
129
var = strtol(varname, (char **) NULL, 16);
134
/* yeah, it's ugly, but it's going away, so ignore it for now */
135
for (i = 0; netvars[i].name != NULL; i++) {
136
if (netvars[i].type == var) {
137
snprintfcat(buf, bufsize, " %s", netvars[i].name);
144
upslogx(LOG_NOTICE, "Failed to find %04X in netvars (%d)\n", var, i);
147
/* walk the tree and build an old-style variable list */
148
static void geninfolist(struct st_tree_t *node, char *buf, size_t bufsize,
155
geninfolist(node->left, buf, bufsize, rw);
159
/* only list rw entries */
160
if (node->flags & ST_FLAG_RW)
161
append_oldinfoname(buf, bufsize, node->var);
164
/* list all entries */
165
append_oldinfoname(buf, bufsize, node->var);
169
geninfolist(node->right, buf, bufsize, rw);
172
36
/* set the 'last heard' time to now for later staleness checks */
173
37
static void update_time(upstype *ups)
414
/* strictly a name-based transform - no netcmds numerics */
415
static const char *oldvar_to_new_noconv(const char *old)
419
for (i = 0; var_map[i].name != NULL; i++)
420
if (!strcasecmp(var_map[i].old, old))
421
return var_map[i].name;
426
const char *sstate_getinfo(const upstype *ups, const char *var, int tryold)
282
const char *sstate_getinfo(const upstype *ups, const char *var)
430
284
/* requesting an old variable name? */
431
if (!strchr(var, '.')) {
436
new = oldvar_to_new_noconv(var);
438
/* if it's been mapped, look up the new name */
440
return state_getinfo(ups->inforoot, new);
442
/* otherwise fall through and try it directly */
285
if (!strchr(var, '.'))
445
288
return state_getinfo(ups->inforoot, var);
465
308
return ups->cmdlist;
468
void sstate_makeinfolist(const upstype *ups, char *buf, size_t bufsize)
470
geninfolist(ups->inforoot, buf, bufsize, 0);
473
void sstate_makerwlist(const upstype *ups, char *buf, size_t bufsize)
475
geninfolist(ups->inforoot, buf, bufsize, 1);
478
void sstate_makeinstcmdlist(const upstype *ups, char *buf, size_t bufsize)
480
struct cmdlist_t *tmp;
485
append_oldcmdname(buf, bufsize, tmp->name);
490
311
int sstate_dead(upstype *ups, int maxage)