chiark / gitweb /
cvs-repomove: work with Solaris's shoddy sed. (Closes: #497670)
[chiark-utils.git] / scripts / genspic2gnuplot
1 #!/usr/bin/perl
2 #   genspic2gnuplot - Copyright 2004 Ian Jackson - see below
3 #
4 # Reads a `genspic' file on stdin.  Takes exactly one arg, <pfx>.
5 #
6 # Produces various output files:
7 #  <pfx>.gnuplots.sh                     run this to display results
8 #  <pfx>,<Kind><N>.gnuplot-cmd           gnuplot script for displaying:
9 #  <pfx>,<Kind><N>-<M>.gnuplot-data      gnuplot-format input data
10 #  <pfx>,gnuplot-fifo                    working fifo for .gnuplots.sh
11 # where
12 #  <Kind> is Freq or Time (according to the type of analysis)
13 #  <N>    is the count, starting at 0, of which report this is from gnucap
14 #  <M>    is the individual column of Y data
15 #
16 # Limitations
17 #
18 #  There's no easy way to mess with the gnuplot settings.
19 #
20 #  This whole scheme is very clumsy.  There should be a driver program
21 #  with command-line option parsing.
22
23 # This program and its documentation are free software; you can
24 # redistribute them and/or modify them under the terms of the GNU
25 # General Public License as published by the Free Software Foundation;
26 # either version 3, or (at your option) any later version.
27
28 # This program and its documentation are distributed in the hope that
29 # they will be useful, but WITHOUT ANY WARRANTY; without even the
30 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
31 # PURPOSE.  See the GNU General Public License for more details.
32
33 # You should have received a copy of the GNU General Public License along
34 # with this program; if not, consult the Free Software Foundation's
35 # website at www.fsf.org, or the GNU Project website at www.gnu.org.
36
37 die unless @ARGV==1;
38 die if $ARGV[0] =~ m/^\-/;
39
40 ($ofb)= @ARGV;
41 $sof= "$ofb.gnuplots.sh";
42 open A, "> $sof" or die $!;
43 system 'chmod','+x',"$sof"; $? and die $?;
44 print A <<END
45 #!/bin/sh
46 set -e
47 fi=$ofb,gnuplot-fifo
48 rm -f \$fi
49 mkfifo -m 600 \$fi
50 END
51     or die $!;
52
53 for (;;) {
54     ($c,@s)= split /\s+/, <STDIN>;
55     if ($c eq 'S') {
56         ($cplot,$logxy,@columns) = @s;
57         $cplot .= $counter{$cplot}++;
58         unshift @columns, 'x:';
59         @mmm= map { s/^(\w+)\:// or die; $1; } @columns;
60         open S, "> $ofb,$cplot.gnuplot-cmd" or die $!;
61         print S <<END
62 set data style lines
63 set title '$cplot'
64 END
65         or die $!;
66         print S "set logscale xy\n" or die $! if $logxy;
67         print S "set y2tics autofreq\n" or die $! if grep { $_ eq 'y2' } @mmm;
68         undef %min;
69         undef %max;
70         for ($yn=1; $yn<=$#columns; $yn++) {
71             open "O$yn", "> $ofb,$cplot-$yn.gnuplot-data" or die $!;
72         }
73     } elsif ($c eq 'T') {
74         die unless @mmm;
75         die if @s;
76         foreach $mmm (keys %min) {
77             print S "set ${mmm}range [$min{$mmm}:$max{$mmm}]\n" or die $!;
78         }
79         $sep= "plot ";
80         for ($yn=1; $yn<=$#columns; $yn++) {
81             close "O$yn" or die $!;
82             $mmm[$yn] =~ m/^y2?$/ or die "$mmm[$yn]";
83             $axes= $mmm[$yn]; $axes =~ s/^y$/y1/;
84             $yoff= 1-$yn;
85             print S "$sep\\\n".
86                 " '$ofb,$cplot-$yn.gnuplot-data'".
87                     " axes x1$axes title '$columns[$yn]'"
88                         or die $!;
89             $sep= ',';
90         }
91         print S "\n\npause -1\n" or die $!;
92         close S or die $!;
93         print A "  gnuplot $ofb,$cplot.gnuplot-cmd <\$fi &\n" or die $!;
94         @mmm=@columns=();
95     } elsif ($c eq 'D') {
96         die unless @mmm;
97         @numbers= @s;
98         die unless @numbers == @columns;
99         for ($yn=0; $yn<=$#columns; $yn++) {
100             $_= $numbers[$yn];
101             $mmm= $mmm[$yn];
102             $min{$mmm}= $_ unless exists($min{$mmm}) && $min{$mmm} <= $_;
103             $max{$mmm}= $_ unless exists($max{$mmm}) && $max{$mmm} >= $_;
104             if ($yn) {
105                 printf {"O$yn"} "%s %s\n", $numbers[0], $_
106                     or die $!;
107             }
108         }
109     } elsif ($c eq 'F') {
110         last;
111     } else {
112         die;
113     }
114 }
115
116 print A <<END
117 exec 3>\$fi
118 printf 'hit return to quit: '
119 read
120 exec 3>&-
121 END
122     or die $!;
123 close A or die $!;
124
125 print ": generated ; $sof\n" or die $!;
126
127 # $Id: genspic2gnuplot,v 1.6 2007-09-21 21:21:15 ianmdlvl Exp $