]> icculus.org git repositories - icculus/xz.git/blob - src/lzma/args.c
Oh well, big messy commit again. Some highlights:
[icculus/xz.git] / src / lzma / args.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       args.c
4 /// \brief      Argument parsing
5 ///
6 /// \note       Filter-specific options parsing is in options.c.
7 //
8 //  Copyright (C) 2007 Lasse Collin
9 //
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.
14 //
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.
19 //
20 ///////////////////////////////////////////////////////////////////////////////
21
22 #include "private.h"
23
24 #include "getopt.h"
25 #include <ctype.h>
26
27
28 bool opt_stdout = false;
29 bool opt_force = false;
30 bool opt_keep_original = false;
31
32 // We don't modify or free() this, but we need to assign it in some
33 // non-const pointers.
34 const char *stdin_filename = "(stdin)";
35
36
37 static void
38 parse_real(args_info *args, int argc, char **argv)
39 {
40         enum {
41                 OPT_SUBBLOCK = INT_MIN,
42                 OPT_X86,
43                 OPT_POWERPC,
44                 OPT_IA64,
45                 OPT_ARM,
46                 OPT_ARMTHUMB,
47                 OPT_SPARC,
48                 OPT_DELTA,
49                 OPT_LZMA1,
50                 OPT_LZMA2,
51
52                 OPT_FILES,
53                 OPT_FILES0,
54         };
55
56         static const char short_opts[] = "cC:dfF:hHlLkM:p:qrS:tT:vVz123456789";
57
58         static const struct option long_opts[] = {
59                 // Operation mode
60                 { "compress",       no_argument,       NULL,  'z' },
61                 { "decompress",     no_argument,       NULL,  'd' },
62                 { "uncompress",     no_argument,       NULL,  'd' },
63                 { "test",           no_argument,       NULL,  't' },
64                 { "list",           no_argument,       NULL,  'l' },
65                 { "info",           no_argument,       NULL,  'l' },
66
67                 // Operation modifiers
68                 { "keep",           no_argument,       NULL,  'k' },
69                 { "force",          no_argument,       NULL,  'f' },
70                 { "stdout",         no_argument,       NULL,  'c' },
71                 { "to-stdout",      no_argument,       NULL,  'c' },
72                 { "suffix",         required_argument, NULL,  'S' },
73                 // { "recursive",      no_argument,       NULL,  'r' }, // TODO
74                 { "files",          optional_argument, NULL,  OPT_FILES },
75                 { "files0",         optional_argument, NULL,  OPT_FILES0 },
76
77                 // Basic compression settings
78                 { "format",         required_argument, NULL,  'F' },
79                 { "check",          required_argument, NULL,  'C' },
80                 { "preset",         required_argument, NULL,  'p' },
81                 { "memory",         required_argument, NULL,  'M' },
82                 { "threads",        required_argument, NULL,  'T' },
83
84                 { "fast",           no_argument,       NULL,  '1' },
85                 { "best",           no_argument,       NULL,  '9' },
86
87                 // Filters
88                 { "lzma1",          optional_argument, NULL,  OPT_LZMA1 },
89                 { "lzma2",          optional_argument, NULL,  OPT_LZMA2 },
90                 { "x86",            no_argument,       NULL,  OPT_X86 },
91                 { "bcj",            no_argument,       NULL,  OPT_X86 },
92                 { "powerpc",        no_argument,       NULL,  OPT_POWERPC },
93                 { "ppc",            no_argument,       NULL,  OPT_POWERPC },
94                 { "ia64",           no_argument,       NULL,  OPT_IA64 },
95                 { "itanium",        no_argument,       NULL,  OPT_IA64 },
96                 { "arm",            no_argument,       NULL,  OPT_ARM },
97                 { "armthumb",       no_argument,       NULL,  OPT_ARMTHUMB },
98                 { "sparc",          no_argument,       NULL,  OPT_SPARC },
99                 { "delta",          optional_argument, NULL,  OPT_DELTA },
100                 { "subblock",       optional_argument, NULL,  OPT_SUBBLOCK },
101
102                 // Other options
103                 { "quiet",          no_argument,       NULL,  'q' },
104                 { "verbose",        no_argument,       NULL,  'v' },
105                 { "help",           no_argument,       NULL,  'h' },
106                 { "long-help",      no_argument,       NULL,  'H' },
107                 { "version",        no_argument,       NULL,  'V' },
108
109                 { NULL,                 0,                 NULL,   0 }
110         };
111
112         int c;
113
114         while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL))
115                         != -1) {
116                 switch (c) {
117                 // gzip-like options
118
119                 case '1': case '2': case '3': case '4':
120                 case '5': case '6': case '7': case '8': case '9':
121                         coder_set_preset(c - '0');
122                         break;
123
124                 case 'p': {
125                         const uint64_t preset = str_to_uint64(
126                                         "preset", optarg, 1, 9);
127                         coder_set_preset(preset);
128                         break;
129                 }
130
131                 // --memory
132                 case 'M':
133                         // On 32-bit systems, SIZE_MAX would make more sense
134                         // than UINT64_MAX. But use UINT64_MAX still so that
135                         // scripts that assume > 4 GiB values don't break.
136                         hardware_memlimit_set(str_to_uint64(
137                                         "memory", optarg, 0, UINT64_MAX));
138                         break;
139
140                 // --suffix
141                 case 'S':
142                         suffix_set(optarg);
143                         break;
144
145                 case 'T':
146                         opt_threads = str_to_uint64("threads", optarg,
147                                         1, SIZE_MAX);
148                         break;
149
150                 // --version
151                 case 'V':
152                         // This doesn't return.
153                         message_version();
154
155                 // --stdout
156                 case 'c':
157                         opt_stdout = true;
158                         break;
159
160                 // --decompress
161                 case 'd':
162                         opt_mode = MODE_DECOMPRESS;
163                         break;
164
165                 // --force
166                 case 'f':
167                         opt_force = true;
168                         break;
169
170                 // --help
171                 case 'h':
172                         // This doesn't return.
173                         message_help(false);
174
175                 // --long-help
176                 case 'H':
177                         // This doesn't return.
178                         message_help(true);
179
180                 // --list
181                 case 'l':
182                         opt_mode = MODE_LIST;
183                         break;
184
185                 // --keep
186                 case 'k':
187                         opt_keep_original = true;
188                         break;
189
190                 // --quiet
191                 case 'q':
192                         message_verbosity_decrease();
193                         break;
194
195                 case 't':
196                         opt_mode = MODE_TEST;
197                         break;
198
199                 // --verbose
200                 case 'v':
201                         message_verbosity_increase();
202                         break;
203
204                 case 'z':
205                         opt_mode = MODE_COMPRESS;
206                         break;
207
208                 // Filter setup
209
210                 case OPT_SUBBLOCK:
211                         coder_add_filter(LZMA_FILTER_SUBBLOCK,
212                                         options_subblock(optarg));
213                         break;
214
215                 case OPT_X86:
216                         coder_add_filter(LZMA_FILTER_X86, NULL);
217                         break;
218
219                 case OPT_POWERPC:
220                         coder_add_filter(LZMA_FILTER_POWERPC, NULL);
221                         break;
222
223                 case OPT_IA64:
224                         coder_add_filter(LZMA_FILTER_IA64, NULL);
225                         break;
226
227                 case OPT_ARM:
228                         coder_add_filter(LZMA_FILTER_ARM, NULL);
229                         break;
230
231                 case OPT_ARMTHUMB:
232                         coder_add_filter(LZMA_FILTER_ARMTHUMB, NULL);
233                         break;
234
235                 case OPT_SPARC:
236                         coder_add_filter(LZMA_FILTER_SPARC, NULL);
237                         break;
238
239                 case OPT_DELTA:
240                         coder_add_filter(LZMA_FILTER_DELTA,
241                                         options_delta(optarg));
242                         break;
243
244                 case OPT_LZMA1:
245                         coder_add_filter(LZMA_FILTER_LZMA1,
246                                         options_lzma(optarg));
247                         break;
248
249                 case OPT_LZMA2:
250                         coder_add_filter(LZMA_FILTER_LZMA2,
251                                         options_lzma(optarg));
252                         break;
253
254                 // Other
255
256                 // --format
257                 case 'F': {
258                         // Just in case, support both "lzma" and "alone" since
259                         // the latter was used for forward compatibility in
260                         // LZMA Utils 4.32.x.
261                         static const struct {
262                                 char str[8];
263                                 enum format_type format;
264                         } types[] = {
265                                 { "auto",   FORMAT_AUTO },
266                                 { "xz",     FORMAT_XZ },
267                                 { "lzma",   FORMAT_LZMA },
268                                 { "alone",  FORMAT_LZMA },
269                                 // { "gzip",   FORMAT_GZIP },
270                                 // { "gz",     FORMAT_GZIP },
271                                 { "raw",    FORMAT_RAW },
272                         };
273
274                         size_t i = 0;
275                         while (strcmp(types[i].str, optarg) != 0)
276                                 if (++i == ARRAY_SIZE(types))
277                                         message_fatal(_("%s: Unknown file "
278                                                         "format type"),
279                                                         optarg);
280
281                         opt_format = types[i].format;
282                         break;
283                 }
284
285                 // --check
286                 case 'C': {
287                         static const struct {
288                                 char str[8];
289                                 lzma_check check;
290                         } types[] = {
291                                 { "none",   LZMA_CHECK_NONE },
292                                 { "crc32",  LZMA_CHECK_CRC32 },
293                                 { "crc64",  LZMA_CHECK_CRC64 },
294                                 { "sha256", LZMA_CHECK_SHA256 },
295                         };
296
297                         size_t i = 0;
298                         while (strcmp(types[i].str, optarg) != 0) {
299                                 if (++i == ARRAY_SIZE(types))
300                                         message_fatal(_("%s: Unknown integrity"
301                                                         "check type"), optarg);
302                         }
303
304                         coder_set_check(types[i].check);
305                         break;
306                 }
307
308                 case OPT_FILES:
309                         args->files_delim = '\n';
310
311                 // Fall through
312
313                 case OPT_FILES0:
314                         if (args->files_name != NULL)
315                                 message_fatal(_("Only one file can be "
316                                                 "specified with `--files'"
317                                                 "or `--files0'."));
318
319                         if (optarg == NULL) {
320                                 args->files_name = (char *)stdin_filename;
321                                 args->files_file = stdin;
322                         } else {
323                                 args->files_name = optarg;
324                                 args->files_file = fopen(optarg,
325                                                 c == OPT_FILES ? "r" : "rb");
326                                 if (args->files_file == NULL)
327                                         message_fatal("%s: %s", optarg,
328                                                         strerror(errno));
329                         }
330
331                         break;
332
333                 default:
334                         message_try_help();
335                         my_exit(E_ERROR);
336                 }
337         }
338
339         return;
340 }
341
342
343 static void
344 parse_environment(args_info *args, char *argv0)
345 {
346         char *env = getenv("XZ_OPT");
347         if (env == NULL)
348                 return;
349
350         // We modify the string, so make a copy of it.
351         env = xstrdup(env);
352
353         // Calculate the number of arguments in env. argc stats at one
354         // to include space for the program name.
355         int argc = 1;
356         bool prev_was_space = true;
357         for (size_t i = 0; env[i] != '\0'; ++i) {
358                 if (isspace(env[i])) {
359                         prev_was_space = true;
360                 } else if (prev_was_space) {
361                         prev_was_space = false;
362
363                         // Keep argc small enough to fit into a singed int
364                         // and to keep it usable for memory allocation.
365                         if (++argc == MIN(INT_MAX, SIZE_MAX / sizeof(char *)))
366                                 message_fatal(_("The environment variable "
367                                                 "XZ_OPT contains too many "
368                                                 "arguments"));
369                 }
370         }
371
372         // Allocate memory to hold pointers to the arguments. Add one to get
373         // space for the terminating NULL (if some systems happen to need it).
374         char **argv = xmalloc(((size_t)(argc) + 1) * sizeof(char *));
375         argv[0] = argv0;
376         argv[argc] = NULL;
377
378         // Go through the string again. Split the arguments using '\0'
379         // characters and add pointers to the resulting strings to argv.
380         argc = 1;
381         prev_was_space = true;
382         for (size_t i = 0; env[i] != '\0'; ++i) {
383                 if (isspace(env[i])) {
384                         prev_was_space = true;
385                         env[i] = '\0';
386                 } else if (prev_was_space) {
387                         prev_was_space = false;
388                         argv[argc++] = env + i;
389                 }
390         }
391
392         // Parse the argument list we got from the environment. All non-option
393         // arguments i.e. filenames are ignored.
394         parse_real(args, argc, argv);
395
396         // Reset the state of the getopt_long() so that we can parse the
397         // command line options too. There are two incompatible ways to
398         // do it.
399 #ifdef HAVE_OPTRESET
400         // BSD
401         optind = 1;
402         optreset = 1;
403 #else
404         // GNU, Solaris
405         optind = 0;
406 #endif
407
408         // We don't need the argument list from environment anymore.
409         free(argv);
410         free(env);
411
412         return;
413 }
414
415
416 extern void
417 args_parse(args_info *args, int argc, char **argv)
418 {
419         // Initialize those parts of *args that we need later.
420         args->files_name = NULL;
421         args->files_file = NULL;
422         args->files_delim = '\0';
423
424         // Type of the file format to use when --format=auto or no --format
425         // was specified.
426         enum format_type format_compress_auto = FORMAT_XZ;
427
428         // Check how we were called.
429         {
430                 // Remove the leading path name, if any.
431                 const char *name = strrchr(argv[0], '/');
432                 if (name == NULL)
433                         name = argv[0];
434                 else
435                         ++name;
436
437                 // NOTE: It's possible that name[0] is now '\0' if argv[0]
438                 // is weird, but it doesn't matter here.
439
440                 // The default file format is .lzma if the command name
441                 // contains "lz".
442                 if (strstr(name, "lz") != NULL)
443                         format_compress_auto = FORMAT_LZMA;
444
445                 // Operation mode
446                 if (strstr(name, "cat") != NULL) {
447                         // Imply --decompress --stdout
448                         opt_mode = MODE_DECOMPRESS;
449                         opt_stdout = true;
450                 } else if (strstr(name, "un") != NULL) {
451                         // Imply --decompress
452                         opt_mode = MODE_DECOMPRESS;
453                 }
454         }
455
456         // First the flags from environment
457         parse_environment(args, argv[0]);
458
459         // Then from the command line
460         optind = 1;
461         parse_real(args, argc, argv);
462
463         // Never remove the source file when the destination is not on disk.
464         // In test mode the data is written nowhere, but setting opt_stdout
465         // will make the rest of the code behave well.
466         if (opt_stdout || opt_mode == MODE_TEST) {
467                 opt_keep_original = true;
468                 opt_stdout = true;
469         }
470
471         // If no --format flag was used, or it was --format=auto, we need to
472         // decide what is the target file format we are going to use. This
473         // depends on how we were called (checked earlier in this function).
474         if (opt_mode == MODE_COMPRESS && opt_format == FORMAT_AUTO)
475                 opt_format = format_compress_auto;
476
477         // Compression settings need to be validated (options themselves and
478         // their memory usage) when compressing to any file format. It has to
479         // be done also when uncompressing raw data, since for raw decoding
480         // the options given on the command line are used to know what kind
481         // of raw data we are supposed to decode.
482         if (opt_mode == MODE_COMPRESS || opt_format == FORMAT_RAW)
483                 coder_set_compression_settings();
484
485         // If no filenames are given, use stdin.
486         if (argv[optind] == NULL && args->files_name == NULL) {
487                 // We don't modify or free() the "-" constant. The caller
488                 // modifies this so don't make the struct itself const.
489                 static char *names_stdin[2] = { (char *)"-", NULL };
490                 args->arg_names = names_stdin;
491                 args->arg_count = 1;
492         } else {
493                 // We got at least one filename from the command line, or
494                 // --files or --files0 was specified.
495                 args->arg_names = argv + optind;
496                 args->arg_count = argc - optind;
497         }
498
499         return;
500 }