2007-12-08 23:42:33 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
/// \file args.c
|
|
|
|
/// \brief Argument parsing
|
|
|
|
///
|
|
|
|
/// \note Filter-specific options parsing is in options.c.
|
|
|
|
//
|
2009-04-13 10:27:40 +02:00
|
|
|
// Author: Lasse Collin
|
2007-12-08 23:42:33 +01:00
|
|
|
//
|
2009-04-13 10:27:40 +02:00
|
|
|
// This file has been put into the public domain.
|
|
|
|
// You can do whatever you want with this file.
|
2007-12-08 23:42:33 +01:00
|
|
|
//
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "private.h"
|
|
|
|
|
|
|
|
#include "getopt.h"
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
|
|
|
|
bool opt_stdout = false;
|
|
|
|
bool opt_force = false;
|
|
|
|
bool opt_keep_original = false;
|
2009-11-16 17:16:45 +01:00
|
|
|
bool opt_robot = false;
|
2014-08-05 21:32:36 +02:00
|
|
|
bool opt_ignore_check = false;
|
2007-12-08 23:42:33 +01:00
|
|
|
|
|
|
|
// We don't modify or free() this, but we need to assign it in some
|
|
|
|
// non-const pointers.
|
2010-09-05 20:11:33 +02:00
|
|
|
const char stdin_filename[] = "(stdin)";
|
2007-12-08 23:42:33 +01:00
|
|
|
|
|
|
|
|
2010-08-07 19:45:18 +02:00
|
|
|
/// Parse and set the memory usage limit for compression and/or decompression.
|
|
|
|
static void
|
|
|
|
parse_memlimit(const char *name, const char *name_percentage, char *str,
|
|
|
|
bool set_compress, bool set_decompress)
|
|
|
|
{
|
|
|
|
bool is_percentage = false;
|
|
|
|
uint64_t value;
|
|
|
|
|
|
|
|
const size_t len = strlen(str);
|
|
|
|
if (len > 0 && str[len - 1] == '%') {
|
|
|
|
str[len - 1] = '\0';
|
|
|
|
is_percentage = true;
|
|
|
|
value = str_to_uint64(name_percentage, str, 1, 100);
|
|
|
|
} else {
|
|
|
|
// On 32-bit systems, SIZE_MAX would make more sense than
|
|
|
|
// UINT64_MAX. But use UINT64_MAX still so that scripts
|
|
|
|
// that assume > 4 GiB values don't break.
|
|
|
|
value = str_to_uint64(name, str, 0, UINT64_MAX);
|
|
|
|
}
|
|
|
|
|
|
|
|
hardware_memlimit_set(
|
|
|
|
value, set_compress, set_decompress, is_percentage);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-03 20:16:39 +02:00
|
|
|
static void
|
|
|
|
parse_block_list(char *str)
|
|
|
|
{
|
|
|
|
// It must be non-empty and not begin with a comma.
|
|
|
|
if (str[0] == '\0' || str[0] == ',')
|
|
|
|
message_fatal(_("%s: Invalid argument to --block-list"), str);
|
|
|
|
|
|
|
|
// Count the number of comma-separated strings.
|
|
|
|
size_t count = 1;
|
|
|
|
for (size_t i = 0; str[i] != '\0'; ++i)
|
|
|
|
if (str[i] == ',')
|
|
|
|
++count;
|
|
|
|
|
|
|
|
// Prevent an unlikely integer overflow.
|
|
|
|
if (count > SIZE_MAX / sizeof(uint64_t) - 1)
|
|
|
|
message_fatal(_("%s: Too many arguments to --block-list"),
|
|
|
|
str);
|
|
|
|
|
|
|
|
// Allocate memory to hold all the sizes specified.
|
|
|
|
// If --block-list was specified already, its value is forgotten.
|
|
|
|
free(opt_block_list);
|
|
|
|
opt_block_list = xmalloc((count + 1) * sizeof(uint64_t));
|
|
|
|
|
|
|
|
for (size_t i = 0; i < count; ++i) {
|
|
|
|
// Locate the next comma and replace it with \0.
|
|
|
|
char *p = strchr(str, ',');
|
|
|
|
if (p != NULL)
|
|
|
|
*p = '\0';
|
|
|
|
|
|
|
|
if (str[0] == '\0') {
|
|
|
|
// There is no string, that is, a comma follows
|
|
|
|
// another comma. Use the previous value.
|
|
|
|
//
|
|
|
|
// NOTE: We checked earler that the first char
|
|
|
|
// of the whole list cannot be a comma.
|
|
|
|
assert(i > 0);
|
|
|
|
opt_block_list[i] = opt_block_list[i - 1];
|
|
|
|
} else {
|
|
|
|
opt_block_list[i] = str_to_uint64("block-list", str,
|
|
|
|
0, UINT64_MAX);
|
|
|
|
|
|
|
|
// Zero indicates no more new Blocks.
|
|
|
|
if (opt_block_list[i] == 0) {
|
|
|
|
if (i + 1 != count)
|
|
|
|
message_fatal(_("0 can only be used "
|
|
|
|
"as the last element "
|
|
|
|
"in --block-list"));
|
|
|
|
|
|
|
|
opt_block_list[i] = UINT64_MAX;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
str = p + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Terminate the array.
|
|
|
|
opt_block_list[count] = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-12-08 23:42:33 +01:00
|
|
|
static void
|
2008-11-19 19:46:52 +01:00
|
|
|
parse_real(args_info *args, int argc, char **argv)
|
2007-12-08 23:42:33 +01:00
|
|
|
{
|
2008-11-19 19:46:52 +01:00
|
|
|
enum {
|
2010-05-26 08:55:47 +02:00
|
|
|
OPT_X86 = INT_MIN,
|
2008-11-19 19:46:52 +01:00
|
|
|
OPT_POWERPC,
|
|
|
|
OPT_IA64,
|
|
|
|
OPT_ARM,
|
|
|
|
OPT_ARMTHUMB,
|
|
|
|
OPT_SPARC,
|
|
|
|
OPT_DELTA,
|
|
|
|
OPT_LZMA1,
|
|
|
|
OPT_LZMA2,
|
|
|
|
|
2011-03-18 17:19:19 +01:00
|
|
|
OPT_SINGLE_STREAM,
|
2009-11-25 10:19:20 +01:00
|
|
|
OPT_NO_SPARSE,
|
2008-11-19 19:46:52 +01:00
|
|
|
OPT_FILES,
|
|
|
|
OPT_FILES0,
|
2011-03-18 18:10:30 +01:00
|
|
|
OPT_BLOCK_SIZE,
|
2012-07-03 20:16:39 +02:00
|
|
|
OPT_BLOCK_LIST,
|
2010-08-07 19:45:18 +02:00
|
|
|
OPT_MEM_COMPRESS,
|
|
|
|
OPT_MEM_DECOMPRESS,
|
2010-06-15 13:06:29 +02:00
|
|
|
OPT_NO_ADJUST,
|
2009-11-16 17:16:45 +01:00
|
|
|
OPT_INFO_MEMORY,
|
|
|
|
OPT_ROBOT,
|
2013-07-04 13:18:46 +02:00
|
|
|
OPT_FLUSH_TIMEOUT,
|
2014-08-05 21:32:36 +02:00
|
|
|
OPT_IGNORE_CHECK,
|
2008-11-19 19:46:52 +01:00
|
|
|
};
|
|
|
|
|
2009-05-22 13:43:00 +02:00
|
|
|
static const char short_opts[]
|
|
|
|
= "cC:defF:hHlkM:qQrS:tT:vVz0123456789";
|
2008-11-19 19:46:52 +01:00
|
|
|
|
|
|
|
static const struct option long_opts[] = {
|
|
|
|
// Operation mode
|
2009-11-16 17:16:45 +01:00
|
|
|
{ "compress", no_argument, NULL, 'z' },
|
|
|
|
{ "decompress", no_argument, NULL, 'd' },
|
|
|
|
{ "uncompress", no_argument, NULL, 'd' },
|
|
|
|
{ "test", no_argument, NULL, 't' },
|
|
|
|
{ "list", no_argument, NULL, 'l' },
|
2008-11-19 19:46:52 +01:00
|
|
|
|
|
|
|
// Operation modifiers
|
2009-11-16 17:16:45 +01:00
|
|
|
{ "keep", no_argument, NULL, 'k' },
|
|
|
|
{ "force", no_argument, NULL, 'f' },
|
|
|
|
{ "stdout", no_argument, NULL, 'c' },
|
|
|
|
{ "to-stdout", no_argument, NULL, 'c' },
|
2011-03-18 17:19:19 +01:00
|
|
|
{ "single-stream", no_argument, NULL, OPT_SINGLE_STREAM },
|
2009-11-25 10:19:20 +01:00
|
|
|
{ "no-sparse", no_argument, NULL, OPT_NO_SPARSE },
|
2009-11-16 17:16:45 +01:00
|
|
|
{ "suffix", required_argument, NULL, 'S' },
|
2008-11-19 19:46:52 +01:00
|
|
|
// { "recursive", no_argument, NULL, 'r' }, // TODO
|
2009-11-16 17:16:45 +01:00
|
|
|
{ "files", optional_argument, NULL, OPT_FILES },
|
|
|
|
{ "files0", optional_argument, NULL, OPT_FILES0 },
|
2008-11-19 19:46:52 +01:00
|
|
|
|
|
|
|
// Basic compression settings
|
2009-11-16 17:16:45 +01:00
|
|
|
{ "format", required_argument, NULL, 'F' },
|
|
|
|
{ "check", required_argument, NULL, 'C' },
|
2014-08-05 21:32:36 +02:00
|
|
|
{ "ignore-check", no_argument, NULL, OPT_IGNORE_CHECK },
|
2011-03-18 18:10:30 +01:00
|
|
|
{ "block-size", required_argument, NULL, OPT_BLOCK_SIZE },
|
2012-07-03 20:16:39 +02:00
|
|
|
{ "block-list", required_argument, NULL, OPT_BLOCK_LIST },
|
2010-08-07 19:45:18 +02:00
|
|
|
{ "memlimit-compress", required_argument, NULL, OPT_MEM_COMPRESS },
|
|
|
|
{ "memlimit-decompress", required_argument, NULL, OPT_MEM_DECOMPRESS },
|
|
|
|
{ "memlimit", required_argument, NULL, 'M' },
|
|
|
|
{ "memory", required_argument, NULL, 'M' }, // Old alias
|
2010-06-15 13:06:29 +02:00
|
|
|
{ "no-adjust", no_argument, NULL, OPT_NO_ADJUST },
|
2009-11-16 17:16:45 +01:00
|
|
|
{ "threads", required_argument, NULL, 'T' },
|
2013-07-04 13:18:46 +02:00
|
|
|
{ "flush-timeout", required_argument, NULL, OPT_FLUSH_TIMEOUT },
|
2008-11-19 19:46:52 +01:00
|
|
|
|
2009-11-16 17:16:45 +01:00
|
|
|
{ "extreme", no_argument, NULL, 'e' },
|
|
|
|
{ "fast", no_argument, NULL, '0' },
|
|
|
|
{ "best", no_argument, NULL, '9' },
|
2008-11-19 19:46:52 +01:00
|
|
|
|
|
|
|
// Filters
|
2009-11-16 17:16:45 +01:00
|
|
|
{ "lzma1", optional_argument, NULL, OPT_LZMA1 },
|
|
|
|
{ "lzma2", optional_argument, NULL, OPT_LZMA2 },
|
|
|
|
{ "x86", optional_argument, NULL, OPT_X86 },
|
|
|
|
{ "powerpc", optional_argument, NULL, OPT_POWERPC },
|
|
|
|
{ "ia64", optional_argument, NULL, OPT_IA64 },
|
|
|
|
{ "arm", optional_argument, NULL, OPT_ARM },
|
|
|
|
{ "armthumb", optional_argument, NULL, OPT_ARMTHUMB },
|
|
|
|
{ "sparc", optional_argument, NULL, OPT_SPARC },
|
|
|
|
{ "delta", optional_argument, NULL, OPT_DELTA },
|
2008-11-19 19:46:52 +01:00
|
|
|
|
|
|
|
// Other options
|
2009-11-16 17:16:45 +01:00
|
|
|
{ "quiet", no_argument, NULL, 'q' },
|
|
|
|
{ "verbose", no_argument, NULL, 'v' },
|
|
|
|
{ "no-warn", no_argument, NULL, 'Q' },
|
|
|
|
{ "robot", no_argument, NULL, OPT_ROBOT },
|
|
|
|
{ "info-memory", no_argument, NULL, OPT_INFO_MEMORY },
|
|
|
|
{ "help", no_argument, NULL, 'h' },
|
|
|
|
{ "long-help", no_argument, NULL, 'H' },
|
|
|
|
{ "version", no_argument, NULL, 'V' },
|
2008-11-19 19:46:52 +01:00
|
|
|
|
2010-08-07 19:45:18 +02:00
|
|
|
{ NULL, 0, NULL, 0 }
|
2008-11-19 19:46:52 +01:00
|
|
|
};
|
2007-12-08 23:42:33 +01:00
|
|
|
|
|
|
|
int c;
|
|
|
|
|
|
|
|
while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL))
|
|
|
|
!= -1) {
|
|
|
|
switch (c) {
|
2008-12-27 18:30:19 +01:00
|
|
|
// Compression preset (also for decompression if --format=raw)
|
|
|
|
case '0': case '1': case '2': case '3': case '4':
|
2007-12-08 23:42:33 +01:00
|
|
|
case '5': case '6': case '7': case '8': case '9':
|
2008-11-19 19:46:52 +01:00
|
|
|
coder_set_preset(c - '0');
|
2007-12-08 23:42:33 +01:00
|
|
|
break;
|
|
|
|
|
2010-08-07 19:45:18 +02:00
|
|
|
// --memlimit-compress
|
|
|
|
case OPT_MEM_COMPRESS:
|
|
|
|
parse_memlimit("memlimit-compress",
|
|
|
|
"memlimit-compress%", optarg,
|
|
|
|
true, false);
|
|
|
|
break;
|
2009-05-22 10:29:50 +02:00
|
|
|
|
2010-08-07 19:45:18 +02:00
|
|
|
// --memlimit-decompress
|
|
|
|
case OPT_MEM_DECOMPRESS:
|
|
|
|
parse_memlimit("memlimit-decompress",
|
|
|
|
"memlimit-decompress%", optarg,
|
|
|
|
false, true);
|
|
|
|
break;
|
|
|
|
|
|
|
|
// --memlimit
|
|
|
|
case 'M':
|
|
|
|
parse_memlimit("memlimit", "memlimit%", optarg,
|
|
|
|
true, true);
|
2007-12-08 23:42:33 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
// --suffix
|
|
|
|
case 'S':
|
2008-11-19 19:46:52 +01:00
|
|
|
suffix_set(optarg);
|
2007-12-08 23:42:33 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'T':
|
2011-04-11 21:06:03 +02:00
|
|
|
// The max is from src/liblzma/common/common.h.
|
2011-04-09 14:11:13 +02:00
|
|
|
hardware_threads_set(str_to_uint64("threads",
|
2011-04-11 21:06:03 +02:00
|
|
|
optarg, 0, 16384));
|
2007-12-08 23:42:33 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
// --version
|
|
|
|
case 'V':
|
|
|
|
// This doesn't return.
|
2008-11-19 19:46:52 +01:00
|
|
|
message_version();
|
2007-12-08 23:42:33 +01:00
|
|
|
|
|
|
|
// --stdout
|
|
|
|
case 'c':
|
|
|
|
opt_stdout = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
// --decompress
|
|
|
|
case 'd':
|
|
|
|
opt_mode = MODE_DECOMPRESS;
|
|
|
|
break;
|
|
|
|
|
2008-12-27 18:30:19 +01:00
|
|
|
// --extreme
|
|
|
|
case 'e':
|
|
|
|
coder_set_extreme();
|
|
|
|
break;
|
|
|
|
|
2007-12-08 23:42:33 +01:00
|
|
|
// --force
|
|
|
|
case 'f':
|
|
|
|
opt_force = true;
|
|
|
|
break;
|
|
|
|
|
2009-11-16 17:16:45 +01:00
|
|
|
// --info-memory
|
|
|
|
case OPT_INFO_MEMORY:
|
|
|
|
// This doesn't return.
|
2010-08-07 19:45:18 +02:00
|
|
|
hardware_memlimit_show();
|
2009-11-16 17:16:45 +01:00
|
|
|
|
2007-12-08 23:42:33 +01:00
|
|
|
// --help
|
|
|
|
case 'h':
|
|
|
|
// This doesn't return.
|
2008-11-19 19:46:52 +01:00
|
|
|
message_help(false);
|
|
|
|
|
|
|
|
// --long-help
|
|
|
|
case 'H':
|
|
|
|
// This doesn't return.
|
|
|
|
message_help(true);
|
2007-12-08 23:42:33 +01:00
|
|
|
|
|
|
|
// --list
|
|
|
|
case 'l':
|
|
|
|
opt_mode = MODE_LIST;
|
|
|
|
break;
|
|
|
|
|
|
|
|
// --keep
|
|
|
|
case 'k':
|
|
|
|
opt_keep_original = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
// --quiet
|
|
|
|
case 'q':
|
2008-11-19 19:46:52 +01:00
|
|
|
message_verbosity_decrease();
|
2007-12-08 23:42:33 +01:00
|
|
|
break;
|
|
|
|
|
2009-05-22 13:43:00 +02:00
|
|
|
case 'Q':
|
|
|
|
set_exit_no_warn();
|
|
|
|
break;
|
|
|
|
|
2007-12-08 23:42:33 +01:00
|
|
|
case 't':
|
|
|
|
opt_mode = MODE_TEST;
|
|
|
|
break;
|
|
|
|
|
|
|
|
// --verbose
|
|
|
|
case 'v':
|
2008-11-19 19:46:52 +01:00
|
|
|
message_verbosity_increase();
|
2007-12-08 23:42:33 +01:00
|
|
|
break;
|
|
|
|
|
2009-11-16 17:16:45 +01:00
|
|
|
// --robot
|
|
|
|
case OPT_ROBOT:
|
|
|
|
opt_robot = true;
|
2010-01-24 21:45:14 +01:00
|
|
|
|
|
|
|
// This is to make sure that floating point numbers
|
|
|
|
// always have a dot as decimal separator.
|
|
|
|
setlocale(LC_NUMERIC, "C");
|
2009-11-16 17:16:45 +01:00
|
|
|
break;
|
|
|
|
|
2007-12-08 23:42:33 +01:00
|
|
|
case 'z':
|
|
|
|
opt_mode = MODE_COMPRESS;
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Filter setup
|
|
|
|
|
|
|
|
case OPT_X86:
|
2009-05-23 14:12:23 +02:00
|
|
|
coder_add_filter(LZMA_FILTER_X86,
|
|
|
|
options_bcj(optarg));
|
2007-12-08 23:42:33 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OPT_POWERPC:
|
2009-05-23 14:12:23 +02:00
|
|
|
coder_add_filter(LZMA_FILTER_POWERPC,
|
|
|
|
options_bcj(optarg));
|
2007-12-08 23:42:33 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OPT_IA64:
|
2009-05-23 14:12:23 +02:00
|
|
|
coder_add_filter(LZMA_FILTER_IA64,
|
|
|
|
options_bcj(optarg));
|
2007-12-08 23:42:33 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OPT_ARM:
|
2009-05-23 14:12:23 +02:00
|
|
|
coder_add_filter(LZMA_FILTER_ARM,
|
|
|
|
options_bcj(optarg));
|
2007-12-08 23:42:33 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OPT_ARMTHUMB:
|
2009-05-23 14:12:23 +02:00
|
|
|
coder_add_filter(LZMA_FILTER_ARMTHUMB,
|
|
|
|
options_bcj(optarg));
|
2007-12-08 23:42:33 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OPT_SPARC:
|
2009-05-23 14:12:23 +02:00
|
|
|
coder_add_filter(LZMA_FILTER_SPARC,
|
|
|
|
options_bcj(optarg));
|
2007-12-08 23:42:33 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OPT_DELTA:
|
2008-11-19 19:46:52 +01:00
|
|
|
coder_add_filter(LZMA_FILTER_DELTA,
|
|
|
|
options_delta(optarg));
|
2007-12-08 23:42:33 +01:00
|
|
|
break;
|
|
|
|
|
2008-09-04 10:53:06 +02:00
|
|
|
case OPT_LZMA1:
|
2008-11-19 19:46:52 +01:00
|
|
|
coder_add_filter(LZMA_FILTER_LZMA1,
|
|
|
|
options_lzma(optarg));
|
2007-12-08 23:42:33 +01:00
|
|
|
break;
|
|
|
|
|
2008-08-28 21:53:15 +02:00
|
|
|
case OPT_LZMA2:
|
2008-11-19 19:46:52 +01:00
|
|
|
coder_add_filter(LZMA_FILTER_LZMA2,
|
|
|
|
options_lzma(optarg));
|
2008-08-28 21:53:15 +02:00
|
|
|
break;
|
|
|
|
|
2007-12-08 23:42:33 +01:00
|
|
|
// Other
|
|
|
|
|
|
|
|
// --format
|
|
|
|
case 'F': {
|
2008-10-02 21:51:46 +02:00
|
|
|
// Just in case, support both "lzma" and "alone" since
|
|
|
|
// the latter was used for forward compatibility in
|
|
|
|
// LZMA Utils 4.32.x.
|
|
|
|
static const struct {
|
|
|
|
char str[8];
|
|
|
|
enum format_type format;
|
|
|
|
} types[] = {
|
|
|
|
{ "auto", FORMAT_AUTO },
|
|
|
|
{ "xz", FORMAT_XZ },
|
|
|
|
{ "lzma", FORMAT_LZMA },
|
|
|
|
{ "alone", FORMAT_LZMA },
|
|
|
|
// { "gzip", FORMAT_GZIP },
|
|
|
|
// { "gz", FORMAT_GZIP },
|
|
|
|
{ "raw", FORMAT_RAW },
|
2007-12-08 23:42:33 +01:00
|
|
|
};
|
|
|
|
|
2008-10-02 21:51:46 +02:00
|
|
|
size_t i = 0;
|
2008-11-19 19:46:52 +01:00
|
|
|
while (strcmp(types[i].str, optarg) != 0)
|
|
|
|
if (++i == ARRAY_SIZE(types))
|
|
|
|
message_fatal(_("%s: Unknown file "
|
2007-12-08 23:42:33 +01:00
|
|
|
"format type"),
|
|
|
|
optarg);
|
|
|
|
|
2008-10-02 21:51:46 +02:00
|
|
|
opt_format = types[i].format;
|
2007-12-08 23:42:33 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// --check
|
|
|
|
case 'C': {
|
|
|
|
static const struct {
|
2008-10-02 21:51:46 +02:00
|
|
|
char str[8];
|
|
|
|
lzma_check check;
|
2007-12-08 23:42:33 +01:00
|
|
|
} types[] = {
|
|
|
|
{ "none", LZMA_CHECK_NONE },
|
|
|
|
{ "crc32", LZMA_CHECK_CRC32 },
|
|
|
|
{ "crc64", LZMA_CHECK_CRC64 },
|
|
|
|
{ "sha256", LZMA_CHECK_SHA256 },
|
|
|
|
};
|
|
|
|
|
|
|
|
size_t i = 0;
|
|
|
|
while (strcmp(types[i].str, optarg) != 0) {
|
2008-11-19 19:46:52 +01:00
|
|
|
if (++i == ARRAY_SIZE(types))
|
2009-02-13 16:30:30 +01:00
|
|
|
message_fatal(_("%s: Unsupported "
|
|
|
|
"integrity "
|
2008-11-19 19:46:52 +01:00
|
|
|
"check type"), optarg);
|
2007-12-08 23:42:33 +01:00
|
|
|
}
|
|
|
|
|
2009-02-13 16:30:30 +01:00
|
|
|
// Use a separate check in case we are using different
|
|
|
|
// liblzma than what was used to compile us.
|
|
|
|
if (!lzma_check_is_supported(types[i].check))
|
|
|
|
message_fatal(_("%s: Unsupported integrity "
|
|
|
|
"check type"), optarg);
|
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
coder_set_check(types[i].check);
|
2007-12-08 23:42:33 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-08-05 21:32:36 +02:00
|
|
|
case OPT_IGNORE_CHECK:
|
|
|
|
opt_ignore_check = true;
|
|
|
|
break;
|
|
|
|
|
2011-03-18 18:10:30 +01:00
|
|
|
case OPT_BLOCK_SIZE:
|
|
|
|
opt_block_size = str_to_uint64("block-size", optarg,
|
|
|
|
0, LZMA_VLI_MAX);
|
|
|
|
break;
|
|
|
|
|
2012-07-03 20:16:39 +02:00
|
|
|
case OPT_BLOCK_LIST: {
|
|
|
|
parse_block_list(optarg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-03-18 17:19:19 +01:00
|
|
|
case OPT_SINGLE_STREAM:
|
|
|
|
opt_single_stream = true;
|
|
|
|
break;
|
|
|
|
|
2009-11-25 10:19:20 +01:00
|
|
|
case OPT_NO_SPARSE:
|
|
|
|
io_no_sparse();
|
|
|
|
break;
|
|
|
|
|
2007-12-08 23:42:33 +01:00
|
|
|
case OPT_FILES:
|
2008-11-19 19:46:52 +01:00
|
|
|
args->files_delim = '\n';
|
2007-12-08 23:42:33 +01:00
|
|
|
|
|
|
|
// Fall through
|
|
|
|
|
|
|
|
case OPT_FILES0:
|
2008-11-19 19:46:52 +01:00
|
|
|
if (args->files_name != NULL)
|
|
|
|
message_fatal(_("Only one file can be "
|
2010-01-26 22:37:46 +01:00
|
|
|
"specified with `--files' "
|
2007-12-08 23:42:33 +01:00
|
|
|
"or `--files0'."));
|
|
|
|
|
|
|
|
if (optarg == NULL) {
|
2008-11-19 19:46:52 +01:00
|
|
|
args->files_name = (char *)stdin_filename;
|
|
|
|
args->files_file = stdin;
|
2007-12-08 23:42:33 +01:00
|
|
|
} else {
|
2008-11-19 19:46:52 +01:00
|
|
|
args->files_name = optarg;
|
|
|
|
args->files_file = fopen(optarg,
|
2007-12-08 23:42:33 +01:00
|
|
|
c == OPT_FILES ? "r" : "rb");
|
2008-11-19 19:46:52 +01:00
|
|
|
if (args->files_file == NULL)
|
|
|
|
message_fatal("%s: %s", optarg,
|
2007-12-08 23:42:33 +01:00
|
|
|
strerror(errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2010-06-15 13:06:29 +02:00
|
|
|
case OPT_NO_ADJUST:
|
|
|
|
opt_auto_adjust = false;
|
|
|
|
break;
|
|
|
|
|
2013-07-04 13:18:46 +02:00
|
|
|
case OPT_FLUSH_TIMEOUT:
|
|
|
|
opt_flush_timeout = str_to_uint64("flush-timeout",
|
|
|
|
optarg, 0, UINT64_MAX);
|
|
|
|
break;
|
|
|
|
|
2007-12-08 23:42:33 +01:00
|
|
|
default:
|
2008-11-19 19:46:52 +01:00
|
|
|
message_try_help();
|
2009-09-19 08:47:30 +02:00
|
|
|
tuklib_exit(E_ERROR, E_ERROR, false);
|
2007-12-08 23:42:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2010-08-07 19:45:18 +02:00
|
|
|
parse_environment(args_info *args, char *argv0, const char *varname)
|
2007-12-08 23:42:33 +01:00
|
|
|
{
|
2010-08-07 19:45:18 +02:00
|
|
|
char *env = getenv(varname);
|
2007-12-08 23:42:33 +01:00
|
|
|
if (env == NULL)
|
|
|
|
return;
|
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
// We modify the string, so make a copy of it.
|
2007-12-08 23:42:33 +01:00
|
|
|
env = xstrdup(env);
|
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
// Calculate the number of arguments in env. argc stats at one
|
|
|
|
// to include space for the program name.
|
|
|
|
int argc = 1;
|
2007-12-08 23:42:33 +01:00
|
|
|
bool prev_was_space = true;
|
|
|
|
for (size_t i = 0; env[i] != '\0'; ++i) {
|
2009-06-24 12:01:59 +02:00
|
|
|
// NOTE: Cast to unsigned char is needed so that correct
|
|
|
|
// value gets passed to isspace(), which expects
|
|
|
|
// unsigned char cast to int. Casting to int is done
|
|
|
|
// automatically due to integer promotion, but we need to
|
|
|
|
// force char to unsigned char manually. Otherwise 8-bit
|
|
|
|
// characters would get promoted to wrong value if
|
|
|
|
// char is signed.
|
|
|
|
if (isspace((unsigned char)env[i])) {
|
2007-12-08 23:42:33 +01:00
|
|
|
prev_was_space = true;
|
|
|
|
} else if (prev_was_space) {
|
|
|
|
prev_was_space = false;
|
2008-11-19 19:46:52 +01:00
|
|
|
|
2011-11-04 16:57:16 +01:00
|
|
|
// Keep argc small enough to fit into a signed int
|
2008-11-19 19:46:52 +01:00
|
|
|
// and to keep it usable for memory allocation.
|
2010-05-26 09:36:46 +02:00
|
|
|
if (++argc == my_min(
|
|
|
|
INT_MAX, SIZE_MAX / sizeof(char *)))
|
2008-11-19 19:46:52 +01:00
|
|
|
message_fatal(_("The environment variable "
|
2010-08-07 19:45:18 +02:00
|
|
|
"%s contains too many "
|
|
|
|
"arguments"), varname);
|
2007-12-08 23:42:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
// Allocate memory to hold pointers to the arguments. Add one to get
|
|
|
|
// space for the terminating NULL (if some systems happen to need it).
|
|
|
|
char **argv = xmalloc(((size_t)(argc) + 1) * sizeof(char *));
|
2007-12-08 23:42:33 +01:00
|
|
|
argv[0] = argv0;
|
|
|
|
argv[argc] = NULL;
|
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
// Go through the string again. Split the arguments using '\0'
|
|
|
|
// characters and add pointers to the resulting strings to argv.
|
2007-12-08 23:42:33 +01:00
|
|
|
argc = 1;
|
|
|
|
prev_was_space = true;
|
|
|
|
for (size_t i = 0; env[i] != '\0'; ++i) {
|
2009-06-24 12:01:59 +02:00
|
|
|
if (isspace((unsigned char)env[i])) {
|
2007-12-08 23:42:33 +01:00
|
|
|
prev_was_space = true;
|
2008-11-19 19:46:52 +01:00
|
|
|
env[i] = '\0';
|
2007-12-08 23:42:33 +01:00
|
|
|
} else if (prev_was_space) {
|
|
|
|
prev_was_space = false;
|
|
|
|
argv[argc++] = env + i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
// Parse the argument list we got from the environment. All non-option
|
|
|
|
// arguments i.e. filenames are ignored.
|
|
|
|
parse_real(args, argc, argv);
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
// Reset the state of the getopt_long() so that we can parse the
|
|
|
|
// command line options too. There are two incompatible ways to
|
|
|
|
// do it.
|
|
|
|
#ifdef HAVE_OPTRESET
|
|
|
|
// BSD
|
|
|
|
optind = 1;
|
|
|
|
optreset = 1;
|
|
|
|
#else
|
|
|
|
// GNU, Solaris
|
|
|
|
optind = 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// We don't need the argument list from environment anymore.
|
|
|
|
free(argv);
|
2007-12-08 23:42:33 +01:00
|
|
|
free(env);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
extern void
|
|
|
|
args_parse(args_info *args, int argc, char **argv)
|
2007-12-08 23:42:33 +01:00
|
|
|
{
|
2008-11-19 19:46:52 +01:00
|
|
|
// Initialize those parts of *args that we need later.
|
|
|
|
args->files_name = NULL;
|
|
|
|
args->files_file = NULL;
|
|
|
|
args->files_delim = '\0';
|
2008-10-02 21:51:46 +02:00
|
|
|
|
2007-12-08 23:42:33 +01:00
|
|
|
// Check how we were called.
|
|
|
|
{
|
2009-02-13 16:29:02 +01:00
|
|
|
// Remove the leading path name, if any.
|
2008-11-19 19:46:52 +01:00
|
|
|
const char *name = strrchr(argv[0], '/');
|
|
|
|
if (name == NULL)
|
|
|
|
name = argv[0];
|
|
|
|
else
|
|
|
|
++name;
|
|
|
|
|
|
|
|
// NOTE: It's possible that name[0] is now '\0' if argv[0]
|
|
|
|
// is weird, but it doesn't matter here.
|
|
|
|
|
2009-07-10 10:33:21 +02:00
|
|
|
// Look for full command names instead of substrings like
|
|
|
|
// "un", "cat", and "lz" to reduce possibility of false
|
|
|
|
// positives when the programs have been renamed.
|
|
|
|
if (strstr(name, "xzcat") != NULL) {
|
|
|
|
opt_mode = MODE_DECOMPRESS;
|
|
|
|
opt_stdout = true;
|
|
|
|
} else if (strstr(name, "unxz") != NULL) {
|
|
|
|
opt_mode = MODE_DECOMPRESS;
|
|
|
|
} else if (strstr(name, "lzcat") != NULL) {
|
2009-05-22 11:27:43 +02:00
|
|
|
opt_format = FORMAT_LZMA;
|
2008-11-19 19:46:52 +01:00
|
|
|
opt_mode = MODE_DECOMPRESS;
|
|
|
|
opt_stdout = true;
|
2009-07-10 10:33:21 +02:00
|
|
|
} else if (strstr(name, "unlzma") != NULL) {
|
|
|
|
opt_format = FORMAT_LZMA;
|
2008-11-19 19:46:52 +01:00
|
|
|
opt_mode = MODE_DECOMPRESS;
|
2009-07-10 10:33:21 +02:00
|
|
|
} else if (strstr(name, "lzma") != NULL) {
|
|
|
|
opt_format = FORMAT_LZMA;
|
2007-12-08 23:42:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-07 19:45:18 +02:00
|
|
|
// First the flags from the environment
|
|
|
|
parse_environment(args, argv[0], "XZ_DEFAULTS");
|
|
|
|
parse_environment(args, argv[0], "XZ_OPT");
|
2007-12-08 23:42:33 +01:00
|
|
|
|
|
|
|
// Then from the command line
|
2008-11-19 19:46:52 +01:00
|
|
|
parse_real(args, argc, argv);
|
2007-12-08 23:42:33 +01:00
|
|
|
|
|
|
|
// Never remove the source file when the destination is not on disk.
|
|
|
|
// In test mode the data is written nowhere, but setting opt_stdout
|
|
|
|
// will make the rest of the code behave well.
|
|
|
|
if (opt_stdout || opt_mode == MODE_TEST) {
|
|
|
|
opt_keep_original = true;
|
|
|
|
opt_stdout = true;
|
|
|
|
}
|
|
|
|
|
2009-05-22 11:27:43 +02:00
|
|
|
// When compressing, if no --format flag was used, or it
|
|
|
|
// was --format=auto, we compress to the .xz format.
|
2008-10-02 21:51:46 +02:00
|
|
|
if (opt_mode == MODE_COMPRESS && opt_format == FORMAT_AUTO)
|
2009-05-22 11:27:43 +02:00
|
|
|
opt_format = FORMAT_XZ;
|
2008-10-02 21:51:46 +02:00
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
// Compression settings need to be validated (options themselves and
|
|
|
|
// their memory usage) when compressing to any file format. It has to
|
|
|
|
// be done also when uncompressing raw data, since for raw decoding
|
|
|
|
// the options given on the command line are used to know what kind
|
|
|
|
// of raw data we are supposed to decode.
|
2008-10-02 21:51:46 +02:00
|
|
|
if (opt_mode == MODE_COMPRESS || opt_format == FORMAT_RAW)
|
2008-11-19 19:46:52 +01:00
|
|
|
coder_set_compression_settings();
|
2007-12-08 23:42:33 +01:00
|
|
|
|
|
|
|
// If no filenames are given, use stdin.
|
2008-11-19 19:46:52 +01:00
|
|
|
if (argv[optind] == NULL && args->files_name == NULL) {
|
|
|
|
// We don't modify or free() the "-" constant. The caller
|
|
|
|
// modifies this so don't make the struct itself const.
|
|
|
|
static char *names_stdin[2] = { (char *)"-", NULL };
|
|
|
|
args->arg_names = names_stdin;
|
|
|
|
args->arg_count = 1;
|
|
|
|
} else {
|
|
|
|
// We got at least one filename from the command line, or
|
|
|
|
// --files or --files0 was specified.
|
|
|
|
args->arg_names = argv + optind;
|
|
|
|
args->arg_count = argc - optind;
|
2007-12-08 23:42:33 +01:00
|
|
|
}
|
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
return;
|
2007-12-08 23:42:33 +01:00
|
|
|
}
|
2012-07-03 20:16:39 +02:00
|
|
|
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
extern void
|
|
|
|
args_free(void)
|
|
|
|
{
|
|
|
|
free(opt_block_list);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|