summaryrefslogtreecommitdiffstats
path: root/zgrep.cc
diff options
context:
space:
mode:
authorDaniel Baumann <mail@daniel-baumann.ch>2015-11-08 04:40:35 +0000
committerDaniel Baumann <mail@daniel-baumann.ch>2015-11-08 04:40:35 +0000
commitf7deb94049481143ef12eab590840d39aecedc14 (patch)
tree9ff4090b78d8343fd073ba1ea32068bb0d07caa6 /zgrep.cc
parentAdding debian version 1.3-4. (diff)
downloadzutils-f7deb94049481143ef12eab590840d39aecedc14.tar.xz
zutils-f7deb94049481143ef12eab590840d39aecedc14.zip
Merging upstream version 1.4~pre2.
Signed-off-by: Daniel Baumann <mail@daniel-baumann.ch>
Diffstat (limited to 'zgrep.cc')
-rw-r--r--zgrep.cc97
1 files changed, 33 insertions, 64 deletions
diff --git a/zgrep.cc b/zgrep.cc
index 118b6b2..0f7bd50 100644
--- a/zgrep.cc
+++ b/zgrep.cc
@@ -1,5 +1,5 @@
/* Zgrep - search compressed files for a regular expression
- Copyright (C) 2010-2014 Antonio Diaz Diaz.
+ Copyright (C) 2010-2015 Antonio Diaz Diaz.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -39,14 +39,11 @@
#include "rc.h"
#include "zutils.h"
-#ifndef O_BINARY
-#define O_BINARY 0
-#endif
-
namespace {
#include "recursive.cc"
+#include "zcatgrep.cc"
void show_help()
{
@@ -75,7 +72,6 @@ void show_help()
" -E, --extended-regexp <pattern> is an extended regular expression\n"
" -f, --file=<file> obtain patterns from <file>\n"
" -F, --fixed-strings <pattern> is a set of newline-separated strings\n"
- " --format=<fmt> force given format (bz2, gz, lz, xz)\n"
" -h, --no-filename suppress the prefixing filename on output\n"
" -H, --with-filename print the filename for each match\n"
" -i, --ignore-case ignore case distinctions\n"
@@ -83,9 +79,11 @@ void show_help()
" -l, --files-with-matches only print names of files containing matches\n"
" -L, --files-without-match only print names of files containing no matches\n"
" -m, --max-count=<n> stop after <n> matches\n"
+ " -M, --format=<list> exclude formats not in <list>\n"
" -n, --line-number print the line number of each line\n"
" -N, --no-rcfile don't read runtime configuration file\n"
" -o, --only-matching show only the part of a line matching <pattern>\n"
+ " -O, --force-format=<fmt> force given format (bz2, gz, lz, xz)\n"
" -q, --quiet suppress all messages\n"
" -r, --recursive operate recursively on directories\n"
" -s, --no-messages suppress error messages\n"
@@ -101,42 +99,6 @@ void show_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 )
{
@@ -243,13 +205,11 @@ int zgrep_file( int infd, const int format_index,
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 };
+ enum { help_opt = 256, 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;
@@ -277,9 +237,11 @@ int main( const int argc, const char * const argv[] )
{ '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
+ { 'M', "format", Arg_parser::yes },
{ 'n', "line-number", Arg_parser::no }, // grep
{ 'N', "no-rcfile", Arg_parser::no },
{ 'o', "only-matching", Arg_parser::no }, // grep
+ { 'O', "force-format", Arg_parser::yes },
{ 'q', "quiet", Arg_parser::no },
{ 'r', "recursive", Arg_parser::no },
{ 's', "no-messages", Arg_parser::no }, // grep
@@ -287,7 +249,6 @@ int main( const int argc, const char * const argv[] )
{ '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 },
@@ -307,26 +268,24 @@ int main( const int argc, const char * const argv[] )
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
- }
+ if( !code ) break; // no more options
+ const std::string & arg = parser.argument( argind );
switch( code )
{
case 'a': grep_args.push_back( "-a" ); break;
- case 'A': grep_args.push_back( "-A" ); grep_args.push_back( arg ); break;
+ case 'A': grep_args.push_back( "-A" );
+ grep_args.push_back( arg.c_str() ); break;
case 'b': grep_args.push_back( "-b" ); break;
- case 'B': grep_args.push_back( "-B" ); grep_args.push_back( arg ); break;
+ case 'B': grep_args.push_back( "-B" );
+ grep_args.push_back( arg.c_str() ); 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 'C': grep_args.push_back( "-C" );
+ grep_args.push_back( arg.c_str() ); break;
+ case 'e': grep_args.push_back( "-e" );
+ grep_args.push_back( arg.c_str() ); 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" );
+ grep_args.push_back( arg.c_str() ); pattern_found = true; break;
case 'F': grep_args.push_back( "-F" ); break;
case 'h': show_name = false; break;
case 'H': show_name = true; break;
@@ -334,10 +293,13 @@ int main( const int argc, const char * const argv[] )
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 'm': grep_args.push_back( "-m" );
+ grep_args.push_back( arg.c_str() ); break;
+ case 'M': parse_format_list( arg ); break;
case 'n': grep_args.push_back( "-n" ); break;
case 'N': break;
case 'o': grep_args.push_back( "-o" ); break;
+ case 'O': format_index = parse_format_type( arg ); 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;
@@ -345,7 +307,6 @@ int main( const int argc, const char * const argv[] )
case 'V': show_version(); 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;
@@ -362,7 +323,14 @@ int main( const int argc, const char * const argv[] )
setmode( STDOUT_FILENO, O_BINARY );
#endif
- if( !pattern_found ) { show_error( "Pattern not found." ); return 2; }
+ if( !pattern_found )
+ {
+ if( argind >= parser.arguments() )
+ { show_error( "Pattern not found." ); return 2; }
+ const std::string & arg = parser.argument( argind++ );
+ if( arg.size() && arg[0] == '-' ) grep_args.push_back( "-e" );
+ grep_args.push_back( arg.c_str() );
+ }
for( ; argind < parser.arguments(); ++argind )
filenames.push_back( parser.argument( argind ) );
@@ -372,13 +340,14 @@ int main( const int argc, const char * const argv[] )
if( show_name < 0 ) show_name = ( filenames.size() != 1 || recursive );
int retval = 1;
+ bool error = false;
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 );
+ infd = open_instream( input_filename, format_index < 0, no_messages );
if( infd < 0 ) { error = true; continue; }
}