66use FindBin qw( $Bin ) ;
77
88use File::Path qw( mkpath ) ;
9- use File::Slurp qw( edit_file read_file ) ;
10- use File::Which qw( which ) ;
119
1210sub main {
1311 my $target = shift || " $Bin /.." ;
1412
1513 my @translators = qw ( lowdown pandoc ) ;
1614 my $translator ;
1715 foreach my $p (@translators ) {
18- if ( defined which ($p ) ) {
16+ if ( _which ($p ) ) {
1917 $translator = $p ;
2018 last ;
2119 }
@@ -32,6 +30,14 @@ sub main {
3230 _make_man( $translator , $target , ' mmdblookup' , 1 );
3331}
3432
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+
3541sub _make_man {
3642 my $translator = shift ;
3743 my $target = shift ;
@@ -53,7 +59,7 @@ sub _make_man {
5359 ' -M' , " section:$section " ,
5460 $input ,
5561 ' -o' , $output ,
56- ) == 0 or die " Failed to run pandoc: $! " ;
62+ ) == 0 or die " Failed to run pandoc: $? " ;
5763 _pandoc_postprocess($output );
5864 }
5965 elsif ( $translator eq ' lowdown' ) {
@@ -66,14 +72,21 @@ sub _make_man {
6672 ' -M' , " section:$section " ,
6773 $input ,
6874 ' -o' , $output ,
69- ) == 0 or die " Failed to run lowdown: $! " ;
75+ ) == 0 or die " Failed to run lowdown: $? " ;
7076 }
7177}
7278
7379sub _make_lib_man_links {
7480 my $target = shift ;
7581
76- 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+
7790 for my $proto ( $header =~ / ^ *extern.+?(MMDB_\w +)\( /gsm ) {
7891 open my $fh , ' >' , " $target /man/man3/$proto .3"
7992 or die " Failed to open file: $! " ;
@@ -88,13 +101,20 @@ sub _make_lib_man_links {
88101sub _pandoc_postprocess {
89102 my $file = shift ;
90103
91- edit_file(
92- sub {
93- s / ^\. IP\n\. nf/ .IP "" 4\n .nf/ gm ;
94- s / (Automatically generated by Pandoc)(.+)$/ $1 / m ;
95- },
96- $file ,
97- );
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: $! " ;
98118}
99119
100120main(shift );
0 commit comments