diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2022-07-26 05:52:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2022-07-26 05:52:14 +0000 |
commit | 8a67143a2cc2f55f9c3013a8467a84a2dd0c1c14 (patch) | |
tree | c64b3cecf08395f4e1e48ce022a2c69478a8ec6a /zgrep.cc | |
parent | Adding upstream version 1.11. (diff) | |
download | zutils-8a67143a2cc2f55f9c3013a8467a84a2dd0c1c14.tar.xz zutils-8a67143a2cc2f55f9c3013a8467a84a2dd0c1c14.zip |
Adding upstream version 1.12~pre2.upstream/1.12_pre2
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'zgrep.cc')
-rw-r--r-- | zgrep.cc | 154 |
1 files changed, 83 insertions, 71 deletions
@@ -79,26 +79,33 @@ 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" + " -G, --basic-regexp <pattern> is a basic regular expression (default)\n" " -h, --no-filename suppress the prefixing file name on output\n" " -H, --with-filename print the file name 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" + " --label=<label> use <label> as file name for standard input\n" + " --line-buffered flush output on every line\n" " -m, --max-count=<n> stop after <n> matches\n" " -M, --format=<list> process only the formats 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 the format given (bz2, gz, lz, xz, zst)\n" - " -q, --quiet suppress all messages\n" + " -P, --perl-regexp <pattern> is a Perl regular expression\n" + " -q, --quiet, --silent suppress all messages\n" " -r, --recursive operate recursively on directories\n" " -R, --dereference-recursive recursively follow symbolic links\n" " -s, --no-messages suppress error messages\n" + " -T, --initial-tab make tabs line up (if needed)\n" + " -U, --binary don't strip CR characters at EOL (DOS/Windows)\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" + " -Z, --null print 0 byte (ASCII NUL) after file name\n" " --bz2=<command> set compressor and options for bzip2 format\n" " --gz=<command> set compressor and options for gzip format\n" " --lz=<command> set compressor and options for lzip format\n" @@ -110,43 +117,12 @@ void show_help() } -int zgrep_stdin( int infd, const int format_index, - const std::vector< const char * > & grep_args ) - { - Children children; - if( !set_data_feeder( "", &infd, children, format_index ) ) return 2; - const pid_t grep_pid = fork(); - if( grep_pid == 0 ) // child (grep) - { - if( dup2( infd, STDIN_FILENO ) >= 0 && close( infd ) == 0 ) - { - const char ** const argv = new const char *[grep_args.size()+2]; - argv[0] = GREP; - 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 ); - } - show_exec_error( GREP ); - _exit( 2 ); - } - if( grep_pid < 0 ) // parent - { show_fork_error( GREP ); return 2; } - - int retval = wait_for_child( grep_pid, GREP ); - - if( !good_status( children, retval == 1 ) ) retval = 2; - - if( close( infd ) != 0 ) - { show_close_error(); return 2; } - return retval; - } - - int zgrep_file( int infd, const int format_index, const std::string & input_filename, const std::vector< const char * > & grep_args, - const int list_mode, const bool show_name ) + const int list_mode, const bool initial_tab, + const bool line_buffered, const bool show_name, + const bool z_null ) { Children children; if( !set_data_feeder( input_filename, &infd, children, format_index ) ) @@ -178,34 +154,48 @@ int zgrep_file( int infd, const int format_index, enum { buffer_size = 256 }; uint8_t buffer[buffer_size]; bool line_begin = true; - while( true ) + bool at_eof = false; + while( !at_eof ) { - const int size = readblock( fda[0], buffer, buffer_size ); - if( size != buffer_size && errno ) - { show_error( "Read error", errno ); return 2; } + int size; + bool error = false; + if( line_buffered ) + for( size = 0; size < buffer_size; ) + { if( readblock( fda[0], buffer + size, 1 ) == 1 ) + { ++size; if( buffer[size-1] == '\n' ) break; } + else { at_eof = true; if( errno ) { error = true; } break; } } + else + { size = readblock( fda[0], buffer, buffer_size ); + if( size < buffer_size ) { at_eof = true; if( errno ) error = true; } } + if( error ) + { std::fflush( stdout ); show_error( "Read error", errno ); return 2; } if( size > 0 && !list_mode ) { - if( show_name ) + if( show_name ) // print the file name for each match for( int i = 0; i < size; ++i ) { if( line_begin ) - { line_begin = false; std::printf( "%s:", input_filename.c_str() ); } - if( buffer[i] == '\n' ) line_begin = true; + { line_begin = false; + const int len = std::printf( "%s%c", input_filename.c_str(), + z_null ? 0 : ':' ); + if( initial_tab && len > 0 && len % 8 ) putchar( '\t' ); } putchar( buffer[i] ); + if( buffer[i] == '\n' ) + { line_begin = true; if( line_buffered ) std::fflush( stdout ); } } else if( std::fwrite( buffer, 1, size, stdout ) != (unsigned)size ) { std::fflush( stdout ); show_error( "Write error", errno ); return 2; } - std::fflush( stdout ); } - if( size < buffer_size ) break; // end of grep's output } + std::fflush( stdout ); int retval = wait_for_child( grep_pid, GREP ); if( !good_status( children, retval == 1 ) ) retval = 2; if( list_mode && (retval == 0) == (list_mode == 1) ) - { std::printf( "%s\n", input_filename.c_str() ); std::fflush( stdout ); } + { std::printf( "%s%c", input_filename.c_str(), z_null ? 0 : '\n' ); + std::fflush( stdout ); } if( close( infd ) != 0 ) { show_close_error(); return 2; } if( close( fda[0] ) != 0 ) @@ -218,16 +208,21 @@ int zgrep_file( int infd, const int format_index, int main( const int argc, const char * const argv[] ) { - enum { help_opt = 256, verbose_opt, color_opt, + enum { help_opt = 256, verbose_opt, color_opt, label_opt, linebuf_opt, bz2_opt, gz_opt, lz_opt, xz_opt, zst_opt }; int format_index = -1; int list_mode = 0; // 1 = list matches, -1 = list non-matches int recursive = 0; // 1 = '-r', 2 = '-R' int show_name = -1; // tri-state bool + bool initial_tab = false; + bool line_buffered = false; bool no_messages = false; + bool z_null = false; // for '-Z, --null' std::list< std::string > filenames; std::vector< const char * > grep_args; // args to grep, maybe empty - std::string color_option; // needed because of optional arg + std::string color_option; // additional args to grep + std::string label_option; + std::string label = "(standard input)"; // prefix for standard input program_name = "zgrep"; invocation_name = ( argc > 0 ) ? argv[0] : program_name; @@ -243,6 +238,7 @@ int main( const int argc, const char * const argv[] ) { 'E', "extended-regexp", Arg_parser::no }, // grep { 'f', "file ", Arg_parser::yes }, // grep { 'F', "fixed-strings", Arg_parser::no }, // grep + { 'G', "basic-regexp", Arg_parser::no }, // grep GNU { 'h', "no-filename", Arg_parser::no }, // grep GNU { 'H', "with-filename", Arg_parser::no }, // grep GNU { 'i', "ignore-case", Arg_parser::no }, // grep @@ -255,17 +251,24 @@ int main( const int argc, const char * const argv[] ) { 'N', "no-rcfile", Arg_parser::no }, { 'o', "only-matching", Arg_parser::no }, // grep { 'O', "force-format", Arg_parser::yes }, + { 'P', "perl-regexp", Arg_parser::no }, // grep GNU { 'q', "quiet", Arg_parser::no }, + { 'q', "silent", Arg_parser::no }, { 'r', "recursive", Arg_parser::no }, { 'R', "dereference-recursive", Arg_parser::no }, { 's', "no-messages", Arg_parser::no }, // grep + { 'T', "initial-tab", Arg_parser::no }, // grep GNU + { 'U', "binary", Arg_parser::no }, // grep GNU { '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 + { 'Z', "null", Arg_parser::no }, // grep GNU { help_opt, "help", Arg_parser::no }, { verbose_opt, "verbose", Arg_parser::no }, { color_opt, "color", Arg_parser::maybe }, + { label_opt, "label", Arg_parser::yes }, + { linebuf_opt, "line-buffered", Arg_parser::no }, { bz2_opt, "bz2", Arg_parser::yes }, { gz_opt, "gz", Arg_parser::yes }, { lz_opt, "lz", Arg_parser::yes }, @@ -286,24 +289,26 @@ int main( const int argc, const char * const argv[] ) const int code = parser.code( argind ); if( !code ) break; // no more options const char * const pn = parser.parsed_name( argind ).c_str(); - const std::string & arg = parser.argument( argind ); + const std::string & sarg = parser.argument( argind ); + const char * const arg = sarg.c_str(); switch( code ) { case 'a': grep_args.push_back( "-a" ); break; case 'A': grep_args.push_back( "-A" ); - grep_args.push_back( arg.c_str() ); break; + 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.c_str() ); break; + 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.c_str() ); break; + grep_args.push_back( arg ); break; case 'e': grep_args.push_back( "-e" ); - grep_args.push_back( arg.c_str() ); pattern_found = true; break; + 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.c_str() ); pattern_found = true; break; + grep_args.push_back( arg ); pattern_found = true; break; case 'F': grep_args.push_back( "-F" ); break; + case 'G': grep_args.push_back( "-G" ); break; case 'h': show_name = false; break; case 'H': show_name = true; break; case 'i': grep_args.push_back( "-i" ); break; @@ -311,37 +316,46 @@ int main( const int argc, const char * const argv[] ) 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.c_str() ); break; - case 'M': parse_format_list( arg, pn ); break; + grep_args.push_back( arg ); break; + case 'M': parse_format_list( sarg, pn ); 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, pn ); break; + case 'O': format_index = parse_format_type( sarg, pn ); break; + case 'P': grep_args.push_back( "-P" ); break; case 'q': grep_args.push_back( "-q" ); verbosity = -1; break; case 'r': recursive = 1; break; case 'R': recursive = 2; break; case 's': grep_args.push_back( "-s" ); no_messages = true; break; + case 'T': grep_args.push_back( "-T" ); initial_tab = true; break; + case 'U': grep_args.push_back( "-U" ); break; case 'v': grep_args.push_back( "-v" ); break; case 'V': show_version( GREP " --version" ); return 0; case 'w': grep_args.push_back( "-w" ); break; case 'x': grep_args.push_back( "-x" ); break; + case 'Z': z_null = true; break; case help_opt: show_help(); return 0; case verbose_opt: no_messages = false; if( verbosity < 4 ) ++verbosity; break; case color_opt: color_option = "--color"; - if( !arg.empty() ) { color_option += '='; color_option += arg; } + if( !sarg.empty() ) { color_option += '='; color_option += sarg; } 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; - case zst_opt: parse_compressor( arg, fmt_zst ); break; + case label_opt: label_option = label = sarg; break; + case linebuf_opt: grep_args.push_back( "--line-buffered" ); + line_buffered = true; break; + case bz2_opt: parse_compressor( sarg, fmt_bz2 ); break; + case gz_opt: parse_compressor( sarg, fmt_gz ); break; + case lz_opt: parse_compressor( sarg, fmt_lz ); break; + case xz_opt: parse_compressor( sarg, fmt_xz ); break; + case zst_opt: parse_compressor( sarg, fmt_zst ); break; default : internal_error( "uncaught option." ); } } // end process options if( !color_option.empty() ) // push the last value set grep_args.push_back( color_option.c_str() ); + if( !label_option.empty() ) // for "Binary file <label> matches" + grep_args.push_back( label_option.insert( 0, "--label=" ).c_str() ); #if defined __MSVCRT__ || defined __OS2__ setmode( STDIN_FILENO, O_BINARY ); @@ -352,9 +366,9 @@ int main( const int argc, const char * const argv[] ) { 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() ); + const std::string & pat = parser.argument( argind++ ); + if( pat.size() && pat[0] == '-' ) grep_args.push_back( "-e" ); + grep_args.push_back( pat.c_str() ); } for( ; argind < parser.arguments(); ++argind ) @@ -375,7 +389,7 @@ int main( const int argc, const char * const argv[] ) if( input_filename == "." ) { if( stdin_used ) continue; else stdin_used = true; - infd = STDIN_FILENO; input_filename = "-"; + infd = STDIN_FILENO; input_filename = label; } else { @@ -383,11 +397,9 @@ int main( const int argc, const char * const argv[] ) 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 ); + const int tmp = zgrep_file( infd, format_index, input_filename, grep_args, + list_mode, initial_tab, line_buffered, + show_name, z_null ); if( tmp == 0 || ( tmp == 2 && retval == 1 ) ) retval = tmp; if( close( infd ) != 0 ) |