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