Skip to content

Commit 02af15d

Browse files
phillipwoodgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 32f3ac2 commit 02af15d

File tree

2 files changed

+27
-21
lines changed

2 files changed

+27
-21
lines changed

compat/terminal.c

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ void restore_term(void)
3434
sigchain_pop_common();
3535
}
3636

37-
int save_term(int full_duplex)
37+
int save_term(enum save_term_flags flags)
3838
{
3939
if (term_fd < 0)
4040
term_fd = open("/dev/tty", O_RDWR);
@@ -47,11 +47,11 @@ int save_term(int full_duplex)
4747
return 0;
4848
}
4949

50-
static int disable_bits(tcflag_t bits)
50+
static int disable_bits(enum save_term_flags flags, tcflag_t bits)
5151
{
5252
struct termios t;
5353

54-
if (save_term(0) < 0)
54+
if (save_term(flags) < 0)
5555
goto error;
5656

5757
t = old_term;
@@ -71,14 +71,14 @@ static int disable_bits(tcflag_t bits)
7171
return -1;
7272
}
7373

74-
static int disable_echo(void)
74+
static int disable_echo(enum save_term_flags flags)
7575
{
76-
return disable_bits(ECHO);
76+
return disable_bits(flags, ECHO);
7777
}
7878

79-
static int enable_non_canonical(void)
79+
static int enable_non_canonical(enum save_term_flags flags)
8080
{
81-
return disable_bits(ICANON | ECHO);
81+
return disable_bits(flags, ICANON | ECHO);
8282
}
8383

8484
#elif defined(GIT_WINDOWS_NATIVE)
@@ -126,15 +126,15 @@ void restore_term(void)
126126
hconin = hconout = INVALID_HANDLE_VALUE;
127127
}
128128

129-
int save_term(int full_duplex)
129+
int save_term(enum save_term_flags flags)
130130
{
131131
hconin = CreateFileA("CONIN$", GENERIC_READ | GENERIC_WRITE,
132132
FILE_SHARE_READ, NULL, OPEN_EXISTING,
133133
FILE_ATTRIBUTE_NORMAL, NULL);
134134
if (hconin == INVALID_HANDLE_VALUE)
135135
return -1;
136136

137-
if (full_duplex) {
137+
if (flags & SAVE_TERM_DUPLEX) {
138138
hconout = CreateFileA("CONOUT$", GENERIC_READ | GENERIC_WRITE,
139139
FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
140140
FILE_ATTRIBUTE_NORMAL, NULL);
@@ -154,7 +154,7 @@ int save_term(int full_duplex)
154154
return -1;
155155
}
156156

157-
static int disable_bits(DWORD bits)
157+
static int disable_bits(enum save_term_flags flags, DWORD bits)
158158
{
159159
if (use_stty) {
160160
struct child_process cp = CHILD_PROCESS_INIT;
@@ -191,7 +191,7 @@ static int disable_bits(DWORD bits)
191191
use_stty = 0;
192192
}
193193

194-
if (save_term(0) < 0)
194+
if (save_term(flags) < 0)
195195
return -1;
196196

197197
if (!SetConsoleMode(hconin, cmode_in & ~bits)) {
@@ -204,14 +204,15 @@ static int disable_bits(DWORD bits)
204204
return 0;
205205
}
206206

207-
static int disable_echo(void)
207+
static int disable_echo(enum save_term_flags flags)
208208
{
209-
return disable_bits(ENABLE_ECHO_INPUT);
209+
return disable_bits(flags, ENABLE_ECHO_INPUT);
210210
}
211211

212-
static int enable_non_canonical(void)
212+
static int enable_non_canonical(enum save_term_flags flags)
213213
{
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);
215216
}
216217

217218
/*
@@ -267,7 +268,7 @@ char *git_terminal_prompt(const char *prompt, int echo)
267268
return NULL;
268269
}
269270

270-
if (!echo && disable_echo()) {
271+
if (!echo && disable_echo(0)) {
271272
fclose(input_fh);
272273
fclose(output_fh);
273274
return NULL;
@@ -361,7 +362,7 @@ int read_key_without_echo(struct strbuf *buf)
361362
static int warning_displayed;
362363
int ch;
363364

364-
if (warning_displayed || enable_non_canonical() < 0) {
365+
if (warning_displayed || enable_non_canonical(0) < 0) {
365366
if (!warning_displayed) {
366367
warning("reading single keystrokes not supported on "
367368
"this platform; reading line instead");
@@ -413,10 +414,10 @@ int read_key_without_echo(struct strbuf *buf)
413414

414415
#else
415416

416-
int save_term(int full_duplex)
417+
int save_term(enum save_term_flags flags)
417418
{
418-
/* full_duplex == 1, but no support available */
419-
return -full_duplex;
419+
/* no duplex support available */
420+
return -!!(flags & SAVE_TERM_DUPLEX);
420421
}
421422

422423
void restore_term(void)

compat/terminal.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
#ifndef COMPAT_TERMINAL_H
22
#define COMPAT_TERMINAL_H
33

4+
enum save_term_flags {
5+
/* Save input and output settings */
6+
SAVE_TERM_DUPLEX = 1 << 0,
7+
};
8+
49
/*
510
* Save the terminal attributes so they can be restored later by a
611
* call to restore_term(). Note that every successful call to
712
* save_term() must be matched by a call to restore_term() even if the
813
* attributes have not been changed. Returns 0 on success, -1 on
914
* failure.
1015
*/
11-
int save_term(int full_duplex);
16+
int save_term(enum save_term_flags flags);
1217
/* Restore the terminal attributes that were saved with save_term() */
1318
void restore_term(void);
1419

0 commit comments

Comments
 (0)