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 \