terminal: use flags for save_term()

The next commit will add another flag in addition to the existing full_duplex so change the function signature to take a flags argument. Also alter the functions that call save_term() so that they can pass flags down to it. The choice to use an enum for tho bitwise flags is because gdb will display the symbolic names of all the flags that are set rather than the integer value. Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Phillip Wood committed Mar 16, 2022 at 18:54 UTC 02af15dec5bf0114b03c4c3bec5d8f60890a1c58
2 files changed +27 -21
compat/terminal.c
+21 -20
@@ -34,7 +34,7 @@ void restore_term(void)
34 sigchain_pop_common();
35 }
36
37 -int save_term(int full_duplex)
37 +int save_term(enum save_term_flags flags)
38 {
39 if (term_fd < 0)
40 term_fd = open("/dev/tty", O_RDWR);
@@ -47,11 +47,11 @@ int save_term(int full_duplex)
47 return 0;
48 }
49
50 -static int disable_bits(tcflag_t bits)
50 +static int disable_bits(enum save_term_flags flags, tcflag_t bits)
51 {
52 struct termios t;
53
54 - if (save_term(0) < 0)
54 + if (save_term(flags) < 0)
55 goto error;
56
57 t = old_term;
@@ -71,14 +71,14 @@ error:
71 return -1;
72 }
73
74 -static int disable_echo(void)
74 +static int disable_echo(enum save_term_flags flags)
75 {
76 - return disable_bits(ECHO);
76 + return disable_bits(flags, ECHO);
77 }
78
79 -static int enable_non_canonical(void)
79 +static int enable_non_canonical(enum save_term_flags flags)
80 {
81 - return disable_bits(ICANON | ECHO);
81 + return disable_bits(flags, ICANON | ECHO);
82 }
83
84 #elif defined(GIT_WINDOWS_NATIVE)
@@ -126,7 +126,7 @@ void restore_term(void)
126 hconin = hconout = INVALID_HANDLE_VALUE;
127 }
128
129 -int save_term(int full_duplex)
129 +int save_term(enum save_term_flags flags)
130 {
131 hconin = CreateFileA("CONIN$", GENERIC_READ | GENERIC_WRITE,
132 FILE_SHARE_READ, NULL, OPEN_EXISTING,
@@ -134,7 +134,7 @@ int save_term(int full_duplex)
134 if (hconin == INVALID_HANDLE_VALUE)
135 return -1;
136
137 - if (full_duplex) {
137 + if (flags & SAVE_TERM_DUPLEX) {
138 hconout = CreateFileA("CONOUT$", GENERIC_READ | GENERIC_WRITE,
139 FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
140 FILE_ATTRIBUTE_NORMAL, NULL);
@@ -154,7 +154,7 @@ error:
154 return -1;
155 }
156
157 -static int disable_bits(DWORD bits)
157 +static int disable_bits(enum save_term_flags flags, DWORD bits)
158 {
159 if (use_stty) {
160 struct child_process cp = CHILD_PROCESS_INIT;
@@ -191,7 +191,7 @@ static int disable_bits(DWORD bits)
191 use_stty = 0;
192 }
193
194 - if (save_term(0) < 0)
194 + if (save_term(flags) < 0)
195 return -1;
196
197 if (!SetConsoleMode(hconin, cmode_in & ~bits)) {
@@ -204,14 +204,15 @@ static int disable_bits(DWORD bits)
204 return 0;
205 }
206
207 -static int disable_echo(void)
207 +static int disable_echo(enum save_term_flags flags)
208 {
209 - return disable_bits(ENABLE_ECHO_INPUT);
209 + return disable_bits(flags, ENABLE_ECHO_INPUT);
210 }
211
212 -static int enable_non_canonical(void)
212 +static int enable_non_canonical(enum save_term_flags flags)
213 {
214 - return disable_bits(ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT);
214 + return disable_bits(flags,
215 + ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT);
216 }
217
218 /*
@@ -267,7 +268,7 @@ char *git_terminal_prompt(const char *prompt, int echo)
268 return NULL;
269 }
270
270 - if (!echo && disable_echo()) {
271 + if (!echo && disable_echo(0)) {
272 fclose(input_fh);
273 fclose(output_fh);
274 return NULL;
@@ -361,7 +362,7 @@ int read_key_without_echo(struct strbuf *buf)
362 static int warning_displayed;
363 int ch;
364
364 - if (warning_displayed || enable_non_canonical() < 0) {
365 + if (warning_displayed || enable_non_canonical(0) < 0) {
366 if (!warning_displayed) {
367 warning("reading single keystrokes not supported on "
368 "this platform; reading line instead");
@@ -413,10 +414,10 @@ int read_key_without_echo(struct strbuf *buf)
414
415 #else
416
416 -int save_term(int full_duplex)
417 +int save_term(enum save_term_flags flags)
418 {
418 - /* full_duplex == 1, but no support available */
419 - return -full_duplex;
419 + /* no duplex support available */
420 + return -!!(flags & SAVE_TERM_DUPLEX);
421 }
422
423 void restore_term(void)
compat/terminal.h
+6 -1
@@ -1,6 +1,11 @@
1 #ifndef COMPAT_TERMINAL_H
2 #define COMPAT_TERMINAL_H
3
4 +enum save_term_flags {
5 + /* Save input and output settings */
6 + SAVE_TERM_DUPLEX = 1 << 0,
7 +};
8 +
9 /*
10 * Save the terminal attributes so they can be restored later by a
11 * call to restore_term(). Note that every successful call to
@@ -8,7 +13,7 @@
13 * attributes have not been changed. Returns 0 on success, -1 on
14 * failure.
15 */
11 -int save_term(int full_duplex);
16 +int save_term(enum save_term_flags flags);
17 /* Restore the terminal attributes that were saved with save_term() */
18 void restore_term(void);
19