xref: /freebsd/contrib/xz/src/xz/message.c (revision 783d3ff6)
1 // SPDX-License-Identifier: 0BSD
2 
3 ///////////////////////////////////////////////////////////////////////////////
4 //
5 /// \file       message.c
6 /// \brief      Printing messages
7 //
8 //  Authors:    Lasse Collin
9 //              Jia Tan
10 //
11 ///////////////////////////////////////////////////////////////////////////////
12 
13 #include "private.h"
14 
15 #include <stdarg.h>
16 
17 
18 /// Number of the current file
19 static unsigned int files_pos = 0;
20 
21 /// Total number of input files; zero if unknown.
22 static unsigned int files_total;
23 
24 /// Verbosity level
25 static enum message_verbosity verbosity = V_WARNING;
26 
27 /// Filename which we will print with the verbose messages
28 static const char *filename;
29 
30 /// True once the a filename has been printed to stderr as part of progress
31 /// message. If automatic progress updating isn't enabled, this becomes true
32 /// after the first progress message has been printed due to user sending
33 /// SIGINFO, SIGUSR1, or SIGALRM. Once this variable is true, we will print
34 /// an empty line before the next filename to make the output more readable.
35 static bool first_filename_printed = false;
36 
37 /// This is set to true when we have printed the current filename to stderr
38 /// as part of a progress message. This variable is useful only if not
39 /// updating progress automatically: if user sends many SIGINFO, SIGUSR1, or
40 /// SIGALRM signals, we won't print the name of the same file multiple times.
41 static bool current_filename_printed = false;
42 
43 /// True if we should print progress indicator and update it automatically
44 /// if also verbose >= V_VERBOSE.
45 static bool progress_automatic = false;
46 
47 /// True if message_progress_start() has been called but
48 /// message_progress_end() hasn't been called yet.
49 static bool progress_started = false;
50 
51 /// This is true when a progress message was printed and the cursor is still
52 /// on the same line with the progress message. In that case, a newline has
53 /// to be printed before any error messages.
54 static bool progress_active = false;
55 
56 /// Pointer to lzma_stream used to do the encoding or decoding.
57 static lzma_stream *progress_strm;
58 
59 /// This is true if we are in passthru mode (not actually compressing or
60 /// decompressing) and thus cannot use lzma_get_progress(progress_strm, ...).
61 /// That is, we are using coder_passthru() in coder.c.
62 static bool progress_is_from_passthru;
63 
64 /// Expected size of the input stream is needed to show completion percentage
65 /// and estimate remaining time.
66 static uint64_t expected_in_size;
67 
68 
69 // Use alarm() and SIGALRM when they are supported. This has two minor
70 // advantages over the alternative of polling gettimeofday():
71 //  - It is possible for the user to send SIGINFO, SIGUSR1, or SIGALRM to
72 //    get intermediate progress information even when --verbose wasn't used
73 //    or stderr is not a terminal.
74 //  - alarm() + SIGALRM seems to have slightly less overhead than polling
75 //    gettimeofday().
76 #ifdef SIGALRM
77 
78 const int message_progress_sigs[] = {
79 	SIGALRM,
80 #ifdef SIGINFO
81 	SIGINFO,
82 #endif
83 #ifdef SIGUSR1
84 	SIGUSR1,
85 #endif
86 	0
87 };
88 
89 /// The signal handler for SIGALRM sets this to true. It is set back to false
90 /// once the progress message has been updated.
91 static volatile sig_atomic_t progress_needs_updating = false;
92 
93 /// Signal handler for SIGALRM
94 static void
95 progress_signal_handler(int sig lzma_attribute((__unused__)))
96 {
97 	progress_needs_updating = true;
98 	return;
99 }
100 
101 #else
102 
103 /// This is true when progress message printing is wanted. Using the same
104 /// variable name as above to avoid some ifdefs.
105 static bool progress_needs_updating = false;
106 
107 /// Elapsed time when the next progress message update should be done.
108 static uint64_t progress_next_update;
109 
110 #endif
111 
112 
113 extern void
114 message_init(void)
115 {
116 	// If --verbose is used, we use a progress indicator if and only
117 	// if stderr is a terminal. If stderr is not a terminal, we print
118 	// verbose information only after finishing the file. As a special
119 	// exception, even if --verbose was not used, user can send SIGALRM
120 	// to make us print progress information once without automatic
121 	// updating.
122 	progress_automatic = is_tty(STDERR_FILENO);
123 
124 #ifdef SIGALRM
125 	// Establish the signal handlers which set a flag to tell us that
126 	// progress info should be updated.
127 	struct sigaction sa;
128 	sigemptyset(&sa.sa_mask);
129 	sa.sa_flags = 0;
130 	sa.sa_handler = &progress_signal_handler;
131 
132 	for (size_t i = 0; message_progress_sigs[i] != 0; ++i)
133 		if (sigaction(message_progress_sigs[i], &sa, NULL))
134 			message_signal_handler();
135 #endif
136 
137 	return;
138 }
139 
140 
141 extern void
142 message_verbosity_increase(void)
143 {
144 	if (verbosity < V_DEBUG)
145 		++verbosity;
146 
147 	return;
148 }
149 
150 
151 extern void
152 message_verbosity_decrease(void)
153 {
154 	if (verbosity > V_SILENT)
155 		--verbosity;
156 
157 	return;
158 }
159 
160 
161 extern enum message_verbosity
162 message_verbosity_get(void)
163 {
164 	return verbosity;
165 }
166 
167 
168 extern void
169 message_set_files(unsigned int files)
170 {
171 	files_total = files;
172 	return;
173 }
174 
175 
176 /// Prints the name of the current file if it hasn't been printed already,
177 /// except if we are processing exactly one stream from stdin to stdout.
178 /// I think it looks nicer to not print "(stdin)" when --verbose is used
179 /// in a pipe and no other files are processed.
180 static void
181 print_filename(void)
182 {
183 	if (!opt_robot && (files_total != 1 || filename != stdin_filename)) {
184 		signals_block();
185 
186 		FILE *file = opt_mode == MODE_LIST ? stdout : stderr;
187 
188 		// If a file was already processed, put an empty line
189 		// before the next filename to improve readability.
190 		if (first_filename_printed)
191 			fputc('\n', file);
192 
193 		first_filename_printed = true;
194 		current_filename_printed = true;
195 
196 		// If we don't know how many files there will be due
197 		// to usage of --files or --files0.
198 		if (files_total == 0)
199 			fprintf(file, "%s (%u)\n", filename,
200 					files_pos);
201 		else
202 			fprintf(file, "%s (%u/%u)\n", filename,
203 					files_pos, files_total);
204 
205 		signals_unblock();
206 	}
207 
208 	return;
209 }
210 
211 
212 extern void
213 message_filename(const char *src_name)
214 {
215 	// Start numbering the files starting from one.
216 	++files_pos;
217 	filename = src_name;
218 
219 	if (verbosity >= V_VERBOSE
220 			&& (progress_automatic || opt_mode == MODE_LIST))
221 		print_filename();
222 	else
223 		current_filename_printed = false;
224 
225 	return;
226 }
227 
228 
229 extern void
230 message_progress_start(lzma_stream *strm, bool is_passthru, uint64_t in_size)
231 {
232 	// Store the pointer to the lzma_stream used to do the coding.
233 	// It is needed to find out the position in the stream.
234 	progress_strm = strm;
235 	progress_is_from_passthru = is_passthru;
236 
237 	// Store the expected size of the file. If we aren't printing any
238 	// statistics, then is will be unused. But since it is possible
239 	// that the user sends us a signal to show statistics, we need
240 	// to have it available anyway.
241 	expected_in_size = in_size;
242 
243 	// Indicate that progress info may need to be printed before
244 	// printing error messages.
245 	progress_started = true;
246 
247 	// If progress indicator is wanted, print the filename and possibly
248 	// the file count now.
249 	if (verbosity >= V_VERBOSE && progress_automatic) {
250 		// Start the timer to display the first progress message
251 		// after one second. An alternative would be to show the
252 		// first message almost immediately, but delaying by one
253 		// second looks better to me, since extremely early
254 		// progress info is pretty much useless.
255 #ifdef SIGALRM
256 		// First disable a possibly existing alarm.
257 		alarm(0);
258 		progress_needs_updating = false;
259 		alarm(1);
260 #else
261 		progress_needs_updating = true;
262 		progress_next_update = 1000;
263 #endif
264 	}
265 
266 	return;
267 }
268 
269 
270 /// Make the string indicating completion percentage.
271 static const char *
272 progress_percentage(uint64_t in_pos)
273 {
274 	// If the size of the input file is unknown or the size told us is
275 	// clearly wrong since we have processed more data than the alleged
276 	// size of the file, show a static string indicating that we have
277 	// no idea of the completion percentage.
278 	if (expected_in_size == 0 || in_pos > expected_in_size)
279 		return "--- %";
280 
281 	// Never show 100.0 % before we actually are finished.
282 	double percentage = (double)(in_pos) / (double)(expected_in_size)
283 			* 99.9;
284 
285 	// Use big enough buffer to hold e.g. a multibyte decimal point.
286 	static char buf[16];
287 	snprintf(buf, sizeof(buf), "%.1f %%", percentage);
288 
289 	return buf;
290 }
291 
292 
293 /// Make the string containing the amount of input processed, amount of
294 /// output produced, and the compression ratio.
295 static const char *
296 progress_sizes(uint64_t compressed_pos, uint64_t uncompressed_pos, bool final)
297 {
298 	// Use big enough buffer to hold e.g. a multibyte thousand separators.
299 	static char buf[128];
300 	char *pos = buf;
301 	size_t left = sizeof(buf);
302 
303 	// Print the sizes. If this the final message, use more reasonable
304 	// units than MiB if the file was small.
305 	const enum nicestr_unit unit_min = final ? NICESTR_B : NICESTR_MIB;
306 	my_snprintf(&pos, &left, "%s / %s",
307 			uint64_to_nicestr(compressed_pos,
308 				unit_min, NICESTR_TIB, false, 0),
309 			uint64_to_nicestr(uncompressed_pos,
310 				unit_min, NICESTR_TIB, false, 1));
311 
312 	// Avoid division by zero. If we cannot calculate the ratio, set
313 	// it to some nice number greater than 10.0 so that it gets caught
314 	// in the next if-clause.
315 	const double ratio = uncompressed_pos > 0
316 			? (double)(compressed_pos) / (double)(uncompressed_pos)
317 			: 16.0;
318 
319 	// If the ratio is very bad, just indicate that it is greater than
320 	// 9.999. This way the length of the ratio field stays fixed.
321 	if (ratio > 9.999)
322 		snprintf(pos, left, " > %.3f", 9.999);
323 	else
324 		snprintf(pos, left, " = %.3f", ratio);
325 
326 	return buf;
327 }
328 
329 
330 /// Make the string containing the processing speed of uncompressed data.
331 static const char *
332 progress_speed(uint64_t uncompressed_pos, uint64_t elapsed)
333 {
334 	// Don't print the speed immediately, since the early values look
335 	// somewhat random.
336 	if (elapsed < 3000)
337 		return "";
338 
339 	// The first character of KiB/s, MiB/s, or GiB/s:
340 	static const char unit[] = { 'K', 'M', 'G' };
341 
342 	size_t unit_index = 0;
343 
344 	// Calculate the speed as KiB/s.
345 	double speed = (double)(uncompressed_pos)
346 			/ ((double)(elapsed) * (1024.0 / 1000.0));
347 
348 	// Adjust the unit of the speed if needed.
349 	while (speed > 999.0) {
350 		speed /= 1024.0;
351 		if (++unit_index == ARRAY_SIZE(unit))
352 			return ""; // Way too fast ;-)
353 	}
354 
355 	// Use decimal point only if the number is small. Examples:
356 	//  - 0.1 KiB/s
357 	//  - 9.9 KiB/s
358 	//  - 99 KiB/s
359 	//  - 999 KiB/s
360 	// Use big enough buffer to hold e.g. a multibyte decimal point.
361 	static char buf[16];
362 	snprintf(buf, sizeof(buf), "%.*f %ciB/s",
363 			speed > 9.9 ? 0 : 1, speed, unit[unit_index]);
364 	return buf;
365 }
366 
367 
368 /// Make a string indicating elapsed time. The format is either
369 /// M:SS or H:MM:SS depending on if the time is an hour or more.
370 static const char *
371 progress_time(uint64_t mseconds)
372 {
373 	// 9999 hours = 416 days
374 	static char buf[sizeof("9999:59:59")];
375 
376 	// 32-bit variable is enough for elapsed time (136 years).
377 	uint32_t seconds = (uint32_t)(mseconds / 1000);
378 
379 	// Don't show anything if the time is zero or ridiculously big.
380 	if (seconds == 0 || seconds > ((9999 * 60) + 59) * 60 + 59)
381 		return "";
382 
383 	uint32_t minutes = seconds / 60;
384 	seconds %= 60;
385 
386 	if (minutes >= 60) {
387 		const uint32_t hours = minutes / 60;
388 		minutes %= 60;
389 		snprintf(buf, sizeof(buf),
390 				"%" PRIu32 ":%02" PRIu32 ":%02" PRIu32,
391 				hours, minutes, seconds);
392 	} else {
393 		snprintf(buf, sizeof(buf), "%" PRIu32 ":%02" PRIu32,
394 				minutes, seconds);
395 	}
396 
397 	return buf;
398 }
399 
400 
401 /// Return a string containing estimated remaining time when
402 /// reasonably possible.
403 static const char *
404 progress_remaining(uint64_t in_pos, uint64_t elapsed)
405 {
406 	// Don't show the estimated remaining time when it wouldn't
407 	// make sense:
408 	//  - Input size is unknown.
409 	//  - Input has grown bigger since we started (de)compressing.
410 	//  - We haven't processed much data yet, so estimate would be
411 	//    too inaccurate.
412 	//  - Only a few seconds has passed since we started (de)compressing,
413 	//    so estimate would be too inaccurate.
414 	if (expected_in_size == 0 || in_pos > expected_in_size
415 			|| in_pos < (UINT64_C(1) << 19) || elapsed < 8000)
416 		return "";
417 
418 	// Calculate the estimate. Don't give an estimate of zero seconds,
419 	// since it is possible that all the input has been already passed
420 	// to the library, but there is still quite a bit of output pending.
421 	uint32_t remaining = (uint32_t)((double)(expected_in_size - in_pos)
422 			* ((double)(elapsed) / 1000.0) / (double)(in_pos));
423 	if (remaining < 1)
424 		remaining = 1;
425 
426 	static char buf[sizeof("9 h 55 min")];
427 
428 	// Select appropriate precision for the estimated remaining time.
429 	if (remaining <= 10) {
430 		// A maximum of 10 seconds remaining.
431 		// Show the number of seconds as is.
432 		snprintf(buf, sizeof(buf), "%" PRIu32 " s", remaining);
433 
434 	} else if (remaining <= 50) {
435 		// A maximum of 50 seconds remaining.
436 		// Round up to the next multiple of five seconds.
437 		remaining = (remaining + 4) / 5 * 5;
438 		snprintf(buf, sizeof(buf), "%" PRIu32 " s", remaining);
439 
440 	} else if (remaining <= 590) {
441 		// A maximum of 9 minutes and 50 seconds remaining.
442 		// Round up to the next multiple of ten seconds.
443 		remaining = (remaining + 9) / 10 * 10;
444 		snprintf(buf, sizeof(buf), "%" PRIu32 " min %" PRIu32 " s",
445 				remaining / 60, remaining % 60);
446 
447 	} else if (remaining <= 59 * 60) {
448 		// A maximum of 59 minutes remaining.
449 		// Round up to the next multiple of a minute.
450 		remaining = (remaining + 59) / 60;
451 		snprintf(buf, sizeof(buf), "%" PRIu32 " min", remaining);
452 
453 	} else if (remaining <= 9 * 3600 + 50 * 60) {
454 		// A maximum of 9 hours and 50 minutes left.
455 		// Round up to the next multiple of ten minutes.
456 		remaining = (remaining + 599) / 600 * 10;
457 		snprintf(buf, sizeof(buf), "%" PRIu32 " h %" PRIu32 " min",
458 				remaining / 60, remaining % 60);
459 
460 	} else if (remaining <= 23 * 3600) {
461 		// A maximum of 23 hours remaining.
462 		// Round up to the next multiple of an hour.
463 		remaining = (remaining + 3599) / 3600;
464 		snprintf(buf, sizeof(buf), "%" PRIu32 " h", remaining);
465 
466 	} else if (remaining <= 9 * 24 * 3600 + 23 * 3600) {
467 		// A maximum of 9 days and 23 hours remaining.
468 		// Round up to the next multiple of an hour.
469 		remaining = (remaining + 3599) / 3600;
470 		snprintf(buf, sizeof(buf), "%" PRIu32 " d %" PRIu32 " h",
471 				remaining / 24, remaining % 24);
472 
473 	} else if (remaining <= 999 * 24 * 3600) {
474 		// A maximum of 999 days remaining. ;-)
475 		// Round up to the next multiple of a day.
476 		remaining = (remaining + 24 * 3600 - 1) / (24 * 3600);
477 		snprintf(buf, sizeof(buf), "%" PRIu32 " d", remaining);
478 
479 	} else {
480 		// The estimated remaining time is too big. Don't show it.
481 		return "";
482 	}
483 
484 	return buf;
485 }
486 
487 
488 /// Get how much uncompressed and compressed data has been processed.
489 static void
490 progress_pos(uint64_t *in_pos,
491 		uint64_t *compressed_pos, uint64_t *uncompressed_pos)
492 {
493 	uint64_t out_pos;
494 	if (progress_is_from_passthru) {
495 		// In passthru mode the progress info is in total_in/out but
496 		// the *progress_strm itself isn't initialized and thus we
497 		// cannot use lzma_get_progress().
498 		*in_pos = progress_strm->total_in;
499 		out_pos = progress_strm->total_out;
500 	} else {
501 		lzma_get_progress(progress_strm, in_pos, &out_pos);
502 	}
503 
504 	// It cannot have processed more input than it has been given.
505 	assert(*in_pos <= progress_strm->total_in);
506 
507 	// It cannot have produced more output than it claims to have ready.
508 	assert(out_pos >= progress_strm->total_out);
509 
510 	if (opt_mode == MODE_COMPRESS) {
511 		*compressed_pos = out_pos;
512 		*uncompressed_pos = *in_pos;
513 	} else {
514 		*compressed_pos = *in_pos;
515 		*uncompressed_pos = out_pos;
516 	}
517 
518 	return;
519 }
520 
521 
522 extern void
523 message_progress_update(void)
524 {
525 	if (!progress_needs_updating)
526 		return;
527 
528 	// Calculate how long we have been processing this file.
529 	const uint64_t elapsed = mytime_get_elapsed();
530 
531 #ifndef SIGALRM
532 	if (progress_next_update > elapsed)
533 		return;
534 
535 	progress_next_update = elapsed + 1000;
536 #endif
537 
538 	// Get our current position in the stream.
539 	uint64_t in_pos;
540 	uint64_t compressed_pos;
541 	uint64_t uncompressed_pos;
542 	progress_pos(&in_pos, &compressed_pos, &uncompressed_pos);
543 
544 	// Block signals so that fprintf() doesn't get interrupted.
545 	signals_block();
546 
547 	// Print the filename if it hasn't been printed yet.
548 	if (!current_filename_printed)
549 		print_filename();
550 
551 	// Print the actual progress message. The idea is that there is at
552 	// least three spaces between the fields in typical situations, but
553 	// even in rare situations there is at least one space.
554 	const char *cols[5] = {
555 		progress_percentage(in_pos),
556 		progress_sizes(compressed_pos, uncompressed_pos, false),
557 		progress_speed(uncompressed_pos, elapsed),
558 		progress_time(elapsed),
559 		progress_remaining(in_pos, elapsed),
560 	};
561 	fprintf(stderr, "\r %*s %*s   %*s %10s   %10s\r",
562 			tuklib_mbstr_fw(cols[0], 6), cols[0],
563 			tuklib_mbstr_fw(cols[1], 35), cols[1],
564 			tuklib_mbstr_fw(cols[2], 9), cols[2],
565 			cols[3],
566 			cols[4]);
567 
568 #ifdef SIGALRM
569 	// Updating the progress info was finished. Reset
570 	// progress_needs_updating to wait for the next SIGALRM.
571 	//
572 	// NOTE: This has to be done before alarm(1) or with (very) bad
573 	// luck we could be setting this to false after the alarm has already
574 	// been triggered.
575 	progress_needs_updating = false;
576 
577 	if (verbosity >= V_VERBOSE && progress_automatic) {
578 		// Mark that the progress indicator is active, so if an error
579 		// occurs, the error message gets printed cleanly.
580 		progress_active = true;
581 
582 		// Restart the timer so that progress_needs_updating gets
583 		// set to true after about one second.
584 		alarm(1);
585 	} else {
586 		// The progress message was printed because user had sent us
587 		// SIGALRM. In this case, each progress message is printed
588 		// on its own line.
589 		fputc('\n', stderr);
590 	}
591 #else
592 	// When SIGALRM isn't supported and we get here, it's always due to
593 	// automatic progress update. We set progress_active here too like
594 	// described above.
595 	assert(verbosity >= V_VERBOSE);
596 	assert(progress_automatic);
597 	progress_active = true;
598 #endif
599 
600 	signals_unblock();
601 
602 	return;
603 }
604 
605 
606 static void
607 progress_flush(bool finished)
608 {
609 	if (!progress_started || verbosity < V_VERBOSE)
610 		return;
611 
612 	uint64_t in_pos;
613 	uint64_t compressed_pos;
614 	uint64_t uncompressed_pos;
615 	progress_pos(&in_pos, &compressed_pos, &uncompressed_pos);
616 
617 	// Avoid printing intermediate progress info if some error occurs
618 	// in the beginning of the stream. (If something goes wrong later in
619 	// the stream, it is sometimes useful to tell the user where the
620 	// error approximately occurred, especially if the error occurs
621 	// after a time-consuming operation.)
622 	if (!finished && !progress_active
623 			&& (compressed_pos == 0 || uncompressed_pos == 0))
624 		return;
625 
626 	progress_active = false;
627 
628 	const uint64_t elapsed = mytime_get_elapsed();
629 
630 	signals_block();
631 
632 	// When using the auto-updating progress indicator, the final
633 	// statistics are printed in the same format as the progress
634 	// indicator itself.
635 	if (progress_automatic) {
636 		const char *cols[5] = {
637 			finished ? "100 %" : progress_percentage(in_pos),
638 			progress_sizes(compressed_pos, uncompressed_pos, true),
639 			progress_speed(uncompressed_pos, elapsed),
640 			progress_time(elapsed),
641 			finished ? "" : progress_remaining(in_pos, elapsed),
642 		};
643 		fprintf(stderr, "\r %*s %*s   %*s %10s   %10s\n",
644 				tuklib_mbstr_fw(cols[0], 6), cols[0],
645 				tuklib_mbstr_fw(cols[1], 35), cols[1],
646 				tuklib_mbstr_fw(cols[2], 9), cols[2],
647 				cols[3],
648 				cols[4]);
649 	} else {
650 		// The filename is always printed.
651 		fprintf(stderr, _("%s: "), filename);
652 
653 		// Percentage is printed only if we didn't finish yet.
654 		if (!finished) {
655 			// Don't print the percentage when it isn't known
656 			// (starts with a dash).
657 			const char *percentage = progress_percentage(in_pos);
658 			if (percentage[0] != '-')
659 				fprintf(stderr, "%s, ", percentage);
660 		}
661 
662 		// Size information is always printed.
663 		fprintf(stderr, "%s", progress_sizes(
664 				compressed_pos, uncompressed_pos, true));
665 
666 		// The speed and elapsed time aren't always shown.
667 		const char *speed = progress_speed(uncompressed_pos, elapsed);
668 		if (speed[0] != '\0')
669 			fprintf(stderr, ", %s", speed);
670 
671 		const char *elapsed_str = progress_time(elapsed);
672 		if (elapsed_str[0] != '\0')
673 			fprintf(stderr, ", %s", elapsed_str);
674 
675 		fputc('\n', stderr);
676 	}
677 
678 	signals_unblock();
679 
680 	return;
681 }
682 
683 
684 extern void
685 message_progress_end(bool success)
686 {
687 	assert(progress_started);
688 	progress_flush(success);
689 	progress_started = false;
690 	return;
691 }
692 
693 
694 static void
695 vmessage(enum message_verbosity v, const char *fmt, va_list ap)
696 {
697 	if (v <= verbosity) {
698 		signals_block();
699 
700 		progress_flush(false);
701 
702 		// TRANSLATORS: This is the program name in the beginning
703 		// of the line in messages. Usually it becomes "xz: ".
704 		// This is a translatable string because French needs
705 		// a space before a colon.
706 		fprintf(stderr, _("%s: "), progname);
707 
708 #ifdef __clang__
709 #	pragma GCC diagnostic push
710 #	pragma GCC diagnostic ignored "-Wformat-nonliteral"
711 #endif
712 		vfprintf(stderr, fmt, ap);
713 #ifdef __clang__
714 #	pragma GCC diagnostic pop
715 #endif
716 
717 		fputc('\n', stderr);
718 
719 		signals_unblock();
720 	}
721 
722 	return;
723 }
724 
725 
726 extern void
727 message(enum message_verbosity v, const char *fmt, ...)
728 {
729 	va_list ap;
730 	va_start(ap, fmt);
731 	vmessage(v, fmt, ap);
732 	va_end(ap);
733 	return;
734 }
735 
736 
737 extern void
738 message_warning(const char *fmt, ...)
739 {
740 	va_list ap;
741 	va_start(ap, fmt);
742 	vmessage(V_WARNING, fmt, ap);
743 	va_end(ap);
744 
745 	set_exit_status(E_WARNING);
746 	return;
747 }
748 
749 
750 extern void
751 message_error(const char *fmt, ...)
752 {
753 	va_list ap;
754 	va_start(ap, fmt);
755 	vmessage(V_ERROR, fmt, ap);
756 	va_end(ap);
757 
758 	set_exit_status(E_ERROR);
759 	return;
760 }
761 
762 
763 extern void
764 message_fatal(const char *fmt, ...)
765 {
766 	va_list ap;
767 	va_start(ap, fmt);
768 	vmessage(V_ERROR, fmt, ap);
769 	va_end(ap);
770 
771 	tuklib_exit(E_ERROR, E_ERROR, false);
772 }
773 
774 
775 extern void
776 message_bug(void)
777 {
778 	message_fatal(_("Internal error (bug)"));
779 }
780 
781 
782 extern void
783 message_signal_handler(void)
784 {
785 	message_fatal(_("Cannot establish signal handlers"));
786 }
787 
788 
789 extern const char *
790 message_strm(lzma_ret code)
791 {
792 	switch (code) {
793 	case LZMA_NO_CHECK:
794 		return _("No integrity check; not verifying file integrity");
795 
796 	case LZMA_UNSUPPORTED_CHECK:
797 		return _("Unsupported type of integrity check; "
798 				"not verifying file integrity");
799 
800 	case LZMA_MEM_ERROR:
801 		return strerror(ENOMEM);
802 
803 	case LZMA_MEMLIMIT_ERROR:
804 		return _("Memory usage limit reached");
805 
806 	case LZMA_FORMAT_ERROR:
807 		return _("File format not recognized");
808 
809 	case LZMA_OPTIONS_ERROR:
810 		return _("Unsupported options");
811 
812 	case LZMA_DATA_ERROR:
813 		return _("Compressed data is corrupt");
814 
815 	case LZMA_BUF_ERROR:
816 		return _("Unexpected end of input");
817 
818 	case LZMA_OK:
819 	case LZMA_STREAM_END:
820 	case LZMA_GET_CHECK:
821 	case LZMA_PROG_ERROR:
822 	case LZMA_SEEK_NEEDED:
823 	case LZMA_RET_INTERNAL1:
824 	case LZMA_RET_INTERNAL2:
825 	case LZMA_RET_INTERNAL3:
826 	case LZMA_RET_INTERNAL4:
827 	case LZMA_RET_INTERNAL5:
828 	case LZMA_RET_INTERNAL6:
829 	case LZMA_RET_INTERNAL7:
830 	case LZMA_RET_INTERNAL8:
831 		// Without "default", compiler will warn if new constants
832 		// are added to lzma_ret, it is not too easy to forget to
833 		// add the new constants to this function.
834 		break;
835 	}
836 
837 	return _("Internal error (bug)");
838 }
839 
840 
841 extern void
842 message_mem_needed(enum message_verbosity v, uint64_t memusage)
843 {
844 	if (v > verbosity)
845 		return;
846 
847 	// Convert memusage to MiB, rounding up to the next full MiB.
848 	// This way the user can always use the displayed usage as
849 	// the new memory usage limit. (If we rounded to the nearest,
850 	// the user might need to +1 MiB to get high enough limit.)
851 	memusage = round_up_to_mib(memusage);
852 
853 	uint64_t memlimit = hardware_memlimit_get(opt_mode);
854 
855 	// Handle the case when there is no memory usage limit.
856 	// This way we don't print a weird message with a huge number.
857 	if (memlimit == UINT64_MAX) {
858 		message(v, _("%s MiB of memory is required. "
859 				"The limiter is disabled."),
860 				uint64_to_str(memusage, 0));
861 		return;
862 	}
863 
864 	// With US-ASCII:
865 	// 2^64 with thousand separators + " MiB" suffix + '\0' = 26 + 4 + 1
866 	// But there may be multibyte chars so reserve enough space.
867 	char memlimitstr[128];
868 
869 	// Show the memory usage limit as MiB unless it is less than 1 MiB.
870 	// This way it's easy to notice errors where one has typed
871 	// --memory=123 instead of --memory=123MiB.
872 	if (memlimit < (UINT32_C(1) << 20)) {
873 		snprintf(memlimitstr, sizeof(memlimitstr), "%s B",
874 				uint64_to_str(memlimit, 1));
875 	} else {
876 		// Round up just like with memusage. If this function is
877 		// called for informational purposes (to just show the
878 		// current usage and limit), we should never show that
879 		// the usage is higher than the limit, which would give
880 		// a false impression that the memory usage limit isn't
881 		// properly enforced.
882 		snprintf(memlimitstr, sizeof(memlimitstr), "%s MiB",
883 				uint64_to_str(round_up_to_mib(memlimit), 1));
884 	}
885 
886 	message(v, _("%s MiB of memory is required. The limit is %s."),
887 			uint64_to_str(memusage, 0), memlimitstr);
888 
889 	return;
890 }
891 
892 
893 extern void
894 message_filters_show(enum message_verbosity v, const lzma_filter *filters)
895 {
896 	if (v > verbosity)
897 		return;
898 
899 	char *buf;
900 	const lzma_ret ret = lzma_str_from_filters(&buf, filters,
901 			LZMA_STR_ENCODER | LZMA_STR_GETOPT_LONG, NULL);
902 	if (ret != LZMA_OK)
903 		message_fatal("%s", message_strm(ret));
904 
905 	fprintf(stderr, _("%s: Filter chain: %s\n"), progname, buf);
906 	free(buf);
907 	return;
908 }
909 
910 
911 extern void
912 message_try_help(void)
913 {
914 	// Print this with V_WARNING instead of V_ERROR to prevent it from
915 	// showing up when --quiet has been specified.
916 	message(V_WARNING, _("Try '%s --help' for more information."),
917 			progname);
918 	return;
919 }
920 
921 
922 extern void
923 message_version(void)
924 {
925 	// It is possible that liblzma version is different than the command
926 	// line tool version, so print both.
927 	if (opt_robot) {
928 		printf("XZ_VERSION=%" PRIu32 "\nLIBLZMA_VERSION=%" PRIu32 "\n",
929 				LZMA_VERSION, lzma_version_number());
930 	} else {
931 		printf("xz (" PACKAGE_NAME ") " LZMA_VERSION_STRING "\n");
932 		printf("liblzma %s\n", lzma_version_string());
933 	}
934 
935 	tuklib_exit(E_SUCCESS, E_ERROR, verbosity != V_SILENT);
936 }
937 
938 
939 extern void
940 message_help(bool long_help)
941 {
942 	printf(_("Usage: %s [OPTION]... [FILE]...\n"
943 			"Compress or decompress FILEs in the .xz format.\n\n"),
944 			progname);
945 
946 	// NOTE: The short help doesn't currently have options that
947 	// take arguments.
948 	if (long_help)
949 		puts(_("Mandatory arguments to long options are mandatory "
950 				"for short options too.\n"));
951 
952 	if (long_help)
953 		puts(_(" Operation mode:\n"));
954 
955 	puts(_(
956 "  -z, --compress      force compression\n"
957 "  -d, --decompress    force decompression\n"
958 "  -t, --test          test compressed file integrity\n"
959 "  -l, --list          list information about .xz files"));
960 
961 	if (long_help)
962 		puts(_("\n Operation modifiers:\n"));
963 
964 	puts(_(
965 "  -k, --keep          keep (don't delete) input files\n"
966 "  -f, --force         force overwrite of output file and (de)compress links\n"
967 "  -c, --stdout        write to standard output and don't delete input files"));
968 	// NOTE: --to-stdout isn't included above because it's not
969 	// the recommended spelling. It was copied from gzip but other
970 	// compressors with gzip-like syntax don't support it.
971 
972 	if (long_help) {
973 		puts(_(
974 "      --single-stream decompress only the first stream, and silently\n"
975 "                      ignore possible remaining input data"));
976 		puts(_(
977 "      --no-sparse     do not create sparse files when decompressing\n"
978 "  -S, --suffix=.SUF   use the suffix '.SUF' on compressed files\n"
979 "      --files[=FILE]  read filenames to process from FILE; if FILE is\n"
980 "                      omitted, filenames are read from the standard input;\n"
981 "                      filenames must be terminated with the newline character\n"
982 "      --files0[=FILE] like --files but use the null character as terminator"));
983 	}
984 
985 	if (long_help) {
986 		puts(_("\n Basic file format and compression options:\n"));
987 		puts(_(
988 "  -F, --format=FMT    file format to encode or decode; possible values are\n"
989 "                      'auto' (default), 'xz', 'lzma', 'lzip', and 'raw'\n"
990 "  -C, --check=CHECK   integrity check type: 'none' (use with caution),\n"
991 "                      'crc32', 'crc64' (default), or 'sha256'"));
992 		puts(_(
993 "      --ignore-check  don't verify the integrity check when decompressing"));
994 	}
995 
996 	puts(_(
997 "  -0 ... -9           compression preset; default is 6; take compressor *and*\n"
998 "                      decompressor memory usage into account before using 7-9!"));
999 
1000 	puts(_(
1001 "  -e, --extreme       try to improve compression ratio by using more CPU time;\n"
1002 "                      does not affect decompressor memory requirements"));
1003 
1004 	puts(_(
1005 "  -T, --threads=NUM   use at most NUM threads; the default is 0 which uses\n"
1006 "                      as many threads as there are processor cores"));
1007 
1008 	if (long_help) {
1009 		puts(_(
1010 "      --block-size=SIZE\n"
1011 "                      start a new .xz block after every SIZE bytes of input;\n"
1012 "                      use this to set the block size for threaded compression"));
1013 		puts(_(
1014 "      --block-list=BLOCKS\n"
1015 "                      start a new .xz block after the given comma-separated\n"
1016 "                      intervals of uncompressed data; optionally, specify a\n"
1017 "                      filter chain number (0-9) followed by a ':' before the\n"
1018 "                      uncompressed data size"));
1019 		puts(_(
1020 "      --flush-timeout=TIMEOUT\n"
1021 "                      when compressing, if more than TIMEOUT milliseconds has\n"
1022 "                      passed since the previous flush and reading more input\n"
1023 "                      would block, all pending data is flushed out"
1024 		));
1025 		puts(_( // xgettext:no-c-format
1026 "      --memlimit-compress=LIMIT\n"
1027 "      --memlimit-decompress=LIMIT\n"
1028 "      --memlimit-mt-decompress=LIMIT\n"
1029 "  -M, --memlimit=LIMIT\n"
1030 "                      set memory usage limit for compression, decompression,\n"
1031 "                      threaded decompression, or all of these; LIMIT is in\n"
1032 "                      bytes, % of RAM, or 0 for defaults"));
1033 
1034 		puts(_(
1035 "      --no-adjust     if compression settings exceed the memory usage limit,\n"
1036 "                      give an error instead of adjusting the settings downwards"));
1037 	}
1038 
1039 	if (long_help) {
1040 		puts(_(
1041 "\n Custom filter chain for compression (alternative for using presets):"));
1042 
1043 		puts(_(
1044 "\n"
1045 "  --filters=FILTERS   set the filter chain using the liblzma filter string\n"
1046 "                      syntax; use --filters-help for more information"
1047 		));
1048 
1049 		puts(_(
1050 "  --filters1=FILTERS ... --filters9=FILTERS\n"
1051 "                      set additional filter chains using the liblzma filter\n"
1052 "                      string syntax to use with --block-list"
1053 		));
1054 
1055 		puts(_(
1056 "  --filters-help      display more information about the liblzma filter string\n"
1057 "                      syntax and exit."
1058 		));
1059 
1060 #if defined(HAVE_ENCODER_LZMA1) || defined(HAVE_DECODER_LZMA1) \
1061 		|| defined(HAVE_ENCODER_LZMA2) || defined(HAVE_DECODER_LZMA2)
1062 		// TRANSLATORS: The word "literal" in "literal context bits"
1063 		// means how many "context bits" to use when encoding
1064 		// literals. A literal is a single 8-bit byte. It doesn't
1065 		// mean "literally" here.
1066 		puts(_(
1067 "\n"
1068 "  --lzma1[=OPTS]      LZMA1 or LZMA2; OPTS is a comma-separated list of zero or\n"
1069 "  --lzma2[=OPTS]      more of the following options (valid values; default):\n"
1070 "                        preset=PRE reset options to a preset (0-9[e])\n"
1071 "                        dict=NUM   dictionary size (4KiB - 1536MiB; 8MiB)\n"
1072 "                        lc=NUM     number of literal context bits (0-4; 3)\n"
1073 "                        lp=NUM     number of literal position bits (0-4; 0)\n"
1074 "                        pb=NUM     number of position bits (0-4; 2)\n"
1075 "                        mode=MODE  compression mode (fast, normal; normal)\n"
1076 "                        nice=NUM   nice length of a match (2-273; 64)\n"
1077 "                        mf=NAME    match finder (hc3, hc4, bt2, bt3, bt4; bt4)\n"
1078 "                        depth=NUM  maximum search depth; 0=automatic (default)"));
1079 #endif
1080 
1081 		puts(_(
1082 "\n"
1083 "  --x86[=OPTS]        x86 BCJ filter (32-bit and 64-bit)\n"
1084 "  --arm[=OPTS]        ARM BCJ filter\n"
1085 "  --armthumb[=OPTS]   ARM-Thumb BCJ filter\n"
1086 "  --arm64[=OPTS]      ARM64 BCJ filter\n"
1087 "  --powerpc[=OPTS]    PowerPC BCJ filter (big endian only)\n"
1088 "  --ia64[=OPTS]       IA-64 (Itanium) BCJ filter\n"
1089 "  --sparc[=OPTS]      SPARC BCJ filter\n"
1090 "  --riscv[=OPTS]      RISC-V BCJ filter\n"
1091 "                      Valid OPTS for all BCJ filters:\n"
1092 "                        start=NUM  start offset for conversions (default=0)"));
1093 
1094 #if defined(HAVE_ENCODER_DELTA) || defined(HAVE_DECODER_DELTA)
1095 		puts(_(
1096 "\n"
1097 "  --delta[=OPTS]      Delta filter; valid OPTS (valid values; default):\n"
1098 "                        dist=NUM   distance between bytes being subtracted\n"
1099 "                                   from each other (1-256; 1)"));
1100 #endif
1101 	}
1102 
1103 	if (long_help)
1104 		puts(_("\n Other options:\n"));
1105 
1106 	puts(_(
1107 "  -q, --quiet         suppress warnings; specify twice to suppress errors too\n"
1108 "  -v, --verbose       be verbose; specify twice for even more verbose"));
1109 
1110 	if (long_help) {
1111 		puts(_(
1112 "  -Q, --no-warn       make warnings not affect the exit status"));
1113 		puts(_(
1114 "      --robot         use machine-parsable messages (useful for scripts)"));
1115 		puts("");
1116 		puts(_(
1117 "      --info-memory   display the total amount of RAM and the currently active\n"
1118 "                      memory usage limits, and exit"));
1119 		puts(_(
1120 "  -h, --help          display the short help (lists only the basic options)\n"
1121 "  -H, --long-help     display this long help and exit"));
1122 	} else {
1123 		puts(_(
1124 "  -h, --help          display this short help and exit\n"
1125 "  -H, --long-help     display the long help (lists also the advanced options)"));
1126 	}
1127 
1128 	puts(_(
1129 "  -V, --version       display the version number and exit"));
1130 
1131 	puts(_("\nWith no FILE, or when FILE is -, read standard input.\n"));
1132 
1133 	// TRANSLATORS: This message indicates the bug reporting address
1134 	// for this package. Please add _another line_ saying
1135 	// "Report translation bugs to <...>\n" with the email or WWW
1136 	// address for translation bugs. Thanks.
1137 	printf(_("Report bugs to <%s> (in English or Finnish).\n"),
1138 			PACKAGE_BUGREPORT);
1139 	printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
1140 
1141 #if LZMA_VERSION_STABILITY != LZMA_VERSION_STABILITY_STABLE
1142 	puts(_(
1143 "THIS IS A DEVELOPMENT VERSION NOT INTENDED FOR PRODUCTION USE."));
1144 #endif
1145 
1146 	tuklib_exit(E_SUCCESS, E_ERROR, verbosity != V_SILENT);
1147 }
1148 
1149 
1150 extern void
1151 message_filters_help(void)
1152 {
1153 	char *encoder_options;
1154 	if (lzma_str_list_filters(&encoder_options, LZMA_VLI_UNKNOWN,
1155 			LZMA_STR_ENCODER, NULL) != LZMA_OK)
1156 		message_bug();
1157 
1158 	if (!opt_robot) {
1159 		puts(_(
1160 "Filter chains are set using the --filters=FILTERS or\n"
1161 "--filters1=FILTERS ... --filters9=FILTERS options. Each filter in the chain\n"
1162 "can be separated by spaces or '--'. Alternatively a preset <0-9>[e] can be\n"
1163 "specified instead of a filter chain.\n"
1164 		));
1165 
1166 		puts(_("The supported filters and their options are:"));
1167 	}
1168 
1169 	puts(encoder_options);
1170 
1171 	tuklib_exit(E_SUCCESS, E_ERROR, verbosity != V_SILENT);
1172 }
1173