^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) #!/usr/bin/env perl
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) # SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) # Prefix all lines with "# ", unbuffered. Command being piped in may need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) # to have unbuffering forced with "stdbuf -i0 -o0 -e0 $cmd".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) use strict;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) use IO::Handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) binmode STDIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) binmode STDOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) STDOUT->autoflush(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) my $needed = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) my $char;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) my $bytes = sysread(STDIN, $char, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) exit 0 if ($bytes == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) if ($needed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) print "# ";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) $needed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) print $char;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) $needed = 1 if ($char eq "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) }