2007-12-08 23:42:33 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
/// \file main.c
|
|
|
|
/// \brief main()
|
|
|
|
//
|
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 <ctype.h>
|
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
/// Exit status to use. This can be changed with set_exit_status().
|
|
|
|
static enum exit_status_type exit_status = E_SUCCESS;
|
|
|
|
|
2010-06-02 20:32:12 +02:00
|
|
|
#if defined(_WIN32) && !defined(__CYGWIN__)
|
|
|
|
/// exit_status has to be protected with a critical section due to
|
|
|
|
/// how "signal handling" is done on Windows. See signals.c for details.
|
|
|
|
static CRITICAL_SECTION exit_status_cs;
|
|
|
|
#endif
|
|
|
|
|
2009-05-22 13:43:00 +02:00
|
|
|
/// True if --no-warn is specified. When this is true, we don't set
|
|
|
|
/// the exit status to E_WARNING when something worth a warning happens.
|
|
|
|
static bool no_warn = false;
|
|
|
|
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
extern void
|
|
|
|
set_exit_status(enum exit_status_type new_status)
|
2007-12-08 23:42:33 +01:00
|
|
|
{
|
2008-11-19 19:46:52 +01:00
|
|
|
assert(new_status == E_WARNING || new_status == E_ERROR);
|
|
|
|
|
2010-06-02 20:32:12 +02:00
|
|
|
#if defined(_WIN32) && !defined(__CYGWIN__)
|
|
|
|
EnterCriticalSection(&exit_status_cs);
|
|
|
|
#endif
|
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
if (exit_status != E_ERROR)
|
|
|
|
exit_status = new_status;
|
|
|
|
|
2010-06-02 20:32:12 +02:00
|
|
|
#if defined(_WIN32) && !defined(__CYGWIN__)
|
|
|
|
LeaveCriticalSection(&exit_status_cs);
|
|
|
|
#endif
|
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-22 13:43:00 +02:00
|
|
|
extern void
|
|
|
|
set_exit_no_warn(void)
|
|
|
|
{
|
|
|
|
no_warn = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
static const char *
|
|
|
|
read_name(const args_info *args)
|
|
|
|
{
|
|
|
|
// FIXME: Maybe we should have some kind of memory usage limit here
|
2010-02-12 12:16:15 +01:00
|
|
|
// like the tool has for the actual compression and decompression.
|
2008-11-19 19:46:52 +01:00
|
|
|
// Giving some huge text file with --files0 makes us to read the
|
|
|
|
// whole file in RAM.
|
|
|
|
static char *name = NULL;
|
|
|
|
static size_t size = 256;
|
|
|
|
|
|
|
|
// Allocate the initial buffer. This is never freed, since after it
|
|
|
|
// is no longer needed, the program exits very soon. It is safe to
|
|
|
|
// use xmalloc() and xrealloc() in this function, because while
|
|
|
|
// executing this function, no files are open for writing, and thus
|
|
|
|
// there's no need to cleanup anything before exiting.
|
|
|
|
if (name == NULL)
|
|
|
|
name = xmalloc(size);
|
|
|
|
|
|
|
|
// Write position in name
|
|
|
|
size_t pos = 0;
|
|
|
|
|
|
|
|
// Read one character at a time into name.
|
|
|
|
while (!user_abort) {
|
|
|
|
const int c = fgetc(args->files_file);
|
|
|
|
|
|
|
|
if (ferror(args->files_file)) {
|
|
|
|
// Take care of EINTR since we have established
|
|
|
|
// the signal handlers already.
|
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
message_error(_("%s: Error reading filenames: %s"),
|
|
|
|
args->files_name, strerror(errno));
|
2007-12-08 23:42:33 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
if (feof(args->files_file)) {
|
|
|
|
if (pos != 0)
|
|
|
|
message_error(_("%s: Unexpected end of input "
|
|
|
|
"when reading filenames"),
|
|
|
|
args->files_name);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c == args->files_delim) {
|
|
|
|
// We allow consecutive newline (--files) or '\0'
|
|
|
|
// characters (--files0), and ignore such empty
|
|
|
|
// filenames.
|
|
|
|
if (pos == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// A non-empty name was read. Terminate it with '\0'
|
|
|
|
// and return it.
|
|
|
|
name[pos] = '\0';
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c == '\0') {
|
|
|
|
// A null character was found when using --files,
|
|
|
|
// which expects plain text input separated with
|
|
|
|
// newlines.
|
|
|
|
message_error(_("%s: Null character found when "
|
|
|
|
"reading filenames; maybe you meant "
|
|
|
|
"to use `--files0' instead "
|
|
|
|
"of `--files'?"), args->files_name);
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-12-08 23:42:33 +01:00
|
|
|
|
|
|
|
name[pos++] = c;
|
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
// Allocate more memory if needed. There must always be space
|
|
|
|
// at least for one character to allow terminating the string
|
|
|
|
// with '\0'.
|
2007-12-08 23:42:33 +01:00
|
|
|
if (pos == size) {
|
|
|
|
size *= 2;
|
2008-11-19 19:46:52 +01:00
|
|
|
name = xrealloc(name, size);
|
2007-12-08 23:42:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
return NULL;
|
2007-12-08 23:42:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
2010-06-02 20:32:12 +02:00
|
|
|
#if defined(_WIN32) && !defined(__CYGWIN__)
|
|
|
|
InitializeCriticalSection(&exit_status_cs);
|
|
|
|
#endif
|
|
|
|
|
2009-09-19 08:47:30 +02:00
|
|
|
// Set up the progname variable.
|
|
|
|
tuklib_progname_init(argv);
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2009-09-19 08:47:30 +02:00
|
|
|
// Initialize the file I/O. This makes sure that
|
|
|
|
// stdin, stdout, and stderr are something valid.
|
|
|
|
io_init();
|
2008-11-19 19:46:52 +01:00
|
|
|
|
2009-09-19 08:47:30 +02:00
|
|
|
// Set up the locale and message translations.
|
|
|
|
tuklib_gettext_init(PACKAGE, LOCALEDIR);
|
2008-11-19 19:46:52 +01:00
|
|
|
|
2009-09-19 08:47:30 +02:00
|
|
|
// Initialize handling of error/warning/other messages.
|
|
|
|
message_init();
|
2007-12-08 23:42:33 +01:00
|
|
|
|
|
|
|
// Set hardware-dependent default values. These can be overriden
|
2010-01-26 13:46:43 +01:00
|
|
|
// on the command line, thus this must be done before args_parse().
|
2007-12-08 23:42:33 +01:00
|
|
|
hardware_init();
|
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
// Parse the command line arguments and get an array of filenames.
|
|
|
|
// This doesn't return if something is wrong with the command line
|
|
|
|
// arguments. If there are no arguments, one filename ("-") is still
|
|
|
|
// returned to indicate stdin.
|
|
|
|
args_info args;
|
|
|
|
args_parse(&args, argc, argv);
|
|
|
|
|
2010-01-24 22:50:54 +01:00
|
|
|
if (opt_mode != MODE_LIST && opt_robot)
|
2009-11-16 17:16:45 +01:00
|
|
|
message_fatal(_("Compression and decompression with --robot "
|
|
|
|
"are not supported yet."));
|
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
// Tell the message handling code how many input files there are if
|
|
|
|
// we know it. This way the progress indicator can show it.
|
|
|
|
if (args.files_name != NULL)
|
|
|
|
message_set_files(0);
|
2007-12-08 23:42:33 +01:00
|
|
|
else
|
2008-11-19 19:46:52 +01:00
|
|
|
message_set_files(args.arg_count);
|
|
|
|
|
|
|
|
// Refuse to write compressed data to standard output if it is
|
2010-01-13 18:10:25 +01:00
|
|
|
// a terminal.
|
|
|
|
if (opt_mode == MODE_COMPRESS) {
|
2008-11-19 19:46:52 +01:00
|
|
|
if (opt_stdout || (args.arg_count == 1
|
|
|
|
&& strcmp(args.arg_names[0], "-") == 0)) {
|
|
|
|
if (is_tty_stdout()) {
|
|
|
|
message_try_help();
|
2009-09-19 08:47:30 +02:00
|
|
|
tuklib_exit(E_ERROR, E_ERROR, false);
|
2008-11-19 19:46:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2010-02-01 21:46:56 +01:00
|
|
|
// Set up the signal handlers. We don't need these before we
|
|
|
|
// start the actual action and not in --list mode, so this is
|
|
|
|
// done after parsing the command line arguments.
|
|
|
|
//
|
|
|
|
// It's good to keep signal handlers in normal compression and
|
|
|
|
// decompression modes even when only writing to stdout, because
|
|
|
|
// we might need to restore O_APPEND flag on stdout before exiting.
|
|
|
|
// In --test mode, signal handlers aren't really needed, but let's
|
|
|
|
// keep them there for consistency with normal decompression.
|
|
|
|
if (opt_mode != MODE_LIST)
|
|
|
|
signals_init();
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2015-03-31 21:19:34 +02:00
|
|
|
#ifdef ENABLE_SANDBOX
|
|
|
|
// Set a flag that sandboxing is allowed if all these are true:
|
|
|
|
// - --files or --files0 wasn't used.
|
|
|
|
// - There is exactly one input file or we are reading from stdin.
|
|
|
|
// - We won't create any files: output goes to stdout or --test
|
|
|
|
// or --list was used. Note that --test implies opt_stdout = true
|
|
|
|
// but --list doesn't.
|
|
|
|
//
|
|
|
|
// This is obviously not ideal but it was easy to implement and
|
|
|
|
// it covers the most common use cases.
|
|
|
|
//
|
|
|
|
// TODO: Make sandboxing work for other situations too.
|
|
|
|
if (args.files_name == NULL && args.arg_count == 1
|
|
|
|
&& (opt_stdout || strcmp("-", args.arg_names[0]) == 0
|
|
|
|
|| opt_mode == MODE_LIST))
|
|
|
|
io_allow_sandbox();
|
|
|
|
#endif
|
|
|
|
|
2010-02-12 12:16:15 +01:00
|
|
|
// coder_run() handles compression, decompression, and testing.
|
2010-01-24 22:50:54 +01:00
|
|
|
// list_file() is for --list.
|
|
|
|
void (*run)(const char *filename) = opt_mode == MODE_LIST
|
|
|
|
? &list_file : &coder_run;
|
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
// Process the files given on the command line. Note that if no names
|
2010-01-26 13:46:43 +01:00
|
|
|
// were given, args_parse() gave us a fake "-" filename.
|
2015-03-07 18:54:00 +01:00
|
|
|
for (unsigned i = 0; i < args.arg_count && !user_abort; ++i) {
|
2008-11-19 19:46:52 +01:00
|
|
|
if (strcmp("-", args.arg_names[i]) == 0) {
|
2010-01-13 18:10:25 +01:00
|
|
|
// Processing from stdin to stdout. Check that we
|
|
|
|
// aren't writing compressed data to a terminal or
|
|
|
|
// reading it from a terminal.
|
|
|
|
if (opt_mode == MODE_COMPRESS) {
|
|
|
|
if (is_tty_stdout())
|
2007-12-08 23:42:33 +01:00
|
|
|
continue;
|
2010-01-13 18:10:25 +01:00
|
|
|
} else if (is_tty_stdin()) {
|
|
|
|
continue;
|
2007-12-08 23:42:33 +01:00
|
|
|
}
|
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
// It doesn't make sense to compress data from stdin
|
|
|
|
// if we are supposed to read filenames from stdin
|
|
|
|
// too (enabled with --files or --files0).
|
|
|
|
if (args.files_name == stdin_filename) {
|
|
|
|
message_error(_("Cannot read data from "
|
2007-12-08 23:42:33 +01:00
|
|
|
"standard input when "
|
|
|
|
"reading filenames "
|
|
|
|
"from standard input"));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
// Replace the "-" with a special pointer, which is
|
2009-06-26 19:43:36 +02:00
|
|
|
// recognized by coder_run() and other things.
|
2008-11-19 19:46:52 +01:00
|
|
|
// This way error messages get a proper filename
|
|
|
|
// string and the code still knows that it is
|
|
|
|
// handling the special case of stdin.
|
|
|
|
args.arg_names[i] = (char *)stdin_filename;
|
2007-12-08 23:42:33 +01:00
|
|
|
}
|
|
|
|
|
2010-02-12 12:16:15 +01:00
|
|
|
// Do the actual compression or decompression.
|
2010-01-24 22:50:54 +01:00
|
|
|
run(args.arg_names[i]);
|
2007-12-08 23:42:33 +01:00
|
|
|
}
|
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
// If --files or --files0 was used, process the filenames from the
|
|
|
|
// given file or stdin. Note that here we don't consider "-" to
|
|
|
|
// indicate stdin like we do with the command line arguments.
|
|
|
|
if (args.files_name != NULL) {
|
|
|
|
// read_name() checks for user_abort so we don't need to
|
|
|
|
// check it as loop termination condition.
|
2007-12-08 23:42:33 +01:00
|
|
|
while (true) {
|
2008-11-19 19:46:52 +01:00
|
|
|
const char *name = read_name(&args);
|
2007-12-08 23:42:33 +01:00
|
|
|
if (name == NULL)
|
|
|
|
break;
|
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
// read_name() doesn't return empty names.
|
|
|
|
assert(name[0] != '\0');
|
2010-01-24 22:50:54 +01:00
|
|
|
run(name);
|
2007-12-08 23:42:33 +01:00
|
|
|
}
|
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
if (args.files_name != stdin_filename)
|
|
|
|
(void)fclose(args.files_file);
|
2007-12-08 23:42:33 +01:00
|
|
|
}
|
|
|
|
|
2010-01-24 22:50:54 +01:00
|
|
|
// All files have now been handled. If in --list mode, display
|
2010-02-01 21:46:56 +01:00
|
|
|
// the totals before exiting. We don't have signal handlers
|
|
|
|
// enabled in --list mode, so we don't need to check user_abort.
|
2010-02-02 10:50:11 +01:00
|
|
|
if (opt_mode == MODE_LIST) {
|
|
|
|
assert(!user_abort);
|
2010-01-24 22:50:54 +01:00
|
|
|
list_totals();
|
2010-02-02 10:50:11 +01:00
|
|
|
}
|
2010-01-24 22:50:54 +01:00
|
|
|
|
2011-04-05 14:13:29 +02:00
|
|
|
#ifndef NDEBUG
|
|
|
|
coder_free();
|
2012-07-03 20:16:39 +02:00
|
|
|
args_free();
|
2011-04-05 14:13:29 +02:00
|
|
|
#endif
|
|
|
|
|
2009-09-19 08:47:30 +02:00
|
|
|
// If we have got a signal, raise it to kill the program instead
|
|
|
|
// of calling tuklib_exit().
|
|
|
|
signals_exit();
|
|
|
|
|
2010-06-02 20:32:12 +02:00
|
|
|
// Make a local copy of exit_status to keep the Windows code
|
|
|
|
// thread safe. At this point it is fine if we miss the user
|
|
|
|
// pressing C-c and don't set the exit_status to E_ERROR on
|
|
|
|
// Windows.
|
|
|
|
#if defined(_WIN32) && !defined(__CYGWIN__)
|
|
|
|
EnterCriticalSection(&exit_status_cs);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
enum exit_status_type es = exit_status;
|
|
|
|
|
|
|
|
#if defined(_WIN32) && !defined(__CYGWIN__)
|
|
|
|
LeaveCriticalSection(&exit_status_cs);
|
|
|
|
#endif
|
|
|
|
|
2009-09-19 08:47:30 +02:00
|
|
|
// Suppress the exit status indicating a warning if --no-warn
|
|
|
|
// was specified.
|
2010-06-02 20:32:12 +02:00
|
|
|
if (es == E_WARNING && no_warn)
|
|
|
|
es = E_SUCCESS;
|
2009-09-19 08:47:30 +02:00
|
|
|
|
2010-06-02 20:32:12 +02:00
|
|
|
tuklib_exit(es, E_ERROR, message_verbosity_get() != V_SILENT);
|
2007-12-08 23:42:33 +01:00
|
|
|
}
|