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