| 1 |
#! /usr/bin/perl |
| 2 |
# Written by Zack Weinberg <zackw at panix.com> in 2017. |
| 3 |
# To the extent possible under law, Zack Weinberg has waived all |
| 4 |
# copyright and related or neighboring rights to this work. |
| 5 |
# |
| 6 |
# See https://creativecommons.org/publicdomain/zero/1.0/ for further |
| 7 |
# details. |
| 8 |
|
| 9 |
# Generate (x)crypt.h from (x)crypt.h.in. |
| 10 |
|
| 11 |
use v5.14; # implicit use strict, use feature ':5.14' |
| 12 |
use warnings FATAL => 'all'; |
| 13 |
use utf8; |
| 14 |
use open qw(:std :utf8); |
| 15 |
no if $] >= 5.022, warnings => 'experimental::re_strict'; |
| 16 |
use if $] >= 5.022, re => 'strict'; |
| 17 |
|
| 18 |
use FindBin (); |
| 19 |
use lib $FindBin::Bin; |
| 20 |
use BuildCommon qw( |
| 21 |
enabled_set |
| 22 |
parse_hashes_conf |
| 23 |
); |
| 24 |
|
| 25 |
# Substitutions we know about are accumulated in this hash. |
| 26 |
my %substs; |
| 27 |
|
| 28 |
sub process_config_h { |
| 29 |
my ($config_h) = @_; |
| 30 |
open my $fh, '<', $config_h |
| 31 |
or die "$config_h: $!\n"; |
| 32 |
|
| 33 |
my $have_sys_cdefs_h = 0; |
| 34 |
my $have_sys_cdefs_begin_end_decls = 0; |
| 35 |
my $have_sys_cdefs_throw = 0; |
| 36 |
local $_; |
| 37 |
while (<$fh>) { |
| 38 |
chomp; |
| 39 |
|
| 40 |
if ($_ eq '#define HAVE_SYS_CDEFS_H 1') { |
| 41 |
$have_sys_cdefs_h = 1; |
| 42 |
} elsif ($_ eq '#define HAVE_SYS_CDEFS_BEGIN_END_DECLS 1') { |
| 43 |
$have_sys_cdefs_begin_end_decls = 1; |
| 44 |
} elsif ($_ eq '#define HAVE_SYS_CDEFS_THROW 1') { |
| 45 |
$have_sys_cdefs_throw = 1; |
| 46 |
} elsif (/^#define PACKAGE_VERSION "((\d+)\.(\d+)\.\d+)"$/) { |
| 47 |
$substs{XCRYPT_VERSION_STR} = $1; |
| 48 |
$substs{XCRYPT_VERSION_MAJOR} = $2; |
| 49 |
$substs{XCRYPT_VERSION_MINOR} = $3; |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
die "$config_h: error: inconsistent <sys/cdefs.h> settings\n" |
| 54 |
if !$have_sys_cdefs_h |
| 55 |
&& ($have_sys_cdefs_begin_end_decls || $have_sys_cdefs_throw); |
| 56 |
|
| 57 |
my $s = q{}; |
| 58 |
my $e = q{}; |
| 59 |
$s .= "#include <sys/cdefs.h>\n" if $have_sys_cdefs_h; |
| 60 |
$s .= "#define __THROW /* nothing */\n" unless $have_sys_cdefs_throw; |
| 61 |
$s .= "\n"; |
| 62 |
if ($have_sys_cdefs_begin_end_decls) { |
| 63 |
$s .= '__BEGIN_DECLS'; |
| 64 |
$e .= '__END_DECLS'; |
| 65 |
} else { |
| 66 |
$s .= "#ifdef __cplusplus\nextern \"C\" {\n#endif"; |
| 67 |
$e .= "#ifdef __cplusplus\n} /* extern \"C\" */\n#endif"; |
| 68 |
} |
| 69 |
$substs{BEGIN_DECLS} = $s; |
| 70 |
$substs{END_DECLS} = $e; |
| 71 |
return; |
| 72 |
} |
| 73 |
|
| 74 |
sub process_hashes_conf { |
| 75 |
my ($hashes_conf, $hashes_enabled) = @_; |
| 76 |
my %enabled = enabled_set($hashes_enabled); |
| 77 |
my $hconf = parse_hashes_conf($hashes_conf); |
| 78 |
|
| 79 |
my $default_prefix_enabled = 0; |
| 80 |
for my $e (@{$hconf->default_candidates}) { |
| 81 |
if ($enabled{$e->name}) { |
| 82 |
$default_prefix_enabled = 1; |
| 83 |
last; |
| 84 |
} |
| 85 |
} |
| 86 |
$substs{DEFAULT_PREFIX_ENABLED} = $default_prefix_enabled; |
| 87 |
return; |
| 88 |
} |
| 89 |
|
| 90 |
sub substitute { |
| 91 |
my ($infile) = @_; |
| 92 |
open my $fh, '<', $infile |
| 93 |
or die "$infile: $!\n"; |
| 94 |
my $error = 0; |
| 95 |
local $_; |
| 96 |
my $subst1 = sub { |
| 97 |
my $rep = $substs{$_[0]}; |
| 98 |
return $rep if defined $rep; |
| 99 |
|
| 100 |
print {*STDERR} "$infile:$.: error: no substitution for $_[0]\n"; |
| 101 |
$error = 1; |
| 102 |
return "!$_[0]!"; |
| 103 |
}; |
| 104 |
while (<$fh>) { |
| 105 |
s/@([A-Z0-9_]+)@/&$subst1($1)/eg; |
| 106 |
print; |
| 107 |
} |
| 108 |
exit 1 if $error; |
| 109 |
return; |
| 110 |
} |
| 111 |
|
| 112 |
# |
| 113 |
# Main |
| 114 |
# |
| 115 |
if (scalar(@ARGV) != 2 && scalar(@ARGV) != 4) { |
| 116 |
print {*STDERR} 'usage: ' |
| 117 |
. ${FindBin::Script} |
| 118 |
. ' template config.h [hashes.conf ,hashes,enabled,]' . "\n"; |
| 119 |
exit 1; |
| 120 |
} |
| 121 |
exit 0 if eval { |
| 122 |
my $infile = shift @ARGV; |
| 123 |
process_config_h(shift @ARGV); |
| 124 |
process_hashes_conf(@ARGV) if @ARGV; |
| 125 |
substitute($infile); |
| 126 |
close STDOUT or die "write error: $!\n"; |
| 127 |
1; |
| 128 |
}; |
| 129 |
|
| 130 |
print {*STDERR} "${FindBin::Script}: $@"; |
| 131 |
exit 1; |