summaryrefslogtreecommitdiff
path: root/avr/con-utils.c
blob: 430ba98ae6407fbdc964f42e87a207b34766aa08 (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
#include <string.h>
#include <stdio.h>

#include "serial.h"
#include "con-utils.h"


uint_fast8_t tstc(void)
{
	return serial_tstc();
}

int my_getchar(void)
{
	int c;
	
	while((c = serial_getc()) < 0)
		;
	return c;
}


/* test if ctrl-c was pressed */

static uint_fast8_t ctrlc_disabled = 0;	/* see disable_ctrl() */
static uint_fast8_t ctrlc_was_pressed = 0;

uint_fast8_t ctrlc(void)
{
	if (!ctrlc_disabled) {
		switch (serial_getc()) {
		case 0x03:		/* ^C - Control C */
			ctrlc_was_pressed = 1;
			return 1;
		default:
			break;
		}
	}
	return 0;
}

/* Reads user's confirmation.
   Returns 1 if user's input is "y", "Y", "yes" or "YES"
*/
uint_fast8_t confirm_yesno(void)
{
	unsigned int i;
	char str_input[5];

	/* Flush input */
	while (serial_getc())
		;
	i = 0;
	while (i < sizeof(str_input)) {
		str_input[i] = my_getchar();
		putchar(str_input[i]);
		if (str_input[i] == '\r')
			break;
		i++;
	}
	putchar('\n');
	if (strncmp(str_input, "y\r", 2) == 0 ||
	    strncmp(str_input, "Y\r", 2) == 0 ||
	    strncmp(str_input, "yes\r", 4) == 0 ||
	    strncmp(str_input, "YES\r", 4) == 0)
		return 1;
	return 0;
}

/* pass 1 to disable ctrlc() checking, 0 to enable.
 * returns previous state
 */
uint_fast8_t disable_ctrlc(uint_fast8_t disable)
{
	uint_fast8_t prev = ctrlc_disabled;	/* save previous state */

	ctrlc_disabled = disable;
	return prev;
}

uint_fast8_t had_ctrlc (void)
{
	return ctrlc_was_pressed;
}

void clear_ctrlc(void)
{
	ctrlc_was_pressed = 0;
}