common: Add generic status print function.

* common/status.c (gnupg_set_status_fd): New.
(gnupg_status_printf): New.
* po/Makevars (XGETTEXT_OPTIONS): Add gnupg-status_printf.
--

Some of the extra tools take a --status-fd option to print certain
status messages.  A generic printf style print function thus makes
sense.

Signed-off-by: Werner Koch <wk@gnupg.org>
This commit is contained in:
Werner Koch 2019-01-22 09:02:17 +01:00
parent fa9d703de5
commit 03cf23b43e
No known key found for this signature in database
GPG Key ID: E3FDFF218E45B72B
3 changed files with 63 additions and 0 deletions

View File

@ -34,6 +34,10 @@
#include "status.h"
#include "status-codes.h"
/* The stream to output the status information. Output is disabled if
* this is NULL. */
static estream_t statusfp;
/* Return the status string for code NO. */
const char *
@ -47,6 +51,60 @@ get_status_string ( int no )
}
/* Set a global status FD. */
void
gnupg_set_status_fd (int fd)
{
static int last_fd = -1;
if (fd != -1 && last_fd == fd)
return;
if (statusfp && statusfp != es_stdout && statusfp != es_stderr)
es_fclose (statusfp);
statusfp = NULL;
if (fd == -1)
return;
if (fd == 1)
statusfp = es_stdout;
else if (fd == 2)
statusfp = es_stderr;
else
statusfp = es_fdopen (fd, "w");
if (!statusfp)
{
log_fatal ("can't open fd %d for status output: %s\n",
fd, gpg_strerror (gpg_error_from_syserror ()));
}
last_fd = fd;
}
/* Write a status line with code NO followed by the output of the
* printf style FORMAT. The caller needs to make sure that LFs and
* CRs are not printed. */
void
gnupg_status_printf (int no, const char *format, ...)
{
va_list arg_ptr;
if (!statusfp)
return; /* Not enabled. */
es_fputs ("[GNUPG:] ", statusfp);
es_fputs (get_status_string (no), statusfp);
if (format)
{
es_putc (' ', statusfp);
va_start (arg_ptr, format);
es_vfprintf (statusfp, format, arg_ptr);
va_end (arg_ptr);
}
es_putc ('\n', statusfp);
}
const char *
get_inv_recpsgnr_code (gpg_error_t err)
{

View File

@ -163,6 +163,10 @@ enum
const char *get_status_string (int code);
void gnupg_set_status_fd (int fd);
void gnupg_status_printf (int no, const char *format,
...) GPGRT_ATTR_PRINTF(2,3);
const char *get_inv_recpsgnr_code (gpg_error_t err);

View File

@ -61,6 +61,7 @@ XGETTEXT_OPTIONS = \
--flag=ks_printf_help:2:c-format \
--flag=print_further_info:1:c-format \
--flag=write_status_printf:2:c-format \
--flag=gnupg_printf_status:2:c-format \
--flag=kbxd_print_status:3:c-format \
--flag=gpgconf_write_status:2:c-format \
--flag=send_status_printf:3:c-format \