Blame


1 27363023 2022-08-25 op #!/usr/bin/env perl
2 27363023 2022-08-25 op
3 27363023 2022-08-25 op use open ":std", ":encoding(UTF-8)";
4 27363023 2022-08-25 op use strict;
5 27363023 2022-08-25 op use warnings;
6 27363023 2022-08-25 op use v5.32;
7 27363023 2022-08-25 op use IO::Poll qw(POLLOUT);
8 27363023 2022-08-25 op
9 054f3fd4 2022-08-25 op use OpenBSD::Pledge;
10 054f3fd4 2022-08-25 op use OpenBSD::Unveil;
11 054f3fd4 2022-08-25 op
12 27363023 2022-08-25 op my $jobs = $ENV{'MAKE_JOBS'} // 1;
13 27363023 2022-08-25 op
14 27363023 2022-08-25 op my $poll = IO::Poll->new();
15 27363023 2022-08-25 op for (1..$jobs) {
16 27363023 2022-08-25 op open(my $kid, '|-', './mexp') or die "can't exec ./mexp: $!";
17 27363023 2022-08-25 op $poll->mask($kid => POLLOUT);
18 27363023 2022-08-25 op }
19 27363023 2022-08-25 op
20 27363023 2022-08-25 op sub process {
21 27363023 2022-08-25 op die "poll: $!" if $poll->poll() == -1;
22 27363023 2022-08-25 op my @handles = $poll->handles(POLLOUT) or die "no procs ready?";
23 27363023 2022-08-25 op my $handle = $handles[int(rand(@handles))];
24 de557185 2022-08-26 op print $handle $_ foreach @_;
25 27363023 2022-08-25 op }
26 27363023 2022-08-25 op
27 054f3fd4 2022-08-25 op unveil("./mexp", "rx") or die "unveil mexp: $!";
28 054f3fd4 2022-08-25 op pledge("stdio proc exec") or die "pledge: $!";
29 054f3fd4 2022-08-25 op
30 27363023 2022-08-25 op my @thread;
31 27363023 2022-08-25 op while (<>) {
32 27363023 2022-08-25 op print; # continue the pipeline
33 27363023 2022-08-25 op
34 de557185 2022-08-26 op my $new_thread = m/^-/;
35 de557185 2022-08-26 op if ($new_thread && @thread) {
36 27363023 2022-08-25 op process @thread;
37 27363023 2022-08-25 op @thread = ();
38 27363023 2022-08-25 op }
39 27363023 2022-08-25 op
40 27363023 2022-08-25 op push @thread, $_;
41 27363023 2022-08-25 op }
42 27363023 2022-08-25 op
43 27363023 2022-08-25 op process @thread if @thread;