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