aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWerner Koch <[email protected]>2019-01-22 08:02:17 +0000
committerWerner Koch <[email protected]>2019-01-22 08:02:17 +0000
commit03cf23b43ec5fea8a355d3ba2200e86a8efc589b (patch)
treee3d75a006782a881ffc0e737718213f80d1b96c6
parentscd: Support CHV-STATUS and CHECKPIN for PIV. (diff)
downloadgnupg-03cf23b43ec5fea8a355d3ba2200e86a8efc589b.tar.gz
gnupg-03cf23b43ec5fea8a355d3ba2200e86a8efc589b.zip
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 <[email protected]>
-rw-r--r--common/status.c58
-rw-r--r--common/status.h4
-rw-r--r--po/Makevars1
3 files changed, 63 insertions, 0 deletions
diff --git a/common/status.c b/common/status.c
index 50afce496..269ffea5d 100644
--- a/common/status.c
+++ b/common/status.c
@@ -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)
{
diff --git a/common/status.h b/common/status.h
index dc62f3629..aeab54202 100644
--- a/common/status.h
+++ b/common/status.h
@@ -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);
diff --git a/po/Makevars b/po/Makevars
index 20d6ae9d6..07778e055 100644
--- a/po/Makevars
+++ b/po/Makevars
@@ -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 \