]> icculus.org git repositories - icculus/xz.git/blob - src/xz/message.c
Some xz command line tool improvements.
[icculus/xz.git] / src / xz / message.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       message.c
4 /// \brief      Printing messages to stderr
5 //
6 //  Copyright (C) 2007-2008 Lasse Collin
7 //
8 //  This program is free software; you can redistribute it and/or
9 //  modify it under the terms of the GNU Lesser General Public
10 //  License as published by the Free Software Foundation; either
11 //  version 2.1 of the License, or (at your option) any later version.
12 //
13 //  This program is distributed in the hope that it will be useful,
14 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 //  Lesser General Public License for more details.
17 //
18 ///////////////////////////////////////////////////////////////////////////////
19
20 #include "private.h"
21
22 #if defined(HAVE_SYS_TIME_H)
23 #       include <sys/time.h>
24 #elif defined(SIGALRM)
25 // FIXME
26 #endif
27
28 #include <stdarg.h>
29
30
31 /// Name of the program which is prefixed to the error messages.
32 static const char *argv0;
33
34 /// Number of the current file
35 static unsigned int files_pos = 0;
36
37 /// Total number of input files; zero if unknown.
38 static unsigned int files_total;
39
40 /// Verbosity level
41 static enum message_verbosity verbosity = V_WARNING;
42
43 /// Filename which we will print with the verbose messages
44 static const char *filename;
45
46 /// True once the a filename has been printed to stderr as part of progress
47 /// message. If automatic progress updating isn't enabled, this becomes true
48 /// after the first progress message has been printed due to user sending
49 /// SIGALRM. Once this variable is true,  we will print an empty line before
50 /// the next filename to make the output more readable.
51 static bool first_filename_printed = false;
52
53 /// This is set to true when we have printed the current filename to stderr
54 /// as part of a progress message. This variable is useful only if not
55 /// updating progress automatically: if user sends many SIGALRM signals,
56 /// we won't print the name of the same file multiple times.
57 static bool current_filename_printed = false;
58
59 /// True if we should print progress indicator and update it automatically.
60 static bool progress_automatic;
61
62 /// This is true when a progress message was printed and the cursor is still
63 /// on the same line with the progress message. In that case, a newline has
64 /// to be printed before any error messages.
65 static bool progress_active = false;
66
67 /// Expected size of the input stream is needed to show completion percentage
68 /// and estimate remaining time.
69 static uint64_t expected_in_size;
70
71 /// Time when we started processing the file
72 static double start_time;
73
74 /// The signal handler for SIGALRM sets this to true. It is set back to false
75 /// once the progress message has been updated.
76 static volatile sig_atomic_t progress_needs_updating = false;
77
78
79 /// Signal handler for SIGALRM
80 static void
81 progress_signal_handler(int sig lzma_attribute((unused)))
82 {
83         progress_needs_updating = true;
84         return;
85 }
86
87
88 /// Get the current time as double
89 static double
90 my_time(void)
91 {
92         struct timeval tv;
93
94         // This really shouldn't fail. I'm not sure what to return if it
95         // still fails. It doesn't look so useful to check the return value
96         // everywhere. FIXME?
97         if (gettimeofday(&tv, NULL))
98                 return -1.0;
99
100         return (double)(tv.tv_sec) + (double)(tv.tv_usec) / 1.0e9;
101 }
102
103
104 /// Wrapper for snprintf() to help constructing a string in pieces.
105 static void lzma_attribute((format(printf, 3, 4)))
106 my_snprintf(char **pos, size_t *left, const char *fmt, ...)
107 {
108         va_list ap;
109         va_start(ap, fmt);
110         const int len = vsnprintf(*pos, *left, fmt, ap);
111         va_end(ap);
112
113         // If an error occurred, we want the caller to think that the whole
114         // buffer was used. This way no more data will be written to the
115         // buffer. We don't need better error handling here.
116         if (len < 0 || (size_t)(len) >= *left) {
117                 *left = 0;
118         } else {
119                 *pos += len;
120                 *left -= len;
121         }
122
123         return;
124 }
125
126
127 extern void
128 message_init(const char *given_argv0)
129 {
130         // Name of the program
131         argv0 = given_argv0;
132
133         // If --verbose is used, we use a progress indicator if and only
134         // if stderr is a terminal. If stderr is not a terminal, we print
135         // verbose information only after finishing the file. As a special
136         // exception, even if --verbose was not used, user can send SIGALRM
137         // to make us print progress information once without automatic
138         // updating.
139         progress_automatic = isatty(STDERR_FILENO);
140
141         // Commented out because COLUMNS is rarely exported to environment.
142         // Most users have at least 80 columns anyway, let's think something
143         // fancy here if enough people complain.
144 /*
145         if (progress_automatic) {
146                 // stderr is a terminal. Check the COLUMNS environment
147                 // variable to see if the terminal is wide enough. If COLUMNS
148                 // doesn't exist or it has some unparseable value, we assume
149                 // that the terminal is wide enough.
150                 const char *columns_str = getenv("COLUMNS");
151                 if (columns_str != NULL) {
152                         char *endptr;
153                         const long columns = strtol(columns_str, &endptr, 10);
154                         if (*endptr != '\0' || columns < 80)
155                                 progress_automatic = false;
156                 }
157         }
158 */
159
160 #ifdef SIGALRM
161         // Establish the signal handler for SIGALRM. Since this signal
162         // doesn't require any quick action, we set SA_RESTART.
163         struct sigaction sa;
164         sigemptyset(&sa.sa_mask);
165         sa.sa_flags = SA_RESTART;
166         sa.sa_handler = &progress_signal_handler;
167         if (sigaction(SIGALRM, &sa, NULL))
168                 message_signal_handler();
169 #endif
170
171         return;
172 }
173
174
175 extern void
176 message_verbosity_increase(void)
177 {
178         if (verbosity < V_DEBUG)
179                 ++verbosity;
180
181         return;
182 }
183
184
185 extern void
186 message_verbosity_decrease(void)
187 {
188         if (verbosity > V_SILENT)
189                 --verbosity;
190
191         return;
192 }
193
194
195 extern void
196 message_set_files(unsigned int files)
197 {
198         files_total = files;
199         return;
200 }
201
202
203 /// Prints the name of the current file if it hasn't been printed already,
204 /// except if we are processing exactly one stream from stdin to stdout.
205 /// I think it looks nicer to not print "(stdin)" when --verbose is used
206 /// in a pipe and no other files are processed.
207 static void
208 print_filename(void)
209 {
210         if (!current_filename_printed
211                         && (files_total != 1 || filename != stdin_filename)) {
212                 signals_block();
213
214                 // If a file was already processed, put an empty line
215                 // before the next filename to improve readability.
216                 if (first_filename_printed)
217                         fputc('\n', stderr);
218
219                 first_filename_printed = true;
220                 current_filename_printed = true;
221
222                 // If we don't know how many files there will be due
223                 // to usage of --files or --files0.
224                 if (files_total == 0)
225                         fprintf(stderr, "%s (%u)\n", filename,
226                                         files_pos);
227                 else
228                         fprintf(stderr, "%s (%u/%u)\n", filename,
229                                         files_pos, files_total);
230
231                 signals_unblock();
232         }
233
234         return;
235 }
236
237
238 extern void
239 message_progress_start(const char *src_name, uint64_t in_size)
240 {
241         // Store the processing start time of the file and its expected size.
242         // If we aren't printing any statistics, then these are unused. But
243         // since it is possible that the user tells us with SIGALRM to show
244         // statistics, we need to have these available anyway.
245         start_time = my_time();
246         filename = src_name;
247         expected_in_size = in_size;
248
249         // Indicate the name of this file hasn't been printed to
250         // stderr yet.
251         current_filename_printed = false;
252
253         // Start numbering the files starting from one.
254         ++files_pos;
255
256         // If progress indicator is wanted, print the filename and possibly
257         // the file count now. As an exception, if there is exactly one file,
258         // do not print the filename at all.
259         if (verbosity >= V_VERBOSE && progress_automatic) {
260                 // Print the filename to stderr if that is appropriate with
261                 // the current settings.
262                 print_filename();
263
264                 // Start the timer to set progress_needs_updating to true
265                 // after about one second. An alternative would to be set
266                 // progress_needs_updating to true here immediatelly, but
267                 // setting the timer looks better to me, since extremely
268                 // early progress info is pretty much useless.
269                 alarm(1);
270         }
271
272         return;
273 }
274
275
276 /// Make the string indicating completion percentage.
277 static const char *
278 progress_percentage(uint64_t in_pos)
279 {
280         // If the size of the input file is unknown or the size told us is
281         // clearly wrong since we have processed more data than the alleged
282         // size of the file, show a static string indicating that we have
283         // no idea of the completion percentage.
284         if (expected_in_size == 0 || in_pos > expected_in_size)
285                 return "--- %";
286
287         static char buf[sizeof("99.9 %")];
288
289         // Never show 100.0 % before we actually are finished (that case is
290         // handled separately in message_progress_end()).
291         snprintf(buf, sizeof(buf), "%.1f %%",
292                         (double)(in_pos) / (double)(expected_in_size) * 99.9);
293
294         return buf;
295 }
296
297
298 static void
299 progress_sizes_helper(char **pos, size_t *left, uint64_t value, bool final)
300 {
301         if (final) {
302                 // At maximum of four digits is allowed for exact byte count.
303                 if (value < 10000) {
304                         my_snprintf(pos, left, "%'" PRIu64 " B", value);
305                         return;
306                 }
307
308                 // At maximum of five significant digits is allowed for KiB.
309                 if (value < UINT64_C(10239900)) {
310                         my_snprintf(pos, left, "%'.1f KiB",
311                                         (double)(value) / 1024.0);
312                         return;
313                 }
314         }
315
316         // Otherwise we use MiB.
317         my_snprintf(pos, left, "%'.1f MiB",
318                         (double)(value) / (1024.0 * 1024.0));
319         return;
320 }
321
322
323 /// Make the string containing the amount of input processed, amount of
324 /// output produced, and the compression ratio.
325 static const char *
326 progress_sizes(uint64_t compressed_pos, uint64_t uncompressed_pos, bool final)
327 {
328         // This is enough to hold sizes up to about 99 TiB if thousand
329         // separator is used, or about 1 PiB without thousand separator.
330         // After that the progress indicator will look a bit silly, since
331         // the compression ratio no longer fits with three decimal places.
332         static char buf[44];
333
334         char *pos = buf;
335         size_t left = sizeof(buf);
336
337         // Print the sizes. If this the final message, use more reasonable
338         // units than MiB if the file was small.
339         progress_sizes_helper(&pos, &left, compressed_pos, final);
340         my_snprintf(&pos, &left, " / ");
341         progress_sizes_helper(&pos, &left, uncompressed_pos, final);
342
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;
349
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);
356
357         return buf;
358 }
359
360
361 /// Make the string containing the processing speed of uncompressed data.
362 static const char *
363 progress_speed(uint64_t uncompressed_pos, double elapsed)
364 {
365         // Don't print the speed immediatelly, since the early values look
366         // like somewhat random.
367         if (elapsed < 3.0)
368                 return "";
369
370         static const char unit[][8] = {
371                 "KiB/s",
372                 "MiB/s",
373                 "GiB/s",
374         };
375
376         size_t unit_index = 0;
377
378         // Calculate the speed as KiB/s.
379         double speed = (double)(uncompressed_pos) / (elapsed * 1024.0);
380
381         // Adjust the unit of the speed if needed.
382         while (speed > 999.9) {
383                 speed /= 1024.0;
384                 if (++unit_index == ARRAY_SIZE(unit))
385                         return ""; // Way too fast ;-)
386         }
387
388         static char buf[sizeof("999.9 GiB/s")];
389         snprintf(buf, sizeof(buf), "%.1f %s", speed, unit[unit_index]);
390         return buf;
391 }
392
393
394 /// Make a string indicating elapsed or remaining time. The format is either
395 /// M:SS or H:MM:SS depending on if the time is an hour or more.
396 static const char *
397 progress_time(uint32_t seconds)
398 {
399         // 9999 hours = 416 days
400         static char buf[sizeof("9999:59:59")];
401
402         // Don't show anything if the time is zero or ridiculously big.
403         if (seconds == 0 || seconds > ((UINT32_C(9999) * 60) + 59) * 60 + 59)
404                 return "";
405
406         uint32_t minutes = seconds / 60;
407         seconds %= 60;
408
409         if (minutes >= 60) {
410                 const uint32_t hours = minutes / 60;
411                 minutes %= 60;
412                 snprintf(buf, sizeof(buf),
413                                 "%" PRIu32 ":%02" PRIu32 ":%02" PRIu32,
414                                 hours, minutes, seconds);
415         } else {
416                 snprintf(buf, sizeof(buf), "%" PRIu32 ":%02" PRIu32,
417                                 minutes, seconds);
418         }
419
420         return buf;
421 }
422
423
424 /// Make the string to contain the estimated remaining time, or if the amount
425 /// of input isn't known, how much time has elapsed.
426 static const char *
427 progress_remaining(uint64_t in_pos, double elapsed)
428 {
429         // If we don't know the size of the input, we indicate the time
430         // spent so far.
431         if (expected_in_size == 0 || in_pos > expected_in_size)
432                 return progress_time((uint32_t)(elapsed));
433
434         // If we are at the very beginning of the file or the file is very
435         // small, don't give any estimate to avoid far too wrong estimations.
436         if (in_pos < (UINT64_C(1) << 19) || elapsed < 8.0)
437                 return "";
438
439         // Calculate the estimate. Don't give an estimate of zero seconds,
440         // since it is possible that all the input has been already passed
441         // to the library, but there is still quite a bit of output pending.
442         uint32_t remaining = (double)(expected_in_size - in_pos)
443                         * elapsed / (double)(in_pos);
444         if (remaining == 0)
445                 remaining = 1;
446
447         return progress_time(remaining);
448 }
449
450
451 extern void
452 message_progress_update(uint64_t in_pos, uint64_t out_pos)
453 {
454         // If there's nothing to do, return immediatelly.
455         if (!progress_needs_updating || in_pos == 0)
456                 return;
457
458         // Print the filename if it hasn't been printed yet.
459         print_filename();
460
461         // Calculate how long we have been processing this file.
462         const double elapsed = my_time() - start_time;
463
464         // Set compressed_pos and uncompressed_pos.
465         uint64_t compressed_pos;
466         uint64_t uncompressed_pos;
467         if (opt_mode == MODE_COMPRESS) {
468                 compressed_pos = out_pos;
469                 uncompressed_pos = in_pos;
470         } else {
471                 compressed_pos = in_pos;
472                 uncompressed_pos = out_pos;
473         }
474
475         signals_block();
476
477         // Print the actual progress message. The idea is that there is at
478         // least three spaces between the fields in typical situations, but
479         // even in rare situations there is at least one space.
480         fprintf(stderr, "  %7s %43s   %11s %10s\r",
481                 progress_percentage(in_pos),
482                 progress_sizes(compressed_pos, uncompressed_pos, false),
483                 progress_speed(uncompressed_pos, elapsed),
484                 progress_remaining(in_pos, elapsed));
485
486         // Updating the progress info was finished. Reset
487         // progress_needs_updating to wait for the next SIGALRM.
488         //
489         // NOTE: This has to be done before alarm() call or with (very) bad
490         // luck we could be setting this to false after the alarm has already
491         // been triggered.
492         progress_needs_updating = false;
493
494         if (progress_automatic) {
495                 // Mark that the progress indicator is active, so if an error
496                 // occurs, the error message gets printed cleanly.
497                 progress_active = true;
498
499                 // Restart the timer so that progress_needs_updating gets
500                 // set to true after about one second.
501                 alarm(1);
502         } else {
503                 // The progress message was printed because user had sent us
504                 // SIGALRM. In this case, each progress message is printed
505                 // on its own line.
506                 fputc('\n', stderr);
507         }
508
509         signals_unblock();
510
511         return;
512 }
513
514
515 extern void
516 message_progress_end(uint64_t in_pos, uint64_t out_pos, bool success)
517 {
518         // If we are not in verbose mode, we have nothing to do.
519         if (verbosity < V_VERBOSE || user_abort)
520                 return;
521
522         // Cancel a pending alarm, if any.
523         if (progress_automatic) {
524                 alarm(0);
525                 progress_active = false;
526         }
527
528         const double elapsed = my_time() - start_time;
529
530         uint64_t compressed_pos;
531         uint64_t uncompressed_pos;
532         if (opt_mode == MODE_COMPRESS) {
533                 compressed_pos = out_pos;
534                 uncompressed_pos = in_pos;
535         } else {
536                 compressed_pos = in_pos;
537                 uncompressed_pos = out_pos;
538         }
539
540         // If it took less than a second, don't display the time.
541         const char *elapsed_str = progress_time((double)(elapsed));
542
543         signals_block();
544
545         // When using the auto-updating progress indicator, the final
546         // statistics are printed in the same format as the progress
547         // indicator itself.
548         if (progress_automatic && in_pos > 0) {
549                 // Using floating point conversion for the percentage instead
550                 // of static "100.0 %" string, because the decimal separator
551                 // isn't a dot in all locales.
552                 fprintf(stderr, "  %5.1f %% %43s   %11s %10s\n",
553                         100.0,
554                         progress_sizes(compressed_pos, uncompressed_pos, true),
555                         progress_speed(uncompressed_pos, elapsed),
556                         elapsed_str);
557
558         // When no automatic progress indicator is used, don't print a verbose
559         // message at all if we something went wrong and we couldn't produce
560         // any output. If we did produce output, then it is sometimes useful
561         // to tell that to the user, especially if we detected an error after
562         // a time-consuming operation.
563         } else if (success || out_pos > 0) {
564                 // The filename and size information are always printed.
565                 fprintf(stderr, "%s: %s", filename, progress_sizes(
566                                 compressed_pos, uncompressed_pos, true));
567
568                 // The speed and elapsed time aren't always shown.
569                 const char *speed = progress_speed(uncompressed_pos, elapsed);
570                 if (speed[0] != '\0')
571                         fprintf(stderr, ", %s", speed);
572
573                 if (elapsed_str[0] != '\0')
574                         fprintf(stderr, ", %s", elapsed_str);
575
576                 fputc('\n', stderr);
577         }
578
579         signals_unblock();
580
581         return;
582 }
583
584
585 static void
586 vmessage(enum message_verbosity v, const char *fmt, va_list ap)
587 {
588         if (v <= verbosity) {
589                 signals_block();
590
591                 // If there currently is a progress message on the screen,
592                 // print a newline so that the progress message is left
593                 // readable. This is good, because it is nice to be able to
594                 // see where the error occurred. (The alternative would be
595                 // to clear the progress message and replace it with the
596                 // error message.)
597                 if (progress_active) {
598                         progress_active = false;
599                         fputc('\n', stderr);
600                 }
601
602                 fprintf(stderr, "%s: ", argv0);
603                 vfprintf(stderr, fmt, ap);
604                 fputc('\n', stderr);
605
606                 signals_unblock();
607         }
608
609         return;
610 }
611
612
613 extern void
614 message(enum message_verbosity v, const char *fmt, ...)
615 {
616         va_list ap;
617         va_start(ap, fmt);
618         vmessage(v, fmt, ap);
619         va_end(ap);
620         return;
621 }
622
623
624 extern void
625 message_warning(const char *fmt, ...)
626 {
627         va_list ap;
628         va_start(ap, fmt);
629         vmessage(V_WARNING, fmt, ap);
630         va_end(ap);
631
632         set_exit_status(E_WARNING);
633         return;
634 }
635
636
637 extern void
638 message_error(const char *fmt, ...)
639 {
640         va_list ap;
641         va_start(ap, fmt);
642         vmessage(V_ERROR, fmt, ap);
643         va_end(ap);
644
645         set_exit_status(E_ERROR);
646         return;
647 }
648
649
650 extern void
651 message_fatal(const char *fmt, ...)
652 {
653         va_list ap;
654         va_start(ap, fmt);
655         vmessage(V_ERROR, fmt, ap);
656         va_end(ap);
657
658         my_exit(E_ERROR);
659 }
660
661
662 extern void
663 message_bug(void)
664 {
665         message_fatal(_("Internal error (bug)"));
666 }
667
668
669 extern void
670 message_signal_handler(void)
671 {
672         message_fatal(_("Cannot establish signal handlers"));
673 }
674
675
676 extern const char *
677 message_strm(lzma_ret code)
678 {
679         switch (code) {
680         case LZMA_NO_CHECK:
681                 return _("No integrity check; not verifying file integrity");
682
683         case LZMA_UNSUPPORTED_CHECK:
684                 return _("Unsupported type of integrity check; "
685                                 "not verifying file integrity");
686
687         case LZMA_MEM_ERROR:
688                 return strerror(ENOMEM);
689
690         case LZMA_MEMLIMIT_ERROR:
691                 return _("Memory usage limit reached");
692
693         case LZMA_FORMAT_ERROR:
694                 return _("File format not recognized");
695
696         case LZMA_OPTIONS_ERROR:
697                 return _("Unsupported options");
698
699         case LZMA_DATA_ERROR:
700                 return _("Compressed data is corrupt");
701
702         case LZMA_BUF_ERROR:
703                 return _("Unexpected end of input");
704
705         case LZMA_OK:
706         case LZMA_STREAM_END:
707         case LZMA_GET_CHECK:
708         case LZMA_PROG_ERROR:
709                 return _("Internal error (bug)");
710         }
711
712         return NULL;
713 }
714
715
716 extern void
717 message_filters(enum message_verbosity v, const lzma_filter *filters)
718 {
719         if (v > verbosity)
720                 return;
721
722         fprintf(stderr, _("%s: Filter chain:"), argv0);
723
724         for (size_t i = 0; filters[i].id != LZMA_VLI_UNKNOWN; ++i) {
725                 fprintf(stderr, " --");
726
727                 switch (filters[i].id) {
728                 case LZMA_FILTER_LZMA1:
729                 case LZMA_FILTER_LZMA2: {
730                         const lzma_options_lzma *opt = filters[i].options;
731                         const char *mode;
732                         const char *mf;
733
734                         switch (opt->mode) {
735                         case LZMA_MODE_FAST:
736                                 mode = "fast";
737                                 break;
738
739                         case LZMA_MODE_NORMAL:
740                                 mode = "normal";
741                                 break;
742
743                         default:
744                                 mode = "UNKNOWN";
745                                 break;
746                         }
747
748                         switch (opt->mf) {
749                         case LZMA_MF_HC3:
750                                 mf = "hc3";
751                                 break;
752
753                         case LZMA_MF_HC4:
754                                 mf = "hc4";
755                                 break;
756
757                         case LZMA_MF_BT2:
758                                 mf = "bt2";
759                                 break;
760
761                         case LZMA_MF_BT3:
762                                 mf = "bt3";
763                                 break;
764
765                         case LZMA_MF_BT4:
766                                 mf = "bt4";
767                                 break;
768
769                         default:
770                                 mf = "UNKNOWN";
771                                 break;
772                         }
773
774                         fprintf(stderr, "lzma%c=dict=%" PRIu32
775                                         ",lc=%" PRIu32 ",lp=%" PRIu32
776                                         ",pb=%" PRIu32
777                                         ",mode=%s,nice=%" PRIu32 ",mf=%s"
778                                         ",depth=%" PRIu32,
779                                         filters[i].id == LZMA_FILTER_LZMA2
780                                                 ? '2' : '1',
781                                         opt->dict_size,
782                                         opt->lc, opt->lp, opt->pb,
783                                         mode, opt->nice_len, mf, opt->depth);
784                         break;
785                 }
786
787                 case LZMA_FILTER_X86:
788                         fprintf(stderr, "x86");
789                         break;
790
791                 case LZMA_FILTER_POWERPC:
792                         fprintf(stderr, "powerpc");
793                         break;
794
795                 case LZMA_FILTER_IA64:
796                         fprintf(stderr, "ia64");
797                         break;
798
799                 case LZMA_FILTER_ARM:
800                         fprintf(stderr, "arm");
801                         break;
802
803                 case LZMA_FILTER_ARMTHUMB:
804                         fprintf(stderr, "armthumb");
805                         break;
806
807                 case LZMA_FILTER_SPARC:
808                         fprintf(stderr, "sparc");
809                         break;
810
811                 case LZMA_FILTER_DELTA: {
812                         const lzma_options_delta *opt = filters[i].options;
813                         fprintf(stderr, "delta=dist=%" PRIu32, opt->dist);
814                         break;
815                 }
816
817                 default:
818                         fprintf(stderr, "UNKNOWN");
819                         break;
820                 }
821         }
822
823         fputc('\n', stderr);
824         return;
825 }
826
827
828 extern void
829 message_try_help(void)
830 {
831         // Print this with V_WARNING instead of V_ERROR to prevent it from
832         // showing up when --quiet has been specified.
833         message(V_WARNING, _("Try `%s --help' for more information."), argv0);
834         return;
835 }
836
837
838 extern void
839 message_version(void)
840 {
841         // It is possible that liblzma version is different than the command
842         // line tool version, so print both.
843         printf("xz " PACKAGE_VERSION "\n");
844         printf("liblzma %s\n", lzma_version_string());
845         my_exit(E_SUCCESS);
846 }
847
848
849 extern void
850 message_help(bool long_help)
851 {
852         printf(_("Usage: %s [OPTION]... [FILE]...\n"
853                         "Compress or decompress FILEs in the .xz format.\n\n"),
854                         argv0);
855
856         puts(_("Mandatory arguments to long options are mandatory for "
857                         "short options too.\n"));
858
859         if (long_help)
860                 puts(_(" Operation mode:\n"));
861
862         puts(_(
863 "  -z, --compress      force compression\n"
864 "  -d, --decompress    force decompression\n"
865 "  -t, --test          test compressed file integrity\n"
866 "  -l, --list          list information about files"));
867
868         if (long_help)
869                 puts(_("\n Operation modifiers:\n"));
870
871         puts(_(
872 "  -k, --keep          keep (don't delete) input files\n"
873 "  -f, --force         force overwrite of output file and (de)compress links\n"
874 "  -c, --stdout        write to standard output and don't delete input files"));
875
876         if (long_help)
877                 puts(_(
878 "  -S, --suffix=.SUF   use the suffix `.SUF' on compressed files\n"
879 "      --files=[FILE]  read filenames to process from FILE; if FILE is\n"
880 "                      omitted, filenames are read from the standard input;\n"
881 "                      filenames must be terminated with the newline character\n"
882 "      --files0=[FILE] like --files but use the null character as terminator"));
883
884         if (long_help) {
885                 puts(_("\n Basic file format and compression options:\n"));
886                 puts(_(
887 "  -F, --format=FMT    file format to encode or decode; possible values are\n"
888 "                      `auto' (default), `xz', `lzma', and `raw'\n"
889 "  -C, --check=CHECK   integrity check type: `crc32', `crc64' (default),\n"
890 "                      or `sha256'"));
891         }
892
893         puts(_(
894 "  -0 .. -9            compression preset; 0-2 fast compression, 3-5 good\n"
895 "                      compression, 6-9 excellent compression; default is 6"));
896
897         puts(_(
898 "  -M, --memory=NUM    use roughly NUM bytes of memory at maximum; 0 indicates\n"
899 "                      the default setting, which depends on the operation mode\n"
900 "                      and the amount of physical memory (RAM)"));
901
902         if (long_help) {
903                 puts(_(
904 "\n Custom filter chain for compression (alternative for using presets):"));
905
906 #if defined(HAVE_ENCODER_LZMA1) || defined(HAVE_DECODER_LZMA1) \
907                 || defined(HAVE_ENCODER_LZMA2) || defined(HAVE_DECODER_LZMA2)
908                 puts(_(
909 "\n"
910 "  --lzma1=[OPTS]      LZMA1 or LZMA2; OPTS is a comma-separated list of zero or\n"
911 "  --lzma2=[OPTS]      more of the following options (valid values; default):\n"
912 "                        preset=NUM reset options to preset number NUM (1-9)\n"
913 "                        dict=NUM   dictionary size (4KiB - 1536MiB; 8MiB)\n"
914 "                        lc=NUM     number of literal context bits (0-4; 3)\n"
915 "                        lp=NUM     number of literal position bits (0-4; 0)\n"
916 "                        pb=NUM     number of position bits (0-4; 2)\n"
917 "                        mode=MODE  compression mode (fast, normal; normal)\n"
918 "                        nice=NUM   nice length of a match (2-273; 64)\n"
919 "                        mf=NAME    match finder (hc3, hc4, bt2, bt3, bt4; bt4)\n"
920 "                        depth=NUM  maximum search depth; 0=automatic (default)"));
921 #endif
922
923                 puts(_(
924 "\n"
925 "  --x86               x86 filter (sometimes called BCJ filter)\n"
926 "  --powerpc           PowerPC (big endian) filter\n"
927 "  --ia64              IA64 (Itanium) filter\n"
928 "  --arm               ARM filter\n"
929 "  --armthumb          ARM-Thumb filter\n"
930 "  --sparc             SPARC filter"));
931
932 #if defined(HAVE_ENCODER_DELTA) || defined(HAVE_DECODER_DELTA)
933                 puts(_(
934 "\n"
935 "  --delta=[OPTS]      Delta filter; valid OPTS (valid values; default):\n"
936 "                        dist=NUM   distance between bytes being subtracted\n"
937 "                                   from each other (1-256; 1)"));
938 #endif
939
940 #if defined(HAVE_ENCODER_SUBBLOCK) || defined(HAVE_DECODER_SUBBLOCK)
941                 puts(_(
942 "\n"
943 "  --subblock=[OPTS]   Subblock filter; valid OPTS (valid values; default):\n"
944 "                        size=NUM   number of bytes of data per subblock\n"
945 "                                   (1 - 256Mi; 4Ki)\n"
946 "                        rle=NUM    run-length encoder chunk size (0-256; 0)"));
947 #endif
948         }
949
950         if (long_help)
951                 puts(_("\n Other options:\n"));
952
953         puts(_(
954 "  -q, --quiet         suppress warnings; specify twice to suppress errors too\n"
955 "  -v, --verbose       be verbose; specify twice for even more verbose"));
956
957         if (long_help)
958                 puts(_(
959 "\n"
960 "  -h, --help          display the short help (lists only the basic options)\n"
961 "  -H, --long-help     display this long help"));
962         else
963                 puts(_(
964 "  -h, --help          display this short help\n"
965 "  -H, --long-help     display the long help (lists also the advanced options)"));
966
967         puts(_(
968 "  -V, --version       display the version number"));
969
970         puts(_("\nWith no FILE, or when FILE is -, read standard input.\n"));
971
972         if (long_help) {
973                 printf(_(
974 "On this system and configuration, the tool will use at maximum of\n"
975 "  * roughly %'" PRIu64 " MiB RAM for compression;\n"
976 "  * roughly %'" PRIu64 " MiB RAM for decompression; and\n"),
977                                 hardware_memlimit_encoder() / (1024 * 1024),
978                                 hardware_memlimit_decoder() / (1024 * 1024));
979                 printf(N_("  * one thread for (de)compression.\n\n",
980                         "  * %'" PRIu64 " threads for (de)compression.\n\n",
981                         (uint64_t)(opt_threads)), (uint64_t)(opt_threads));
982         }
983
984         printf(_("Report bugs to <%s> (in English or Finnish).\n"),
985                         PACKAGE_BUGREPORT);
986
987         my_exit(E_SUCCESS);
988 }