summaryrefslogtreecommitdiff
path: root/src/pmieconf/pmieconf.c
blob: 0de4ff6212a8012d8785ea0859407158b80ab835 (plain)
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
/*
 * Copyright (c) 2014 Red Hat.
 * Copyright (c) 1998-2001, Silicon Graphics, Inc.  All Rights Reserved.
 *
 * 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.
 */

#include <ctype.h>
#include <sys/param.h>
#include "pmapi.h"
#include "impl.h"
#include "rules.h"

#define MAXSYMLEN	(MAXPATHLEN+1)
#define MAXVARLEN	(512+1)
#define MAXBUFLEN	(1024+1)

static int	verbose;
static int	autocreate;
static int	interactive = 1;
static int	pmiefile_modified;
static char	warn[MAXBUFLEN];	/* buffer for any warning messages */

static char	help[] = \
    "  1. help  [ { . | all | global | <rule> | <group> } [<variable>] ]\n"
    "               descriptive text on one or more variables or rules\n"
    "  2. rules  [ enabled | disabled ]\n"
    "               list of available, enabled, or disabled rules\n"
    "  3. groups    list of available groups of rules\n"
    "  4. status    print status information\n"
    "  5. enable  { . | all | <rule> | <group> }\n"
    "               switch evaluation on for given rule(s)\n"
    "  6. disable  { . | all | <rule> | <group> }\n"
    "               switch evaluation off for given rule(s)\n"
    "  7. list  { . | all | global | <rule> | <group> } [<variable>]\n"
    "               print values of variables for given rule(s)\n"
    "  8. modify  { . | all | global | <rule> | <group> } <variable> <value>\n"
    "               change the value of a rule variable\n"
    "  9. undo  { . | all | global | <rule> | <group> } [<variable>]\n"
    "               revert to default value of rule variable\n"
    " 10. verbose  [ { on | off } ]\n"
    "               change amount of info displayed ('*' denotes global variables)\n"
    " 11. quit      save changes then exit\n"
    " 12. abort     discard changes then exit";

#define MAXARGS	4
static char	inbuf[MAXARGS][MAXBUFLEN+1];	/* input buffer */
static char	previous[MAXVARLEN+1];		/* buffer for last rule name */

static symbol_t commands[] = {
#define COMMAND_HELP	1
    { COMMAND_HELP, "help" }, { COMMAND_HELP, "h" }, { COMMAND_HELP, "?" },
#define COMMAND_RULES	2
    { COMMAND_RULES, "rules" }, { COMMAND_RULES, "r" },
#define COMMAND_GROUPS	3
    { COMMAND_GROUPS, "groups" }, { COMMAND_GROUPS, "g" },
#define COMMAND_STATUS	4
    { COMMAND_STATUS, "status" }, { COMMAND_STATUS, "s" },
#define COMMAND_ENABLE	5
    { COMMAND_ENABLE, "enable" }, { COMMAND_ENABLE, "e" },
#define COMMAND_DISABLE	6
    { COMMAND_DISABLE, "disable" }, { COMMAND_DISABLE, "d" },
#define COMMAND_LIST	7
    { COMMAND_LIST, "list" }, { COMMAND_LIST, "l" },
#define COMMAND_MODIFY	8
    { COMMAND_MODIFY, "modify" }, { COMMAND_MODIFY, "m" },
#define COMMAND_UNDO	9
    { COMMAND_UNDO, "undo" }, { COMMAND_UNDO, "u" },
#define COMMAND_VERBOSE	10
    { COMMAND_VERBOSE, "verbose" }, { COMMAND_VERBOSE, "v" },
#define COMMAND_QUIT	11
    { COMMAND_QUIT, "quit" }, { COMMAND_QUIT, "q" },
#define COMMAND_ABORT	12
    { COMMAND_ABORT, "abort" }, { COMMAND_ABORT, "a" },
#define LAST_COMMAND	12
};
static int	ncommands = (sizeof(commands)/sizeof(commands[0]));

/* io-related stuff */
extern void setio(int);
extern void setscroll(void);
extern void onwinch(int);
extern int pprintf(char *, ...);
extern void error(char *, ...);


/*
 *   ####  simple printing routines  ###
 */

static void
print_helpstring(char *value)
{
    char	*s;
    char	*str;
    int		i = 0;

    if (value == NULL) {
	pprintf("  help: No help available.\n");
	return;
    }
    if ((str = strdup(value)) == NULL) {
	error("insufficient memory to display help");
	exit(1);
    }
    s = strtok(str, "\n");
    while (s != NULL) {
	pprintf("%s%s\n", i++ == 0? "  help: ":"\t", s);
	s = strtok(NULL, "\n");
    }
    free(str);
}

static void
print_predicatestring(char *value)
{
    char	*s;
    char	*str;

    if ((str = strdup(value)) == NULL) {
	error("insufficient memory to display predicate");
	exit(1);
    }
    s = strtok(str, "\n");
    pprintf("\tpredicate = \n");
    while (s != NULL) {
	pprintf("\t  %s\n", s);
	s = strtok(NULL, "\n");
    }
    free(str);
}

/* prints help string for a parameter of a rule */
void
print_help(rule_t *rule, char *attrib)
{
    atom_t	*aptr;

    for (aptr = &rule->self; aptr != NULL; aptr = aptr->next)
	if (strcmp(get_aname(rule, aptr), attrib) == 0) {
	    if (aptr->help == NULL)
		goto nohelp;
	    print_helpstring(aptr->help);
	    return;
	}
    for (aptr = &globals->self; aptr != NULL; aptr = aptr->next)
	if (strcmp(get_aname(globals, aptr), attrib) == 0) {
	    if (aptr->help == NULL)
		goto nohelp;
	    print_helpstring(aptr->help);
	    return;
	}
nohelp:
    error("no help available for variable \"%s\" of rule %s",
	attrib, rule->self.name);
}

/* prints a named parameter from a rule, return -1 on failure */
int
print_attribute(rule_t *rule, char *attrib, int dohelp)
{
    atom_t	*aptr;
    int		isattrib = is_attribute(attrib);

    if (isattrib == ATTRIB_PREDICATE && rule != globals)
	print_predicatestring(rule->predicate);
    else if (isattrib == ATTRIB_HELP)
	print_helpstring(rule->self.help);
    else if (isattrib != -1 && rule != globals) {
	if (dohelp)
	    pprintf("   var: %s\n  help: No help available.\n", attrib);
	else
	    pprintf("\t%s = %s\n", attrib, get_attribute(attrib, &rule->self));
    }
    else {
	for (aptr = rule->self.next; aptr != NULL; aptr = aptr->next)
	    if (strcmp(get_aname(rule, aptr), attrib) == 0) {
		if (dohelp) {
		    pprintf("   var: %s\n", attrib);
		    print_helpstring(aptr->help);
		}
		else
		    pprintf("\t%s = %s\n", attrib, value_string(aptr, 1));
		return 0;
	    }
	for (aptr = globals->self.next; aptr != NULL; aptr = aptr->next)
	    if (strcmp(get_aname(globals, aptr), attrib) == 0) {
		if (dohelp) {
		    pprintf("   var: %s\n", attrib);
		    print_helpstring(aptr->help);
		}
		else
		    pprintf("\t%s = %s\n", attrib, value_string(aptr, 1));
		return 0;
	    }
	error("variable \"%s\" is undefined for rule %s",
		attrib, rule->self.name);
	return -1;
    }
    return 0;
}


/* one line summary (name and short help) for a given rule */
void
print_rule_summary(rule_t *rule, char *prefix)
{
    char	*str;
    char	fmt[] = "%s%s  [%s]\n";

    if ((str = dollar_expand(rule, rule->self.data, 0)) == NULL)
	return;
    pprintf(fmt, prefix, rule->self.name, str);
    free(str);
}


void
print_rule(rule_t *rule)
{
    atom_t		*a;
    int			needvars = 1;

    print_rule_summary(rule, "  rule: ");
    if (rule->self.help != NULL)
	print_helpstring(rule->self.help);
    if (rule != globals) {	/* non-global */
	print_predicatestring(rule->predicate);
	pprintf("  vars: enabled = %s\n", rule->self.enabled?"yes":"no");
    }
    for (a = rule->self.next; a != NULL; a = a->next) {
	if (!a->display)
	    continue;
	pprintf("%s%s = %s\n", (rule == globals && needvars)?
	       "  vars: ":"\t", get_aname(rule, a), value_string(a, 1));
	needvars = 0;
    }
    if (verbose && rule != globals) {
	for (a = globals->self.next; a != NULL; a = a->next) {
	    if (is_overridden(rule, a))
		continue;	/* printed already as part of attribs */
	    pprintf("\t%s = %s (*)\n", get_aname(globals,a), value_string(a,1));
	}
    }
}


/* print out the list of unique group names (sort done previously) */
static int
print_grouplist(int argcount)
{
    int		i;
    char	*j;
    char	lastgroup[MAXVARLEN];

    if (argcount != 0) {
	error("too many arguments for \"groups\" command");
	return -1;
    }
    lastgroup[0] = '\0';
    for (i = 1; i < rulecount; i++) {
	if ((j = strchr(rulelist[i].self.name, '.')) != NULL) {
	    *j = '\0';	/* mark end of group name */
	    if (strcmp(rulelist[i].self.name, lastgroup) != 0) {
		strcpy(lastgroup, rulelist[i].self.name);
		pprintf("  %s\n", lastgroup);
	    }
	    *j = '.';	/* repair the rule name */
	}
    }
    return 0;
}

/*
 * print out the current verbosity setting, running pmies using this
 * pmie file, total number of rules & number of rules switched on.
 */
static int
print_status(int argcount)
{
    int		i, count = 0, pcount = 0;
    char	**processes;

    if (argcount != 0) {
	error("too many arguments for \"status\" command");
	return -1;
    }

    for (i = 1; i < rulecount; i++)		/* find enabled rules */
	if (rulelist[i].self.enabled)
	    count++;
    lookup_processes(&pcount, &processes);	/* find running pmies */
    printf("  verbose:  %s\n"
	   "  enabled rules:  %u of %u\n"
	   "  pmie configuration file:  %s\n"
	   "  pmie %s using this file: ",
		verbose? "on" : "off", count, rulecount-1, get_pmiefile(),
		pcount == 1? "process (PID)" : "processes (PIDs)");
    if (pcount == 0)
	printf(" (none found)");
    else {
	for (i = 0; i < pcount; i++) {
	    printf(" %s", processes[i]);
	    free(processes[i]);
	}
	free(processes);
    }
    printf("\n");
    return 0;
}

int
write_pmie(void)
{
    int		i, count;
    char	*msg;
    dep_t	*list;

    if ((msg = write_pmiefile(pmProgname, autocreate)) != NULL) {
	error(msg);
	return 1;
    }
    if ((count = fetch_deprecated(&list)) > 0) {
	if (interactive)
	    pprintf("  Warning - some rules have been deprecated:\n");
	else
	    pprintf("%s: some rules have been deprecated:\n", pmProgname);
	for (i = 0; i < count; i++) {
	    pprintf("    %s (deprecated, %s)\n", list[i].name, list[i].reason);
	    free(list[i].name);
	}
	free(list);
	pprintf("\n  See %s for details\n", get_pmiefile());
    }
    return 0;
}

/* display the list of available, enabled or disabled rules */
static int
command_rules(int argcount)
{
    int	i;

    if (argcount > 1) {
	error("too many arguments for \"rules\" command");
	return -1;
    }
    if (argcount == 0)
	for (i = 1; i < rulecount; i++)
	    print_rule_summary(&rulelist[i], "  ");
    else {
	if (strcmp(inbuf[1], "enabled") == 0) {
	    for (i = 1; i < rulecount; i++)
		if (rulelist[i].self.enabled)
		    print_rule_summary(&rulelist[i], "  ");
	}
	else if (strcmp(inbuf[1], "disabled") == 0) {
	    for (i = 1; i < rulecount; i++)
		if (!rulelist[i].self.enabled)
		    print_rule_summary(&rulelist[i], "  ");
	}
	else {
	    error("invalid argument for \"rules\" command");
	    return -1;
	}
    }
    return 0;
}

/* display or set the verbosity level */
static int
command_verbose(int argcount)
{
    int sts;

    if (argcount < 1) {
	printf("  verbose:  %s\n", verbose? "on" : "off");
	return 0;
    }
    else if (argcount > 1) {
	error("too many arguments for \"verbose\" command");
	return -1;
    }
    if (strcmp(inbuf[1], "on") == 0)
	sts = 1;
    else if (strcmp(inbuf[1], "off") == 0)
	sts = 0;
    else {
	error("invalid argument, expected \"on\" or \"off\"");
	return -1;
    }
    verbose = sts;
    return 0;
}

static int
command_list(int argcount)
{
    unsigned int	rcount;
    rule_t		**rptr;
    char		*msg;
    int			all = 0;
    int			sts = 0;
    int			i;

    if (argcount < 1) {
	error("too few arguments for \"list\" command");
	return -1;
    }
    else if (argcount > 2) {
	error("too many arguments for \"list\" command");
	return -1;
    }

    if (strcmp(".", inbuf[1]) == 0)
	strcpy(inbuf[1], previous);
    if (strcmp("all", inbuf[1]) == 0)
	all = 1;
    if ((msg = lookup_rules(inbuf[1], &rptr, &rcount, all)) != NULL) {
	error(msg);
	return -1;
    }
    if (argcount == 1) {	/* print out one rule or one group */
	for (i = 0; i < rcount; i++) {
	    if (i > 0) pprintf("\n");
	    print_rule(rptr[i]);
	}
    }
    else {	/* print out one rule/group variable */
	for (i = 0; i < rcount; i++) {
	    print_rule_summary(rptr[i], "  rule: ");
	    if (print_attribute(rptr[i], inbuf[2], 0) == -1)
		sts = -1;	/* failure */
	}
    }
    free(rptr);
    strcpy(previous, inbuf[1]);
    return sts;
}

static int
command_undo(int argcount)
{
    unsigned int	rcount;
    rule_t		**rptr;
    char		*msg = NULL;
    char		*var = NULL;
    int			all = 0;
    int			i;

    if (argcount < 1) {
	error("too few arguments for \"undo\" command");
	return -1;
    }
    else if (argcount > 2) {
	error("too many arguments for \"undo\" command");
	return -1;
    }

    if (strcmp(".", inbuf[1]) == 0)
	strcpy(inbuf[1], previous);
    if (strcmp("all", inbuf[1]) == 0)
	all = 1;
    if ((msg = lookup_rules(inbuf[1], &rptr, &rcount, all)) != NULL) {
	error(msg);
	return -1;
    }
    if (argcount == 2)
	var = inbuf[2];

    for (i = 0; i < rcount; i++)
	if ((msg = rule_defaults(rptr[i], var)) != NULL)
	    break;
    free(rptr);
    if (msg != NULL) {
	error(msg);
	return -1;
    }
    strcpy(previous, inbuf[1]);
    pmiefile_modified = 1;
    return 0;
}

static int
command_modify(int command, int argcount)
{
    unsigned int	rcount;
    rule_t		**rptr;
    char		*msg;
    int			all = 0;
    int			c;

    if (command == COMMAND_MODIFY) {
	if (argcount != 3) {
	    error("too %s arguments for \"modify\" command",
		    argcount < 3? "few":"many");
	    return -1;
	}
    }
    else if (strcmp(inbuf[1], "global") == 0) {
	error("invalid argument - \"global\"");
	return -1;
    }
    else if (command == COMMAND_ENABLE) {
	if (argcount != 1) {
	    error("too %s arguments for \"enable\" command",
		argcount < 1? "few":"many");
	    return -1;
	}
	strcpy(inbuf[2], "enabled");
	strcpy(inbuf[3], "yes");
    }
    else { /* (command == COMMAND_DISABLE) */
	if (argcount != 1) {
	    error("too %s arguments for \"disable\" command",
		argcount < 1? "few":"many");
	    return -1;
	}
	strcpy(inbuf[2], "enabled");
	strcpy(inbuf[3], "no");
    }

    if (strcmp(".", inbuf[1]) == 0)
	strcpy(inbuf[1], previous);
    if (strcmp("all", inbuf[1]) == 0)
	all = 1;
    if ( ((c = is_attribute(inbuf[2])) != -1) && c != ATTRIB_ENABLED ) {
	error("no change - variable \"%s\" is always readonly", inbuf[2]);
	return -1;
    }
    if ((msg = lookup_rules(inbuf[1], &rptr, &rcount, all)) != NULL) {
	error(msg);
	return -1;
    }

    for (c = 0; c < rcount; c++) {
	if ((msg = value_change(rptr[c], inbuf[2], inbuf[3])) != NULL) {
	    error("change aborted - %s", msg);
	    free(rptr);
	    return -1;
	}
    }
    pmiefile_modified = 1;
    free(rptr);
    strcpy(previous, inbuf[1]);
    return 0;
}

static int
command_help(int argcount)
{
    unsigned int	rcount;
    rule_t		**rptr;
    char		*msg;
    int			sts = 0;
    int			all = 0;
    int			i;

    if (argcount < 1) {
	puts(help);
	return 0;
    }
    else if (argcount > 2) {
	error("too many arguments for \"help\" command");
	return -1;
    }

    if (strcmp(".", inbuf[1]) == 0)
	strcpy(inbuf[1], previous);
    if (strcmp("all", inbuf[1]) == 0)
	all = 1;
    if ((msg = lookup_rules(inbuf[1], &rptr, &rcount, all)) != NULL) {
	error(msg);
	return -1;
    }

    if (argcount == 1) {
	for (i = 0; i < rcount; i++) {
	    print_rule_summary(rptr[i], "  rule: ");
	    print_helpstring(rptr[i]->self.help);
	}
    }
    else {
	for (i = 0; i < rcount; i++) {
	    print_rule_summary(rptr[i], "  rule: ");
	    if (print_attribute(rptr[i], inbuf[2], 1) == -1)
		sts = -1;	/* failure */
	}
    }
    free(rptr);
    strcpy(previous, inbuf[1]);
    return sts;
}

static void
command_quit(void)
{
    static int	done = 0;
    int		i, pcount = 0;
    char	**processes;
    char	*msg;

    /* must only come thru here once, but can be called multiple times */
    if (done != 0)
	return;
    done = 1;

    if (pmiefile_modified && write_pmie() != 0)
	exit(1);

    /* show any running pmie processes which use this pmie config */
    if (interactive && pmiefile_modified) {
	if ((msg = lookup_processes(&pcount, &processes)) != NULL)
	    error(msg);
	else if (pcount > 0) {
	    pprintf("  %s is in use by %d running pmie process%s:\n\t",
		    get_pmiefile(), pcount, pcount == 1? "":"es");
	    for (i = 0; i < pcount; i++)
		pprintf("%s ", processes[i]);
	    pprintf("\n  Restart %s for the configuration change to take effect.",
		    pcount == 1 ?  "this process" : "these processes");
	    pprintf("\n  o  Use kill(1) to stop; e.g.\tkill -INT ");
	    for (i = 0; i < pcount; i++) {
		pprintf("%s ", processes[i]);
		free(processes[i]);
	    }
	    free(processes);
	    pprintf("\n\
  o  Refer to pmie_check(1) for a convenient mechanism for restarting pmie\n\
     daemons launched under the control of %s/pmie/control;\n\
     e.g.\t%s/pmie_check -V\n", pmGetConfig("PCP_SYSCONF_DIR"), pmGetConfig("PCP_BINADM_DIR"));
	}
    }
}

/*
 * workhorse routine - dishes out work depending on user command;
 * returns 1 on user-quit, 0 on success, -1 on failure
 */
static int
configure(int count)
{
    int	command = atoi(inbuf[0]);

    if (command <= 0 || command > LAST_COMMAND)
	command = map_symbol(commands, ncommands, inbuf[0]);

    switch(command) {
    case COMMAND_HELP:
	return command_help(count-1);
    case COMMAND_RULES:
	return command_rules(count-1);
    case COMMAND_GROUPS:
	return print_grouplist(count-1);
    case COMMAND_STATUS:
	return print_status(count-1);
    case COMMAND_VERBOSE:
	return command_verbose(count-1);
    case COMMAND_ENABLE:	/* shortcut for modify */
    case COMMAND_DISABLE:	/* shortcut for modify */
    case COMMAND_MODIFY:
	return command_modify(command, count-1);
    case COMMAND_LIST:
	return command_list(count-1);
    case COMMAND_UNDO:
	return command_undo(count-1);
    case COMMAND_QUIT:
	command_quit();
	return 1;
    case COMMAND_ABORT:
	exit(0);
    default:
	error("unrecognised command \"%s\" - try \"help\"", inbuf[0]);
	return -1;
    }
    /*NOTREACHED*/
}


static void
interact(void)
{
    int		done = 0;
    int		n, sts;

    if (interactive)
	printf("Updates will be made to %s\n", get_pmiefile());
    do {
	sts = 0;
	for (n = 0; n < MAXARGS; n++)
	    inbuf[n][0] = '\0';
	if (interactive) {
	    setio(0);
	    printf("\n%s> ", pmProgname);
	    fflush(stdout);
	}

	do {
	    if ((n = read_token(stdin, inbuf[sts], MAXBUFLEN, '\n')) == -1) {
		error("failed to parse argument %d correctly", sts+1);
		break;
	    }
	    else if (n > 0)
		sts++;
	    else {
		if (n == -2) {
		    command_quit();
		    done = 1;
		}
		break;
	    }
	} while (sts <= MAXARGS);

	if (n < 0)	/* done (EOF) or error reported above */
	    continue;
	else if (sts > MAXARGS) {
	    error("too many arguments - try \"help\"");
	    /* consume until '\n' reached... */
	    while ((n = read_token(stdin, inbuf[0], MAXBUFLEN, '\n')) > 0);
	    if (n == -2) {
		command_quit();
	    	done = 1;	/* reached EOF, bail out! */
	    }
	}
	else if (sts > 0 && !done)
	    done = (configure(sts) == 1);
    } while (!done);
}

static pmLongOptions longopts[] = {
    PMAPI_OPTIONS_HEADER("Options"),
    { "", 0, 'c', 0, "an automated pmie configuration by the system" },
    { "force", 0, 'F', 0, "force creation/update of pmie file, then exit" },
    { "config", 1, 'f', "FILE", "location of generated pmie configuration file" },
    { "rules", 1, 'r', "PATH", "path specifying groups of rule files" },
    { "verbose", 0, 'v', 0, "increase level of diagnostics" },
    PMOPT_HELP,
    PMAPI_OPTIONS_END
};

static pmOptions opts = {
    .flags = PM_OPTFLAG_NOFLUSH,
    .short_options = "cFf:r:v?",
    .long_options = longopts,
    .short_usage = "[options] [ command [args...] ]",
};

int
main(int argc, char **argv)
{
    int		c;
    int		force = 0;
    char	*p;
    char	*in_rules = NULL;
    char	*in_pmie = NULL;

    while ((c = pmgetopt_r(argc, argv, &opts)) != EOF) {
	switch (c) {
	case 'c':
	    autocreate = 1;
	    interactive = 0;
	    break;

	case 'F':
	    force = 1;
	    break;

	case 'f':
	    in_pmie = opts.optarg;
	    break;

	case 'r':
	    in_rules = opts.optarg;
	    break;

	case 'v':
	    verbose = 1;
	    break;

	case '?':
	default:
	    opts.errors++;
	}
    }

    if (force && opts.optind < argc) {
	pmprintf("%s: cannot use -F option with a command\n", pmProgname);
	opts.optind = argc;
	opts.errors++;
    }

    for (c = 0; opts.optind < argc && c < MAXARGS; c++) {
	strncpy(inbuf[c], argv[opts.optind++], MAXBUFLEN);
	inbuf[c][MAXBUFLEN] = '\0';
	interactive = 0;
    }
    if (opts.optind < argc) {
	pmprintf("%s: too many arguments\n", pmProgname);
	opts.errors++;
    }

    if (opts.errors) {
	pmUsageMessage(&opts);
	pmprintf("\nCommands:\n%s\n", help);
	pmflush();
	exit(1);
    }

    if ((p = initialise(in_rules, in_pmie, warn, sizeof(warn))) != NULL) {
	error(p);
	exit(1);
    }
    sort_rules();

    if (rulecount <= 1) {
	fprintf(stderr, "%s: no rules were found using rule path: %s\n",
		pmProgname, get_rules());
	exit(1);
    }

    if (force || (*warn && p == NULL)) {	/* force/pmie doesn't exist */
	if (write_pmie() != 0)
	    exit(1);
	else if (force)
	    exit(0);
    }
    else if (*warn)		/* some other warning */
	error(warn);

    if (interactive) {
	if (!isatty(0))		/* reading commands from a file */
	    interactive = 0;
	else if (isatty(1)) {	/* be $PAGER, handle window-resize */
	    setscroll();
	    onwinch(0);
	}
	interact();
    }
    else if (configure(c) == -1)
	exit(1);
    command_quit();
    exit(0);
    /*NOTREACHED*/
}