1
0
mirror of git://git.gnupg.org/gnupg.git synced 2024-09-21 15:01:41 +02:00

use estream for status output.

This commit is contained in:
Werner Koch 2010-06-07 17:04:50 +00:00
parent c8eb7bd839
commit db0fd50da1
2 changed files with 148 additions and 121 deletions

View File

@ -1,3 +1,7 @@
2010-06-07 Werner Koch <wk@g10code.com>
* cpr.c: Use estream for status output.
2010-05-12 Werner Koch <wk@g10code.com> 2010-05-12 Werner Koch <wk@g10code.com>
* armor.c (radix64_read): Extended 2006-04-28 fix to fix bug#1179. * armor.c (radix64_read): Extended 2006-04-28 fix to fix bug#1179.

123
g10/cpr.c
View File

@ -40,8 +40,9 @@
#define CONTROL_D ('D' - 'A' + 1) #define CONTROL_D ('D' - 'A' + 1)
/* The stream to output the status information. Output is disabled if
static FILE *statusfp; this is NULL. */
static estream_t statusfp;
static void static void
@ -101,19 +102,20 @@ set_status_fd ( int fd )
if (fd != -1 && last_fd == fd) if (fd != -1 && last_fd == fd)
return; return;
if ( statusfp && statusfp != stdout && statusfp != stderr ) if (statusfp && statusfp != es_stdout && statusfp != es_stderr )
fclose (statusfp); es_fclose (statusfp);
statusfp = NULL; statusfp = NULL;
if (fd == -1) if (fd == -1)
return; return;
if (fd == 1) if (fd == 1)
statusfp = stdout; statusfp = es_stdout;
else if (fd == 2) else if (fd == 2)
statusfp = stderr; statusfp = es_stderr;
else else
statusfp = fdopen( fd, "w" ); statusfp = es_fdopen (fd, "w");
if( !statusfp ) { if (!statusfp)
{
log_fatal ("can't open fd %d for status output: %s\n", log_fatal ("can't open fd %d for status output: %s\n",
fd, strerror (errno)); fd, strerror (errno));
} }
@ -122,39 +124,44 @@ set_status_fd ( int fd )
gcry_set_progress_handler (progress_cb, NULL); gcry_set_progress_handler (progress_cb, NULL);
} }
int int
is_status_enabled () is_status_enabled ()
{ {
return !!statusfp; return !!statusfp;
} }
void void
write_status ( int no ) write_status ( int no )
{ {
write_status_text( no, NULL ); write_status_text( no, NULL );
} }
void void
write_status_text (int no, const char *text) write_status_text (int no, const char *text)
{ {
if (!statusfp || !status_currently_allowed (no) ) if (!statusfp || !status_currently_allowed (no) )
return; /* Not enabled or allowed. */ return; /* Not enabled or allowed. */
fputs ( "[GNUPG:] ", statusfp ); es_fputs ("[GNUPG:] ", statusfp);
fputs ( get_status_string (no), statusfp ); es_fputs (get_status_string (no), statusfp);
if( text ) { if ( text )
putc ( ' ', statusfp ); {
for (; *text; text++) { es_putc ( ' ', statusfp);
for (; *text; text++)
{
if (*text == '\n') if (*text == '\n')
fputs ( "\\n", statusfp ); es_fputs ("\\n", statusfp);
else if (*text == '\r') else if (*text == '\r')
fputs ( "\\r", statusfp ); es_fputs ("\\r", statusfp);
else else
putc ( *(const byte *)text, statusfp ); es_fputc ( *(const byte *)text, statusfp);
} }
} }
putc ('\n',statusfp); es_putc ('\n', statusfp);
if ( fflush (statusfp) && opt.exit_on_status_write_error ) if (es_fflush (statusfp) && opt.exit_on_status_write_error)
g10_exit (0); g10_exit (0);
} }
@ -166,23 +173,23 @@ write_status_error (const char *where, gpg_error_t err)
if (!statusfp || !status_currently_allowed (STATUS_ERROR)) if (!statusfp || !status_currently_allowed (STATUS_ERROR))
return; /* Not enabled or allowed. */ return; /* Not enabled or allowed. */
fprintf (statusfp, "[GNUPG:] %s %s %u\n", es_fprintf (statusfp, "[GNUPG:] %s %s %u\n",
get_status_string (STATUS_ERROR), where, err); get_status_string (STATUS_ERROR), where, err);
if (fflush (statusfp) && opt.exit_on_status_write_error) if (es_fflush (statusfp) && opt.exit_on_status_write_error)
g10_exit (0); g10_exit (0);
} }
/* Same as above but only putputs the error code. */ /* Same as above but outputs the error code only. */
void void
write_status_errcode (const char *where, int errcode) write_status_errcode (const char *where, int errcode)
{ {
if (!statusfp || !status_currently_allowed (STATUS_ERROR)) if (!statusfp || !status_currently_allowed (STATUS_ERROR))
return; /* Not enabled or allowed. */ return; /* Not enabled or allowed. */
fprintf (statusfp, "[GNUPG:] %s %s %u\n", es_fprintf (statusfp, "[GNUPG:] %s %s %u\n",
get_status_string (STATUS_ERROR), where, gpg_err_code (errcode)); get_status_string (STATUS_ERROR), where, gpg_err_code (errcode));
if (fflush (statusfp) && opt.exit_on_status_write_error) if (es_fflush (statusfp) && opt.exit_on_status_write_error)
g10_exit (0); g10_exit (0);
} }
@ -205,58 +212,68 @@ write_status_text_and_buffer ( int no, const char *string,
if (!statusfp || !status_currently_allowed (no)) if (!statusfp || !status_currently_allowed (no))
return; /* Not enabled or allowed. */ return; /* Not enabled or allowed. */
if (wrap == -1) { if (wrap == -1)
{
lower_limit--; lower_limit--;
wrap = 0; wrap = 0;
} }
text = get_status_string (no); text = get_status_string (no);
count = dowrap = first = 1; count = dowrap = first = 1;
do { do
if (dowrap) { {
fprintf (statusfp, "[GNUPG:] %s ", text ); if (dowrap)
{
es_fprintf (statusfp, "[GNUPG:] %s ", text);
count = dowrap = 0; count = dowrap = 0;
if (first && string) { if (first && string)
fputs (string, statusfp); {
es_fputs (string, statusfp);
count += strlen (string); count += strlen (string);
/* Make sure that there is space after the string. */ /* Make sure that there is a space after the string. */
if (*string && string[strlen (string)-1] != ' ') if (*string && string[strlen (string)-1] != ' ')
{ {
putc (' ', statusfp); es_putc (' ', statusfp);
count++; count++;
} }
} }
first = 0; first = 0;
} }
for (esc=0, s=buffer, n=len; n && !esc; s++, n-- ) { for (esc=0, s=buffer, n=len; n && !esc; s++, n--)
{
if (*s == '%' || *(const byte*)s <= lower_limit if (*s == '%' || *(const byte*)s <= lower_limit
|| *(const byte*)s == 127 ) || *(const byte*)s == 127 )
esc = 1; esc = 1;
if ( wrap && ++count > wrap ) { if (wrap && ++count > wrap)
{
dowrap=1; dowrap=1;
break; break;
} }
} }
if (esc) { if (esc)
{
s--; n++; s--; n++;
} }
if (s != buffer) if (s != buffer)
fwrite (buffer, s-buffer, 1, statusfp ); es_fwrite (buffer, s-buffer, 1, statusfp);
if ( esc ) { if ( esc )
fprintf (statusfp, "%%%02X", *(const byte*)s ); {
es_fprintf (statusfp, "%%%02X", *(const byte*)s );
s++; n--; s++; n--;
} }
buffer = s; buffer = s;
len = n; len = n;
if (dowrap && len) if (dowrap && len)
putc ( '\n', statusfp ); es_putc ('\n', statusfp);
} while ( len ); }
while (len);
putc ('\n',statusfp); es_putc ('\n',statusfp);
if ( fflush (statusfp) && opt.exit_on_status_write_error ) if (es_fflush (statusfp) && opt.exit_on_status_write_error)
g10_exit (0); g10_exit (0);
} }
void void
write_status_buffer (int no, const char *buffer, size_t len, int wrap) write_status_buffer (int no, const char *buffer, size_t len, int wrap)
{ {
@ -302,23 +319,29 @@ static int
myread(int fd, void *buf, size_t count) myread(int fd, void *buf, size_t count)
{ {
int rc; int rc;
do { do
{
rc = read( fd, buf, count ); rc = read( fd, buf, count );
} while ( rc == -1 && errno == EINTR ); }
if ( !rc && count ) { while (rc == -1 && errno == EINTR);
if (!rc && count)
{
static int eof_emmited=0; static int eof_emmited=0;
if ( eof_emmited < 3 ) { if ( eof_emmited < 3 )
{
*(char*)buf = CONTROL_D; *(char*)buf = CONTROL_D;
rc = 1; rc = 1;
eof_emmited++; eof_emmited++;
} }
else { /* Ctrl-D not caught - do something reasonable */ else /* Ctrl-D not caught - do something reasonable */
{
#ifdef HAVE_DOSISH_SYSTEM #ifdef HAVE_DOSISH_SYSTEM
#ifndef HAVE_W32CE_SYSTEM #ifndef HAVE_W32CE_SYSTEM
raise (SIGINT); /* nothing to hangup under DOS */ raise (SIGINT); /* Nothing to hangup under DOS. */
#endif #endif
#else #else
raise (SIGHUP); /* no more input data */ raise (SIGHUP); /* No more input data. */
#endif #endif
} }
} }
@ -336,8 +359,8 @@ do_get_from_fd ( const char *keyword, int hidden, int getbool )
int i, len; int i, len;
char *string; char *string;
if (statusfp != stdout) if (statusfp != es_stdout)
fflush (stdout); es_fflush (es_stdout);
write_status_text (getbool? STATUS_GET_BOOL : write_status_text (getbool? STATUS_GET_BOOL :
hidden? STATUS_GET_HIDDEN : STATUS_GET_LINE, keyword); hidden? STATUS_GET_HIDDEN : STATUS_GET_LINE, keyword);