1
0
mirror of git://git.gnupg.org/gnupg.git synced 2024-12-22 10:19:57 +01:00

common: Re-indent ttyio.c and remove EMX, RISCOS, and CE support

* common/ttyio.c: Remove cruft like EMX and RISCOS support.  Translate
a few strings.  Re-indent.
--
This commit is contained in:
Werner Koch 2021-03-05 09:19:43 +01:00
parent 7262d602d8
commit 8622f53994
No known key found for this signature in database
GPG Key ID: E3FDFF218E45B72B
2 changed files with 186 additions and 246 deletions

View File

@ -1,6 +1,7 @@
/* ttyio.c - tty i/O functions /* ttyio.c - tty i/O functions
* Copyright (C) 1998,1999,2000,2001,2002,2003,2004,2006,2007, * Copyright (C) 1997-2019 Werner Koch
* 2009, 2010 Free Software Foundation, Inc. * Copyright (C) 1998-2020 Free Software Foundation, Inc.
* Copyright (C) 2015-2020 g10 Code GmbH
* *
* This file is part of GnuPG. * This file is part of GnuPG.
* *
@ -26,6 +27,7 @@
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program; if not, see <https://www.gnu.org/licenses/>. * along with this program; if not, see <https://www.gnu.org/licenses/>.
* SPDX-License-Identifier: (LGPL-3.0-or-later OR GPL-2.0-or-later)
*/ */
#include <config.h> #include <config.h>
@ -35,24 +37,20 @@
#include <stdarg.h> #include <stdarg.h>
#include <unistd.h> #include <unistd.h>
#if defined(HAVE_W32_SYSTEM) && !defined(HAVE_W32CE_SYSTEM)
# define USE_W32_CONSOLE 1
#endif
#ifdef HAVE_TCGETATTR #ifdef HAVE_TCGETATTR
#include <termios.h> # include <termios.h>
#else #else
#ifdef HAVE_TERMIO_H # ifdef HAVE_TERMIO_H
/* simulate termios with termio */ /* simulate termios with termio */
#include <termio.h> # include <termio.h>
#define termios termio # define termios termio
#define tcsetattr ioctl # define tcsetattr ioctl
#define TCSAFLUSH TCSETAF # define TCSAFLUSH TCSETAF
#define tcgetattr(A,B) ioctl(A,TCGETA,B) # define tcgetattr(A,B) ioctl(A,TCGETA,B)
#define HAVE_TCGETATTR # define HAVE_TCGETATTR
# endif
#endif #endif
#endif #ifdef HAVE_W32_SYSTEM
#ifdef USE_W32_CONSOLE
# ifdef HAVE_WINSOCK2_H # ifdef HAVE_WINSOCK2_H
# include <winsock2.h> # include <winsock2.h>
# endif # endif
@ -66,12 +64,13 @@
#include "util.h" #include "util.h"
#include "ttyio.h" #include "ttyio.h"
#include "i18n.h"
#include "common-defs.h" #include "common-defs.h"
#define CONTROL_D ('D' - 'A' + 1) #define CONTROL_D ('D' - 'A' + 1)
#ifdef USE_W32_CONSOLE #ifdef HAVE_W32_SYSTEM
static struct { static struct {
HANDLE in, out; HANDLE in, out;
} con; } con;
@ -80,9 +79,9 @@ static struct {
#define HID_INPMODE (ENABLE_LINE_INPUT|ENABLE_PROCESSED_INPUT ) #define HID_INPMODE (ENABLE_LINE_INPUT|ENABLE_PROCESSED_INPUT )
#define DEF_OUTMODE (ENABLE_WRAP_AT_EOL_OUTPUT|ENABLE_PROCESSED_OUTPUT) #define DEF_OUTMODE (ENABLE_WRAP_AT_EOL_OUTPUT|ENABLE_PROCESSED_OUTPUT)
#else /* yeah, we have a real OS */ #else /* Unix */
static FILE *ttyfp = NULL; static FILE *ttyfp = NULL;
#endif #endif /* Unix */
static int initialized; static int initialized;
static int last_prompt_len; static int last_prompt_len;
@ -145,65 +144,62 @@ tty_get_ttyname (void)
static void static void
cleanup(void) cleanup(void)
{ {
if( restore_termios ) { if (restore_termios)
restore_termios = 0; /* do it prios in case it is interrupted again */ {
if( tcsetattr(fileno(ttyfp), TCSAFLUSH, &termsave) ) restore_termios = 0; /* do it prior in case it is interrupted again */
log_error("tcsetattr() failed: %s\n", strerror(errno) ); if (tcsetattr(fileno(ttyfp), TCSAFLUSH, &termsave))
log_error ("tcsetattr() failed: %s\n", strerror (errno));
} }
} }
#endif #endif /*HAVE_TCGETATTR*/
static void static void
init_ttyfp(void) init_ttyfp(void)
{ {
if( initialized ) if (initialized)
return; return;
#if defined(USE_W32_CONSOLE) #ifdef HAVE_W32_SYSTEM
{ {
SECURITY_ATTRIBUTES sa; SECURITY_ATTRIBUTES sa;
memset(&sa, 0, sizeof(sa)); memset (&sa, 0, sizeof(sa));
sa.nLength = sizeof(sa); sa.nLength = sizeof(sa);
sa.bInheritHandle = TRUE; sa.bInheritHandle = TRUE;
con.out = CreateFileA( "CONOUT$", GENERIC_READ|GENERIC_WRITE, con.out = CreateFileA ("CONOUT$", GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,
&sa, OPEN_EXISTING, 0, 0 ); &sa, OPEN_EXISTING, 0, 0 );
if( con.out == INVALID_HANDLE_VALUE ) if (con.out == INVALID_HANDLE_VALUE)
log_fatal("open(CONOUT$) failed: rc=%d", (int)GetLastError() ); log_fatal ("open(CONOUT$) failed: %s\n", w32_strerror (-1));
memset(&sa, 0, sizeof(sa));
sa.nLength = sizeof(sa);
sa.bInheritHandle = TRUE;
con.in = CreateFileA( "CONIN$", GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE,
&sa, OPEN_EXISTING, 0, 0 );
if( con.in == INVALID_HANDLE_VALUE )
log_fatal("open(CONIN$) failed: rc=%d", (int)GetLastError() );
}
SetConsoleMode(con.in, DEF_INPMODE );
SetConsoleMode(con.out, DEF_OUTMODE );
#elif defined(__EMX__) memset (&sa, 0, sizeof(sa));
ttyfp = stdout; /* Fixme: replace by the real functions: see wklib */ sa.nLength = sizeof(sa);
if (my_rl_init_stream) sa.bInheritHandle = TRUE;
my_rl_init_stream (ttyfp); con.in = CreateFileA ("CONIN$", GENERIC_READ|GENERIC_WRITE,
#elif defined (HAVE_W32CE_SYSTEM) FILE_SHARE_READ|FILE_SHARE_WRITE,
ttyfp = stderr; &sa, OPEN_EXISTING, 0, 0 );
#else if (con.in == INVALID_HANDLE_VALUE)
log_fatal ("open(CONIN$) failed: %s\n", w32_strerror (-1));
}
SetConsoleMode (con.in, DEF_INPMODE);
SetConsoleMode (con.out, DEF_OUTMODE);
#else /* Unix */
ttyfp = batchmode? stderr : fopen (tty_get_ttyname (), "r+"); ttyfp = batchmode? stderr : fopen (tty_get_ttyname (), "r+");
if( !ttyfp ) { if (!ttyfp)
log_error("cannot open '%s': %s\n", tty_get_ttyname (), {
strerror(errno) ); log_error ("cannot open '%s': %s\n", tty_get_ttyname (), strerror(errno));
exit(2); exit (2);
} }
if (my_rl_init_stream) if (my_rl_init_stream)
my_rl_init_stream (ttyfp); my_rl_init_stream (ttyfp);
#endif #endif /* Unix */
#ifdef HAVE_TCGETATTR #ifdef HAVE_TCGETATTR
atexit( cleanup ); atexit (cleanup);
#endif #endif
initialized = 1; initialized = 1;
} }
@ -212,7 +208,7 @@ int
tty_batchmode( int onoff ) tty_batchmode( int onoff )
{ {
int old = batchmode; int old = batchmode;
if( onoff != -1 ) if (onoff != -1)
batchmode = onoff; batchmode = onoff;
return old; return old;
} }
@ -226,38 +222,39 @@ tty_no_terminal(int onoff)
} }
void void
tty_printf( const char *fmt, ... ) tty_printf (const char *fmt, ... )
{ {
va_list arg_ptr; va_list arg_ptr;
if (no_terminal) if (no_terminal)
return; return;
if( !initialized ) if (!initialized)
init_ttyfp(); init_ttyfp ();
va_start( arg_ptr, fmt ) ; va_start (arg_ptr, fmt);
#ifdef USE_W32_CONSOLE
#ifdef HAVE_W32_SYSTEM
{ {
char *buf = NULL; char *buf = NULL;
int n; int n;
DWORD nwritten; DWORD nwritten;
n = vasprintf(&buf, fmt, arg_ptr); n = vasprintf(&buf, fmt, arg_ptr);
if( !buf ) if (!buf)
log_bug("vasprintf() failed\n"); log_bug ("vasprintf() failed\n");
if( !WriteConsoleA( con.out, buf, n, &nwritten, NULL ) ) if (!WriteConsoleA (con.out, buf, n, &nwritten, NULL))
log_fatal("WriteConsole failed: rc=%d", (int)GetLastError() ); log_fatal ("WriteConsole failed: rc=%d", (int)GetLastError());
if( n != nwritten ) if (n != nwritten)
log_fatal("WriteConsole failed: %d != %d\n", n, (int)nwritten ); log_fatal ("WriteConsole failed: %d != %d\n", n, (int)nwritten);
last_prompt_len += n; last_prompt_len += n;
xfree (buf); xfree (buf);
} }
#else #else /* Unix */
last_prompt_len += vfprintf(ttyfp,fmt,arg_ptr) ; last_prompt_len += vfprintf (ttyfp, fmt, arg_ptr) ;
fflush(ttyfp); fflush (ttyfp);
#endif #endif /* Unix */
va_end(arg_ptr); va_end(arg_ptr);
} }
@ -284,27 +281,29 @@ tty_fprintf (estream_t fp, const char *fmt, ... )
init_ttyfp (); init_ttyfp ();
va_start (arg_ptr, fmt); va_start (arg_ptr, fmt);
#ifdef USE_W32_CONSOLE
#ifdef HAVE_W32_SYSTEM
{ {
char *buf = NULL; char *buf = NULL;
int n; int n;
DWORD nwritten; DWORD nwritten;
n = vasprintf(&buf, fmt, arg_ptr); n = vasprintf (&buf, fmt, arg_ptr);
if (!buf) if (!buf)
log_bug("vasprintf() failed\n"); log_bug ("vasprintf() failed\n");
if (!WriteConsoleA( con.out, buf, n, &nwritten, NULL )) if (!WriteConsoleA (con.out, buf, n, &nwritten, NULL))
log_fatal("WriteConsole failed: rc=%d", (int)GetLastError() ); log_fatal ("WriteConsole failed: rc=%d", (int)GetLastError());
if (n != nwritten) if (n != nwritten)
log_fatal("WriteConsole failed: %d != %d\n", n, (int)nwritten ); log_fatal ("WriteConsole failed: %d != %d\n", n, (int)nwritten);
last_prompt_len += n; last_prompt_len += n;
xfree (buf); xfree (buf);
} }
#else #else /* Unix */
last_prompt_len += vfprintf(ttyfp,fmt,arg_ptr) ; last_prompt_len += vfprintf(ttyfp,fmt,arg_ptr) ;
fflush(ttyfp); fflush(ttyfp);
#endif #endif /* Unix */
va_end(arg_ptr); va_end(arg_ptr);
} }
@ -326,7 +325,7 @@ do_print_string (estream_t fp, const byte *p, size_t n )
return; return;
} }
#ifdef USE_W32_CONSOLE #ifdef HAVE_W32_SYSTEM
/* Not so effective, change it if you want */ /* Not so effective, change it if you want */
for (; n; n--, p++) for (; n; n--, p++)
{ {
@ -342,7 +341,7 @@ do_print_string (estream_t fp, const byte *p, size_t n )
else else
tty_printf ("%c", *p); tty_printf ("%c", *p);
} }
#else #else /* Unix */
for (; n; n--, p++) for (; n; n--, p++)
{ {
if (iscntrl (*p)) if (iscntrl (*p))
@ -358,7 +357,7 @@ do_print_string (estream_t fp, const byte *p, size_t n )
else else
putc (*p, ttyfp); putc (*p, ttyfp);
} }
#endif #endif /* Unix */
} }
@ -371,54 +370,57 @@ tty_print_utf8_string2 (estream_t fp, const byte *p, size_t n, size_t max_n)
if (no_terminal && !fp) if (no_terminal && !fp)
return; return;
/* we can handle plain ascii simpler, so check for it first */ /* We can handle plain ascii simpler, so check for it first. */
for(i=0; i < n; i++ ) { for(i=0; i < n; i++ )
if( p[i] & 0x80 ) {
if (p[i] & 0x80)
break; break;
} }
if( i < n ) { if (i < n)
buf = utf8_to_native( (const char *)p, n, 0 ); {
if( max_n && (strlen( buf ) > max_n )) { buf = utf8_to_native ((const char *)p, n, 0);
if (max_n && (strlen (buf) > max_n))
buf[max_n] = 0; buf[max_n] = 0;
} /* (utf8_to_native already did the control character quoting) */
/*(utf8 conversion already does the control character quoting)*/
tty_fprintf (fp, "%s", buf); tty_fprintf (fp, "%s", buf);
xfree (buf); xfree (buf);
} }
else { else
if( max_n && (n > max_n) ) { {
if (max_n && (n > max_n))
n = max_n; n = max_n;
}
do_print_string (fp, p, n ); do_print_string (fp, p, n );
} }
} }
void void
tty_print_utf8_string( const byte *p, size_t n ) tty_print_utf8_string (const byte *p, size_t n)
{ {
tty_print_utf8_string2 (NULL, p, n, 0); tty_print_utf8_string2 (NULL, p, n, 0);
} }
/* Read a string from the tty using PROMPT. If HIDDEN is set the
* input is not echoed. */
static char * static char *
do_get( const char *prompt, int hidden ) do_get (const char *prompt, int hidden)
{ {
char *buf; char *buf;
#ifndef __riscos__ int n; /* Allocated size of BUF. */
int i; /* Number of bytes in BUF. */
int c;
byte cbuf[1]; byte cbuf[1];
#endif
int c, n, i;
if (batchmode) if (batchmode)
{ {
log_error ("Sorry, we are in batchmode - can't get input\n"); log_error (_("Sorry, we are in batchmode - can't get input\n"));
exit (2); exit (2);
} }
if (no_terminal) if (no_terminal)
{ {
log_error ("Sorry, no terminal at all requested - can't get input\n"); log_error (_("Sorry, no terminal at all requested - can't get input\n"));
exit (2); exit (2);
} }
@ -430,7 +432,7 @@ do_get( const char *prompt, int hidden )
buf = xmalloc ((n=50)); buf = xmalloc ((n=50));
i = 0; i = 0;
#ifdef USE_W32_CONSOLE #ifdef HAVE_W32_SYSTEM
if (hidden) if (hidden)
SetConsoleMode(con.in, HID_INPMODE ); SetConsoleMode(con.in, HID_INPMODE );
@ -463,73 +465,7 @@ do_get( const char *prompt, int hidden )
if (hidden) if (hidden)
SetConsoleMode(con.in, DEF_INPMODE ); SetConsoleMode(con.in, DEF_INPMODE );
#elif defined(__riscos__) || defined(HAVE_W32CE_SYSTEM) #else /* Unix */
do
{
#ifdef HAVE_W32CE_SYSTEM
/* Using getchar is not a correct solution but for now it
doesn't matter because we have no real console at all. We
should rework this as soon as we have switched this entire
module to estream. */
c = getchar();
#else
c = riscos_getchar();
#endif
if (c == 0xa || c == 0xd) /* Return || Enter */
{
c = (int) '\n';
}
else if (c == 0x8 || c == 0x7f) /* Backspace || Delete */
{
if (i>0)
{
i--;
if (!hidden)
{
last_prompt_len--;
fputc(8, ttyfp);
fputc(32, ttyfp);
fputc(8, ttyfp);
fflush(ttyfp);
}
}
else
{
fputc(7, ttyfp);
fflush(ttyfp);
}
continue;
}
else if (c == (int) '\t') /* Tab */
{
c = ' ';
}
else if (c > 0xa0)
{
; /* we don't allow 0xa0, as this is a protected blank which may
* confuse the user */
}
else if (iscntrl(c))
{
continue;
}
if (!(i < n-1))
{
n += 50;
buf = xrealloc (buf, n);
}
buf[i++] = c;
if (!hidden)
{
last_prompt_len++;
fputc(c, ttyfp);
fflush(ttyfp);
}
}
while (c != '\n');
i = (i>0) ? i-1 : 0;
#else /* Other systems. */
if (hidden) if (hidden)
{ {
@ -537,13 +473,13 @@ do_get( const char *prompt, int hidden )
struct termios term; struct termios term;
if (tcgetattr(fileno(ttyfp), &termsave)) if (tcgetattr(fileno(ttyfp), &termsave))
log_fatal("tcgetattr() failed: %s\n", strerror(errno)); log_fatal ("tcgetattr() failed: %s\n", strerror(errno));
restore_termios = 1; restore_termios = 1;
term = termsave; term = termsave;
term.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL); term.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
if (tcsetattr( fileno(ttyfp), TCSAFLUSH, &term ) ) if (tcsetattr( fileno(ttyfp), TCSAFLUSH, &term ) )
log_fatal("tcsetattr() failed: %s\n", strerror(errno)); log_fatal("tcsetattr() failed: %s\n", strerror(errno));
#endif #endif /*HAVE_TCGETATTR*/
} }
/* fixme: How can we avoid that the \n is echoed w/o disabling /* fixme: How can we avoid that the \n is echoed w/o disabling
@ -554,7 +490,7 @@ do_get( const char *prompt, int hidden )
last_prompt_len++; last_prompt_len++;
c = *cbuf; c = *cbuf;
if (c == CONTROL_D) if (c == CONTROL_D)
log_info ("Control-D detected\n"); log_info (_("Control-D detected\n"));
if (c == '\t') /* Map tab to a space. */ if (c == '\t') /* Map tab to a space. */
c = ' '; c = ' ';
@ -576,12 +512,12 @@ do_get( const char *prompt, int hidden )
if (hidden) if (hidden)
{ {
#ifdef HAVE_TCGETATTR #ifdef HAVE_TCGETATTR
if (tcsetattr(fileno(ttyfp), TCSAFLUSH, &termsave)) if (tcsetattr (fileno(ttyfp), TCSAFLUSH, &termsave))
log_error ("tcsetattr() failed: %s\n", strerror(errno)); log_error ("tcsetattr() failed: %s\n", strerror(errno));
restore_termios = 0; restore_termios = 0;
#endif #endif /*HAVE_TCGETATTR*/
} }
#endif /* end unix version */ #endif /* Unix */
buf[i] = 0; buf[i] = 0;
return buf; return buf;
@ -628,8 +564,9 @@ tty_get( const char *prompt )
return do_get ( prompt, 0 ); return do_get ( prompt, 0 );
} }
/* Variable argument version of tty_get. The prompt is actually a /* Variable argument version of tty_get. The prompt is actually a
format string with arguments. */ * format string with arguments. */
char * char *
tty_getf (const char *promptfmt, ... ) tty_getf (const char *promptfmt, ... )
{ {
@ -647,39 +584,38 @@ tty_getf (const char *promptfmt, ... )
} }
char * char *
tty_get_hidden( const char *prompt ) tty_get_hidden( const char *prompt )
{ {
return do_get( prompt, 1 ); return do_get (prompt, 1);
} }
void void
tty_kill_prompt() tty_kill_prompt (void)
{ {
if ( no_terminal ) if (no_terminal)
return; return;
if( !initialized ) if (!initialized)
init_ttyfp(); init_ttyfp ();
if( batchmode ) if (batchmode)
last_prompt_len = 0; last_prompt_len = 0;
if( !last_prompt_len ) if (!last_prompt_len)
return; return;
#ifdef USE_W32_CONSOLE #ifdef HAVE_W32_SYSTEM
tty_printf("\r%*s\r", last_prompt_len, ""); tty_printf ("\r%*s\r", last_prompt_len, "");
#else #else /* Unix */
{ {
int i; int i;
putc('\r', ttyfp); putc ('\r', ttyfp);
for(i=0; i < last_prompt_len; i ++ ) for (i=0; i < last_prompt_len; i ++ )
putc(' ', ttyfp); putc (' ', ttyfp);
putc('\r', ttyfp); putc ('\r', ttyfp);
fflush(ttyfp); fflush (ttyfp);
} }
#endif #endif /* Unix */
last_prompt_len = 0; last_prompt_len = 0;
} }
@ -688,10 +624,13 @@ int
tty_get_answer_is_yes( const char *prompt ) tty_get_answer_is_yes( const char *prompt )
{ {
int yes; int yes;
char *p = tty_get( prompt ); char *p;
tty_kill_prompt();
yes = answer_is_yes(p); p = tty_get (prompt);
xfree(p); tty_kill_prompt ();
yes = answer_is_yes (p);
xfree (p);
return yes; return yes;
} }
@ -773,7 +712,7 @@ tty_disable_completion (void)
my_rl_inhibit_completion (1); my_rl_inhibit_completion (1);
} }
#endif #endif /* HAVE_LIBREADLINE */
void void
tty_cleanup_after_signal (void) tty_cleanup_after_signal (void)

View File

@ -25,6 +25,7 @@ common/audit.c
common/helpfile.c common/helpfile.c
common/gettime.c common/gettime.c
common/ksba-io-support.c common/ksba-io-support.c
common/ttyio.c
common/utf8conv.c common/utf8conv.c
common/dotlock.c common/dotlock.c