Revert last. It is still wrong.
[gnupg.git] / util / ttyio.c
blob97b160ef2b2d9f124dbb6fc2e11af46f7b39d980
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,
20 * USA.
23 #include <config.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdarg.h>
28 #include <unistd.h>
29 #ifdef HAVE_TCGETATTR
30 #include <termios.h>
31 #else
32 #ifdef HAVE_TERMIO_H
33 /* simulate termios with termio */
34 #include <termio.h>
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
40 #endif
41 #endif
42 #ifdef _WIN32 /* use the odd Win32 functions */
43 #include <windows.h>
44 #ifdef HAVE_TCGETATTR
45 #error windows and termios
46 #endif
47 #endif
48 #include <errno.h>
49 #include <ctype.h>
50 #ifdef HAVE_LIBREADLINE
51 #include <readline/readline.h>
52 #include <readline/history.h>
53 #endif
55 #include "util.h"
56 #include "memory.h"
57 #include "ttyio.h"
59 #define CONTROL_D ('D' - 'A' + 1)
61 #ifdef _WIN32 /* use the odd Win32 functions */
62 static struct {
63 HANDLE in, out;
64 } con;
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;
72 #endif
74 static int initialized;
75 static int last_prompt_len;
76 static int batchmode;
77 static int no_terminal;
79 #ifdef HAVE_TCGETATTR
80 static struct termios termsave;
81 static int restore_termios;
82 #endif
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. */
89 const char *
90 tty_get_ttyname (void)
92 static char *name;
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
96 Right Thing now. */
97 #ifdef HAVE_CTERMID
98 static int got_name;
100 if (!got_name)
102 const char *s;
103 s = ctermid (NULL);
104 if (s)
105 name = strdup (s);
106 got_name = 1;
108 #endif
109 /* Assume the standard tty on memory error or when there is no
110 ctermid. */
111 return name? name : "/dev/tty";
115 #ifdef HAVE_TCGETATTR
116 static void
117 cleanup(void)
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) );
125 #endif
127 static void
128 init_ttyfp(void)
130 if( initialized )
131 return;
133 #if defined(_WIN32)
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 */
159 #else
160 ttyfp = batchmode? stderr : fopen( tty_get_ttyname (), "r+");
161 if( !ttyfp ) {
162 log_error("cannot open `%s': %s\n",
163 tty_get_ttyname (), strerror(errno) );
164 exit(2);
166 #ifdef HAVE_LIBREADLINE
167 rl_catch_signals = 0;
168 rl_instream = rl_outstream = ttyfp;
169 rl_inhibit_completion = 1;
170 #endif
171 #endif
172 #ifdef HAVE_TCGETATTR
173 atexit( cleanup );
174 #endif
175 initialized = 1;
178 #ifdef HAVE_LIBREADLINE
179 void
180 tty_enable_completion(rl_completion_func_t *completer)
182 if( no_terminal )
183 return;
185 if( !initialized )
186 init_ttyfp();
188 rl_attempted_completion_function=completer;
189 rl_inhibit_completion=0;
192 void
193 tty_disable_completion(void)
195 if( no_terminal )
196 return;
198 if( !initialized )
199 init_ttyfp();
201 rl_inhibit_completion=1;
203 #endif /*HAVE_LIBREADLINE*/
206 tty_batchmode( int onoff )
208 int old = batchmode;
209 if( onoff != -1 )
210 batchmode = onoff;
211 return old;
215 tty_no_terminal(int onoff)
217 int old = no_terminal;
218 no_terminal = onoff ? 1 : 0;
219 return old;
222 void
223 tty_printf( const char *fmt, ... )
225 va_list arg_ptr;
227 if (no_terminal)
228 return;
230 if( !initialized )
231 init_ttyfp();
233 va_start( arg_ptr, fmt ) ;
234 #ifdef _WIN32
236 char *buf = NULL;
237 int n;
238 DWORD nwritten;
240 n = vasprintf(&buf, fmt, arg_ptr);
241 if( !buf )
242 log_bug("vasprintf() failed\n");
244 if (!WriteConsoleA (con.out, buf, n, &nwritten, NULL))
245 log_fatal ("WriteConsole failed: %s", w32_strerror (0));
246 if( n != nwritten )
247 log_fatal ("WriteConsole failed: %d != %d\n", n, (int)nwritten );
248 last_prompt_len += n;
249 xfree (buf);
251 #else
252 last_prompt_len += vfprintf(ttyfp,fmt,arg_ptr) ;
253 fflush(ttyfp);
254 #endif
255 va_end(arg_ptr);
259 /* Same as tty_printf but if FP is not NULL, behave like the standard
260 fprintf. */
261 void
262 tty_fprintf (FILE *fp, const char *fmt, ... )
264 va_list arg_ptr;
266 if (fp)
268 va_start (arg_ptr, fmt) ;
269 vfprintf (fp, fmt, arg_ptr );
270 va_end (arg_ptr);
271 return;
274 if (no_terminal)
275 return;
277 if( !initialized )
278 init_ttyfp();
280 va_start( arg_ptr, fmt ) ;
281 #ifdef _WIN32
283 char *buf = NULL;
284 int n;
285 DWORD nwritten;
287 n = vasprintf(&buf, fmt, arg_ptr);
288 if( !buf )
289 log_bug("vasprintf() failed\n");
291 if (!WriteConsoleA (con.out, buf, n, &nwritten, NULL))
292 log_fatal ("WriteConsole failed: %s", w32_strerror (0));
293 if (n != nwritten)
294 log_fatal ("WriteConsole failed: %d != %d\n", n, (int)nwritten);
295 last_prompt_len += n;
296 xfree (buf);
298 #else
299 last_prompt_len += vfprintf(ttyfp,fmt,arg_ptr) ;
300 fflush(ttyfp);
301 #endif
302 va_end(arg_ptr);
306 /****************
307 * Print a string, but filter all control characters out.
309 void
310 tty_print_string( const byte *p, size_t n )
312 if (no_terminal)
313 return;
315 if( !initialized )
316 init_ttyfp();
318 #ifdef _WIN32
319 /* not so effective, change it if you want */
320 for( ; n; n--, p++ )
321 if( iscntrl( *p ) ) {
322 if( *p == '\n' )
323 tty_printf("\\n");
324 else if( !*p )
325 tty_printf("\\0");
326 else
327 tty_printf("\\x%02x", *p);
329 else
330 tty_printf("%c", *p);
331 #else
332 for( ; n; n--, p++ )
333 if( iscntrl( *p ) ) {
334 putc('\\', ttyfp);
335 if( *p == '\n' )
336 putc('n', ttyfp);
337 else if( !*p )
338 putc('0', ttyfp);
339 else
340 fprintf(ttyfp, "x%02x", *p );
342 else
343 putc(*p, ttyfp);
344 #endif
347 void
348 tty_print_utf8_string2 (const byte *p, size_t n, size_t max_n )
350 size_t i;
351 char *buf;
353 if (no_terminal)
354 return;
356 /* we can handle plain ascii simpler, so check for it first */
357 for(i=0; i < n; i++ ) {
358 if( p[i] & 0x80 )
359 break;
361 if( i < n ) {
362 buf = utf8_to_native( p, n, 0 );
363 if( max_n && (strlen( buf ) > max_n )) {
364 buf[max_n] = 0;
366 /*(utf8 conversion already does the control character quoting)*/
367 tty_printf("%s", buf );
368 xfree( buf );
370 else {
371 if( max_n && (n > max_n) ) {
372 n = max_n;
374 tty_print_string( p, n );
378 void
379 tty_print_utf8_string( const byte *p, size_t n )
381 tty_print_utf8_string2( p, n, 0 );
385 static char *
386 do_get( const char *prompt, int hidden )
388 char *buf;
389 #ifndef __riscos__
390 byte cbuf[1];
391 #endif
392 int c, n, i;
394 if( batchmode ) {
395 log_error("Sorry, we are in batchmode - can't get input\n");
396 exit(2);
399 if (no_terminal) {
400 log_error("Sorry, no terminal at all requested - can't get input\n");
401 exit(2);
404 if( !initialized )
405 init_ttyfp();
407 last_prompt_len = 0;
408 buf = xmalloc(n=50);
409 i = 0;
411 #ifdef _WIN32 /* windoze version */
412 if( hidden )
413 SetConsoleMode(con.in, HID_INPMODE );
415 tty_printf( "%s", prompt );
417 for(;;) {
418 DWORD nread;
420 if (!ReadConsoleA (con.in, cbuf, 1, &nread, NULL))
421 log_fatal ("ReadConsole failed: %s", w32_strerror (0));
422 if( !nread )
423 continue;
424 if( *cbuf == '\n' )
425 break;
427 if( !hidden )
428 last_prompt_len++;
429 c = *cbuf;
430 if( c == '\t' )
431 c = ' ';
432 else if( c > 0xa0 )
433 ; /* we don't allow 0xa0, as this is a protected blank which may
434 * confuse the user */
435 else if( iscntrl(c) )
436 continue;
437 if( !(i < n-1) ) {
438 n += 50;
439 buf = xrealloc( buf, n );
441 buf[i++] = c;
444 if( hidden )
445 SetConsoleMode(con.in, DEF_INPMODE );
447 #elif defined(__riscos__)
448 tty_printf( "%s", prompt );
449 do {
450 c = riscos_getchar();
451 if (c == 0xa || c == 0xd) { /* Return || Enter */
452 c = (int) '\n';
453 } else if (c == 0x8 || c == 0x7f) { /* Backspace || Delete */
454 if (i>0) {
455 i--;
456 if (!hidden) {
457 last_prompt_len--;
458 fputc(8, ttyfp);
459 fputc(32, ttyfp);
460 fputc(8, ttyfp);
461 fflush(ttyfp);
463 } else {
464 fputc(7, ttyfp);
465 fflush(ttyfp);
467 continue;
468 } else if (c == (int) '\t') { /* Tab */
469 c = ' ';
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)) {
474 continue;
476 if(!(i < n-1)) {
477 n += 50;
478 buf = xrealloc(buf, n);
480 buf[i++] = c;
481 if (!hidden) {
482 last_prompt_len++;
483 fputc(c, ttyfp);
484 fflush(ttyfp);
486 } while (c != '\n');
487 i = (i>0) ? i-1 : 0;
488 #else /* unix version */
489 if( hidden ) {
490 #ifdef HAVE_TCGETATTR
491 struct termios term;
493 if( tcgetattr(fileno(ttyfp), &termsave) )
494 log_fatal("tcgetattr() failed: %s\n", strerror(errno) );
495 restore_termios = 1;
496 term = termsave;
497 term.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
498 if( tcsetattr( fileno(ttyfp), TCSAFLUSH, &term ) )
499 log_fatal("tcsetattr() failed: %s\n", strerror(errno) );
500 #endif
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' ) {
508 if( !hidden )
509 last_prompt_len++;
510 c = *cbuf;
511 if( c == CONTROL_D )
512 log_info("control d found\n");
513 if( c == '\t' )
514 c = ' ';
515 else if( c > 0xa0 )
516 ; /* we don't allow 0xa0, as this is a protected blank which may
517 * confuse the user */
518 else if( iscntrl(c) )
519 continue;
520 if( !(i < n-1) ) {
521 n += 50;
522 buf = xrealloc( buf, n );
524 buf[i++] = c;
526 if( *cbuf != '\n' ) {
527 buf[0] = CONTROL_D;
528 i = 1;
532 if( hidden ) {
533 #ifdef HAVE_TCGETATTR
534 if( tcsetattr(fileno(ttyfp), TCSAFLUSH, &termsave) )
535 log_error("tcsetattr() failed: %s\n", strerror(errno) );
536 restore_termios = 0;
537 #endif
539 #endif /* end unix version */
540 buf[i] = 0;
541 return buf;
545 char *
546 tty_get( const char *prompt )
548 #ifdef HAVE_LIBREADLINE
549 if (!batchmode && !no_terminal) {
550 char *line;
551 char *buf;
553 if( !initialized )
554 init_ttyfp();
556 last_prompt_len = 0;
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
562 convention. */
563 buf = xmalloc(line? strlen(line)+1:2);
564 if (line)
566 strcpy (buf, line);
567 trim_spaces (buf);
568 if (strlen (buf) > 2 )
569 add_history (line); /* Note that we test BUF but add LINE. */
570 free (line);
572 else
574 buf[0] = CONTROL_D;
575 buf[1] = 0;
577 return buf;
579 else
580 #endif /* HAVE_LIBREADLINE */
581 return do_get( prompt, 0 );
584 char *
585 tty_get_hidden( const char *prompt )
587 return do_get( prompt, 1 );
591 void
592 tty_kill_prompt()
594 if ( no_terminal )
595 return;
597 if( !initialized )
598 init_ttyfp();
600 if( batchmode )
601 last_prompt_len = 0;
602 if( !last_prompt_len )
603 return;
604 #ifdef _WIN32
605 tty_printf("\r%*s\r", last_prompt_len, "");
606 #else
608 int i;
609 putc('\r', ttyfp);
610 for(i=0; i < last_prompt_len; i ++ )
611 putc(' ', ttyfp);
612 putc('\r', ttyfp);
613 fflush(ttyfp);
615 #endif
616 last_prompt_len = 0;
621 tty_get_answer_is_yes( const char *prompt )
623 int yes;
624 char *p = tty_get( prompt );
625 tty_kill_prompt();
626 yes = answer_is_yes(p);
627 xfree(p);
628 return yes;