summaryrefslogtreecommitdiffstats
path: root/main.cc
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--main.cc62
1 files changed, 41 insertions, 21 deletions
diff --git a/main.cc b/main.cc
index 9e3774b..9425858 100644
--- a/main.cc
+++ b/main.cc
@@ -79,7 +79,7 @@ struct { const char * from; const char * to; } const known_extensions[] = {
{ 0, 0 } };
enum Mode { m_none, m_debug_delay, m_debug_repair, m_decompress, m_list,
- m_merge, m_range_dec, m_repair, m_split, m_test };
+ m_merge, m_range_dec, m_repair, m_show_packets, m_split, m_test };
std::string output_filename;
int outfd = -1;
@@ -106,7 +106,7 @@ void show_help()
" -V, --version output version information and exit\n"
" -c, --stdout send decompressed output to standard output\n"
" -d, --decompress decompress\n"
- " -D, --range-decompress=<range> decompress only a range of bytes (N-M)\n"
+ " -D, --range-decompress=<range> decompress a range of bytes (N-M) to stdout\n"
" -f, --force overwrite existing output files\n"
" -i, --ignore-errors make '--range-decompress' ignore data errors\n"
" -k, --keep keep (don't delete) input files\n"
@@ -120,8 +120,9 @@ void show_help()
" -v, --verbose be verbose (a 2nd -v gives more)\n" );
if( verbosity >= 1 )
{
- std::printf( " -y, --debug-delay=<range> find max error detection delay in <range>\n"
- " -z, --debug-repair=<pos>,<val> test repair one-byte error at <pos>\n" );
+ std::printf( " -x, --show-packets[=<pos>,<val>] show in stdout the decoded LZMA packets\n"
+ " -y, --debug-delay=<range> find max error detection delay in <range>\n"
+ " -z, --debug-repair=<pos>,<val> test repair one-byte error at <pos>\n" );
}
std::printf( "Numbers may be followed by a multiplier: k = kB = 10^3 = 1000,\n"
"Ki = KiB = 2^10 = 1024, M = 10^6, Mi = 2^20, G = 10^9, Gi = 2^30, etc...\n"
@@ -292,7 +293,7 @@ int open_instream( const char * const name, struct stat * const in_statsp,
if( infd < 0 )
{
if( verbosity >= 0 )
- std::fprintf( stderr, "%s: Can't open input file '%s': %s.\n",
+ std::fprintf( stderr, "%s: Can't open input file '%s': %s\n",
program_name, name, std::strerror( errno ) );
}
else
@@ -332,7 +333,7 @@ void set_d_outname( const std::string & name, const int i )
}
output_filename = name; output_filename += ".out";
if( verbosity >= 1 )
- std::fprintf( stderr, "%s: Can't guess original name for '%s' -- using '%s'.\n",
+ std::fprintf( stderr, "%s: Can't guess original name for '%s' -- using '%s'\n",
program_name, name.c_str(), output_filename.c_str() );
}
@@ -349,7 +350,7 @@ bool open_outstream( const bool force )
std::fprintf( stderr, "%s: Output file '%s' already exists, skipping.\n",
program_name, output_filename.c_str() );
else
- std::fprintf( stderr, "%s: Can't create output file '%s': %s.\n",
+ std::fprintf( stderr, "%s: Can't create output file '%s': %s\n",
program_name, output_filename.c_str(), std::strerror( errno ) );
}
return ( outfd >= 0 );
@@ -504,23 +505,20 @@ int decompress( const int infd, const Pretty_print & pp, const bool testing )
if( verbosity >= 0 && result <= 2 )
{
pp();
- if( result == 2 )
- std::fprintf( stderr, "File ends unexpectedly at pos %llu.\n",
- partial_file_pos );
- else
- std::fprintf( stderr, "Decoder error at pos %llu.\n",
- partial_file_pos );
+ std::fprintf( stderr, "%s at pos %llu\n", ( result == 2 ) ?
+ "File ends unexpectedly" : "Decoder error",
+ partial_file_pos );
}
retval = 2; break;
}
if( verbosity >= 2 )
- { std::fprintf( stderr, testing ? "ok\n" : "done\n" ); pp.reset(); }
+ { std::fputs( testing ? "ok\n" : "done\n", stderr ); pp.reset(); }
}
}
catch( std::bad_alloc ) { pp( "Not enough memory." ); retval = 1; }
catch( Error e ) { pp(); show_error( e.msg, errno ); retval = 1; }
if( verbosity == 1 && retval == 0 )
- std::fprintf( stderr, testing ? "ok\n" : "done\n" );
+ std::fputs( testing ? "ok\n" : "done\n", stderr );
return retval;
}
@@ -542,6 +540,21 @@ void set_signals()
} // end namespace
+bool file_exists( const std::string & filename )
+ {
+ struct stat st;
+ if( stat( filename.c_str(), &st ) == 0 )
+ {
+ if( verbosity >= 0 )
+ std::fprintf( stderr, "%s: Output file '%s' already exists."
+ " Use '--force' to overwrite it.\n",
+ program_name, filename.c_str() );
+ return true;
+ }
+ return false;
+ }
+
+
int open_outstream_rw( const std::string & output_filename, const bool force )
{
int flags = O_CREAT | O_RDWR | O_BINARY;
@@ -555,7 +568,7 @@ int open_outstream_rw( const std::string & output_filename, const bool force )
" Use '--force' to overwrite it.\n",
program_name, output_filename.c_str() );
else
- std::fprintf( stderr, "%s: Can't create output file '%s': %s.\n",
+ std::fprintf( stderr, "%s: Can't create output file '%s': %s\n",
program_name, output_filename.c_str(), std::strerror( errno ) );
}
return outfd;
@@ -570,8 +583,8 @@ void show_error( const char * const msg, const int errcode, const bool help )
{
std::fprintf( stderr, "%s: %s", program_name, msg );
if( errcode > 0 )
- std::fprintf( stderr, ": %s.", std::strerror( errcode ) );
- std::fprintf( stderr, "\n" );
+ std::fprintf( stderr, ": %s", std::strerror( errcode ) );
+ std::fputc( '\n', stderr );
}
if( help )
std::fprintf( stderr, "Try '%s --help' for more information.\n",
@@ -599,7 +612,7 @@ void show_error2( const char * const msg1, const char * const name,
int main( const int argc, const char * const argv[] )
{
Block range( 0, 0 );
- long long bad_pos = 0;
+ long long bad_pos = -1;
std::string input_filename;
std::string default_output_filename;
std::vector< std::string > filenames;
@@ -631,6 +644,7 @@ int main( const int argc, const char * const argv[] )
{ 't', "test", Arg_parser::no },
{ 'v', "verbose", Arg_parser::no },
{ 'V', "version", Arg_parser::no },
+ { 'x', "show-packets", Arg_parser::maybe },
{ 'y', "debug-delay", Arg_parser::yes },
{ 'z', "debug-repair", Arg_parser::yes },
{ 0 , 0, Arg_parser::no } };
@@ -665,6 +679,9 @@ int main( const int argc, const char * const argv[] )
case 't': set_mode( program_mode, m_test ); break;
case 'v': if( verbosity < 4 ) ++verbosity; break;
case 'V': show_version(); return 0;
+ case 'x': set_mode( program_mode, m_show_packets );
+ if( arg.size() )
+ parse_pos_value( arg.c_str(), bad_pos, bad_value ); break;
case 'y': set_mode( program_mode, m_debug_delay );
parse_range( arg.c_str(), range ); break;
case 'z': set_mode( program_mode, m_debug_repair );
@@ -720,8 +737,11 @@ int main( const int argc, const char * const argv[] )
one_file( filenames.size() );
if( default_output_filename.empty() )
default_output_filename = insert_fixed( filenames[0] );
- return repair_file( filenames[0], default_output_filename,
- verbosity, force );
+ return repair_file( filenames[0], default_output_filename, verbosity,
+ force );
+ case m_show_packets:
+ one_file( filenames.size() );
+ return debug_show_packets( filenames[0], bad_pos, verbosity, bad_value );
case m_split:
one_file( filenames.size() );
return split_file( filenames[0], default_output_filename, verbosity, force );