win32: support echo for terminal-prompt
v2 cover win32: support echo for terminal-prompt
So here's v2 of this series. For reference, you can find v1 and it's discussions here:
http://mid.gmane.org/[email protected]
The changes since the last round:
* 1/6: This patch has been added. It was missing in the last round, due to stupidity on my behalf. I'm sorry about that.
* 3/6: This patch got a fixup for the disable_echo function signature squashed in. I forgot "void" for the empty parameter list. Thanks to Junio for noticing.
Otherwise, things are unchanged.
Erik Faye-Lund (6):
mingw: correct exit-code for SIGALRM's SIG_DFL
mingw: make fgetc raise SIGINT if apropriate compat/terminal: factor out echo-disabling compat/terminal: separate input and output handles
mingw: reuse tty-version of git_terminal_prompt
mingw: get rid of getpass implementation
compat/mingw.c | 88 +++++++++++++++++++++++++++---------- compat/mingw.h | 8 +++- compat/terminal.c | 129 ++++++++++++++++++++++++++++++++++++++++-------------- 3 files changed, 169 insertions(+), 56 deletions(-)
Signature · 15 lines
1.8.0.4.g3c6fb4f.dirty -- *** Please reply-to-all at all times *** *** (do not pretend to know who is subscribed and who is not) *** *** Please avoid top-posting. *** The msysGit Wiki is here: https://github.com/msysgit/msysgit/wiki - Github accounts are free. You received this message because you are subscribed to the Google Groups "msysGit" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, and view previous threads, visit this group at http://groups.google.com/group/msysgit?hl=en_US?hl=en
PATCH v2 1/6 mingw: correct exit-code for SIGALRM's SIG_DFL
Make sure SIG_DFL for SIGALRM exits with 128 + SIGALRM so other processes can diagnose why it exits.
While we're at it, make sure we only write to stderr if it's a terminal, and change the output to match that of Linux.
--- compat/mingw.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
compat/mingw.c +5 -2
@@ -1538,8 +1538,11 @@ static sig_handler_t timer_fn = SIG_DFL;static unsigned __stdcallticktack (void *dummy) {while (WaitForSingleObject (timer_event, timer_interval) ==WAIT_TIMEOUT ) { -if (timer_fn ==SIG_DFL ) -die ("Alarm" ); +if (timer_fn ==SIG_DFL ) { +if (isatty (STDERR_FILENO )) +fputs ("Alarm clock\n" , stderr); +exit (128 +SIGALRM ); + }if (timer_fn !=SIG_IGN )timer_fn (SIGALRM );if (one_shot)
Signature · 15 lines
1.8.0.4.g3c6fb4f.dirty -- *** Please reply-to-all at all times *** *** (do not pretend to know who is subscribed and who is not) *** *** Please avoid top-posting. *** The msysGit Wiki is here: https://github.com/msysgit/msysgit/wiki - Github accounts are free. You received this message because you are subscribed to the Google Groups "msysGit" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, and view previous threads, visit this group at http://groups.google.com/group/msysgit?hl=en_US?hl=en
PATCH v2 2/6 mingw: make fgetc raise SIGINT if apropriate
In reply to Erik Faye-LundSet a control-handler to prevent the process from terminating, and simulate SIGINT so it can be handled by a signal-handler as usual.
--- compat/mingw.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++-------- compat/mingw.h | 6 +++++ 2 files changed, 72 insertions(+), 10 deletions(-)
compat/mingw.c +66 -10
@@ -319,6 +319,31 @@ ssize_t mingw_write(int fd, const void *buf, size_t count)return write (fd, buf,min (count,31 *1024 *1024 )); } +static BOOL WINAPI ctrl_ignore(DWORD type) +{ +return TRUE; +} + +#undef fgetc +int mingw_fgetc (FILE *stream) +{ +int ch; +if (!isatty (_fileno (stream))) +return fgetc (stream); + +SetConsoleCtrlHandler (ctrl_ignore, TRUE); +while (1 ) { + ch =fgetc (stream); +if (ch !=EOF ||GetLastError () !=ERROR_OPERATION_ABORTED ) +break ; + +/* Ctrl+C was pressed, simulate SIGINT and retry */ +mingw_raise (SIGINT ); + } +SetConsoleCtrlHandler (ctrl_ignore, FALSE); +return ch; +} +#undef fopenFILE *mingw_fopen (const char *filename,const char *otype) {
@@ -1524,7 +1549,7 @@ static HANDLE timer_event;static HANDLE timer_thread;static int timer_interval;static int one_shot; -static sig_handler_t timer_fn =SIG_DFL ; +static sig_handler_t timer_fn =SIG_DFL , sigint_fn =SIG_DFL ;/* The timer works like this: * The thread, ticktack(), is a trivial routine that most of the time
@@ -1538,13 +1563,7 @@ static sig_handler_t timer_fn = SIG_DFL;static unsigned __stdcallticktack (void *dummy) {while (WaitForSingleObject (timer_event, timer_interval) ==WAIT_TIMEOUT ) { -if (timer_fn ==SIG_DFL ) { -if (isatty (STDERR_FILENO )) -fputs ("Alarm clock\n" , stderr); -exit (128 +SIGALRM ); - } -if (timer_fn !=SIG_IGN ) -timer_fn (SIGALRM ); +mingw_raise (SIGALRM );if (one_shot)break ; }
@@ -1635,12 +1654,49 @@ int sigaction(int sig, struct sigaction *in, struct sigaction *out)sig_handler_t mingw_signal (int sig,sig_handler_t handler) {sig_handler_t old = timer_fn; -if (sig !=SIGALRM ) + +switch (sig) { +case SIGALRM : + timer_fn = handler; +break ; + +case SIGINT : + sigint_fn = handler; +break ; + +default :return signal (sig, handler); - timer_fn = handler; + } +return old; } +#undef raise +int mingw_raise (int sig) +{ +switch (sig) { +case SIGALRM : +if (timer_fn ==SIG_DFL ) { +if (isatty (STDERR_FILENO )) +fputs ("Alarm clock\n" , stderr); +exit (128 +SIGALRM ); + }else if (timer_fn !=SIG_IGN ) +timer_fn (SIGALRM ); +return 0 ; + +case SIGINT : +if (sigint_fn ==SIG_DFL ) +exit (128 +SIGINT ); +else if (sigint_fn !=SIG_IGN ) +sigint_fn (SIGINT ); +return 0 ; + +default : +return raise (sig); + } +} + +static const char *make_backslash_path (const char *path) {static char buf[PATH_MAX +1 ];
compat/mingw.h +6 -0
@@ -179,6 +179,9 @@ int mingw_open (const char *filename, int oflags, ...);ssize_t mingw_write (int fd,const void *buf,size_t count);#define write mingw_write +int mingw_fgetc (FILE *stream); +#define fgetc mingw_fgetc +FILE *mingw_fopen (const char *filename,const char *otype);#define fopen mingw_fopen
@@ -287,6 +290,9 @@ static inline unsigned int git_ntohl(unsigned int x)sig_handler_t mingw_signal (int sig,sig_handler_t handler);#define signal mingw_signal +int mingw_raise (int sig); +#define raise mingw_raise +/* * ANSI emulation wrappers */
Signature · 15 lines
1.8.0.4.g3c6fb4f.dirty -- *** Please reply-to-all at all times *** *** (do not pretend to know who is subscribed and who is not) *** *** Please avoid top-posting. *** The msysGit Wiki is here: https://github.com/msysgit/msysgit/wiki - Github accounts are free. You received this message because you are subscribed to the Google Groups "msysGit" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, and view previous threads, visit this group at http://groups.google.com/group/msysgit?hl=en_US?hl=en
PATCH v2 3/6 compat/terminal: factor out echo-disabling
In reply to Erik Faye-LundBy moving the echo-disabling code to a separate function, we can implement OS-specific versions of it for non-POSIX platforms.
--- compat/terminal.c | 43 +++++++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 18 deletions(-)
compat/terminal.c +25 -18
@@ -14,6 +14,7 @@ static void restore_term(void)return ;tcsetattr (term_fd,TCSAFLUSH , &old_term); +close (term_fd); term_fd =-1 ; }
@@ -24,6 +25,27 @@ static void restore_term_on_signal(int sig)raise (sig); } +static int disable_echo (void ) +{ +struct termios t; + + term_fd =open ("/dev/tty" ,O_RDWR ); +if (tcgetattr (term_fd, &t) <0 ) + goto error; + + old_term = t; +sigchain_push_common (restore_term_on_signal); + + t.c_lflag &= ~ECHO ; +if (!tcsetattr (term_fd,TCSAFLUSH , &t)) +return 0 ; + +error: +close (term_fd); + term_fd =-1 ; +return -1 ; +} +char *git_terminal_prompt (const char *prompt,int echo) {static struct strbuf buf =STRBUF_INIT ;
@@ -34,24 +56,9 @@ char *git_terminal_prompt(const char *prompt, int echo)if (!fh)return NULL ; -if (!echo) { -struct termios t; - -if (tcgetattr (fileno (fh), &t) <0 ) { -fclose (fh); -return NULL ; - } - - old_term = t; - term_fd =fileno (fh); -sigchain_push_common (restore_term_on_signal); - - t.c_lflag &= ~ECHO ; -if (tcsetattr (fileno (fh),TCSAFLUSH , &t) <0 ) { - term_fd =-1 ; -fclose (fh); -return NULL ; - } +if (!echo &&disable_echo ()) { +fclose (fh); +return NULL ; }fputs (prompt, fh);
Signature · 15 lines
1.8.0.4.g3c6fb4f.dirty -- *** Please reply-to-all at all times *** *** (do not pretend to know who is subscribed and who is not) *** *** Please avoid top-posting. *** The msysGit Wiki is here: https://github.com/msysgit/msysgit/wiki - Github accounts are free. You received this message because you are subscribed to the Google Groups "msysGit" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, and view previous threads, visit this group at http://groups.google.com/group/msysgit?hl=en_US?hl=en
PATCH v2 4/6 compat/terminal: separate input and output handles
In reply to Erik Faye-LundOn Windows, the terminal cannot be opened in read-write mode, so we need distinct pairs for reading and writing. Since this works fine on other platforms as well, always open them in pairs.
--- compat/terminal.c | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-)
compat/terminal.c +18 -11
@@ -50,29 +50,36 @@ char *git_terminal_prompt(const char *prompt, int echo) {static struct strbuf buf =STRBUF_INIT ;int r; -FILE *fh; +FILE *input_fh, *output_fh; - fh =fopen ("/dev/tty" ,"w+" ); -if (!fh) + input_fh =fopen ("/dev/tty" ,"r" ); +if (!input_fh)return NULL ; + output_fh =fopen ("/dev/tty" ,"w" ); +if (!output_fh) { +fclose (input_fh); +return NULL ; + } +if (!echo &&disable_echo ()) { -fclose (fh); +fclose (input_fh); +fclose (output_fh);return NULL ; } -fputs (prompt, fh); -fflush (fh); +fputs (prompt, output_fh); +fflush (output_fh); - r =strbuf_getline (&buf, fh,'\n' ); + r =strbuf_getline (&buf, input_fh,'\n' );if (!echo) { -fseek (fh,SEEK_CUR ,0 ); -putc ('\n' , fh); -fflush (fh); +putc ('\n' , output_fh); +fflush (output_fh); }restore_term (); -fclose (fh); +fclose (input_fh); +fclose (output_fh);if (r ==EOF )return NULL ;
Signature · 15 lines
1.8.0.4.g3c6fb4f.dirty -- *** Please reply-to-all at all times *** *** (do not pretend to know who is subscribed and who is not) *** *** Please avoid top-posting. *** The msysGit Wiki is here: https://github.com/msysgit/msysgit/wiki - Github accounts are free. You received this message because you are subscribed to the Google Groups "msysGit" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, and view previous threads, visit this group at http://groups.google.com/group/msysgit?hl=en_US?hl=en
PATCH v2 6/6 mingw: get rid of getpass implementation
In reply to Erik Faye-LundThere's no remaining call-sites, and as pointed out in the previous commit message, it's not quite ideal. So let's just lose it.
--- compat/mingw.c | 15 --------------- compat/mingw.h | 2 -- 2 files changed, 17 deletions(-)
compat/mingw.c +0 -15
@@ -1758,21 +1758,6 @@ int link(const char *oldpath, const char *newpath)return 0 ; } -char *getpass (const char *prompt) -{ -struct strbuf buf =STRBUF_INIT ; - -fputs (prompt, stderr); -for (;;) { -char c =_getch (); -if (c =='\r' || c =='\n' ) -break ; -strbuf_addch (&buf, c); - } -fputs ("\n" , stderr); -return strbuf_detach (&buf,NULL ); -} -pid_t waitpid (pid_t pid,int *status,int options) { HANDLE h = OpenProcess(SYNCHRONIZE | PROCESS_QUERY_INFORMATION,
compat/mingw.h +0 -2
@@ -55,8 +55,6 @@ struct passwd {char *pw_dir; }; -extern char *getpass (const char *prompt); -typedef void (__cdecl *sig_handler_t )(int );struct sigaction {sig_handler_t sa_handler;
Signature · 15 lines
1.8.0.4.g3c6fb4f.dirty -- *** Please reply-to-all at all times *** *** (do not pretend to know who is subscribed and who is not) *** *** Please avoid top-posting. *** The msysGit Wiki is here: https://github.com/msysgit/msysgit/wiki - Github accounts are free. You received this message because you are subscribed to the Google Groups "msysGit" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, and view previous threads, visit this group at http://groups.google.com/group/msysgit?hl=en_US?hl=en
PATCH v2 5/6 mingw: reuse tty-version of git_terminal_prompt
In reply to Erik Faye-LundThe getpass-implementation we use on Windows isn't at all ideal; it works in raw-mode (as opposed to cooked mode), and as a result does not deal correcly with deletion, arrow-keys etc.
Instead, use cooked mode to read a line at the time, allowing the C run-time to process the input properly.
Since we set files to be opened in binary-mode by default on Windows, introduce a FORCE_TEXT macro that expands to the "t" modifier that forces the terminal to be opened in text-mode so we do not have to deal with CRLF issues.
--- compat/terminal.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 60 insertions(+), 9 deletions(-)
compat/terminal.c +60 -9
@@ -3,8 +3,22 @@#include "sigchain.h" #include "strbuf.h" +#if defined(HAVE_DEV_TTY ) || defined(WIN32 ) + +static void restore_term (void ); + +static void restore_term_on_signal (int sig) +{ +restore_term (); +sigchain_pop (sig); +raise (sig); +} +#ifdef HAVE_DEV_TTY +#define INPUT_PATH "/dev/tty" +#define OUTPUT_PATH "/dev/tty" +static int term_fd =-1 ;static struct termios old_term;
@@ -18,13 +32,6 @@ static void restore_term(void) term_fd =-1 ; } -static void restore_term_on_signal (int sig) -{ -restore_term (); -sigchain_pop (sig); -raise (sig); -} -static int disable_echo (void ) {struct termios t;
@@ -46,17 +53,61 @@ error:return -1 ; } +#elif defined(WIN32 ) + +#define INPUT_PATH "CONIN$" +#define OUTPUT_PATH "CONOUT$" +#define FORCE_TEXT "t" + +static HANDLE hconin =INVALID_HANDLE_VALUE ; +static DWORD cmode; + +static void restore_term (void ) +{ +if (hconin ==INVALID_HANDLE_VALUE ) +return ; + +SetConsoleMode (hconin, cmode); +CloseHandle (hconin); + hconin =INVALID_HANDLE_VALUE ; +} + +static int disable_echo (void ) +{ + hconin =CreateFile ("CONIN$" ,GENERIC_READ |GENERIC_WRITE , +FILE_SHARE_READ ,NULL ,OPEN_EXISTING , +FILE_ATTRIBUTE_NORMAL ,NULL ); +if (hconin ==INVALID_HANDLE_VALUE ) +return -1 ; + +GetConsoleMode (hconin, &cmode); +sigchain_push_common (restore_term_on_signal); +if (!SetConsoleMode (hconin, cmode & (~ENABLE_ECHO_INPUT ))) { +CloseHandle (hconin); + hconin =INVALID_HANDLE_VALUE ; +return -1 ; + } + +return 0 ; +} + +#endif + +#ifndef FORCE_TEXT +#define FORCE_TEXT +#endif +char *git_terminal_prompt (const char *prompt,int echo) {static struct strbuf buf =STRBUF_INIT ;int r;FILE *input_fh, *output_fh; - input_fh =fopen ("/dev/tty" ,"r" ); + input_fh =fopen (INPUT_PATH ,"r" FORCE_TEXT );if (!input_fh)return NULL ; - output_fh =fopen ("/dev/tty" ,"w" ); + output_fh =fopen (OUTPUT_PATH ,"w" FORCE_TEXT );if (!output_fh) {fclose (input_fh);return NULL ;
Signature · 15 lines
1.8.0.4.g3c6fb4f.dirty -- *** Please reply-to-all at all times *** *** (do not pretend to know who is subscribed and who is not) *** *** Please avoid top-posting. *** The msysGit Wiki is here: https://github.com/msysgit/msysgit/wiki - Github accounts are free. You received this message because you are subscribed to the Google Groups "msysGit" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, and view previous threads, visit this group at http://groups.google.com/group/msysgit?hl=en_US?hl=en