Blame


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