forked from networkupstools/wmnut
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrcfiles.c
265 lines (246 loc) · 7.87 KB
/
rcfiles.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
* rcfiles.c: useful functions to deal with parameters from rc files
* or command line
*
* Copyright (C)
* 2002 - 2012 Arnaud Quette <[email protected]>
* 2022 - 2024 Jim Klimov <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program (see the file COPYING); if not, write to the
* Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include "wmgeneral.h"
#include "config.h"
#ifdef HAVE_GETOPT_LONG
# include <getopt.h>
#endif
/*******************************************************************************\
|* AddRcKey |
\*******************************************************************************/
void AddRcKey(rckeys *key, const char *label, int type, void *var) {
if (label != NULL) {
/* key->label = (char *)xmalloc(strlen(label) + 1); */
key->label = (char *)malloc(strlen(label) + 1);
strcpy(key->label, label);
}
else
key->label = NULL;
key->type = type;
switch(type) {
case TYPE_STRING :
key->var.str = (char *)var;
break;
case TYPE_BOOL:
case TYPE_INT:
key->var.integer = (int *)var;
break;
case TYPE_FLOAT:
key->var.floater = (float *)var;
break;
case TYPE_NULL:
break;
}
}
/* TODO : void FreeRcKeys(rckeys *key)*/
/*******************************************************************************\
|* ParseRCFile |
\*******************************************************************************/
void ParseRCFile(const char *filename, rckeys *keys)
{
char *p, *tmp;
char temp[128];
char *tokens = " =\t\n#";
FILE *fp;
int key;
fp = fopen(filename, "r");
if (fp) {
#ifdef DEBUG
printf("Opening rc file %s\n", filename);
#endif
while (fgets(temp, 128, fp)) {
if (temp[0] != '#') {
key = 0;
while (key >= 0 && keys[key].label) {
if ((p = strstr(&temp[0], keys[key].label))) {
/* jump to "=" */
p += strlen(keys[key].label);
/* suppress tokens before value */
p += strspn(p, tokens);
/* suppress tokens after value */
tmp = strpbrk(p, tokens);
*tmp = '\0';
#ifdef DEBUG
printf("Parameter -> %s%s\tValue -> %s\n", keys[key].label,
((strlen(keys[key].label) <12)?"\t\t":"\t"), p);
#endif
switch(keys[key].type)
{
case TYPE_STRING:
/*
if (keys[key].var.str !=NULL)
free(keys[key].var.str);
keys[key].var.str = (char *)xmalloc(strlen(p) + 1);
strncpy(keys[key].var.str, p, strlen(p));
*/
if (!strcmp(keys[key].label, "UPS"))
AddHost(p);
break;
case TYPE_BOOL:
*keys[key].var.boolean = strncmp(p, "on", 2) == 0;
break;
case TYPE_INT:
*keys[key].var.integer = atoi(p);
break;
case TYPE_FLOAT:
*keys[key].var.floater = atof(p);
break;
}
key = -1;
} else
key++;
}
}
}
fclose(fp);
}
}
/*******************************************************************************\
|* LoadRCFile |
\*******************************************************************************/
void LoadRCFile(rckeys *keys)
{
char home_file[128];
char *p;
#ifdef DEBUG
printf("Loading rc files\n");
#endif
ParseRCFile(MAINRC_FILE, keys);
p = getenv("HOME");
sprintf(home_file, "%s/%s", p, RC_FILE);
ParseRCFile(home_file, keys);
}
/*******************************************************************************\
|* ParseCMDLine()
\*******************************************************************************/
void ParseCMDLine(int argc, char *argv[])
{
char *p;
int c;
#if 0
char *cmdline;
int i;
#endif
#ifdef HAVE_GETOPT_LONG
static struct option long_options[] =
{
{"alarmint",required_argument,NULL,'A'},
{"blinkrate",required_argument,NULL,'b'},
{"beepvol",required_argument,NULL,'B'},
{"criticalevel",required_argument,NULL,'C'},
{"display",required_argument,NULL,'d'},
{"help",no_argument,NULL,'h'},
{"lowcolor",no_argument,NULL,'l'},
{"lowlevel",required_argument,NULL,'L'},
{"upsname",required_argument,NULL,'U'},
{"version",no_argument,NULL,'v'},
{"verbose",no_argument,NULL,'V'},
{"windowed",no_argument,NULL,'w'},
{0, 0, 0, 0}
};
#define GETOPTFUNC(x,y,z) getopt_long(x,y,"-" z, long_options, NULL)
#define GETOPTENDCHAR -1
#else /* ! HAVE_GETOPT_LONG */
#define GETOPTFUNC(x,y,z) getopt(x,y,z)
#define GETOPTENDCHAR EOF
#endif /* ! HAVE_GETOPT_LONG */
while(1)
{
if ((c=GETOPTFUNC (argc, argv, "A:b:B:C:d:hlL:U:vVw")) == GETOPTENDCHAR)
break;
switch (c)
{
case 'A':
Alert = 1;
printf ("option A : valeur %s\n", optarg);
p = strchr(optarg, ',');
if (p != NULL)
*p = '\0';
LAlertRate = atof(optarg);
CAlertRate = atof(p+1);
printf ("option A : valeur LAlertRate = %f(%s), CAlertRate = %f(%s)\n",
LAlertRate, optarg, CAlertRate, (p+1));
break;
case 'b':
BlinkRate = atof(optarg);
break;
case 'C':
CriticalLevel = atoi(optarg);
break;
case 'L':
LowLevel = atoi(optarg);
break;
case 'l':
UseLowColorPixmap = 1;
break;
case 'V':
Verbose = 1;
break;
case 'v':
printf("\nThis is wmnut version: %s\n", VERSION);
printf("\nCopyright 2001-2016 Arnaud Quette <%s>\n", PACKAGE_BUGREPORT);
printf("\nComplete documentation for WMNUT should be found on this system using\n");
printf("`man wmnut' or `wmnut -h'. If you have access to the Internet, point your\n");
printf("browser at https://github.com/networkupstools/wmnut - the WMNUT Repository.\n\n");
/* Previously http://wmnut.mgeops.org, now defunct; last cached at
* http://web.archive.org/web/20111003175836/http://wmnut.mgeops.org/
*/
exit(1);
case 'w':
WithDrawn = 0; /* not in default withdrawn mode, so in windowed mode */
break;
case 'B':
Beep = 1;
Volume = atoi(optarg);
break;
case 'U':
AddHost(optarg);
break;
case 'h':
default:
printf("\n%s version: %s\n", PACKAGE, VERSION);
printf("Usage: %s [arguments]\n\n", PACKAGE_NAME);
printf("-A <T1,T2>\tSend messages to users terminals when Low and critical.\n");
printf(" \tT1 is seconds between messages when Low.\n");
printf(" \tT2 is seconds between messages when Critical.\n");
printf("-b <BlinkRate>\tBlink rate for red LED. (0 for no blinking.)\n");
printf("-B <Volume>\tBeep at Critical Level with Volume (between -100%% to 100%%).\n");
printf("-C <CriticaLvl>\tDefine level at which red LED turns on (CriticalLevel).\n");
printf("-d <display>\tUse alternate display.\n");
printf("-h\t\tDisplay this help screen.\n");
printf("-l\t\tUse a low-color pixmap to conserve colors on 8-bit displays.\n");
printf("-L <LowLevel>\tDefine level at which yellow LED turns on.\n");
printf(" \tCriticalLevel takes precedence if LowLevel < CriticalLevel.\n");
printf("-U <upsname>\tDefine upsname ([upsname@]hostname, default is localhost)\n");
printf("-v \t\tPrint version (includes important WMNUT info).\n");
printf("-V \t\tVerbose mode : display NUT available features and base value.\n");
printf("-w \t\tWindowed mode (opposite to native Window Maker withdrawn mode).\n\n");
exit (1);
}
}
}