1 /* status.c - status code helper functions
2 * Copyright (C) 2007 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * This file is free software; you can redistribute it and/or modify
7 * it under the terms of either
9 * - the GNU Lesser General Public License as published by the Free
10 * Software Foundation; either version 3 of the License, or (at
11 * your option) any later version.
15 * - the GNU General Public License as published by the Free
16 * Software Foundation; either version 2 of the License, or (at
17 * your option) any later version.
19 * or both in parallel, as here.
21 * This file is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, see <https://www.gnu.org/licenses/>.
35 #include "status-codes.h"
37 /* The stream to output the status information. Output is disabled if
39 static estream_t statusfp;
42 /* Return the status string for code NO. */
44 get_status_string ( int no )
46 int idx = statusstr_msgidxof (no);
50 return statusstr_msgstr + statusstr_msgidx[idx];
54 /* Set a global status FD. */
56 gnupg_set_status_fd (int fd)
58 static int last_fd = -1;
60 if (fd != -1 && last_fd == fd)
63 if (statusfp && statusfp != es_stdout && statusfp != es_stderr)
74 statusfp = es_fdopen (fd, "w");
77 log_fatal ("can't open fd %d for status output: %s\n",
78 fd, gpg_strerror (gpg_error_from_syserror ()));
84 /* Write a status line with code NO followed by the output of the
85 * printf style FORMAT. The caller needs to make sure that LFs and
86 * CRs are not printed. */
88 gnupg_status_printf (int no, const char *format, ...)
93 return; /* Not enabled. */
95 es_fputs ("[GNUPG:] ", statusfp);
96 es_fputs (get_status_string (no), statusfp);
99 es_putc (' ', statusfp);
100 va_start (arg_ptr, format);
101 es_vfprintf (statusfp, format, arg_ptr);
104 es_putc ('\n', statusfp);
108 /* Write a status line with code NO followed by the remaining
109 * arguments which must be a list of strings terminated by a NULL.
110 * Embedded CR and LFs in the strings are C-style escaped. All
111 * strings are printed with a space as delimiter. */
113 gnupg_status_strings (ctrl_t dummy, int no, ...)
121 return 0; /* Not enabled. */
123 va_start (arg_ptr, no);
125 es_fputs ("[GNUPG:] ", statusfp);
126 es_fputs (get_status_string (no), statusfp);
127 while ((s = va_arg (arg_ptr, const char*)))
130 es_putc (' ', statusfp);
134 es_fputs ("\\n", statusfp);
136 es_fputs ("\\r", statusfp);
138 es_fputc (*(const byte *)s, statusfp);
141 es_putc ('\n', statusfp);
142 es_fflush (statusfp);
150 get_inv_recpsgnr_code (gpg_error_t err)
154 switch (gpg_err_code (err))
156 case GPG_ERR_NO_PUBKEY: errstr = "1"; break;
157 case GPG_ERR_AMBIGUOUS_NAME: errstr = "2"; break;
158 case GPG_ERR_WRONG_KEY_USAGE: errstr = "3"; break;
159 case GPG_ERR_CERT_REVOKED: errstr = "4"; break;
160 case GPG_ERR_CERT_EXPIRED: errstr = "5"; break;
161 case GPG_ERR_NO_CRL_KNOWN:
162 case GPG_ERR_INV_CRL_OBJ: errstr = "6"; break;
163 case GPG_ERR_CRL_TOO_OLD: errstr = "7"; break;
164 case GPG_ERR_NO_POLICY_MATCH: errstr = "8"; break;
166 case GPG_ERR_UNUSABLE_SECKEY:
167 case GPG_ERR_NO_SECKEY: errstr = "9"; break;
169 case GPG_ERR_NOT_TRUSTED: errstr = "10"; break;
170 case GPG_ERR_MISSING_CERT: errstr = "11"; break;
171 case GPG_ERR_MISSING_ISSUER_CERT: errstr = "12"; break;
172 default: errstr = "0"; break;