Blob


1 #!/usr/bin/env perl
2 #
3 # pe was written by Omar Polo <op@openbsd.org> and is placed in the
4 # public domain. The author hereby disclaims copyright to this source
5 # code.
7 use open ":std", ":encoding(UTF-8)";
8 use strict;
9 use warnings;
10 use v5.32;
12 use Digest::SHA;
13 use Encode qw(encode);
14 use IO::Poll qw(POLLOUT);
16 use OpenBSD::Pledge;
17 use OpenBSD::Unveil;
19 use GotMArc qw(parse);
21 my $jobs = $ENV{'MAKE_JOBS'} // 1;
23 my $csumdir = $ENV{'CSUMDIR'};
24 die '$CSUMDIR undefined' unless defined $csumdir;
26 my $poll = IO::Poll->new();
27 for (1..$jobs) {
28 open(my $kid, '|-', 'mexp') or die "can't exec mexp: $!";
29 $poll->mask($kid => POLLOUT);
30 }
32 # get current thread checksum
33 sub thrsum {
34 my $sha = Digest::SHA->new(256);
35 $sha->add(encode('UTF-8', $_)) for @_;
36 return $sha->hexdigest;
37 }
39 # get stored thread checksum
40 sub oldsum {
41 my $tid = shift;
42 open my $fh, '<', "$csumdir/$tid" or return "";
43 my $sum = <$fh>;
44 chomp $sum;
45 return $sum;
46 }
48 # save thread checksum
49 sub savesum {
50 my ($tid, $sum) = @_;
51 open my $fh, '>', "$csumdir/$tid"
52 or die "can't open checksum file $csumdir/$tid: $!";
53 say $fh $sum;
54 close $fh;
55 }
57 sub process {
58 my @entries = @_;
60 return unless @entries;
62 my $mail = parse $entries[0];
63 die "wtf?" if $mail->{level} != 0;
65 my $tid = $mail->{mid};
66 my $thrsum = thrsum @_;
67 my $oldsum = oldsum $tid;
68 return if $thrsum eq $oldsum;
70 die "poll: $!" if $poll->poll() == -1;
71 my @handles = $poll->handles(POLLOUT) or die "no procs ready?";
72 my $handle = $handles[int(rand(@handles))];
73 print $handle $_ foreach @entries;
75 savesum($tid, $thrsum);
76 }
78 unveil($csumdir, "rwc") or die "unveil $csumdir: $!";
79 pledge("stdio rpath wpath cpath") or die "pledge: $!";
81 my @thread;
82 while (<>) {
83 print; # continue the pipeline
85 my $new_thread = m/^-/;
86 if ($new_thread && @thread) {
87 process @thread;
88 @thread = ();
89 }
91 push @thread, $_;
92 }
94 process @thread if @thread;