#!/usr/bin/perl -w

#TODO:  don't buffer if it's coming from a file, read it twice...
#	otherwise, use temp file instead of buffering in memory?

use strict;

my $gap = @ARGV ? shift : 2;
my @options = @ARGV;

my $opt;
for $opt (@options) {
	$opt ||= "-";
	$opt =~ /[a-z]$/ or $opt .= $opt =~ /\./ ? "f" : "s";
	$opt = "%$opt";
}

# print "@options\n";

my @width;
my @rows;

my $line;
my $length;
my $i;
while (defined ($line = <STDIN>)) {
	my @row;
	chomp $line;
	@row = split /\t/, $line, -1;
	$i = 0;
	for (@row) {
		if ($_ eq "\0") {
			$_ = "[NULL]";
		} else {
#			s/([^\\]|^)((?:\\\\)*)\\0/$1$2\0/g;
#			s/([^\\]|^)((?:\\\\)*)\\n/$1$2\n/g;
#			s/([^\\]|^)((?:\\\\)*)\\t/$1$2\t/g;
#			s/\\\\/\\/g;
		}
		$length = length sprintf $options[$i]||="%-s", $_;
		$length > ($width[$i]||-1) and $width[$i] = $length;
		++$i;
	}
	push @rows, \@row;
}

$i = 0;
for $opt (@options) {
	$opt =~ /(%|[^\.0-9])([1-9])/ or $opt =~ s/(\.|.$)/$width[$i].$1/e;
	++$i;
}

my $format = join " "x$gap, @options;

# print "$format\n";
use Data::Dumper;

my @empty = ("")x@options;

# XXX this is broken 
for (@rows) {
	printf "$format\n", @$_, @empty;
}
