]> cloudbase.mooo.com Git - z180-stamp.git/blob - avr/con-utils.c
f20dbfed214258bf77eb18e85d3f10f93a3b610c
[z180-stamp.git] / avr / con-utils.c
1 /*
2 * (C) Copyright 2014 Leo C. <erbl259-lmu@yahoo.de>
3 *
4 * SPDX-License-Identifier: GPL-2.0+
5 */
6
7 #include <string.h>
8 #include "common.h"
9
10 #include "serial.h"
11 #include "background.h"
12 #include "con-utils.h"
13
14 uint_fast8_t tstc(void)
15 {
16 bg_shed();
17 return serial_tstc();
18 }
19
20 int my_getchar(uint_fast8_t waitforchar)
21 {
22 int c;
23
24 do {
25 bg_shed();
26 c = serial_getc();
27 } while ((c < 0) && waitforchar);
28
29 return c;
30 }
31
32
33 /* test if ctrl-c was pressed */
34
35 static uint_fast8_t ctrlc_disabled; /* see disable_ctrl() */
36 static uint_fast8_t ctrlc_was_pressed;
37
38 uint_fast8_t ctrlc(void)
39 {
40 if (!ctrlc_disabled) {
41 switch (serial_getc()) {
42 case 0x03: /* ^C - Control C */
43 ctrlc_was_pressed = 1;
44 return 1;
45 default:
46 break;
47 }
48 }
49 return 0;
50 }
51
52 /* Reads user's confirmation.
53 Returns 1 if user's input is "y", "Y", "yes" or "YES"
54 */
55 uint_fast8_t confirm_yesno(void)
56 {
57 unsigned int i;
58 char str_input[5];
59
60 /* Flush input */
61 while (serial_getc())
62 ;
63 i = 0;
64 while (i < sizeof(str_input)) {
65 str_input[i] = my_getchar(1);
66 putchar(str_input[i]);
67 if (str_input[i] == '\r')
68 break;
69 i++;
70 }
71 putchar('\n');
72 if (strncmp(str_input, "y\r", 2) == 0 ||
73 strncmp(str_input, "Y\r", 2) == 0 ||
74 strncmp(str_input, "yes\r", 4) == 0 ||
75 strncmp(str_input, "YES\r", 4) == 0)
76 return 1;
77 return 0;
78 }
79
80 /* pass 1 to disable ctrlc() checking, 0 to enable.
81 * returns previous state
82 */
83 uint_fast8_t disable_ctrlc(uint_fast8_t disable)
84 {
85 uint_fast8_t prev = ctrlc_disabled; /* save previous state */
86
87 ctrlc_disabled = disable;
88 return prev;
89 }
90
91 uint_fast8_t had_ctrlc (void)
92 {
93 return ctrlc_was_pressed;
94 }
95
96 void clear_ctrlc(void)
97 {
98 ctrlc_was_pressed = 0;
99 }