summaryrefslogtreecommitdiffstats
path: root/zgrep.cc
diff options
context:
space:
mode:
Diffstat (limited to 'zgrep.cc')
-rw-r--r--zgrep.cc247
1 files changed, 240 insertions, 7 deletions
diff --git a/zgrep.cc b/zgrep.cc
index 0c78504..47f5983 100644
--- a/zgrep.cc
+++ b/zgrep.cc
@@ -15,7 +15,40 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-void show_zgrep_help()
+#define _FILE_OFFSET_BITS 64
+
+#include <cerrno>
+#include <climits>
+#include <csignal>
+#include <cstdio>
+#include <cstdlib>
+#include <cstring>
+#include <list>
+#include <string>
+#include <vector>
+#include <dirent.h>
+#include <fcntl.h>
+#include <stdint.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#if defined(__MSVCRT__) || defined(__OS2__)
+#include <io.h>
+#endif
+
+#include "arg_parser.h"
+#include "rc.h"
+#include "zutils.h"
+
+#ifndef O_BINARY
+#define O_BINARY 0
+#endif
+
+
+namespace {
+
+#include "recursive.cc"
+
+void show_help()
{
std::printf( "Zgrep is a front end to the grep program that allows transparent search\n"
"on any combination of compressed and uncompressed files. If any given\n"
@@ -68,6 +101,42 @@ void show_zgrep_help()
}
+int simple_extension_index( const std::string & name )
+ {
+ for( int i = 0; i < num_formats; ++i )
+ {
+ const std::string ext( simple_extensions[i] );
+ if( name.size() > ext.size() &&
+ name.compare( name.size() - ext.size(), ext.size(), ext ) == 0 )
+ return i;
+ }
+ return -1;
+ }
+
+
+int open_instream( std::string & input_filename,
+ const bool no_messages, const bool search )
+ {
+ int infd = open( input_filename.c_str(), O_RDONLY | O_BINARY );
+ if( infd < 0 )
+ {
+ if( search && simple_extension_index( input_filename ) < 0 )
+ {
+ for( int i = 0; i < num_formats; ++i )
+ {
+ const std::string name( input_filename +
+ simple_extensions[format_order[i]] );
+ infd = open( name.c_str(), O_RDONLY | O_BINARY );
+ if( infd >= 0 ) { input_filename = name; break; }
+ }
+ }
+ if( infd < 0 && !no_messages )
+ show_error2( "Can't open input file", input_filename.c_str() );
+ }
+ return infd;
+ }
+
+
int zgrep_stdin( int infd, const int format_index,
const std::vector< const char * > & grep_args )
{
@@ -96,7 +165,7 @@ int zgrep_stdin( int infd, const int format_index,
if( !good_status( children, retval == 1 ) ) retval = 2;
if( close( infd ) != 0 )
- { show_close_error( "data feeder" ); return 2; }
+ { show_close_error(); return 2; }
return retval;
}
@@ -104,7 +173,7 @@ int zgrep_stdin( int infd, const int format_index,
int zgrep_file( int infd, const int format_index,
const std::string & input_filename,
const std::vector< const char * > & grep_args,
- const int grep_list_mode, const bool grep_show_name )
+ const int list_mode, const bool show_name )
{
Children children;
if( !set_data_feeder( &infd, children, format_index ) ) return 2;
@@ -140,9 +209,9 @@ int zgrep_file( int infd, const int format_index,
const int size = readblock( fda[0], buffer, buffer_size );
if( size != buffer_size && errno )
{ show_error( "Read error", errno ); return 2; }
- if( size > 0 && !grep_list_mode )
+ if( size > 0 && !list_mode )
{
- if( grep_show_name )
+ if( show_name )
for( int i = 0; i < size; ++i )
{
if( line_begin )
@@ -160,11 +229,175 @@ int zgrep_file( int infd, const int format_index,
if( !good_status( children, retval == 1 ) ) retval = 2;
- if( grep_list_mode && (retval == 0) == (grep_list_mode == 1) )
+ if( list_mode && (retval == 0) == (list_mode == 1) )
std::printf( "%s\n", input_filename.c_str() );
if( close( infd ) != 0 )
- { show_close_error( "data feeder" ); return 2; }
+ { show_close_error(); return 2; }
if( close( fda[0] ) != 0 )
{ show_close_error( GREP ); return 2; }
return retval;
}
+
+} // end namespace
+
+
+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 };
+ int format_index = -1;
+ int infd = -1;
+ int list_mode = 0; // 1 = list matches, -1 = list non matches
+ int show_name = -1; // tri-state bool
+ bool error = false;
+ bool no_messages = false;
+ bool recursive = false;
+ std::string input_filename;
+ std::list< std::string > filenames;
+ std::vector< const char * > grep_args; // args to grep, maybe empty
+ invocation_name = argv[0];
+ program_name = "zgrep";
+
+ const Arg_parser::Option options[] =
+ {
+ { 'a', "text", Arg_parser::no }, // grep GNU
+ { 'A', "after-context", Arg_parser::yes }, // grep GNU
+ { 'b', "byte-offset", Arg_parser::no }, // grep GNU
+ { 'B', "before-context", Arg_parser::yes }, // grep GNU
+ { 'c', "count", Arg_parser::no }, // grep
+ { 'C', "context", Arg_parser::yes }, // grep GNU
+ { 'e', "regexp", Arg_parser::yes }, // grep
+ { 'E', "extended-regexp", Arg_parser::no }, // grep
+ { 'f', "file ", Arg_parser::yes }, // grep
+ { 'F', "fixed-strings", Arg_parser::no }, // grep
+ { 'h', "no-filename", Arg_parser::no }, // grep GNU
+ { 'H', "with-filename", Arg_parser::no }, // grep GNU
+ { 'i', "ignore-case", Arg_parser::no }, // grep
+ { 'I', 0, Arg_parser::no }, // grep GNU
+ { 'l', "files-with-matches", Arg_parser::no }, // grep
+ { '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 },
+ { 's', "no-messages", Arg_parser::no }, // grep
+ { 'v', "invert-match", Arg_parser::no }, // grep
+ { 'V', "version", Arg_parser::no },
+ { 'w', "word-regexp", Arg_parser::no }, // grep GNU
+ { 'x', "line-regexp", Arg_parser::no }, // grep
+ { 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 },
+ { 0 , 0, Arg_parser::no } };
+
+ const Arg_parser parser( argc, argv, options );
+ if( parser.error().size() ) // bad option
+ { show_error( parser.error().c_str(), 0, true ); return 2; }
+
+ maybe_process_config_file( parser );
+
+ int argind = 0;
+ bool pattern_found = false;
+ for( ; argind < parser.arguments(); ++argind )
+ {
+ const int code = parser.code( argind );
+ const char * const arg = parser.argument( argind ).c_str();
+ if( !code )
+ {
+ if( !pattern_found )
+ { grep_args.push_back( arg ); pattern_found = true; continue; }
+ else break; // no more options
+ }
+ switch( code )
+ {
+ case 'a': grep_args.push_back( "-a" ); break;
+ case 'A': grep_args.push_back( "-A" ); grep_args.push_back( arg ); break;
+ case 'b': grep_args.push_back( "-b" ); break;
+ case 'B': grep_args.push_back( "-B" ); grep_args.push_back( arg ); break;
+ case 'c': grep_args.push_back( "-c" ); break;
+ case 'C': grep_args.push_back( "-C" ); grep_args.push_back( arg ); break;
+ case 'e': grep_args.push_back( "-e" ); grep_args.push_back( arg );
+ pattern_found = true; break;
+ case 'E': grep_args.push_back( "-E" ); break;
+ case 'f': grep_args.push_back( "-f" ); grep_args.push_back( arg );
+ pattern_found = true; break;
+ case 'F': grep_args.push_back( "-F" ); break;
+ case 'h': show_name = false; break;
+ case 'H': show_name = true; break;
+ case 'i': grep_args.push_back( "-i" ); break;
+ case 'I': grep_args.push_back( "-I" ); break;
+ case 'l': grep_args.push_back( "-l" ); list_mode = 1; break;
+ case 'L': grep_args.push_back( "-L" ); list_mode = -1; break;
+ case 'm': grep_args.push_back( "-m" ); grep_args.push_back( arg ); break;
+ case 'n': grep_args.push_back( "-n" ); break;
+ case 'N': break;
+ case 'o': grep_args.push_back( "-o" ); break;
+ case 'q': grep_args.push_back( "-q" ); verbosity = -1; break;
+ case 'r': recursive = true; break;
+ case 's': grep_args.push_back( "-s" ); no_messages = true; break;
+ case 'v': grep_args.push_back( "-v" ); break;
+ case 'V': show_version( "Zgrep" ); return 0;
+ case 'w': grep_args.push_back( "-w" ); break;
+ case 'x': grep_args.push_back( "-x" ); break;
+ case format_opt : format_index = parse_format_type( arg ); break;
+ case help_opt : show_help(); return 0;
+ case verbose_opt: if( verbosity < 4 ) ++verbosity;
+ no_messages = false; break;
+ case bz2_opt: parse_compressor( arg, fmt_bz2 ); break;
+ case gz_opt: parse_compressor( arg, fmt_gz ); break;
+ case lz_opt: parse_compressor( arg, fmt_lz ); break;
+ case xz_opt: parse_compressor( arg, fmt_xz ); break;
+ default : internal_error( "uncaught option" );
+ }
+ } // end process options
+
+#if defined(__MSVCRT__) || defined(__OS2__)
+ setmode( STDIN_FILENO, O_BINARY );
+ setmode( STDOUT_FILENO, O_BINARY );
+#endif
+
+ if( !pattern_found ) { show_error( "Pattern not found." ); return 2; }
+
+ for( ; argind < parser.arguments(); ++argind )
+ filenames.push_back( parser.argument( argind ) );
+
+ if( filenames.empty() ) filenames.push_back("-");
+
+ if( show_name < 0 ) show_name = ( filenames.size() != 1 || recursive );
+
+ int retval = 1;
+ while( next_filename( filenames, input_filename, error, recursive,
+ false, no_messages ) )
+ {
+ if( input_filename.empty() ) infd = STDIN_FILENO;
+ else
+ {
+ infd = open_instream( input_filename, no_messages, format_index < 0 );
+ if( infd < 0 ) { error = true; continue; }
+ }
+
+ int tmp;
+ if( infd == STDIN_FILENO )
+ tmp = zgrep_stdin( infd, format_index, grep_args );
+ else tmp = zgrep_file( infd, format_index, input_filename, grep_args,
+ list_mode, show_name );
+ if( tmp == 0 || ( tmp == 2 && retval == 1 ) ) retval = tmp;
+
+ if( input_filename.size() ) { close( infd ); infd = -1; }
+ if( retval == 0 && verbosity < 0 ) break;
+ }
+
+ if( std::fclose( stdout ) != 0 )
+ {
+ show_error( "Can't close stdout", errno );
+ error = true;
+ }
+ if( error && ( retval != 0 || verbosity >= 0 ) ) retval = 2;
+ return retval;
+ }