xref: /csrg-svn/sys/tahoe/stand/vdformat/cmd.c (revision 29981)
1*29981Skarels /*	cmd.c	1.2	86/11/04	*/
229527Ssam 
329527Ssam #include	"vdfmt.h"
429527Ssam #include	"cmd.h"
529527Ssam 
629527Ssam #define TRUE	1
729527Ssam #define FALSE	0
829527Ssam 
929527Ssam #define	HELP	1
1029527Ssam #define	STATUS	2
1129527Ssam #define	KILL	3
1229527Ssam #define	KQUIT	4
1329527Ssam 
1429527Ssam static cmd_text_element	primary[] = {
1529527Ssam 	{ STATUS,	"!",		"" },
1629527Ssam 	{ HELP,		"HElp",		"" },
1729527Ssam 	{ KILL,		"KILL",		"" },
1829527Ssam 	{ KQUIT,	"QUIT",		"" },
1929527Ssam 	{ STATUS,	"STATus",	"" },
2029527Ssam 	{ HELP,		"?",		"" },
2129527Ssam 	{ 0,		"",		"" }
2229527Ssam };
2329527Ssam 
2429527Ssam 
2529527Ssam /*
2629527Ssam */
2729527Ssam 
confirm(token)2829527Ssam boolean confirm(token)
2929527Ssam int	token;
3029527Ssam {
3129527Ssam 	char	*action;
3229527Ssam 	char	query[50];
3329527Ssam 
3429527Ssam 	if(token == KILL)
3529527Ssam 		action = "kill";
3629527Ssam 	else
3729527Ssam 		action = "quit";
3829527Ssam 	sprintf(query, "Confirm %s operations", action);
3929527Ssam 	return get_yes_no(query);
4029527Ssam }
4129527Ssam 
4229527Ssam 
4329527Ssam /*
4429527Ssam **
4529527Ssam */
4629527Ssam 
get_text_cmd(table,tokens)4729527Ssam get_text_cmd(table, tokens)
4829527Ssam cmd_text_element	*table;
4929527Ssam int			*tokens;
5029527Ssam {
5129527Ssam 	extern boolean	get_yes_no();
5229527Ssam 	int		*t_ptr;
5329527Ssam 	char		line[133];
5429527Ssam 
55*29981Skarels 	agets(line);
5629527Ssam 	/* Check for help, status, or kill */
5729527Ssam 	cmd_parse(primary, line, tokens);
5829527Ssam 	t_ptr = tokens;
5929527Ssam 	while(*t_ptr) {
6029527Ssam 		switch (*t_ptr) {
6129527Ssam 			case STATUS :
6229527Ssam 				cmd_status();
6329527Ssam 				break;
6429527Ssam 			case KQUIT :
6529527Ssam 			case KILL :
6629527Ssam 				if(confirm(*t_ptr) == true) {
6729527Ssam 					kill_processes = true;
6829527Ssam 					return 0;
6929527Ssam 				}
7029527Ssam 				break;
7129527Ssam 			default:
7229527Ssam 				help_text(table);
7329527Ssam 				break;
7429527Ssam 		}
7529527Ssam 		t_ptr++;
7629527Ssam 	}
7729527Ssam 	/* Now parse all the operator's commands */
7829527Ssam 	cmd_parse(table, line, tokens);
7929527Ssam 	return strlen(line);
8029527Ssam }
8129527Ssam 
8229527Ssam 
8329527Ssam /*
8429527Ssam **
8529527Ssam */
8629527Ssam 
cmd_intcmp(a,b)8729527Ssam cmd_intcmp(a, b)
8829527Ssam int	*a, *b;
8929527Ssam {
9029527Ssam 	if(*a==*b)
9129527Ssam 		return 0;
9229527Ssam 	if(*a<*b)
9329527Ssam 		return -1;
9429527Ssam 	return 1;
9529527Ssam }
9629527Ssam 
9729527Ssam 
9829527Ssam /*
9929527Ssam **
10029527Ssam */
10129527Ssam 
condition_list(tokens,sentinal)10229527Ssam condition_list(tokens, sentinal)
10329527Ssam int	*tokens, sentinal;
10429527Ssam {
10529527Ssam 	register int	*t_ptr = tokens;
10629527Ssam 	register int	num_tok;
10729527Ssam 
10829527Ssam 	for(num_tok=0; *t_ptr++ != sentinal; num_tok++)
10929527Ssam 		;
11029527Ssam 	qsort(tokens, num_tok, sizeof(int), cmd_intcmp);
11129527Ssam 	/* compress out dups */
11229527Ssam 	while(*tokens != sentinal) {
11329527Ssam 		if(*tokens == *(tokens+1)) {
11429527Ssam 			for(t_ptr=tokens+1; *t_ptr != sentinal; t_ptr++) {
11529527Ssam 				*t_ptr = *(t_ptr+1);
11629527Ssam 			}
11729527Ssam 			continue;
11829527Ssam 		}
11929527Ssam 		tokens++;
12029527Ssam 	}
12129527Ssam }
12229527Ssam 
12329527Ssam 
12429527Ssam /*
12529527Ssam **
12629527Ssam */
12729527Ssam 
cmd_parse(table,line,tokens)12829527Ssam cmd_parse(table, line, tokens)
12929527Ssam cmd_text_element	*table;
13029527Ssam char			*line;
13129527Ssam int			*tokens;
13229527Ssam {
13329527Ssam 	char		*seperators = "\t ,.;:-~+/\\";
13429527Ssam 	register char	*tok_start;
13529527Ssam 	register int	*tok = tokens;
13629527Ssam 	char		save_buf[133];
13729527Ssam 
13829527Ssam 	strcpy(save_buf, line);
13929527Ssam 	tok_start = (char *)strtok((char *)save_buf, seperators);
14029527Ssam 	while(tok_start != NULL) {
14129527Ssam 		if(strlen(tok_start)) {
14229527Ssam 			if(*tok = cmd_search(table, tok_start)) {
14329527Ssam 				tok++;
14429527Ssam 			}
14529527Ssam 		}
14629527Ssam 		tok_start = (char *)strtok((char *)NULL, seperators);
14729527Ssam 	}
14829527Ssam 	*tok = 0;
14929527Ssam 	condition_list(tokens, 0);
15029527Ssam }
15129527Ssam 
15229527Ssam 
15329527Ssam /*
15429527Ssam **
15529527Ssam */
15629527Ssam 
cmd_search(table,command)15729527Ssam cmd_search(table, command)
15829527Ssam cmd_text_element	*table;
15929527Ssam char			*command;
16029527Ssam {
16129527Ssam 	register char	*tbl_ptr;
16229527Ssam 	register char	*cmd_ptr;
16329527Ssam 
16429527Ssam 	while(table->cmd_token != 0) {
16529527Ssam 		cmd_ptr = command;
16629527Ssam 		tbl_ptr = table->cmd_text;
16729527Ssam 		while(ismustmatch(*tbl_ptr)) {
16829527Ssam 			if(toupper(*cmd_ptr) != *tbl_ptr)
16929527Ssam 				break;
17029527Ssam 			cmd_ptr++;
17129527Ssam 			tbl_ptr++;
17229527Ssam 		}
17329527Ssam 		if((*tbl_ptr == 0) || !ismustmatch(*tbl_ptr))
17429527Ssam 			return table->cmd_token;
17529527Ssam 		table++;
17629527Ssam 	}
17729527Ssam 	return 0;
17829527Ssam }
17929527Ssam 
18029527Ssam 
18129527Ssam /*
18229527Ssam **
18329527Ssam */
18429527Ssam 
is_in_digit_table(table,token)18529527Ssam is_in_digit_table(table, token)
18629527Ssam int	*table, token;
18729527Ssam {
18829527Ssam 	while(*table != -1) {
18929527Ssam 		if(token == *table)
19029527Ssam 			return TRUE;
19129527Ssam 		table++;
19229527Ssam 	}
19329527Ssam 	return FALSE;
19429527Ssam }
19529527Ssam 
19629527Ssam 
19729527Ssam /*
19829527Ssam **
19929527Ssam */
20029527Ssam 
fill_in(tokens,table,start,end)20129527Ssam int *fill_in(tokens, table, start, end)
20229527Ssam int	*tokens, *table, start, end;
20329527Ssam {
20429527Ssam 
20529527Ssam 	if(start > end) {
20629527Ssam 		register int temp = end;
20729527Ssam 
20829527Ssam 		end = start;
20929527Ssam 		start = temp;
21029527Ssam 	}
21129527Ssam 	while((*table != -1) && (*table < start))
21229527Ssam 		table++;
21329527Ssam 	while((*table != -1) && (*table <= end)) {
21429527Ssam 		*tokens++ = *table++;
21529527Ssam 	}
21629527Ssam 	return tokens;
21729527Ssam }
21829527Ssam 
21929527Ssam /*
22029527Ssam **
22129527Ssam */
22229527Ssam 
22329527Ssam get_digit_list(tokens, table, help)
22429527Ssam int	*tokens, *table, (*help)();
22529527Ssam {
22629527Ssam 	int		*tok_ptr;
22729527Ssam 	char		*ptr, line[133];
22829527Ssam 
22929527Ssam 	condition_list(table, -1);
230*29981Skarels 	agets(line);
23129527Ssam 	if(!line[0]) {
23229527Ssam 		*tokens = -1;
23329527Ssam 		return;
23429527Ssam 	}
23529527Ssam 	/* Check for help, status, or kill */
23629527Ssam 	cmd_parse(primary, line, tokens);
23729527Ssam 	tok_ptr = tokens;
23829527Ssam 	while(*tok_ptr) {
23929527Ssam 		switch (*tok_ptr) {
24029527Ssam 			case STATUS :
24129527Ssam 				cmd_status();
24229527Ssam 				break;
24329527Ssam 			case KQUIT :
24429527Ssam 			case KILL :
24529527Ssam 				if(confirm(*tok_ptr)) {
24629527Ssam 					kill_processes = true;
24729527Ssam 					return;
24829527Ssam 				}
24929527Ssam 				break;
25029527Ssam 			default:
25129527Ssam 				(help)();
25229527Ssam 				break;
25329527Ssam 		}
25429527Ssam 		tok_ptr++;
25529527Ssam 	}
25629527Ssam 	tok_ptr = tokens;
25729527Ssam 	ptr = line;
25829527Ssam 	while(*ptr) {
25929527Ssam 		finddigit(ptr);
26029527Ssam 		if(sscanf(ptr, "%d", tok_ptr) > 0) {
26129527Ssam 			skipdigits(ptr);
26229527Ssam 			skip_junk(ptr);
26329527Ssam 			if((*ptr == '~') || (*ptr == '-')) {
26429527Ssam 				register int	start = *tok_ptr;
26529527Ssam 
26629527Ssam 				finddigit(ptr);
26729527Ssam 				if(sscanf(ptr, "%d", tok_ptr) > 0) {
26829527Ssam 					skipdigits(ptr);
26929527Ssam 					tok_ptr = fill_in(tok_ptr,
27029527Ssam 					    table, start, *tok_ptr);
27129527Ssam 					continue;
27229527Ssam 				}
27329527Ssam 				else
27429527Ssam 					*tok_ptr = start;
27529527Ssam 			}
27629527Ssam 			if(is_in_digit_table(table, *tok_ptr))
27729527Ssam 				tok_ptr++;
27829527Ssam 		}
27929527Ssam 	}
28029527Ssam 	*tok_ptr = -1;
28129527Ssam 	condition_list(tokens, -1);
28229527Ssam }
28329527Ssam 
28429527Ssam 
28529527Ssam 
28629527Ssam /*
28729527Ssam **
28829527Ssam */
28929527Ssam 
29029527Ssam get_digit_cmd(help)
29129527Ssam int	(*help)();
29229527Ssam {
29329527Ssam 	int	tokens[20], *t_ptr;
29429527Ssam 	char	line[80];
29529527Ssam 	int	results;
29629527Ssam 
297*29981Skarels 	agets(line);
29829527Ssam 	if(!*line)
29929527Ssam 		return -1;
30029527Ssam 	/* Check for help, status, or kill */
30129527Ssam 	cmd_parse(primary, line, tokens);
30229527Ssam 	t_ptr = tokens;
30329527Ssam 	while(*t_ptr) {
30429527Ssam 		switch (*t_ptr) {
30529527Ssam 			case STATUS :
30629527Ssam 				cmd_status();
30729527Ssam 				break;
30829527Ssam 			case KQUIT :
30929527Ssam 			case KILL :
31029527Ssam 				if(confirm(*t_ptr)) {
31129527Ssam 					kill_processes = true;
31229527Ssam 					return -1;
31329527Ssam 				}
31429527Ssam 				break;
31529527Ssam 			default:
31629527Ssam 				(*help)();
31729527Ssam 				break;
31829527Ssam 		}
31929527Ssam 		t_ptr++;
32029527Ssam 	}
32129527Ssam 	if(sscanf(line, "%d", &results) > 0)
32229527Ssam 		return results;
32329527Ssam 	return -1;
32429527Ssam }
32529527Ssam 
32629527Ssam 
32729527Ssam /*
32829527Ssam **
32929527Ssam */
33029527Ssam 
get_string_cmd(line,help)33129527Ssam get_string_cmd(line, help)
33229527Ssam char	*line;
33329527Ssam int	(*help)();
33429527Ssam {
33529527Ssam 	int	tokens[20], *t_ptr;
33629527Ssam 
337*29981Skarels 	agets(line);
33829527Ssam 	if(!*line)
33929527Ssam 		return;
34029527Ssam 	/* Check for help, status, or kill */
34129527Ssam 	cmd_parse(primary, line, tokens);
34229527Ssam 	t_ptr = tokens;
34329527Ssam 	while(*t_ptr) {
34429527Ssam 		switch (*t_ptr) {
34529527Ssam 			case STATUS :
34629527Ssam 				cmd_status();
34729527Ssam 				break;
34829527Ssam 			case KQUIT :
34929527Ssam 			case KILL :
35029527Ssam 				if(confirm(*t_ptr)) {
35129527Ssam 					kill_processes = true;
35229527Ssam 					return;
35329527Ssam 				}
35429527Ssam 				break;
35529527Ssam 			default:
35629527Ssam 				(*help)();
35729527Ssam 				break;
35829527Ssam 		}
35929527Ssam 		t_ptr++;
36029527Ssam 	}
36129527Ssam 	while(*line) {
36229527Ssam 		*line = tolower(*line);
36329527Ssam 		line++;
36429527Ssam 	}
36529527Ssam 	return;
36629527Ssam }
36729527Ssam 
36829527Ssam 
36929527Ssam /*
37029527Ssam **
37129527Ssam */
37229527Ssam 
cmd_status()37329527Ssam cmd_status()
37429527Ssam {
37529527Ssam 	indent();
37629527Ssam 	switch (cur.state) {
37729527Ssam 		case cmd :
37829527Ssam 			print("Waiting for operator input.\n\n");
37929527Ssam 			break;
38029527Ssam 		default :
38129527Ssam 			status();
38229527Ssam 			break;
38329527Ssam 	}
38429527Ssam 	exdent(1);
38529527Ssam }
38629527Ssam 
38729527Ssam 
38829527Ssam /*
38929527Ssam ** 	Vdget_yes_no is used to ask simple yes or no questions.  The question
39029527Ssam ** prompt is supplied by the caller,  The question mark, possible responses,
39129527Ssam ** and the default response is printed at the end of the prompt.  The routine
39229527Ssam ** then reads the answer and returns a 1 if a 'y' is typed or no response was
39329527Ssam ** given, otherwise, a zero is returned.
39429527Ssam */
39529527Ssam 
get_yes_no(str)39629527Ssam boolean get_yes_no(str)
39729527Ssam register char	*str;
39829527Ssam {
399*29981Skarels 	extern int	wait_for_char;
40029527Ssam 	char		answer[80];
40129527Ssam 	boolean		retval;
402*29981Skarels 	int		old_wait_status = wait_for_char;
40329527Ssam 
404*29981Skarels 	wait_for_char = 1;
40529527Ssam 	for(;;) {
40629527Ssam 		if(*str)
40729527Ssam 			print("%s", str);
40829527Ssam 		printf("? [Yes/No] ");
409*29981Skarels 		agets(answer);
41029527Ssam 		if((answer[0] == 'Y') || (answer[0] == 'y')) {
41129527Ssam 			retval = true;
41229527Ssam 			break;
41329527Ssam 		}
41429527Ssam 		if((answer[0] == 'N') || (answer[0] == 'n')) {
41529527Ssam 			retval = false;
41629527Ssam 			break;
41729527Ssam 		}
41829527Ssam 		print("\n");
41929527Ssam 		print("A 'Yes' or 'No' must be entered!\n\n");
42029527Ssam 	}
421*29981Skarels 	wait_for_char = old_wait_status;
42229527Ssam 	return retval;
42329527Ssam }
42429527Ssam 
42529527Ssam 
42629527Ssam /*
42729527Ssam **
42829527Ssam */
42929527Ssam 
get_next_digit(ptr)43029527Ssam get_next_digit(ptr)
43129527Ssam char	*ptr;
43229527Ssam {
43329527Ssam 	int	results;
43429527Ssam 
43529527Ssam 	finddigit(ptr);
43629527Ssam 	if(sscanf(ptr, "%d", &results) <= 0)
43729527Ssam 		return -1;
43829527Ssam 	return results;
43929527Ssam }
440