make getpeername() return the original socket address which before it was intercepted
[hband-tools.git] / user-tools / untabularize
blob140e1f752c72fcabbac8730bd2706ca25b850264
1 #!/usr/bin/env perl
3 =pod
5 =head1 NAME
7 untabularize - Revert the formatting done by tabularize(1)
9 =head1 SYNOPSIS
11 untabularize [I<OPTIONS>]
13 =head1 DESCRIPTION
15 =head1 OPTIONS
17 =over 4
19 =back
21 =head1 LIMITATIONS
23 =head1 SEE ALSO
25 tabularize(1)
27 =cut
30 use Data::Dumper;
31 use Getopt::Long qw/:config no_ignore_case no_bundling no_getopt_compat no_auto_abbrev require_order/;
32 use List::MoreUtils qw/all any none/;
33 use Pod::Usage;
34 use POSIX;
35 no if ($] >= 5.018), 'warnings' => 'experimental::smartmatch';
36 use utf8;
37 use open ':std', ':encoding(UTF-8)';
39 GetOptions(
40 'help' => sub { pod2usage(-exitval=>0, -verbose=>99); },
41 '<>' => sub { unshift @ARGV, @_[0]; die '!FINISH'; },
42 ) or pod2usage(-exitval=>2, -verbose=>99);
45 while(<STDIN>)
47 chomp;
48 if(/^(┌|├|└|\+)(.)/ and not @cellWidths)
50 my $linechar = $2;
51 s/^[^\Q$linechar\E]//;
52 s/[^\Q$linechar\E]$//;
53 @cellWidths = map {length} split /[^\Q$linechar\E]/;
54 next;
56 if(/^(\||│)/)
58 my $gridchar = $1;
59 s/^\Q$gridchar\E//;
60 s/\Q$gridchar\E$//;
61 if(not @cellWidths)
63 if($gridchar eq '|')
65 # pipe char may occur in the cell data as well,
66 # so can not confidentally take it as a cell separator.
67 die "$0: can not determine the columns in --ascii --no-horizontal mode\n";
69 @cellWidths = map {length} split /\Q$gridchar\E/;
71 my $record = $_;
72 my $prev_cell_endpos = 0;
73 my @cells = map { s/\s+$// or s/^\s+//; $_ }
74 map { my $cell = substr $record, $prev_cell_endpos, $_; $prev_cell_endpos += $_ + length $gridchar; $cell }
75 @cellWidths;
76 print join("\t", @cells) . "\n";