new tool
[hband-tools.git] / tabdata / td-gnuplot
blob7c7e131dc36b7aebbade98650ecb2bcbf75813f1
1 #!/usr/bin/env perl
3 use constant { OUTPUT_TERM=>1, OUTPUT_IMAGE=>2, OUTPUT_DEFAULT=>3, };
4 use Data::Dumper;
5 use Term::Size;
6 use Fcntl qw/F_GETFL F_SETFL F_GETFD F_SETFD FD_CLOEXEC/;
7 no if ($] >= 5.018), 'warnings' => 'experimental::smartmatch';
10 $OptOutput = OUTPUT_TERM;
11 @gnuplot_commands = ();
13 %OptionDefs = (
14 'i' => sub { $OptOutput = OUTPUT_IMAGE; },
15 'd' => sub { $OptOutput = OUTPUT_DEFAULT; },
16 'e=s@' => \@gnuplot_commands,
19 do '/usr/lib/tool/perl5/tabdata/common.pl' or die "$@";
21 pipe $conf_script_rfh, $conf_script_wfh or die "$0: pipe: $!\n";
22 pipe $plot_script_rfh, $plot_script_wfh or die "$0: pipe: $!\n";
23 fcntl($conf_script_rfh, F_SETFD, fcntl($conf_script_rfh, F_GETFD, 0) & ~FD_CLOEXEC);
24 fcntl($plot_script_rfh, F_SETFD, fcntl($plot_script_rfh, F_GETFD, 0) & ~FD_CLOEXEC);
26 process_header(sys_read_line());
28 select $conf_script_wfh;
30 print <<"EOT"
31 set xlabel "$Header[0]"
32 set autoscale
33 #set key on outside
34 set key on bmargin left horizontal
35 set xtics out nomirror
36 set x2tics out nomirror
37 set ytics out nomirror
38 set y2tics out nomirror
39 EOT
42 if($OptOutput eq OUTPUT_TERM)
44 my ($term_cols, $term_rows) = Term::Size::chars *STDOUT{IO};
45 if(not $term_cols) { ($term_cols, $term_rows) = Term::Size::chars *STDERR{IO}; }
47 print <<"EOT"
48 unset grid
49 set terminal dumb size $term_cols,$term_rows ansi256
50 EOT
53 elsif($OptOutput eq OUTPUT_IMAGE)
55 print <<"EOT"
56 set grid
57 set terminal png
58 set output "/dev/stdout"
59 EOT
63 close $conf_script_wfh;
64 select $plot_script_wfh;
66 print "plot ";
68 for my $colnum (2..$#Header+1)
70 my $col = $Header[$colnum-1];
71 print "\"/dev/stdin\" using 1:$colnum with linespoints title \"$col\" axes x1y1, ";
73 print "\n";
75 close $plot_script_wfh;
78 exec 'gnuplot', '--persist', map {('-e', $_)} 'load "/dev/fd/'.fileno($conf_script_rfh).'"', @gnuplot_commands, 'load "/dev/fd/'.fileno($plot_script_rfh).'"';
79 ($errno, $errstr) = (int $!, $!);
80 warn "gnuplot: $errstr\n";
81 exit 125+$errno;