xref: /openbmc/linux/tools/testing/selftests/kselftest/prefix.pl (revision 5c069b6dedef1fab5420ca8658ed7f9ee4d26007)
1*5c069b6dSKees Cook#!/usr/bin/perl
2*5c069b6dSKees Cook# SPDX-License-Identifier: GPL-2.0
3*5c069b6dSKees Cook# Prefix all lines with "# ", unbuffered. Command being piped in may need
4*5c069b6dSKees Cook# to have unbuffering forced with "stdbuf -i0 -o0 -e0 $cmd".
5*5c069b6dSKees Cookuse strict;
6*5c069b6dSKees Cook
7*5c069b6dSKees Cookbinmode STDIN;
8*5c069b6dSKees Cookbinmode STDOUT;
9*5c069b6dSKees Cook
10*5c069b6dSKees CookSTDOUT->autoflush(1);
11*5c069b6dSKees Cook
12*5c069b6dSKees Cookmy $needed = 1;
13*5c069b6dSKees Cookwhile (1) {
14*5c069b6dSKees Cook	my $char;
15*5c069b6dSKees Cook	my $bytes = sysread(STDIN, $char, 1);
16*5c069b6dSKees Cook	exit 0 if ($bytes == 0);
17*5c069b6dSKees Cook	if ($needed) {
18*5c069b6dSKees Cook		print "# ";
19*5c069b6dSKees Cook		$needed = 0;
20*5c069b6dSKees Cook	}
21*5c069b6dSKees Cook	print $char;
22*5c069b6dSKees Cook	$needed = 1 if ($char eq "\n");
23*5c069b6dSKees Cook}
24