#!/usr/bin/perl use strict; use warnings; my($app_svnid) = '$HeadURL$ $LastChangedRevision$'; ## no critic (RequireInterpolationOfMetachars) use lib substr(`ade-config ade_share_prefix`,0,-1) . '/include'; ## no critic (ProhibitBacktickOperators) use ADE; use lib substr(`fad-config fad_share_prefix`,0,-1) . '/include'; ## no critic (ProhibitBacktickOperators) use FAD; use Getopt::Long qw(:config no_ignore_case); use Fatal qw( close unlink ); # obviate checking close()'s return code my(%fadcat_defined_errors) = ( fadcat_err_access => { fmt => '%s: can\'t %s' }, ); # Options my($opt_regexp, $opt_outfile); sub fadcat { my($errstack_ref) = @_; my($rc, $out_handle, %data_store); # Set ADE options ADE::register_error_types(\%fadcat_defined_errors); # Defaults for options $opt_regexp = undef; $opt_outfile = undef; # Register options if (($rc=ADE::register_options($errstack_ref, 'g:so:s', 'grep:s,file:s', 'main::fadcat_opt_handler_%s')) != $ADE::OK) { return($rc); } # Register handler functions if (($rc=ADE::set_callbacks($errstack_ref, \&fadcat_usage_help, \&fadcat_version, \&fadcat_paths)) != $ADE::OK) { return($rc); } # Process options ADE::debug($errstack_ref, 10, 'fadcat: processing options ...'); if (($rc=ADE::process_options($errstack_ref)) != $ADE::OK) { return($rc); } # Process arguments # All arguments are optional. # Guts if (defined $ARGV[0]) { while (defined $ARGV[0]) { $_ = shift @ARGV; ADE::debug($errstack_ref, 5, "fadcat: calling ade_open_compressed_file_for_reading() to open $_ ..."); if (($rc=ADE::open_compressed_file_for_reading($errstack_ref, $_, \*IN_HANDLE)) != $ADE::OK) { ADE::error($errstack_ref, 'fadcat_err_access', $_, 'open'); return($rc); } ADE::debug($errstack_ref, 5, 'fadcat: calling load() ...'); if (($rc=FAD::load($errstack_ref, \*IN_HANDLE, \%data_store, undef, $opt_regexp)) != $ADE::OK) { return($rc); } ADE::debug($errstack_ref, 5, 'fadcat: load() returned'); close IN_HANDLE; } } else { ADE::debug($errstack_ref, 5, 'fadcat: calling fad_load() ...'); if (($rc=FAD::load($errstack_ref, \*STDIN, \%data_store, undef, $opt_regexp)) != $ADE::OK) { return($rc); } ADE::debug($errstack_ref, 5, 'fadcat: fad_load() returned'); } if (defined $opt_outfile) { ADE::debug($errstack_ref, 10, 'fadcat: opening output file ...'); ADE::register_temp_file($errstack_ref, $opt_outfile); if (($rc=ADE::open_compressed_file_for_writing($errstack_ref, $opt_outfile, \*OUT_HANDLE)) != $ADE::OK) { ADE::error($errstack_ref, 'fadcat_err_access', $opt_outfile, 'open'); return($rc); } $out_handle = \*OUT_HANDLE; } else { $out_handle = \*STDOUT; } ADE::debug($errstack_ref, 10, 'fadcat: dumping FAD store to ' . (defined $opt_outfile ? $opt_outfile : 'stdout') . ' ...'); if (($rc=FAD::dump($errstack_ref, $out_handle, \%data_store)) != $ADE::OK) { return($rc); } if (defined $opt_outfile) { close OUT_HANDLE; ADE::deregister_temp_file($errstack_ref, $opt_outfile); } # Ensure sensible return code return($ADE::OK); } sub fadcat_opt_handler_g ## no critic (RequireArgUnpacking) { return(fadcat_opt_handler_grep(@_)); } sub fadcat_opt_handler_o ## no critic (RequireArgUnpacking) { return(fadcat_opt_handler_file(@_)); } sub fadcat_opt_handler_grep { my($errstack_ref, $regexp) = @_; $opt_regexp = $regexp; return($ADE::OK); } sub fadcat_opt_handler_file { my($errstack_ref, $outfile) = @_; $opt_outfile = $outfile; return($ADE::OK); } sub fadcat_version { my($errstack_ref, $version_text_ref) = @_; return(ADE::extract_version($errstack_ref, $app_svnid, $version_text_ref)); } sub fadcat_paths { my($errstack_ref, $pathlist_text_ref) = @_; my($rc); ${$pathlist_text_ref} = undef; return($ADE::OK); } sub fadcat_usage_help { my($errstack_ref, $usage_text_short_ref, $usage_text_long_ref) = @_; ${$usage_text_short_ref} = '[ ... ]'; ${$usage_text_long_ref} = " -g | --grep= grep out lines containing \n" . " -o | --file= send output to \n"; return($ADE::OK); } ADE::main(\&fadcat);