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