| 1 |
#! /usr/bin/perl |
| 2 |
# Written by Zack Weinberg <zackw at panix.com> in 2018 and 2020. |
| 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 |
# Using the information in lib/hashes.conf, validate a list of |
| 10 |
# selected hashes and hash groups that was supplied as the argument of |
| 11 |
# the --enable-hashes option to the configure script, expand all |
| 12 |
# the groups, and remove duplicates. |
| 13 |
# |
| 14 |
# Caution: if you change the format of lib/hashes.conf you will |
| 15 |
# probably need to modify gen-crypt-hashes-h as well as this script. |
| 16 |
|
| 17 |
use v5.14; # implicit use strict, use feature ':5.14' |
| 18 |
use warnings FATAL => 'all'; |
| 19 |
use utf8; |
| 20 |
use open qw(:std :utf8); |
| 21 |
no if $] >= 5.022, warnings => 'experimental::re_strict'; |
| 22 |
use if $] >= 5.022, re => 'strict'; |
| 23 |
|
| 24 |
use FindBin (); |
| 25 |
use lib $FindBin::Bin; |
| 26 |
use BuildCommon qw( |
| 27 |
parse_hashes_conf |
| 28 |
); |
| 29 |
|
| 30 |
sub expand_selected { |
| 31 |
my ($hconf, $selected) = @_; |
| 32 |
|
| 33 |
if ($selected eq 'all') { |
| 34 |
return keys %{$hconf->hashes}; |
| 35 |
} else { |
| 36 |
my %enabled; |
| 37 |
my @errors; |
| 38 |
my $hashes = $hconf->hashes; |
| 39 |
my $groups = $hconf->groups; |
| 40 |
for my $w (split /,/, $selected) { |
| 41 |
if ($w eq 'all') { |
| 42 |
push @errors, "'all' must be used alone\n"; |
| 43 |
} elsif (exists $hashes->{$w}) { |
| 44 |
$enabled{$w} = 1; |
| 45 |
} elsif (exists $groups->{$w}) { |
| 46 |
for my $h (@{$groups->{$w}}) { |
| 47 |
$enabled{$h->name} = 1; |
| 48 |
} |
| 49 |
} else { |
| 50 |
push @errors, "'$w' is not a hash or group name\n"; |
| 51 |
} |
| 52 |
} |
| 53 |
if (scalar(%enabled) == 0) { |
| 54 |
push @errors, "no hashes are enabled\n"; |
| 55 |
} |
| 56 |
die join q{}, @errors if @errors; |
| 57 |
return keys %enabled; |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
# |
| 62 |
# Main |
| 63 |
# |
| 64 |
if (scalar(@ARGV) != 2) { |
| 65 |
print {*STDERR} |
| 66 |
"usage: ${FindBin::Script} hashes.conf names,of,selected,hashes\n"; |
| 67 |
exit 1; |
| 68 |
} |
| 69 |
exit 0 if eval { |
| 70 |
my ($hashes_conf, $hashes_selected) = @ARGV; |
| 71 |
my $hconf = parse_hashes_conf($hashes_conf); |
| 72 |
my @enabled = expand_selected($hconf, $hashes_selected); |
| 73 |
|
| 74 |
print ',', (join ',', sort @enabled), ",\n"; |
| 75 |
close STDOUT or die "write error: $!\n"; |
| 76 |
1; |
| 77 |
}; |
| 78 |
|
| 79 |
print {*STDERR} "${FindBin::Script}: $@"; |
| 80 |
exit 1; |