summaryrefslogtreecommitdiff
path: root/avr/con-utils.c
blob: f20dbfed214258bf77eb18e85d3f10f93a3b610c (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
/*
 * (C) Copyright 2014 Leo C. <erbl259-lmu@yahoo.de>
 *
 * SPDX-License-Identifier:	GPL-2.0+
 */

#include <string.h>
#include "common.h"

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

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

int my_getchar(uint_fast8_t waitforchar)
{
	int c;

	do {
		bg_shed();
		c = serial_getc();
	} while ((c < 0) && waitforchar);

	return c;
}


/* test if ctrl-c was pressed */

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

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(1);
		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;
}