summaryrefslogtreecommitdiffstats
path: root/zgrep.cc
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--zgrep.cc103
1 files changed, 53 insertions, 50 deletions
diff --git a/zgrep.cc b/zgrep.cc
index b185de0..c642da7 100644
--- a/zgrep.cc
+++ b/zgrep.cc
@@ -1,5 +1,5 @@
/* Zgrep - search compressed files for a regular expression
- Copyright (C) 2010, 2011 Antonio Diaz Diaz.
+ Copyright (C) 2010, 2011, 2012, 2013 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
@@ -15,57 +15,59 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-void show_zgrep_help() throw()
+void show_zgrep_help()
{
- std::printf( "Zgrep is a front end to the grep program that allows transparent search\n" );
- std::printf( "on any combination of compressed and non-compressed files. If any given\n" );
- std::printf( "file is compressed, its uncompressed content is used. If a given file\n" );
- std::printf( "does not exist, and its name does not end with one of the known\n" );
- std::printf( "extensions, zgrep tries the compressed file names corresponding to the\n" );
- std::printf( "supported compressors. If no files are specified, data is read from\n" );
- std::printf( "standard input, decompressed if needed, and fed to grep. Data read from\n" );
- std::printf( "standard input must be of the same type; all uncompressed or all\n" );
- std::printf( "compressed with the same compressor.\n" );
- std::printf( "The supported compressors are bzip2, gzip, lzip and xz.\n" );
- std::printf( "\nUsage: zgrep [options] <pattern> [files]\n" );
- std::printf( "\nExit status is 0 if match, 1 if no match, 2 if trouble.\n" );
- std::printf( "\nOptions:\n" );
- std::printf( " --help display this help and exit\n" );
- std::printf( " -V, --version output version information and exit\n" );
- std::printf( " -a, --text treat all files as text\n" );
- std::printf( " -A, --after-context=<n> print <n> lines of trailing context\n" );
- std::printf( " -b, --byte-offset print the byte offset of each line\n" );
- std::printf( " -B, --before-context=<n> print <n> lines of leading context\n" );
- std::printf( " -c, --count only print a count of matching lines per file\n" );
- std::printf( " -C, --context=<n> print <n> lines of output context\n" );
- std::printf( " -e, --regexp=<pattern> use <pattern> as the pattern to match\n" );
- std::printf( " -E, --extended-regexp <pattern> is an extended regular expression\n" );
- std::printf( " -f, --file=<file> obtain patterns from <file>\n" );
- std::printf( " -F, --fixed-strings <pattern> is a set of newline-separated strings\n" );
- std::printf( " -h, --no-filename suppress the prefixing filename on output\n" );
- std::printf( " -H, --with-filename print the filename for each match\n" );
- std::printf( " -i, --ignore-case ignore case distinctions\n" );
- std::printf( " -I ignore binary files\n" );
- std::printf( " -l, --files-with-matches only print names of files containing matches\n" );
- std::printf( " -L, --files-without-match only print names of files containing no matches\n" );
- std::printf( " -m, --max-count=<n> stop after <n> matches\n" );
- std::printf( " -n, --line-number print the line number of each line\n" );
- std::printf( " -o, --only-matching show only the part of a line matching <pattern>\n" );
- std::printf( " -q, --quiet suppress all messages\n" );
- std::printf( " -r, --recursive operate recursively on directories\n" );
- std::printf( " -s, --no-messages suppress error messages\n" );
- std::printf( " -v, --invert-match select non-matching lines\n" );
- std::printf( " --verbose verbose mode (show error messages)\n" );
- std::printf( " -w, --word-regexp match only whole words\n" );
- std::printf( " -x, --line-regexp match only whole lines\n" );
+ std::printf( "Zgrep is a front end to the grep program that allows transparent search\n"
+ "on any combination of compressed and non-compressed files. If any given\n"
+ "file is compressed, its uncompressed content is used. If a given file\n"
+ "does not exist, and its name does not end with one of the known\n"
+ "extensions, zgrep tries the compressed file names corresponding to the\n"
+ "supported formats. If no files are specified, data is read from\n"
+ "standard input, decompressed if needed, and fed to grep. Data read from\n"
+ "standard input must be of the same type; all uncompressed or all\n"
+ "in the same compression format.\n"
+ "\nThe supported formats are bzip2, gzip, lzip and xz.\n"
+ "\nUsage: zgrep [options] <pattern> [files]\n"
+ "\nExit status is 0 if match, 1 if no match, 2 if trouble.\n"
+ "\nOptions:\n"
+ " --help display this help and exit\n"
+ " -V, --version output version information and exit\n"
+ " -a, --text treat all files as text\n"
+ " -A, --after-context=<n> print <n> lines of trailing context\n"
+ " -b, --byte-offset print the byte offset of each line\n"
+ " -B, --before-context=<n> print <n> lines of leading context\n"
+ " -c, --count only print a count of matching lines per file\n"
+ " -C, --context=<n> print <n> lines of output context\n"
+ " -e, --regexp=<pattern> use <pattern> as the pattern to match\n"
+ " -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"
+ " -I ignore binary files\n"
+ " -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"
+ " -n, --line-number print the line number of each line\n"
+ " -o, --only-matching show only the part of a line matching <pattern>\n"
+ " -q, --quiet suppress all messages\n"
+ " -r, --recursive operate recursively on directories\n"
+ " -s, --no-messages suppress error messages\n"
+ " -v, --invert-match select non-matching lines\n"
+ " --verbose verbose mode (show error messages)\n"
+ " -w, --word-regexp match only whole words\n"
+ " -x, --line-regexp match only whole lines\n" );
show_help_addr();
}
-int zgrep_stdin( int infd, const std::vector< const char * > & grep_args )
+int zgrep_stdin( int infd, const int format_type,
+ const std::vector< const char * > & grep_args )
{
pid_t pid;
- if( !set_data_feeder( &infd, &pid ) ) return 2;
+ if( !set_data_feeder( &infd, &pid, format_type ) ) return 2;
const pid_t grep_pid = fork();
if( grep_pid == 0 ) // child (grep)
{
@@ -73,7 +75,7 @@ int zgrep_stdin( int infd, const std::vector< const char * > & grep_args )
{
const char ** const argv = new const char *[grep_args.size()+2];
argv[0] = GREP;
- for( unsigned int i = 0; i < grep_args.size(); ++i )
+ for( unsigned i = 0; i < grep_args.size(); ++i )
argv[i+1] = grep_args[i];
argv[grep_args.size()+1] = 0;
execvp( argv[0], (char **)argv );
@@ -96,12 +98,13 @@ int zgrep_stdin( int infd, const std::vector< const char * > & grep_args )
}
-int zgrep_file( int infd, const std::string & input_filename,
+int zgrep_file( int infd, const int format_type,
+ const std::string & input_filename,
const std::vector< const char * > & grep_args,
const bool grep_list, const bool grep_show_name )
{
pid_t pid;
- if( !set_data_feeder( &infd, &pid ) ) return 2;
+ if( !set_data_feeder( &infd, &pid, format_type ) ) return 2;
int fda[2]; // pipe from grep
if( pipe( fda ) < 0 )
{ show_error( "Can't create pipe", errno ); return 2; }
@@ -114,7 +117,7 @@ int zgrep_file( int infd, const std::string & input_filename,
{
const char ** const argv = new const char *[grep_args.size()+2];
argv[0] = GREP;
- for( unsigned int i = 0; i < grep_args.size(); ++i )
+ for( unsigned i = 0; i < grep_args.size(); ++i )
argv[i+1] = grep_args[i];
argv[grep_args.size()+1] = 0;
execvp( argv[0], (char **)argv );
@@ -144,7 +147,7 @@ int zgrep_file( int infd, const std::string & input_filename,
if( buffer[i] == '\n' ) line_begin = true;
putchar( buffer[i] );
}
- else if( std::fwrite( buffer, 1, size, stdout ) != (unsigned int)size )
+ else if( std::fwrite( buffer, 1, size, stdout ) != (unsigned)size )
{ show_error( "Write error", errno ); return 2; }
}
if( size < buffer_size ) break;