2003-08-05 19:11:04 +02:00
|
|
|
/* ttyio.c - tty i/O functions
|
2021-03-05 09:19:43 +01:00
|
|
|
* Copyright (C) 1997-2019 Werner Koch
|
|
|
|
* Copyright (C) 1998-2020 Free Software Foundation, Inc.
|
|
|
|
* Copyright (C) 2015-2020 g10 Code GmbH
|
2003-08-05 19:11:04 +02:00
|
|
|
*
|
|
|
|
* This file is part of GnuPG.
|
|
|
|
*
|
Change license for some files in common to LGPLv3+/GPLv2+.
Having the LGPL on the common GnuPG code helps to share code
between GnuPG and related projects (like GPGME and Libassuan). This
is good for interoperability and to reduces bugs.
* common/asshelp.c, common/asshelp.h, common/asshelp2.c, common/b64dec.c
* common/b64enc.c, common/convert.c, common/dns-cert.c
* common/dns-cert.h common/exechelp-posix.c, common/exechelp-w32.c
* common/exechelp-w32ce.c, common/exechelp.h, common/get-passphrase.c
* common/get-passphrase.h, common/gettime.c, common/gpgrlhelp.c
* common/helpfile.c, common/homedir.c, common/http.c, common/http.h
* common/i18n.c, common/init.c, common/init.h, common/iobuf.c
* common/iobuf.h, common/localename.c, common/membuf.c, common/membuf.h
* common/miscellaneous.c, common/openpgp-oid.c, common/openpgpdefs.h
* common/percent.c, common/pka.c, common/pka.h, common/session-env.c
* common/session-env.h, common/sexp-parse.h, common/sexputil.c
* common/signal.c, common/srv.c, common/srv.h, common/ssh-utils.c
* common/ssh-utils.h, common/sysutils.c, common/sysutils.h
* common/tlv.c, common/tlv.h, common/ttyio.c, common/ttyio.h
* common/userids.c, common/userids.h, common/xasprintf.c: Change
license to LGPLv3+/GPLv2+/
2012-04-20 15:43:06 +02:00
|
|
|
* This file is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of either
|
2003-08-05 19:11:04 +02:00
|
|
|
*
|
Change license for some files in common to LGPLv3+/GPLv2+.
Having the LGPL on the common GnuPG code helps to share code
between GnuPG and related projects (like GPGME and Libassuan). This
is good for interoperability and to reduces bugs.
* common/asshelp.c, common/asshelp.h, common/asshelp2.c, common/b64dec.c
* common/b64enc.c, common/convert.c, common/dns-cert.c
* common/dns-cert.h common/exechelp-posix.c, common/exechelp-w32.c
* common/exechelp-w32ce.c, common/exechelp.h, common/get-passphrase.c
* common/get-passphrase.h, common/gettime.c, common/gpgrlhelp.c
* common/helpfile.c, common/homedir.c, common/http.c, common/http.h
* common/i18n.c, common/init.c, common/init.h, common/iobuf.c
* common/iobuf.h, common/localename.c, common/membuf.c, common/membuf.h
* common/miscellaneous.c, common/openpgp-oid.c, common/openpgpdefs.h
* common/percent.c, common/pka.c, common/pka.h, common/session-env.c
* common/session-env.h, common/sexp-parse.h, common/sexputil.c
* common/signal.c, common/srv.c, common/srv.h, common/ssh-utils.c
* common/ssh-utils.h, common/sysutils.c, common/sysutils.h
* common/tlv.c, common/tlv.h, common/ttyio.c, common/ttyio.h
* common/userids.c, common/userids.h, common/xasprintf.c: Change
license to LGPLv3+/GPLv2+/
2012-04-20 15:43:06 +02:00
|
|
|
* - the GNU Lesser General Public License as published by the Free
|
|
|
|
* Software Foundation; either version 3 of the License, or (at
|
|
|
|
* your option) any later version.
|
|
|
|
*
|
|
|
|
* or
|
|
|
|
*
|
|
|
|
* - 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.
|
|
|
|
*
|
|
|
|
* or both in parallel, as here.
|
|
|
|
*
|
|
|
|
* This file is distributed in the hope that it will be useful,
|
2003-08-05 19:11:04 +02:00
|
|
|
* 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
|
2016-11-05 12:02:19 +01:00
|
|
|
* along with this program; if not, see <https://www.gnu.org/licenses/>.
|
2021-03-05 09:19:43 +01:00
|
|
|
* SPDX-License-Identifier: (LGPL-3.0-or-later OR GPL-2.0-or-later)
|
2003-08-05 19:11:04 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <unistd.h>
|
2010-03-24 13:15:30 +01:00
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
#ifdef HAVE_TCGETATTR
|
2021-03-05 09:19:43 +01:00
|
|
|
# include <termios.h>
|
2003-08-05 19:11:04 +02:00
|
|
|
#else
|
2021-03-05 09:19:43 +01:00
|
|
|
# ifdef HAVE_TERMIO_H
|
2003-08-05 19:11:04 +02:00
|
|
|
/* simulate termios with termio */
|
2021-03-05 09:19:43 +01:00
|
|
|
# include <termio.h>
|
|
|
|
# define termios termio
|
|
|
|
# define tcsetattr ioctl
|
|
|
|
# define TCSAFLUSH TCSETAF
|
|
|
|
# define tcgetattr(A,B) ioctl(A,TCGETA,B)
|
|
|
|
# define HAVE_TCGETATTR
|
|
|
|
# endif
|
2003-08-05 19:11:04 +02:00
|
|
|
#endif
|
2021-03-05 09:19:43 +01:00
|
|
|
#ifdef HAVE_W32_SYSTEM
|
2014-03-07 09:46:44 +01:00
|
|
|
# ifdef HAVE_WINSOCK2_H
|
|
|
|
# include <winsock2.h>
|
|
|
|
# endif
|
2010-03-24 13:15:30 +01:00
|
|
|
# include <windows.h>
|
|
|
|
# ifdef HAVE_TCGETATTR
|
|
|
|
# error mingw32 and termios
|
|
|
|
# endif
|
2003-08-05 19:11:04 +02:00
|
|
|
#endif
|
|
|
|
#include <errno.h>
|
|
|
|
#include <ctype.h>
|
2006-05-23 18:19:43 +02:00
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
#include "util.h"
|
|
|
|
#include "ttyio.h"
|
2021-03-05 09:19:43 +01:00
|
|
|
#include "i18n.h"
|
2006-09-21 15:30:45 +02:00
|
|
|
#include "common-defs.h"
|
2003-08-05 19:11:04 +02:00
|
|
|
|
|
|
|
#define CONTROL_D ('D' - 'A' + 1)
|
|
|
|
|
2010-03-24 13:15:30 +01:00
|
|
|
|
2021-03-05 09:19:43 +01:00
|
|
|
#ifdef HAVE_W32_SYSTEM
|
2003-08-05 19:11:04 +02:00
|
|
|
static struct {
|
|
|
|
HANDLE in, out;
|
|
|
|
} con;
|
|
|
|
#define DEF_INPMODE (ENABLE_LINE_INPUT|ENABLE_ECHO_INPUT \
|
2021-03-05 09:19:43 +01:00
|
|
|
|ENABLE_PROCESSED_INPUT )
|
2003-08-05 19:11:04 +02:00
|
|
|
#define HID_INPMODE (ENABLE_LINE_INPUT|ENABLE_PROCESSED_INPUT )
|
|
|
|
#define DEF_OUTMODE (ENABLE_WRAP_AT_EOL_OUTPUT|ENABLE_PROCESSED_OUTPUT)
|
|
|
|
|
2021-03-05 09:19:43 +01:00
|
|
|
#else /* Unix */
|
2003-08-05 19:11:04 +02:00
|
|
|
static FILE *ttyfp = NULL;
|
2021-03-05 09:19:43 +01:00
|
|
|
#endif /* Unix */
|
2003-08-05 19:11:04 +02:00
|
|
|
|
|
|
|
static int initialized;
|
|
|
|
static int last_prompt_len;
|
|
|
|
static int batchmode;
|
|
|
|
static int no_terminal;
|
|
|
|
|
|
|
|
#ifdef HAVE_TCGETATTR
|
|
|
|
static struct termios termsave;
|
|
|
|
static int restore_termios;
|
|
|
|
#endif
|
|
|
|
|
2006-09-21 15:30:45 +02:00
|
|
|
/* Hooks set by gpgrlhelp.c if required. */
|
|
|
|
static void (*my_rl_set_completer) (rl_completion_func_t *);
|
|
|
|
static void (*my_rl_inhibit_completion) (int);
|
|
|
|
static void (*my_rl_cleanup_after_signal) (void);
|
2006-09-22 13:39:45 +02:00
|
|
|
static void (*my_rl_init_stream) (FILE *);
|
|
|
|
static char *(*my_rl_readline) (const char*);
|
|
|
|
static void (*my_rl_add_history) (const char*);
|
2020-07-02 15:47:57 +02:00
|
|
|
static int (*my_rl_rw_history)(const char *, int, int);
|
2003-08-05 19:11:04 +02:00
|
|
|
|
|
|
|
/* This is a wrapper around ttyname so that we can use it even when
|
|
|
|
the standard streams are redirected. It figures the name out the
|
|
|
|
first time and returns it in a statically allocated buffer. */
|
|
|
|
const char *
|
|
|
|
tty_get_ttyname (void)
|
|
|
|
{
|
|
|
|
static char *name;
|
|
|
|
|
|
|
|
/* On a GNU system ctermid() always return /dev/tty, so this does
|
|
|
|
not make much sense - however if it is ever changed we do the
|
|
|
|
Right Thing now. */
|
|
|
|
#ifdef HAVE_CTERMID
|
|
|
|
static int got_name;
|
|
|
|
|
|
|
|
if (!got_name)
|
|
|
|
{
|
|
|
|
const char *s;
|
2006-05-23 18:19:43 +02:00
|
|
|
/* Note that despite our checks for these macros the function is
|
|
|
|
not necessarily thread save. We mainly do this for
|
|
|
|
portability reasons, in case L_ctermid is not defined. */
|
|
|
|
# if defined(_POSIX_THREAD_SAFE_FUNCTIONS) || defined(_POSIX_TRHEADS)
|
|
|
|
char buffer[L_ctermid];
|
|
|
|
s = ctermid (buffer);
|
|
|
|
# else
|
2003-08-05 19:11:04 +02:00
|
|
|
s = ctermid (NULL);
|
2006-05-23 18:19:43 +02:00
|
|
|
# endif
|
2003-08-05 19:11:04 +02:00
|
|
|
if (s)
|
|
|
|
name = strdup (s);
|
|
|
|
got_name = 1;
|
|
|
|
}
|
2006-05-23 18:19:43 +02:00
|
|
|
#endif /*HAVE_CTERMID*/
|
2015-10-27 22:09:43 +01:00
|
|
|
/* Assume the standard tty on memory error or when there is no
|
2010-03-24 13:15:30 +01:00
|
|
|
ctermid. */
|
2003-08-05 19:11:04 +02:00
|
|
|
return name? name : "/dev/tty";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_TCGETATTR
|
|
|
|
static void
|
|
|
|
cleanup(void)
|
|
|
|
{
|
2021-03-05 09:19:43 +01:00
|
|
|
if (restore_termios)
|
|
|
|
{
|
|
|
|
restore_termios = 0; /* do it prior in case it is interrupted again */
|
|
|
|
if (tcsetattr(fileno(ttyfp), TCSAFLUSH, &termsave))
|
|
|
|
log_error ("tcsetattr() failed: %s\n", strerror (errno));
|
2003-08-05 19:11:04 +02:00
|
|
|
}
|
|
|
|
}
|
2021-03-05 09:19:43 +01:00
|
|
|
#endif /*HAVE_TCGETATTR*/
|
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
|
|
|
|
static void
|
|
|
|
init_ttyfp(void)
|
|
|
|
{
|
2021-03-05 09:19:43 +01:00
|
|
|
if (initialized)
|
|
|
|
return;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
2021-03-05 09:19:43 +01:00
|
|
|
#ifdef HAVE_W32_SYSTEM
|
|
|
|
{
|
|
|
|
SECURITY_ATTRIBUTES sa;
|
|
|
|
|
|
|
|
memset (&sa, 0, sizeof(sa));
|
|
|
|
sa.nLength = sizeof(sa);
|
|
|
|
sa.bInheritHandle = TRUE;
|
|
|
|
con.out = CreateFileA ("CONOUT$", GENERIC_READ|GENERIC_WRITE,
|
|
|
|
FILE_SHARE_READ|FILE_SHARE_WRITE,
|
|
|
|
&sa, OPEN_EXISTING, 0, 0 );
|
|
|
|
if (con.out == INVALID_HANDLE_VALUE)
|
|
|
|
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: %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+");
|
|
|
|
if (!ttyfp)
|
2003-08-05 19:11:04 +02:00
|
|
|
{
|
2021-03-05 09:19:43 +01:00
|
|
|
log_error ("cannot open '%s': %s\n", tty_get_ttyname (), strerror(errno));
|
|
|
|
exit (2);
|
2003-08-05 19:11:04 +02:00
|
|
|
}
|
2021-03-05 09:19:43 +01:00
|
|
|
if (my_rl_init_stream)
|
|
|
|
my_rl_init_stream (ttyfp);
|
|
|
|
#endif /* Unix */
|
2006-09-22 13:39:45 +02:00
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
#ifdef HAVE_TCGETATTR
|
2021-03-05 09:19:43 +01:00
|
|
|
atexit (cleanup);
|
2003-08-05 19:11:04 +02:00
|
|
|
#endif
|
2021-03-05 09:19:43 +01:00
|
|
|
|
|
|
|
initialized = 1;
|
2003-08-05 19:11:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
tty_batchmode( int onoff )
|
|
|
|
{
|
2021-03-05 09:19:43 +01:00
|
|
|
int old = batchmode;
|
|
|
|
if (onoff != -1)
|
|
|
|
batchmode = onoff;
|
|
|
|
return old;
|
2003-08-05 19:11:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
tty_no_terminal(int onoff)
|
|
|
|
{
|
2021-03-05 09:19:43 +01:00
|
|
|
int old = no_terminal;
|
|
|
|
no_terminal = onoff ? 1 : 0;
|
|
|
|
return old;
|
2003-08-05 19:11:04 +02:00
|
|
|
}
|
|
|
|
|
2021-03-05 10:13:21 +01:00
|
|
|
|
|
|
|
#ifdef HAVE_W32_SYSTEM
|
|
|
|
/* Write the UTF-8 encoded STRING to the console. */
|
|
|
|
static void
|
|
|
|
w32_write_console (const char *string)
|
|
|
|
{
|
|
|
|
wchar_t *wstring;
|
|
|
|
DWORD n, nwritten;
|
|
|
|
|
|
|
|
wstring = utf8_to_wchar (string);
|
|
|
|
if (!wstring)
|
|
|
|
log_fatal ("w32_write_console failed: %s", strerror (errno));
|
|
|
|
n = wcslen (wstring);
|
|
|
|
|
|
|
|
if (!WriteConsoleW (con.out, wstring, n, &nwritten, NULL))
|
2021-06-22 11:08:05 +02:00
|
|
|
{
|
|
|
|
static int shown;
|
|
|
|
if (!shown)
|
|
|
|
{
|
|
|
|
shown = 1;
|
|
|
|
log_info ("WriteConsole failed: %s", w32_strerror (-1));
|
|
|
|
log_info ("Please configure a suitable font for the console\n");
|
|
|
|
}
|
|
|
|
n = strlen (string);
|
|
|
|
if (!WriteConsoleA (con.out, string, n , &nwritten, NULL))
|
|
|
|
log_fatal ("WriteConsole fallback failed: %s", w32_strerror (-1));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (n != nwritten)
|
|
|
|
log_fatal ("WriteConsole failed: %lu != %lu\n",
|
|
|
|
(unsigned long)n, (unsigned long)nwritten);
|
|
|
|
}
|
2021-03-05 10:13:21 +01:00
|
|
|
last_prompt_len += n;
|
2021-03-05 10:53:37 +01:00
|
|
|
xfree (wstring);
|
2021-03-05 10:13:21 +01:00
|
|
|
}
|
|
|
|
#endif /*HAVE_W32_SYSTEM*/
|
|
|
|
|
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
void
|
2021-03-05 09:19:43 +01:00
|
|
|
tty_printf (const char *fmt, ... )
|
2003-08-05 19:11:04 +02:00
|
|
|
{
|
2021-03-05 09:19:43 +01:00
|
|
|
va_list arg_ptr;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
2021-03-05 09:19:43 +01:00
|
|
|
if (no_terminal)
|
|
|
|
return;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
2021-03-05 09:19:43 +01:00
|
|
|
if (!initialized)
|
|
|
|
init_ttyfp ();
|
2003-08-05 19:11:04 +02:00
|
|
|
|
2021-03-05 09:19:43 +01:00
|
|
|
va_start (arg_ptr, fmt);
|
|
|
|
|
|
|
|
#ifdef HAVE_W32_SYSTEM
|
|
|
|
{
|
|
|
|
char *buf = NULL;
|
|
|
|
|
2021-03-05 10:13:21 +01:00
|
|
|
vasprintf(&buf, fmt, arg_ptr);
|
2021-03-05 09:19:43 +01:00
|
|
|
if (!buf)
|
|
|
|
log_bug ("vasprintf() failed\n");
|
2021-03-05 10:13:21 +01:00
|
|
|
w32_write_console (buf);
|
2021-03-05 09:19:43 +01:00
|
|
|
xfree (buf);
|
|
|
|
}
|
|
|
|
#else /* Unix */
|
|
|
|
last_prompt_len += vfprintf (ttyfp, fmt, arg_ptr) ;
|
|
|
|
fflush (ttyfp);
|
|
|
|
#endif /* Unix */
|
|
|
|
va_end(arg_ptr);
|
2003-08-05 19:11:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-06-21 20:44:48 +02:00
|
|
|
/* Same as tty_printf but if FP is not NULL, behave like a regular
|
2003-09-18 17:51:01 +02:00
|
|
|
fprintf. */
|
|
|
|
void
|
2010-03-15 12:15:45 +01:00
|
|
|
tty_fprintf (estream_t fp, const char *fmt, ... )
|
2003-09-18 17:51:01 +02:00
|
|
|
{
|
|
|
|
va_list arg_ptr;
|
|
|
|
|
|
|
|
if (fp)
|
|
|
|
{
|
|
|
|
va_start (arg_ptr, fmt) ;
|
2010-03-15 12:15:45 +01:00
|
|
|
es_vfprintf (fp, fmt, arg_ptr );
|
2003-09-18 17:51:01 +02:00
|
|
|
va_end (arg_ptr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (no_terminal)
|
|
|
|
return;
|
|
|
|
|
2010-03-15 12:15:45 +01:00
|
|
|
if (!initialized)
|
|
|
|
init_ttyfp ();
|
2003-09-18 17:51:01 +02:00
|
|
|
|
2010-03-15 12:15:45 +01:00
|
|
|
va_start (arg_ptr, fmt);
|
2021-03-05 09:19:43 +01:00
|
|
|
|
|
|
|
#ifdef HAVE_W32_SYSTEM
|
2011-02-04 12:57:53 +01:00
|
|
|
{
|
2010-03-15 12:15:45 +01:00
|
|
|
char *buf = NULL;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2021-03-05 10:13:21 +01:00
|
|
|
vasprintf (&buf, fmt, arg_ptr);
|
2010-03-15 12:15:45 +01:00
|
|
|
if (!buf)
|
2021-03-05 09:19:43 +01:00
|
|
|
log_bug ("vasprintf() failed\n");
|
2021-03-05 10:13:21 +01:00
|
|
|
w32_write_console (buf);
|
2010-03-15 12:15:45 +01:00
|
|
|
xfree (buf);
|
|
|
|
}
|
2021-03-05 09:19:43 +01:00
|
|
|
#else /* Unix */
|
2010-03-15 12:15:45 +01:00
|
|
|
last_prompt_len += vfprintf(ttyfp,fmt,arg_ptr) ;
|
|
|
|
fflush(ttyfp);
|
2021-03-05 09:19:43 +01:00
|
|
|
#endif /* Unix */
|
|
|
|
|
2010-03-15 12:15:45 +01:00
|
|
|
va_end(arg_ptr);
|
2003-09-18 17:51:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-01-17 10:19:06 +01:00
|
|
|
/* Print a string, but filter all control characters out. If FP is
|
|
|
|
* not NULL print to that stream instead to the tty. */
|
|
|
|
static void
|
|
|
|
do_print_string (estream_t fp, const byte *p, size_t n )
|
2003-08-05 19:11:04 +02:00
|
|
|
{
|
2017-01-17 10:19:06 +01:00
|
|
|
if (no_terminal && !fp)
|
|
|
|
return;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
2017-01-17 10:19:06 +01:00
|
|
|
if (!initialized && !fp)
|
|
|
|
init_ttyfp();
|
|
|
|
|
|
|
|
if (fp)
|
|
|
|
{
|
|
|
|
print_utf8_buffer (fp, p, n);
|
|
|
|
return;
|
|
|
|
}
|
2003-08-05 19:11:04 +02:00
|
|
|
|
2021-03-05 09:19:43 +01:00
|
|
|
#ifdef HAVE_W32_SYSTEM
|
2017-01-17 10:19:06 +01:00
|
|
|
/* Not so effective, change it if you want */
|
|
|
|
for (; n; n--, p++)
|
|
|
|
{
|
|
|
|
if (iscntrl (*p))
|
|
|
|
{
|
|
|
|
if( *p == '\n' )
|
|
|
|
tty_printf ("\\n");
|
|
|
|
else if( !*p )
|
|
|
|
tty_printf ("\\0");
|
|
|
|
else
|
|
|
|
tty_printf ("\\x%02x", *p);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
tty_printf ("%c", *p);
|
|
|
|
}
|
2021-03-05 09:19:43 +01:00
|
|
|
#else /* Unix */
|
2017-01-17 10:19:06 +01:00
|
|
|
for (; n; n--, p++)
|
|
|
|
{
|
|
|
|
if (iscntrl (*p))
|
|
|
|
{
|
|
|
|
putc ('\\', ttyfp);
|
|
|
|
if ( *p == '\n' )
|
|
|
|
putc ('n', ttyfp);
|
|
|
|
else if ( !*p )
|
|
|
|
putc ('0', ttyfp);
|
|
|
|
else
|
|
|
|
fprintf (ttyfp, "x%02x", *p );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
putc (*p, ttyfp);
|
|
|
|
}
|
2021-03-05 09:19:43 +01:00
|
|
|
#endif /* Unix */
|
2003-08-05 19:11:04 +02:00
|
|
|
}
|
|
|
|
|
2017-01-17 10:19:06 +01:00
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
void
|
2014-03-27 12:59:55 +01:00
|
|
|
tty_print_utf8_string2 (estream_t fp, const byte *p, size_t n, size_t max_n)
|
2003-08-05 19:11:04 +02:00
|
|
|
{
|
2021-03-05 09:19:43 +01:00
|
|
|
size_t i;
|
|
|
|
char *buf;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
2021-03-05 09:19:43 +01:00
|
|
|
if (no_terminal && !fp)
|
|
|
|
return;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
2021-03-05 09:19:43 +01:00
|
|
|
/* We can handle plain ascii simpler, so check for it first. */
|
|
|
|
for(i=0; i < n; i++ )
|
|
|
|
{
|
|
|
|
if (p[i] & 0x80)
|
|
|
|
break;
|
2003-08-05 19:11:04 +02:00
|
|
|
}
|
2021-03-05 09:19:43 +01:00
|
|
|
if (i < n)
|
|
|
|
{
|
|
|
|
buf = utf8_to_native ((const char *)p, n, 0);
|
|
|
|
if (max_n && (strlen (buf) > max_n))
|
|
|
|
buf[max_n] = 0;
|
|
|
|
/* (utf8_to_native already did the control character quoting) */
|
|
|
|
tty_fprintf (fp, "%s", buf);
|
|
|
|
xfree (buf);
|
2003-08-05 19:11:04 +02:00
|
|
|
}
|
2021-03-05 09:19:43 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (max_n && (n > max_n))
|
|
|
|
n = max_n;
|
|
|
|
do_print_string (fp, p, n );
|
2003-08-05 19:11:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-27 12:59:55 +01:00
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
void
|
2021-03-05 09:19:43 +01:00
|
|
|
tty_print_utf8_string (const byte *p, size_t n)
|
2003-08-05 19:11:04 +02:00
|
|
|
{
|
2014-03-27 12:59:55 +01:00
|
|
|
tty_print_utf8_string2 (NULL, p, n, 0);
|
2003-08-05 19:11:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-05 09:19:43 +01:00
|
|
|
/* Read a string from the tty using PROMPT. If HIDDEN is set the
|
|
|
|
* input is not echoed. */
|
2003-08-05 19:11:04 +02:00
|
|
|
static char *
|
2021-03-05 09:19:43 +01:00
|
|
|
do_get (const char *prompt, int hidden)
|
2003-08-05 19:11:04 +02:00
|
|
|
{
|
2019-03-07 14:11:46 +01:00
|
|
|
char *buf;
|
2021-03-05 09:19:43 +01:00
|
|
|
int n; /* Allocated size of BUF. */
|
|
|
|
int i; /* Number of bytes in BUF. */
|
|
|
|
int c;
|
2021-03-05 10:13:21 +01:00
|
|
|
#ifdef HAVE_W32_SYSTEM
|
|
|
|
char *utf8buf;
|
|
|
|
int errcount = 0;
|
|
|
|
#else
|
2019-03-07 14:11:46 +01:00
|
|
|
byte cbuf[1];
|
2021-03-05 10:13:21 +01:00
|
|
|
#endif
|
2003-08-05 19:11:04 +02:00
|
|
|
|
2019-03-07 14:11:46 +01:00
|
|
|
if (batchmode)
|
|
|
|
{
|
2021-03-05 09:19:43 +01:00
|
|
|
log_error (_("Sorry, we are in batchmode - can't get input\n"));
|
2019-03-07 14:11:46 +01:00
|
|
|
exit (2);
|
2003-08-05 19:11:04 +02:00
|
|
|
}
|
|
|
|
|
2019-03-07 14:11:46 +01:00
|
|
|
if (no_terminal)
|
|
|
|
{
|
2021-03-05 09:19:43 +01:00
|
|
|
log_error (_("Sorry, no terminal at all requested - can't get input\n"));
|
2019-03-07 14:11:46 +01:00
|
|
|
exit (2);
|
2003-08-05 19:11:04 +02:00
|
|
|
}
|
|
|
|
|
2019-03-07 14:11:46 +01:00
|
|
|
if (!initialized)
|
|
|
|
init_ttyfp ();
|
2003-08-05 19:11:04 +02:00
|
|
|
|
2019-03-07 14:11:46 +01:00
|
|
|
last_prompt_len = 0;
|
|
|
|
tty_printf ("%s", prompt);
|
|
|
|
buf = xmalloc ((n=50));
|
|
|
|
i = 0;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
2021-03-05 09:19:43 +01:00
|
|
|
#ifdef HAVE_W32_SYSTEM
|
2019-03-07 14:11:46 +01:00
|
|
|
if (hidden)
|
|
|
|
SetConsoleMode(con.in, HID_INPMODE );
|
2003-08-05 19:11:04 +02:00
|
|
|
|
2021-03-05 10:13:21 +01:00
|
|
|
utf8buf = NULL;
|
2019-03-07 14:11:46 +01:00
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
DWORD nread;
|
2021-03-05 10:13:21 +01:00
|
|
|
wchar_t wbuf[2];
|
|
|
|
const unsigned char *s;
|
2019-03-07 14:11:46 +01:00
|
|
|
|
2021-03-05 10:13:21 +01:00
|
|
|
if (!ReadConsoleW (con.in, wbuf, 1, &nread, NULL))
|
|
|
|
log_fatal ("ReadConsole failed: %s", w32_strerror (-1));
|
2019-03-07 14:11:46 +01:00
|
|
|
if (!nread)
|
|
|
|
continue;
|
|
|
|
|
2021-03-05 10:13:21 +01:00
|
|
|
wbuf[1] = 0;
|
|
|
|
xfree (utf8buf);
|
|
|
|
utf8buf = wchar_to_utf8 (wbuf);
|
|
|
|
if (!utf8buf)
|
|
|
|
{
|
|
|
|
log_info ("wchar_to_utf8 failed: %s\n", strerror (errno));
|
|
|
|
if (++errcount > 10)
|
|
|
|
log_fatal (_("too many errors; giving up\n"));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (*utf8buf == '\n')
|
|
|
|
{
|
|
|
|
if (utf8buf[1])
|
|
|
|
{
|
|
|
|
log_info ("ReadConsole returned more than requested"
|
|
|
|
" (0x0a,0x%02x)\n", utf8buf[1]);
|
|
|
|
if (++errcount > 10)
|
|
|
|
log_fatal (_("too many errors; giving up\n"));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2019-03-07 14:11:46 +01:00
|
|
|
if (!hidden)
|
|
|
|
last_prompt_len++;
|
2021-03-05 10:13:21 +01:00
|
|
|
|
|
|
|
for (s=utf8buf; *s; s++)
|
2019-03-07 14:11:46 +01:00
|
|
|
{
|
2021-03-05 10:13:21 +01:00
|
|
|
c = *s;
|
|
|
|
if (c == '\t')
|
|
|
|
c = ' '; /* Map tab to a space. */
|
|
|
|
else if ((c >= 0 && c <= 0x1f) || c == 0x7f)
|
|
|
|
continue; /* Remove control characters. */
|
|
|
|
if (!(i < n-1))
|
|
|
|
{
|
|
|
|
n += 50;
|
|
|
|
buf = xrealloc (buf, n);
|
|
|
|
}
|
|
|
|
buf[i++] = c;
|
2019-03-07 14:11:46 +01:00
|
|
|
}
|
2003-08-05 19:11:04 +02:00
|
|
|
}
|
2021-03-05 10:13:21 +01:00
|
|
|
xfree (utf8buf);
|
2003-08-05 19:11:04 +02:00
|
|
|
|
2019-03-07 14:11:46 +01:00
|
|
|
if (hidden)
|
|
|
|
SetConsoleMode(con.in, DEF_INPMODE );
|
2003-08-05 19:11:04 +02:00
|
|
|
|
2021-03-05 09:19:43 +01:00
|
|
|
#else /* Unix */
|
2019-03-07 14:11:46 +01:00
|
|
|
|
|
|
|
if (hidden)
|
|
|
|
{
|
2003-08-05 19:11:04 +02:00
|
|
|
#ifdef HAVE_TCGETATTR
|
2019-03-07 14:11:46 +01:00
|
|
|
struct termios term;
|
|
|
|
|
|
|
|
if (tcgetattr(fileno(ttyfp), &termsave))
|
2021-03-05 09:19:43 +01:00
|
|
|
log_fatal ("tcgetattr() failed: %s\n", strerror(errno));
|
2019-03-07 14:11:46 +01:00
|
|
|
restore_termios = 1;
|
|
|
|
term = termsave;
|
|
|
|
term.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
|
|
|
|
if (tcsetattr( fileno(ttyfp), TCSAFLUSH, &term ) )
|
|
|
|
log_fatal("tcsetattr() failed: %s\n", strerror(errno));
|
2021-03-05 09:19:43 +01:00
|
|
|
#endif /*HAVE_TCGETATTR*/
|
2003-08-05 19:11:04 +02:00
|
|
|
}
|
|
|
|
|
2019-03-07 14:11:46 +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;
|
|
|
|
if (c == CONTROL_D)
|
2021-03-05 09:19:43 +01:00
|
|
|
log_info (_("Control-D detected\n"));
|
2019-03-07 14:11:46 +01:00
|
|
|
|
|
|
|
if (c == '\t') /* Map tab to a space. */
|
|
|
|
c = ' ';
|
|
|
|
else if ( (c >= 0 && c <= 0x1f) || c == 0x7f)
|
|
|
|
continue; /* Skip all other ASCII control characters. */
|
|
|
|
if (!(i < n-1))
|
|
|
|
{
|
|
|
|
n += 50;
|
|
|
|
buf = xrealloc (buf, n);
|
|
|
|
}
|
|
|
|
buf[i++] = c;
|
2003-08-05 19:11:04 +02:00
|
|
|
}
|
2019-03-07 14:11:46 +01:00
|
|
|
if (*cbuf != '\n')
|
|
|
|
{
|
|
|
|
buf[0] = CONTROL_D;
|
|
|
|
i = 1;
|
2003-08-05 19:11:04 +02:00
|
|
|
}
|
|
|
|
|
2019-03-07 14:11:46 +01:00
|
|
|
if (hidden)
|
|
|
|
{
|
2003-08-05 19:11:04 +02:00
|
|
|
#ifdef HAVE_TCGETATTR
|
2021-03-05 09:19:43 +01:00
|
|
|
if (tcsetattr (fileno(ttyfp), TCSAFLUSH, &termsave))
|
2019-03-07 14:11:46 +01:00
|
|
|
log_error ("tcsetattr() failed: %s\n", strerror(errno));
|
|
|
|
restore_termios = 0;
|
2021-03-05 09:19:43 +01:00
|
|
|
#endif /*HAVE_TCGETATTR*/
|
2003-08-05 19:11:04 +02:00
|
|
|
}
|
2021-03-05 09:19:43 +01:00
|
|
|
#endif /* Unix */
|
2019-03-07 14:11:46 +01:00
|
|
|
|
|
|
|
buf[i] = 0;
|
|
|
|
return buf;
|
2003-08-05 19:11:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-01-26 23:10:38 +01:00
|
|
|
|
|
|
|
/* Note: This function never returns NULL. */
|
2003-08-05 19:11:04 +02:00
|
|
|
char *
|
|
|
|
tty_get( const char *prompt )
|
|
|
|
{
|
2006-09-22 13:39:45 +02:00
|
|
|
if (!batchmode && !no_terminal && my_rl_readline && my_rl_add_history)
|
|
|
|
{
|
|
|
|
char *line;
|
|
|
|
char *buf;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2006-09-22 13:39:45 +02:00
|
|
|
if (!initialized)
|
|
|
|
init_ttyfp();
|
|
|
|
|
|
|
|
last_prompt_len = 0;
|
|
|
|
|
|
|
|
line = my_rl_readline (prompt?prompt:"");
|
|
|
|
|
|
|
|
/* We need to copy it to memory controlled by our malloc
|
|
|
|
implementations; further we need to convert an EOF to our
|
|
|
|
convention. */
|
|
|
|
buf = xmalloc(line? strlen(line)+1:2);
|
|
|
|
if (line)
|
|
|
|
{
|
|
|
|
strcpy (buf, line);
|
|
|
|
trim_spaces (buf);
|
|
|
|
if (strlen (buf) > 2 )
|
|
|
|
my_rl_add_history (line); /* Note that we test BUF but add LINE. */
|
|
|
|
free (line);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
buf[0] = CONTROL_D;
|
|
|
|
buf[1] = 0;
|
|
|
|
}
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return do_get ( prompt, 0 );
|
2003-08-05 19:11:04 +02:00
|
|
|
}
|
|
|
|
|
2021-03-05 09:19:43 +01:00
|
|
|
|
2017-02-20 22:19:50 +01:00
|
|
|
/* Variable argument version of tty_get. The prompt is actually a
|
2021-03-05 09:19:43 +01:00
|
|
|
* format string with arguments. */
|
2007-06-21 20:44:48 +02:00
|
|
|
char *
|
|
|
|
tty_getf (const char *promptfmt, ... )
|
|
|
|
{
|
|
|
|
va_list arg_ptr;
|
|
|
|
char *prompt;
|
|
|
|
char *answer;
|
|
|
|
|
|
|
|
va_start (arg_ptr, promptfmt);
|
2014-08-26 17:47:22 +02:00
|
|
|
if (gpgrt_vasprintf (&prompt, promptfmt, arg_ptr) < 0)
|
2007-06-21 20:44:48 +02:00
|
|
|
log_fatal ("estream_vasprintf failed: %s\n", strerror (errno));
|
|
|
|
va_end (arg_ptr);
|
|
|
|
answer = tty_get (prompt);
|
|
|
|
xfree (prompt);
|
|
|
|
return answer;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
char *
|
|
|
|
tty_get_hidden( const char *prompt )
|
|
|
|
{
|
2021-03-05 09:19:43 +01:00
|
|
|
return do_get (prompt, 1);
|
2003-08-05 19:11:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2021-03-05 09:19:43 +01:00
|
|
|
tty_kill_prompt (void)
|
2003-08-05 19:11:04 +02:00
|
|
|
{
|
2021-03-05 09:19:43 +01:00
|
|
|
if (no_terminal)
|
|
|
|
return;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
2021-03-05 09:19:43 +01:00
|
|
|
if (!initialized)
|
|
|
|
init_ttyfp ();
|
2003-08-05 19:11:04 +02:00
|
|
|
|
2021-03-05 09:19:43 +01:00
|
|
|
if (batchmode)
|
2003-08-05 19:11:04 +02:00
|
|
|
last_prompt_len = 0;
|
2021-03-05 09:19:43 +01:00
|
|
|
if (!last_prompt_len)
|
|
|
|
return;
|
|
|
|
#ifdef HAVE_W32_SYSTEM
|
|
|
|
tty_printf ("\r%*s\r", last_prompt_len, "");
|
|
|
|
#else /* Unix */
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
putc ('\r', ttyfp);
|
|
|
|
for (i=0; i < last_prompt_len; i ++ )
|
|
|
|
putc (' ', ttyfp);
|
|
|
|
putc ('\r', ttyfp);
|
|
|
|
fflush (ttyfp);
|
|
|
|
}
|
|
|
|
#endif /* Unix */
|
|
|
|
last_prompt_len = 0;
|
2003-08-05 19:11:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
tty_get_answer_is_yes( const char *prompt )
|
|
|
|
{
|
2021-03-05 09:19:43 +01:00
|
|
|
int yes;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
p = tty_get (prompt);
|
|
|
|
tty_kill_prompt ();
|
|
|
|
yes = answer_is_yes (p);
|
|
|
|
xfree (p);
|
|
|
|
|
|
|
|
return yes;
|
2003-08-05 19:11:04 +02:00
|
|
|
}
|
2006-09-21 15:30:45 +02:00
|
|
|
|
|
|
|
|
2007-06-26 15:48:44 +02:00
|
|
|
/* Called by gnupg_rl_initialize to setup the readline support. */
|
2006-09-21 15:30:45 +02:00
|
|
|
void
|
2006-09-22 13:39:45 +02:00
|
|
|
tty_private_set_rl_hooks (void (*init_stream) (FILE *),
|
|
|
|
void (*set_completer) (rl_completion_func_t*),
|
2006-09-21 15:30:45 +02:00
|
|
|
void (*inhibit_completion) (int),
|
2006-09-22 13:39:45 +02:00
|
|
|
void (*cleanup_after_signal) (void),
|
|
|
|
char *(*readline_fun) (const char*),
|
2020-07-02 15:47:57 +02:00
|
|
|
void (*add_history_fun) (const char*),
|
|
|
|
int (*rw_history_fun)(const char *, int, int))
|
2006-09-21 15:30:45 +02:00
|
|
|
{
|
2006-09-22 13:39:45 +02:00
|
|
|
my_rl_init_stream = init_stream;
|
2006-09-21 15:30:45 +02:00
|
|
|
my_rl_set_completer = set_completer;
|
|
|
|
my_rl_inhibit_completion = inhibit_completion;
|
|
|
|
my_rl_cleanup_after_signal = cleanup_after_signal;
|
2006-09-22 13:39:45 +02:00
|
|
|
my_rl_readline = readline_fun;
|
|
|
|
my_rl_add_history = add_history_fun;
|
2020-07-02 15:47:57 +02:00
|
|
|
my_rl_rw_history = rw_history_fun;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Read the history from FILENAME or limit the size of the history.
|
|
|
|
* If FILENAME is NULL and NLINES is zero the current history is
|
|
|
|
* cleared. Returns 0 on success or -1 on error and sets ERRNO. No
|
|
|
|
* error is return if readline support is not available. */
|
|
|
|
int
|
|
|
|
tty_read_history (const char *filename, int nlines)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
if (!my_rl_rw_history)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
rc = my_rl_rw_history (filename, 0, nlines);
|
|
|
|
if (rc && gpg_err_code_from_syserror () == GPG_ERR_ENOENT)
|
|
|
|
rc = 0;
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Write the current history to the file FILENAME. Returns 0 on
|
|
|
|
* success or -1 on error and sets ERRNO. No error is return if
|
|
|
|
* readline support is not available. */
|
|
|
|
int
|
|
|
|
tty_write_history (const char *filename)
|
|
|
|
{
|
|
|
|
if (!my_rl_rw_history)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return my_rl_rw_history (filename, 1, 0);
|
2006-09-21 15:30:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-11 20:34:16 +02:00
|
|
|
#ifdef HAVE_LIBREADLINE
|
2006-09-21 15:30:45 +02:00
|
|
|
void
|
|
|
|
tty_enable_completion (rl_completion_func_t *completer)
|
|
|
|
{
|
|
|
|
if (no_terminal || !my_rl_set_completer )
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!initialized)
|
|
|
|
init_ttyfp();
|
|
|
|
|
|
|
|
my_rl_set_completer (completer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tty_disable_completion (void)
|
|
|
|
{
|
|
|
|
if (no_terminal || !my_rl_inhibit_completion)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!initialized)
|
|
|
|
init_ttyfp();
|
|
|
|
|
|
|
|
my_rl_inhibit_completion (1);
|
|
|
|
}
|
2021-03-05 09:19:43 +01:00
|
|
|
#endif /* HAVE_LIBREADLINE */
|
2006-09-21 15:30:45 +02:00
|
|
|
|
2009-05-26 11:29:33 +02:00
|
|
|
void
|
|
|
|
tty_cleanup_after_signal (void)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_TCGETATTR
|
|
|
|
cleanup ();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2006-09-21 15:30:45 +02:00
|
|
|
void
|
|
|
|
tty_cleanup_rl_after_signal (void)
|
|
|
|
{
|
|
|
|
if (my_rl_cleanup_after_signal)
|
|
|
|
my_rl_cleanup_after_signal ();
|
|
|
|
}
|