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 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 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 142 message_verbosity_increase(void) 143 { 144 if (verbosity < V_DEBUG) 145 ++verbosity; 146 147 return; 148 } 149 150 151 extern 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 162 message_verbosity_get(void) 163 { 164 return verbosity; 165 } 166 167 168 extern void 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 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 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 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 * 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 * 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 * 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 * 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 * 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 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 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 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 fprintf(stderr, _("%s: "), tuklib_mask_nonprint(filename)); 654 655 // Percentage is printed only if we didn't finish yet. 656 if (!finished) { 657 // Don't print the percentage when it isn't known 658 // (starts with a dash). 659 const char *percentage = progress_percentage(in_pos); 660 if (percentage[0] != '-') 661 fprintf(stderr, "%s, ", percentage); 662 } 663 664 // Size information is always printed. 665 fprintf(stderr, "%s", progress_sizes( 666 compressed_pos, uncompressed_pos, true)); 667 668 // The speed and elapsed time aren't always shown. 669 const char *speed = progress_speed(uncompressed_pos, elapsed); 670 if (speed[0] != '\0') 671 fprintf(stderr, ", %s", speed); 672 673 const char *elapsed_str = progress_time(elapsed); 674 if (elapsed_str[0] != '\0') 675 fprintf(stderr, ", %s", elapsed_str); 676 677 fputc('\n', stderr); 678 } 679 680 signals_unblock(); 681 682 return; 683 } 684 685 686 extern void 687 message_progress_end(bool success) 688 { 689 assert(progress_started); 690 progress_flush(success); 691 progress_started = false; 692 return; 693 } 694 695 696 static void 697 vmessage(enum message_verbosity v, const char *fmt, va_list ap) 698 { 699 if (v <= verbosity) { 700 signals_block(); 701 702 progress_flush(false); 703 704 // TRANSLATORS: This is the program name in the beginning 705 // of the line in messages. Usually it becomes "xz: ". 706 // This is a translatable string because French needs 707 // a space before a colon. 708 fprintf(stderr, _("%s: "), progname); 709 710 #ifdef __clang__ 711 # pragma GCC diagnostic push 712 # pragma GCC diagnostic ignored "-Wformat-nonliteral" 713 #endif 714 vfprintf(stderr, fmt, ap); 715 #ifdef __clang__ 716 # pragma GCC diagnostic pop 717 #endif 718 719 fputc('\n', stderr); 720 721 signals_unblock(); 722 } 723 724 return; 725 } 726 727 728 extern void 729 message(enum message_verbosity v, const char *fmt, ...) 730 { 731 va_list ap; 732 va_start(ap, fmt); 733 vmessage(v, fmt, ap); 734 va_end(ap); 735 return; 736 } 737 738 739 extern void 740 message_warning(const char *fmt, ...) 741 { 742 va_list ap; 743 va_start(ap, fmt); 744 vmessage(V_WARNING, fmt, ap); 745 va_end(ap); 746 747 set_exit_status(E_WARNING); 748 return; 749 } 750 751 752 extern void 753 message_error(const char *fmt, ...) 754 { 755 va_list ap; 756 va_start(ap, fmt); 757 vmessage(V_ERROR, fmt, ap); 758 va_end(ap); 759 760 set_exit_status(E_ERROR); 761 return; 762 } 763 764 765 extern void 766 message_fatal(const char *fmt, ...) 767 { 768 va_list ap; 769 va_start(ap, fmt); 770 vmessage(V_ERROR, fmt, ap); 771 va_end(ap); 772 773 tuklib_exit(E_ERROR, E_ERROR, false); 774 } 775 776 777 extern void 778 message_bug(void) 779 { 780 message_fatal(_("Internal error (bug)")); 781 } 782 783 784 extern void 785 message_signal_handler(void) 786 { 787 message_fatal(_("Cannot establish signal handlers")); 788 } 789 790 791 extern const char * 792 message_strm(lzma_ret code) 793 { 794 switch (code) { 795 case LZMA_NO_CHECK: 796 return _("No integrity check; not verifying file integrity"); 797 798 case LZMA_UNSUPPORTED_CHECK: 799 return _("Unsupported type of integrity check; " 800 "not verifying file integrity"); 801 802 case LZMA_MEM_ERROR: 803 return strerror(ENOMEM); 804 805 case LZMA_MEMLIMIT_ERROR: 806 return _("Memory usage limit reached"); 807 808 case LZMA_FORMAT_ERROR: 809 return _("File format not recognized"); 810 811 case LZMA_OPTIONS_ERROR: 812 return _("Unsupported options"); 813 814 case LZMA_DATA_ERROR: 815 return _("Compressed data is corrupt"); 816 817 case LZMA_BUF_ERROR: 818 return _("Unexpected end of input"); 819 820 case LZMA_OK: 821 case LZMA_STREAM_END: 822 case LZMA_GET_CHECK: 823 case LZMA_PROG_ERROR: 824 case LZMA_SEEK_NEEDED: 825 case LZMA_RET_INTERNAL1: 826 case LZMA_RET_INTERNAL2: 827 case LZMA_RET_INTERNAL3: 828 case LZMA_RET_INTERNAL4: 829 case LZMA_RET_INTERNAL5: 830 case LZMA_RET_INTERNAL6: 831 case LZMA_RET_INTERNAL7: 832 case LZMA_RET_INTERNAL8: 833 // Without "default", compiler will warn if new constants 834 // are added to lzma_ret, it is not too easy to forget to 835 // add the new constants to this function. 836 break; 837 } 838 839 return _("Internal error (bug)"); 840 } 841 842 843 extern void 844 message_mem_needed(enum message_verbosity v, uint64_t memusage) 845 { 846 if (v > verbosity) 847 return; 848 849 // Convert memusage to MiB, rounding up to the next full MiB. 850 // This way the user can always use the displayed usage as 851 // the new memory usage limit. (If we rounded to the nearest, 852 // the user might need to +1 MiB to get high enough limit.) 853 memusage = round_up_to_mib(memusage); 854 855 uint64_t memlimit = hardware_memlimit_get(opt_mode); 856 857 // Handle the case when there is no memory usage limit. 858 // This way we don't print a weird message with a huge number. 859 if (memlimit == UINT64_MAX) { 860 message(v, _("%s MiB of memory is required. " 861 "The limiter is disabled."), 862 uint64_to_str(memusage, 0)); 863 return; 864 } 865 866 // With US-ASCII: 867 // 2^64 with thousand separators + " MiB" suffix + '\0' = 26 + 4 + 1 868 // But there may be multibyte chars so reserve enough space. 869 char memlimitstr[128]; 870 871 // Show the memory usage limit as MiB unless it is less than 1 MiB. 872 // This way it's easy to notice errors where one has typed 873 // --memory=123 instead of --memory=123MiB. 874 if (memlimit < (UINT32_C(1) << 20)) { 875 snprintf(memlimitstr, sizeof(memlimitstr), "%s B", 876 uint64_to_str(memlimit, 1)); 877 } else { 878 // Round up just like with memusage. If this function is 879 // called for informational purposes (to just show the 880 // current usage and limit), we should never show that 881 // the usage is higher than the limit, which would give 882 // a false impression that the memory usage limit isn't 883 // properly enforced. 884 snprintf(memlimitstr, sizeof(memlimitstr), "%s MiB", 885 uint64_to_str(round_up_to_mib(memlimit), 1)); 886 } 887 888 message(v, _("%s MiB of memory is required. The limit is %s."), 889 uint64_to_str(memusage, 0), memlimitstr); 890 891 return; 892 } 893 894 895 extern void 896 message_filters_show(enum message_verbosity v, const lzma_filter *filters) 897 { 898 if (v > verbosity) 899 return; 900 901 char *buf; 902 const lzma_ret ret = lzma_str_from_filters(&buf, filters, 903 LZMA_STR_ENCODER | LZMA_STR_GETOPT_LONG, NULL); 904 if (ret != LZMA_OK) 905 message_fatal("%s", message_strm(ret)); 906 907 fprintf(stderr, _("%s: Filter chain: %s\n"), progname, buf); 908 free(buf); 909 return; 910 } 911 912 913 extern void 914 message_try_help(void) 915 { 916 // Print this with V_WARNING instead of V_ERROR to prevent it from 917 // showing up when --quiet has been specified. 918 message(V_WARNING, _("Try '%s --help' for more information."), 919 progname); 920 return; 921 } 922 923 924 extern void 925 message_version(void) 926 { 927 // It is possible that liblzma version is different than the command 928 // line tool version, so print both. 929 if (opt_robot) { 930 printf("XZ_VERSION=%" PRIu32 "\nLIBLZMA_VERSION=%" PRIu32 "\n", 931 LZMA_VERSION, lzma_version_number()); 932 } else { 933 printf("xz (" PACKAGE_NAME ") " LZMA_VERSION_STRING "\n"); 934 printf("liblzma %s\n", lzma_version_string()); 935 } 936 937 tuklib_exit(E_SUCCESS, E_ERROR, verbosity != V_SILENT); 938 } 939 940 941 static void 942 detect_wrapping_errors(int error_mask) 943 { 944 #ifndef NDEBUG 945 // This might help in catching problematic strings in translations. 946 // It's a debug message so don't translate this. 947 if (error_mask & TUKLIB_WRAP_WARN_OVERLONG) 948 message_fatal("The help text contains overlong lines"); 949 #endif 950 951 if (error_mask & ~TUKLIB_WRAP_WARN_OVERLONG) 952 message_fatal(_("Error printing the help text " 953 "(error code %d)"), error_mask); 954 955 return; 956 } 957 958 959 extern void 960 message_help(bool long_help) 961 { 962 static const struct tuklib_wrap_opt wrap0 = { 0, 0, 0, 0, 79 }; 963 static const struct tuklib_wrap_opt wrap1 = { 1, 1, 1, 1, 79 }; 964 static const struct tuklib_wrap_opt wrap2 = { 2, 2, 22, 22, 79 }; 965 static const struct tuklib_wrap_opt wrap3 = { 24, 24, 36, 36, 79 }; 966 967 // Accumulated error codes from tuklib_wraps() and tuklib_wrapf() 968 int e = 0; 969 970 printf(_("Usage: %s [OPTION]... [FILE]...\n"), progname); 971 e |= tuklib_wraps(stdout, &wrap0, 972 W_("Compress or decompress FILEs in the .xz format.")); 973 putchar('\n'); 974 975 e |= tuklib_wraps(stdout, &wrap0, 976 W_("Mandatory arguments to long options are " 977 "mandatory for short options too.")); 978 putchar('\n'); 979 980 if (long_help) { 981 e |= tuklib_wraps(stdout, &wrap1, W_("Operation mode:")); 982 putchar('\n'); 983 } 984 985 e |= tuklib_wrapf(stdout, &wrap2, 986 "-z, --compress\v%s\r" 987 "-d, --decompress\v%s\r" 988 "-t, --test\v%s\r" 989 "-l, --list\v%s", 990 W_("force compression"), 991 W_("force decompression"), 992 W_("test compressed file integrity"), 993 W_("list information about .xz files")); 994 995 if (long_help) { 996 putchar('\n'); 997 e |= tuklib_wraps(stdout, &wrap1, W_("Operation modifiers:")); 998 putchar('\n'); 999 } 1000 1001 e |= tuklib_wrapf(stdout, &wrap2, 1002 "-k, --keep\v%s\r" 1003 "-f, --force\v%s\r" 1004 "-c, --stdout\v%s", 1005 W_("keep (don't delete) input files"), 1006 W_("force overwrite of output file and (de)compress links"), 1007 W_("write to standard output and don't delete input files")); 1008 // NOTE: --to-stdout isn't included above because it's not 1009 // the recommended spelling. It was copied from gzip but other 1010 // compressors with gzip-like syntax don't support it. 1011 1012 if (long_help) { 1013 e |= tuklib_wrapf(stdout, &wrap2, 1014 " --no-sync\v%s\r" 1015 " --single-stream\v%s\r" 1016 " --no-sparse\v%s\r" 1017 "-S, --suffix=%s\v%s\r" 1018 " --files[=%s]\v%s\r" 1019 " --files0[=%s]\v%s\r", 1020 W_("don't synchronize the output file to the storage " 1021 "device before removing the input file"), 1022 W_("decompress only the first stream, and silently " 1023 "ignore possible remaining input data"), 1024 W_("do not create sparse files when decompressing"), 1025 _(".SUF"), 1026 W_("use the suffix '.SUF' on compressed files"), 1027 _("FILE"), 1028 W_("read filenames to process from FILE; " 1029 "if FILE is omitted, " 1030 "filenames are read from the standard input; " 1031 "filenames must be terminated with " 1032 "the newline character"), 1033 _("FILE"), 1034 W_("like --files but use the null character as " 1035 "terminator")); 1036 1037 e |= tuklib_wraps(stdout, &wrap1, 1038 W_("Basic file format and compression options:")); 1039 1040 e |= tuklib_wrapf(stdout, &wrap2, 1041 "\n" 1042 "-F, --format=%s\v%s\r" 1043 "-C, --check=%s\v%s\r" 1044 " --ignore-check\v%s", 1045 _("FORMAT"), 1046 W_("file format to encode or decode; possible values " 1047 "are 'auto' (default), 'xz', 'lzma', 'lzip', " 1048 "and 'raw'"), 1049 _("NAME"), 1050 W_("integrity check type: 'none' (use with caution), " 1051 "'crc32', 'crc64' (default), or 'sha256'"), 1052 W_("don't verify the integrity check when " 1053 "decompressing")); 1054 } 1055 1056 e |= tuklib_wrapf(stdout, &wrap2, 1057 "-0 ... -9\v%s\r" 1058 "-e, --extreme\v%s\r" 1059 "-T, --threads=%s\v%s", 1060 W_("compression preset; default is 6; take compressor *and* " 1061 "decompressor memory usage into account before " 1062 "using 7-9!"), 1063 W_("try to improve compression ratio by using more CPU time; " 1064 "does not affect decompressor memory requirements"), 1065 // TRANSLATORS: Short for NUMBER. A longer string is fine but 1066 // wider than 5 columns makes --long-help a few lines longer. 1067 _("NUM"), 1068 W_("use at most NUM threads; the default is 0 which uses " 1069 "as many threads as there are processor cores")); 1070 1071 if (long_help) { 1072 e |= tuklib_wrapf(stdout, &wrap2, 1073 " --block-size=%s\v%s\r" 1074 " --block-list=%s\v%s\r" 1075 " --flush-timeout=%s\v%s", 1076 _("SIZE"), 1077 W_("start a new .xz block after every SIZE bytes " 1078 "of input; use this to set the block size " 1079 "for threaded compression"), 1080 _("BLOCKS"), 1081 W_("start a new .xz block after the given " 1082 "comma-separated intervals of uncompressed " 1083 "data; optionally, specify a " 1084 "filter chain number (0-9) followed by " 1085 "a ':' before the uncompressed data size"), 1086 _("NUM"), 1087 W_("when compressing, if more than NUM " 1088 "milliseconds has passed since the previous " 1089 "flush and reading more input would block, " 1090 "all pending data is flushed out")); 1091 1092 e |= tuklib_wrapf(stdout, &wrap2, 1093 " --memlimit-compress=%s\n" 1094 " --memlimit-decompress=%s\n" 1095 " --memlimit-mt-decompress=%s\n" 1096 "-M, --memlimit=%s\v%s\r" 1097 " --no-adjust\v%s", 1098 _("LIMIT"), 1099 _("LIMIT"), 1100 _("LIMIT"), 1101 _("LIMIT"), 1102 // xgettext:no-c-format 1103 W_("set memory usage limit for compression, " 1104 "decompression, threaded decompression, " 1105 "or all of these; LIMIT is in " 1106 "bytes, % of RAM, or 0 for defaults"), 1107 W_("if compression settings exceed the " 1108 "memory usage limit, " 1109 "give an error instead of adjusting " 1110 "the settings downwards")); 1111 } 1112 1113 if (long_help) { 1114 putchar('\n'); 1115 1116 e |= tuklib_wraps(stdout, &wrap1, 1117 W_("Custom filter chain for compression " 1118 "(an alternative to using presets):")); 1119 1120 e |= tuklib_wrapf(stdout, &wrap2, 1121 "\n" 1122 "--filters=%s\v%s\r" 1123 "--filters1=%s ... --filters9=%s\v%s\r" 1124 "--filters-help\v%s", 1125 _("FILTERS"), 1126 W_("set the filter chain using the " 1127 "liblzma filter string syntax; " 1128 "use --filters-help for more information"), 1129 _("FILTERS"), 1130 _("FILTERS"), 1131 W_("set additional filter chains using the " 1132 "liblzma filter string syntax to use " 1133 "with --block-list"), 1134 W_("display more information about the " 1135 "liblzma filter string syntax and exit")); 1136 1137 #if defined(HAVE_ENCODER_LZMA1) || defined(HAVE_DECODER_LZMA1) \ 1138 || defined(HAVE_ENCODER_LZMA2) || defined(HAVE_DECODER_LZMA2) 1139 e |= tuklib_wrapf(stdout, &wrap2, 1140 "\n" 1141 "--lzma1[=%s]\n" 1142 "--lzma2[=%s]\v%s", 1143 // TRANSLATORS: Short for OPTIONS. 1144 _("OPTS"), 1145 _("OPTS"), 1146 // TRANSLATORS: Use semicolon (or its fullwidth form) 1147 // in "(valid values; default)" even if it is weird in 1148 // your language. There are non-translatable strings 1149 // that look like "(foo, bar, baz; foo)" which list 1150 // the supported values and the default value. 1151 W_("LZMA1 or LZMA2; OPTS is a comma-separated list " 1152 "of zero or more of the following options " 1153 "(valid values; default):")); 1154 1155 e |= tuklib_wrapf(stdout, &wrap3, 1156 "preset=%s\v%s (0-9[e])\r" 1157 "dict=%s\v%s \b(4KiB - 1536MiB; 8MiB)\b\r" 1158 "lc=%s\v%s \b(0-4; 3)\b\r" 1159 "lp=%s\v%s \b(0-4; 0)\b\r" 1160 "pb=%s\v%s \b(0-4; 2)\b\r" 1161 "mode=%s\v%s (fast, normal; normal)\r" 1162 "nice=%s\v%s \b(2-273; 64)\b\r" 1163 "mf=%s\v%s (hc3, hc4, bt2, bt3, bt4; bt4)\r" 1164 "depth=%s\v%s", 1165 // TRANSLATORS: Short for PRESET. A longer string is 1166 // fine but wider than 4 columns makes --long-help 1167 // one line longer. 1168 _("PRE"), 1169 W_("reset options to a preset"), 1170 _("NUM"), W_("dictionary size"), 1171 _("NUM"), 1172 // TRANSLATORS: The word "literal" in "literal context 1173 // bits" means how many "context bits" to use when 1174 // encoding literals. A literal is a single 8-bit 1175 // byte. It doesn't mean "literally" here. 1176 W_("number of literal context bits"), 1177 _("NUM"), W_("number of literal position bits"), 1178 _("NUM"), W_("number of position bits"), 1179 _("MODE"), W_("compression mode"), 1180 _("NUM"), W_("nice length of a match"), 1181 _("NAME"), W_("match finder"), 1182 _("NUM"), W_("maximum search depth; " 1183 "0=automatic (default)")); 1184 #endif 1185 1186 e |= tuklib_wrapf(stdout, &wrap2, 1187 "\n" 1188 "--x86[=%s]\v%s\r" 1189 "--arm[=%s]\v%s\r" 1190 "--armthumb[=%s]\v%s\r" 1191 "--arm64[=%s]\v%s\r" 1192 "--powerpc[=%s]\v%s\r" 1193 "--ia64[=%s]\v%s\r" 1194 "--sparc[=%s]\v%s\r" 1195 "--riscv[=%s]\v%s\r" 1196 "\v%s", 1197 _("OPTS"), 1198 W_("x86 BCJ filter (32-bit and 64-bit)"), 1199 _("OPTS"), 1200 W_("ARM BCJ filter"), 1201 _("OPTS"), 1202 W_("ARM-Thumb BCJ filter"), 1203 _("OPTS"), 1204 W_("ARM64 BCJ filter"), 1205 _("OPTS"), 1206 W_("PowerPC BCJ filter (big endian only)"), 1207 _("OPTS"), 1208 W_("IA-64 (Itanium) BCJ filter"), 1209 _("OPTS"), 1210 W_("SPARC BCJ filter"), 1211 _("OPTS"), 1212 W_("RISC-V BCJ filter"), 1213 W_("Valid OPTS for all BCJ filters:")); 1214 e |= tuklib_wrapf(stdout, &wrap3, 1215 "start=%s\v%s", 1216 _("NUM"), 1217 W_("start offset for conversions (default=0)")); 1218 1219 #if defined(HAVE_ENCODER_DELTA) || defined(HAVE_DECODER_DELTA) 1220 e |= tuklib_wrapf(stdout, &wrap2, 1221 "\n" 1222 "--delta[=%s]\v%s", 1223 _("OPTS"), 1224 W_("Delta filter; valid OPTS " 1225 "(valid values; default):")); 1226 e |= tuklib_wrapf(stdout, &wrap3, 1227 "dist=%s\v%s \b(1-256; 1)\b", 1228 _("NUM"), 1229 W_("distance between bytes being subtracted " 1230 "from each other")); 1231 #endif 1232 } 1233 1234 if (long_help) { 1235 putchar('\n'); 1236 e |= tuklib_wraps(stdout, &wrap1, W_("Other options:")); 1237 putchar('\n'); 1238 } 1239 1240 e |= tuklib_wrapf(stdout, &wrap2, 1241 "-q, --quiet\v%s\r" 1242 "-v, --verbose\v%s", 1243 W_("suppress warnings; specify twice to suppress errors too"), 1244 W_("be verbose; specify twice for even more verbose")); 1245 1246 if (long_help) { 1247 e |= tuklib_wrapf(stdout, &wrap2, 1248 "-Q, --no-warn\v%s\r" 1249 " --robot\v%s\r" 1250 "\n" 1251 " --info-memory\v%s\r" 1252 "-h, --help\v%s\r" 1253 "-H, --long-help\v%s", 1254 W_("make warnings not affect the exit status"), 1255 W_("use machine-parsable messages (useful for scripts)"), 1256 W_("display the total amount of RAM and the currently active " 1257 "memory usage limits, and exit"), 1258 W_("display the short help (lists only the basic options)"), 1259 W_("display this long help and exit")); 1260 } else { 1261 e |= tuklib_wrapf(stdout, &wrap2, 1262 "-h, --help\v%s\r" 1263 "-H, --long-help\v%s", 1264 W_("display this short help and exit"), 1265 W_("display the long help (lists also the advanced options)")); 1266 } 1267 1268 e |= tuklib_wrapf(stdout, &wrap2, "-V, --version\v%s", 1269 W_("display the version number and exit")); 1270 1271 putchar('\n'); 1272 e |= tuklib_wraps(stdout, &wrap0, 1273 W_("With no FILE, or when FILE is -, read standard input.")); 1274 putchar('\n'); 1275 1276 e |= tuklib_wrapf(stdout, &wrap0, 1277 // TRANSLATORS: This message indicates the bug reporting 1278 // address for this package. Please add another line saying 1279 // "\nReport translation bugs to <...>." with the email or WWW 1280 // address for translation bugs. Thanks! 1281 W_("Report bugs to <%s> (in English or Finnish)."), 1282 PACKAGE_BUGREPORT); 1283 1284 e |= tuklib_wrapf(stdout, &wrap0, 1285 // TRANSLATORS: The first %s is the name of this software. 1286 // The second <%s> is an URL. 1287 W_("%s home page: <%s>"), PACKAGE_NAME, PACKAGE_URL); 1288 1289 #if LZMA_VERSION_STABILITY != LZMA_VERSION_STABILITY_STABLE 1290 e |= tuklib_wraps(stdout, &wrap0, W_( 1291 "THIS IS A DEVELOPMENT VERSION NOT INTENDED FOR PRODUCTION USE.")); 1292 #endif 1293 1294 detect_wrapping_errors(e); 1295 tuklib_exit(E_SUCCESS, E_ERROR, verbosity != V_SILENT); 1296 } 1297 1298 1299 extern void 1300 message_filters_help(void) 1301 { 1302 static const struct tuklib_wrap_opt wrap = { .right_margin = 76 }; 1303 1304 char *encoder_options; 1305 if (lzma_str_list_filters(&encoder_options, LZMA_VLI_UNKNOWN, 1306 LZMA_STR_ENCODER, NULL) != LZMA_OK) 1307 message_bug(); 1308 1309 if (!opt_robot) { 1310 int e = tuklib_wrapf(stdout, &wrap, 1311 W_("Filter chains are set using the --filters=FILTERS or " 1312 "--filters1=FILTERS ... --filters9=FILTERS options. " 1313 "Each filter in the chain can be separated by spaces or '--'. " 1314 "Alternatively a preset %s can be specified instead of a filter chain."), 1315 "<0-9>[e]"); 1316 putchar('\n'); 1317 e |= tuklib_wraps(stdout, &wrap, 1318 W_("The supported filters and their options are:")); 1319 1320 detect_wrapping_errors(e); 1321 } 1322 1323 puts(encoder_options); 1324 1325 tuklib_exit(E_SUCCESS, E_ERROR, verbosity != V_SILENT); 1326 } 1327