1
0
mirror of git://git.gnupg.org/gnupg.git synced 2024-11-04 20:38:50 +01:00
gnupg/util/ttyio.c

187 lines
3.6 KiB
C
Raw Normal View History

1997-11-18 15:06:00 +01:00
/* ttyio.c - tty i/O functions
* Copyright (c) 1997 by Werner Koch (dd9jn)
*
* This file is part of G10.
*
* G10 is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* G10 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
1998-01-16 22:15:24 +01:00
#include <unistd.h>
1998-01-05 20:13:15 +01:00
#ifdef HAVE_TCGETATTR
#include <termios.h>
#endif
1997-11-18 15:06:00 +01:00
#include <errno.h>
#include <ctype.h>
#include "util.h"
#include "memory.h"
#include "ttyio.h"
1998-01-02 21:40:10 +01:00
static FILE *ttyfp = NULL;
1997-11-18 15:06:00 +01:00
static int last_prompt_len;
1998-01-02 21:40:10 +01:00
static void
init_ttyfp()
1997-11-18 15:06:00 +01:00
{
1998-01-02 21:40:10 +01:00
if( ttyfp )
return;
1997-12-31 13:32:54 +01:00
1998-01-06 10:17:55 +01:00
#if defined(__MINGW32__)
ttyfp = stderr; /* fixme */
#else
1998-01-02 21:40:10 +01:00
ttyfp = fopen("/dev/tty", "r+");
1998-01-06 10:17:55 +01:00
#endif
1998-01-02 21:40:10 +01:00
if( !ttyfp )
1997-11-18 15:06:00 +01:00
log_fatal("cannot open /dev/tty: %s\n", strerror(errno) );
}
void
tty_printf( const char *fmt, ... )
{
va_list arg_ptr;
1998-01-02 21:40:10 +01:00
if( !ttyfp )
init_ttyfp();
1997-11-18 15:06:00 +01:00
va_start( arg_ptr, fmt ) ;
1998-01-02 21:40:10 +01:00
last_prompt_len += vfprintf(ttyfp,fmt,arg_ptr) ;
1997-11-18 15:06:00 +01:00
va_end(arg_ptr);
1998-01-02 21:40:10 +01:00
fflush(ttyfp);
1997-11-18 15:06:00 +01:00
}
1997-12-16 20:15:09 +01:00
/****************
* Print a string, but filter all control characters out.
*/
void
tty_print_string( byte *p, size_t n )
{
1998-01-02 21:40:10 +01:00
if( !ttyfp )
init_ttyfp();
1997-12-16 20:15:09 +01:00
for( ; n; n--, p++ )
if( iscntrl( *p ) ) {
1998-01-02 21:40:10 +01:00
putc('\\', ttyfp);
1997-12-16 20:15:09 +01:00
if( *p == '\n' )
1998-01-02 21:40:10 +01:00
putc('n', ttyfp);
1997-12-16 20:15:09 +01:00
else if( !*p )
1998-01-02 21:40:10 +01:00
putc('0', ttyfp);
1997-12-16 20:15:09 +01:00
else
1998-01-02 21:40:10 +01:00
fprintf(ttyfp, "x%02x", *p );
1997-12-16 20:15:09 +01:00
}
else
1998-01-02 21:40:10 +01:00
putc(*p, ttyfp);
1997-12-16 20:15:09 +01:00
}
1997-12-31 13:32:54 +01:00
static char *
do_get( const char *prompt, int hidden )
1997-11-18 15:06:00 +01:00
{
char *buf;
1998-01-02 21:40:10 +01:00
byte cbuf[1];
1997-11-18 15:06:00 +01:00
int c, n, i;
1998-01-05 20:13:15 +01:00
#ifdef HAVE_TCGETATTR
1997-12-31 13:32:54 +01:00
struct termios termsave;
1998-01-05 20:13:15 +01:00
#endif
1997-11-18 15:06:00 +01:00
1998-01-02 21:40:10 +01:00
if( !ttyfp )
init_ttyfp();
1997-11-18 15:06:00 +01:00
last_prompt_len = 0;
tty_printf( prompt );
buf = m_alloc(n=50);
i = 0;
1998-01-02 21:40:10 +01:00
if( hidden ) {
1998-01-05 20:13:15 +01:00
#ifdef HAVE_TCGETATTR
1998-01-02 21:40:10 +01:00
struct termios term;
if( tcgetattr(fileno(ttyfp), &termsave) )
log_fatal("tcgetattr() failed: %s\n", strerror(errno) );
term = termsave;
term.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
if( tcsetattr( fileno(ttyfp), TCSAFLUSH, &term ) )
log_fatal("tcsetattr() failed: %s\n", strerror(errno) );
1998-01-05 20:13:15 +01:00
#endif
1998-01-02 21:40:10 +01:00
}
/* fixme: How can we avoid that the \n is echoed w/o disabling
* canonical mode - w/o this kill_prompt can't work */
while( read(fileno(ttyfp), cbuf, 1) == 1 && *cbuf != '\n' ) {
if( !hidden )
last_prompt_len++;
c = *cbuf;
1997-11-18 15:06:00 +01:00
if( c == '\t' )
c = ' ';
else if( iscntrl(c) )
continue;
if( !(i < n-1) ) {
n += 50;
buf = m_realloc( buf, n );
}
buf[i++] = c;
}
1998-01-02 21:40:10 +01:00
1998-01-05 20:13:15 +01:00
1998-01-02 21:40:10 +01:00
if( hidden ) {
1998-01-05 20:13:15 +01:00
#ifdef HAVE_TCGETATTR
1998-01-02 21:40:10 +01:00
if( tcsetattr(fileno(ttyfp), TCSAFLUSH, &termsave) )
log_error("tcsetattr() failed: %s\n", strerror(errno) );
1998-01-05 20:13:15 +01:00
#endif
1998-01-02 21:40:10 +01:00
}
1997-11-18 15:06:00 +01:00
buf[i] = 0;
return buf;
}
1997-12-31 13:32:54 +01:00
char *
tty_get( const char *prompt )
{
return do_get( prompt, 0 );
}
1997-11-18 15:06:00 +01:00
char *
tty_get_hidden( const char *prompt )
{
1997-12-31 13:32:54 +01:00
return do_get( prompt, 1 );
1997-11-18 15:06:00 +01:00
}
void
tty_kill_prompt()
{
int i;
1998-01-02 21:40:10 +01:00
if( !ttyfp )
init_ttyfp();
if( !last_prompt_len )
return;
fputc('\r', ttyfp);
1997-11-18 15:06:00 +01:00
for(i=0; i < last_prompt_len; i ++ )
1998-01-02 21:40:10 +01:00
fputc(' ', ttyfp);
fputc('\r', ttyfp);
1997-11-18 15:06:00 +01:00
last_prompt_len = 0;
1998-01-02 21:40:10 +01:00
fflush(ttyfp);
1997-11-18 15:06:00 +01:00
}