| 1 | package Gimp::ScriptFu::Client; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | our $VERSION = '1.01'; | |||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use Cwd; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use IO::Socket; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use Getopt::Long; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use Text::Template; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use Filter::Simple; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 2 | ▶ | FILTER { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=15 SCAR=42.1
| |||||||||||||||||||||||||||||
| 14 | 2 | my $script = $_; | |||||||||||||||||||||||||||
| 15 | 2 | ▶ | $script =~ s/\r\n/\n/g if $^O =~ /win32/i; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | # defaults | ||||||||||||||||||||||||||||
| 18 | 2 | my $peer_host = "localhost"; | |||||||||||||||||||||||||||
| 19 | 2 | my $peer_port = 10008; # Gimp default | |||||||||||||||||||||||||||
| 20 | 2 | my $verbose = 0; | |||||||||||||||||||||||||||
| 21 | 2 | my $syntax = 0; | |||||||||||||||||||||||||||
| 22 | 2 | my $include = ''; | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 2 | Getopt::Long::Configure("bundling"); | |||||||||||||||||||||||||||
| 25 | 2 | GetOptions( "server|s=s" => \$peer_host, # alternate server | |||||||||||||||||||||||||||
| 26 | "port|p=i" => \$peer_port, # and/or port | ||||||||||||||||||||||||||||
| 27 | "verbose|v" => \$verbose, # display Scheme before sending request | ||||||||||||||||||||||||||||
| 28 | "check|c" => \$syntax, # generate Scheme for syntax check and exit | ||||||||||||||||||||||||||||
| 29 | "include=s" => \$include, # return for inclusion in parent Scheme/Perl file | ||||||||||||||||||||||||||||
| 30 | # for internal use | ||||||||||||||||||||||||||||
| 31 | ); | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | # preprocess Scheme code using Perl | ||||||||||||||||||||||||||||
| 34 | my $scheme = Text::Template::fill_in_string( | ||||||||||||||||||||||||||||
| 35 | $script, | ||||||||||||||||||||||||||||
| 36 | PACKAGE => 'PERL_FRAGMENTS', | ||||||||||||||||||||||||||||
| 37 | BROKEN_ARG => $include || $0, | ||||||||||||||||||||||||||||
| 38 | 0 | ▶ | BROKEN => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 39 | 0 | my %args = @_; | |||||||||||||||||||||||||||
| 40 | 0 | $args{error} =~ s/at template line/at $args{arg} line/; | |||||||||||||||||||||||||||
| 41 | 0 | print STDERR "ERROR: Perl fragment: ", $args{error}; | |||||||||||||||||||||||||||
| 42 | 0 | exit 1; # instead of 'die' in case include is being eval'd | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | 2 | ▶ | ); | ||||||||||||||||||||||||||
|
Condition33%1/3
$include || $0
MC/DC0%0/2
$include || $0
$include
$0
Truth table33%1/3
A || B
A= $includeB= $0
| |||||||||||||||||||||||||||||
| 45 | 2 | ▶ | if ($include) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 0 | unshift @ARGV, $scheme; | |||||||||||||||||||||||||||
| 47 | 0 | return; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | 2 | ▶ | print $scheme if $verbose; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 2 | my $length = length($scheme); | |||||||||||||||||||||||||||
| 51 | 2 | ▶ | die "ERROR: script is too long for one server request: $length > 65535\n" if $length > 65535; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 2 | ▶ | if ($syntax) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 1 | print STDERR "$0 syntax check done\n"; | |||||||||||||||||||||||||||
| 54 | 1 | exit 0; | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | # connect to the Gimp ScriptFu server | ||||||||||||||||||||||||||||
| 58 | 1 | ▶ | my $gimp = IO::Socket::INET->new( Proto => "tcp", | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | PeerHost => $peer_host, | ||||||||||||||||||||||||||||
| 60 | PeerPort => $peer_port, | ||||||||||||||||||||||||||||
| 61 | ) or die "ERROR: can't connect to server at $peer_host:$peer_port\n"; | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | # request | ||||||||||||||||||||||||||||
| 64 | 0 | my $header = pack( 'an', 'G', $length ); | |||||||||||||||||||||||||||
| 65 | 0 | syswrite( $gimp, $_ ) for ( $header, $scheme ); | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | # wait for response | ||||||||||||||||||||||||||||
| 68 | 0 | my $rin = ''; | |||||||||||||||||||||||||||
| 69 | 0 | vec( $rin, fileno($gimp), 1 ) = 1; | |||||||||||||||||||||||||||
| 70 | 0 | select( $rin, undef, undef, undef ); # wait (forever) for response start | |||||||||||||||||||||||||||
| 71 | 0 | select( undef, undef, undef, .1 ); # wait a bit for response to finish | |||||||||||||||||||||||||||
| 72 | # increase wait if INVALID/INCOMPLETE RESPONSE occurs | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | # response | ||||||||||||||||||||||||||||
| 75 | 0 | ▶ | $length = sysread( $gimp, $header, 4 ) or die "INVALID RESPONSE: empty response\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 0 | ▶ | ( $length == 4 and $header =~ /^G/ ) or die "INVALID RESPONSE: bad header\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$length == 4 and $header =~ /^G/
MC/DC0%0/2
$length == 4 and $header =~ /^G/
$length == 4
$header =~ /^G/
Truth table0%0/3
A and B
A= $length == 4B= $header =~ /^G/
| |||||||||||||||||||||||||||||
| 77 | 0 | my $status; | |||||||||||||||||||||||||||
| 78 | 0 | ( $status, $length ) = unpack( 'xCn', $header ); | |||||||||||||||||||||||||||
| 79 | 0 | my $response; | |||||||||||||||||||||||||||
| 80 | 0 | ▶ | ( sysread( $gimp, $response, $length ) == $length ) or die "INCOMPLETE RESPONSE: $response\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | # convert user generated "Success" message to no error status | ||||||||||||||||||||||||||||
| 83 | 0 | ▶ | if ( $status and $response =~ /^Error: Success\n/i ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$status and $response =~ /^Error: Success\n/i
MC/DC0%0/2
$status and $response =~ /^Error: Success\n/i
$status
$response =~ /^Error: Success\n/i
Truth table0%0/3
A and B
A= $statusB= $response =~ /^Error: Success\n/i
| |||||||||||||||||||||||||||||
| 84 | 0 | $response =~ s/^Error: Success\n//i; | |||||||||||||||||||||||||||
| 85 | 0 | $status = 0; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | 0 | print $response; | |||||||||||||||||||||||||||
| 88 | 0 | exit $status; | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | package PERL_FRAGMENTS; | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | # helper functions | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | # make Scheme expression from a quoted Perl list | ||||||||||||||||||||||||||||
| 95 | 0 | ▶ | sub sexp_from_list { | ||||||||||||||||||||||||||
|
Subroutine
sexp_from_list: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 96 | 0 | "(" . join( " ", map { qq("$_") } @_ ) . ")"; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | # make set! expression for Scheme variable argv = Perl @ARGV | ||||||||||||||||||||||||||||
| 100 | 0 | ▶ | sub set_argv { | ||||||||||||||||||||||||||
|
Subroutine
set_argv: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 101 | 0 | "(set! argv '" . sexp_from_list(@ARGV) . ")"; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | # expand a Perl list of filenames/patterns by globbing patterns | ||||||||||||||||||||||||||||
| 105 | # and adding full pathnames. Dies on a non-existant filename. | ||||||||||||||||||||||||||||
| 106 | 0 | ▶ | sub expand_files { | ||||||||||||||||||||||||||
|
Subroutine
expand_files: not called CC=4 SCAR=25.3
| |||||||||||||||||||||||||||||
| 107 | 0 | my @pats = @_; | |||||||||||||||||||||||||||
| 108 | 0 | ▶ | @pats = map { /^[^"].* / ? "\"$_\"" : $_ } @pats | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 109 | if $^O =~ /win32/i; # re-quote names w/spaces | ||||||||||||||||||||||||||||
| 110 | 0 | ▶ | return map { $_ = Cwd::realpath($_); s|\\|/|g if $^O =~ /win32/i; $_ } glob "@pats"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | # include another Scheme/Perl script into this one, processing any | ||||||||||||||||||||||||||||
| 114 | # Perl first. @ARGV in the current script is preserved and arguments | ||||||||||||||||||||||||||||
| 115 | # for the included script are placed in its @ARGV | ||||||||||||||||||||||||||||
| 116 | 0 | ▶ | sub include_script { | ||||||||||||||||||||||||||
|
Subroutine
include_script: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 117 | 0 | my @save_argv = @ARGV; | |||||||||||||||||||||||||||
| 118 | 0 | my $file = shift; | |||||||||||||||||||||||||||
| 119 | 0 | @ARGV = ( '--include', $file, '--', @_ ); | |||||||||||||||||||||||||||
| 120 | 0 | do $file; | |||||||||||||||||||||||||||
| 121 | 0 | my $included = shift @ARGV; | |||||||||||||||||||||||||||
| 122 | 0 | @ARGV = @save_argv; | |||||||||||||||||||||||||||
| 123 | 0 | return $included; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | 2 | __END__ | |||||||||||||||||||||||||||