aboutsummaryrefslogtreecommitdiffstats
path: root/common/ttyio.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--common/ttyio.c (renamed from util/ttyio.c)138
1 files changed, 54 insertions, 84 deletions
diff --git a/util/ttyio.c b/common/ttyio.c
index 97b160ef2..38883afa5 100644
--- a/util/ttyio.c
+++ b/common/ttyio.c
@@ -1,6 +1,6 @@
/* ttyio.c - tty i/O functions
- * Copyright (C) 1998, 1999, 2000, 2001, 2002,
- * 2004 Free Software Foundation, Inc.
+ * Copyright (C) 1998,1999,2000,2001,2002,2003,
+ * 2004, 2006 Free Software Foundation, Inc.
*
* This file is part of GnuPG.
*
@@ -42,7 +42,7 @@
#ifdef _WIN32 /* use the odd Win32 functions */
#include <windows.h>
#ifdef HAVE_TCGETATTR
-#error windows and termios
+#error mingw32 and termios
#endif
#endif
#include <errno.h>
@@ -52,6 +52,7 @@
#include <readline/history.h>
#endif
+
#include "util.h"
#include "memory.h"
#include "ttyio.h"
@@ -100,18 +101,27 @@ tty_get_ttyname (void)
if (!got_name)
{
const char *s;
+ /* 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
s = ctermid (NULL);
+# endif
if (s)
name = strdup (s);
got_name = 1;
}
-#endif
- /* Assume the standard tty on memory error or when there is no
- ctermid. */
+#endif /*HAVE_CTERMID*/
+ /* Assume the standard tty on memory error or when tehre is no
+ certmid. */
return name? name : "/dev/tty";
}
+
#ifdef HAVE_TCGETATTR
static void
cleanup(void)
@@ -141,15 +151,15 @@ init_ttyfp(void)
FILE_SHARE_READ|FILE_SHARE_WRITE,
&sa, OPEN_EXISTING, 0, 0 );
if( con.out == INVALID_HANDLE_VALUE )
- log_fatal ("open(CONOUT$) failed: %s", w32_strerror (0));
+ log_fatal("open(CONOUT$) failed: rc=%d", (int)GetLastError() );
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", w32_strerror (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 );
@@ -157,17 +167,12 @@ init_ttyfp(void)
#elif defined(__EMX__)
ttyfp = stdout; /* Fixme: replace by the real functions: see wklib */
#else
- ttyfp = batchmode? stderr : fopen( tty_get_ttyname (), "r+");
+ ttyfp = batchmode? stderr : fopen (tty_get_ttyname (), "r+");
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);
}
-#ifdef HAVE_LIBREADLINE
- rl_catch_signals = 0;
- rl_instream = rl_outstream = ttyfp;
- rl_inhibit_completion = 1;
-#endif
#endif
#ifdef HAVE_TCGETATTR
atexit( cleanup );
@@ -175,33 +180,35 @@ init_ttyfp(void)
initialized = 1;
}
+
#ifdef HAVE_LIBREADLINE
void
tty_enable_completion(rl_completion_func_t *completer)
{
- if( no_terminal )
- return;
+/* if( no_terminal ) */
+/* return; */
- if( !initialized )
- init_ttyfp();
+/* if( !initialized ) */
+/* init_ttyfp(); */
- rl_attempted_completion_function=completer;
- rl_inhibit_completion=0;
+/* rl_attempted_completion_function=completer; */
+/* rl_inhibit_completion=0; */
}
void
tty_disable_completion(void)
{
- if( no_terminal )
- return;
+/* if( no_terminal ) */
+/* return; */
- if( !initialized )
- init_ttyfp();
+/* if( !initialized ) */
+/* init_ttyfp(); */
- rl_inhibit_completion=1;
+/* rl_inhibit_completion=1; */
}
#endif /*HAVE_LIBREADLINE*/
+
int
tty_batchmode( int onoff )
{
@@ -241,10 +248,10 @@ tty_printf( const char *fmt, ... )
if( !buf )
log_bug("vasprintf() failed\n");
- if (!WriteConsoleA (con.out, buf, n, &nwritten, NULL))
- log_fatal ("WriteConsole failed: %s", w32_strerror (0));
+ if( !WriteConsoleA( con.out, buf, n, &nwritten, NULL ) )
+ log_fatal("WriteConsole failed: rc=%d", (int)GetLastError() );
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;
xfree (buf);
}
@@ -256,7 +263,7 @@ tty_printf( const char *fmt, ... )
}
-/* Same as tty_printf but if FP is not NULL, behave like the standard
+/* Same as tty_printf but if FP is not NULL, behave like a regualr
fprintf. */
void
tty_fprintf (FILE *fp, const char *fmt, ... )
@@ -288,10 +295,10 @@ tty_fprintf (FILE *fp, const char *fmt, ... )
if( !buf )
log_bug("vasprintf() failed\n");
- if (!WriteConsoleA (con.out, buf, n, &nwritten, NULL))
- log_fatal ("WriteConsole failed: %s", w32_strerror (0));
- if (n != nwritten)
- log_fatal ("WriteConsole failed: %d != %d\n", n, (int)nwritten);
+ if( !WriteConsoleA( con.out, buf, n, &nwritten, NULL ) )
+ log_fatal("WriteConsole failed: rc=%d", (int)GetLastError() );
+ if( n != nwritten )
+ log_fatal("WriteConsole failed: %d != %d\n", n, (int)nwritten );
last_prompt_len += n;
xfree (buf);
}
@@ -307,7 +314,7 @@ tty_fprintf (FILE *fp, const char *fmt, ... )
* Print a string, but filter all control characters out.
*/
void
-tty_print_string( const byte *p, size_t n )
+tty_print_string ( const byte *p, size_t n )
{
if (no_terminal)
return;
@@ -345,7 +352,7 @@ tty_print_string( const byte *p, size_t n )
}
void
-tty_print_utf8_string2 (const byte *p, size_t n, size_t max_n )
+tty_print_utf8_string2( const byte *p, size_t n, size_t max_n )
{
size_t i;
char *buf;
@@ -359,7 +366,7 @@ tty_print_utf8_string2 (const byte *p, size_t n, size_t max_n )
break;
}
if( i < n ) {
- buf = utf8_to_native( p, n, 0 );
+ buf = utf8_to_native( (const char *)p, n, 0 );
if( max_n && (strlen( buf ) > max_n )) {
buf[max_n] = 0;
}
@@ -405,20 +412,19 @@ do_get( const char *prompt, int hidden )
init_ttyfp();
last_prompt_len = 0;
- buf = xmalloc(n=50);
+ tty_printf( "%s", prompt );
+ buf = xmalloc((n=50));
i = 0;
#ifdef _WIN32 /* windoze version */
if( hidden )
SetConsoleMode(con.in, HID_INPMODE );
- tty_printf( "%s", prompt );
-
for(;;) {
DWORD nread;
- if (!ReadConsoleA (con.in, cbuf, 1, &nread, NULL))
- log_fatal ("ReadConsole failed: %s", w32_strerror (0));
+ if( !ReadConsoleA( con.in, cbuf, 1, &nread, NULL ) )
+ log_fatal("ReadConsole failed: rc=%d", (int)GetLastError() );
if( !nread )
continue;
if( *cbuf == '\n' )
@@ -436,7 +442,7 @@ do_get( const char *prompt, int hidden )
continue;
if( !(i < n-1) ) {
n += 50;
- buf = xrealloc( buf, n );
+ buf = xrealloc (buf, n);
}
buf[i++] = c;
}
@@ -445,7 +451,6 @@ do_get( const char *prompt, int hidden )
SetConsoleMode(con.in, DEF_INPMODE );
#elif defined(__riscos__)
- tty_printf( "%s", prompt );
do {
c = riscos_getchar();
if (c == 0xa || c == 0xd) { /* Return || Enter */
@@ -475,7 +480,7 @@ do_get( const char *prompt, int hidden )
}
if(!(i < n-1)) {
n += 50;
- buf = xrealloc(buf, n);
+ buf = xrealloc (buf, n);
}
buf[i++] = c;
if (!hidden) {
@@ -500,8 +505,6 @@ do_get( const char *prompt, int hidden )
#endif
}
- tty_printf( "%s", prompt );
-
/* 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' ) {
@@ -519,7 +522,7 @@ do_get( const char *prompt, int hidden )
continue;
if( !(i < n-1) ) {
n += 50;
- buf = xrealloc( buf, n );
+ buf = xrealloc (buf, n );
}
buf[i++] = c;
}
@@ -545,40 +548,7 @@ do_get( const char *prompt, int hidden )
char *
tty_get( const char *prompt )
{
-#ifdef HAVE_LIBREADLINE
- if (!batchmode && !no_terminal) {
- char *line;
- char *buf;
-
- if( !initialized )
- init_ttyfp();
-
- last_prompt_len = 0;
-
- line = 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 )
- add_history (line); /* Note that we test BUF but add LINE. */
- free (line);
- }
- else
- {
- buf[0] = CONTROL_D;
- buf[1] = 0;
- }
- return buf;
- }
- else
-#endif /* HAVE_LIBREADLINE */
- return do_get( prompt, 0 );
+ return do_get( prompt, 0 );
}
char *