1 ///////////////////////////////////////////////////////////////////////////////
4 /// \brief Argument parsing
6 /// \note Filter-specific options parsing is in options.c.
8 // Copyright (C) 2007 Lasse Collin
10 // This program is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Lesser General Public
12 // License as published by the Free Software Foundation; either
13 // version 2.1 of the License, or (at your option) any later version.
15 // This program is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
20 ///////////////////////////////////////////////////////////////////////////////
28 enum tool_mode opt_mode = MODE_COMPRESS;
29 enum header_type opt_header = HEADER_AUTO;
31 char *opt_suffix = NULL;
33 char *opt_files_name = NULL;
34 char opt_files_split = '\0';
35 FILE *opt_files_file = NULL;
37 bool opt_stdout = false;
38 bool opt_force = false;
39 bool opt_keep_original = false;
40 bool opt_preserve_name = false;
42 lzma_check_type opt_check = LZMA_CHECK_CRC64;
43 lzma_options_filter opt_filters[8];
45 // We don't modify or free() this, but we need to assign it in some
46 // non-const pointers.
47 const char *stdin_filename = "(stdin)";
49 static size_t preset_number = 7 - 1;
50 static bool preset_default = true;
51 static size_t filter_count = 0;
55 OPT_SUBBLOCK = INT_MIN,
70 static const char short_opts[] = "cC:dfF:hlLkM:qrS:tT:vVz123456789";
73 static const struct option long_opts[] = {
75 { "fast", no_argument, NULL, '1' },
76 { "best", no_argument, NULL, '9' },
77 { "memory", required_argument, NULL, 'M' },
78 { "name", no_argument, NULL, 'N' },
79 { "suffix", required_argument, NULL, 'S' },
80 { "threads", required_argument, NULL, 'T' },
81 { "version", no_argument, NULL, 'V' },
82 { "stdout", no_argument, NULL, 'c' },
83 { "to-stdout", no_argument, NULL, 'c' },
84 { "decompress", no_argument, NULL, 'd' },
85 { "uncompress", no_argument, NULL, 'd' },
86 { "force", no_argument, NULL, 'f' },
87 { "help", no_argument, NULL, 'h' },
88 { "list", no_argument, NULL, 'l' },
89 { "info", no_argument, NULL, 'l' },
90 { "keep", no_argument, NULL, 'k' },
91 { "no-name", no_argument, NULL, 'n' },
92 { "quiet", no_argument, NULL, 'q' },
93 // { "recursive", no_argument, NULL, 'r' }, // TODO
94 { "test", no_argument, NULL, 't' },
95 { "verbose", no_argument, NULL, 'v' },
96 { "compress", no_argument, NULL, 'z' },
99 { "subblock", optional_argument, NULL, OPT_SUBBLOCK },
100 { "x86", no_argument, NULL, OPT_X86 },
101 { "bcj", no_argument, NULL, OPT_X86 },
102 { "powerpc", no_argument, NULL, OPT_POWERPC },
103 { "ppc", no_argument, NULL, OPT_POWERPC },
104 { "ia64", no_argument, NULL, OPT_IA64 },
105 { "itanium", no_argument, NULL, OPT_IA64 },
106 { "arm", no_argument, NULL, OPT_ARM },
107 { "armthumb", no_argument, NULL, OPT_ARMTHUMB },
108 { "sparc", no_argument, NULL, OPT_SPARC },
109 { "delta", optional_argument, NULL, OPT_DELTA },
110 { "lzma", optional_argument, NULL, OPT_LZMA },
113 { "format", required_argument, NULL, 'F' },
114 { "check", required_argument, NULL, 'C' },
115 { "files", optional_argument, NULL, OPT_FILES },
116 { "files0", optional_argument, NULL, OPT_FILES0 },
123 add_filter(lzma_vli id, const char *opt_str)
125 if (filter_count == 7) {
126 errmsg(V_ERROR, _("Maximum number of filters is seven"));
130 opt_filters[filter_count].id = id;
133 case LZMA_FILTER_SUBBLOCK:
134 opt_filters[filter_count].options
135 = parse_options_subblock(opt_str);
138 case LZMA_FILTER_DELTA:
139 opt_filters[filter_count].options
140 = parse_options_delta(opt_str);
143 case LZMA_FILTER_LZMA:
144 opt_filters[filter_count].options
145 = parse_options_lzma(opt_str);
149 assert(opt_str == NULL);
150 opt_filters[filter_count].options = NULL;
155 preset_default = false;
161 parse_real(int argc, char **argv)
165 while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL))
170 case '1': case '2': case '3': case '4':
171 case '5': case '6': case '7': case '8': case '9':
172 preset_number = c - '1';
173 preset_default = false;
178 opt_memory = str_to_uint64("memory", optarg,
183 opt_preserve_name = true;
188 // Empty suffix and suffixes having a slash are
189 // rejected. Such suffixes would break things later.
190 if (optarg[0] == '\0' || strchr(optarg, '/') != NULL) {
191 errmsg(V_ERROR, _("%s: Invalid filename "
197 opt_suffix = xstrdup(optarg);
201 opt_threads = str_to_uint64("threads", optarg,
207 // This doesn't return.
217 opt_mode = MODE_DECOMPRESS;
227 // This doesn't return.
232 opt_mode = MODE_LIST;
237 opt_keep_original = true;
241 opt_preserve_name = false;
246 if (verbosity > V_SILENT)
252 opt_mode = MODE_TEST;
257 if (verbosity < V_DEBUG)
263 opt_mode = MODE_COMPRESS;
269 add_filter(LZMA_FILTER_SUBBLOCK, optarg);
273 add_filter(LZMA_FILTER_X86, NULL);
277 add_filter(LZMA_FILTER_POWERPC, NULL);
281 add_filter(LZMA_FILTER_IA64, NULL);
285 add_filter(LZMA_FILTER_ARM, NULL);
289 add_filter(LZMA_FILTER_ARMTHUMB, NULL);
293 add_filter(LZMA_FILTER_SPARC, NULL);
297 add_filter(LZMA_FILTER_DELTA, optarg);
301 add_filter(LZMA_FILTER_LZMA, optarg);
308 static const char *types[] = {
317 while (strcmp(types[opt_header], optarg) != 0) {
318 if (types[++opt_header] == NULL) {
319 errmsg(V_ERROR, _("%s: Unknown file "
331 static const struct {
335 { "none", LZMA_CHECK_NONE },
336 { "crc32", LZMA_CHECK_CRC32 },
337 { "crc64", LZMA_CHECK_CRC64 },
338 { "sha256", LZMA_CHECK_SHA256 },
343 while (strcmp(types[i].str, optarg) != 0) {
344 if (types[++i].str == NULL) {
345 errmsg(V_ERROR, _("%s: Unknown "
352 opt_check = types[i].value;
357 opt_files_split = '\n';
362 if (opt_files_name != NULL) {
363 errmsg(V_ERROR, _("Only one file can be "
364 "specified with `--files'"
369 if (optarg == NULL) {
370 opt_files_name = (char *)stdin_filename;
371 opt_files_file = stdin;
373 opt_files_name = optarg;
374 opt_files_file = fopen(optarg,
375 c == OPT_FILES ? "r" : "rb");
376 if (opt_files_file == NULL) {
377 errmsg(V_ERROR, "%s: %s", optarg,
396 parse_environment(void)
398 char *env = getenv("LZMA_OPT");
404 // Calculate the number of arguments in env.
405 unsigned int argc = 1;
406 bool prev_was_space = true;
407 for (size_t i = 0; env[i] != '\0'; ++i) {
408 if (isspace(env[i])) {
409 prev_was_space = true;
410 } else if (prev_was_space) {
411 prev_was_space = false;
412 if (++argc > (unsigned int)(INT_MAX)) {
413 errmsg(V_ERROR, _("The environment variable "
414 "LZMA_OPT contains too many "
421 char **argv = xmalloc((argc + 1) * sizeof(char*));
426 prev_was_space = true;
427 for (size_t i = 0; env[i] != '\0'; ++i) {
428 if (isspace(env[i])) {
429 prev_was_space = true;
430 } else if (prev_was_space) {
431 prev_was_space = false;
432 argv[argc++] = env + i;
436 parse_real((int)(argc), argv);
445 set_compression_settings(void)
447 if (filter_count == 0) {
448 opt_filters[0].id = LZMA_FILTER_LZMA;
449 opt_filters[0].options = (lzma_options_lzma *)(
450 lzma_preset_lzma + preset_number);
454 // Terminate the filter options array.
455 opt_filters[filter_count].id = LZMA_VLI_VALUE_UNKNOWN;
457 // If we are using the LZMA_Alone format, allow exactly one filter
458 // which has to be LZMA.
459 if (opt_header == HEADER_ALONE && (filter_count != 1
460 || opt_filters[0].id != LZMA_FILTER_LZMA)) {
461 errmsg(V_ERROR, _("With --format=alone only the LZMA filter "
466 const uint32_t memory_limit = opt_memory / (1024 * 1024) + 1;
467 uint32_t memory_usage = lzma_memory_usage(opt_filters, true);
469 // Don't go over the memory limits when the default
471 if (preset_default) {
472 while (memory_usage > memory_limit) {
473 if (preset_number == 0) {
474 errmsg(V_ERROR, _("Memory usage limit is too "
475 "small for any internal "
481 opt_filters[0].options = (lzma_options_lzma *)(
484 memory_usage = lzma_memory_usage(opt_filters,
488 if (memory_usage > memory_limit) {
489 errmsg(V_ERROR, _("Memory usage limit is too small "
490 "for the given filter setup"));
495 // Limit the number of worked threads so that memory usage
496 // limit isn't exceeded.
497 // FIXME: Probably should use bytes instead of mebibytes for
498 // memory_usage and memory_limit.
499 if (memory_usage == 0)
502 size_t thread_limit = memory_limit / memory_usage;
503 if (thread_limit == 0)
506 if (opt_threads > thread_limit)
507 opt_threads = thread_limit;
514 parse_args(int argc, char **argv)
516 // Check how we were called.
518 const char *name = str_filename(argv[0]);
520 if (strstr(name, "cat") != NULL) {
521 opt_mode = MODE_DECOMPRESS;
523 } else if (strstr(name, "un") != NULL) {
524 opt_mode = MODE_DECOMPRESS;
529 // First the flags from environment
532 // Then from the command line
534 parse_real(argc, argv);
536 // Never remove the source file when the destination is not on disk.
537 // In test mode the data is written nowhere, but setting opt_stdout
538 // will make the rest of the code behave well.
539 if (opt_stdout || opt_mode == MODE_TEST) {
540 opt_keep_original = true;
544 if (opt_mode == MODE_COMPRESS)
545 set_compression_settings();
547 // If no filenames are given, use stdin.
548 if (argv[optind] == NULL && opt_files_name == NULL) {
549 // We don't modify or free() the "-" constant.
550 static char *argv_stdin[2] = { (char *)"-", NULL };
554 return argv + optind;