1 ///////////////////////////////////////////////////////////////////////////////
4 /// \brief Argument parsing
6 /// \note Filter-specific options parsing is in options.c.
8 // Author: Lasse Collin
10 // This file has been put into the public domain.
11 // You can do whatever you want with this file.
13 ///////////////////////////////////////////////////////////////////////////////
21 bool opt_stdout = false;
22 bool opt_force = false;
23 bool opt_keep_original = false;
24 bool opt_robot = false;
25 bool opt_ignore_check = false;
27 // We don't modify or free() this, but we need to assign it in some
28 // non-const pointers.
29 const char stdin_filename[] = "(stdin)";
32 /// Parse and set the memory usage limit for compression and/or decompression.
34 parse_memlimit(const char *name, const char *name_percentage, char *str,
35 bool set_compress, bool set_decompress)
37 bool is_percentage = false;
40 const size_t len = strlen(str);
41 if (len > 0 && str[len - 1] == '%') {
44 value = str_to_uint64(name_percentage, str, 1, 100);
46 // On 32-bit systems, SIZE_MAX would make more sense than
47 // UINT64_MAX. But use UINT64_MAX still so that scripts
48 // that assume > 4 GiB values don't break.
49 value = str_to_uint64(name, str, 0, UINT64_MAX);
52 hardware_memlimit_set(
53 value, set_compress, set_decompress, is_percentage);
59 parse_block_list(char *str)
61 // It must be non-empty and not begin with a comma.
62 if (str[0] == '\0' || str[0] == ',')
63 message_fatal(_("%s: Invalid argument to --block-list"), str);
65 // Count the number of comma-separated strings.
67 for (size_t i = 0; str[i] != '\0'; ++i)
71 // Prevent an unlikely integer overflow.
72 if (count > SIZE_MAX / sizeof(uint64_t) - 1)
73 message_fatal(_("%s: Too many arguments to --block-list"),
76 // Allocate memory to hold all the sizes specified.
77 // If --block-list was specified already, its value is forgotten.
79 opt_block_list = xmalloc((count + 1) * sizeof(uint64_t));
81 for (size_t i = 0; i < count; ++i) {
82 // Locate the next comma and replace it with \0.
83 char *p = strchr(str, ',');
88 // There is no string, that is, a comma follows
89 // another comma. Use the previous value.
91 // NOTE: We checked earler that the first char
92 // of the whole list cannot be a comma.
94 opt_block_list[i] = opt_block_list[i - 1];
96 opt_block_list[i] = str_to_uint64("block-list", str,
99 // Zero indicates no more new Blocks.
100 if (opt_block_list[i] == 0) {
102 message_fatal(_("0 can only be used "
103 "as the last element "
106 opt_block_list[i] = UINT64_MAX;
113 // Terminate the array.
114 opt_block_list[count] = 0;
120 parse_real(args_info *args, int argc, char **argv)
148 static const char short_opts[]
149 = "cC:defF:hHlkM:qQrS:tT:vVz0123456789";
151 static const struct option long_opts[] = {
153 { "compress", no_argument, NULL, 'z' },
154 { "decompress", no_argument, NULL, 'd' },
155 { "uncompress", no_argument, NULL, 'd' },
156 { "test", no_argument, NULL, 't' },
157 { "list", no_argument, NULL, 'l' },
159 // Operation modifiers
160 { "keep", no_argument, NULL, 'k' },
161 { "force", no_argument, NULL, 'f' },
162 { "stdout", no_argument, NULL, 'c' },
163 { "to-stdout", no_argument, NULL, 'c' },
164 { "single-stream", no_argument, NULL, OPT_SINGLE_STREAM },
165 { "no-sparse", no_argument, NULL, OPT_NO_SPARSE },
166 { "suffix", required_argument, NULL, 'S' },
167 // { "recursive", no_argument, NULL, 'r' }, // TODO
168 { "files", optional_argument, NULL, OPT_FILES },
169 { "files0", optional_argument, NULL, OPT_FILES0 },
171 // Basic compression settings
172 { "format", required_argument, NULL, 'F' },
173 { "check", required_argument, NULL, 'C' },
174 { "ignore-check", no_argument, NULL, OPT_IGNORE_CHECK },
175 { "block-size", required_argument, NULL, OPT_BLOCK_SIZE },
176 { "block-list", required_argument, NULL, OPT_BLOCK_LIST },
177 { "memlimit-compress", required_argument, NULL, OPT_MEM_COMPRESS },
178 { "memlimit-decompress", required_argument, NULL, OPT_MEM_DECOMPRESS },
179 { "memlimit", required_argument, NULL, 'M' },
180 { "memory", required_argument, NULL, 'M' }, // Old alias
181 { "no-adjust", no_argument, NULL, OPT_NO_ADJUST },
182 { "threads", required_argument, NULL, 'T' },
183 { "flush-timeout", required_argument, NULL, OPT_FLUSH_TIMEOUT },
185 { "extreme", no_argument, NULL, 'e' },
186 { "fast", no_argument, NULL, '0' },
187 { "best", no_argument, NULL, '9' },
190 { "lzma1", optional_argument, NULL, OPT_LZMA1 },
191 { "lzma2", optional_argument, NULL, OPT_LZMA2 },
192 { "x86", optional_argument, NULL, OPT_X86 },
193 { "powerpc", optional_argument, NULL, OPT_POWERPC },
194 { "ia64", optional_argument, NULL, OPT_IA64 },
195 { "arm", optional_argument, NULL, OPT_ARM },
196 { "armthumb", optional_argument, NULL, OPT_ARMTHUMB },
197 { "sparc", optional_argument, NULL, OPT_SPARC },
198 { "delta", optional_argument, NULL, OPT_DELTA },
201 { "quiet", no_argument, NULL, 'q' },
202 { "verbose", no_argument, NULL, 'v' },
203 { "no-warn", no_argument, NULL, 'Q' },
204 { "robot", no_argument, NULL, OPT_ROBOT },
205 { "info-memory", no_argument, NULL, OPT_INFO_MEMORY },
206 { "help", no_argument, NULL, 'h' },
207 { "long-help", no_argument, NULL, 'H' },
208 { "version", no_argument, NULL, 'V' },
215 while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL))
218 // Compression preset (also for decompression if --format=raw)
219 case '0': case '1': case '2': case '3': case '4':
220 case '5': case '6': case '7': case '8': case '9':
221 coder_set_preset(c - '0');
224 // --memlimit-compress
225 case OPT_MEM_COMPRESS:
226 parse_memlimit("memlimit-compress",
227 "memlimit-compress%", optarg,
231 // --memlimit-decompress
232 case OPT_MEM_DECOMPRESS:
233 parse_memlimit("memlimit-decompress",
234 "memlimit-decompress%", optarg,
240 parse_memlimit("memlimit", "memlimit%", optarg,
250 // The max is from src/liblzma/common/common.h.
251 hardware_threads_set(str_to_uint64("threads",
257 // This doesn't return.
267 opt_mode = MODE_DECOMPRESS;
281 case OPT_INFO_MEMORY:
282 // This doesn't return.
283 hardware_memlimit_show();
287 // This doesn't return.
292 // This doesn't return.
297 opt_mode = MODE_LIST;
302 opt_keep_original = true;
307 message_verbosity_decrease();
315 opt_mode = MODE_TEST;
320 message_verbosity_increase();
327 // This is to make sure that floating point numbers
328 // always have a dot as decimal separator.
329 setlocale(LC_NUMERIC, "C");
333 opt_mode = MODE_COMPRESS;
339 coder_add_filter(LZMA_FILTER_X86,
340 options_bcj(optarg));
344 coder_add_filter(LZMA_FILTER_POWERPC,
345 options_bcj(optarg));
349 coder_add_filter(LZMA_FILTER_IA64,
350 options_bcj(optarg));
354 coder_add_filter(LZMA_FILTER_ARM,
355 options_bcj(optarg));
359 coder_add_filter(LZMA_FILTER_ARMTHUMB,
360 options_bcj(optarg));
364 coder_add_filter(LZMA_FILTER_SPARC,
365 options_bcj(optarg));
369 coder_add_filter(LZMA_FILTER_DELTA,
370 options_delta(optarg));
374 coder_add_filter(LZMA_FILTER_LZMA1,
375 options_lzma(optarg));
379 coder_add_filter(LZMA_FILTER_LZMA2,
380 options_lzma(optarg));
387 // Just in case, support both "lzma" and "alone" since
388 // the latter was used for forward compatibility in
389 // LZMA Utils 4.32.x.
390 static const struct {
392 enum format_type format;
394 { "auto", FORMAT_AUTO },
396 { "lzma", FORMAT_LZMA },
397 { "alone", FORMAT_LZMA },
398 // { "gzip", FORMAT_GZIP },
399 // { "gz", FORMAT_GZIP },
400 { "raw", FORMAT_RAW },
404 while (strcmp(types[i].str, optarg) != 0)
405 if (++i == ARRAY_SIZE(types))
406 message_fatal(_("%s: Unknown file "
410 opt_format = types[i].format;
416 static const struct {
420 { "none", LZMA_CHECK_NONE },
421 { "crc32", LZMA_CHECK_CRC32 },
422 { "crc64", LZMA_CHECK_CRC64 },
423 { "sha256", LZMA_CHECK_SHA256 },
427 while (strcmp(types[i].str, optarg) != 0) {
428 if (++i == ARRAY_SIZE(types))
429 message_fatal(_("%s: Unsupported "
431 "check type"), optarg);
434 // Use a separate check in case we are using different
435 // liblzma than what was used to compile us.
436 if (!lzma_check_is_supported(types[i].check))
437 message_fatal(_("%s: Unsupported integrity "
438 "check type"), optarg);
440 coder_set_check(types[i].check);
444 case OPT_IGNORE_CHECK:
445 opt_ignore_check = true;
449 opt_block_size = str_to_uint64("block-size", optarg,
453 case OPT_BLOCK_LIST: {
454 parse_block_list(optarg);
458 case OPT_SINGLE_STREAM:
459 opt_single_stream = true;
467 args->files_delim = '\n';
472 if (args->files_name != NULL)
473 message_fatal(_("Only one file can be "
474 "specified with `--files' "
477 if (optarg == NULL) {
478 args->files_name = (char *)stdin_filename;
479 args->files_file = stdin;
481 args->files_name = optarg;
482 args->files_file = fopen(optarg,
483 c == OPT_FILES ? "r" : "rb");
484 if (args->files_file == NULL)
485 message_fatal("%s: %s", optarg,
492 opt_auto_adjust = false;
495 case OPT_FLUSH_TIMEOUT:
496 opt_flush_timeout = str_to_uint64("flush-timeout",
497 optarg, 0, UINT64_MAX);
502 tuklib_exit(E_ERROR, E_ERROR, false);
511 parse_environment(args_info *args, char *argv0, const char *varname)
513 char *env = getenv(varname);
517 // We modify the string, so make a copy of it.
520 // Calculate the number of arguments in env. argc stats at one
521 // to include space for the program name.
523 bool prev_was_space = true;
524 for (size_t i = 0; env[i] != '\0'; ++i) {
525 // NOTE: Cast to unsigned char is needed so that correct
526 // value gets passed to isspace(), which expects
527 // unsigned char cast to int. Casting to int is done
528 // automatically due to integer promotion, but we need to
529 // force char to unsigned char manually. Otherwise 8-bit
530 // characters would get promoted to wrong value if
532 if (isspace((unsigned char)env[i])) {
533 prev_was_space = true;
534 } else if (prev_was_space) {
535 prev_was_space = false;
537 // Keep argc small enough to fit into a signed int
538 // and to keep it usable for memory allocation.
539 if (++argc == my_min(
540 INT_MAX, SIZE_MAX / sizeof(char *)))
541 message_fatal(_("The environment variable "
542 "%s contains too many "
543 "arguments"), varname);
547 // Allocate memory to hold pointers to the arguments. Add one to get
548 // space for the terminating NULL (if some systems happen to need it).
549 char **argv = xmalloc(((size_t)(argc) + 1) * sizeof(char *));
553 // Go through the string again. Split the arguments using '\0'
554 // characters and add pointers to the resulting strings to argv.
556 prev_was_space = true;
557 for (size_t i = 0; env[i] != '\0'; ++i) {
558 if (isspace((unsigned char)env[i])) {
559 prev_was_space = true;
561 } else if (prev_was_space) {
562 prev_was_space = false;
563 argv[argc++] = env + i;
567 // Parse the argument list we got from the environment. All non-option
568 // arguments i.e. filenames are ignored.
569 parse_real(args, argc, argv);
571 // Reset the state of the getopt_long() so that we can parse the
572 // command line options too. There are two incompatible ways to
583 // We don't need the argument list from environment anymore.
592 args_parse(args_info *args, int argc, char **argv)
594 // Initialize those parts of *args that we need later.
595 args->files_name = NULL;
596 args->files_file = NULL;
597 args->files_delim = '\0';
599 // Check how we were called.
601 // Remove the leading path name, if any.
602 const char *name = strrchr(argv[0], '/');
608 // NOTE: It's possible that name[0] is now '\0' if argv[0]
609 // is weird, but it doesn't matter here.
611 // Look for full command names instead of substrings like
612 // "un", "cat", and "lz" to reduce possibility of false
613 // positives when the programs have been renamed.
614 if (strstr(name, "xzcat") != NULL) {
615 opt_mode = MODE_DECOMPRESS;
617 } else if (strstr(name, "unxz") != NULL) {
618 opt_mode = MODE_DECOMPRESS;
619 } else if (strstr(name, "lzcat") != NULL) {
620 opt_format = FORMAT_LZMA;
621 opt_mode = MODE_DECOMPRESS;
623 } else if (strstr(name, "unlzma") != NULL) {
624 opt_format = FORMAT_LZMA;
625 opt_mode = MODE_DECOMPRESS;
626 } else if (strstr(name, "lzma") != NULL) {
627 opt_format = FORMAT_LZMA;
631 // First the flags from the environment
632 parse_environment(args, argv[0], "XZ_DEFAULTS");
633 parse_environment(args, argv[0], "XZ_OPT");
635 // Then from the command line
636 parse_real(args, argc, argv);
638 // If encoder or decoder support was omitted at build time,
639 // show an error now so that the rest of the code can rely on
640 // that whatever is in opt_mode is also supported.
641 #ifndef HAVE_ENCODERS
642 if (opt_mode == MODE_COMPRESS)
643 message_fatal(_("Compression support was disabled "
646 #ifndef HAVE_DECODERS
647 // Even MODE_LIST cannot work without decoder support so MODE_COMPRESS
648 // is the only valid choice.
649 if (opt_mode != MODE_COMPRESS)
650 message_fatal(_("Decompression support was disabled "
654 // Never remove the source file when the destination is not on disk.
655 // In test mode the data is written nowhere, but setting opt_stdout
656 // will make the rest of the code behave well.
657 if (opt_stdout || opt_mode == MODE_TEST) {
658 opt_keep_original = true;
662 // When compressing, if no --format flag was used, or it
663 // was --format=auto, we compress to the .xz format.
664 if (opt_mode == MODE_COMPRESS && opt_format == FORMAT_AUTO)
665 opt_format = FORMAT_XZ;
667 // Compression settings need to be validated (options themselves and
668 // their memory usage) when compressing to any file format. It has to
669 // be done also when uncompressing raw data, since for raw decoding
670 // the options given on the command line are used to know what kind
671 // of raw data we are supposed to decode.
672 if (opt_mode == MODE_COMPRESS || opt_format == FORMAT_RAW)
673 coder_set_compression_settings();
675 // If no filenames are given, use stdin.
676 if (argv[optind] == NULL && args->files_name == NULL) {
677 // We don't modify or free() the "-" constant. The caller
678 // modifies this so don't make the struct itself const.
679 static char *names_stdin[2] = { (char *)"-", NULL };
680 args->arg_names = names_stdin;
683 // We got at least one filename from the command line, or
684 // --files or --files0 was specified.
685 args->arg_names = argv + optind;
686 args->arg_count = argc - optind;
697 free(opt_block_list);