Added support for --quiet and --no-warn to xzdec.

Cleaned up the --help message a little.
This commit is contained in:
Lasse Collin 2009-05-22 16:40:50 +03:00
parent 5f735dae80
commit dcedb6998c
1 changed files with 49 additions and 27 deletions

View File

@ -13,6 +13,7 @@
#include "sysdefs.h" #include "sysdefs.h"
#include "lzma.h" #include "lzma.h"
#include <stdarg.h>
#include <errno.h> #include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
@ -36,10 +37,31 @@
/// Number of bytes to use memory at maximum /// Number of bytes to use memory at maximum
static uint64_t memlimit; static uint64_t memlimit;
/// Error messages are suppressed if this is zero, which is the case when
/// --quiet has been given at least twice.
static unsigned int display_errors = 2;
/// Program name to be shown in error messages /// Program name to be shown in error messages
static const char *argv0; static const char *argv0;
static void lzma_attribute((format(printf, 1, 2)))
my_errorf(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
if (display_errors) {
fprintf(stderr, "%s: ", argv0);
vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n");
}
va_end(ap);
return;
}
static void lzma_attribute((noreturn)) static void lzma_attribute((noreturn))
my_exit(void) my_exit(void)
{ {
@ -54,9 +76,8 @@ my_exit(void)
// If it was fclose() that failed, we have the reason // If it was fclose() that failed, we have the reason
// in errno. If only ferror() indicated an error, // in errno. If only ferror() indicated an error,
// we have no idea what the reason was. // we have no idea what the reason was.
fprintf(stderr, "%s: Cannot write to standard output: %s\n", my_errorf("Cannot write to standard output: %s", fclose_err
argv0, fclose_err ? strerror(errno) : "Unknown error");
? strerror(errno) : "Unknown error");
status = EXIT_FAILURE; status = EXIT_FAILURE;
} }
@ -77,15 +98,18 @@ help(void)
" -f, --force (ignored)\n" " -f, --force (ignored)\n"
" -M, --memory=NUM use NUM bytes of memory at maximum (0 means default);\n" " -M, --memory=NUM use NUM bytes of memory at maximum (0 means default);\n"
" the suffixes k, M, G, Ki, Mi, and Gi are supported.\n" " the suffixes k, M, G, Ki, Mi, and Gi are supported.\n"
" -q, --quiet specify *twice* to suppress errors\n"
" -Q, --no-warn (ignored)\n"
" -h, --help display this help and exit\n" " -h, --help display this help and exit\n"
" -V, --version display version and license information and exit\n" " -V, --version display the version number and exit\n"
"\n" "\n"
"With no FILE, or when FILE is -, read standard input.\n" "With no FILE, or when FILE is -, read standard input.\n"
"\n" "\n"
"On this system and configuration, this program will use at maximum of roughly\n" "On this system and configuration, this program will use at maximum of roughly\n"
"%" PRIu64 " MiB RAM.\n" "%" PRIu64 " MiB RAM.\n"
"\n" "\n"
"Report bugs to <" PACKAGE_BUGREPORT "> (in English or Finnish).\n", "Report bugs to <" PACKAGE_BUGREPORT "> (in English or Finnish).\n"
"XZ Utils home page: <http://tukaani.org/xz/>\n",
argv0, memlimit / (1024 * 1024)); argv0, memlimit / (1024 * 1024));
my_exit(); my_exit();
} }
@ -133,7 +157,7 @@ str_to_uint64(const char *value)
return UINT64_MAX; return UINT64_MAX;
if (*value < '0' || *value > '9') { if (*value < '0' || *value > '9') {
fprintf(stderr, "%s: %s: Not a number\n", argv0, value); my_errorf("%s: Not a number", value);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -176,8 +200,7 @@ str_to_uint64(const char *value)
} }
if (multiplier == 0) { if (multiplier == 0) {
fprintf(stderr, "%s: %s: Invalid suffix\n", my_errorf("%s: Invalid suffix", value);
argv0, value);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -196,7 +219,7 @@ str_to_uint64(const char *value)
static void static void
parse_options(int argc, char **argv) parse_options(int argc, char **argv)
{ {
static const char short_opts[] = "cdkfM:hV"; static const char short_opts[] = "cdkfM:hqQV";
static const struct option long_opts[] = { static const struct option long_opts[] = {
{ "stdout", no_argument, NULL, 'c' }, { "stdout", no_argument, NULL, 'c' },
{ "to-stdout", no_argument, NULL, 'c' }, { "to-stdout", no_argument, NULL, 'c' },
@ -205,6 +228,8 @@ parse_options(int argc, char **argv)
{ "force", no_argument, NULL, 'f' }, { "force", no_argument, NULL, 'f' },
{ "keep", no_argument, NULL, 'k' }, { "keep", no_argument, NULL, 'k' },
{ "memory", required_argument, NULL, 'M' }, { "memory", required_argument, NULL, 'M' },
{ "quiet", no_argument, NULL, 'q' },
{ "no-warn", no_argument, NULL, 'Q' },
{ "help", no_argument, NULL, 'h' }, { "help", no_argument, NULL, 'h' },
{ "version", no_argument, NULL, 'V' }, { "version", no_argument, NULL, 'V' },
{ NULL, 0, NULL, 0 } { NULL, 0, NULL, 0 }
@ -219,6 +244,7 @@ parse_options(int argc, char **argv)
case 'd': case 'd':
case 'f': case 'f':
case 'k': case 'k':
case 'Q':
break; break;
case 'M': case 'M':
@ -228,6 +254,12 @@ parse_options(int argc, char **argv)
break; break;
case 'q':
if (display_errors > 0)
--display_errors;
break;
case 'h': case 'h':
help(); help();
@ -258,13 +290,8 @@ uncompress(lzma_stream *strm, FILE *file, const char *filename)
// The only reasonable error here is LZMA_MEM_ERROR. // The only reasonable error here is LZMA_MEM_ERROR.
// FIXME: Maybe also LZMA_MEMLIMIT_ERROR in future? // FIXME: Maybe also LZMA_MEMLIMIT_ERROR in future?
if (ret != LZMA_OK) { if (ret != LZMA_OK) {
fprintf(stderr, "%s: ", argv0); my_errorf("%s", ret == LZMA_MEM_ERROR ? strerror(ENOMEM)
: "Internal program error (bug)");
if (ret == LZMA_MEM_ERROR)
fprintf(stderr, "%s\n", strerror(ENOMEM));
else
fprintf(stderr, "Internal program error (bug)\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -287,10 +314,8 @@ uncompress(lzma_stream *strm, FILE *file, const char *filename)
// POSIX says that fread() sets errno if // POSIX says that fread() sets errno if
// an error occurred. ferror() doesn't // an error occurred. ferror() doesn't
// touch errno. // touch errno.
fprintf(stderr, "%s: %s: Error reading " my_errorf("%s: Error reading input file: %s",
"input file: %s\n", filename, strerror(errno));
argv0, filename,
strerror(errno));
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -315,9 +340,8 @@ uncompress(lzma_stream *strm, FILE *file, const char *filename)
// Wouldn't be a surprise if writing to stderr // Wouldn't be a surprise if writing to stderr
// would fail too but at least try to show an // would fail too but at least try to show an
// error message. // error message.
fprintf(stderr, "%s: Cannot write to " my_errorf("Cannot write to standard output: "
"standard output: %s\n", argv0, "%s", strerror(errno));
strerror(errno));
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -378,8 +402,7 @@ uncompress(lzma_stream *strm, FILE *file, const char *filename)
break; break;
} }
fprintf(stderr, "%s: %s: %s\n", argv0, filename, msg); my_errorf("%s: %s", filename, msg);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
} }
@ -423,8 +446,7 @@ main(int argc, char **argv)
} else { } else {
FILE *file = fopen(argv[optind], "rb"); FILE *file = fopen(argv[optind], "rb");
if (file == NULL) { if (file == NULL) {
fprintf(stderr, "%s: %s: %s\n", my_errorf("%s: %s", argv[optind],
argv0, argv[optind],
strerror(errno)); strerror(errno));
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }