summaryrefslogtreecommitdiff
path: root/src/chroot-tools/indent
blob: 5a7f6547e1096d6d8bc5f04a72721374e9628b58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/usr/bin/env perl
use warnings;
use strict;
use constant BUFFER_SIZE => 40;
binmode(STDIN);
binmode(STDOUT);

exit(1) if ($#ARGV != 0);
my $indent = $ARGV[0];

my $print_indent = 1;
my $buffer;
my $size;
my $c;
while (1) {
	$size = sysread(STDIN, $buffer, BUFFER_SIZE);
	last if ($size < 1);
	for (0..$size-1) {
		$c = substr($buffer, $_, 1);
		if ($c eq "\n") {
			syswrite(STDOUT, $indent) if ($print_indent);
			syswrite(STDOUT, $c, 1);
			$print_indent = 1;
		} elsif ($c eq "\r") {
			syswrite(STDOUT, $c, 1);
			$print_indent = 1;
		} else {
			syswrite(STDOUT, $indent) if ($print_indent);
			syswrite(STDOUT, $c, 1);
			$print_indent = 0;
		}
	}
}