GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / tools / misc / xz / src / xz / message.c
blob80c86a49c3dbd839ae4077d6cc42b098a80f593d
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.
11 ///////////////////////////////////////////////////////////////////////////////
13 #include "private.h"
15 #ifdef HAVE_SYS_TIME_H
16 # include <sys/time.h>
17 #endif
19 #include <stdarg.h>
22 /// Number of the current file
23 static unsigned int files_pos = 0;
25 /// Total number of input files; zero if unknown.
26 static unsigned int files_total;
28 /// Verbosity level
29 static enum message_verbosity verbosity = V_WARNING;
31 /// Filename which we will print with the verbose messages
32 static const char *filename;
34 /// True once the a filename has been printed to stderr as part of progress
35 /// message. If automatic progress updating isn't enabled, this becomes true
36 /// after the first progress message has been printed due to user sending
37 /// SIGINFO, SIGUSR1, or SIGALRM. Once this variable is true, we will print
38 /// an empty line before the next filename to make the output more readable.
39 static bool first_filename_printed = false;
41 /// This is set to true when we have printed the current filename to stderr
42 /// as part of a progress message. This variable is useful only if not
43 /// updating progress automatically: if user sends many SIGINFO, SIGUSR1, or
44 /// SIGALRM signals, we won't print the name of the same file multiple times.
45 static bool current_filename_printed = false;
47 /// True if we should print progress indicator and update it automatically
48 /// if also verbose >= V_VERBOSE.
49 static bool progress_automatic;
51 /// True if message_progress_start() has been called but
52 /// message_progress_end() hasn't been called yet.
53 static bool progress_started = false;
55 /// This is true when a progress message was printed and the cursor is still
56 /// on the same line with the progress message. In that case, a newline has
57 /// to be printed before any error messages.
58 static bool progress_active = false;
60 /// Pointer to lzma_stream used to do the encoding or decoding.
61 static lzma_stream *progress_strm;
63 /// Expected size of the input stream is needed to show completion percentage
64 /// and estimate remaining time.
65 static uint64_t expected_in_size;
67 /// Time when we started processing the file
68 static uint64_t start_time;
71 // Use alarm() and SIGALRM when they are supported. This has two minor
72 // advantages over the alternative of polling gettimeofday():
73 // - It is possible for the user to send SIGINFO, SIGUSR1, or SIGALRM to
74 // get intermediate progress information even when --verbose wasn't used
75 // or stderr is not a terminal.
76 // - alarm() + SIGALRM seems to have slightly less overhead than polling
77 // gettimeofday().
78 #ifdef SIGALRM
80 const int message_progress_sigs[] = {
81 SIGALRM,
82 #ifdef SIGINFO
83 SIGINFO,
84 #endif
85 #ifdef SIGUSR1
86 SIGUSR1,
87 #endif
91 /// The signal handler for SIGALRM sets this to true. It is set back to false
92 /// once the progress message has been updated.
93 static volatile sig_atomic_t progress_needs_updating = false;
95 /// Signal handler for SIGALRM
96 static void
97 progress_signal_handler(int sig lzma_attribute((__unused__)))
99 progress_needs_updating = true;
100 return;
103 #else
105 /// This is true when progress message printing is wanted. Using the same
106 /// variable name as above to avoid some ifdefs.
107 static bool progress_needs_updating = false;
109 /// Elapsed time when the next progress message update should be done.
110 static uint64_t progress_next_update;
112 #endif
115 /// Get the current time as microseconds since epoch
116 static uint64_t
117 my_time(void)
119 struct timeval tv;
120 gettimeofday(&tv, NULL);
121 return (uint64_t)(tv.tv_sec) * UINT64_C(1000000) + tv.tv_usec;
125 extern void
126 message_init(void)
128 // If --verbose is used, we use a progress indicator if and only
129 // if stderr is a terminal. If stderr is not a terminal, we print
130 // verbose information only after finishing the file. As a special
131 // exception, even if --verbose was not used, user can send SIGALRM
132 // to make us print progress information once without automatic
133 // updating.
134 progress_automatic = isatty(STDERR_FILENO);
136 // Commented out because COLUMNS is rarely exported to environment.
137 // Most users have at least 80 columns anyway, let's think something
138 // fancy here if enough people complain.
140 if (progress_automatic) {
141 // stderr is a terminal. Check the COLUMNS environment
142 // variable to see if the terminal is wide enough. If COLUMNS
143 // doesn't exist or it has some unparsable value, we assume
144 // that the terminal is wide enough.
145 const char *columns_str = getenv("COLUMNS");
146 if (columns_str != NULL) {
147 char *endptr;
148 const long columns = strtol(columns_str, &endptr, 10);
149 if (*endptr != '\0' || columns < 80)
150 progress_automatic = false;
155 #ifdef SIGALRM
156 // Establish the signal handlers which set a flag to tell us that
157 // progress info should be updated.
158 struct sigaction sa;
159 sigemptyset(&sa.sa_mask);
160 sa.sa_flags = 0;
161 sa.sa_handler = &progress_signal_handler;
163 for (size_t i = 0; message_progress_sigs[i] != 0; ++i)
164 if (sigaction(message_progress_sigs[i], &sa, NULL))
165 message_signal_handler();
166 #endif
168 return;
172 extern void
173 message_verbosity_increase(void)
175 if (verbosity < V_DEBUG)
176 ++verbosity;
178 return;
182 extern void
183 message_verbosity_decrease(void)
185 if (verbosity > V_SILENT)
186 --verbosity;
188 return;
192 extern enum message_verbosity
193 message_verbosity_get(void)
195 return verbosity;
199 extern void
200 message_set_files(unsigned int files)
202 files_total = files;
203 return;
207 /// Prints the name of the current file if it hasn't been printed already,
208 /// except if we are processing exactly one stream from stdin to stdout.
209 /// I think it looks nicer to not print "(stdin)" when --verbose is used
210 /// in a pipe and no other files are processed.
211 static void
212 print_filename(void)
214 if (files_total != 1 || filename != stdin_filename) {
215 signals_block();
217 FILE *file = opt_mode == MODE_LIST ? stdout : stderr;
219 // If a file was already processed, put an empty line
220 // before the next filename to improve readability.
221 if (first_filename_printed)
222 fputc('\n', file);
224 first_filename_printed = true;
225 current_filename_printed = true;
227 // If we don't know how many files there will be due
228 // to usage of --files or --files0.
229 if (files_total == 0)
230 fprintf(file, "%s (%u)\n", filename,
231 files_pos);
232 else
233 fprintf(file, "%s (%u/%u)\n", filename,
234 files_pos, files_total);
236 signals_unblock();
239 return;
243 extern void
244 message_filename(const char *src_name)
246 // Start numbering the files starting from one.
247 ++files_pos;
248 filename = src_name;
250 if (verbosity >= V_VERBOSE
251 && (progress_automatic || opt_mode == MODE_LIST))
252 print_filename();
253 else
254 current_filename_printed = false;
256 return;
260 extern void
261 message_progress_start(lzma_stream *strm, uint64_t in_size)
263 // Store the pointer to the lzma_stream used to do the coding.
264 // It is needed to find out the position in the stream.
265 progress_strm = strm;
267 // Store the processing start time of the file and its expected size.
268 // If we aren't printing any statistics, then these are unused. But
269 // since it is possible that the user sends us a signal to show
270 // statistics, we need to have these available anyway.
271 start_time = my_time();
272 expected_in_size = in_size;
274 // Indicate that progress info may need to be printed before
275 // printing error messages.
276 progress_started = true;
278 // If progress indicator is wanted, print the filename and possibly
279 // the file count now.
280 if (verbosity >= V_VERBOSE && progress_automatic) {
281 // Start the timer to display the first progress message
282 // after one second. An alternative would be to show the
283 // first message almost immediately, but delaying by one
284 // second looks better to me, since extremely early
285 // progress info is pretty much useless.
286 #ifdef SIGALRM
287 // First disable a possibly existing alarm.
288 alarm(0);
289 progress_needs_updating = false;
290 alarm(1);
291 #else
292 progress_needs_updating = true;
293 progress_next_update = 1000000;
294 #endif
297 return;
301 /// Make the string indicating completion percentage.
302 static const char *
303 progress_percentage(uint64_t in_pos)
305 // If the size of the input file is unknown or the size told us is
306 // clearly wrong since we have processed more data than the alleged
307 // size of the file, show a static string indicating that we have
308 // no idea of the completion percentage.
309 if (expected_in_size == 0 || in_pos > expected_in_size)
310 return "--- %";
312 // Never show 100.0 % before we actually are finished.
313 double percentage = (double)(in_pos) / (double)(expected_in_size)
314 * 99.9;
316 // Use big enough buffer to hold e.g. a multibyte decimal point.
317 static char buf[16];
318 snprintf(buf, sizeof(buf), "%.1f %%", percentage);
320 return buf;
324 /// Make the string containing the amount of input processed, amount of
325 /// output produced, and the compression ratio.
326 static const char *
327 progress_sizes(uint64_t compressed_pos, uint64_t uncompressed_pos, bool final)
329 // Use big enough buffer to hold e.g. a multibyte thousand separators.
330 static char buf[128];
331 char *pos = buf;
332 size_t left = sizeof(buf);
334 // Print the sizes. If this the final message, use more reasonable
335 // units than MiB if the file was small.
336 const enum nicestr_unit unit_min = final ? NICESTR_B : NICESTR_MIB;
337 my_snprintf(&pos, &left, "%s / %s",
338 uint64_to_nicestr(compressed_pos,
339 unit_min, NICESTR_TIB, false, 0),
340 uint64_to_nicestr(uncompressed_pos,
341 unit_min, NICESTR_TIB, false, 1));
343 // Avoid division by zero. If we cannot calculate the ratio, set
344 // it to some nice number greater than 10.0 so that it gets caught
345 // in the next if-clause.
346 const double ratio = uncompressed_pos > 0
347 ? (double)(compressed_pos) / (double)(uncompressed_pos)
348 : 16.0;
350 // If the ratio is very bad, just indicate that it is greater than
351 // 9.999. This way the length of the ratio field stays fixed.
352 if (ratio > 9.999)
353 snprintf(pos, left, " > %.3f", 9.999);
354 else
355 snprintf(pos, left, " = %.3f", ratio);
357 return buf;
361 /// Make the string containing the processing speed of uncompressed data.
362 static const char *
363 progress_speed(uint64_t uncompressed_pos, uint64_t elapsed)
365 // Don't print the speed immediately, since the early values look
366 // somewhat random.
367 if (elapsed < 3000000)
368 return "";
370 static const char unit[][8] = {
371 "KiB/s",
372 "MiB/s",
373 "GiB/s",
376 size_t unit_index = 0;
378 // Calculate the speed as KiB/s.
379 double speed = (double)(uncompressed_pos)
380 / ((double)(elapsed) * (1024.0 / 1e6));
382 // Adjust the unit of the speed if needed.
383 while (speed > 999.0) {
384 speed /= 1024.0;
385 if (++unit_index == ARRAY_SIZE(unit))
386 return ""; // Way too fast ;-)
389 // Use decimal point only if the number is small. Examples:
390 // - 0.1 KiB/s
391 // - 9.9 KiB/s
392 // - 99 KiB/s
393 // - 999 KiB/s
394 // Use big enough buffer to hold e.g. a multibyte decimal point.
395 static char buf[16];
396 snprintf(buf, sizeof(buf), "%.*f %s",
397 speed > 9.9 ? 0 : 1, speed, unit[unit_index]);
398 return buf;
402 /// Make a string indicating elapsed or remaining time. The format is either
403 /// M:SS or H:MM:SS depending on if the time is an hour or more.
404 static const char *
405 progress_time(uint64_t useconds)
407 // 9999 hours = 416 days
408 static char buf[sizeof("9999:59:59")];
410 uint32_t seconds = useconds / 1000000;
412 // Don't show anything if the time is zero or ridiculously big.
413 if (seconds == 0 || seconds > ((9999 * 60) + 59) * 60 + 59)
414 return "";
416 uint32_t minutes = seconds / 60;
417 seconds %= 60;
419 if (minutes >= 60) {
420 const uint32_t hours = minutes / 60;
421 minutes %= 60;
422 snprintf(buf, sizeof(buf),
423 "%" PRIu32 ":%02" PRIu32 ":%02" PRIu32,
424 hours, minutes, seconds);
425 } else {
426 snprintf(buf, sizeof(buf), "%" PRIu32 ":%02" PRIu32,
427 minutes, seconds);
430 return buf;
434 /// Return a string containing estimated remaining time when
435 /// reasonably possible.
436 static const char *
437 progress_remaining(uint64_t in_pos, uint64_t elapsed)
439 // Don't show the estimated remaining time when it wouldn't
440 // make sense:
441 // - Input size is unknown.
442 // - Input has grown bigger since we started (de)compressing.
443 // - We haven't processed much data yet, so estimate would be
444 // too inaccurate.
445 // - Only a few seconds has passed since we started (de)compressing,
446 // so estimate would be too inaccurate.
447 if (expected_in_size == 0 || in_pos > expected_in_size
448 || in_pos < (UINT64_C(1) << 19) || elapsed < 8000000)
449 return "";
451 // Calculate the estimate. Don't give an estimate of zero seconds,
452 // since it is possible that all the input has been already passed
453 // to the library, but there is still quite a bit of output pending.
454 uint32_t remaining = (double)(expected_in_size - in_pos)
455 * ((double)(elapsed) / 1e6) / (double)(in_pos);
456 if (remaining < 1)
457 remaining = 1;
459 static char buf[sizeof("9 h 55 min")];
461 // Select appropriate precision for the estimated remaining time.
462 if (remaining <= 10) {
463 // A maximum of 10 seconds remaining.
464 // Show the number of seconds as is.
465 snprintf(buf, sizeof(buf), "%" PRIu32 " s", remaining);
467 } else if (remaining <= 50) {
468 // A maximum of 50 seconds remaining.
469 // Round up to the next multiple of five seconds.
470 remaining = (remaining + 4) / 5 * 5;
471 snprintf(buf, sizeof(buf), "%" PRIu32 " s", remaining);
473 } else if (remaining <= 590) {
474 // A maximum of 9 minutes and 50 seconds remaining.
475 // Round up to the next multiple of ten seconds.
476 remaining = (remaining + 9) / 10 * 10;
477 snprintf(buf, sizeof(buf), "%" PRIu32 " min %" PRIu32 " s",
478 remaining / 60, remaining % 60);
480 } else if (remaining <= 59 * 60) {
481 // A maximum of 59 minutes remaining.
482 // Round up to the next multiple of a minute.
483 remaining = (remaining + 59) / 60;
484 snprintf(buf, sizeof(buf), "%" PRIu32 " min", remaining);
486 } else if (remaining <= 9 * 3600 + 50 * 60) {
487 // A maximum of 9 hours and 50 minutes left.
488 // Round up to the next multiple of ten minutes.
489 remaining = (remaining + 599) / 600 * 10;
490 snprintf(buf, sizeof(buf), "%" PRIu32 " h %" PRIu32 " min",
491 remaining / 60, remaining % 60);
493 } else if (remaining <= 23 * 3600) {
494 // A maximum of 23 hours remaining.
495 // Round up to the next multiple of an hour.
496 remaining = (remaining + 3599) / 3600;
497 snprintf(buf, sizeof(buf), "%" PRIu32 " h", remaining);
499 } else if (remaining <= 9 * 24 * 3600 + 23 * 3600) {
500 // A maximum of 9 days and 23 hours remaining.
501 // Round up to the next multiple of an hour.
502 remaining = (remaining + 3599) / 3600;
503 snprintf(buf, sizeof(buf), "%" PRIu32 " d %" PRIu32 " h",
504 remaining / 24, remaining % 24);
506 } else if (remaining <= 999 * 24 * 3600) {
507 // A maximum of 999 days remaining. ;-)
508 // Round up to the next multiple of a day.
509 remaining = (remaining + 24 * 3600 - 1) / (24 * 3600);
510 snprintf(buf, sizeof(buf), "%" PRIu32 " d", remaining);
512 } else {
513 // The estimated remaining time is too big. Don't show it.
514 return "";
517 return buf;
521 /// Calculate the elapsed time as microseconds.
522 static uint64_t
523 progress_elapsed(void)
525 return my_time() - start_time;
529 /// Get information about position in the stream. This is currently simple,
530 /// but it will become more complicated once we have multithreading support.
531 static void
532 progress_pos(uint64_t *in_pos,
533 uint64_t *compressed_pos, uint64_t *uncompressed_pos)
535 *in_pos = progress_strm->total_in;
537 if (opt_mode == MODE_COMPRESS) {
538 *compressed_pos = progress_strm->total_out;
539 *uncompressed_pos = progress_strm->total_in;
540 } else {
541 *compressed_pos = progress_strm->total_in;
542 *uncompressed_pos = progress_strm->total_out;
545 return;
549 extern void
550 message_progress_update(void)
552 if (!progress_needs_updating)
553 return;
555 // Calculate how long we have been processing this file.
556 const uint64_t elapsed = progress_elapsed();
558 #ifndef SIGALRM
559 if (progress_next_update > elapsed)
560 return;
562 progress_next_update = elapsed + 1000000;
563 #endif
565 // Get our current position in the stream.
566 uint64_t in_pos;
567 uint64_t compressed_pos;
568 uint64_t uncompressed_pos;
569 progress_pos(&in_pos, &compressed_pos, &uncompressed_pos);
571 // Block signals so that fprintf() doesn't get interrupted.
572 signals_block();
574 // Print the filename if it hasn't been printed yet.
575 if (!current_filename_printed)
576 print_filename();
578 // Print the actual progress message. The idea is that there is at
579 // least three spaces between the fields in typical situations, but
580 // even in rare situations there is at least one space.
581 const char *cols[5] = {
582 progress_percentage(in_pos),
583 progress_sizes(compressed_pos, uncompressed_pos, false),
584 progress_speed(uncompressed_pos, elapsed),
585 progress_time(elapsed),
586 progress_remaining(in_pos, elapsed),
588 fprintf(stderr, "\r %*s %*s %*s %10s %10s\r",
589 tuklib_mbstr_fw(cols[0], 6), cols[0],
590 tuklib_mbstr_fw(cols[1], 35), cols[1],
591 tuklib_mbstr_fw(cols[2], 9), cols[2],
592 cols[3],
593 cols[4]);
595 #ifdef SIGALRM
596 // Updating the progress info was finished. Reset
597 // progress_needs_updating to wait for the next SIGALRM.
599 // NOTE: This has to be done before alarm(1) or with (very) bad
600 // luck we could be setting this to false after the alarm has already
601 // been triggered.
602 progress_needs_updating = false;
604 if (verbosity >= V_VERBOSE && progress_automatic) {
605 // Mark that the progress indicator is active, so if an error
606 // occurs, the error message gets printed cleanly.
607 progress_active = true;
609 // Restart the timer so that progress_needs_updating gets
610 // set to true after about one second.
611 alarm(1);
612 } else {
613 // The progress message was printed because user had sent us
614 // SIGALRM. In this case, each progress message is printed
615 // on its own line.
616 fputc('\n', stderr);
618 #else
619 // When SIGALRM isn't supported and we get here, it's always due to
620 // automatic progress update. We set progress_active here too like
621 // described above.
622 assert(verbosity >= V_VERBOSE);
623 assert(progress_automatic);
624 progress_active = true;
625 #endif
627 signals_unblock();
629 return;
633 static void
634 progress_flush(bool finished)
636 if (!progress_started || verbosity < V_VERBOSE)
637 return;
639 uint64_t in_pos;
640 uint64_t compressed_pos;
641 uint64_t uncompressed_pos;
642 progress_pos(&in_pos, &compressed_pos, &uncompressed_pos);
644 // Avoid printing intermediate progress info if some error occurs
645 // in the beginning of the stream. (If something goes wrong later in
646 // the stream, it is sometimes useful to tell the user where the
647 // error approximately occurred, especially if the error occurs
648 // after a time-consuming operation.)
649 if (!finished && !progress_active
650 && (compressed_pos == 0 || uncompressed_pos == 0))
651 return;
653 progress_active = false;
655 const uint64_t elapsed = progress_elapsed();
657 signals_block();
659 // When using the auto-updating progress indicator, the final
660 // statistics are printed in the same format as the progress
661 // indicator itself.
662 if (progress_automatic) {
663 const char *cols[5] = {
664 finished ? "100 %" : progress_percentage(in_pos),
665 progress_sizes(compressed_pos, uncompressed_pos, true),
666 progress_speed(uncompressed_pos, elapsed),
667 progress_time(elapsed),
668 finished ? "" : progress_remaining(in_pos, elapsed),
670 fprintf(stderr, "\r %*s %*s %*s %10s %10s\n",
671 tuklib_mbstr_fw(cols[0], 6), cols[0],
672 tuklib_mbstr_fw(cols[1], 35), cols[1],
673 tuklib_mbstr_fw(cols[2], 9), cols[2],
674 cols[3],
675 cols[4]);
676 } else {
677 // The filename is always printed.
678 fprintf(stderr, "%s: ", filename);
680 // Percentage is printed only if we didn't finish yet.
681 if (!finished) {
682 // Don't print the percentage when it isn't known
683 // (starts with a dash).
684 const char *percentage = progress_percentage(in_pos);
685 if (percentage[0] != '-')
686 fprintf(stderr, "%s, ", percentage);
689 // Size information is always printed.
690 fprintf(stderr, "%s", progress_sizes(
691 compressed_pos, uncompressed_pos, true));
693 // The speed and elapsed time aren't always shown.
694 const char *speed = progress_speed(uncompressed_pos, elapsed);
695 if (speed[0] != '\0')
696 fprintf(stderr, ", %s", speed);
698 const char *elapsed_str = progress_time(elapsed);
699 if (elapsed_str[0] != '\0')
700 fprintf(stderr, ", %s", elapsed_str);
702 fputc('\n', stderr);
705 signals_unblock();
707 return;
711 extern void
712 message_progress_end(bool success)
714 assert(progress_started);
715 progress_flush(success);
716 progress_started = false;
717 return;
721 static void
722 vmessage(enum message_verbosity v, const char *fmt, va_list ap)
724 if (v <= verbosity) {
725 signals_block();
727 progress_flush(false);
729 // TRANSLATORS: This is the program name in the beginning
730 // of the line in messages. Usually it becomes "xz: ".
731 // This is a translatable string because French needs
732 // a space before a colon.
733 fprintf(stderr, _("%s: "), progname);
734 vfprintf(stderr, fmt, ap);
735 fputc('\n', stderr);
737 signals_unblock();
740 return;
744 extern void
745 message(enum message_verbosity v, const char *fmt, ...)
747 va_list ap;
748 va_start(ap, fmt);
749 vmessage(v, fmt, ap);
750 va_end(ap);
751 return;
755 extern void
756 message_warning(const char *fmt, ...)
758 va_list ap;
759 va_start(ap, fmt);
760 vmessage(V_WARNING, fmt, ap);
761 va_end(ap);
763 set_exit_status(E_WARNING);
764 return;
768 extern void
769 message_error(const char *fmt, ...)
771 va_list ap;
772 va_start(ap, fmt);
773 vmessage(V_ERROR, fmt, ap);
774 va_end(ap);
776 set_exit_status(E_ERROR);
777 return;
781 extern void
782 message_fatal(const char *fmt, ...)
784 va_list ap;
785 va_start(ap, fmt);
786 vmessage(V_ERROR, fmt, ap);
787 va_end(ap);
789 tuklib_exit(E_ERROR, E_ERROR, false);
793 extern void
794 message_bug(void)
796 message_fatal(_("Internal error (bug)"));
800 extern void
801 message_signal_handler(void)
803 message_fatal(_("Cannot establish signal handlers"));
807 extern const char *
808 message_strm(lzma_ret code)
810 switch (code) {
811 case LZMA_NO_CHECK:
812 return _("No integrity check; not verifying file integrity");
814 case LZMA_UNSUPPORTED_CHECK:
815 return _("Unsupported type of integrity check; "
816 "not verifying file integrity");
818 case LZMA_MEM_ERROR:
819 return strerror(ENOMEM);
821 case LZMA_MEMLIMIT_ERROR:
822 return _("Memory usage limit reached");
824 case LZMA_FORMAT_ERROR:
825 return _("File format not recognized");
827 case LZMA_OPTIONS_ERROR:
828 return _("Unsupported options");
830 case LZMA_DATA_ERROR:
831 return _("Compressed data is corrupt");
833 case LZMA_BUF_ERROR:
834 return _("Unexpected end of input");
836 case LZMA_OK:
837 case LZMA_STREAM_END:
838 case LZMA_GET_CHECK:
839 case LZMA_PROG_ERROR:
840 // Without "default", compiler will warn if new constants
841 // are added to lzma_ret, it is not too easy to forget to
842 // add the new constants to this function.
843 break;
846 return _("Internal error (bug)");
850 extern void
851 message_mem_needed(enum message_verbosity v, uint64_t memusage)
853 if (v > verbosity)
854 return;
856 // Convert memusage to MiB, rounding up to the next full MiB.
857 // This way the user can always use the displayed usage as
858 // the new memory usage limit. (If we rounded to the nearest,
859 // the user might need to +1 MiB to get high enough limit.)
860 memusage = round_up_to_mib(memusage);
862 // With US-ASCII:
863 // 2^64 with thousand separators + " MiB" suffix + '\0' = 26 + 4 + 1
864 // But there may be multibyte chars so reserve enough space.
865 char memlimitstr[128];
867 // Show the memory usage limit as MiB unless it is less than 1 MiB.
868 // This way it's easy to notice errors where one has typed
869 // --memory=123 instead of --memory=123MiB.
870 uint64_t memlimit = hardware_memlimit_get(opt_mode);
871 if (memlimit < (UINT32_C(1) << 20)) {
872 snprintf(memlimitstr, sizeof(memlimitstr), "%s B",
873 uint64_to_str(memlimit, 1));
874 } else {
875 // Round up just like with memusage. If this function is
876 // called for informational purposes (to just show the
877 // current usage and limit), we should never show that
878 // the usage is higher than the limit, which would give
879 // a false impression that the memory usage limit isn't
880 // properly enforced.
881 snprintf(memlimitstr, sizeof(memlimitstr), "%s MiB",
882 uint64_to_str(round_up_to_mib(memlimit), 1));
885 message(v, _("%s MiB of memory is required. The limit is %s."),
886 uint64_to_str(memusage, 0), memlimitstr);
888 return;
892 /// \brief Convert uint32_t to a nice string for --lzma[12]=dict=SIZE
894 /// The idea is to use KiB or MiB suffix when possible.
895 static const char *
896 uint32_to_optstr(uint32_t num)
898 static char buf[16];
900 if ((num & ((UINT32_C(1) << 20) - 1)) == 0)
901 snprintf(buf, sizeof(buf), "%" PRIu32 "MiB", num >> 20);
902 else if ((num & ((UINT32_C(1) << 10) - 1)) == 0)
903 snprintf(buf, sizeof(buf), "%" PRIu32 "KiB", num >> 10);
904 else
905 snprintf(buf, sizeof(buf), "%" PRIu32, num);
907 return buf;
911 extern void
912 message_filters_to_str(char buf[FILTERS_STR_SIZE],
913 const lzma_filter *filters, bool all_known)
915 char *pos = buf;
916 size_t left = FILTERS_STR_SIZE;
918 for (size_t i = 0; filters[i].id != LZMA_VLI_UNKNOWN; ++i) {
919 // Add the dashes for the filter option. A space is
920 // needed after the first and later filters.
921 my_snprintf(&pos, &left, "%s", i == 0 ? "--" : " --");
923 switch (filters[i].id) {
924 case LZMA_FILTER_LZMA1:
925 case LZMA_FILTER_LZMA2: {
926 const lzma_options_lzma *opt = filters[i].options;
927 const char *mode = NULL;
928 const char *mf = NULL;
930 if (all_known) {
931 switch (opt->mode) {
932 case LZMA_MODE_FAST:
933 mode = "fast";
934 break;
936 case LZMA_MODE_NORMAL:
937 mode = "normal";
938 break;
940 default:
941 mode = "UNKNOWN";
942 break;
945 switch (opt->mf) {
946 case LZMA_MF_HC3:
947 mf = "hc3";
948 break;
950 case LZMA_MF_HC4:
951 mf = "hc4";
952 break;
954 case LZMA_MF_BT2:
955 mf = "bt2";
956 break;
958 case LZMA_MF_BT3:
959 mf = "bt3";
960 break;
962 case LZMA_MF_BT4:
963 mf = "bt4";
964 break;
966 default:
967 mf = "UNKNOWN";
968 break;
972 // Add the filter name and dictionary size, which
973 // is always known.
974 my_snprintf(&pos, &left, "lzma%c=dict=%s",
975 filters[i].id == LZMA_FILTER_LZMA2
976 ? '2' : '1',
977 uint32_to_optstr(opt->dict_size));
979 // With LZMA1 also lc/lp/pb are known when
980 // decompressing, but this function is never
981 // used to print information about .lzma headers.
982 assert(filters[i].id == LZMA_FILTER_LZMA2
983 || all_known);
985 // Print the rest of the options, which are known
986 // only when compressing.
987 if (all_known)
988 my_snprintf(&pos, &left,
989 ",lc=%" PRIu32 ",lp=%" PRIu32
990 ",pb=%" PRIu32
991 ",mode=%s,nice=%" PRIu32 ",mf=%s"
992 ",depth=%" PRIu32,
993 opt->lc, opt->lp, opt->pb,
994 mode, opt->nice_len, mf, opt->depth);
995 break;
998 case LZMA_FILTER_X86:
999 case LZMA_FILTER_POWERPC:
1000 case LZMA_FILTER_IA64:
1001 case LZMA_FILTER_ARM:
1002 case LZMA_FILTER_ARMTHUMB:
1003 case LZMA_FILTER_SPARC: {
1004 static const char bcj_names[][9] = {
1005 "x86",
1006 "powerpc",
1007 "ia64",
1008 "arm",
1009 "armthumb",
1010 "sparc",
1013 const lzma_options_bcj *opt = filters[i].options;
1014 my_snprintf(&pos, &left, "%s", bcj_names[filters[i].id
1015 - LZMA_FILTER_X86]);
1017 // Show the start offset only when really needed.
1018 if (opt != NULL && opt->start_offset != 0)
1019 my_snprintf(&pos, &left, "=start=%" PRIu32,
1020 opt->start_offset);
1022 break;
1025 case LZMA_FILTER_DELTA: {
1026 const lzma_options_delta *opt = filters[i].options;
1027 my_snprintf(&pos, &left, "delta=dist=%" PRIu32,
1028 opt->dist);
1029 break;
1032 default:
1033 // This should be possible only if liblzma is
1034 // newer than the xz tool.
1035 my_snprintf(&pos, &left, "UNKNOWN");
1036 break;
1040 return;
1044 extern void
1045 message_filters_show(enum message_verbosity v, const lzma_filter *filters)
1047 if (v > verbosity)
1048 return;
1050 char buf[FILTERS_STR_SIZE];
1051 message_filters_to_str(buf, filters, true);
1052 fprintf(stderr, _("%s: Filter chain: %s\n"), progname, buf);
1053 return;
1057 extern void
1058 message_try_help(void)
1060 // Print this with V_WARNING instead of V_ERROR to prevent it from
1061 // showing up when --quiet has been specified.
1062 message(V_WARNING, _("Try `%s --help' for more information."),
1063 progname);
1064 return;
1068 extern void
1069 message_version(void)
1071 // It is possible that liblzma version is different than the command
1072 // line tool version, so print both.
1073 if (opt_robot) {
1074 printf("XZ_VERSION=%" PRIu32 "\nLIBLZMA_VERSION=%" PRIu32 "\n",
1075 LZMA_VERSION, lzma_version_number());
1076 } else {
1077 printf("xz (" PACKAGE_NAME ") " LZMA_VERSION_STRING "\n");
1078 printf("liblzma %s\n", lzma_version_string());
1081 tuklib_exit(E_SUCCESS, E_ERROR, verbosity != V_SILENT);
1085 extern void
1086 message_help(bool long_help)
1088 printf(_("Usage: %s [OPTION]... [FILE]...\n"
1089 "Compress or decompress FILEs in the .xz format.\n\n"),
1090 progname);
1092 // NOTE: The short help doesn't currently have options that
1093 // take arguments.
1094 if (long_help)
1095 puts(_("Mandatory arguments to long options are mandatory "
1096 "for short options too.\n"));
1098 if (long_help)
1099 puts(_(" Operation mode:\n"));
1101 puts(_(
1102 " -z, --compress force compression\n"
1103 " -d, --decompress force decompression\n"
1104 " -t, --test test compressed file integrity\n"
1105 " -l, --list list information about .xz files"));
1107 if (long_help)
1108 puts(_("\n Operation modifiers:\n"));
1110 puts(_(
1111 " -k, --keep keep (don't delete) input files\n"
1112 " -f, --force force overwrite of output file and (de)compress links\n"
1113 " -c, --stdout write to standard output and don't delete input files"));
1115 if (long_help)
1116 puts(_(
1117 " --no-sparse do not create sparse files when decompressing\n"
1118 " -S, --suffix=.SUF use the suffix `.SUF' on compressed files\n"
1119 " --files[=FILE] read filenames to process from FILE; if FILE is\n"
1120 " omitted, filenames are read from the standard input;\n"
1121 " filenames must be terminated with the newline character\n"
1122 " --files0[=FILE] like --files but use the null character as terminator"));
1124 if (long_help) {
1125 puts(_("\n Basic file format and compression options:\n"));
1126 puts(_(
1127 " -F, --format=FMT file format to encode or decode; possible values are\n"
1128 " `auto' (default), `xz', `lzma', and `raw'\n"
1129 " -C, --check=CHECK integrity check type: `none' (use with caution),\n"
1130 " `crc32', `crc64' (default), or `sha256'"));
1133 puts(_(
1134 " -0 ... -9 compression preset; default is 6; take compressor *and*\n"
1135 " decompressor memory usage into account before using 7-9!"));
1137 puts(_(
1138 " -e, --extreme try to improve compression ratio by using more CPU time;\n"
1139 " does not affect decompressor memory requirements"));
1141 if (long_help) {
1142 puts(_( // xgettext:no-c-format
1143 " --memlimit-compress=LIMIT\n"
1144 " --memlimit-decompress=LIMIT\n"
1145 " -M, --memlimit=LIMIT\n"
1146 " set memory usage limit for compression, decompression,\n"
1147 " or both; LIMIT is in bytes, % of RAM, or 0 for defaults"));
1149 puts(_(
1150 " --no-adjust if compression settings exceed the memory usage limit,\n"
1151 " give an error instead of adjusting the settings downwards"));
1154 if (long_help) {
1155 puts(_(
1156 "\n Custom filter chain for compression (alternative for using presets):"));
1158 #if defined(HAVE_ENCODER_LZMA1) || defined(HAVE_DECODER_LZMA1) \
1159 || defined(HAVE_ENCODER_LZMA2) || defined(HAVE_DECODER_LZMA2)
1160 // TRANSLATORS: The word "literal" in "literal context bits"
1161 // means how many "context bits" to use when encoding
1162 // literals. A literal is a single 8-bit byte. It doesn't
1163 // mean "literally" here.
1164 puts(_(
1165 "\n"
1166 " --lzma1[=OPTS] LZMA1 or LZMA2; OPTS is a comma-separated list of zero or\n"
1167 " --lzma2[=OPTS] more of the following options (valid values; default):\n"
1168 " preset=PRE reset options to a preset (0-9[e])\n"
1169 " dict=NUM dictionary size (4KiB - 1536MiB; 8MiB)\n"
1170 " lc=NUM number of literal context bits (0-4; 3)\n"
1171 " lp=NUM number of literal position bits (0-4; 0)\n"
1172 " pb=NUM number of position bits (0-4; 2)\n"
1173 " mode=MODE compression mode (fast, normal; normal)\n"
1174 " nice=NUM nice length of a match (2-273; 64)\n"
1175 " mf=NAME match finder (hc3, hc4, bt2, bt3, bt4; bt4)\n"
1176 " depth=NUM maximum search depth; 0=automatic (default)"));
1177 #endif
1179 puts(_(
1180 "\n"
1181 " --x86[=OPTS] x86 BCJ filter (32-bit and 64-bit)\n"
1182 " --powerpc[=OPTS] PowerPC BCJ filter (big endian only)\n"
1183 " --ia64[=OPTS] IA-64 (Itanium) BCJ filter\n"
1184 " --arm[=OPTS] ARM BCJ filter (little endian only)\n"
1185 " --armthumb[=OPTS] ARM-Thumb BCJ filter (little endian only)\n"
1186 " --sparc[=OPTS] SPARC BCJ filter\n"
1187 " Valid OPTS for all BCJ filters:\n"
1188 " start=NUM start offset for conversions (default=0)"));
1190 #if defined(HAVE_ENCODER_DELTA) || defined(HAVE_DECODER_DELTA)
1191 puts(_(
1192 "\n"
1193 " --delta[=OPTS] Delta filter; valid OPTS (valid values; default):\n"
1194 " dist=NUM distance between bytes being subtracted\n"
1195 " from each other (1-256; 1)"));
1196 #endif
1199 if (long_help)
1200 puts(_("\n Other options:\n"));
1202 puts(_(
1203 " -q, --quiet suppress warnings; specify twice to suppress errors too\n"
1204 " -v, --verbose be verbose; specify twice for even more verbose"));
1206 if (long_help) {
1207 puts(_(
1208 " -Q, --no-warn make warnings not affect the exit status"));
1209 puts(_(
1210 " --robot use machine-parsable messages (useful for scripts)"));
1211 puts("");
1212 puts(_(
1213 " --info-memory display the total amount of RAM and the currently active\n"
1214 " memory usage limits, and exit"));
1215 puts(_(
1216 " -h, --help display the short help (lists only the basic options)\n"
1217 " -H, --long-help display this long help and exit"));
1218 } else {
1219 puts(_(
1220 " -h, --help display this short help and exit\n"
1221 " -H, --long-help display the long help (lists also the advanced options)"));
1224 puts(_(
1225 " -V, --version display the version number and exit"));
1227 puts(_("\nWith no FILE, or when FILE is -, read standard input.\n"));
1229 // TRANSLATORS: This message indicates the bug reporting address
1230 // for this package. Please add _another line_ saying
1231 // "Report translation bugs to <...>\n" with the email or WWW
1232 // address for translation bugs. Thanks.
1233 printf(_("Report bugs to <%s> (in English or Finnish).\n"),
1234 PACKAGE_BUGREPORT);
1235 printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
1237 tuklib_exit(E_SUCCESS, E_ERROR, verbosity != V_SILENT);