1 /* ttyio.c - tty i/O functions
2 * Copyright (C) 1998,1999,2000,2001,2002,2003,
3 * 2004, 2006 Free Software Foundation, Inc.
5 * This file is part of GnuPG.
7 * GnuPG is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * GnuPG is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
31 /* simulate termios with termio */
33 #define termios termio
34 #define tcsetattr ioctl
35 #define TCSAFLUSH TCSETAF
36 #define tcgetattr(A,B) ioctl(A,TCGETA,B)
37 #define HAVE_TCGETATTR
40 #ifdef _WIN32 /* use the odd Win32 functions */
43 #error mingw32 and termios
51 #include "estream-printf.h"
52 #include "common-defs.h"
54 #define CONTROL_D ('D' - 'A' + 1)
56 #ifdef _WIN32 /* use the odd Win32 functions */
60 #define DEF_INPMODE (ENABLE_LINE_INPUT|ENABLE_ECHO_INPUT \
61 |ENABLE_PROCESSED_INPUT )
62 #define HID_INPMODE (ENABLE_LINE_INPUT|ENABLE_PROCESSED_INPUT )
63 #define DEF_OUTMODE (ENABLE_WRAP_AT_EOL_OUTPUT|ENABLE_PROCESSED_OUTPUT)
65 #else /* yeah, we have a real OS */
66 static FILE *ttyfp
= NULL
;
69 static int initialized
;
70 static int last_prompt_len
;
72 static int no_terminal
;
75 static struct termios termsave
;
76 static int restore_termios
;
79 /* Hooks set by gpgrlhelp.c if required. */
80 static void (*my_rl_set_completer
) (rl_completion_func_t
*);
81 static void (*my_rl_inhibit_completion
) (int);
82 static void (*my_rl_cleanup_after_signal
) (void);
83 static void (*my_rl_init_stream
) (FILE *);
84 static char *(*my_rl_readline
) (const char*);
85 static void (*my_rl_add_history
) (const char*);
88 /* This is a wrapper around ttyname so that we can use it even when
89 the standard streams are redirected. It figures the name out the
90 first time and returns it in a statically allocated buffer. */
92 tty_get_ttyname (void)
96 /* On a GNU system ctermid() always return /dev/tty, so this does
97 not make much sense - however if it is ever changed we do the
105 /* Note that despite our checks for these macros the function is
106 not necessarily thread save. We mainly do this for
107 portability reasons, in case L_ctermid is not defined. */
108 # if defined(_POSIX_THREAD_SAFE_FUNCTIONS) || defined(_POSIX_TRHEADS)
109 char buffer
[L_ctermid
];
110 s
= ctermid (buffer
);
118 #endif /*HAVE_CTERMID*/
119 /* Assume the standard tty on memory error or when tehre is no
121 return name
? name
: "/dev/tty";
126 #ifdef HAVE_TCGETATTR
130 if( restore_termios
) {
131 restore_termios
= 0; /* do it prios in case it is interrupted again */
132 if( tcsetattr(fileno(ttyfp
), TCSAFLUSH
, &termsave
) )
133 log_error("tcsetattr() failed: %s\n", strerror(errno
) );
146 SECURITY_ATTRIBUTES sa
;
148 memset(&sa
, 0, sizeof(sa
));
149 sa
.nLength
= sizeof(sa
);
150 sa
.bInheritHandle
= TRUE
;
151 con
.out
= CreateFileA( "CONOUT$", GENERIC_READ
|GENERIC_WRITE
,
152 FILE_SHARE_READ
|FILE_SHARE_WRITE
,
153 &sa
, OPEN_EXISTING
, 0, 0 );
154 if( con
.out
== INVALID_HANDLE_VALUE
)
155 log_fatal("open(CONOUT$) failed: rc=%d", (int)GetLastError() );
156 memset(&sa
, 0, sizeof(sa
));
157 sa
.nLength
= sizeof(sa
);
158 sa
.bInheritHandle
= TRUE
;
159 con
.in
= CreateFileA( "CONIN$", GENERIC_READ
|GENERIC_WRITE
,
160 FILE_SHARE_READ
|FILE_SHARE_WRITE
,
161 &sa
, OPEN_EXISTING
, 0, 0 );
162 if( con
.in
== INVALID_HANDLE_VALUE
)
163 log_fatal("open(CONIN$) failed: rc=%d", (int)GetLastError() );
165 SetConsoleMode(con
.in
, DEF_INPMODE
);
166 SetConsoleMode(con
.out
, DEF_OUTMODE
);
168 #elif defined(__EMX__)
169 ttyfp
= stdout
; /* Fixme: replace by the real functions: see wklib */
170 if (my_rl_init_stream
)
171 my_rl_init_stream (ttyfp
);
173 ttyfp
= batchmode
? stderr
: fopen (tty_get_ttyname (), "r+");
175 log_error("cannot open `%s': %s\n", tty_get_ttyname (),
179 if (my_rl_init_stream
)
180 my_rl_init_stream (ttyfp
);
184 #ifdef HAVE_TCGETATTR
192 tty_batchmode( int onoff
)
201 tty_no_terminal(int onoff
)
203 int old
= no_terminal
;
204 no_terminal
= onoff
? 1 : 0;
209 tty_printf( const char *fmt
, ... )
219 va_start( arg_ptr
, fmt
) ;
226 n
= vasprintf(&buf
, fmt
, arg_ptr
);
228 log_bug("vasprintf() failed\n");
230 if( !WriteConsoleA( con
.out
, buf
, n
, &nwritten
, NULL
) )
231 log_fatal("WriteConsole failed: rc=%d", (int)GetLastError() );
233 log_fatal("WriteConsole failed: %d != %d\n", n
, (int)nwritten
);
234 last_prompt_len
+= n
;
238 last_prompt_len
+= vfprintf(ttyfp
,fmt
,arg_ptr
) ;
245 /* Same as tty_printf but if FP is not NULL, behave like a regular
248 tty_fprintf (FILE *fp
, const char *fmt
, ... )
254 va_start (arg_ptr
, fmt
) ;
255 vfprintf (fp
, fmt
, arg_ptr
);
266 va_start( arg_ptr
, fmt
) ;
273 n
= vasprintf(&buf
, fmt
, arg_ptr
);
275 log_bug("vasprintf() failed\n");
277 if( !WriteConsoleA( con
.out
, buf
, n
, &nwritten
, NULL
) )
278 log_fatal("WriteConsole failed: rc=%d", (int)GetLastError() );
280 log_fatal("WriteConsole failed: %d != %d\n", n
, (int)nwritten
);
281 last_prompt_len
+= n
;
285 last_prompt_len
+= vfprintf(ttyfp
,fmt
,arg_ptr
) ;
293 * Print a string, but filter all control characters out.
296 tty_print_string ( const byte
*p
, size_t n
)
305 /* not so effective, change it if you want */
307 if( iscntrl( *p
) ) {
313 tty_printf("\\x%02x", *p
);
316 tty_printf("%c", *p
);
319 if( iscntrl( *p
) ) {
326 fprintf(ttyfp
, "x%02x", *p
);
334 tty_print_utf8_string2( const byte
*p
, size_t n
, size_t max_n
)
342 /* we can handle plain ascii simpler, so check for it first */
343 for(i
=0; i
< n
; i
++ ) {
348 buf
= utf8_to_native( (const char *)p
, n
, 0 );
349 if( max_n
&& (strlen( buf
) > max_n
)) {
352 /*(utf8 conversion already does the control character quoting)*/
353 tty_printf("%s", buf
);
357 if( max_n
&& (n
> max_n
) ) {
360 tty_print_string( p
, n
);
365 tty_print_utf8_string( const byte
*p
, size_t n
)
367 tty_print_utf8_string2( p
, n
, 0 );
372 do_get( const char *prompt
, int hidden
)
381 log_error("Sorry, we are in batchmode - can't get input\n");
386 log_error("Sorry, no terminal at all requested - can't get input\n");
394 tty_printf( "%s", prompt
);
395 buf
= xmalloc((n
=50));
398 #ifdef _WIN32 /* windoze version */
400 SetConsoleMode(con
.in
, HID_INPMODE
);
405 if( !ReadConsoleA( con
.in
, cbuf
, 1, &nread
, NULL
) )
406 log_fatal("ReadConsole failed: rc=%d", (int)GetLastError() );
418 ; /* we don't allow 0xa0, as this is a protected blank which may
419 * confuse the user */
420 else if( iscntrl(c
) )
424 buf
= xrealloc (buf
, n
);
430 SetConsoleMode(con
.in
, DEF_INPMODE
);
432 #elif defined(__riscos__)
434 c
= riscos_getchar();
435 if (c
== 0xa || c
== 0xd) { /* Return || Enter */
437 } else if (c
== 0x8 || c
== 0x7f) { /* Backspace || Delete */
452 } else if (c
== (int) '\t') { /* Tab */
454 } else if (c
> 0xa0) {
455 ; /* we don't allow 0xa0, as this is a protected blank which may
456 * confuse the user */
457 } else if (iscntrl(c
)) {
462 buf
= xrealloc (buf
, n
);
472 #else /* unix version */
474 #ifdef HAVE_TCGETATTR
477 if( tcgetattr(fileno(ttyfp
), &termsave
) )
478 log_fatal("tcgetattr() failed: %s\n", strerror(errno
) );
481 term
.c_lflag
&= ~(ECHO
| ECHOE
| ECHOK
| ECHONL
);
482 if( tcsetattr( fileno(ttyfp
), TCSAFLUSH
, &term
) )
483 log_fatal("tcsetattr() failed: %s\n", strerror(errno
) );
487 /* fixme: How can we avoid that the \n is echoed w/o disabling
488 * canonical mode - w/o this kill_prompt can't work */
489 while( read(fileno(ttyfp
), cbuf
, 1) == 1 && *cbuf
!= '\n' ) {
494 log_info("control d found\n");
498 ; /* we don't allow 0xa0, as this is a protected blank which may
499 * confuse the user */
500 else if( iscntrl(c
) )
504 buf
= xrealloc (buf
, n
);
508 if( *cbuf
!= '\n' ) {
515 #ifdef HAVE_TCGETATTR
516 if( tcsetattr(fileno(ttyfp
), TCSAFLUSH
, &termsave
) )
517 log_error("tcsetattr() failed: %s\n", strerror(errno
) );
521 #endif /* end unix version */
528 tty_get( const char *prompt
)
530 if (!batchmode
&& !no_terminal
&& my_rl_readline
&& my_rl_add_history
)
540 line
= my_rl_readline (prompt
?prompt
:"");
542 /* We need to copy it to memory controlled by our malloc
543 implementations; further we need to convert an EOF to our
545 buf
= xmalloc(line
? strlen(line
)+1:2);
550 if (strlen (buf
) > 2 )
551 my_rl_add_history (line
); /* Note that we test BUF but add LINE. */
562 return do_get ( prompt
, 0 );
565 /* Variable argument version of tty_get. The prompt is is actually a
566 format string with arguments. */
568 tty_getf (const char *promptfmt
, ... )
574 va_start (arg_ptr
, promptfmt
);
575 if (estream_vasprintf (&prompt
, promptfmt
, arg_ptr
) < 0)
576 log_fatal ("estream_vasprintf failed: %s\n", strerror (errno
));
578 answer
= tty_get (prompt
);
586 tty_get_hidden( const char *prompt
)
588 return do_get( prompt
, 1 );
603 if( !last_prompt_len
)
606 tty_printf("\r%*s\r", last_prompt_len
, "");
611 for(i
=0; i
< last_prompt_len
; i
++ )
622 tty_get_answer_is_yes( const char *prompt
)
625 char *p
= tty_get( prompt
);
627 yes
= answer_is_yes(p
);
633 /* Called by gnupg_rl_initialize to setup the readline support. */
635 tty_private_set_rl_hooks (void (*init_stream
) (FILE *),
636 void (*set_completer
) (rl_completion_func_t
*),
637 void (*inhibit_completion
) (int),
638 void (*cleanup_after_signal
) (void),
639 char *(*readline_fun
) (const char*),
640 void (*add_history_fun
) (const char*))
642 my_rl_init_stream
= init_stream
;
643 my_rl_set_completer
= set_completer
;
644 my_rl_inhibit_completion
= inhibit_completion
;
645 my_rl_cleanup_after_signal
= cleanup_after_signal
;
646 my_rl_readline
= readline_fun
;
647 my_rl_add_history
= add_history_fun
;
652 tty_enable_completion (rl_completion_func_t
*completer
)
654 if (no_terminal
|| !my_rl_set_completer
)
660 my_rl_set_completer (completer
);
664 tty_disable_completion (void)
666 if (no_terminal
|| !my_rl_inhibit_completion
)
672 my_rl_inhibit_completion (1);
677 tty_cleanup_after_signal (void)
679 #ifdef HAVE_TCGETATTR
685 tty_cleanup_rl_after_signal (void)
687 if (my_rl_cleanup_after_signal
)
688 my_rl_cleanup_after_signal ();