summaryrefslogtreecommitdiffstats
path: root/util
diff options
context:
space:
mode:
authorWerner Koch <wk@gnupg.org>1998-01-02 21:40:10 +0100
committerWerner Koch <wk@gnupg.org>1998-01-02 21:40:10 +0100
commitb7bdef0834f9d04f96f69a5323e0ac3e5e1b7bc2 (patch)
treecb6bae66627f4ea4d0a6b29631223ed2fe78ee8e /util
parentSylvester Version (diff)
downloadgnupg2-b7bdef0834f9d04f96f69a5323e0ac3e5e1b7bc2.tar.xz
gnupg2-b7bdef0834f9d04f96f69a5323e0ac3e5e1b7bc2.zip
added more stuff
Diffstat (limited to 'util')
-rw-r--r--util/iobuf.c9
-rw-r--r--util/logger.c14
-rw-r--r--util/ttyio.c104
3 files changed, 73 insertions, 54 deletions
diff --git a/util/iobuf.c b/util/iobuf.c
index 58b0d7785..981fb2efb 100644
--- a/util/iobuf.c
+++ b/util/iobuf.c
@@ -450,6 +450,7 @@ iobuf_push_filter( IOBUF a,
/* remove the filter stuff from the new stream */
a->filter = NULL;
a->filter_ov = NULL;
+ a->filter_eof = 0;
if( a->usage == 2 ) { /* allocate a fresh buffer for the original stream */
b->d.buf = m_alloc( a->d.size );
b->d.len = 0;
@@ -539,7 +540,7 @@ iobuf_pop_filter( IOBUF a, int (*f)(void *opaque, int control,
m_free(b);
}
else if( !b->chain ) { /* remove the last iobuf from the chain */
- log_bug("Ohh jeee, trying to a head filter\n");
+ log_bug("Ohh jeee, trying to remove a head filter\n");
}
else { /* remove an intermediate iobuf from the chain */
log_bug("Ohh jeee, trying to remove an intermediate filter\n");
@@ -833,13 +834,13 @@ iobuf_set_block_mode( IOBUF a, size_t n )
/****************
- * checks wether the stream is in block mode
+ * Checks wether the stream is in block mode
+ * Note: This does not work if other filters are pushed on the stream.
*/
int
iobuf_in_block_mode( IOBUF a )
{
- for(; a; a = a->chain )
- if( a->filter == block_filter )
+ if( a && a->filter == block_filter )
return 1; /* yes */
return 0; /* no */
}
diff --git a/util/logger.c b/util/logger.c
index 8993ba43d..c32332b48 100644
--- a/util/logger.c
+++ b/util/logger.c
@@ -26,10 +26,10 @@
#include "util.h"
static char pidstring[15];
-
+static int errorcount;
void
-set_log_pid( int pid )
+log_set_pid( int pid )
{
if( pid )
sprintf(pidstring,"[%u]", (unsigned)pid );
@@ -37,6 +37,15 @@ set_log_pid( int pid )
*pidstring = 0;
}
+int
+log_get_errorcount( int clear)
+{
+ int n = errorcount;
+ if( clear )
+ errorcount = 0;
+ return n;
+}
+
/****************
* General interface for printing a line
@@ -90,6 +99,7 @@ log_error( const char *fmt, ... )
va_start( arg_ptr, fmt ) ;
vfprintf(stderr,fmt,arg_ptr) ;
va_end(arg_ptr);
+ errorcount++;
}
void
diff --git a/util/ttyio.c b/util/ttyio.c
index c8a4e8f68..31b80e68a 100644
--- a/util/ttyio.c
+++ b/util/ttyio.c
@@ -30,52 +30,33 @@
#include "memory.h"
#include "ttyio.h"
+static FILE *ttyfp = NULL;
static int last_prompt_len;
-static FILE *
-open_tty(struct termios *termsave )
+static void
+init_ttyfp()
{
- struct termios term;
+ if( ttyfp )
+ return;
- FILE *tty = fopen("/dev/tty", "r");
- if( !tty )
+ ttyfp = fopen("/dev/tty", "r+");
+ if( !ttyfp )
log_fatal("cannot open /dev/tty: %s\n", strerror(errno) );
-
- if( termsave ) { /* hide input */
- if( tcgetattr(fileno(tty), termsave) )
- log_fatal("tcgetattr() failed: %s\n", strerror(errno) );
- term = *termsave;
- term.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
- if( tcsetattr( fileno(tty), TCSAFLUSH, &term ) )
- log_fatal("tcsetattr() failed: %s\n", strerror(errno) );
- }
-
-
- return tty;
-}
-
-static void
-close_tty( FILE *tty, struct termios *termsave )
-{
- if( termsave ) {
- if( tcsetattr(fileno(tty), TCSAFLUSH, termsave) )
- log_error("tcsetattr() failed: %s\n", strerror(errno) );
- putc('\n', stderr);
- }
- fclose(tty);
}
-
void
tty_printf( const char *fmt, ... )
{
va_list arg_ptr;
+ if( !ttyfp )
+ init_ttyfp();
+
va_start( arg_ptr, fmt ) ;
- last_prompt_len += vfprintf(stderr,fmt,arg_ptr) ;
+ last_prompt_len += vfprintf(ttyfp,fmt,arg_ptr) ;
va_end(arg_ptr);
- fflush(stderr);
+ fflush(ttyfp);
}
@@ -85,18 +66,21 @@ tty_printf( const char *fmt, ... )
void
tty_print_string( byte *p, size_t n )
{
+ if( !ttyfp )
+ init_ttyfp();
+
for( ; n; n--, p++ )
if( iscntrl( *p ) ) {
- putc('\\', stderr);
+ putc('\\', ttyfp);
if( *p == '\n' )
- putc('n', stderr);
+ putc('n', ttyfp);
else if( !*p )
- putc('0', stderr);
+ putc('0', ttyfp);
else
- fprintf(stderr, "x%02x", *p );
+ fprintf(ttyfp, "x%02x", *p );
}
else
- putc(*p, stderr);
+ putc(*p, ttyfp);
}
@@ -107,17 +91,36 @@ static char *
do_get( const char *prompt, int hidden )
{
char *buf;
+ byte cbuf[1];
int c, n, i;
FILE *fp;
struct termios termsave;
+ if( !ttyfp )
+ init_ttyfp();
+
last_prompt_len = 0;
tty_printf( prompt );
buf = m_alloc(n=50);
i = 0;
- fp = open_tty(hidden? &termsave: NULL);
- while( (c=getc(fp)) != EOF && c != '\n' ) {
- last_prompt_len++;
+
+ if( hidden ) {
+ 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) );
+ }
+
+ /* 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 == '\t' )
c = ' ';
else if( iscntrl(c) )
@@ -128,7 +131,11 @@ do_get( const char *prompt, int hidden )
}
buf[i++] = c;
}
- close_tty(fp, hidden? &termsave: NULL);
+
+ if( hidden ) {
+ if( tcsetattr(fileno(ttyfp), TCSAFLUSH, &termsave) )
+ log_error("tcsetattr() failed: %s\n", strerror(errno) );
+ }
buf[i] = 0;
return buf;
}
@@ -151,15 +158,16 @@ void
tty_kill_prompt()
{
int i;
-#if 0
- for(i=0; i < last_prompt_len; i ++ )
- fputc('\b', stderr);
- for(i=0; i < last_prompt_len; i ++ )
- fputc(' ', stderr);
+
+ if( !ttyfp )
+ init_ttyfp();
+ if( !last_prompt_len )
+ return;
+ fputc('\r', ttyfp);
for(i=0; i < last_prompt_len; i ++ )
- fputc('\b', stderr);
-#endif
+ fputc(' ', ttyfp);
+ fputc('\r', ttyfp);
last_prompt_len = 0;
- fflush(stderr);
+ fflush(ttyfp);
}