| OLD | NEW |
| 1 /////////////////////////////////////////////////////////////////////////////// | 1 /////////////////////////////////////////////////////////////////////////////// |
| 2 // | 2 // |
| 3 /// \file args.c | 3 /// \file args.c |
| 4 /// \brief Argument parsing | 4 /// \brief Argument parsing |
| 5 /// | 5 /// |
| 6 /// \note Filter-specific options parsing is in options.c. | 6 /// \note Filter-specific options parsing is in options.c. |
| 7 // | 7 // |
| 8 // Author: Lasse Collin | 8 // Author: Lasse Collin |
| 9 // | 9 // |
| 10 // This file has been put into the public domain. | 10 // This file has been put into the public domain. |
| 11 // You can do whatever you want with this file. | 11 // You can do whatever you want with this file. |
| 12 // | 12 // |
| 13 /////////////////////////////////////////////////////////////////////////////// | 13 /////////////////////////////////////////////////////////////////////////////// |
| 14 | 14 |
| 15 #include "private.h" | 15 #include "private.h" |
| 16 | 16 |
| 17 #include "getopt.h" | 17 #include "getopt.h" |
| 18 #include <ctype.h> | 18 #include <ctype.h> |
| 19 | 19 |
| 20 | 20 |
| 21 bool opt_stdout = false; | 21 bool opt_stdout = false; |
| 22 bool opt_force = false; | 22 bool opt_force = false; |
| 23 bool opt_keep_original = false; | 23 bool opt_keep_original = false; |
| 24 bool opt_robot = false; | 24 bool opt_robot = false; |
| 25 | 25 |
| 26 // We don't modify or free() this, but we need to assign it in some | 26 // We don't modify or free() this, but we need to assign it in some |
| 27 // non-const pointers. | 27 // non-const pointers. |
| 28 const char *stdin_filename = "(stdin)"; | 28 const char stdin_filename[] = "(stdin)"; |
| 29 |
| 30 |
| 31 /// Parse and set the memory usage limit for compression and/or decompression. |
| 32 static void |
| 33 parse_memlimit(const char *name, const char *name_percentage, char *str, |
| 34 » » bool set_compress, bool set_decompress) |
| 35 { |
| 36 » bool is_percentage = false; |
| 37 » uint64_t value; |
| 38 |
| 39 » const size_t len = strlen(str); |
| 40 » if (len > 0 && str[len - 1] == '%') { |
| 41 » » str[len - 1] = '\0'; |
| 42 » » is_percentage = true; |
| 43 » » value = str_to_uint64(name_percentage, str, 1, 100); |
| 44 » } else { |
| 45 » » // On 32-bit systems, SIZE_MAX would make more sense than |
| 46 » » // UINT64_MAX. But use UINT64_MAX still so that scripts |
| 47 » » // that assume > 4 GiB values don't break. |
| 48 » » value = str_to_uint64(name, str, 0, UINT64_MAX); |
| 49 » } |
| 50 |
| 51 » hardware_memlimit_set( |
| 52 » » » value, set_compress, set_decompress, is_percentage); |
| 53 » return; |
| 54 } |
| 29 | 55 |
| 30 | 56 |
| 31 static void | 57 static void |
| 32 parse_real(args_info *args, int argc, char **argv) | 58 parse_real(args_info *args, int argc, char **argv) |
| 33 { | 59 { |
| 34 enum { | 60 enum { |
| 35 OPT_X86 = INT_MIN, | 61 OPT_X86 = INT_MIN, |
| 36 OPT_POWERPC, | 62 OPT_POWERPC, |
| 37 OPT_IA64, | 63 OPT_IA64, |
| 38 OPT_ARM, | 64 OPT_ARM, |
| 39 OPT_ARMTHUMB, | 65 OPT_ARMTHUMB, |
| 40 OPT_SPARC, | 66 OPT_SPARC, |
| 41 OPT_DELTA, | 67 OPT_DELTA, |
| 42 OPT_LZMA1, | 68 OPT_LZMA1, |
| 43 OPT_LZMA2, | 69 OPT_LZMA2, |
| 44 | 70 |
| 45 OPT_NO_SPARSE, | 71 OPT_NO_SPARSE, |
| 46 OPT_FILES, | 72 OPT_FILES, |
| 47 OPT_FILES0, | 73 OPT_FILES0, |
| 74 OPT_MEM_COMPRESS, |
| 75 OPT_MEM_DECOMPRESS, |
| 48 OPT_NO_ADJUST, | 76 OPT_NO_ADJUST, |
| 49 OPT_INFO_MEMORY, | 77 OPT_INFO_MEMORY, |
| 50 OPT_ROBOT, | 78 OPT_ROBOT, |
| 51 }; | 79 }; |
| 52 | 80 |
| 53 static const char short_opts[] | 81 static const char short_opts[] |
| 54 = "cC:defF:hHlkM:qQrS:tT:vVz0123456789"; | 82 = "cC:defF:hHlkM:qQrS:tT:vVz0123456789"; |
| 55 | 83 |
| 56 static const struct option long_opts[] = { | 84 static const struct option long_opts[] = { |
| 57 // Operation mode | 85 // Operation mode |
| (...skipping 10 matching lines...) Expand all Loading... |
| 68 { "to-stdout", no_argument, NULL, 'c' }, | 96 { "to-stdout", no_argument, NULL, 'c' }, |
| 69 { "no-sparse", no_argument, NULL, OPT_NO_SPARSE }, | 97 { "no-sparse", no_argument, NULL, OPT_NO_SPARSE }, |
| 70 { "suffix", required_argument, NULL, 'S' }, | 98 { "suffix", required_argument, NULL, 'S' }, |
| 71 // { "recursive", no_argument, NULL, 'r' }, // TODO | 99 // { "recursive", no_argument, NULL, 'r' }, // TODO |
| 72 { "files", optional_argument, NULL, OPT_FILES }, | 100 { "files", optional_argument, NULL, OPT_FILES }, |
| 73 { "files0", optional_argument, NULL, OPT_FILES0 }, | 101 { "files0", optional_argument, NULL, OPT_FILES0 }, |
| 74 | 102 |
| 75 // Basic compression settings | 103 // Basic compression settings |
| 76 { "format", required_argument, NULL, 'F' }, | 104 { "format", required_argument, NULL, 'F' }, |
| 77 { "check", required_argument, NULL, 'C' }, | 105 { "check", required_argument, NULL, 'C' }, |
| 106 { "memlimit-compress", required_argument, NULL, OPT_MEM_COMPRE
SS }, |
| 107 { "memlimit-decompress", required_argument, NULL, OPT_MEM_DECOMP
RESS }, |
| 108 { "memlimit", required_argument, NULL, 'M' }, |
| 109 { "memory", required_argument, NULL, 'M' }, // Old alias |
| 78 { "no-adjust", no_argument, NULL, OPT_NO_ADJUST }, | 110 { "no-adjust", no_argument, NULL, OPT_NO_ADJUST }, |
| 79 { "memory", required_argument, NULL, 'M' }, | |
| 80 { "threads", required_argument, NULL, 'T' }, | 111 { "threads", required_argument, NULL, 'T' }, |
| 81 | 112 |
| 82 { "extreme", no_argument, NULL, 'e' }, | 113 { "extreme", no_argument, NULL, 'e' }, |
| 83 { "fast", no_argument, NULL, '0' }, | 114 { "fast", no_argument, NULL, '0' }, |
| 84 { "best", no_argument, NULL, '9' }, | 115 { "best", no_argument, NULL, '9' }, |
| 85 | 116 |
| 86 // Filters | 117 // Filters |
| 87 { "lzma1", optional_argument, NULL, OPT_LZMA1 }, | 118 { "lzma1", optional_argument, NULL, OPT_LZMA1 }, |
| 88 { "lzma2", optional_argument, NULL, OPT_LZMA2 }, | 119 { "lzma2", optional_argument, NULL, OPT_LZMA2 }, |
| 89 { "x86", optional_argument, NULL, OPT_X86 }, | 120 { "x86", optional_argument, NULL, OPT_X86 }, |
| 90 { "powerpc", optional_argument, NULL, OPT_POWERPC }, | 121 { "powerpc", optional_argument, NULL, OPT_POWERPC }, |
| 91 { "ia64", optional_argument, NULL, OPT_IA64 }, | 122 { "ia64", optional_argument, NULL, OPT_IA64 }, |
| 92 { "arm", optional_argument, NULL, OPT_ARM }, | 123 { "arm", optional_argument, NULL, OPT_ARM }, |
| 93 { "armthumb", optional_argument, NULL, OPT_ARMTHUMB }, | 124 { "armthumb", optional_argument, NULL, OPT_ARMTHUMB }, |
| 94 { "sparc", optional_argument, NULL, OPT_SPARC }, | 125 { "sparc", optional_argument, NULL, OPT_SPARC }, |
| 95 { "delta", optional_argument, NULL, OPT_DELTA }, | 126 { "delta", optional_argument, NULL, OPT_DELTA }, |
| 96 | 127 |
| 97 // Other options | 128 // Other options |
| 98 { "quiet", no_argument, NULL, 'q' }, | 129 { "quiet", no_argument, NULL, 'q' }, |
| 99 { "verbose", no_argument, NULL, 'v' }, | 130 { "verbose", no_argument, NULL, 'v' }, |
| 100 { "no-warn", no_argument, NULL, 'Q' }, | 131 { "no-warn", no_argument, NULL, 'Q' }, |
| 101 { "robot", no_argument, NULL, OPT_ROBOT }, | 132 { "robot", no_argument, NULL, OPT_ROBOT }, |
| 102 { "info-memory", no_argument, NULL, OPT_INFO_MEMORY }, | 133 { "info-memory", no_argument, NULL, OPT_INFO_MEMORY }, |
| 103 { "help", no_argument, NULL, 'h' }, | 134 { "help", no_argument, NULL, 'h' }, |
| 104 { "long-help", no_argument, NULL, 'H' }, | 135 { "long-help", no_argument, NULL, 'H' }, |
| 105 { "version", no_argument, NULL, 'V' }, | 136 { "version", no_argument, NULL, 'V' }, |
| 106 | 137 |
| 107 » » { NULL, 0, NULL, 0 } | 138 » » { NULL, 0, NULL, 0 } |
| 108 }; | 139 }; |
| 109 | 140 |
| 110 int c; | 141 int c; |
| 111 | 142 |
| 112 while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) | 143 while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) |
| 113 != -1) { | 144 != -1) { |
| 114 switch (c) { | 145 switch (c) { |
| 115 // Compression preset (also for decompression if --format=raw) | 146 // Compression preset (also for decompression if --format=raw) |
| 116 case '0': case '1': case '2': case '3': case '4': | 147 case '0': case '1': case '2': case '3': case '4': |
| 117 case '5': case '6': case '7': case '8': case '9': | 148 case '5': case '6': case '7': case '8': case '9': |
| 118 coder_set_preset(c - '0'); | 149 coder_set_preset(c - '0'); |
| 119 break; | 150 break; |
| 120 | 151 |
| 121 » » // --memory | 152 » » // --memlimit-compress |
| 122 » » case 'M': { | 153 » » case OPT_MEM_COMPRESS: |
| 123 » » » // Support specifying the limit as a percentage of | 154 » » » parse_memlimit("memlimit-compress", |
| 124 » » » // installed physical RAM. | 155 » » » » » "memlimit-compress%", optarg, |
| 125 » » » size_t len = strlen(optarg); | 156 » » » » » true, false); |
| 126 » » » if (len > 0 && optarg[len - 1] == '%') { | 157 » » » break; |
| 127 » » » » optarg[len - 1] = '\0'; | |
| 128 » » » » hardware_memlimit_set_percentage( | |
| 129 » » » » » » str_to_uint64( | |
| 130 » » » » » » "memory%", optarg, 1, 100)); | |
| 131 » » » } else { | |
| 132 » » » » // On 32-bit systems, SIZE_MAX would make more | |
| 133 » » » » // sense than UINT64_MAX. But use UINT64_MAX | |
| 134 » » » » // still so that scripts that assume > 4 GiB | |
| 135 » » » » // values don't break. | |
| 136 » » » » hardware_memlimit_set(str_to_uint64( | |
| 137 » » » » » » "memory", optarg, | |
| 138 » » » » » » 0, UINT64_MAX)); | |
| 139 » » » } | |
| 140 | 158 |
| 159 // --memlimit-decompress |
| 160 case OPT_MEM_DECOMPRESS: |
| 161 parse_memlimit("memlimit-decompress", |
| 162 "memlimit-decompress%", optarg, |
| 163 false, true); |
| 141 break; | 164 break; |
| 142 » » } | 165 |
| 166 » » // --memlimit |
| 167 » » case 'M': |
| 168 » » » parse_memlimit("memlimit", "memlimit%", optarg, |
| 169 » » » » » true, true); |
| 170 » » » break; |
| 143 | 171 |
| 144 // --suffix | 172 // --suffix |
| 145 case 'S': | 173 case 'S': |
| 146 suffix_set(optarg); | 174 suffix_set(optarg); |
| 147 break; | 175 break; |
| 148 | 176 |
| 149 case 'T': | 177 case 'T': |
| 150 hardware_threadlimit_set(str_to_uint64( | 178 hardware_threadlimit_set(str_to_uint64( |
| 151 "threads", optarg, 0, UINT32_MAX)); | 179 "threads", optarg, 0, UINT32_MAX)); |
| 152 break; | 180 break; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 172 break; | 200 break; |
| 173 | 201 |
| 174 // --force | 202 // --force |
| 175 case 'f': | 203 case 'f': |
| 176 opt_force = true; | 204 opt_force = true; |
| 177 break; | 205 break; |
| 178 | 206 |
| 179 // --info-memory | 207 // --info-memory |
| 180 case OPT_INFO_MEMORY: | 208 case OPT_INFO_MEMORY: |
| 181 // This doesn't return. | 209 // This doesn't return. |
| 182 » » » message_memlimit(); | 210 » » » hardware_memlimit_show(); |
| 183 | 211 |
| 184 // --help | 212 // --help |
| 185 case 'h': | 213 case 'h': |
| 186 // This doesn't return. | 214 // This doesn't return. |
| 187 message_help(false); | 215 message_help(false); |
| 188 | 216 |
| 189 // --long-help | 217 // --long-help |
| 190 case 'H': | 218 case 'H': |
| 191 // This doesn't return. | 219 // This doesn't return. |
| 192 message_help(true); | 220 message_help(true); |
| (...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 377 message_try_help(); | 405 message_try_help(); |
| 378 tuklib_exit(E_ERROR, E_ERROR, false); | 406 tuklib_exit(E_ERROR, E_ERROR, false); |
| 379 } | 407 } |
| 380 } | 408 } |
| 381 | 409 |
| 382 return; | 410 return; |
| 383 } | 411 } |
| 384 | 412 |
| 385 | 413 |
| 386 static void | 414 static void |
| 387 parse_environment(args_info *args, char *argv0) | 415 parse_environment(args_info *args, char *argv0, const char *varname) |
| 388 { | 416 { |
| 389 » char *env = getenv("XZ_OPT"); | 417 » char *env = getenv(varname); |
| 390 if (env == NULL) | 418 if (env == NULL) |
| 391 return; | 419 return; |
| 392 | 420 |
| 393 // We modify the string, so make a copy of it. | 421 // We modify the string, so make a copy of it. |
| 394 env = xstrdup(env); | 422 env = xstrdup(env); |
| 395 | 423 |
| 396 // Calculate the number of arguments in env. argc stats at one | 424 // Calculate the number of arguments in env. argc stats at one |
| 397 // to include space for the program name. | 425 // to include space for the program name. |
| 398 int argc = 1; | 426 int argc = 1; |
| 399 bool prev_was_space = true; | 427 bool prev_was_space = true; |
| 400 for (size_t i = 0; env[i] != '\0'; ++i) { | 428 for (size_t i = 0; env[i] != '\0'; ++i) { |
| 401 // NOTE: Cast to unsigned char is needed so that correct | 429 // NOTE: Cast to unsigned char is needed so that correct |
| 402 // value gets passed to isspace(), which expects | 430 // value gets passed to isspace(), which expects |
| 403 // unsigned char cast to int. Casting to int is done | 431 // unsigned char cast to int. Casting to int is done |
| 404 // automatically due to integer promotion, but we need to | 432 // automatically due to integer promotion, but we need to |
| 405 // force char to unsigned char manually. Otherwise 8-bit | 433 // force char to unsigned char manually. Otherwise 8-bit |
| 406 // characters would get promoted to wrong value if | 434 // characters would get promoted to wrong value if |
| 407 // char is signed. | 435 // char is signed. |
| 408 if (isspace((unsigned char)env[i])) { | 436 if (isspace((unsigned char)env[i])) { |
| 409 prev_was_space = true; | 437 prev_was_space = true; |
| 410 } else if (prev_was_space) { | 438 } else if (prev_was_space) { |
| 411 prev_was_space = false; | 439 prev_was_space = false; |
| 412 | 440 |
| 413 // Keep argc small enough to fit into a singed int | 441 // Keep argc small enough to fit into a singed int |
| 414 // and to keep it usable for memory allocation. | 442 // and to keep it usable for memory allocation. |
| 415 if (++argc == my_min( | 443 if (++argc == my_min( |
| 416 INT_MAX, SIZE_MAX / sizeof(char *))) | 444 INT_MAX, SIZE_MAX / sizeof(char *))) |
| 417 message_fatal(_("The environment variable " | 445 message_fatal(_("The environment variable " |
| 418 » » » » » » "XZ_OPT contains too many " | 446 » » » » » » "%s contains too many " |
| 419 » » » » » » "arguments")); | 447 » » » » » » "arguments"), varname); |
| 420 } | 448 } |
| 421 } | 449 } |
| 422 | 450 |
| 423 // Allocate memory to hold pointers to the arguments. Add one to get | 451 // Allocate memory to hold pointers to the arguments. Add one to get |
| 424 // space for the terminating NULL (if some systems happen to need it). | 452 // space for the terminating NULL (if some systems happen to need it). |
| 425 char **argv = xmalloc(((size_t)(argc) + 1) * sizeof(char *)); | 453 char **argv = xmalloc(((size_t)(argc) + 1) * sizeof(char *)); |
| 426 argv[0] = argv0; | 454 argv[0] = argv0; |
| 427 argv[argc] = NULL; | 455 argv[argc] = NULL; |
| 428 | 456 |
| 429 // Go through the string again. Split the arguments using '\0' | 457 // Go through the string again. Split the arguments using '\0' |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 497 opt_mode = MODE_DECOMPRESS; | 525 opt_mode = MODE_DECOMPRESS; |
| 498 opt_stdout = true; | 526 opt_stdout = true; |
| 499 } else if (strstr(name, "unlzma") != NULL) { | 527 } else if (strstr(name, "unlzma") != NULL) { |
| 500 opt_format = FORMAT_LZMA; | 528 opt_format = FORMAT_LZMA; |
| 501 opt_mode = MODE_DECOMPRESS; | 529 opt_mode = MODE_DECOMPRESS; |
| 502 } else if (strstr(name, "lzma") != NULL) { | 530 } else if (strstr(name, "lzma") != NULL) { |
| 503 opt_format = FORMAT_LZMA; | 531 opt_format = FORMAT_LZMA; |
| 504 } | 532 } |
| 505 } | 533 } |
| 506 | 534 |
| 507 » // First the flags from environment | 535 » // First the flags from the environment |
| 508 » parse_environment(args, argv[0]); | 536 » parse_environment(args, argv[0], "XZ_DEFAULTS"); |
| 537 » parse_environment(args, argv[0], "XZ_OPT"); |
| 509 | 538 |
| 510 // Then from the command line | 539 // Then from the command line |
| 511 parse_real(args, argc, argv); | 540 parse_real(args, argc, argv); |
| 512 | 541 |
| 513 // Never remove the source file when the destination is not on disk. | 542 // Never remove the source file when the destination is not on disk. |
| 514 // In test mode the data is written nowhere, but setting opt_stdout | 543 // In test mode the data is written nowhere, but setting opt_stdout |
| 515 // will make the rest of the code behave well. | 544 // will make the rest of the code behave well. |
| 516 if (opt_stdout || opt_mode == MODE_TEST) { | 545 if (opt_stdout || opt_mode == MODE_TEST) { |
| 517 opt_keep_original = true; | 546 opt_keep_original = true; |
| 518 opt_stdout = true; | 547 opt_stdout = true; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 540 args->arg_count = 1; | 569 args->arg_count = 1; |
| 541 } else { | 570 } else { |
| 542 // We got at least one filename from the command line, or | 571 // We got at least one filename from the command line, or |
| 543 // --files or --files0 was specified. | 572 // --files or --files0 was specified. |
| 544 args->arg_names = argv + optind; | 573 args->arg_names = argv + optind; |
| 545 args->arg_count = argc - optind; | 574 args->arg_count = argc - optind; |
| 546 } | 575 } |
| 547 | 576 |
| 548 return; | 577 return; |
| 549 } | 578 } |
| OLD | NEW |