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 { "powerpc", optional_argument, NULL, OPT_POWERPC },
85 { "ia64", optional_argument, NULL, OPT_IA64 },
86 { "arm", optional_argument, NULL, OPT_ARM },
87 { "armthumb", optional_argument, NULL, OPT_ARMTHUMB },
88 { "sparc", optional_argument, NULL, OPT_SPARC },
89 { "delta", optional_argument, NULL, OPT_DELTA },
90 { "subblock", optional_argument, NULL, OPT_SUBBLOCK },
93 { "quiet", no_argument, NULL, 'q' },
94 { "verbose", no_argument, NULL, 'v' },
95 { "no-warn", no_argument, NULL, 'Q' },
96 { "help", no_argument, NULL, 'h' },
97 { "long-help", no_argument, NULL, 'H' },
98 { "version", no_argument, NULL, 'V' },
105 while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL))
108 // Compression preset (also for decompression if --format=raw)
109 case '0': case '1': case '2': case '3': case '4':
110 case '5': case '6': case '7': case '8': case '9':
111 coder_set_preset(c - '0');
116 // Support specifying the limit as a percentage of
117 // installed physical RAM.
118 size_t len = strlen(optarg);
119 if (len > 0 && optarg[len - 1] == '%') {
120 optarg[len - 1] = '\0';
121 hardware_memlimit_set_percentage(
123 "memory%", optarg, 1, 100));
125 // On 32-bit systems, SIZE_MAX would make more
126 // sense than UINT64_MAX. But use UINT64_MAX
127 // still so that scripts that assume > 4 GiB
128 // values don't break.
129 hardware_memlimit_set(str_to_uint64(
143 hardware_threadlimit_set(str_to_uint64(
144 "threads", optarg, 0, UINT32_MAX));
149 // This doesn't return.
159 opt_mode = MODE_DECOMPRESS;
174 // This doesn't return.
179 // This doesn't return.
184 opt_mode = MODE_LIST;
189 opt_keep_original = true;
194 message_verbosity_decrease();
202 opt_mode = MODE_TEST;
207 message_verbosity_increase();
211 opt_mode = MODE_COMPRESS;
217 coder_add_filter(LZMA_FILTER_SUBBLOCK,
218 options_subblock(optarg));
222 coder_add_filter(LZMA_FILTER_X86,
223 options_bcj(optarg));
227 coder_add_filter(LZMA_FILTER_POWERPC,
228 options_bcj(optarg));
232 coder_add_filter(LZMA_FILTER_IA64,
233 options_bcj(optarg));
237 coder_add_filter(LZMA_FILTER_ARM,
238 options_bcj(optarg));
242 coder_add_filter(LZMA_FILTER_ARMTHUMB,
243 options_bcj(optarg));
247 coder_add_filter(LZMA_FILTER_SPARC,
248 options_bcj(optarg));
252 coder_add_filter(LZMA_FILTER_DELTA,
253 options_delta(optarg));
257 coder_add_filter(LZMA_FILTER_LZMA1,
258 options_lzma(optarg));
262 coder_add_filter(LZMA_FILTER_LZMA2,
263 options_lzma(optarg));
270 // Just in case, support both "lzma" and "alone" since
271 // the latter was used for forward compatibility in
272 // LZMA Utils 4.32.x.
273 static const struct {
275 enum format_type format;
277 { "auto", FORMAT_AUTO },
279 { "lzma", FORMAT_LZMA },
280 { "alone", FORMAT_LZMA },
281 // { "gzip", FORMAT_GZIP },
282 // { "gz", FORMAT_GZIP },
283 { "raw", FORMAT_RAW },
287 while (strcmp(types[i].str, optarg) != 0)
288 if (++i == ARRAY_SIZE(types))
289 message_fatal(_("%s: Unknown file "
293 opt_format = types[i].format;
299 static const struct {
303 { "none", LZMA_CHECK_NONE },
304 { "crc32", LZMA_CHECK_CRC32 },
305 { "crc64", LZMA_CHECK_CRC64 },
306 { "sha256", LZMA_CHECK_SHA256 },
310 while (strcmp(types[i].str, optarg) != 0) {
311 if (++i == ARRAY_SIZE(types))
312 message_fatal(_("%s: Unsupported "
314 "check type"), optarg);
317 // Use a separate check in case we are using different
318 // liblzma than what was used to compile us.
319 if (!lzma_check_is_supported(types[i].check))
320 message_fatal(_("%s: Unsupported integrity "
321 "check type"), optarg);
323 coder_set_check(types[i].check);
328 args->files_delim = '\n';
333 if (args->files_name != NULL)
334 message_fatal(_("Only one file can be "
335 "specified with `--files'"
338 if (optarg == NULL) {
339 args->files_name = (char *)stdin_filename;
340 args->files_file = stdin;
342 args->files_name = optarg;
343 args->files_file = fopen(optarg,
344 c == OPT_FILES ? "r" : "rb");
345 if (args->files_file == NULL)
346 message_fatal("%s: %s", optarg,
354 tuklib_exit(E_ERROR, E_ERROR, false);
363 parse_environment(args_info *args, char *argv0)
365 char *env = getenv("XZ_OPT");
369 // We modify the string, so make a copy of it.
372 // Calculate the number of arguments in env. argc stats at one
373 // to include space for the program name.
375 bool prev_was_space = true;
376 for (size_t i = 0; env[i] != '\0'; ++i) {
377 // NOTE: Cast to unsigned char is needed so that correct
378 // value gets passed to isspace(), which expects
379 // unsigned char cast to int. Casting to int is done
380 // automatically due to integer promotion, but we need to
381 // force char to unsigned char manually. Otherwise 8-bit
382 // characters would get promoted to wrong value if
384 if (isspace((unsigned char)env[i])) {
385 prev_was_space = true;
386 } else if (prev_was_space) {
387 prev_was_space = false;
389 // Keep argc small enough to fit into a singed int
390 // and to keep it usable for memory allocation.
391 if (++argc == MIN(INT_MAX, SIZE_MAX / sizeof(char *)))
392 message_fatal(_("The environment variable "
393 "XZ_OPT contains too many "
398 // Allocate memory to hold pointers to the arguments. Add one to get
399 // space for the terminating NULL (if some systems happen to need it).
400 char **argv = xmalloc(((size_t)(argc) + 1) * sizeof(char *));
404 // Go through the string again. Split the arguments using '\0'
405 // characters and add pointers to the resulting strings to argv.
407 prev_was_space = true;
408 for (size_t i = 0; env[i] != '\0'; ++i) {
409 if (isspace((unsigned char)env[i])) {
410 prev_was_space = true;
412 } else if (prev_was_space) {
413 prev_was_space = false;
414 argv[argc++] = env + i;
418 // Parse the argument list we got from the environment. All non-option
419 // arguments i.e. filenames are ignored.
420 parse_real(args, argc, argv);
422 // Reset the state of the getopt_long() so that we can parse the
423 // command line options too. There are two incompatible ways to
434 // We don't need the argument list from environment anymore.
443 args_parse(args_info *args, int argc, char **argv)
445 // Initialize those parts of *args that we need later.
446 args->files_name = NULL;
447 args->files_file = NULL;
448 args->files_delim = '\0';
450 // Check how we were called.
452 // Remove the leading path name, if any.
453 const char *name = strrchr(argv[0], '/');
459 // NOTE: It's possible that name[0] is now '\0' if argv[0]
460 // is weird, but it doesn't matter here.
462 // Look for full command names instead of substrings like
463 // "un", "cat", and "lz" to reduce possibility of false
464 // positives when the programs have been renamed.
465 if (strstr(name, "xzcat") != NULL) {
466 opt_mode = MODE_DECOMPRESS;
468 } else if (strstr(name, "unxz") != NULL) {
469 opt_mode = MODE_DECOMPRESS;
470 } else if (strstr(name, "lzcat") != NULL) {
471 opt_format = FORMAT_LZMA;
472 opt_mode = MODE_DECOMPRESS;
474 } else if (strstr(name, "unlzma") != NULL) {
475 opt_format = FORMAT_LZMA;
476 opt_mode = MODE_DECOMPRESS;
477 } else if (strstr(name, "lzma") != NULL) {
478 opt_format = FORMAT_LZMA;
482 // First the flags from environment
483 parse_environment(args, argv[0]);
485 // Then from the command line
486 parse_real(args, argc, argv);
488 // Never remove the source file when the destination is not on disk.
489 // In test mode the data is written nowhere, but setting opt_stdout
490 // will make the rest of the code behave well.
491 if (opt_stdout || opt_mode == MODE_TEST) {
492 opt_keep_original = true;
496 // When compressing, if no --format flag was used, or it
497 // was --format=auto, we compress to the .xz format.
498 if (opt_mode == MODE_COMPRESS && opt_format == FORMAT_AUTO)
499 opt_format = FORMAT_XZ;
501 // Compression settings need to be validated (options themselves and
502 // their memory usage) when compressing to any file format. It has to
503 // be done also when uncompressing raw data, since for raw decoding
504 // the options given on the command line are used to know what kind
505 // of raw data we are supposed to decode.
506 if (opt_mode == MODE_COMPRESS || opt_format == FORMAT_RAW)
507 coder_set_compression_settings();
509 // If no filenames are given, use stdin.
510 if (argv[optind] == NULL && args->files_name == NULL) {
511 // We don't modify or free() the "-" constant. The caller
512 // modifies this so don't make the struct itself const.
513 static char *names_stdin[2] = { (char *)"-", NULL };
514 args->arg_names = names_stdin;
517 // We got at least one filename from the command line, or
518 // --files or --files0 was specified.
519 args->arg_names = argv + optind;
520 args->arg_count = argc - optind;