22
33use strict;
44use warnings;
5- use autodie qw( :all ) ;
65
76use FindBin qw( $Bin ) ;
87
98use File::Path qw( mkpath ) ;
10- use File::Slurp qw( edit_file read_file ) ;
11- use File::Which qw( which ) ;
129
1310sub main {
1411 my $target = shift || " $Bin /.." ;
1512
1613 my @translators = qw ( lowdown pandoc ) ;
1714 my $translator ;
1815 foreach my $p (@translators ) {
19- if ( defined which ($p ) ) {
16+ if ( _which ($p ) ) {
2017 $translator = $p ;
2118 last ;
2219 }
@@ -33,6 +30,14 @@ sub main {
3330 _make_man( $translator , $target , ' mmdblookup' , 1 );
3431}
3532
33+ sub _which {
34+ my $program = shift ;
35+ for my $path ( split /:/, $ENV {PATH } ) {
36+ return 1 if -x " $path /$program " ;
37+ }
38+ return 0;
39+ }
40+
3641sub _make_man {
3742 my $translator = shift ;
3843 my $target = shift ;
@@ -54,7 +59,7 @@ sub _make_man {
5459 ' -M' , " section:$section " ,
5560 $input ,
5661 ' -o' , $output ,
57- );
62+ ) == 0 or die " Failed to run pandoc: $? " ;
5863 _pandoc_postprocess($output );
5964 }
6065 elsif ( $translator eq ' lowdown' ) {
@@ -67,18 +72,27 @@ sub _make_man {
6772 ' -M' , " section:$section " ,
6873 $input ,
6974 ' -o' , $output ,
70- );
75+ ) == 0 or die " Failed to run lowdown: $? " ;
7176 }
7277}
7378
7479sub _make_lib_man_links {
7580 my $target = shift ;
7681
77- my $header = read_file(" $Bin /../include/maxminddb.h" );
82+ open my $header_fh , ' <' , " $Bin /../include/maxminddb.h"
83+ or die " Failed to open header file: $! " ;
84+ my $header = do { local $/ ; <$header_fh > };
85+
86+ die " Error reading file header file: $! " unless defined $header ;
87+
88+ close $header_fh or die " Failed to close header file: $! " ;
89+
7890 for my $proto ( $header =~ / ^ *extern.+?(MMDB_\w +)\( /gsm ) {
79- open my $fh , ' >' , " $target /man/man3/$proto .3" ;
80- print {$fh } " .so man3/libmaxminddb.3\n " ;
81- close $fh ;
91+ open my $fh , ' >' , " $target /man/man3/$proto .3"
92+ or die " Failed to open file: $! " ;
93+ print {$fh } " .so man3/libmaxminddb.3\n "
94+ or die " Failed to write to file: $! " ;
95+ close $fh or die " Failed to close file: $! " ;
8296 }
8397}
8498
@@ -87,13 +101,20 @@ sub _make_lib_man_links {
87101sub _pandoc_postprocess {
88102 my $file = shift ;
89103
90- edit_file(
91- sub {
92- s / ^\. IP\n\. nf/ .IP "" 4\n .nf/ gm ;
93- s / (Automatically generated by Pandoc)(.+)$/ $1 / m ;
94- },
95- $file
96- );
104+ open my $fh , ' <' , $file or die " Failed to open man file for reading: $! " ;
105+ my @lines = <$fh >;
106+ die " Error when reading man page: $! " if $! ;
107+
108+ close $fh or die " Failed to close file: $! " ;
109+
110+ for my $line (@lines ) {
111+ $line =~ s / ^\. IP\n\. nf/ .IP "" 4\n .nf/ gm ;
112+ $line =~ s / (Automatically generated by Pandoc)(.+)$/ $1 / m ;
113+ }
114+
115+ open $fh , ' >' , $file or die " Failed to open file for writing: $! " ;
116+ print $fh @lines or die " Failed to write to file: $! " ;
117+ close $fh or die " Failed to close file: $! " ;
97118}
98119
99120main(shift );
0 commit comments