terminal: make the code of disable_echo() reusable
We are about to introduce the function `enable_non_canonical()`, which shares almost the complete code with `disable_echo()`. Let's prepare for that, by refactoring out that shared code. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Johannes Schindelin committed
Jan 14, 2020 at 18:43 UTC
94ac3c31f730ab278e1373a942fb4503829f4279
1 file changed
+15
-4
compat/terminal.c
+15
-4
@@ -32,7 +32,7 @@ static void restore_term(void)
32
term_fd = -1;
33
}
34
35
-static int disable_echo(void)
35
+static int disable_bits(tcflag_t bits)
36
{
37
struct termios t;
38
@@ -43,7 +43,7 @@ static int disable_echo(void)
43
old_term = t;
44
sigchain_push_common(restore_term_on_signal);
45
46
- t.c_lflag &= ~ECHO;
46
+ t.c_lflag &= ~bits;
47
if (!tcsetattr(term_fd, TCSAFLUSH, &t))
48
return 0;
49
@@ -53,6 +53,11 @@ error:
53
return -1;
54
}
55
56
+static int disable_echo(void)
57
+{
58
+ return disable_bits(ECHO);
59
+}
60
+
61
#elif defined(GIT_WINDOWS_NATIVE)
62
63
#define INPUT_PATH "CONIN$"
@@ -72,7 +77,7 @@ static void restore_term(void)
77
hconin = INVALID_HANDLE_VALUE;
78
}
79
75
-static int disable_echo(void)
80
+static int disable_bits(DWORD bits)
81
{
82
hconin = CreateFile("CONIN$", GENERIC_READ | GENERIC_WRITE,
83
FILE_SHARE_READ, NULL, OPEN_EXISTING,
@@ -82,7 +87,7 @@ static int disable_echo(void)
87
88
GetConsoleMode(hconin, &cmode);
89
sigchain_push_common(restore_term_on_signal);
85
- if (!SetConsoleMode(hconin, cmode & (~ENABLE_ECHO_INPUT))) {
90
+ if (!SetConsoleMode(hconin, cmode & ~bits)) {
91
CloseHandle(hconin);
92
hconin = INVALID_HANDLE_VALUE;
93
return -1;
@@ -91,6 +96,12 @@ static int disable_echo(void)
96
return 0;
97
}
98
99
+static int disable_echo(void)
100
+{
101
+ return disable_bits(ENABLE_ECHO_INPUT);
102
+}
103
+
104
+
105
#endif
106
107
#ifndef FORCE_TEXT