1 /* status.c - Status message and command-fd interface
2 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003,
3 * 2004, 2005, 2006, 2010 Free Software Foundation, Inc.
5 * This file is part of GnuPG.
7 * GnuPG is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * GnuPG is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <https://www.gnu.org/licenses/>.
39 #define CONTROL_D ('D' - 'A' + 1)
42 /* The stream to output the status information. Output is disabled if
44 static estream_t statusfp;
48 progress_cb (void *ctx, const char *what, int printchar,
49 int current, int total)
55 if ( printchar == '\n' && !strcmp (what, "primegen") )
56 snprintf (buf, sizeof buf, "%.20s X 100 100", what );
58 snprintf (buf, sizeof buf, "%.20s %c %d %d",
59 what, printchar=='\n'?'X':printchar, current, total );
60 write_status_text (STATUS_PROGRESS, buf);
64 /* Return true if the status message NO may currently be issued. We
65 need this to avoid syncronisation problem while auto retrieving a
66 key. There it may happen that a status NODATA is issued for a non
67 available key and the user may falsely interpret this has a missing
70 status_currently_allowed (int no)
72 if (!glo_ctrl.in_auto_key_retrieve)
75 /* We allow some statis anyway, so that import statistics are
76 correct and to avoid problems if the retriebval subsystem will
82 case STATUS_GET_HIDDEN:
85 case STATUS_IMPORT_OK:
86 case STATUS_IMPORT_CHECK:
87 case STATUS_IMPORT_RES:
97 set_status_fd (int fd)
99 static int last_fd = -1;
101 if (fd != -1 && last_fd == fd)
104 if (statusfp && statusfp != es_stdout && statusfp != es_stderr )
105 es_fclose (statusfp);
111 statusfp = es_stdout;
113 statusfp = es_stderr;
115 statusfp = es_fdopen (fd, "w");
118 log_fatal ("can't open fd %d for status output: %s\n",
119 fd, strerror (errno));
123 gcry_set_progress_handler (progress_cb, NULL);
135 write_status ( int no )
137 write_status_text( no, NULL );
141 /* Write a status line with code NO followed by the string TEXT and
142 directly followed by the remaining strings up to a NULL. */
144 write_status_strings (int no, const char *text, ...)
149 if (!statusfp || !status_currently_allowed (no) )
150 return; /* Not enabled or allowed. */
152 es_fputs ("[GNUPG:] ", statusfp);
153 es_fputs (get_status_string (no), statusfp);
156 es_putc ( ' ', statusfp);
157 va_start (arg_ptr, text);
164 es_fputs ("\\n", statusfp);
166 es_fputs ("\\r", statusfp);
168 es_fputc (*(const byte *)s, statusfp);
171 while ((s = va_arg (arg_ptr, const char*)));
174 es_putc ('\n', statusfp);
175 if (es_fflush (statusfp) && opt.exit_on_status_write_error)
181 write_status_text (int no, const char *text)
183 write_status_strings (no, text, NULL);
187 /* Write a status line with code NO followed by the outout of the
188 * printf style FORMAT. The caller needs to make sure that LFs and
189 * CRs are not printed. */
191 write_status_printf (int no, const char *format, ...)
195 if (!statusfp || !status_currently_allowed (no) )
196 return; /* Not enabled or allowed. */
198 es_fputs ("[GNUPG:] ", statusfp);
199 es_fputs (get_status_string (no), statusfp);
202 es_putc ( ' ', statusfp);
203 va_start (arg_ptr, format);
204 es_vfprintf (statusfp, format, arg_ptr);
207 es_putc ('\n', statusfp);
208 if (es_fflush (statusfp) && opt.exit_on_status_write_error)
213 /* Write an ERROR status line using a full gpg-error error value. */
215 write_status_error (const char *where, gpg_error_t err)
217 if (!statusfp || !status_currently_allowed (STATUS_ERROR))
218 return; /* Not enabled or allowed. */
220 es_fprintf (statusfp, "[GNUPG:] %s %s %u\n",
221 get_status_string (STATUS_ERROR), where, err);
222 if (es_fflush (statusfp) && opt.exit_on_status_write_error)
227 /* Same as above but outputs the error code only. */
229 write_status_errcode (const char *where, int errcode)
231 if (!statusfp || !status_currently_allowed (STATUS_ERROR))
232 return; /* Not enabled or allowed. */
234 es_fprintf (statusfp, "[GNUPG:] %s %s %u\n",
235 get_status_string (STATUS_ERROR), where, gpg_err_code (errcode));
236 if (es_fflush (statusfp) && opt.exit_on_status_write_error)
241 /* Write a FAILURE status line. */
243 write_status_failure (const char *where, gpg_error_t err)
245 if (!statusfp || !status_currently_allowed (STATUS_FAILURE))
246 return; /* Not enabled or allowed. */
248 es_fprintf (statusfp, "[GNUPG:] %s %s %u\n",
249 get_status_string (STATUS_FAILURE), where, err);
250 if (es_fflush (statusfp) && opt.exit_on_status_write_error)
256 * Write a status line with a buffer using %XX escapes. If WRAP is >
257 * 0 wrap the line after this length. If STRING is not NULL it will
258 * be prepended to the buffer, no escaping is done for string.
259 * A wrap of -1 forces spaces not to be encoded as %20.
262 write_status_text_and_buffer (int no, const char *string,
263 const char *buffer, size_t len, int wrap)
265 const char *s, *text;
267 int lower_limit = ' ';
268 size_t n, count, dowrap;
270 if (!statusfp || !status_currently_allowed (no))
271 return; /* Not enabled or allowed. */
279 text = get_status_string (no);
280 count = dowrap = first = 1;
285 es_fprintf (statusfp, "[GNUPG:] %s ", text);
289 es_fputs (string, statusfp);
290 count += strlen (string);
291 /* Make sure that there is a space after the string. */
292 if (*string && string[strlen (string)-1] != ' ')
294 es_putc (' ', statusfp);
300 for (esc=0, s=buffer, n=len; n && !esc; s++, n--)
302 if (*s == '%' || *(const byte*)s <= lower_limit
303 || *(const byte*)s == 127 )
305 if (wrap && ++count > wrap)
316 es_fwrite (buffer, s-buffer, 1, statusfp);
319 es_fprintf (statusfp, "%%%02X", *(const byte*)s );
325 es_putc ('\n', statusfp);
329 es_putc ('\n',statusfp);
330 if (es_fflush (statusfp) && opt.exit_on_status_write_error)
336 write_status_buffer (int no, const char *buffer, size_t len, int wrap)
338 write_status_text_and_buffer (no, NULL, buffer, len, wrap);
342 /* Print the BEGIN_SIGNING status message. If MD is not NULL it is
343 used to retrieve the hash algorithms used for the message. */
345 write_status_begin_signing (gcry_md_hd_t md)
354 for (i=1; i <= 110; i++)
356 ga = map_md_openpgp_to_gcry (i);
357 if (ga && gcry_md_is_enabled (md, ga) && buflen+10 < DIM(buf))
359 snprintf (buf+buflen, DIM(buf) - buflen,
360 "%sH%d", buflen? " ":"",i);
361 buflen += strlen (buf+buflen);
364 write_status_text (STATUS_BEGIN_SIGNING, buf);
367 write_status ( STATUS_BEGIN_SIGNING );
372 myread(int fd, void *buf, size_t count)
377 rc = read( fd, buf, count );
379 while (rc == -1 && errno == EINTR);
383 static int eof_emmited=0;
384 if ( eof_emmited < 3 )
386 *(char*)buf = CONTROL_D;
390 else /* Ctrl-D not caught - do something reasonable */
392 #ifdef HAVE_DOSISH_SYSTEM
393 #ifndef HAVE_W32CE_SYSTEM
394 raise (SIGINT); /* Nothing to hangup under DOS. */
397 raise (SIGHUP); /* No more input data. */
406 /* Request a string from the client over the command-fd. If GETBOOL
407 is set the function returns a static string (do not free) if the
408 entered value was true or NULL if the entered value was false. */
410 do_get_from_fd ( const char *keyword, int hidden, int getbool )
415 if (statusfp != es_stdout)
416 es_fflush (es_stdout);
418 write_status_text (getbool? STATUS_GET_BOOL :
419 hidden? STATUS_GET_HIDDEN : STATUS_GET_LINE, keyword);
421 for (string = NULL, i = len = 200; ; i++ )
427 string = hidden? xmalloc_secure ( len ) : xmalloc ( len );
429 memcpy (string, save, i );
433 /* Fixme: why not use our read_line function here? */
434 if ( myread( opt.command_fd, string+i, 1) != 1 || string[i] == '\n' )
436 else if ( string[i] == CONTROL_D )
438 /* Found ETX - Cancel the line and return a sole ETX. */
439 string[0] = CONTROL_D;
446 write_status (STATUS_GOT_IT);
448 if (getbool) /* Fixme: is this correct??? */
449 return (string[0] == 'Y' || string[0] == 'y') ? "" : NULL;
459 if( opt.command_fd != -1 )
465 cpr_get_no_help( const char *keyword, const char *prompt )
469 if( opt.command_fd != -1 )
470 return do_get_from_fd ( keyword, 0, 0 );
472 p = tty_get( prompt );
478 cpr_get( const char *keyword, const char *prompt )
482 if( opt.command_fd != -1 )
483 return do_get_from_fd ( keyword, 0, 0 );
485 p = tty_get( prompt );
486 if( *p=='?' && !p[1] && !(keyword && !*keyword)) {
488 display_online_help( keyword );
497 cpr_get_utf8( const char *keyword, const char *prompt )
500 p = cpr_get( keyword, prompt );
502 char *utf8 = native_to_utf8( p );
510 cpr_get_hidden( const char *keyword, const char *prompt )
514 if( opt.command_fd != -1 )
515 return do_get_from_fd ( keyword, 1, 0 );
517 p = tty_get_hidden( prompt );
518 if( *p == '?' && !p[1] ) {
520 display_online_help( keyword );
528 cpr_kill_prompt(void)
530 if( opt.command_fd != -1 )
537 cpr_get_answer_is_yes_def (const char *keyword, const char *prompt, int def_yes)
542 if( opt.command_fd != -1 )
543 return !!do_get_from_fd ( keyword, 0, 1 );
545 p = tty_get( prompt );
546 trim_spaces(p); /* it is okay to do this here */
547 if( *p == '?' && !p[1] ) {
549 display_online_help( keyword );
553 yes = answer_is_yes_no_default (p, def_yes);
561 cpr_get_answer_is_yes (const char *keyword, const char *prompt)
563 return cpr_get_answer_is_yes_def (keyword, prompt, 0);
567 cpr_get_answer_yes_no_quit( const char *keyword, const char *prompt )
572 if( opt.command_fd != -1 )
573 return !!do_get_from_fd ( keyword, 0, 1 );
575 p = tty_get( prompt );
576 trim_spaces(p); /* it is okay to do this here */
577 if( *p == '?' && !p[1] ) {
579 display_online_help( keyword );
583 yes = answer_is_yes_no_quit(p);
592 cpr_get_answer_okay_cancel (const char *keyword,
600 if( opt.command_fd != -1 )
601 answer = do_get_from_fd ( keyword, 0, 0 );
605 yes = answer_is_okay_cancel (answer, def_answer);
612 p = tty_get( prompt );
613 trim_spaces(p); /* it is okay to do this here */
614 if (*p == '?' && !p[1])
617 display_online_help (keyword);
622 yes = answer_is_okay_cancel (p, def_answer);