]> icculus.org git repositories - icculus/xz.git/blob - src/xz/message.c
Make the memusage functions of LZMA1 and LZMA2 decoders
[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 // Ugly hack to make it possible to use lzma_attribute((format(printf, 3, 4)))
127 // and thus catch stupid things, while still allowing format characters that
128 // are not in ISO C but are in POSIX. This has to be done after my_snprintf()
129 // has been defined.
130 #ifdef __GNUC__
131 #       define my_snprintf __extension__ my_snprintf
132 #       define my_printf __extension__ printf
133 #endif
134
135
136 extern void
137 message_init(const char *given_argv0)
138 {
139         // Name of the program
140         argv0 = given_argv0;
141
142         // If --verbose is used, we use a progress indicator if and only
143         // if stderr is a terminal. If stderr is not a terminal, we print
144         // verbose information only after finishing the file. As a special
145         // exception, even if --verbose was not used, user can send SIGALRM
146         // to make us print progress information once without automatic
147         // updating.
148         progress_automatic = isatty(STDERR_FILENO);
149
150         // Commented out because COLUMNS is rarely exported to environment.
151         // Most users have at least 80 columns anyway, let's think something
152         // fancy here if enough people complain.
153 /*
154         if (progress_automatic) {
155                 // stderr is a terminal. Check the COLUMNS environment
156                 // variable to see if the terminal is wide enough. If COLUMNS
157                 // doesn't exist or it has some unparseable value, we assume
158                 // that the terminal is wide enough.
159                 const char *columns_str = getenv("COLUMNS");
160                 if (columns_str != NULL) {
161                         char *endptr;
162                         const long columns = strtol(columns_str, &endptr, 10);
163                         if (*endptr != '\0' || columns < 80)
164                                 progress_automatic = false;
165                 }
166         }
167 */
168
169 #ifdef SIGALRM
170         // Establish the signal handler for SIGALRM. Since this signal
171         // doesn't require any quick action, we set SA_RESTART.
172         struct sigaction sa;
173         sigemptyset(&sa.sa_mask);
174         sa.sa_flags = SA_RESTART;
175         sa.sa_handler = &progress_signal_handler;
176         if (sigaction(SIGALRM, &sa, NULL))
177                 message_signal_handler();
178 #endif
179
180         return;
181 }
182
183
184 extern void
185 message_verbosity_increase(void)
186 {
187         if (verbosity < V_DEBUG)
188                 ++verbosity;
189
190         return;
191 }
192
193
194 extern void
195 message_verbosity_decrease(void)
196 {
197         if (verbosity > V_SILENT)
198                 --verbosity;
199
200         return;
201 }
202
203
204 extern void
205 message_set_files(unsigned int files)
206 {
207         files_total = files;
208         return;
209 }
210
211
212 /// Prints the name of the current file if it hasn't been printed already,
213 /// except if we are processing exactly one stream from stdin to stdout.
214 /// I think it looks nicer to not print "(stdin)" when --verbose is used
215 /// in a pipe and no other files are processed.
216 static void
217 print_filename(void)
218 {
219         if (!current_filename_printed
220                         && (files_total != 1 || filename != stdin_filename)) {
221                 signals_block();
222
223                 // If a file was already processed, put an empty line
224                 // before the next filename to improve readability.
225                 if (first_filename_printed)
226                         fputc('\n', stderr);
227
228                 first_filename_printed = true;
229                 current_filename_printed = true;
230
231                 // If we don't know how many files there will be due
232                 // to usage of --files or --files0.
233                 if (files_total == 0)
234                         fprintf(stderr, "%s (%u)\n", filename,
235                                         files_pos);
236                 else
237                         fprintf(stderr, "%s (%u/%u)\n", filename,
238                                         files_pos, files_total);
239
240                 signals_unblock();
241         }
242
243         return;
244 }
245
246
247 extern void
248 message_progress_start(const char *src_name, uint64_t in_size)
249 {
250         // Store the processing start time of the file and its expected size.
251         // If we aren't printing any statistics, then these are unused. But
252         // since it is possible that the user tells us with SIGALRM to show
253         // statistics, we need to have these available anyway.
254         start_time = my_time();
255         filename = src_name;
256         expected_in_size = in_size;
257
258         // Indicate the name of this file hasn't been printed to
259         // stderr yet.
260         current_filename_printed = false;
261
262         // Start numbering the files starting from one.
263         ++files_pos;
264
265         // If progress indicator is wanted, print the filename and possibly
266         // the file count now. As an exception, if there is exactly one file,
267         // do not print the filename at all.
268         if (verbosity >= V_VERBOSE && progress_automatic) {
269                 // Print the filename to stderr if that is appropriate with
270                 // the current settings.
271                 print_filename();
272
273                 // Start the timer to set progress_needs_updating to true
274                 // after about one second. An alternative would to be set
275                 // progress_needs_updating to true here immediatelly, but
276                 // setting the timer looks better to me, since extremely
277                 // early progress info is pretty much useless.
278                 alarm(1);
279         }
280
281         return;
282 }
283
284
285 /// Make the string indicating completion percentage.
286 static const char *
287 progress_percentage(uint64_t in_pos)
288 {
289         // If the size of the input file is unknown or the size told us is
290         // clearly wrong since we have processed more data than the alleged
291         // size of the file, show a static string indicating that we have
292         // no idea of the completion percentage.
293         if (expected_in_size == 0 || in_pos > expected_in_size)
294                 return "--- %";
295
296         static char buf[sizeof("99.9 %")];
297
298         // Never show 100.0 % before we actually are finished (that case is
299         // handled separately in message_progress_end()).
300         snprintf(buf, sizeof(buf), "%.1f %%",
301                         (double)(in_pos) / (double)(expected_in_size) * 99.9);
302
303         return buf;
304 }
305
306
307 static void
308 progress_sizes_helper(char **pos, size_t *left, uint64_t value, bool final)
309 {
310         if (final) {
311                 // At maximum of four digits is allowed for exact byte count.
312                 if (value < 10000) {
313                         my_snprintf(pos, left, "%'" PRIu64 " B", value);
314                         return;
315                 }
316
317                 // At maximum of five significant digits is allowed for KiB.
318                 if (value < UINT64_C(10239900)) {
319                         my_snprintf(pos, left, "%'.1f KiB",
320                                         (double)(value) / 1024.0);
321                         return;
322                 }
323         }
324
325         // Otherwise we use MiB.
326         my_snprintf(pos, left, "%'.1f MiB",
327                         (double)(value) / (1024.0 * 1024.0));
328         return;
329 }
330
331
332 /// Make the string containing the amount of input processed, amount of
333 /// output produced, and the compression ratio.
334 static const char *
335 progress_sizes(uint64_t compressed_pos, uint64_t uncompressed_pos, bool final)
336 {
337         // This is enough to hold sizes up to about 99 TiB if thousand
338         // separator is used, or about 1 PiB without thousand separator.
339         // After that the progress indicator will look a bit silly, since
340         // the compression ratio no longer fits with three decimal places.
341         static char buf[44];
342
343         char *pos = buf;
344         size_t left = sizeof(buf);
345
346         // Print the sizes. If this the final message, use more reasonable
347         // units than MiB if the file was small.
348         progress_sizes_helper(&pos, &left, compressed_pos, final);
349         my_snprintf(&pos, &left, " / ");
350         progress_sizes_helper(&pos, &left, uncompressed_pos, final);
351
352         // Avoid division by zero. If we cannot calculate the ratio, set
353         // it to some nice number greater than 10.0 so that it gets caught
354         // in the next if-clause.
355         const double ratio = uncompressed_pos > 0
356                         ? (double)(compressed_pos) / (double)(uncompressed_pos)
357                         : 16.0;
358
359         // If the ratio is very bad, just indicate that it is greater than
360         // 9.999. This way the length of the ratio field stays fixed.
361         if (ratio > 9.999)
362                 snprintf(pos, left, " > %.3f", 9.999);
363         else
364                 snprintf(pos, left, " = %.3f", ratio);
365
366         return buf;
367 }
368
369
370 /// Make the string containing the processing speed of uncompressed data.
371 static const char *
372 progress_speed(uint64_t uncompressed_pos, double elapsed)
373 {
374         // Don't print the speed immediatelly, since the early values look
375         // like somewhat random.
376         if (elapsed < 3.0)
377                 return "";
378
379         static const char unit[][8] = {
380                 "KiB/s",
381                 "MiB/s",
382                 "GiB/s",
383         };
384
385         size_t unit_index = 0;
386
387         // Calculate the speed as KiB/s.
388         double speed = (double)(uncompressed_pos) / (elapsed * 1024.0);
389
390         // Adjust the unit of the speed if needed.
391         while (speed > 999.9) {
392                 speed /= 1024.0;
393                 if (++unit_index == ARRAY_SIZE(unit))
394                         return ""; // Way too fast ;-)
395         }
396
397         static char buf[sizeof("999.9 GiB/s")];
398         snprintf(buf, sizeof(buf), "%.1f %s", speed, unit[unit_index]);
399         return buf;
400 }
401
402
403 /// Make a string indicating elapsed or remaining time. The format is either
404 /// M:SS or H:MM:SS depending on if the time is an hour or more.
405 static const char *
406 progress_time(uint32_t seconds)
407 {
408         // 9999 hours = 416 days
409         static char buf[sizeof("9999:59:59")];
410
411         // Don't show anything if the time is zero or ridiculously big.
412         if (seconds == 0 || seconds > ((UINT32_C(9999) * 60) + 59) * 60 + 59)
413                 return "";
414
415         uint32_t minutes = seconds / 60;
416         seconds %= 60;
417
418         if (minutes >= 60) {
419                 const uint32_t hours = minutes / 60;
420                 minutes %= 60;
421                 snprintf(buf, sizeof(buf),
422                                 "%" PRIu32 ":%02" PRIu32 ":%02" PRIu32,
423                                 hours, minutes, seconds);
424         } else {
425                 snprintf(buf, sizeof(buf), "%" PRIu32 ":%02" PRIu32,
426                                 minutes, seconds);
427         }
428
429         return buf;
430 }
431
432
433 /// Make the string to contain the estimated remaining time, or if the amount
434 /// of input isn't known, how much time has elapsed.
435 static const char *
436 progress_remaining(uint64_t in_pos, double elapsed)
437 {
438         // If we don't know the size of the input, we indicate the time
439         // spent so far.
440         if (expected_in_size == 0 || in_pos > expected_in_size)
441                 return progress_time((uint32_t)(elapsed));
442
443         // If we are at the very beginning of the file or the file is very
444         // small, don't give any estimate to avoid far too wrong estimations.
445         if (in_pos < (UINT64_C(1) << 19) || elapsed < 8.0)
446                 return "";
447
448         // Calculate the estimate. Don't give an estimate of zero seconds,
449         // since it is possible that all the input has been already passed
450         // to the library, but there is still quite a bit of output pending.
451         uint32_t remaining = (double)(expected_in_size - in_pos)
452                         * elapsed / (double)(in_pos);
453         if (remaining == 0)
454                 remaining = 1;
455
456         return progress_time(remaining);
457 }
458
459
460 extern void
461 message_progress_update(uint64_t in_pos, uint64_t out_pos)
462 {
463         // If there's nothing to do, return immediatelly.
464         if (!progress_needs_updating || in_pos == 0)
465                 return;
466
467         // Print the filename if it hasn't been printed yet.
468         print_filename();
469
470         // Calculate how long we have been processing this file.
471         const double elapsed = my_time() - start_time;
472
473         // Set compressed_pos and uncompressed_pos.
474         uint64_t compressed_pos;
475         uint64_t uncompressed_pos;
476         if (opt_mode == MODE_COMPRESS) {
477                 compressed_pos = out_pos;
478                 uncompressed_pos = in_pos;
479         } else {
480                 compressed_pos = in_pos;
481                 uncompressed_pos = out_pos;
482         }
483
484         signals_block();
485
486         // Print the actual progress message. The idea is that there is at
487         // least three spaces between the fields in typical situations, but
488         // even in rare situations there is at least one space.
489         fprintf(stderr, "  %7s %43s   %11s %10s\r",
490                 progress_percentage(in_pos),
491                 progress_sizes(compressed_pos, uncompressed_pos, false),
492                 progress_speed(uncompressed_pos, elapsed),
493                 progress_remaining(in_pos, elapsed));
494
495         // Updating the progress info was finished. Reset
496         // progress_needs_updating to wait for the next SIGALRM.
497         //
498         // NOTE: This has to be done before alarm() call or with (very) bad
499         // luck we could be setting this to false after the alarm has already
500         // been triggered.
501         progress_needs_updating = false;
502
503         if (progress_automatic) {
504                 // Mark that the progress indicator is active, so if an error
505                 // occurs, the error message gets printed cleanly.
506                 progress_active = true;
507
508                 // Restart the timer so that progress_needs_updating gets
509                 // set to true after about one second.
510                 alarm(1);
511         } else {
512                 // The progress message was printed because user had sent us
513                 // SIGALRM. In this case, each progress message is printed
514                 // on its own line.
515                 fputc('\n', stderr);
516         }
517
518         signals_unblock();
519
520         return;
521 }
522
523
524 extern void
525 message_progress_end(uint64_t in_pos, uint64_t out_pos, bool success)
526 {
527         // If we are not in verbose mode, we have nothing to do.
528         if (verbosity < V_VERBOSE || user_abort)
529                 return;
530
531         // Cancel a pending alarm, if any.
532         if (progress_automatic) {
533                 alarm(0);
534                 progress_active = false;
535         }
536
537         const double elapsed = my_time() - start_time;
538
539         uint64_t compressed_pos;
540         uint64_t uncompressed_pos;
541         if (opt_mode == MODE_COMPRESS) {
542                 compressed_pos = out_pos;
543                 uncompressed_pos = in_pos;
544         } else {
545                 compressed_pos = in_pos;
546                 uncompressed_pos = out_pos;
547         }
548
549         // If it took less than a second, don't display the time.
550         const char *elapsed_str = progress_time((double)(elapsed));
551
552         signals_block();
553
554         // When using the auto-updating progress indicator, the final
555         // statistics are printed in the same format as the progress
556         // indicator itself.
557         if (progress_automatic && in_pos > 0) {
558                 // Using floating point conversion for the percentage instead
559                 // of static "100.0 %" string, because the decimal separator
560                 // isn't a dot in all locales.
561                 fprintf(stderr, "  %5.1f %% %43s   %11s %10s\n",
562                         100.0,
563                         progress_sizes(compressed_pos, uncompressed_pos, true),
564                         progress_speed(uncompressed_pos, elapsed),
565                         elapsed_str);
566
567         // When no automatic progress indicator is used, don't print a verbose
568         // message at all if we something went wrong and we couldn't produce
569         // any output. If we did produce output, then it is sometimes useful
570         // to tell that to the user, especially if we detected an error after
571         // a time-consuming operation.
572         } else if (success || out_pos > 0) {
573                 // The filename and size information are always printed.
574                 fprintf(stderr, "%s: %s", filename, progress_sizes(
575                                 compressed_pos, uncompressed_pos, true));
576
577                 // The speed and elapsed time aren't always shown.
578                 const char *speed = progress_speed(uncompressed_pos, elapsed);
579                 if (speed[0] != '\0')
580                         fprintf(stderr, ", %s", speed);
581
582                 if (elapsed_str[0] != '\0')
583                         fprintf(stderr, ", %s", elapsed_str);
584
585                 fputc('\n', stderr);
586         }
587
588         signals_unblock();
589
590         return;
591 }
592
593
594 static void
595 vmessage(enum message_verbosity v, const char *fmt, va_list ap)
596 {
597         if (v <= verbosity) {
598                 signals_block();
599
600                 // If there currently is a progress message on the screen,
601                 // print a newline so that the progress message is left
602                 // readable. This is good, because it is nice to be able to
603                 // see where the error occurred. (The alternative would be
604                 // to clear the progress message and replace it with the
605                 // error message.)
606                 if (progress_active) {
607                         progress_active = false;
608                         fputc('\n', stderr);
609                 }
610
611                 fprintf(stderr, "%s: ", argv0);
612                 vfprintf(stderr, fmt, ap);
613                 fputc('\n', stderr);
614
615                 signals_unblock();
616         }
617
618         return;
619 }
620
621
622 extern void
623 message(enum message_verbosity v, const char *fmt, ...)
624 {
625         va_list ap;
626         va_start(ap, fmt);
627         vmessage(v, fmt, ap);
628         va_end(ap);
629         return;
630 }
631
632
633 extern void
634 message_warning(const char *fmt, ...)
635 {
636         va_list ap;
637         va_start(ap, fmt);
638         vmessage(V_WARNING, fmt, ap);
639         va_end(ap);
640
641         set_exit_status(E_WARNING);
642         return;
643 }
644
645
646 extern void
647 message_error(const char *fmt, ...)
648 {
649         va_list ap;
650         va_start(ap, fmt);
651         vmessage(V_ERROR, fmt, ap);
652         va_end(ap);
653
654         set_exit_status(E_ERROR);
655         return;
656 }
657
658
659 extern void
660 message_fatal(const char *fmt, ...)
661 {
662         va_list ap;
663         va_start(ap, fmt);
664         vmessage(V_ERROR, fmt, ap);
665         va_end(ap);
666
667         my_exit(E_ERROR);
668 }
669
670
671 extern void
672 message_bug(void)
673 {
674         message_fatal(_("Internal error (bug)"));
675 }
676
677
678 extern void
679 message_signal_handler(void)
680 {
681         message_fatal(_("Cannot establish signal handlers"));
682 }
683
684
685 extern const char *
686 message_strm(lzma_ret code)
687 {
688         switch (code) {
689         case LZMA_NO_CHECK:
690                 return _("No integrity check; not verifying file integrity");
691
692         case LZMA_UNSUPPORTED_CHECK:
693                 return _("Unsupported type of integrity check; "
694                                 "not verifying file integrity");
695
696         case LZMA_MEM_ERROR:
697                 return strerror(ENOMEM);
698
699         case LZMA_MEMLIMIT_ERROR:
700                 return _("Memory usage limit reached");
701
702         case LZMA_FORMAT_ERROR:
703                 return _("File format not recognized");
704
705         case LZMA_OPTIONS_ERROR:
706                 return _("Unsupported options");
707
708         case LZMA_DATA_ERROR:
709                 return _("Compressed data is corrupt");
710
711         case LZMA_BUF_ERROR:
712                 return _("Unexpected end of input");
713
714         case LZMA_OK:
715         case LZMA_STREAM_END:
716         case LZMA_GET_CHECK:
717         case LZMA_PROG_ERROR:
718                 return _("Internal error (bug)");
719         }
720
721         return NULL;
722 }
723
724
725 extern void
726 message_try_help(void)
727 {
728         // Print this with V_WARNING instead of V_ERROR to prevent it from
729         // showing up when --quiet has been specified.
730         message(V_WARNING, _("Try `%s --help' for more information."), argv0);
731         return;
732 }
733
734
735 extern void
736 message_version(void)
737 {
738         // It is possible that liblzma version is different than the command
739         // line tool version, so print both.
740         printf("xz " PACKAGE_VERSION "\n");
741         printf("liblzma %s\n", lzma_version_string());
742         my_exit(E_SUCCESS);
743 }
744
745
746 extern void
747 message_help(bool long_help)
748 {
749         printf(_("Usage: %s [OPTION]... [FILE]...\n"
750                         "Compress or decompress FILEs in the .xz format.\n\n"),
751                         argv0);
752
753         puts(_("Mandatory arguments to long options are mandatory for "
754                         "short options too.\n"));
755
756         if (long_help)
757                 puts(_(" Operation mode:\n"));
758
759         puts(_(
760 "  -z, --compress      force compression\n"
761 "  -d, --decompress    force decompression\n"
762 "  -t, --test          test compressed file integrity\n"
763 "  -l, --list          list information about files"));
764
765         if (long_help)
766                 puts(_("\n Operation modifiers:\n"));
767
768         puts(_(
769 "  -k, --keep          keep (don't delete) input files\n"
770 "  -f, --force         force overwrite of output file and (de)compress links\n"
771 "  -c, --stdout        write to standard output and don't delete input files"));
772
773         if (long_help)
774                 puts(_(
775 "  -S, --suffix=.SUF   use the suffix `.SUF' on compressed files\n"
776 "      --files=[FILE]  read filenames to process from FILE; if FILE is\n"
777 "                      omitted, filenames are read from the standard input;\n"
778 "                      filenames must be terminated with the newline character\n"
779 "      --files0=[FILE] like --files but use the null character as terminator"));
780
781         if (long_help) {
782                 puts(_("\n Basic file format and compression options:\n"));
783                 puts(_(
784 "  -F, --format=FMT    file format to encode or decode; possible values are\n"
785 "                      `auto' (default), `xz', `lzma', and `raw'\n"
786 "  -C, --check=CHECK   integrity check type: `crc32', `crc64' (default),\n"
787 "                      or `sha256'"));
788         }
789
790         puts(_(
791 "  -p, --preset=NUM    compression preset: 1-2 fast compression, 3-6 good\n"
792 "                      compression, 7-9 excellent compression; default is 7"));
793
794         puts(_(
795 "  -M, --memory=NUM    use roughly NUM bytes of memory at maximum; 0 indicates\n"
796 "                      the default setting, which depends on the operation mode\n"
797 "                      and the amount of physical memory (RAM)"));
798
799         if (long_help) {
800                 puts(_(
801 "\n Custom filter chain for compression (alternative for using presets):"));
802
803 #if defined(HAVE_ENCODER_LZMA1) || defined(HAVE_DECODER_LZMA1) \
804                 || defined(HAVE_ENCODER_LZMA2) || defined(HAVE_DECODER_LZMA2)
805                 puts(_(
806 "\n"
807 "  --lzma1=[OPTS]      LZMA1 or LZMA2; OPTS is a comma-separated list of zero or\n"
808 "  --lzma2=[OPTS]      more of the following options (valid values; default):\n"
809 "                        dict=NUM   dictionary size (4KiB - 1536MiB; 8MiB)\n"
810 "                        lc=NUM     number of literal context bits (0-4; 3)\n"
811 "                        lp=NUM     number of literal position bits (0-4; 0)\n"
812 "                        pb=NUM     number of position bits (0-4; 2)\n"
813 "                        mode=MODE  compression mode (fast, normal; normal)\n"
814 "                        nice=NUM   nice length of a match (2-273; 64)\n"
815 "                        mf=NAME    match finder (hc3, hc4, bt2, bt3, bt4; bt4)\n"
816 "                        depth=NUM  maximum search depth; 0=automatic (default)"));
817 #endif
818
819                 puts(_(
820 "\n"
821 "  --x86               x86 filter (sometimes called BCJ filter)\n"
822 "  --powerpc           PowerPC (big endian) filter\n"
823 "  --ia64              IA64 (Itanium) filter\n"
824 "  --arm               ARM filter\n"
825 "  --armthumb          ARM-Thumb filter\n"
826 "  --sparc             SPARC filter"));
827
828 #if defined(HAVE_ENCODER_DELTA) || defined(HAVE_DECODER_DELTA)
829                 puts(_(
830 "\n"
831 "  --delta=[OPTS]      Delta filter; valid OPTS (valid values; default):\n"
832 "                        dist=NUM   distance between bytes being subtracted\n"
833 "                                   from each other (1-256; 1)"));
834 #endif
835
836 #if defined(HAVE_ENCODER_SUBBLOCK) || defined(HAVE_DECODER_SUBBLOCK)
837                 puts(_(
838 "\n"
839 "  --subblock=[OPTS]   Subblock filter; valid OPTS (valid values; default):\n"
840 "                        size=NUM   number of bytes of data per subblock\n"
841 "                                   (1 - 256Mi; 4Ki)\n"
842 "                        rle=NUM    run-length encoder chunk size (0-256; 0)"));
843 #endif
844         }
845
846         if (long_help)
847                 puts(_("\n Other options:\n"));
848
849         puts(_(
850 "  -q, --quiet         suppress warnings; specify twice to suppress errors too\n"
851 "  -v, --verbose       be verbose; specify twice for even more verbose"));
852
853         if (long_help)
854                 puts(_(
855 "\n"
856 "  -h, --help          display the short help (lists only the basic options)\n"
857 "  -H, --long-help     display this long help"));
858         else
859                 puts(_(
860 "  -h, --help          display this short help\n"
861 "  -H, --long-help     display the long help (lists also the advanced options)"));
862
863         puts(_(
864 "  -V, --version       display the version number"));
865
866         puts(_("\nWith no FILE, or when FILE is -, read standard input.\n"));
867
868         if (long_help) {
869                 // We use PRIu64 instead of %zu to support pre-C99 libc.
870                 my_printf(_(
871 "On this system and configuration, the tool will use at maximum of\n"
872 "  * roughly %'" PRIu64 " MiB RAM for compression;\n"
873 "  * roughly %'" PRIu64 " MiB RAM for uncompression; and\n"),
874                                 hardware_memlimit_encoder() / (1024 * 1024),
875                                 hardware_memlimit_decoder() / (1024 * 1024));
876                 my_printf(N_("  * one thread for (de)compression.\n\n",
877                         "  * %'" PRIu64 " threads for (de)compression.\n\n",
878                         (uint64_t)(opt_threads)), (uint64_t)(opt_threads));
879         }
880
881         printf(_("Report bugs to <%s> (in English or Finnish).\n"),
882                         PACKAGE_BUGREPORT);
883
884         my_exit(E_SUCCESS);
885 }