| 1 | #!perl | ||||||||||
| 2 | # | ||||||||||
| 3 | # inifilter - make changes to an .ini file patch | ||||||||||
| 4 | # | ||||||||||
| 5 | |||||||||||
| 6 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 2 | ▶ | use Getopt::Std; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 2 | ▶ | use IO::File; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 2 | ▶ | use IO::Handle; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 2 | ▶ | use App::IniDiff::IniFile; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | 2 | my $prog = $0; | |||||||||
| 14 | 2 | $prog =~ s:.*\/::; | |||||||||
| 15 | |||||||||||
| 16 | 2 | my $Usage = "Usage: $prog [-f file] [-V] [-e] [-p] [-o outfile] filter-file [filter-file ...] | |||||||||
| 17 | -f file Read from file (defaults to stdin) | ||||||||||
| 18 | -V Print version number and exit. | ||||||||||
| 19 | -e Exports the filter results to a text format (debug purposes) | ||||||||||
| 20 | -p Preserves the order of Ordered Field names (such as mod_1, mod_2, etc.) | ||||||||||
| 21 | -o file File to save result to (instead of -f file); \"-\" means write | ||||||||||
| 22 | to stdout. | ||||||||||
| 23 | Reads patterns from filter-file(s), then filters keys matching those | ||||||||||
| 24 | patterns from the specified ini file or inidiff output. | ||||||||||
| 25 | "; | ||||||||||
| 26 | |||||||||||
| 27 | # add support for --help and --version | ||||||||||
| 28 | 2 | $Getopt::Std::STANDARD_HELP_VERSION = "true"; | |||||||||
| 29 | 2 | my $VERSION = '0.19'; | |||||||||
| 30 | 0 | ▶ | sub VERSION_MESSAGE { | ||||||||
|
Subroutine
VERSION_MESSAGE: not called CC=1 SCAR=6.9
| |||||||||||
| 31 | 0 | print "$prog: version $VERSION\n"; | |||||||||
| 32 | } | ||||||||||
| 33 | 0 | ▶ | sub HELP_MESSAGE { | ||||||||
|
Subroutine
HELP_MESSAGE: not called CC=1 SCAR=6.9
| |||||||||||
| 34 | 0 | print STDERR $Usage; | |||||||||
| 35 | } | ||||||||||
| 36 | |||||||||||
| 37 | 2 | my %opt; | |||||||||
| 38 | 2 | ▶ | if (!&getopts('o:f:Vep', \%opt)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | 0 | print STDERR $Usage; | |||||||||
| 40 | 0 | exit 1; | |||||||||
| 41 | } | ||||||||||
| 42 | 2 | ▶ | my $outFile = defined $opt{'o'} ? $opt{'o'} : '-'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 43 | 2 | ▶ | if (defined $opt{'V'}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 44 | 0 | print "$prog: version $VERSION\n"; | |||||||||
| 45 | 0 | exit 0; | |||||||||
| 46 | } | ||||||||||
| 47 | 2 | ▶ | my $inFile = defined $opt{'f'} ? $opt{'f'} : undef; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 48 | |||||||||||
| 49 | 2 | ▶ | if (@ARGV == 0) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 50 | 0 | print STDERR "$prog: no filter-pattern files specified on command line\n"; | |||||||||
| 51 | 0 | die $Usage; | |||||||||
| 52 | } | ||||||||||
| 53 | |||||||||||
| 54 | 2 | my $filter = new App::IniDiff::IniFile::Filter; | |||||||||
| 55 | |||||||||||
| 56 | # setPreserveOrderedFields in the filter instance | ||||||||||
| 57 | 2 | ▶ | if (defined $opt{'p'}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | 0 | $filter->setPreserveOrderedFields(1); | |||||||||
| 59 | } | ||||||||||
| 60 | |||||||||||
| 61 | 2 | my $if; | |||||||||
| 62 | 2 | foreach $if (@ARGV) { | |||||||||
| 63 | 2 | ▶ | if (!$filter->readConf($if)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | 0 | die "$prog: $App::IniDiff::IniFile::Filter::errorString\n"; | |||||||||
| 65 | } | ||||||||||
| 66 | |||||||||||
| 67 | # export the filters to STDOUT | ||||||||||
| 68 | 2 | ▶ | if (defined $opt{'e'}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 69 | 0 | $filter->export(); | |||||||||
| 70 | } | ||||||||||
| 71 | } | ||||||||||
| 72 | |||||||||||
| 73 | # if we have been asked to export the filter, stop | ||||||||||
| 74 | 2 | ▶ | if (defined $opt{'e'}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 75 | 0 | exit 0; | |||||||||
| 76 | } | ||||||||||
| 77 | |||||||||||
| 78 | 2 | my $in; | |||||||||
| 79 | 2 | ▶ | if (defined $inFile) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 80 | 2 | $in = new IO::File($inFile, 'r'); | |||||||||
| 81 | 2 | ▶ | if (!defined $in) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 82 | 0 | die "$prog: can't open $inFile - $!\n"; | |||||||||
| 83 | } | ||||||||||
| 84 | } | ||||||||||
| 85 | else { | ||||||||||
| 86 | 0 | $inFile = 'stdin'; | |||||||||
| 87 | 0 | $in = new IO::Handle; | |||||||||
| 88 | 0 | ▶ | if (!$in->fdopen('STDIN', 'r')) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 89 | 0 | die "$prog: can't dup STDIN - $!\n"; | |||||||||
| 90 | } | ||||||||||
| 91 | } | ||||||||||
| 92 | |||||||||||
| 93 | 2 | my $ini = new App::IniDiff::IniFile($in, 1); | |||||||||
| 94 | 2 | ▶ | if (!defined $ini) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 95 | 0 | die "$prog: $App::IniDiff::IniFile::errorString\n"; | |||||||||
| 96 | } | ||||||||||
| 97 | |||||||||||
| 98 | 2 | ▶ | if (!$filter->filter($ini)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 99 | 0 | die "$prog: error filtering key $inFile: $App::IniDiff::IniFile::Filter::errorString\n"; | |||||||||
| 100 | } | ||||||||||
| 101 | |||||||||||
| 102 | 2 | my $out; | |||||||||
| 103 | 2 | ▶ | if ($outFile eq '-') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 104 | 1 | $out = new IO::Handle; | |||||||||
| 105 | 1 | ▶ | if (!$out->fdopen("STDOUT", "w")) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 106 | 0 | die "$prog: can't fdopen STDOUT - $!\n"; | |||||||||
| 107 | } | ||||||||||
| 108 | } | ||||||||||
| 109 | else { | ||||||||||
| 110 | 1 | $out = new IO::File $outFile, 'w'; | |||||||||
| 111 | 1 | ▶ | if (!defined $out) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 112 | 0 | die "$prog: couldn't open $outFile for writing - $!\n"; | |||||||||
| 113 | } | ||||||||||
| 114 | } | ||||||||||
| 115 | 2 | $ini->write($out); | |||||||||
| 116 | |||||||||||
| 117 | 2 | exit(0); | |||||||||
| 118 | |||||||||||
| 119 | __END__ | ||||||||||