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
7 use open ":std", ":encoding(UTF-8)";
13 use Encode qw(encode);
14 use IO::Poll qw(POLLOUT);
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();
28 open(my $kid, '|-', 'mexp') or die "can't exec mexp: $!";
29 $poll->mask($kid => POLLOUT);
32 # get current thread checksum
34 my $sha = Digest::SHA->new(256);
35 $sha->add(encode('UTF-8', $_)) for @_;
36 return $sha->hexdigest;
39 # get stored thread checksum
42 open my $fh, '<', "$csumdir/$tid" or return "";
48 # save thread checksum
51 open my $fh, '>', "$csumdir/$tid"
52 or die "can't open checksum file $csumdir/$tid: $!";
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);
78 unveil($csumdir, "rwc") or die "unveil $csumdir: $!";
79 pledge("stdio rpath wpath cpath") or die "pledge: $!";
83 print; # continue the pipeline
85 my $new_thread = m/^-/;
86 if ($new_thread && @thread) {
94 process @thread if @thread;