summaryrefslogtreecommitdiffstats
path: root/main.cc
diff options
context:
space:
mode:
authorDaniel Baumann <mail@daniel-baumann.ch>2015-11-08 04:26:51 +0000
committerDaniel Baumann <mail@daniel-baumann.ch>2015-11-08 04:26:51 +0000
commitabdd6f5dd4e9b01f68646858e3abc58325436be3 (patch)
tree7d78c505a87aed973c3154a810d57feb259b69a6 /main.cc
parentAdding upstream version 1.0. (diff)
downloadzutils-upstream/1.1_rc2.tar.xz
zutils-upstream/1.1_rc2.zip
Adding upstream version 1.1~rc2.upstream/1.1_rc2
Signed-off-by: Daniel Baumann <mail@daniel-baumann.ch>
Diffstat (limited to '')
-rw-r--r--main.cc50
1 files changed, 37 insertions, 13 deletions
diff --git a/main.cc b/main.cc
index f0a66e1..672d159 100644
--- a/main.cc
+++ b/main.cc
@@ -37,6 +37,7 @@
#include "arg_parser.h"
#include "zutils.h"
+#include "rc.h"
#if CHAR_BIT != 8
#error "Environments where CHAR_BIT != 8 are not supported."
@@ -57,8 +58,8 @@ enum Mode { m_none, m_zcat, m_zgrep, m_ztest };
void show_help()
{
std::printf( "Zutils is a collection of utilities able to deal with any combination of\n"
- "compressed and non-compressed files transparently. If any given file,\n"
- "including standard input, is compressed, its uncompressed content is used.\n"
+ "compressed and uncompressed files transparently. If any given file,\n"
+ "including standard input, is compressed, its decompressed content is used.\n"
"\nThe supported formats are bzip2, gzip, lzip and xz.\n"
"\nUsage: %s <operation> [options] [files]\n", invocation_name );
std::printf( "\nTry '%s <operation> --help' for more specific help.\n", invocation_name );
@@ -118,10 +119,11 @@ int open_instream( std::string & input_filename, const Mode program_mode,
int main( const int argc, const char * const argv[] )
{
enum { format_opt = 256, help_opt, verbose_opt,
+ bz2_opt, gz_opt, lz_opt, xz_opt,
zcat_opt, zgrep_opt, ztest_opt };
const Arg_parser::Option * options = 0;
int infd = -1;
- int format_type = -1;
+ int format_index = -1;
Mode program_mode = m_none;
bool recursive = false;
std::string input_filename;
@@ -144,6 +146,7 @@ int main( const int argc, const char * const argv[] )
{ 'l', "list", Arg_parser::no }, // gzip
{ 'L', "license", Arg_parser::no }, // gzip
{ 'n', "number", Arg_parser::no }, // cat
+ { 'N', "no-rcfile", Arg_parser::no },
{ 'q', "quiet", Arg_parser::no },
{ 'r', "recursive", Arg_parser::no },
{ 's', "squeeze-blank", Arg_parser::no }, // cat
@@ -153,6 +156,10 @@ int main( const int argc, const char * const argv[] )
{ 'V', "version", Arg_parser::no },
{ format_opt, "format", Arg_parser::yes },
{ verbose_opt, "verbose", Arg_parser::no },
+ { bz2_opt, "bz2", Arg_parser::yes },
+ { gz_opt, "gz", Arg_parser::yes },
+ { lz_opt, "lz", Arg_parser::yes },
+ { xz_opt, "xz", Arg_parser::yes },
{ zcat_opt, "zcat", Arg_parser::no },
{ 0 , 0, Arg_parser::no } };
@@ -176,6 +183,7 @@ int main( const int argc, const char * const argv[] )
{ 'L', "files-without-match", Arg_parser::no }, // grep GNU
{ 'm', "max-count", Arg_parser::yes }, // grep GNU
{ 'n', "line-number", Arg_parser::no }, // grep
+ { 'N', "no-rcfile", Arg_parser::no },
{ 'o', "only-matching", Arg_parser::no }, // grep
{ 'q', "quiet", Arg_parser::no },
{ 'r', "recursive", Arg_parser::no },
@@ -187,17 +195,26 @@ int main( const int argc, const char * const argv[] )
{ format_opt, "format", Arg_parser::yes },
{ help_opt, "help", Arg_parser::no },
{ verbose_opt, "verbose", Arg_parser::no },
+ { bz2_opt, "bz2", Arg_parser::yes },
+ { gz_opt, "gz", Arg_parser::yes },
+ { lz_opt, "lz", Arg_parser::yes },
+ { xz_opt, "xz", Arg_parser::yes },
{ zgrep_opt, "zgrep", Arg_parser::no },
{ 0 , 0, Arg_parser::no } };
const Arg_parser::Option m_ztest_options[] =
{
{ 'h', "help", Arg_parser::no },
+ { 'N', "no-rcfile", Arg_parser::no },
{ 'q', "quiet", Arg_parser::no },
{ 'r', "recursive", Arg_parser::no },
{ 'v', "verbose", Arg_parser::no },
{ 'V', "version", Arg_parser::no },
{ format_opt, "format", Arg_parser::yes },
+ { bz2_opt, "bz2", Arg_parser::yes },
+ { gz_opt, "gz", Arg_parser::yes },
+ { lz_opt, "lz", Arg_parser::yes },
+ { xz_opt, "xz", Arg_parser::yes },
{ ztest_opt, "ztest", Arg_parser::no },
{ 0 , 0, Arg_parser::no } };
@@ -244,10 +261,12 @@ int main( const int argc, const char * const argv[] )
}
} // end parse operation
+ const int eretval = ( program_mode == m_zgrep ) ? 2 : 1;
const Arg_parser parser( argc, argv, options );
if( parser.error().size() ) // bad option
- { show_error( parser.error().c_str(), 0, true );
- return ( program_mode == m_zcat || program_mode == m_ztest ) ? 1 : 2; }
+ { show_error( parser.error().c_str(), 0, true ); return eretval; }
+
+ maybe_process_config_file( parser );
int argind = 0;
int grep_show_name = -1;
@@ -263,9 +282,14 @@ int main( const int argc, const char * const argv[] )
{ grep_args.push_back( arg ); grep_pattern_found = true; continue; }
else break; // no more options
}
- if( code == format_opt )
+ switch( code ) // common options
{
- format_type = get_format_type( arg ); continue;
+ case 'N': continue;
+ case format_opt: format_index = parse_format_type( arg ); continue;
+ case bz2_opt: parse_compressor( arg, fmt_bz2, eretval ); continue;
+ case gz_opt: parse_compressor( arg, fmt_gz, eretval ); continue;
+ case lz_opt: parse_compressor( arg, fmt_lz, eretval ); continue;
+ case xz_opt: parse_compressor( arg, fmt_xz, eretval ); continue;
}
switch( program_mode )
{
@@ -396,7 +420,7 @@ int main( const int argc, const char * const argv[] )
continue;
}
}
- infd = open_instream( input_filename, program_mode, format_type < 0 );
+ infd = open_instream( input_filename, program_mode, format_index < 0 );
if( infd < 0 ) { if( retval < 1 ) retval = 1; continue; }
}
@@ -406,18 +430,18 @@ int main( const int argc, const char * const argv[] )
case m_none:
break;
case m_zcat:
- tmp = cat( infd, format_type, input_filename, cat_options );
+ tmp = cat( infd, format_index, input_filename, cat_options );
break;
case m_zgrep:
if( infd == STDIN_FILENO )
- tmp = zgrep_stdin( infd, format_type, grep_args );
- else tmp = zgrep_file( infd, format_type, input_filename, grep_args,
+ tmp = zgrep_stdin( infd, format_index, grep_args );
+ else tmp = zgrep_file( infd, format_index, input_filename, grep_args,
grep_list_mode, grep_show_name );
break;
case m_ztest:
if( infd == STDIN_FILENO )
- tmp = ztest_stdin( infd, format_type, ztest_args );
- else tmp = ztest_file( infd, format_type, input_filename, ztest_args );
+ tmp = ztest_stdin( infd, format_index, ztest_args );
+ else tmp = ztest_file( infd, format_index, input_filename, ztest_args );
break;
}
if( program_mode == m_zgrep )