1 /* ttyio.c - tty i/O functions
2 * Copyright (C) 1998, 1999, 2000, 2001, 2002,
3 * 2004 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 2 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, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
33 /* simulate termios with termio */
35 #define termios termio
36 #define tcsetattr ioctl
37 #define TCSAFLUSH TCSETAF
38 #define tcgetattr(A,B) ioctl(A,TCGETA,B)
39 #define HAVE_TCGETATTR
42 #ifdef _WIN32 /* use the odd Win32 functions */
45 #error windows and termios
50 #ifdef HAVE_LIBREADLINE
51 #include <readline/readline.h>
52 #include <readline/history.h>
59 #define CONTROL_D ('D' - 'A' + 1)
61 #ifdef _WIN32 /* use the odd Win32 functions */
65 #define DEF_INPMODE (ENABLE_LINE_INPUT|ENABLE_ECHO_INPUT \
66 |ENABLE_PROCESSED_INPUT )
67 #define HID_INPMODE (ENABLE_LINE_INPUT|ENABLE_PROCESSED_INPUT )
68 #define DEF_OUTMODE (ENABLE_WRAP_AT_EOL_OUTPUT|ENABLE_PROCESSED_OUTPUT)
70 #else /* yeah, we have a real OS */
71 static FILE *ttyfp
= NULL
;
74 static int initialized
;
75 static int last_prompt_len
;
77 static int no_terminal
;
80 static struct termios termsave
;
81 static int restore_termios
;
86 /* This is a wrapper around ttyname so that we can use it even when
87 the standard streams are redirected. It figures the name out the
88 first time and returns it in a statically allocated buffer. */
90 tty_get_ttyname (void)
94 /* On a GNU system ctermid() always return /dev/tty, so this does
95 not make much sense - however if it is ever changed we do the
109 /* Assume the standard tty on memory error or when there is no
111 return name
? name
: "/dev/tty";
115 #ifdef HAVE_TCGETATTR
119 if( restore_termios
) {
120 restore_termios
= 0; /* do it prios in case it is interrupted again */
121 if( tcsetattr(fileno(ttyfp
), TCSAFLUSH
, &termsave
) )
122 log_error("tcsetattr() failed: %s\n", strerror(errno
) );
135 SECURITY_ATTRIBUTES sa
;
137 memset(&sa
, 0, sizeof(sa
));
138 sa
.nLength
= sizeof(sa
);
139 sa
.bInheritHandle
= TRUE
;
140 con
.out
= CreateFileA( "CONOUT$", GENERIC_READ
|GENERIC_WRITE
,
141 FILE_SHARE_READ
|FILE_SHARE_WRITE
,
142 &sa
, OPEN_EXISTING
, 0, 0 );
143 if( con
.out
== INVALID_HANDLE_VALUE
)
144 log_fatal ("open(CONOUT$) failed: %s", w32_strerror (0));
145 memset(&sa
, 0, sizeof(sa
));
146 sa
.nLength
= sizeof(sa
);
147 sa
.bInheritHandle
= TRUE
;
148 con
.in
= CreateFileA( "CONIN$", GENERIC_READ
|GENERIC_WRITE
,
149 FILE_SHARE_READ
|FILE_SHARE_WRITE
,
150 &sa
, OPEN_EXISTING
, 0, 0 );
151 if (con
.in
== INVALID_HANDLE_VALUE
)
152 log_fatal ("open(CONIN$) failed: %s", w32_strerror (0));
154 SetConsoleMode(con
.in
, DEF_INPMODE
);
155 SetConsoleMode(con
.out
, DEF_OUTMODE
);
157 #elif defined(__EMX__)
158 ttyfp
= stdout
; /* Fixme: replace by the real functions: see wklib */
160 ttyfp
= batchmode
? stderr
: fopen( tty_get_ttyname (), "r+");
162 log_error("cannot open `%s': %s\n",
163 tty_get_ttyname (), strerror(errno
) );
166 #ifdef HAVE_LIBREADLINE
167 rl_catch_signals
= 0;
168 rl_instream
= rl_outstream
= ttyfp
;
169 rl_inhibit_completion
= 1;
172 #ifdef HAVE_TCGETATTR
178 #ifdef HAVE_LIBREADLINE
180 tty_enable_completion(rl_completion_func_t
*completer
)
188 rl_attempted_completion_function
=completer
;
189 rl_inhibit_completion
=0;
193 tty_disable_completion(void)
201 rl_inhibit_completion
=1;
203 #endif /*HAVE_LIBREADLINE*/
206 tty_batchmode( int onoff
)
215 tty_no_terminal(int onoff
)
217 int old
= no_terminal
;
218 no_terminal
= onoff
? 1 : 0;
223 tty_printf( const char *fmt
, ... )
233 va_start( arg_ptr
, fmt
) ;
240 n
= vasprintf(&buf
, fmt
, arg_ptr
);
242 log_bug("vasprintf() failed\n");
244 if (!WriteConsoleA (con
.out
, buf
, n
, &nwritten
, NULL
))
245 log_fatal ("WriteConsole failed: %s", w32_strerror (0));
247 log_fatal ("WriteConsole failed: %d != %d\n", n
, (int)nwritten
);
248 last_prompt_len
+= n
;
252 last_prompt_len
+= vfprintf(ttyfp
,fmt
,arg_ptr
) ;
259 /* Same as tty_printf but if FP is not NULL, behave like the standard
262 tty_fprintf (FILE *fp
, const char *fmt
, ... )
268 va_start (arg_ptr
, fmt
) ;
269 vfprintf (fp
, fmt
, arg_ptr
);
280 va_start( arg_ptr
, fmt
) ;
287 n
= vasprintf(&buf
, fmt
, arg_ptr
);
289 log_bug("vasprintf() failed\n");
291 if (!WriteConsoleA (con
.out
, buf
, n
, &nwritten
, NULL
))
292 log_fatal ("WriteConsole failed: %s", w32_strerror (0));
294 log_fatal ("WriteConsole failed: %d != %d\n", n
, (int)nwritten
);
295 last_prompt_len
+= n
;
299 last_prompt_len
+= vfprintf(ttyfp
,fmt
,arg_ptr
) ;
307 * Print a string, but filter all control characters out.
310 tty_print_string( const byte
*p
, size_t n
)
319 /* not so effective, change it if you want */
321 if( iscntrl( *p
) ) {
327 tty_printf("\\x%02x", *p
);
330 tty_printf("%c", *p
);
333 if( iscntrl( *p
) ) {
340 fprintf(ttyfp
, "x%02x", *p
);
348 tty_print_utf8_string2 (const byte
*p
, size_t n
, size_t max_n
)
356 /* we can handle plain ascii simpler, so check for it first */
357 for(i
=0; i
< n
; i
++ ) {
362 buf
= utf8_to_native( p
, n
, 0 );
363 if( max_n
&& (strlen( buf
) > max_n
)) {
366 /*(utf8 conversion already does the control character quoting)*/
367 tty_printf("%s", buf
);
371 if( max_n
&& (n
> max_n
) ) {
374 tty_print_string( p
, n
);
379 tty_print_utf8_string( const byte
*p
, size_t n
)
381 tty_print_utf8_string2( p
, n
, 0 );
386 do_get( const char *prompt
, int hidden
)
395 log_error("Sorry, we are in batchmode - can't get input\n");
400 log_error("Sorry, no terminal at all requested - can't get input\n");
411 #ifdef _WIN32 /* windoze version */
413 SetConsoleMode(con
.in
, HID_INPMODE
);
415 tty_printf( "%s", prompt
);
420 if (!ReadConsoleA (con
.in
, cbuf
, 1, &nread
, NULL
))
421 log_fatal ("ReadConsole failed: %s", w32_strerror (0));
433 ; /* we don't allow 0xa0, as this is a protected blank which may
434 * confuse the user */
435 else if( iscntrl(c
) )
439 buf
= xrealloc( buf
, n
);
445 SetConsoleMode(con
.in
, DEF_INPMODE
);
447 #elif defined(__riscos__)
448 tty_printf( "%s", prompt
);
450 c
= riscos_getchar();
451 if (c
== 0xa || c
== 0xd) { /* Return || Enter */
453 } else if (c
== 0x8 || c
== 0x7f) { /* Backspace || Delete */
468 } else if (c
== (int) '\t') { /* Tab */
470 } else if (c
> 0xa0) {
471 ; /* we don't allow 0xa0, as this is a protected blank which may
472 * confuse the user */
473 } else if (iscntrl(c
)) {
478 buf
= xrealloc(buf
, n
);
488 #else /* unix version */
490 #ifdef HAVE_TCGETATTR
493 if( tcgetattr(fileno(ttyfp
), &termsave
) )
494 log_fatal("tcgetattr() failed: %s\n", strerror(errno
) );
497 term
.c_lflag
&= ~(ECHO
| ECHOE
| ECHOK
| ECHONL
);
498 if( tcsetattr( fileno(ttyfp
), TCSAFLUSH
, &term
) )
499 log_fatal("tcsetattr() failed: %s\n", strerror(errno
) );
503 tty_printf( "%s", prompt
);
505 /* fixme: How can we avoid that the \n is echoed w/o disabling
506 * canonical mode - w/o this kill_prompt can't work */
507 while( read(fileno(ttyfp
), cbuf
, 1) == 1 && *cbuf
!= '\n' ) {
512 log_info("control d found\n");
516 ; /* we don't allow 0xa0, as this is a protected blank which may
517 * confuse the user */
518 else if( iscntrl(c
) )
522 buf
= xrealloc( buf
, n
);
526 if( *cbuf
!= '\n' ) {
533 #ifdef HAVE_TCGETATTR
534 if( tcsetattr(fileno(ttyfp
), TCSAFLUSH
, &termsave
) )
535 log_error("tcsetattr() failed: %s\n", strerror(errno
) );
539 #endif /* end unix version */
546 tty_get( const char *prompt
)
548 #ifdef HAVE_LIBREADLINE
549 if (!batchmode
&& !no_terminal
) {
558 line
= readline (prompt
?prompt
:"");
560 /* We need to copy it to memory controlled by our malloc
561 implementations; further we need to convert an EOF to our
563 buf
= xmalloc(line
? strlen(line
)+1:2);
568 if (strlen (buf
) > 2 )
569 add_history (line
); /* Note that we test BUF but add LINE. */
580 #endif /* HAVE_LIBREADLINE */
581 return do_get( prompt
, 0 );
585 tty_get_hidden( const char *prompt
)
587 return do_get( prompt
, 1 );
602 if( !last_prompt_len
)
605 tty_printf("\r%*s\r", last_prompt_len
, "");
610 for(i
=0; i
< last_prompt_len
; i
++ )
621 tty_get_answer_is_yes( const char *prompt
)
624 char *p
= tty_get( prompt
);
626 yes
= answer_is_yes(p
);