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