Skip to content

Commit 94ac3c3

Browse files
dschogitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 08b1ea4 commit 94ac3c3

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

compat/terminal.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ static void restore_term(void)
3232
term_fd = -1;
3333
}
3434

35-
static int disable_echo(void)
35+
static int disable_bits(tcflag_t bits)
3636
{
3737
struct termios t;
3838

@@ -43,7 +43,7 @@ static int disable_echo(void)
4343
old_term = t;
4444
sigchain_push_common(restore_term_on_signal);
4545

46-
t.c_lflag &= ~ECHO;
46+
t.c_lflag &= ~bits;
4747
if (!tcsetattr(term_fd, TCSAFLUSH, &t))
4848
return 0;
4949

@@ -53,6 +53,11 @@ static int disable_echo(void)
5353
return -1;
5454
}
5555

56+
static int disable_echo(void)
57+
{
58+
return disable_bits(ECHO);
59+
}
60+
5661
#elif defined(GIT_WINDOWS_NATIVE)
5762

5863
#define INPUT_PATH "CONIN$"
@@ -72,7 +77,7 @@ static void restore_term(void)
7277
hconin = INVALID_HANDLE_VALUE;
7378
}
7479

75-
static int disable_echo(void)
80+
static int disable_bits(DWORD bits)
7681
{
7782
hconin = CreateFile("CONIN$", GENERIC_READ | GENERIC_WRITE,
7883
FILE_SHARE_READ, NULL, OPEN_EXISTING,
@@ -82,7 +87,7 @@ static int disable_echo(void)
8287

8388
GetConsoleMode(hconin, &cmode);
8489
sigchain_push_common(restore_term_on_signal);
85-
if (!SetConsoleMode(hconin, cmode & (~ENABLE_ECHO_INPUT))) {
90+
if (!SetConsoleMode(hconin, cmode & ~bits)) {
8691
CloseHandle(hconin);
8792
hconin = INVALID_HANDLE_VALUE;
8893
return -1;
@@ -91,6 +96,12 @@ static int disable_echo(void)
9196
return 0;
9297
}
9398

99+
static int disable_echo(void)
100+
{
101+
return disable_bits(ENABLE_ECHO_INPUT);
102+
}
103+
104+
94105
#endif
95106

96107
#ifndef FORCE_TEXT

0 commit comments

Comments
 (0)