| 1 | ## XrtUtils.pm is a sub-module of Graph.pm. It has all the subroutines | ||||||||||||||||||
| 2 | ## needed for the Xrt3d part of the package. | ||||||||||||||||||
| 3 | ## | ||||||||||||||||||
| 4 | ## $Id: XrtUtils.pm,v 1.13 2006/06/07 21:09:33 emile Exp $ $Name: $ | ||||||||||||||||||
| 5 | ## | ||||||||||||||||||
| 6 | ## This software product is developed by Michael Young and David Moore, | ||||||||||||||||||
| 7 | ## and copyrighted(C) 1998 by the University of California, San Diego | ||||||||||||||||||
| 8 | ## (UCSD), with all rights reserved. UCSD administers the CAIDA grant, | ||||||||||||||||||
| 9 | ## NCR-9711092, under which part of this code was developed. | ||||||||||||||||||
| 10 | ## | ||||||||||||||||||
| 11 | ## There is no charge for this software. You can redistribute it and/or | ||||||||||||||||||
| 12 | ## modify it under the terms of the GNU General Public License, v. 2 dated | ||||||||||||||||||
| 13 | ## June 1991 which is incorporated by reference herein. This software is | ||||||||||||||||||
| 14 | ## distributed WITHOUT ANY WARRANTY, IMPLIED OR EXPRESS, OF MERCHANTABILITY | ||||||||||||||||||
| 15 | ## OR FITNESS FOR A PARTICULAR PURPOSE or that the use of it will not | ||||||||||||||||||
| 16 | ## infringe on any third party's intellectual property rights. | ||||||||||||||||||
| 17 | ## | ||||||||||||||||||
| 18 | ## You should have received a copy of the GNU GPL along with this program. | ||||||||||||||||||
| 19 | ## | ||||||||||||||||||
| 20 | ## | ||||||||||||||||||
| 21 | ## IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY | ||||||||||||||||||
| 22 | ## PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL | ||||||||||||||||||
| 23 | ## DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS | ||||||||||||||||||
| 24 | ## SOFTWARE, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF | ||||||||||||||||||
| 25 | ## THE POSSIBILITY OF SUCH DAMAGE. | ||||||||||||||||||
| 26 | ## | ||||||||||||||||||
| 27 | ## THE SOFTWARE PROVIDED HEREIN IS ON AN "AS IS" BASIS, AND THE | ||||||||||||||||||
| 28 | ## UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, | ||||||||||||||||||
| 29 | ## SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. THE UNIVERSITY | ||||||||||||||||||
| 30 | ## OF CALIFORNIA MAKES NO REPRESENTATIONS AND EXTENDS NO WARRANTIES | ||||||||||||||||||
| 31 | ## OF ANY KIND, EITHER IMPLIED OR EXPRESS, INCLUDING, BUT NOT LIMITED | ||||||||||||||||||
| 32 | ## TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A | ||||||||||||||||||
| 33 | ## PARTICULAR PURPOSE, OR THAT THE USE OF THE SOFTWARE WILL NOT INFRINGE | ||||||||||||||||||
| 34 | ## ANY PATENT, TRADEMARK OR OTHER RIGHTS. | ||||||||||||||||||
| 35 | ## | ||||||||||||||||||
| 36 | ## | ||||||||||||||||||
| 37 | ## Contact: graph-dev@caida.org | ||||||||||||||||||
| 38 | ## | ||||||||||||||||||
| 39 | ## | ||||||||||||||||||
| 40 | package Chart::Graph::XrtUtils; | ||||||||||||||||||
| 41 | 4 | ▶ | use Exporter (); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 42 | |||||||||||||||||||
| 43 | 4 | @ISA = qw(Exporter); | |||||||||||||||||
| 44 | 4 | @EXPORT = qw(); | |||||||||||||||||
| 45 | 4 | %EXPORT_TAGS = (UTILS => [qw(&_set_xrtpaths &_set_ldpath &_print_matrix | |||||||||||||||||
| 46 | &_print_array &_verify_ticks &_exec_xrt3d &_exec_xrt2d | ||||||||||||||||||
| 47 | &_exec_netpbm &_exec_xvfb &_try_port &_convert_raster | ||||||||||||||||||
| 48 | &_childpid_dead &_transfer_file)], | ||||||||||||||||||
| 49 | ); | ||||||||||||||||||
| 50 | |||||||||||||||||||
| 51 | 4 | Exporter::export_ok_tags('UTILS'); | |||||||||||||||||
| 52 | |||||||||||||||||||
| 53 | 4 | ▶ | use Carp; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 54 | 4 | ▶ | use POSIX ":sys_wait_h"; # for waitpid() | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 55 | 4 | ▶ | use Chart::Graph::Utils qw(:UTILS); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 56 | |||||||||||||||||||
| 57 | 4 | $cvs_Id = '$Id: XrtUtils.pm,v 1.13 2006/06/07 21:09:33 emile Exp $'; | |||||||||||||||||
| 58 | 4 | $cvs_Author = '$Author: emile $'; | |||||||||||||||||
| 59 | 4 | $cvs_Name = '$Name: $'; | |||||||||||||||||
| 60 | 4 | $cvs_Revision = '$Revision: 1.13 $'; | |||||||||||||||||
| 61 | |||||||||||||||||||
| 62 | 4 | $VERSION = 3.2; | |||||||||||||||||
| 63 | |||||||||||||||||||
| 64 | 4 | ▶ | use strict; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 65 | 4 | ▶ | use File::Basename; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 66 | |||||||||||||||||||
| 67 | 4 | ▶ | use vars qw($xrt2d $xrt3d); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 68 | |||||||||||||||||||
| 69 | # | ||||||||||||||||||
| 70 | # Subroutine: set_converterpaths() | ||||||||||||||||||
| 71 | # | ||||||||||||||||||
| 72 | # Description: set paths for converter programs in particular. This | ||||||||||||||||||
| 73 | # subroutine can take one or two arguments and tests if | ||||||||||||||||||
| 74 | # the required converter programs are indeed available | ||||||||||||||||||
| 75 | # for the choosen method to convert a file from one | ||||||||||||||||||
| 76 | # graphics format to another. | ||||||||||||||||||
| 77 | # | ||||||||||||||||||
| 78 | 0 | ▶ | sub _set_converterpaths { | ||||||||||||||||
|
Subroutine
_set_converterpaths: not called CC=4 SCAR=30.0
| |||||||||||||||||||
| 79 | 0 | my @converters = @_; | |||||||||||||||||
| 80 | |||||||||||||||||||
| 81 | # Loop through the list of converter seeing which are available | ||||||||||||||||||
| 82 | 0 | foreach my $converter (@converters) { | |||||||||||||||||
| 83 | 0 | ▶ | if (not -x $$converter) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 84 | 0 | ▶ | if (not $$converter = _get_path($$converter)) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 85 | 0 | return(0); | |||||||||||||||||
| 86 | } | ||||||||||||||||||
| 87 | } | ||||||||||||||||||
| 88 | } | ||||||||||||||||||
| 89 | 0 | return(1); | |||||||||||||||||
| 90 | } | ||||||||||||||||||
| 91 | |||||||||||||||||||
| 92 | # | ||||||||||||||||||
| 93 | # Subroutine: _convert_raster($plot_file, $output_file) | ||||||||||||||||||
| 94 | # | ||||||||||||||||||
| 95 | # Description: A subroutine to over see the conversion process from | ||||||||||||||||||
| 96 | # one raster graphic format to another. It will try | ||||||||||||||||||
| 97 | # ImageMagick convert first and if that fails try Netpbm | ||||||||||||||||||
| 98 | # utilities if they are available in that format. | ||||||||||||||||||
| 99 | # | ||||||||||||||||||
| 100 | 0 | ▶ | sub _convert_raster { | ||||||||||||||||
|
Subroutine
_convert_raster: not called CC=6 SCAR=37.4
| |||||||||||||||||||
| 101 | 0 | my $FORMAT = shift; | |||||||||||||||||
| 102 | 0 | my $plot_file = shift; | |||||||||||||||||
| 103 | 0 | my $output_file = shift; | |||||||||||||||||
| 104 | |||||||||||||||||||
| 105 | # First try ImageMagick as it is more robust and simpler | ||||||||||||||||||
| 106 | 0 | ▶ | if (_set_converterpaths(\$convert)) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 107 | 0 | ▶ | if (_exec_convert($convert, $FORMAT, $plot_file, $output_file)) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 108 | 0 | return(1); | |||||||||||||||||
| 109 | } else { | ||||||||||||||||||
| 110 | 0 | carp "Attempt to use ImageMagick failed, will try Netpbm." | |||||||||||||||||
| 111 | } | ||||||||||||||||||
| 112 | } else { | ||||||||||||||||||
| 113 | 0 | carp "No ImageMagick found, will try Netpbm." | |||||||||||||||||
| 114 | } | ||||||||||||||||||
| 115 | |||||||||||||||||||
| 116 | 0 | ▶ | if ($FORMAT eq 'GIF') { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 117 | 0 | _try_netpbm_combo($xwdtopnm, $ppmtogif, $plot_file, $output_file); | |||||||||||||||||
| 118 | } | ||||||||||||||||||
| 119 | elsif ($FORMAT eq 'JPG') { | ||||||||||||||||||
| 120 | 0 | _try_netpbm_combo($xwdtopnm, $ppmtojpg, $plot_file, $output_file); | |||||||||||||||||
| 121 | } | ||||||||||||||||||
| 122 | elsif ($FORMAT eq 'PNG') { | ||||||||||||||||||
| 123 | 0 | _try_netpbm_combo($xwdtopnm, $pnmtopng, $plot_file, $output_file); | |||||||||||||||||
| 124 | } else { | ||||||||||||||||||
| 125 | 0 | carp "Untrapped raster image format - XrtUtils.pm internal error"; | |||||||||||||||||
| 126 | 0 | return(0); | |||||||||||||||||
| 127 | } | ||||||||||||||||||
| 128 | } | ||||||||||||||||||
| 129 | |||||||||||||||||||
| 130 | # | ||||||||||||||||||
| 131 | # Subroutine _try_netpbm_combo($xwdtopbm, pbmtotarget, $xwd_file, $target_file) | ||||||||||||||||||
| 132 | # | ||||||||||||||||||
| 133 | # | ||||||||||||||||||
| 134 | # Description: Contains the logic for testing if a combination of | ||||||||||||||||||
| 135 | # netpbm programs can be accessed and executed to perform | ||||||||||||||||||
| 136 | # the desired conversion. If not, it produces the | ||||||||||||||||||
| 137 | # appropriate error messages. Basically, it saves a | ||||||||||||||||||
| 138 | # batch of conditional statements that would otherwise be | ||||||||||||||||||
| 139 | # repeated. | ||||||||||||||||||
| 140 | # | ||||||||||||||||||
| 141 | 0 | ▶ | sub _try_netpbm_combo { | ||||||||||||||||
|
Subroutine
_try_netpbm_combo: not called CC=3 SCAR=24.8
| |||||||||||||||||||
| 142 | 0 | my ($xwdtopbm, $pbmtotarget, $xwd_file, $target_file) = @_; | |||||||||||||||||
| 143 | |||||||||||||||||||
| 144 | 0 | ▶ | if (_set_converterpaths(\$xwdtopbm, \$pbmtotarget)) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 145 | 0 | ▶ | if (_exec_netpbm($xwdtopbm, $pbmtotarget, $xwd_file, $target_file)) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 146 | 0 | return(1); | |||||||||||||||||
| 147 | } else { | ||||||||||||||||||
| 148 | 0 | carp "Failure to execute any suitable image " . | |||||||||||||||||
| 149 | "converters for create file: $target_file"; | ||||||||||||||||||
| 150 | 0 | return(0); | |||||||||||||||||
| 151 | } | ||||||||||||||||||
| 152 | } else { | ||||||||||||||||||
| 153 | 0 | carp "Unable to find any suitable image converters to " . | |||||||||||||||||
| 154 | "create file: $target_file"; | ||||||||||||||||||
| 155 | 0 | return(0); | |||||||||||||||||
| 156 | } | ||||||||||||||||||
| 157 | } | ||||||||||||||||||
| 158 | |||||||||||||||||||
| 159 | # | ||||||||||||||||||
| 160 | # Subroutine: set_xrtpaths() | ||||||||||||||||||
| 161 | # | ||||||||||||||||||
| 162 | # Description: set paths for external programs required by xrt() | ||||||||||||||||||
| 163 | # if they are not defined already | ||||||||||||||||||
| 164 | # | ||||||||||||||||||
| 165 | 6 | ▶ | sub _set_xrtpaths { | ||||||||||||||||
|
Subroutine
_set_xrtpaths: called CC=10 SCAR=34.1
| |||||||||||||||||||
| 166 | |||||||||||||||||||
| 167 | 6 | my $xrtver = shift; | |||||||||||||||||
| 168 | |||||||||||||||||||
| 169 | |||||||||||||||||||
| 170 | |||||||||||||||||||
| 171 | 6 | ▶ | if (defined($xrtver)) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 172 | 6 | ▶ | if ($xrtver eq "xrt2d") { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 173 | 2 | ▶ | if (not $Chart::Graph::xrt2d = _get_path("xrt2d")) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 174 | 2 | return 0; | |||||||||||||||||
| 175 | } | ||||||||||||||||||
| 176 | } | ||||||||||||||||||
| 177 | |||||||||||||||||||
| 178 | 4 | ▶ | if ($xrtver eq "xrt3d") { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 179 | 4 | ▶ | if (not $Chart::Graph::xrt3d = _get_path("xrt3d")) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 180 | 4 | return 0; | |||||||||||||||||
| 181 | } | ||||||||||||||||||
| 182 | } | ||||||||||||||||||
| 183 | } | ||||||||||||||||||
| 184 | |||||||||||||||||||
| 185 | 0 | ▶ | if (not defined($xwdtopnm)) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 186 | 0 | ▶ | if (!($xwdtopnm = _get_path("xwdtopnm"))) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 187 | 0 | return 0; | |||||||||||||||||
| 188 | } | ||||||||||||||||||
| 189 | } | ||||||||||||||||||
| 190 | |||||||||||||||||||
| 191 | 0 | ▶ | if (not defined($xvfb)) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 192 | 0 | ▶ | if (not $xvfb = _get_path("Xvfb")) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 193 | 0 | return 0; | |||||||||||||||||
| 194 | } | ||||||||||||||||||
| 195 | } | ||||||||||||||||||
| 196 | |||||||||||||||||||
| 197 | # make sure /usr/dt/lib is in the library path | ||||||||||||||||||
| 198 | 0 | _set_ldpath("/usr/dt/lib"); | |||||||||||||||||
| 199 | |||||||||||||||||||
| 200 | 0 | return 1; | |||||||||||||||||
| 201 | } | ||||||||||||||||||
| 202 | |||||||||||||||||||
| 203 | # | ||||||||||||||||||
| 204 | # Subroutine: set_ldpath() | ||||||||||||||||||
| 205 | # | ||||||||||||||||||
| 206 | # Description: Xvfb has trouble finding libMrm, so we have to add | ||||||||||||||||||
| 207 | # /usr/dt/lib to LD_LIBRARY_PATH | ||||||||||||||||||
| 208 | # | ||||||||||||||||||
| 209 | |||||||||||||||||||
| 210 | 1 | ▶ | sub _set_ldpath { | ||||||||||||||||
|
Subroutine
_set_ldpath: called CC=4 SCAR=21.1
| |||||||||||||||||||
| 211 | 1 | my ($libpath) = @_; | |||||||||||||||||
| 212 | |||||||||||||||||||
| 213 | 1 | ▶ | if (not defined($ENV{LD_LIBRARY_PATH})) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 214 | 1 | $ENV{LD_LIBRARY_PATH} = "$libpath"; | |||||||||||||||||
| 215 | 1 | return 1; | |||||||||||||||||
| 216 | } | ||||||||||||||||||
| 217 | |||||||||||||||||||
| 218 | 0 | my @ldpath = split (/:/, $ENV{LD_LIBRARY_PATH}); | |||||||||||||||||
| 219 | |||||||||||||||||||
| 220 | # make sure library path isn't already defined | ||||||||||||||||||
| 221 | 0 | foreach my $i(@ldpath){ | |||||||||||||||||
| 222 | 0 | ▶ | if ($i eq $libpath) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 223 | 0 | return 1; | |||||||||||||||||
| 224 | } | ||||||||||||||||||
| 225 | } | ||||||||||||||||||
| 226 | |||||||||||||||||||
| 227 | # add library path to LD_LIBRARY_PATH | ||||||||||||||||||
| 228 | 0 | $ENV{LD_LIBRARY_PATH} = "$libpath:$ENV{LD_LIBRARY_PATH}"; | |||||||||||||||||
| 229 | 0 | return 1; | |||||||||||||||||
| 230 | } | ||||||||||||||||||
| 231 | |||||||||||||||||||
| 232 | # | ||||||||||||||||||
| 233 | # Subroutine: print_matrix() | ||||||||||||||||||
| 234 | # | ||||||||||||||||||
| 235 | # Description: print out all the elements | ||||||||||||||||||
| 236 | # in a X by Y matrix, row by row | ||||||||||||||||||
| 237 | # | ||||||||||||||||||
| 238 | |||||||||||||||||||
| 239 | 0 | ▶ | sub _print_matrix { | ||||||||||||||||
|
Subroutine
_print_matrix: not called CC=3 SCAR=24.8
| |||||||||||||||||||
| 240 | 0 | my ($handle, @matrix) = @_; | |||||||||||||||||
| 241 | |||||||||||||||||||
| 242 | 0 | foreach my $row (@matrix){ | |||||||||||||||||
| 243 | 0 | foreach my $i (@{$row}){ | |||||||||||||||||
| 244 | 0 | print $handle "$i\t"; | |||||||||||||||||
| 245 | } | ||||||||||||||||||
| 246 | 0 | print $handle "\n"; | |||||||||||||||||
| 247 | } | ||||||||||||||||||
| 248 | 0 | return 1; | |||||||||||||||||
| 249 | } | ||||||||||||||||||
| 250 | |||||||||||||||||||
| 251 | |||||||||||||||||||
| 252 | # | ||||||||||||||||||
| 253 | # Subroutine: _transfer_file($handle, $data_filename) | ||||||||||||||||||
| 254 | # | ||||||||||||||||||
| 255 | # Description: open file $data_filename. Read the contents | ||||||||||||||||||
| 256 | # and write it into the command file tab delimited. Don't | ||||||||||||||||||
| 257 | # assume data was tab delimited to be safe. | ||||||||||||||||||
| 258 | # | ||||||||||||||||||
| 259 | 0 | ▶ | sub _transfer_file { | ||||||||||||||||
|
Subroutine
_transfer_file: not called CC=5 SCAR=34.0
| |||||||||||||||||||
| 260 | 0 | my $handle = shift; | |||||||||||||||||
| 261 | 0 | my $data_filename = shift; | |||||||||||||||||
| 262 | 0 | my $data; | |||||||||||||||||
| 263 | my @elements; | ||||||||||||||||||
| 264 | |||||||||||||||||||
| 265 | 0 | ▶ | unless(open(DATAHDL, $data_filename)) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 266 | 0 | carp "Unable to open data file: $data_filename for reading"; | |||||||||||||||||
| 267 | 0 | return(0); | |||||||||||||||||
| 268 | } | ||||||||||||||||||
| 269 | 0 | ▶ | while (defined($data = <DATAHDL>)) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 270 | 0 | chomp($data); | |||||||||||||||||
| 271 | 0 | @elements = split(/\s+/, $data); | |||||||||||||||||
| 272 | 0 | foreach my $element (@elements) { | |||||||||||||||||
| 273 | 0 | print $handle $element, "\t"; | |||||||||||||||||
| 274 | } | ||||||||||||||||||
| 275 | 0 | print $handle "\n"; | |||||||||||||||||
| 276 | } | ||||||||||||||||||
| 277 | 0 | ▶ | unless(close(DATAHDL)) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 278 | 0 | carp "Unable to close data file: $data_filename after reading"; | |||||||||||||||||
| 279 | } | ||||||||||||||||||
| 280 | 0 | return(1); | |||||||||||||||||
| 281 | } | ||||||||||||||||||
| 282 | |||||||||||||||||||
| 283 | # | ||||||||||||||||||
| 284 | # Subroutine: print_array() | ||||||||||||||||||
| 285 | # | ||||||||||||||||||
| 286 | # Description: print out each element of array, one per line | ||||||||||||||||||
| 287 | # | ||||||||||||||||||
| 288 | |||||||||||||||||||
| 289 | 0 | ▶ | sub _print_array { | ||||||||||||||||
|
Subroutine
_print_array: not called CC=2 SCAR=17.9
| |||||||||||||||||||
| 290 | 0 | my ($handle, @array) = @_; | |||||||||||||||||
| 291 | 0 | my $i; | |||||||||||||||||
| 292 | |||||||||||||||||||
| 293 | 0 | foreach $i (@array) { | |||||||||||||||||
| 294 | 0 | print $handle "$i\n"; | |||||||||||||||||
| 295 | } | ||||||||||||||||||
| 296 | 0 | return 1; | |||||||||||||||||
| 297 | } | ||||||||||||||||||
| 298 | |||||||||||||||||||
| 299 | # | ||||||||||||||||||
| 300 | # Subroutine: verify_ticks(); | ||||||||||||||||||
| 301 | # | ||||||||||||||||||
| 302 | # Description: check that the number of tick labels is the same | ||||||||||||||||||
| 303 | # as the number of xy rows and columns. we can only have | ||||||||||||||||||
| 304 | # as many ticks as the number of rows or columns | ||||||||||||||||||
| 305 | # we make this subroutine so that the calling subroutine | ||||||||||||||||||
| 306 | # is kept cleaner. | ||||||||||||||||||
| 307 | |||||||||||||||||||
| 308 | 0 | ▶ | sub _verify_ticks { | ||||||||||||||||
|
Subroutine
_verify_ticks: not called CC=4 SCAR=30.0
| |||||||||||||||||||
| 309 | 0 | my ($cnt, $ticks_ref) = @_; | |||||||||||||||||
| 310 | |||||||||||||||||||
| 311 | # if no ticks are given then just | ||||||||||||||||||
| 312 | # give the xrt binary "1, 2,..." | ||||||||||||||||||
| 313 | 0 | ▶ | if (not defined($ticks_ref)) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 314 | 0 | my @def_ticks; | |||||||||||||||||
| 315 | 0 | ▶ | for (my $i = 0; $i < $cnt; $i++) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 316 | 0 | $def_ticks[$i] = $i + 1; | |||||||||||||||||
| 317 | } | ||||||||||||||||||
| 318 | 0 | $ticks_ref = \@def_ticks; | |||||||||||||||||
| 319 | } | ||||||||||||||||||
| 320 | |||||||||||||||||||
| 321 | 0 | my $tick_cnt = @{$ticks_ref}; | |||||||||||||||||
| 322 | |||||||||||||||||||
| 323 | 0 | ▶ | if ($cnt ne $tick_cnt){ | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 324 | 0 | carp "number of tick labels must equal the number of xy rows and columns"; | |||||||||||||||||
| 325 | 0 | return 0; | |||||||||||||||||
| 326 | } | ||||||||||||||||||
| 327 | 0 | return 1; | |||||||||||||||||
| 328 | } | ||||||||||||||||||
| 329 | |||||||||||||||||||
| 330 | # | ||||||||||||||||||
| 331 | # Subroutine: exec_xrt3d() | ||||||||||||||||||
| 332 | # | ||||||||||||||||||
| 333 | # Description: execute the xrt3d program on the command file. | ||||||||||||||||||
| 334 | # xrt3d generates a xwd file. | ||||||||||||||||||
| 335 | # | ||||||||||||||||||
| 336 | 0 | ▶ | sub _exec_xrt3d { | ||||||||||||||||
|
Subroutine
_exec_xrt3d: not called CC=4 SCAR=30.0
| |||||||||||||||||||
| 337 | 0 | my ($command_file) = @_; | |||||||||||||||||
| 338 | 0 | my ($output); | |||||||||||||||||
| 339 | 0 | my ($childpid, $port); | |||||||||||||||||
| 340 | 0 | my $display_env = $ENV{DISPLAY}; | |||||||||||||||||
| 341 | 0 | my $status; | |||||||||||||||||
| 342 | |||||||||||||||||||
| 343 | 0 | ▶ | if ($Chart::Graph::use_xvfb) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 344 | # start the virtual X server | ||||||||||||||||||
| 345 | 0 | ($childpid, $port) = _exec_xvfb(); | |||||||||||||||||
| 346 | 0 | $status = system("$Chart::Graph::xrt3d -display :$port.0 < $command_file"); | |||||||||||||||||
| 347 | } else { | ||||||||||||||||||
| 348 | # use the local X server | ||||||||||||||||||
| 349 | # warning: colors might be messed up | ||||||||||||||||||
| 350 | # depending on your current setup | ||||||||||||||||||
| 351 | 0 | $status = system("$Chart::Graph::xrt3d -display $display_env < $command_file"); | |||||||||||||||||
| 352 | } | ||||||||||||||||||
| 353 | |||||||||||||||||||
| 354 | #my $status = system("$xrt -display :$port.0 < $command_file"); | ||||||||||||||||||
| 355 | 0 | ▶ | if (not _chk_status($status)) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 356 | 0 | return 0; | |||||||||||||||||
| 357 | } | ||||||||||||||||||
| 358 | |||||||||||||||||||
| 359 | 0 | ▶ | if ($Chart::Graph::use_xvfb) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 360 | 0 | kill('KILL', $childpid); | |||||||||||||||||
| 361 | } | ||||||||||||||||||
| 362 | |||||||||||||||||||
| 363 | 0 | return 1; | |||||||||||||||||
| 364 | } | ||||||||||||||||||
| 365 | |||||||||||||||||||
| 366 | # | ||||||||||||||||||
| 367 | # Subroutine: exec_xrt2d() | ||||||||||||||||||
| 368 | # | ||||||||||||||||||
| 369 | # Description: execute the xrt2d program on the command file. | ||||||||||||||||||
| 370 | # xrt2d generates a xwd file. | ||||||||||||||||||
| 371 | # | ||||||||||||||||||
| 372 | 0 | ▶ | sub _exec_xrt2d { | ||||||||||||||||
|
Subroutine
_exec_xrt2d: not called CC=4 SCAR=30.0
| |||||||||||||||||||
| 373 | 0 | my ($command_file, $options) = @_; | |||||||||||||||||
| 374 | 0 | my ($output); | |||||||||||||||||
| 375 | 0 | my ($childpid, $port); | |||||||||||||||||
| 376 | 0 | my $display_env = $ENV{DISPLAY}; | |||||||||||||||||
| 377 | 0 | my $status; | |||||||||||||||||
| 378 | |||||||||||||||||||
| 379 | 0 | ▶ | if ($Chart::Graph::use_xvfb) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 380 | # start the virtual X server | ||||||||||||||||||
| 381 | 0 | ($childpid, $port) = _exec_xvfb(); | |||||||||||||||||
| 382 | 0 | printf STDERR "\tXRT is $Chart::Graph::xrt2d\n"; | |||||||||||||||||
| 383 | 0 | my $status = system("$Chart::Graph::xrt2d -display ipn:$port.0 < $command_file $options"); | |||||||||||||||||
| 384 | } else { | ||||||||||||||||||
| 385 | # use the local X server | ||||||||||||||||||
| 386 | # warning: colors might be messed up | ||||||||||||||||||
| 387 | # depending on your current setup | ||||||||||||||||||
| 388 | 0 | $status = system("$Chart::Graph::xrt2d -display $display_env < $command_file $options"); | |||||||||||||||||
| 389 | } | ||||||||||||||||||
| 390 | |||||||||||||||||||
| 391 | 0 | ▶ | if (not _chk_status($status)) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 392 | 0 | return 0; | |||||||||||||||||
| 393 | } | ||||||||||||||||||
| 394 | |||||||||||||||||||
| 395 | 0 | ▶ | if ($Chart::Graph::use_xvfb) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 396 | 0 | kill('KILL', $childpid); | |||||||||||||||||
| 397 | } | ||||||||||||||||||
| 398 | |||||||||||||||||||
| 399 | 0 | return 1; | |||||||||||||||||
| 400 | } | ||||||||||||||||||
| 401 | |||||||||||||||||||
| 402 | # | ||||||||||||||||||
| 403 | # Subroutine: exec_convert | ||||||||||||||||||
| 404 | # | ||||||||||||||||||
| 405 | # | ||||||||||||||||||
| 406 | # Description: Use the Imagemagick 'convert' utility to convert the xwd | ||||||||||||||||||
| 407 | # file into any one of the other common raster image | ||||||||||||||||||
| 408 | # formats used commonly in web page production. | ||||||||||||||||||
| 409 | # | ||||||||||||||||||
| 410 | 0 | ▶ | sub _exec_convert { | ||||||||||||||||
|
Subroutine
_exec_convert: not called CC=3 SCAR=24.8
| |||||||||||||||||||
| 411 | 0 | my ($convert, $FORMAT, $xwd_file, $target_file) = @_; | |||||||||||||||||
| 412 | 0 | my ($status); | |||||||||||||||||
| 413 | |||||||||||||||||||
| 414 | |||||||||||||||||||
| 415 | 0 | ▶ | if ($Chart::Graph::debug) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 416 | 0 | $status = system(join('', "$convert -verbose $xwd_file ", | |||||||||||||||||
| 417 | $FORMAT, ":$target_file")); | ||||||||||||||||||
| 418 | } else { | ||||||||||||||||||
| 419 | 0 | $status = system(join('', "( $convert $xwd_file ", $FORMAT, | |||||||||||||||||
| 420 | ":$target_file; ) 2> /dev/null")); | ||||||||||||||||||
| 421 | } | ||||||||||||||||||
| 422 | |||||||||||||||||||
| 423 | 0 | ▶ | if (not _chk_status($status)) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 424 | 0 | return 0; | |||||||||||||||||
| 425 | } | ||||||||||||||||||
| 426 | 0 | return 1; | |||||||||||||||||
| 427 | } | ||||||||||||||||||
| 428 | # | ||||||||||||||||||
| 429 | # Subroutine: _exec_netpbm | ||||||||||||||||||
| 430 | # | ||||||||||||||||||
| 431 | # | ||||||||||||||||||
| 432 | # Description: Convert a raster image using the older utilities now | ||||||||||||||||||
| 433 | # collected under the name 'netpbm.' Note that not all | ||||||||||||||||||
| 434 | # conversions are commonly included wiht all UNIX | ||||||||||||||||||
| 435 | # distributions so that while older conversions such as | ||||||||||||||||||
| 436 | # 'xwd' -> 'gif' are likely to work, others such as | ||||||||||||||||||
| 437 | # conversions to 'png' may not without downloading new | ||||||||||||||||||
| 438 | # software. | ||||||||||||||||||
| 439 | # | ||||||||||||||||||
| 440 | # The conversion strategy always involves a pipe from the | ||||||||||||||||||
| 441 | # X-windows 'xwd' format to some sort 'pbm' format and | ||||||||||||||||||
| 442 | # then from that universal format into the target format. | ||||||||||||||||||
| 443 | # For this reason, it is more prone to machine | ||||||||||||||||||
| 444 | # architecture issues and other errors. | ||||||||||||||||||
| 445 | # | ||||||||||||||||||
| 446 | 0 | ▶ | sub _exec_netpbm { | ||||||||||||||||
|
Subroutine
_exec_netpbm: not called CC=3 SCAR=24.8
| |||||||||||||||||||
| 447 | 0 | my ($xwdtopbm, $pbmtotarget, $xwd_file, $target_file) = @_; | |||||||||||||||||
| 448 | 0 | my ($status); | |||||||||||||||||
| 449 | |||||||||||||||||||
| 450 | 0 | ▶ | if ($Chart::Graph::debug) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 451 | 0 | $status = system("$xwdtopbm $xwd_file | $pbmtotarget > $target_file"); | |||||||||||||||||
| 452 | } else { | ||||||||||||||||||
| 453 | 0 | $status = system(join('', "( $xwdtopbm -quiet $xwd_file | ", | |||||||||||||||||
| 454 | "$pbmtotarget -quiet > $target_file; ) ", | ||||||||||||||||||
| 455 | "2> /dev/null")); | ||||||||||||||||||
| 456 | } | ||||||||||||||||||
| 457 | |||||||||||||||||||
| 458 | 0 | ▶ | if (not _chk_status($status)) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 459 | 0 | return 0; | |||||||||||||||||
| 460 | } | ||||||||||||||||||
| 461 | 0 | return 1; | |||||||||||||||||
| 462 | } | ||||||||||||||||||
| 463 | |||||||||||||||||||
| 464 | # | ||||||||||||||||||
| 465 | # Subroutine: exec_xvfb() | ||||||||||||||||||
| 466 | # | ||||||||||||||||||
| 467 | # Description: this starts the vitualX server(X is required by xrt, so | ||||||||||||||||||
| 468 | # we fake out xrt with Xvfb, for speed and compatability) | ||||||||||||||||||
| 469 | # | ||||||||||||||||||
| 470 | # | ||||||||||||||||||
| 471 | 0 | ▶ | sub _exec_xvfb { | ||||||||||||||||
|
Subroutine
_exec_xvfb: not called CC=5 SCAR=34.0
| |||||||||||||||||||
| 472 | 0 | my $port = 99; | |||||||||||||||||
| 473 | 0 | my $childpid; | |||||||||||||||||
| 474 | 0 | my $sleep_time = 1; | |||||||||||||||||
| 475 | 0 | my $try_count = 0; | |||||||||||||||||
| 476 | 0 | my $trialnumber; | |||||||||||||||||
| 477 | my $childpid_status; | ||||||||||||||||||
| 478 | |||||||||||||||||||
| 479 | # starting with port 100, we try to start | ||||||||||||||||||
| 480 | # the virtual server until we find an open port | ||||||||||||||||||
| 481 | # because of the nature of the virtual x server | ||||||||||||||||||
| 482 | # we use, in order to know if we have found an | ||||||||||||||||||
| 483 | # open port, we have to sleep. | ||||||||||||||||||
| 484 | # we check the pid of the virtual x process we started | ||||||||||||||||||
| 485 | # and see if it died or not. | ||||||||||||||||||
| 486 | |||||||||||||||||||
| 487 | 0 | ▶ | while ($childpid_status = _childpid_dead($childpid)) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 488 | 0 | $port++; | |||||||||||||||||
| 489 | 0 | $try_count++; | |||||||||||||||||
| 490 | 0 | ▶ | if ($try_count > 10) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 491 | 0 | die "Error: Failed too many times\n"; | |||||||||||||||||
| 492 | } | ||||||||||||||||||
| 493 | 0 | $trialnumber = _number_to_eng($try_count); | |||||||||||||||||
| 494 | 0 | ▶ | print STDERR "*** $trialnumber try ***" unless (not $Chart::Graph::debug); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 495 | 0 | $childpid = _try_port($port); | |||||||||||||||||
| 496 | 0 | sleep($sleep_time); | |||||||||||||||||
| 497 | } | ||||||||||||||||||
| 498 | 0 | ▶ | print STDERR " SUCCESS!!!\n" unless (not $Chart::Graph::debug); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 499 | |||||||||||||||||||
| 500 | # save the childpid so we can stop the virtual server later | ||||||||||||||||||
| 501 | # save the $port so we can tell xrt where the virtual server is. | ||||||||||||||||||
| 502 | 0 | return ($childpid, $port); | |||||||||||||||||
| 503 | } | ||||||||||||||||||
| 504 | # | ||||||||||||||||||
| 505 | # Subroutine: try_port(); | ||||||||||||||||||
| 506 | # | ||||||||||||||||||
| 507 | # Description: will try to start Xvfb on specified port | ||||||||||||||||||
| 508 | 0 | ▶ | sub _try_port { | ||||||||||||||||
|
Subroutine
_try_port: not called CC=6 SCAR=37.4
| |||||||||||||||||||
| 509 | |||||||||||||||||||
| 510 | 0 | my ($port) = @_; | |||||||||||||||||
| 511 | 0 | my ($childpid); | |||||||||||||||||
| 512 | |||||||||||||||||||
| 513 | #fork a process | ||||||||||||||||||
| 514 | 0 | ▶ | if (not defined($childpid = fork())){ | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 515 | # the fork failed | ||||||||||||||||||
| 516 | 0 | carp "cannot fork: $!"; | |||||||||||||||||
| 517 | 0 | return 0; | |||||||||||||||||
| 518 | } elsif ($childpid == 0) { | ||||||||||||||||||
| 519 | # we are in the child process | ||||||||||||||||||
| 520 | 0 | ▶ | if ($Chart::Graph::debug) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 521 | 0 | ▶ | if (not exec "$xvfb :$port") { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 522 | 0 | die "can't do $xvfb :$port: $!\n"; | |||||||||||||||||
| 523 | } | ||||||||||||||||||
| 524 | } | ||||||||||||||||||
| 525 | else { | ||||||||||||||||||
| 526 | 0 | ▶ | if (not exec "$xvfb :$port 2> /dev/null") { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 527 | 0 | die "can't do $xvfb :$port 2> /dev/null: $!\n"; | |||||||||||||||||
| 528 | } | ||||||||||||||||||
| 529 | } | ||||||||||||||||||
| 530 | |||||||||||||||||||
| 531 | 0 | die "should never reach here\n"; | |||||||||||||||||
| 532 | |||||||||||||||||||
| 533 | } else { | ||||||||||||||||||
| 534 | # we are in the parent, return the childpid | ||||||||||||||||||
| 535 | # so we can kill it later. | ||||||||||||||||||
| 536 | 0 | return $childpid; | |||||||||||||||||
| 537 | } | ||||||||||||||||||
| 538 | |||||||||||||||||||
| 539 | } | ||||||||||||||||||
| 540 | |||||||||||||||||||
| 541 | # | ||||||||||||||||||
| 542 | # Subroutine: childpid_dead | ||||||||||||||||||
| 543 | # | ||||||||||||||||||
| 544 | # Description: check to see if a PID has died or not | ||||||||||||||||||
| 545 | # | ||||||||||||||||||
| 546 | # | ||||||||||||||||||
| 547 | 0 | ▶ | sub _childpid_dead { | ||||||||||||||||
|
Subroutine
_childpid_dead: not called CC=2 SCAR=17.9
| |||||||||||||||||||
| 548 | 0 | my ($childpid) = @_; | |||||||||||||||||
| 549 | |||||||||||||||||||
| 550 | 0 | ▶ | if (not defined($childpid)) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 551 | 0 | return 1; | |||||||||||||||||
| 552 | } | ||||||||||||||||||
| 553 | |||||||||||||||||||
| 554 | # WNOHANG: waitpid() will not suspend execution of | ||||||||||||||||||
| 555 | # the calling process if status is not | ||||||||||||||||||
| 556 | # immediately available for one of the | ||||||||||||||||||
| 557 | # child processes specified by pid. | ||||||||||||||||||
| 558 | 0 | return waitpid($childpid, &WNOHANG); | |||||||||||||||||
| 559 | } | ||||||||||||||||||
| 560 | |||||||||||||||||||
| 561 | 4 | 1; | |||||||||||||||||