Skip to content

Commit

Permalink
tidy PLplot device-reading
Browse files Browse the repository at this point in the history
  • Loading branch information
mohawk2 committed Mar 27, 2024
1 parent b53f3b2 commit 2f10940
Showing 1 changed file with 36 additions and 38 deletions.
74 changes: 36 additions & 38 deletions lib/PDL/Graphics/Simple/PLplot.pm
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use File::Temp qw/tempfile/;
use Time::HiRes qw/usleep/;
use PDL::Options q/iparse/;
use PDL;
use IPC::Open2;

our $mod = {
shortname => 'plplot',
Expand Down Expand Up @@ -59,17 +60,7 @@ sub check {
}

# Module loaded OK, now try to extract valid devices from it.
# We would use a pipe, but Microsoft Windows doesn't play nice
# with them - so we use a temp file instead.
my ($fh1, $gzinta) = tempfile('pgs_gzinta_XXXX');
close $fh1;
my ($fh2, $gzouta) = tempfile('pgs_gzouta_XXXX');
close $fh2;

open my $fh3, ">", $gzinta or die "Couldn't write to temp file";
print $fh3 "1\n"; #Just one line
close $fh3;

my @lines;
if($^O =~ /MSWin32/i) {
eval {require Win32::Process};
die "Win32::Process didn't load: $@"
Expand All @@ -79,55 +70,62 @@ sub check {
? "\\Windows\\system32\\cmd.exe"
: cmd_location();

# We would use a pipe, but Microsoft Windows doesn't play nice
# with them - so we use a temp file instead.
my ($fh1, $gzinta) = tempfile('pgs_gzinta_XXXX');
close $fh1;
my ($fh2, $gzouta) = tempfile('pgs_gzouta_XXXX');
close $fh2;

open my $fh3, ">", $gzinta or die "Couldn't write to temp file";
print $fh3 "1\n"; #Just one line
close $fh3;

Win32::Process::Create(
my $ProcessObj,
$cmd_location,
"cmd /c \"$^X -MPDL -MPDL::Graphics::PLplot -e \"PDL::Graphics::PLplot->new() \" <$gzinta >$gzouta\"",
"cmd /c \"$^X -MPDL -MPDL::Graphics::PLplot -e \"PDL::Graphics::PLplot->new(DEV=>'?')\" <$gzinta >$gzouta\"",
0,
32, #NORMAL_PRIORITY_CLASS
".")|| die ErrorReport();

sleep 1; # Don't read $gzouta before it's written
}
else {
my $pid = fork();
unless(defined($pid)) {
print STDERR +($mod->{msg} = "Fork failed in PLplot probe -- returning 0"), "\n";
return 0;
}

if( $pid==0 ) { # assignment
# Snarf up the file.
open my $fh, "<", $gzouta;
@lines = <$fh>;
close $fh;

# Daughter: try to create a PLplot window with a bogus device, to stimulate a driver listing
open STDOUT,">", $gzouta;
open STDERR,">&", STDOUT;
open STDIN, "<", $gzinta;
PDL::Graphics::PLplot->new(DEV=>'?');
exit(0);
unlink $gzinta;
unlink $gzouta;
}
else {
my $pid;
@lines = eval {
$pid = open2(my $chld_out, my $chld_in, $^X, qw(-MPDL -MPDL::Graphics::PLplot -e PDL::Graphics::PLplot->new(DEV=>'?')));
print $chld_in "1\n";
close $chld_in;
<$chld_out>;
};
if ($@) {
$mod->{ok} = 0;
$mod->{msg} = $@;
return 0;
}

# Parent - snarf up the results from the daughter
usleep(2e5); # hang around for 0.2 seconds
eval {kill 9,$pid;}; # kill it dead, just in case it buzzed or hung (I'm looking at you, Microsoft Windows)
waitpid($pid,0); # Clean up.
}

# Snarf up the file.
open my $fh4, "<", $gzouta;
my @lines = <$fh4>;
close $fh4;

unlink $gzinta;
unlink $gzouta;

$mod->{devices} = {};
for my $l(@lines) {
if( $l=~ m/^\s+\<\s*\d+\>\s+(\w+)/ ) {
for my $l (@lines) {
if ( $l=~ m/^\s+\<\s*\d+\>\s+(\w+)/ ) {
$mod->{devices}->{$1} = 1;
}
}

if( my ($good_dev) = grep $mod->{devices}{$_}, @DEVICES ) {
if ( my ($good_dev) = grep $mod->{devices}{$_}, @DEVICES ) {
$mod->{disp_dev} = $good_dev;
} else {
$mod->{ok} = 0;
Expand Down

0 comments on commit 2f10940

Please sign in to comment.