File: dummycons.c

package info (click to toggle)
nut 0.45.5-rel-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,984 kB
  • ctags: 2,146
  • sloc: ansic: 22,216; sh: 1,138; makefile: 405
file content (445 lines) | stat: -rw-r--r-- 9,117 bytes parent folder | download
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/* dummycons.c - testing driver, ala dummyups, but with no X requirement

   Copyright (C) 2001  Russell Kroll <rkroll@exploits.org>

   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 of the License, 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; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/

#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/termios.h>

/* TODO: this may need to be much bigger */
#define INFO_MAX 64

#include "config.h"
#include "proto.h"
#include "shared.h"
#include "shared-tables.h"
#include "version.h"
#include "upscommon.h"
#include "common.h"

	int	info_max = INFO_MAX;

void initinfo (void)
{
	create_info(info_max, 1);

	/* setup the basics */

	addinfo(INFO_MFR, "Console testing", 0, 0);
	addinfo(INFO_MODEL, "Dummy UPS", 0, 0);
	addinfo(INFO_STATUS, "OL", 0, 0);
}

#if 0
/* install pointers to functions for msg handlers called from msgparse */
void setuphandlers()
{
	/* TODO: future */
}
#endif

void do_help(void)
{
	printf("Network UPS Tools dummycons help\n\n");
	printf("? / h / help	- show this help\n");
	printf("a / add VAR VAL	- add variable VAR with value VAL\n");
	printf("l / list	- show current variables in use\n");
	printf("L / List	- show all possible variables and settings\n");
	printf("s / set VAR VAL - set variable VAR to VAL\n");
	printf("q / quit	- quit\n");
	printf("\n");
	printf("Values can use \"quotes\" to store values with embedded spaces.\n");
}

void do_set(char *var, char *val)
{
	int	i;
	char	*dp = NULL;

	if ((!var) || (!val)) {
		printf("Error: need variable and desired values as arguments.\n");
		return;
	}

	for (i = 0; netvars[i].name != NULL; i++)
		if (!strcasecmp(netvars[i].name, var)) {
			dp = getdata(netvars[i].type);

			if (!dp) {
				printf("Error: %s doesn't exist yet.  (Use 'add' before calling set)\n",
					var);
				return;
			}

			printf("INFO_%s = %s\n", netvars[i].name, val);
			setinfo(netvars[i].type, "%s", val);
			return;
		}

	printf("Error: %s unrecognized.\n", var);
}

void do_list(int all)
{
	int	i;
	char	*dp, vn[50];

	for (i = 0; netvars[i].name != NULL; i++) {
		dp = getdata(netvars[i].type);

		snprintf(vn, sizeof(vn), "INFO_%s (%s)", netvars[i].name,
			netvars[i].desc);

		if (!dp) {
			if (all) {
				printf("%-50s = (undefined)\n", vn);
			}
		}
		else
			printf("%-50s = %s\n", vn, dp);
	}
}	

/* for future cleanup activities */
void do_quit(void)
{
	exit(0);
}

void do_add(char *var, char *val)
{
	int	i;

	if ((!var) || (!val)) {
		printf("Error: need variable and desired values as arguments.\n");
		return;
	}

	/* TODO: handle special types (enums, etc) */
	for (i = 0; netvars[i].name != NULL; i++) {
		if (!strcasecmp(netvars[i].name, var)) {
			addinfo(netvars[i].type, val, 0, 0);
			printf("INFO_%s = %s\n", netvars[i].name, val);
			return;
		}
	}

	printf("Error: %s unrecognized.\n", var);
}

void fake_shutdown(void)
{
	printf("dummycons: not a driver, nothing to shutdown...\n");
	exit(0);
}

void help(char *progname)
{
	printf("Dummy UPS driver - for testing and development\n\n");

	printf("usage: %s [-h]\n", progname);
	printf("       %s -k <portname>\n", progname);
	printf("       %s [-m <num>] [-k] <portname>\n\n", progname);

	printf("  -h		- display this help\n");
	printf("  -k		- fake a shutdown\n");
	printf("  -m <num>	- allow <num> items to be added\n");
	printf("  <portname>	- fake port name (for state file)\n");

	exit(0);
}

/* trivia: parseline used to be parseconf, but only dummycons needs it now */

/* split up buf into a number of substrings, returning pointers in arg */
int parseline(char *buf, char **arg, int numargs)
{
	char	*ptr, *ws;
	int	i, buflen, an, state;

	an = state = 0;
	ws = NULL;

	buflen = strlen (buf);
	ptr = buf;

	/* yes, it's a state machine! be afraid! */

	for (i = 0; i < buflen; i++) {
		switch (state) {
			case 0:		/* scan */
				if (*ptr == '"') {
					ws = ptr + 1; 	/* start after quote */
					state = 1;	/* goto quotecollect */
					break;
				}

				if (isspace((unsigned char)ptr[0]))
					break;		/* loop */

				if (*ptr == '\\') {	/* literal as start */
					if (i == (buflen - 1)) {
						upslogx(LOG_NOTICE,
						"\\ at end of line!");
						return 0;	/* failure */
					}

					ws = ptr;

					/* shift string to the left */
					memmove(ptr, ptr+1, buflen-i);

					/* fix length */
					buflen--;

					state = 2;	/* goto collect */
					break;
				}

				/* handle comments - # means we're done */
				if (*ptr == '#') {
					*ptr = '\0';
					buflen = i;
					break;
				}

				if (!isspace((unsigned char)ptr[0])) {
					ws = ptr;
					state = 2;	/* goto collect */
					break;
				}
			
				break;

			case 1:		/* quotecollect */
				if (ptr[0] == '"')
					state = 3;	/* goto save */

				if (ptr[0] == '\\') {	/* literal handling */
					if (i == (buflen - 1)) {
						upslogx(LOG_NOTICE,
						"\\ at end of line!");
						return 0;	/* failure */
					}

					/* shift string to the left */
					memmove(ptr, ptr+1, buflen-i);

					/* fix length */
					buflen--;

					break;
				}

				/* a comment here means a broken string */
				if (ptr[0] == '#') {
					upslogx(LOG_NOTICE,
                                                "Unbalanced quotes due to embedded comment!");
                                                return 0;       /* failure */
				}

				break;			/* loop */

			case 2:		/* collect */
				if (ptr[0] == '\\') {	/* literal handling */
					if (i == (buflen - 1)) {
						upslogx(LOG_NOTICE,
						"\\ at end of line!");
						return 0;	/* failure */
					}

					/* shift string to the left */
					memmove(ptr, ptr+1, buflen-i);

					/* fix length */
					buflen--;
					break;		/* loop */
				}

				/* handle comments - # means we're done */
				if (ptr[0] == '#') {
					ptr[0] = '\0';
					buflen = i;
					break;
				}

				if (!isspace((unsigned char)ptr[0]))
					break;		/* loop */

				state = 3;		/* goto save */
		}

		if (state == 3) {		/* save */
			if (an < numargs)
				arg[an++] = ws;
			ptr[0] = '\0';
			ws = NULL;
			state = 0;
		}

		ptr++;
	}

	if (state == 1)		/* end-of-string in state 1 == missing quote */
		upslogx(LOG_NOTICE, "Unbalanced \" in line");

	if (state == 2) {	/* catch last word when exiting from collect */
		ptr[0] = '\0';
		if (an < numargs)
			arg[an++] = ws;
	}

	/* zap any leftover pointers */
	for (i = an; i < numargs; i++)
		arg[i] = NULL;

	/* safety catch: don't allow all nulls back as 'success' */
	if (arg[0] == NULL)
		return 0;	/* FAILED (don't parse this) */

	return 1;	/* success */
}

int main(int argc, char **argv)
{
	int	res, i;
	char	buf[128], *portname, *prog;
	char	*arg[3];

	printf("Network UPS Tools - Dummy console UPS driver 0.10 (%s)\n", UPS_VERSION);

	prog = argv[0];

	while ((i = getopt(argc, argv, "+hkm:")) != EOF) {
		switch(i) {
			case 'k':
				fake_shutdown();
				break;

			case 'm':
				info_max = atoi(optarg);
				break;

			case 'h':
				help(prog);
				break;
		}
	}

	argc -= optind;
	argv += optind;

	if (argc != 1)
		portname = "null";
	else
		portname = argv[0];

	snprintf(statefn, sizeof(statefn), "%s/dummycons-%s", STATEPATH, portname);

	printf("State file: %s\n", statefn);

	droproot();
	initinfo();

	printf("Command (? for help): ");
	fflush(stdout);

	for (;;) {
		struct	timeval	tv;
		fd_set	rfd;		

		tv.tv_sec = 2;
		tv.tv_usec = 0;
		FD_ZERO(&rfd);
		FD_SET(fileno(stdin), &rfd);

		res = select(fileno(stdin) + 1, &rfd, NULL, NULL, &tv);

		if (res < 0)
			fatal("select");

		/* idle loop */
		if (res == 0) {
			writeinfo();
			continue;
		}

		fgets(buf, sizeof(buf), stdin);

		if (!strncmp(buf, "?", 1))
			do_help();

		/* split into usable chunks */
		res = parseline(buf, arg, 3);

		if (res < 1)
			continue;

		if (!strcmp(arg[0], "set"))
			do_set(arg[1], arg[2]);

		if (!strcmp(arg[0], "s"))
			do_set(arg[1], arg[2]);

		if (!strcmp(arg[0], "quit"))
			do_quit();

		if (!strcmp(arg[0], "q"))
			do_quit();

		if (!strcmp(arg[0], "list"))
			do_list(0);

		if (!strcmp(arg[0], "l"))
			do_list(0);

		if (!strcmp(arg[0], "List"))
			do_list(1);

		if (!strcmp(arg[0], "L"))
			do_list(1);

		if (!strcmp(arg[0], "help"))
			do_help();

		if (!strcmp(arg[0], "h"))
			do_help();

		if (!strcmp(arg[0], "a"))
			do_add(arg[1], arg[2]);

		if (!strcmp(arg[0], "add"))
			do_add(arg[1], arg[2]);

		writeinfo();

		printf("Command (? for help): ");
		fflush(stdout);

	}	/* for (;;) */
}