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;
25 // We don't modify or free() this, but we need to assign it in some
26 // non-const pointers.
27 const char *stdin_filename = "(stdin)";
31 parse_real(args_info *args, int argc, char **argv)
34 OPT_SUBBLOCK = INT_MIN,
49 static const char short_opts[]
50 = "cC:defF:hHlkM:qQrS:tT:vVz0123456789";
52 static const struct option long_opts[] = {
54 { "compress", no_argument, NULL, 'z' },
55 { "decompress", no_argument, NULL, 'd' },
56 { "uncompress", no_argument, NULL, 'd' },
57 { "test", no_argument, NULL, 't' },
58 { "list", no_argument, NULL, 'l' },
60 // Operation modifiers
61 { "keep", no_argument, NULL, 'k' },
62 { "force", no_argument, NULL, 'f' },
63 { "stdout", no_argument, NULL, 'c' },
64 { "to-stdout", no_argument, NULL, 'c' },
65 { "suffix", required_argument, NULL, 'S' },
66 // { "recursive", no_argument, NULL, 'r' }, // TODO
67 { "files", optional_argument, NULL, OPT_FILES },
68 { "files0", optional_argument, NULL, OPT_FILES0 },
70 // Basic compression settings
71 { "format", required_argument, NULL, 'F' },
72 { "check", required_argument, NULL, 'C' },
73 { "memory", required_argument, NULL, 'M' },
74 { "threads", required_argument, NULL, 'T' },
76 { "extreme", no_argument, NULL, 'e' },
77 { "fast", no_argument, NULL, '0' },
78 { "best", no_argument, NULL, '9' },
81 { "lzma1", optional_argument, NULL, OPT_LZMA1 },
82 { "lzma2", optional_argument, NULL, OPT_LZMA2 },
83 { "x86", optional_argument, NULL, OPT_X86 },
84 { "bcj", optional_argument, NULL, OPT_X86 },
85 { "powerpc", optional_argument, NULL, OPT_POWERPC },
86 { "ppc", optional_argument, NULL, OPT_POWERPC },
87 { "ia64", optional_argument, NULL, OPT_IA64 },
88 { "itanium", optional_argument, NULL, OPT_IA64 },
89 { "arm", optional_argument, NULL, OPT_ARM },
90 { "armthumb", optional_argument, NULL, OPT_ARMTHUMB },
91 { "sparc", optional_argument, NULL, OPT_SPARC },
92 { "delta", optional_argument, NULL, OPT_DELTA },
93 { "subblock", optional_argument, NULL, OPT_SUBBLOCK },
96 { "quiet", no_argument, NULL, 'q' },
97 { "verbose", no_argument, NULL, 'v' },
98 { "no-warn", no_argument, NULL, 'Q' },
99 { "help", no_argument, NULL, 'h' },
100 { "long-help", no_argument, NULL, 'H' },
101 { "version", no_argument, NULL, 'V' },
108 while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL))
111 // Compression preset (also for decompression if --format=raw)
112 case '0': case '1': case '2': case '3': case '4':
113 case '5': case '6': case '7': case '8': case '9':
114 coder_set_preset(c - '0');
119 // Support specifying the limit as a percentage of
120 // installed physical RAM.
121 size_t len = strlen(optarg);
122 if (len > 0 && optarg[len - 1] == '%') {
123 optarg[len - 1] = '\0';
124 hardware_memlimit_set_percentage(
126 "memory%", optarg, 1, 100));
128 // On 32-bit systems, SIZE_MAX would make more
129 // sense than UINT64_MAX. But use UINT64_MAX
130 // still so that scripts that assume > 4 GiB
131 // values don't break.
132 hardware_memlimit_set(str_to_uint64(
146 hardware_threadlimit_set(str_to_uint64(
147 "threads", optarg, 0, UINT32_MAX));
152 // This doesn't return.
162 opt_mode = MODE_DECOMPRESS;
177 // This doesn't return.
182 // This doesn't return.
187 opt_mode = MODE_LIST;
192 opt_keep_original = true;
197 message_verbosity_decrease();
205 opt_mode = MODE_TEST;
210 message_verbosity_increase();
214 opt_mode = MODE_COMPRESS;
220 coder_add_filter(LZMA_FILTER_SUBBLOCK,
221 options_subblock(optarg));
225 coder_add_filter(LZMA_FILTER_X86,
226 options_bcj(optarg));
230 coder_add_filter(LZMA_FILTER_POWERPC,
231 options_bcj(optarg));
235 coder_add_filter(LZMA_FILTER_IA64,
236 options_bcj(optarg));
240 coder_add_filter(LZMA_FILTER_ARM,
241 options_bcj(optarg));
245 coder_add_filter(LZMA_FILTER_ARMTHUMB,
246 options_bcj(optarg));
250 coder_add_filter(LZMA_FILTER_SPARC,
251 options_bcj(optarg));
255 coder_add_filter(LZMA_FILTER_DELTA,
256 options_delta(optarg));
260 coder_add_filter(LZMA_FILTER_LZMA1,
261 options_lzma(optarg));
265 coder_add_filter(LZMA_FILTER_LZMA2,
266 options_lzma(optarg));
273 // Just in case, support both "lzma" and "alone" since
274 // the latter was used for forward compatibility in
275 // LZMA Utils 4.32.x.
276 static const struct {
278 enum format_type format;
280 { "auto", FORMAT_AUTO },
282 { "lzma", FORMAT_LZMA },
283 { "alone", FORMAT_LZMA },
284 // { "gzip", FORMAT_GZIP },
285 // { "gz", FORMAT_GZIP },
286 { "raw", FORMAT_RAW },
290 while (strcmp(types[i].str, optarg) != 0)
291 if (++i == ARRAY_SIZE(types))
292 message_fatal(_("%s: Unknown file "
296 opt_format = types[i].format;
302 static const struct {
306 { "none", LZMA_CHECK_NONE },
307 { "crc32", LZMA_CHECK_CRC32 },
308 { "crc64", LZMA_CHECK_CRC64 },
309 { "sha256", LZMA_CHECK_SHA256 },
313 while (strcmp(types[i].str, optarg) != 0) {
314 if (++i == ARRAY_SIZE(types))
315 message_fatal(_("%s: Unsupported "
317 "check type"), optarg);
320 // Use a separate check in case we are using different
321 // liblzma than what was used to compile us.
322 if (!lzma_check_is_supported(types[i].check))
323 message_fatal(_("%s: Unsupported integrity "
324 "check type"), optarg);
326 coder_set_check(types[i].check);
331 args->files_delim = '\n';
336 if (args->files_name != NULL)
337 message_fatal(_("Only one file can be "
338 "specified with `--files'"
341 if (optarg == NULL) {
342 args->files_name = (char *)stdin_filename;
343 args->files_file = stdin;
345 args->files_name = optarg;
346 args->files_file = fopen(optarg,
347 c == OPT_FILES ? "r" : "rb");
348 if (args->files_file == NULL)
349 message_fatal("%s: %s", optarg,
366 parse_environment(args_info *args, char *argv0)
368 char *env = getenv("XZ_OPT");
372 // We modify the string, so make a copy of it.
375 // Calculate the number of arguments in env. argc stats at one
376 // to include space for the program name.
378 bool prev_was_space = true;
379 for (size_t i = 0; env[i] != '\0'; ++i) {
380 if (isspace(env[i])) {
381 prev_was_space = true;
382 } else if (prev_was_space) {
383 prev_was_space = false;
385 // Keep argc small enough to fit into a singed int
386 // and to keep it usable for memory allocation.
387 if (++argc == MIN(INT_MAX, SIZE_MAX / sizeof(char *)))
388 message_fatal(_("The environment variable "
389 "XZ_OPT contains too many "
394 // Allocate memory to hold pointers to the arguments. Add one to get
395 // space for the terminating NULL (if some systems happen to need it).
396 char **argv = xmalloc(((size_t)(argc) + 1) * sizeof(char *));
400 // Go through the string again. Split the arguments using '\0'
401 // characters and add pointers to the resulting strings to argv.
403 prev_was_space = true;
404 for (size_t i = 0; env[i] != '\0'; ++i) {
405 if (isspace(env[i])) {
406 prev_was_space = true;
408 } else if (prev_was_space) {
409 prev_was_space = false;
410 argv[argc++] = env + i;
414 // Parse the argument list we got from the environment. All non-option
415 // arguments i.e. filenames are ignored.
416 parse_real(args, argc, argv);
418 // Reset the state of the getopt_long() so that we can parse the
419 // command line options too. There are two incompatible ways to
430 // We don't need the argument list from environment anymore.
439 args_parse(args_info *args, int argc, char **argv)
441 // Initialize those parts of *args that we need later.
442 args->files_name = NULL;
443 args->files_file = NULL;
444 args->files_delim = '\0';
446 // Check how we were called.
449 // We adjusted argv[0] in the beginning of main() so we don't
450 // need to do anything here.
451 const char *name = argv[0];
453 // Remove the leading path name, if any.
454 const char *name = strrchr(argv[0], '/');
461 // NOTE: It's possible that name[0] is now '\0' if argv[0]
462 // is weird, but it doesn't matter here.
464 // If the command name contains "lz",
465 // it implies --format=lzma.
466 if (strstr(name, "lz") != NULL)
467 opt_format = FORMAT_LZMA;
470 if (strstr(name, "cat") != NULL) {
471 // Imply --decompress --stdout
472 opt_mode = MODE_DECOMPRESS;
474 } else if (strstr(name, "un") != NULL) {
475 // Imply --decompress
476 opt_mode = MODE_DECOMPRESS;
480 // First the flags from environment
481 parse_environment(args, argv[0]);
483 // Then from the command line
485 parse_real(args, argc, argv);
487 // Never remove the source file when the destination is not on disk.
488 // In test mode the data is written nowhere, but setting opt_stdout
489 // will make the rest of the code behave well.
490 if (opt_stdout || opt_mode == MODE_TEST) {
491 opt_keep_original = true;
495 // When compressing, if no --format flag was used, or it
496 // was --format=auto, we compress to the .xz format.
497 if (opt_mode == MODE_COMPRESS && opt_format == FORMAT_AUTO)
498 opt_format = FORMAT_XZ;
500 // Compression settings need to be validated (options themselves and
501 // their memory usage) when compressing to any file format. It has to
502 // be done also when uncompressing raw data, since for raw decoding
503 // the options given on the command line are used to know what kind
504 // of raw data we are supposed to decode.
505 if (opt_mode == MODE_COMPRESS || opt_format == FORMAT_RAW)
506 coder_set_compression_settings();
508 // If no filenames are given, use stdin.
509 if (argv[optind] == NULL && args->files_name == NULL) {
510 // We don't modify or free() the "-" constant. The caller
511 // modifies this so don't make the struct itself const.
512 static char *names_stdin[2] = { (char *)"-", NULL };
513 args->arg_names = names_stdin;
516 // We got at least one filename from the command line, or
517 // --files or --files0 was specified.
518 args->arg_names = argv + optind;
519 args->arg_count = argc - optind;