1 // SPDX-License-Identifier: 0BSD
2
3 ///////////////////////////////////////////////////////////////////////////////
4 //
5 /// \file message.c
6 /// \brief Printing messages
7 //
8 // Authors: Lasse Collin
9 // Jia Tan
10 //
11 ///////////////////////////////////////////////////////////////////////////////
12
13 #include "private.h"
14 #include "tuklib_mbstr_wrap.h"
15 #include <stdarg.h>
16
17
18 /// Number of the current file
19 static unsigned int files_pos = 0;
20
21 /// Total number of input files; zero if unknown.
22 static unsigned int files_total;
23
24 /// Verbosity level
25 static enum message_verbosity verbosity = V_WARNING;
26
27 /// Filename which we will print with the verbose messages
28 static const char *filename;
29
30 /// True once the a filename has been printed to stderr as part of progress
31 /// message. If automatic progress updating isn't enabled, this becomes true
32 /// after the first progress message has been printed due to user sending
33 /// SIGINFO, SIGUSR1, or SIGALRM. Once this variable is true, we will print
34 /// an empty line before the next filename to make the output more readable.
35 static bool first_filename_printed = false;
36
37 /// This is set to true when we have printed the current filename to stderr
38 /// as part of a progress message. This variable is useful only if not
39 /// updating progress automatically: if user sends many SIGINFO, SIGUSR1, or
40 /// SIGALRM signals, we won't print the name of the same file multiple times.
41 static bool current_filename_printed = false;
42
43 /// True if we should print progress indicator and update it automatically
44 /// if also verbose >= V_VERBOSE.
45 static bool progress_automatic = false;
46
47 /// True if message_progress_start() has been called but
48 /// message_progress_end() hasn't been called yet.
49 static bool progress_started = false;
50
51 /// This is true when a progress message was printed and the cursor is still
52 /// on the same line with the progress message. In that case, a newline has
53 /// to be printed before any error messages.
54 static bool progress_active = false;
55
56 /// Pointer to lzma_stream used to do the encoding or decoding.
57 static lzma_stream *progress_strm;
58
59 /// This is true if we are in passthru mode (not actually compressing or
60 /// decompressing) and thus cannot use lzma_get_progress(progress_strm, ...).
61 /// That is, we are using coder_passthru() in coder.c.
62 static bool progress_is_from_passthru;
63
64 /// Expected size of the input stream is needed to show completion percentage
65 /// and estimate remaining time.
66 static uint64_t expected_in_size;
67
68
69 // Use alarm() and SIGALRM when they are supported. This has two minor
70 // advantages over the alternative of polling gettimeofday():
71 // - It is possible for the user to send SIGINFO, SIGUSR1, or SIGALRM to
72 // get intermediate progress information even when --verbose wasn't used
73 // or stderr is not a terminal.
74 // - alarm() + SIGALRM seems to have slightly less overhead than polling
75 // gettimeofday().
76 #ifdef SIGALRM
77
78 const int message_progress_sigs[] = {
79 SIGALRM,
80 #ifdef SIGINFO
81 SIGINFO,
82 #endif
83 #ifdef SIGUSR1
84 SIGUSR1,
85 #endif
86 0
87 };
88
89 /// The signal handler for SIGALRM sets this to true. It is set back to false
90 /// once the progress message has been updated.
91 static volatile sig_atomic_t progress_needs_updating = false;
92
93 /// Signal handler for SIGALRM
94 static void
progress_signal_handler(int sig lzma_attribute ((__unused__)))95 progress_signal_handler(int sig lzma_attribute((__unused__)))
96 {
97 progress_needs_updating = true;
98 return;
99 }
100
101 #else
102
103 /// This is true when progress message printing is wanted. Using the same
104 /// variable name as above to avoid some ifdefs.
105 static bool progress_needs_updating = false;
106
107 /// Elapsed time when the next progress message update should be done.
108 static uint64_t progress_next_update;
109
110 #endif
111
112
113 extern void
message_init(void)114 message_init(void)
115 {
116 // If --verbose is used, we use a progress indicator if and only
117 // if stderr is a terminal. If stderr is not a terminal, we print
118 // verbose information only after finishing the file. As a special
119 // exception, even if --verbose was not used, user can send SIGALRM
120 // to make us print progress information once without automatic
121 // updating.
122 progress_automatic = is_tty(STDERR_FILENO);
123
124 #ifdef SIGALRM
125 // Establish the signal handlers which set a flag to tell us that
126 // progress info should be updated.
127 struct sigaction sa;
128 sigemptyset(&sa.sa_mask);
129 sa.sa_flags = 0;
130 sa.sa_handler = &progress_signal_handler;
131
132 for (size_t i = 0; message_progress_sigs[i] != 0; ++i)
133 if (sigaction(message_progress_sigs[i], &sa, NULL))
134 message_signal_handler();
135 #endif
136
137 return;
138 }
139
140
141 extern void
message_verbosity_increase(void)142 message_verbosity_increase(void)
143 {
144 if (verbosity < V_DEBUG)
145 ++verbosity;
146
147 return;
148 }
149
150
151 extern void
message_verbosity_decrease(void)152 message_verbosity_decrease(void)
153 {
154 if (verbosity > V_SILENT)
155 --verbosity;
156
157 return;
158 }
159
160
161 extern enum message_verbosity
message_verbosity_get(void)162 message_verbosity_get(void)
163 {
164 return verbosity;
165 }
166
167
168 extern void
message_set_files(unsigned int files)169 message_set_files(unsigned int files)
170 {
171 files_total = files;
172 return;
173 }
174
175
176 /// Prints the name of the current file if it hasn't been printed already,
177 /// except if we are processing exactly one stream from stdin to stdout.
178 /// I think it looks nicer to not print "(stdin)" when --verbose is used
179 /// in a pipe and no other files are processed.
180 static void
print_filename(void)181 print_filename(void)
182 {
183 if (!opt_robot && (files_total != 1 || filename != stdin_filename)) {
184 signals_block();
185
186 FILE *file = opt_mode == MODE_LIST ? stdout : stderr;
187
188 // If a file was already processed, put an empty line
189 // before the next filename to improve readability.
190 if (first_filename_printed)
191 fputc('\n', file);
192
193 first_filename_printed = true;
194 current_filename_printed = true;
195
196 // If we don't know how many files there will be due
197 // to usage of --files or --files0.
198 if (files_total == 0)
199 fprintf(file, "%s (%u)\n",
200 tuklib_mask_nonprint(filename),
201 files_pos);
202 else
203 fprintf(file, "%s (%u/%u)\n",
204 tuklib_mask_nonprint(filename),
205 files_pos, files_total);
206
207 signals_unblock();
208 }
209
210 return;
211 }
212
213
214 extern void
message_filename(const char * src_name)215 message_filename(const char *src_name)
216 {
217 // Start numbering the files starting from one.
218 ++files_pos;
219 filename = src_name;
220
221 if (verbosity >= V_VERBOSE
222 && (progress_automatic || opt_mode == MODE_LIST))
223 print_filename();
224 else
225 current_filename_printed = false;
226
227 return;
228 }
229
230
231 extern void
message_progress_start(lzma_stream * strm,bool is_passthru,uint64_t in_size)232 message_progress_start(lzma_stream *strm, bool is_passthru, uint64_t in_size)
233 {
234 // Store the pointer to the lzma_stream used to do the coding.
235 // It is needed to find out the position in the stream.
236 progress_strm = strm;
237 progress_is_from_passthru = is_passthru;
238
239 // Store the expected size of the file. If we aren't printing any
240 // statistics, then is will be unused. But since it is possible
241 // that the user sends us a signal to show statistics, we need
242 // to have it available anyway.
243 expected_in_size = in_size;
244
245 // Indicate that progress info may need to be printed before
246 // printing error messages.
247 progress_started = true;
248
249 // If progress indicator is wanted, print the filename and possibly
250 // the file count now.
251 if (verbosity >= V_VERBOSE && progress_automatic) {
252 // Start the timer to display the first progress message
253 // after one second. An alternative would be to show the
254 // first message almost immediately, but delaying by one
255 // second looks better to me, since extremely early
256 // progress info is pretty much useless.
257 #ifdef SIGALRM
258 // First disable a possibly existing alarm.
259 alarm(0);
260 progress_needs_updating = false;
261 alarm(1);
262 #else
263 progress_needs_updating = true;
264 progress_next_update = 1000;
265 #endif
266 }
267
268 return;
269 }
270
271
272 /// Make the string indicating completion percentage.
273 static const char *
progress_percentage(uint64_t in_pos)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 // Never show 100.0 % before we actually are finished.
284 double percentage = (double)(in_pos) / (double)(expected_in_size)
285 * 99.9;
286
287 // Use big enough buffer to hold e.g. a multibyte decimal point.
288 static char buf[16];
289 snprintf(buf, sizeof(buf), "%.1f %%", percentage);
290
291 return buf;
292 }
293
294
295 /// Make the string containing the amount of input processed, amount of
296 /// output produced, and the compression ratio.
297 static const char *
progress_sizes(uint64_t compressed_pos,uint64_t uncompressed_pos,bool final)298 progress_sizes(uint64_t compressed_pos, uint64_t uncompressed_pos, bool final)
299 {
300 // Use big enough buffer to hold e.g. a multibyte thousand separators.
301 static char buf[128];
302 char *pos = buf;
303 size_t left = sizeof(buf);
304
305 // Print the sizes. If this the final message, use more reasonable
306 // units than MiB if the file was small.
307 const enum nicestr_unit unit_min = final ? NICESTR_B : NICESTR_MIB;
308 my_snprintf(&pos, &left, "%s / %s",
309 uint64_to_nicestr(compressed_pos,
310 unit_min, NICESTR_TIB, false, 0),
311 uint64_to_nicestr(uncompressed_pos,
312 unit_min, NICESTR_TIB, false, 1));
313
314 // Avoid division by zero. If we cannot calculate the ratio, set
315 // it to some nice number greater than 10.0 so that it gets caught
316 // in the next if-clause.
317 const double ratio = uncompressed_pos > 0
318 ? (double)(compressed_pos) / (double)(uncompressed_pos)
319 : 16.0;
320
321 // If the ratio is very bad, just indicate that it is greater than
322 // 9.999. This way the length of the ratio field stays fixed.
323 if (ratio > 9.999)
324 snprintf(pos, left, " > %.3f", 9.999);
325 else
326 snprintf(pos, left, " = %.3f", ratio);
327
328 return buf;
329 }
330
331
332 /// Make the string containing the processing speed of uncompressed data.
333 static const char *
progress_speed(uint64_t uncompressed_pos,uint64_t elapsed)334 progress_speed(uint64_t uncompressed_pos, uint64_t elapsed)
335 {
336 // Don't print the speed immediately, since the early values look
337 // somewhat random.
338 if (elapsed < 3000)
339 return "";
340
341 // The first character of KiB/s, MiB/s, or GiB/s:
342 static const char unit[] = { 'K', 'M', 'G' };
343
344 size_t unit_index = 0;
345
346 // Calculate the speed as KiB/s.
347 double speed = (double)(uncompressed_pos)
348 / ((double)(elapsed) * (1024.0 / 1000.0));
349
350 // Adjust the unit of the speed if needed.
351 while (speed > 999.0) {
352 speed /= 1024.0;
353 if (++unit_index == ARRAY_SIZE(unit))
354 return ""; // Way too fast ;-)
355 }
356
357 // Use decimal point only if the number is small. Examples:
358 // - 0.1 KiB/s
359 // - 9.9 KiB/s
360 // - 99 KiB/s
361 // - 999 KiB/s
362 // Use big enough buffer to hold e.g. a multibyte decimal point.
363 static char buf[16];
364 snprintf(buf, sizeof(buf), "%.*f %ciB/s",
365 speed > 9.9 ? 0 : 1, speed, unit[unit_index]);
366 return buf;
367 }
368
369
370 /// Make a string indicating elapsed time. The format is either
371 /// M:SS or H:MM:SS depending on if the time is an hour or more.
372 static const char *
progress_time(uint64_t mseconds)373 progress_time(uint64_t mseconds)
374 {
375 // 9999 hours = 416 days
376 static char buf[sizeof("9999:59:59")];
377
378 // 32-bit variable is enough for elapsed time (136 years).
379 uint32_t seconds = (uint32_t)(mseconds / 1000);
380
381 // Don't show anything if the time is zero or ridiculously big.
382 if (seconds == 0 || seconds > ((9999 * 60) + 59) * 60 + 59)
383 return "";
384
385 uint32_t minutes = seconds / 60;
386 seconds %= 60;
387
388 if (minutes >= 60) {
389 const uint32_t hours = minutes / 60;
390 minutes %= 60;
391 snprintf(buf, sizeof(buf),
392 "%" PRIu32 ":%02" PRIu32 ":%02" PRIu32,
393 hours, minutes, seconds);
394 } else {
395 snprintf(buf, sizeof(buf), "%" PRIu32 ":%02" PRIu32,
396 minutes, seconds);
397 }
398
399 return buf;
400 }
401
402
403 /// Return a string containing estimated remaining time when
404 /// reasonably possible.
405 static const char *
progress_remaining(uint64_t in_pos,uint64_t elapsed)406 progress_remaining(uint64_t in_pos, uint64_t elapsed)
407 {
408 // Don't show the estimated remaining time when it wouldn't
409 // make sense:
410 // - Input size is unknown.
411 // - Input has grown bigger since we started (de)compressing.
412 // - We haven't processed much data yet, so estimate would be
413 // too inaccurate.
414 // - Only a few seconds has passed since we started (de)compressing,
415 // so estimate would be too inaccurate.
416 if (expected_in_size == 0 || in_pos > expected_in_size
417 || in_pos < (UINT64_C(1) << 19) || elapsed < 8000)
418 return "";
419
420 // Calculate the estimate. Don't give an estimate of zero seconds,
421 // since it is possible that all the input has been already passed
422 // to the library, but there is still quite a bit of output pending.
423 uint32_t remaining = (uint32_t)((double)(expected_in_size - in_pos)
424 * ((double)(elapsed) / 1000.0) / (double)(in_pos));
425 if (remaining < 1)
426 remaining = 1;
427
428 static char buf[sizeof("9 h 55 min")];
429
430 // Select appropriate precision for the estimated remaining time.
431 if (remaining <= 10) {
432 // A maximum of 10 seconds remaining.
433 // Show the number of seconds as is.
434 snprintf(buf, sizeof(buf), "%" PRIu32 " s", remaining);
435
436 } else if (remaining <= 50) {
437 // A maximum of 50 seconds remaining.
438 // Round up to the next multiple of five seconds.
439 remaining = (remaining + 4) / 5 * 5;
440 snprintf(buf, sizeof(buf), "%" PRIu32 " s", remaining);
441
442 } else if (remaining <= 590) {
443 // A maximum of 9 minutes and 50 seconds remaining.
444 // Round up to the next multiple of ten seconds.
445 remaining = (remaining + 9) / 10 * 10;
446 snprintf(buf, sizeof(buf), "%" PRIu32 " min %" PRIu32 " s",
447 remaining / 60, remaining % 60);
448
449 } else if (remaining <= 59 * 60) {
450 // A maximum of 59 minutes remaining.
451 // Round up to the next multiple of a minute.
452 remaining = (remaining + 59) / 60;
453 snprintf(buf, sizeof(buf), "%" PRIu32 " min", remaining);
454
455 } else if (remaining <= 9 * 3600 + 50 * 60) {
456 // A maximum of 9 hours and 50 minutes left.
457 // Round up to the next multiple of ten minutes.
458 remaining = (remaining + 599) / 600 * 10;
459 snprintf(buf, sizeof(buf), "%" PRIu32 " h %" PRIu32 " min",
460 remaining / 60, remaining % 60);
461
462 } else if (remaining <= 23 * 3600) {
463 // A maximum of 23 hours remaining.
464 // Round up to the next multiple of an hour.
465 remaining = (remaining + 3599) / 3600;
466 snprintf(buf, sizeof(buf), "%" PRIu32 " h", remaining);
467
468 } else if (remaining <= 9 * 24 * 3600 + 23 * 3600) {
469 // A maximum of 9 days and 23 hours remaining.
470 // Round up to the next multiple of an hour.
471 remaining = (remaining + 3599) / 3600;
472 snprintf(buf, sizeof(buf), "%" PRIu32 " d %" PRIu32 " h",
473 remaining / 24, remaining % 24);
474
475 } else if (remaining <= 999 * 24 * 3600) {
476 // A maximum of 999 days remaining. ;-)
477 // Round up to the next multiple of a day.
478 remaining = (remaining + 24 * 3600 - 1) / (24 * 3600);
479 snprintf(buf, sizeof(buf), "%" PRIu32 " d", remaining);
480
481 } else {
482 // The estimated remaining time is too big. Don't show it.
483 return "";
484 }
485
486 return buf;
487 }
488
489
490 /// Get how much uncompressed and compressed data has been processed.
491 static void
progress_pos(uint64_t * in_pos,uint64_t * compressed_pos,uint64_t * uncompressed_pos)492 progress_pos(uint64_t *in_pos,
493 uint64_t *compressed_pos, uint64_t *uncompressed_pos)
494 {
495 uint64_t out_pos;
496 if (progress_is_from_passthru) {
497 // In passthru mode the progress info is in total_in/out but
498 // the *progress_strm itself isn't initialized and thus we
499 // cannot use lzma_get_progress().
500 *in_pos = progress_strm->total_in;
501 out_pos = progress_strm->total_out;
502 } else {
503 lzma_get_progress(progress_strm, in_pos, &out_pos);
504 }
505
506 // It cannot have processed more input than it has been given.
507 assert(*in_pos <= progress_strm->total_in);
508
509 // It cannot have produced more output than it claims to have ready.
510 assert(out_pos >= progress_strm->total_out);
511
512 if (opt_mode == MODE_COMPRESS) {
513 *compressed_pos = out_pos;
514 *uncompressed_pos = *in_pos;
515 } else {
516 *compressed_pos = *in_pos;
517 *uncompressed_pos = out_pos;
518 }
519
520 return;
521 }
522
523
524 extern void
message_progress_update(void)525 message_progress_update(void)
526 {
527 if (!progress_needs_updating)
528 return;
529
530 // Calculate how long we have been processing this file.
531 const uint64_t elapsed = mytime_get_elapsed();
532
533 #ifndef SIGALRM
534 if (progress_next_update > elapsed)
535 return;
536
537 progress_next_update = elapsed + 1000;
538 #endif
539
540 // Get our current position in the stream.
541 uint64_t in_pos;
542 uint64_t compressed_pos;
543 uint64_t uncompressed_pos;
544 progress_pos(&in_pos, &compressed_pos, &uncompressed_pos);
545
546 // Block signals so that fprintf() doesn't get interrupted.
547 signals_block();
548
549 // Print the filename if it hasn't been printed yet.
550 if (!current_filename_printed)
551 print_filename();
552
553 // Print the actual progress message. The idea is that there is at
554 // least three spaces between the fields in typical situations, but
555 // even in rare situations there is at least one space.
556 const char *cols[5] = {
557 progress_percentage(in_pos),
558 progress_sizes(compressed_pos, uncompressed_pos, false),
559 progress_speed(uncompressed_pos, elapsed),
560 progress_time(elapsed),
561 progress_remaining(in_pos, elapsed),
562 };
563 fprintf(stderr, "\r %*s %*s %*s %10s %10s\r",
564 tuklib_mbstr_fw(cols[0], 6), cols[0],
565 tuklib_mbstr_fw(cols[1], 35), cols[1],
566 tuklib_mbstr_fw(cols[2], 9), cols[2],
567 cols[3],
568 cols[4]);
569
570 #ifdef SIGALRM
571 // Updating the progress info was finished. Reset
572 // progress_needs_updating to wait for the next SIGALRM.
573 //
574 // NOTE: This has to be done before alarm(1) or with (very) bad
575 // luck we could be setting this to false after the alarm has already
576 // been triggered.
577 progress_needs_updating = false;
578
579 if (verbosity >= V_VERBOSE && progress_automatic) {
580 // Mark that the progress indicator is active, so if an error
581 // occurs, the error message gets printed cleanly.
582 progress_active = true;
583
584 // Restart the timer so that progress_needs_updating gets
585 // set to true after about one second.
586 alarm(1);
587 } else {
588 // The progress message was printed because user had sent us
589 // SIGALRM. In this case, each progress message is printed
590 // on its own line.
591 fputc('\n', stderr);
592 }
593 #else
594 // When SIGALRM isn't supported and we get here, it's always due to
595 // automatic progress update. We set progress_active here too like
596 // described above.
597 assert(verbosity >= V_VERBOSE);
598 assert(progress_automatic);
599 progress_active = true;
600 #endif
601
602 signals_unblock();
603
604 return;
605 }
606
607
608 static void
progress_flush(bool finished)609 progress_flush(bool finished)
610 {
611 if (!progress_started || verbosity < V_VERBOSE)
612 return;
613
614 uint64_t in_pos;
615 uint64_t compressed_pos;
616 uint64_t uncompressed_pos;
617 progress_pos(&in_pos, &compressed_pos, &uncompressed_pos);
618
619 // Avoid printing intermediate progress info if some error occurs
620 // in the beginning of the stream. (If something goes wrong later in
621 // the stream, it is sometimes useful to tell the user where the
622 // error approximately occurred, especially if the error occurs
623 // after a time-consuming operation.)
624 if (!finished && !progress_active
625 && (compressed_pos == 0 || uncompressed_pos == 0))
626 return;
627
628 progress_active = false;
629
630 const uint64_t elapsed = mytime_get_elapsed();
631
632 signals_block();
633
634 // When using the auto-updating progress indicator, the final
635 // statistics are printed in the same format as the progress
636 // indicator itself.
637 if (progress_automatic) {
638 const char *cols[5] = {
639 finished ? "100 %" : progress_percentage(in_pos),
640 progress_sizes(compressed_pos, uncompressed_pos, true),
641 progress_speed(uncompressed_pos, elapsed),
642 progress_time(elapsed),
643 finished ? "" : progress_remaining(in_pos, elapsed),
644 };
645 fprintf(stderr, "\r %*s %*s %*s %10s %10s\n",
646 tuklib_mbstr_fw(cols[0], 6), cols[0],
647 tuklib_mbstr_fw(cols[1], 35), cols[1],
648 tuklib_mbstr_fw(cols[2], 9), cols[2],
649 cols[3],
650 cols[4]);
651 } else {
652 // The filename is always printed.
653 //
654 // NOTE: This function is called from vmessage() whose
655 // caller may have used tuklib_mask_nonprint(). Thus,
656 // we must use the _r variant here.
657 char *mem = NULL;
658 fprintf(stderr, _("%s: "),
659 tuklib_mask_nonprint_r(filename, &mem));
660 free(mem);
661
662 // Percentage is printed only if we didn't finish yet.
663 if (!finished) {
664 // Don't print the percentage when it isn't known
665 // (starts with a dash).
666 const char *percentage = progress_percentage(in_pos);
667 if (percentage[0] != '-')
668 fprintf(stderr, "%s, ", percentage);
669 }
670
671 // Size information is always printed.
672 fprintf(stderr, "%s", progress_sizes(
673 compressed_pos, uncompressed_pos, true));
674
675 // The speed and elapsed time aren't always shown.
676 const char *speed = progress_speed(uncompressed_pos, elapsed);
677 if (speed[0] != '\0')
678 fprintf(stderr, ", %s", speed);
679
680 const char *elapsed_str = progress_time(elapsed);
681 if (elapsed_str[0] != '\0')
682 fprintf(stderr, ", %s", elapsed_str);
683
684 fputc('\n', stderr);
685 }
686
687 signals_unblock();
688
689 return;
690 }
691
692
693 extern void
message_progress_end(bool success)694 message_progress_end(bool success)
695 {
696 assert(progress_started);
697 progress_flush(success);
698 progress_started = false;
699 return;
700 }
701
702
703 static void
vmessage(enum message_verbosity v,const char * fmt,va_list ap)704 vmessage(enum message_verbosity v, const char *fmt, va_list ap)
705 {
706 if (v <= verbosity) {
707 signals_block();
708
709 progress_flush(false);
710
711 // TRANSLATORS: This is the program name in the beginning
712 // of the line in messages. Usually it becomes "xz: ".
713 // This is a translatable string because French needs
714 // a space before a colon.
715 fprintf(stderr, _("%s: "), progname);
716
717 #ifdef __clang__
718 # pragma GCC diagnostic push
719 # pragma GCC diagnostic ignored "-Wformat-nonliteral"
720 #endif
721 vfprintf(stderr, fmt, ap);
722 #ifdef __clang__
723 # pragma GCC diagnostic pop
724 #endif
725
726 fputc('\n', stderr);
727
728 signals_unblock();
729 }
730
731 return;
732 }
733
734
735 extern void
message(enum message_verbosity v,const char * fmt,...)736 message(enum message_verbosity v, const char *fmt, ...)
737 {
738 va_list ap;
739 va_start(ap, fmt);
740 vmessage(v, fmt, ap);
741 va_end(ap);
742 return;
743 }
744
745
746 extern void
message_warning(const char * fmt,...)747 message_warning(const char *fmt, ...)
748 {
749 va_list ap;
750 va_start(ap, fmt);
751 vmessage(V_WARNING, fmt, ap);
752 va_end(ap);
753
754 set_exit_status(E_WARNING);
755 return;
756 }
757
758
759 extern void
message_error(const char * fmt,...)760 message_error(const char *fmt, ...)
761 {
762 va_list ap;
763 va_start(ap, fmt);
764 vmessage(V_ERROR, fmt, ap);
765 va_end(ap);
766
767 set_exit_status(E_ERROR);
768 return;
769 }
770
771
772 extern void
message_fatal(const char * fmt,...)773 message_fatal(const char *fmt, ...)
774 {
775 va_list ap;
776 va_start(ap, fmt);
777 vmessage(V_ERROR, fmt, ap);
778 va_end(ap);
779
780 tuklib_exit(E_ERROR, E_ERROR, false);
781 }
782
783
784 extern void
message_bug(void)785 message_bug(void)
786 {
787 message_fatal(_("Internal error (bug)"));
788 }
789
790
791 extern void
message_signal_handler(void)792 message_signal_handler(void)
793 {
794 message_fatal(_("Cannot establish signal handlers"));
795 }
796
797
798 extern const char *
message_strm(lzma_ret code)799 message_strm(lzma_ret code)
800 {
801 switch (code) {
802 case LZMA_NO_CHECK:
803 return _("No integrity check; not verifying file integrity");
804
805 case LZMA_UNSUPPORTED_CHECK:
806 return _("Unsupported type of integrity check; "
807 "not verifying file integrity");
808
809 case LZMA_MEM_ERROR:
810 return strerror(ENOMEM);
811
812 case LZMA_MEMLIMIT_ERROR:
813 return _("Memory usage limit reached");
814
815 case LZMA_FORMAT_ERROR:
816 return _("File format not recognized");
817
818 case LZMA_OPTIONS_ERROR:
819 return _("Unsupported options");
820
821 case LZMA_DATA_ERROR:
822 return _("Compressed data is corrupt");
823
824 case LZMA_BUF_ERROR:
825 return _("Unexpected end of input");
826
827 case LZMA_OK:
828 case LZMA_STREAM_END:
829 case LZMA_GET_CHECK:
830 case LZMA_PROG_ERROR:
831 case LZMA_SEEK_NEEDED:
832 case LZMA_RET_INTERNAL1:
833 case LZMA_RET_INTERNAL2:
834 case LZMA_RET_INTERNAL3:
835 case LZMA_RET_INTERNAL4:
836 case LZMA_RET_INTERNAL5:
837 case LZMA_RET_INTERNAL6:
838 case LZMA_RET_INTERNAL7:
839 case LZMA_RET_INTERNAL8:
840 // Without "default", compiler will warn if new constants
841 // are added to lzma_ret, it is not too easy to forget to
842 // add the new constants to this function.
843 break;
844 }
845
846 return _("Internal error (bug)");
847 }
848
849
850 extern void
message_mem_needed(enum message_verbosity v,uint64_t memusage)851 message_mem_needed(enum message_verbosity v, uint64_t memusage)
852 {
853 if (v > verbosity)
854 return;
855
856 // Convert memusage to MiB, rounding up to the next full MiB.
857 // This way the user can always use the displayed usage as
858 // the new memory usage limit. (If we rounded to the nearest,
859 // the user might need to +1 MiB to get high enough limit.)
860 memusage = round_up_to_mib(memusage);
861
862 uint64_t memlimit = hardware_memlimit_get(opt_mode);
863
864 // Handle the case when there is no memory usage limit.
865 // This way we don't print a weird message with a huge number.
866 if (memlimit == UINT64_MAX) {
867 message(v, _("%s MiB of memory is required. "
868 "The limiter is disabled."),
869 uint64_to_str(memusage, 0));
870 return;
871 }
872
873 // With US-ASCII:
874 // 2^64 with thousand separators + " MiB" suffix + '\0' = 26 + 4 + 1
875 // But there may be multibyte chars so reserve enough space.
876 char memlimitstr[128];
877
878 // Show the memory usage limit as MiB unless it is less than 1 MiB.
879 // This way it's easy to notice errors where one has typed
880 // --memory=123 instead of --memory=123MiB.
881 if (memlimit < (UINT32_C(1) << 20)) {
882 snprintf(memlimitstr, sizeof(memlimitstr), "%s B",
883 uint64_to_str(memlimit, 1));
884 } else {
885 // Round up just like with memusage. If this function is
886 // called for informational purposes (to just show the
887 // current usage and limit), we should never show that
888 // the usage is higher than the limit, which would give
889 // a false impression that the memory usage limit isn't
890 // properly enforced.
891 snprintf(memlimitstr, sizeof(memlimitstr), "%s MiB",
892 uint64_to_str(round_up_to_mib(memlimit), 1));
893 }
894
895 message(v, _("%s MiB of memory is required. The limit is %s."),
896 uint64_to_str(memusage, 0), memlimitstr);
897
898 return;
899 }
900
901
902 extern void
message_filters_show(enum message_verbosity v,const lzma_filter * filters)903 message_filters_show(enum message_verbosity v, const lzma_filter *filters)
904 {
905 if (v > verbosity)
906 return;
907
908 char *buf;
909 const lzma_ret ret = lzma_str_from_filters(&buf, filters,
910 LZMA_STR_ENCODER | LZMA_STR_GETOPT_LONG, NULL);
911 if (ret != LZMA_OK)
912 message_fatal("%s", message_strm(ret));
913
914 fprintf(stderr, _("%s: Filter chain: %s\n"), progname, buf);
915 free(buf);
916 return;
917 }
918
919
920 extern void
message_try_help(void)921 message_try_help(void)
922 {
923 // Print this with V_WARNING instead of V_ERROR to prevent it from
924 // showing up when --quiet has been specified.
925 message(V_WARNING, _("Try '%s --help' for more information."),
926 progname);
927 return;
928 }
929
930
931 extern void
message_version(void)932 message_version(void)
933 {
934 // It is possible that liblzma version is different than the command
935 // line tool version, so print both.
936 if (opt_robot) {
937 printf("XZ_VERSION=%" PRIu32 "\nLIBLZMA_VERSION=%" PRIu32 "\n",
938 LZMA_VERSION, lzma_version_number());
939 } else {
940 printf("xz (" PACKAGE_NAME ") " LZMA_VERSION_STRING "\n");
941 printf("liblzma %s\n", lzma_version_string());
942 }
943
944 tuklib_exit(E_SUCCESS, E_ERROR, verbosity != V_SILENT);
945 }
946
947
948 static void
detect_wrapping_errors(int error_mask)949 detect_wrapping_errors(int error_mask)
950 {
951 #ifndef NDEBUG
952 // This might help in catching problematic strings in translations.
953 // It's a debug message so don't translate this.
954 if (error_mask & TUKLIB_WRAP_WARN_OVERLONG)
955 message_fatal("The help text contains overlong lines");
956 #endif
957
958 if (error_mask & ~TUKLIB_WRAP_WARN_OVERLONG)
959 message_fatal(_("Error printing the help text "
960 "(error code %d)"), error_mask);
961
962 return;
963 }
964
965
966 extern void
message_help(bool long_help)967 message_help(bool long_help)
968 {
969 static const struct tuklib_wrap_opt wrap0 = { 0, 0, 0, 0, 79 };
970 static const struct tuklib_wrap_opt wrap1 = { 1, 1, 1, 1, 79 };
971 static const struct tuklib_wrap_opt wrap2 = { 2, 2, 22, 22, 79 };
972 static const struct tuklib_wrap_opt wrap3 = { 24, 24, 36, 36, 79 };
973
974 // Accumulated error codes from tuklib_wraps() and tuklib_wrapf()
975 int e = 0;
976
977 printf(_("Usage: %s [OPTION]... [FILE]...\n"), progname);
978 e |= tuklib_wraps(stdout, &wrap0,
979 W_("Compress or decompress FILEs in the .xz format."));
980 putchar('\n');
981
982 e |= tuklib_wraps(stdout, &wrap0,
983 W_("Mandatory arguments to long options are "
984 "mandatory for short options too."));
985 putchar('\n');
986
987 if (long_help) {
988 e |= tuklib_wraps(stdout, &wrap1, W_("Operation mode:"));
989 putchar('\n');
990 }
991
992 e |= tuklib_wrapf(stdout, &wrap2,
993 "-z, --compress\v%s\r"
994 "-d, --decompress\v%s\r"
995 "-t, --test\v%s\r"
996 "-l, --list\v%s",
997 W_("force compression"),
998 W_("force decompression"),
999 W_("test compressed file integrity"),
1000 W_("list information about .xz files"));
1001
1002 if (long_help) {
1003 putchar('\n');
1004 e |= tuklib_wraps(stdout, &wrap1, W_("Operation modifiers:"));
1005 putchar('\n');
1006 }
1007
1008 e |= tuklib_wrapf(stdout, &wrap2,
1009 "-k, --keep\v%s\r"
1010 "-f, --force\v%s\r"
1011 "-c, --stdout\v%s",
1012 W_("keep (don't delete) input files"),
1013 W_("force overwrite of output file and (de)compress links"),
1014 W_("write to standard output and don't delete input files"));
1015 // NOTE: --to-stdout isn't included above because it's not
1016 // the recommended spelling. It was copied from gzip but other
1017 // compressors with gzip-like syntax don't support it.
1018
1019 if (long_help) {
1020 e |= tuklib_wrapf(stdout, &wrap2,
1021 " --no-sync\v%s\r"
1022 " --single-stream\v%s\r"
1023 " --no-sparse\v%s\r"
1024 "-S, --suffix=%s\v%s\r"
1025 " --files[=%s]\v%s\r"
1026 " --files0[=%s]\v%s\r",
1027 W_("don't synchronize the output file to the storage "
1028 "device before removing the input file"),
1029 W_("decompress only the first stream, and silently "
1030 "ignore possible remaining input data"),
1031 W_("do not create sparse files when decompressing"),
1032 _(".SUF"),
1033 W_("use the suffix '.SUF' on compressed files"),
1034 _("FILE"),
1035 W_("read filenames to process from FILE; "
1036 "if FILE is omitted, "
1037 "filenames are read from the standard input; "
1038 "filenames must be terminated with "
1039 "the newline character"),
1040 _("FILE"),
1041 W_("like --files but use the null character as "
1042 "terminator"));
1043
1044 e |= tuklib_wraps(stdout, &wrap1,
1045 W_("Basic file format and compression options:"));
1046
1047 e |= tuklib_wrapf(stdout, &wrap2,
1048 "\n"
1049 "-F, --format=%s\v%s\r"
1050 "-C, --check=%s\v%s\r"
1051 " --ignore-check\v%s",
1052 _("FORMAT"),
1053 W_("file format to encode or decode; possible values "
1054 "are 'auto' (default), 'xz', 'lzma', 'lzip', "
1055 "and 'raw'"),
1056 _("NAME"),
1057 W_("integrity check type: 'none' (use with caution), "
1058 "'crc32', 'crc64' (default), or 'sha256'"),
1059 W_("don't verify the integrity check when "
1060 "decompressing"));
1061 }
1062
1063 e |= tuklib_wrapf(stdout, &wrap2,
1064 "-0 ... -9\v%s\r"
1065 "-e, --extreme\v%s\r"
1066 "-T, --threads=%s\v%s",
1067 W_("compression preset; default is 6; take compressor *and* "
1068 "decompressor memory usage into account before "
1069 "using 7-9!"),
1070 W_("try to improve compression ratio by using more CPU time; "
1071 "does not affect decompressor memory requirements"),
1072 // TRANSLATORS: Short for NUMBER. A longer string is fine but
1073 // wider than 5 columns makes --long-help a few lines longer.
1074 _("NUM"),
1075 W_("use at most NUM threads; the default is 0 which uses "
1076 "as many threads as there are processor cores"));
1077
1078 if (long_help) {
1079 e |= tuklib_wrapf(stdout, &wrap2,
1080 " --block-size=%s\v%s\r"
1081 " --block-list=%s\v%s\r"
1082 " --flush-timeout=%s\v%s",
1083 _("SIZE"),
1084 W_("start a new .xz block after every SIZE bytes "
1085 "of input; use this to set the block size "
1086 "for threaded compression"),
1087 _("BLOCKS"),
1088 W_("start a new .xz block after the given "
1089 "comma-separated intervals of uncompressed "
1090 "data; optionally, specify a "
1091 "filter chain number (0-9) followed by "
1092 "a ':' before the uncompressed data size"),
1093 _("NUM"),
1094 W_("when compressing, if more than NUM "
1095 "milliseconds has passed since the previous "
1096 "flush and reading more input would block, "
1097 "all pending data is flushed out"));
1098
1099 e |= tuklib_wrapf(stdout, &wrap2,
1100 " --memlimit-compress=%s\n"
1101 " --memlimit-decompress=%s\n"
1102 " --memlimit-mt-decompress=%s\n"
1103 "-M, --memlimit=%s\v%s\r"
1104 " --no-adjust\v%s",
1105 _("LIMIT"),
1106 _("LIMIT"),
1107 _("LIMIT"),
1108 _("LIMIT"),
1109 // xgettext:no-c-format
1110 W_("set memory usage limit for compression, "
1111 "decompression, threaded decompression, "
1112 "or all of these; LIMIT is in "
1113 "bytes, % of RAM, or 0 for defaults"),
1114 W_("if compression settings exceed the "
1115 "memory usage limit, "
1116 "give an error instead of adjusting "
1117 "the settings downwards"));
1118 }
1119
1120 if (long_help) {
1121 putchar('\n');
1122
1123 e |= tuklib_wraps(stdout, &wrap1,
1124 W_("Custom filter chain for compression "
1125 "(an alternative to using presets):"));
1126
1127 e |= tuklib_wrapf(stdout, &wrap2,
1128 "\n"
1129 "--filters=%s\v%s\r"
1130 "--filters1=%s ... --filters9=%s\v%s\r"
1131 "--filters-help\v%s",
1132 _("FILTERS"),
1133 W_("set the filter chain using the "
1134 "liblzma filter string syntax; "
1135 "use --filters-help for more information"),
1136 _("FILTERS"),
1137 _("FILTERS"),
1138 W_("set additional filter chains using the "
1139 "liblzma filter string syntax to use "
1140 "with --block-list"),
1141 W_("display more information about the "
1142 "liblzma filter string syntax and exit"));
1143
1144 #if defined(HAVE_ENCODER_LZMA1) || defined(HAVE_DECODER_LZMA1) \
1145 || defined(HAVE_ENCODER_LZMA2) || defined(HAVE_DECODER_LZMA2)
1146 e |= tuklib_wrapf(stdout, &wrap2,
1147 "\n"
1148 "--lzma1[=%s]\n"
1149 "--lzma2[=%s]\v%s",
1150 // TRANSLATORS: Short for OPTIONS.
1151 _("OPTS"),
1152 _("OPTS"),
1153 // TRANSLATORS: Use semicolon (or its fullwidth form)
1154 // in "(valid values; default)" even if it is weird in
1155 // your language. There are non-translatable strings
1156 // that look like "(foo, bar, baz; foo)" which list
1157 // the supported values and the default value.
1158 W_("LZMA1 or LZMA2; OPTS is a comma-separated list "
1159 "of zero or more of the following options "
1160 "(valid values; default):"));
1161
1162 e |= tuklib_wrapf(stdout, &wrap3,
1163 "preset=%s\v%s (0-9[e])\r"
1164 "dict=%s\v%s \b(4KiB - 1536MiB; 8MiB)\b\r"
1165 "lc=%s\v%s \b(0-4; 3)\b\r"
1166 "lp=%s\v%s \b(0-4; 0)\b\r"
1167 "pb=%s\v%s \b(0-4; 2)\b\r"
1168 "mode=%s\v%s (fast, normal; normal)\r"
1169 "nice=%s\v%s \b(2-273; 64)\b\r"
1170 "mf=%s\v%s (hc3, hc4, bt2, bt3, bt4; bt4)\r"
1171 "depth=%s\v%s",
1172 // TRANSLATORS: Short for PRESET. A longer string is
1173 // fine but wider than 4 columns makes --long-help
1174 // one line longer.
1175 _("PRE"),
1176 W_("reset options to a preset"),
1177 _("NUM"), W_("dictionary size"),
1178 _("NUM"),
1179 // TRANSLATORS: The word "literal" in "literal context
1180 // bits" means how many "context bits" to use when
1181 // encoding literals. A literal is a single 8-bit
1182 // byte. It doesn't mean "literally" here.
1183 W_("number of literal context bits"),
1184 _("NUM"), W_("number of literal position bits"),
1185 _("NUM"), W_("number of position bits"),
1186 _("MODE"), W_("compression mode"),
1187 _("NUM"), W_("nice length of a match"),
1188 _("NAME"), W_("match finder"),
1189 _("NUM"), W_("maximum search depth; "
1190 "0=automatic (default)"));
1191 #endif
1192
1193 e |= tuklib_wrapf(stdout, &wrap2,
1194 "\n"
1195 "--x86[=%s]\v%s\r"
1196 "--arm[=%s]\v%s\r"
1197 "--armthumb[=%s]\v%s\r"
1198 "--arm64[=%s]\v%s\r"
1199 "--powerpc[=%s]\v%s\r"
1200 "--ia64[=%s]\v%s\r"
1201 "--sparc[=%s]\v%s\r"
1202 "--riscv[=%s]\v%s\r"
1203 "\v%s",
1204 _("OPTS"),
1205 W_("x86 BCJ filter (32-bit and 64-bit)"),
1206 _("OPTS"),
1207 W_("ARM BCJ filter"),
1208 _("OPTS"),
1209 W_("ARM-Thumb BCJ filter"),
1210 _("OPTS"),
1211 W_("ARM64 BCJ filter"),
1212 _("OPTS"),
1213 W_("PowerPC BCJ filter (big endian only)"),
1214 _("OPTS"),
1215 W_("IA-64 (Itanium) BCJ filter"),
1216 _("OPTS"),
1217 W_("SPARC BCJ filter"),
1218 _("OPTS"),
1219 W_("RISC-V BCJ filter"),
1220 W_("Valid OPTS for all BCJ filters:"));
1221 e |= tuklib_wrapf(stdout, &wrap3,
1222 "start=%s\v%s",
1223 _("NUM"),
1224 W_("start offset for conversions (default=0)"));
1225
1226 #if defined(HAVE_ENCODER_DELTA) || defined(HAVE_DECODER_DELTA)
1227 e |= tuklib_wrapf(stdout, &wrap2,
1228 "\n"
1229 "--delta[=%s]\v%s",
1230 _("OPTS"),
1231 W_("Delta filter; valid OPTS "
1232 "(valid values; default):"));
1233 e |= tuklib_wrapf(stdout, &wrap3,
1234 "dist=%s\v%s \b(1-256; 1)\b",
1235 _("NUM"),
1236 W_("distance between bytes being subtracted "
1237 "from each other"));
1238 #endif
1239 }
1240
1241 if (long_help) {
1242 putchar('\n');
1243 e |= tuklib_wraps(stdout, &wrap1, W_("Other options:"));
1244 putchar('\n');
1245 }
1246
1247 e |= tuklib_wrapf(stdout, &wrap2,
1248 "-q, --quiet\v%s\r"
1249 "-v, --verbose\v%s",
1250 W_("suppress warnings; specify twice to suppress errors too"),
1251 W_("be verbose; specify twice for even more verbose"));
1252
1253 if (long_help) {
1254 e |= tuklib_wrapf(stdout, &wrap2,
1255 "-Q, --no-warn\v%s\r"
1256 " --robot\v%s\r"
1257 "\n"
1258 " --info-memory\v%s\r"
1259 "-h, --help\v%s\r"
1260 "-H, --long-help\v%s",
1261 W_("make warnings not affect the exit status"),
1262 W_("use machine-parsable messages (useful for scripts)"),
1263 W_("display the total amount of RAM and the currently active "
1264 "memory usage limits, and exit"),
1265 W_("display the short help (lists only the basic options)"),
1266 W_("display this long help and exit"));
1267 } else {
1268 e |= tuklib_wrapf(stdout, &wrap2,
1269 "-h, --help\v%s\r"
1270 "-H, --long-help\v%s",
1271 W_("display this short help and exit"),
1272 W_("display the long help (lists also the advanced options)"));
1273 }
1274
1275 e |= tuklib_wrapf(stdout, &wrap2, "-V, --version\v%s",
1276 W_("display the version number and exit"));
1277
1278 putchar('\n');
1279 e |= tuklib_wraps(stdout, &wrap0,
1280 W_("With no FILE, or when FILE is -, read standard input."));
1281 putchar('\n');
1282
1283 e |= tuklib_wrapf(stdout, &wrap0,
1284 // TRANSLATORS: This message indicates the bug reporting
1285 // address for this package. Please add another line saying
1286 // "\nReport translation bugs to <...>." with the email or WWW
1287 // address for translation bugs. Thanks!
1288 W_("Report bugs to <%s> (in English or Finnish)."),
1289 PACKAGE_BUGREPORT);
1290
1291 e |= tuklib_wrapf(stdout, &wrap0,
1292 // TRANSLATORS: The first %s is the name of this software.
1293 // The second <%s> is an URL.
1294 W_("%s home page: <%s>"), PACKAGE_NAME, PACKAGE_URL);
1295
1296 #if LZMA_VERSION_STABILITY != LZMA_VERSION_STABILITY_STABLE
1297 e |= tuklib_wraps(stdout, &wrap0, W_(
1298 "THIS IS A DEVELOPMENT VERSION NOT INTENDED FOR PRODUCTION USE."));
1299 #endif
1300
1301 detect_wrapping_errors(e);
1302 tuklib_exit(E_SUCCESS, E_ERROR, verbosity != V_SILENT);
1303 }
1304
1305
1306 extern void
message_filters_help(void)1307 message_filters_help(void)
1308 {
1309 static const struct tuklib_wrap_opt wrap = { .right_margin = 76 };
1310
1311 char *encoder_options;
1312 if (lzma_str_list_filters(&encoder_options, LZMA_VLI_UNKNOWN,
1313 LZMA_STR_ENCODER, NULL) != LZMA_OK)
1314 message_bug();
1315
1316 if (!opt_robot) {
1317 int e = tuklib_wrapf(stdout, &wrap,
1318 W_("Filter chains are set using the --filters=FILTERS or "
1319 "--filters1=FILTERS ... --filters9=FILTERS options. "
1320 "Each filter in the chain can be separated by spaces or '--'. "
1321 "Alternatively a preset %s can be specified instead of a filter chain."),
1322 "<0-9>[e]");
1323 putchar('\n');
1324 e |= tuklib_wraps(stdout, &wrap,
1325 W_("The supported filters and their options are:"));
1326
1327 detect_wrapping_errors(e);
1328 }
1329
1330 puts(encoder_options);
1331
1332 tuklib_exit(E_SUCCESS, E_ERROR, verbosity != V_SILENT);
1333 }
1334