#!/usr/bin/perl -w use strict; use IO::Socket; use Getopt::Long; use Text::Template; sub sexp_from_list { "(" . join(" ", map { qq("$_") } @_) . ")"; } sub set_argv { "(set! argv '" . sexp_from_list(@ARGV) . ")"; } # defaults my $verbose = 0; my $peer_host = "localhost"; my $peer_port = 10008; GetOptions ( "server|s=s" => \$peer_host, "port|p=i" => \$peer_port, ); # connect to the gimp my $gimp = IO::Socket::INET->new ( Proto => "tcp", PeerHost => $peer_host, PeerPort => $peer_port, ); # preprocess scheme code using perl my $template; if (@ARGV) { $template = Text::Template->new ( TYPE => "FILE", SOURCE => shift ); } else { $template = Text::Template->new ( TYPE => "FILEHANDLE", SOURCE => \*STDIN ); } my $script_fu = $template->fill_in(); $script_fu =~ s/^#.*$//m; # request my $length = length($script_fu) & 0xffff; my $lo_byte = ($length & 0x00ff); my $hi_byte = ($length & 0xff00) >> 8; my $header = "G "; vec($header, 1, 8) = $hi_byte; vec($header, 2, 8) = $lo_byte; syswrite($gimp, $_) for ($header, $script_fu); # response sysread($gimp, $header, 4); my @byte = map { ord } split('', $header); $length = ($byte[2] << 8) | $byte[3]; read($gimp, my $response, $length); print "error | $byte[1]\n", "length | $length\n" if ($verbose); print $response, "\n"; exit $byte[1]; __END__ =head1 NAME gimp-request - send a request to GIMP's Script-Fu Server =head1 SYNOPSIS Syntax: $ gimp-request \ [--server=HOST][--port=PORT] \ [SCHEME_FILE] [ARGS]... Bang Notation: #!/usr/bin/env gimp-request (define beppu `("just" "another" { qw("script-fu") } "hacker")) =head1 DESCRIPTION This is a script for sending a request to a Script-Fu Server. Before the Scheme code is sent, it is preprocessed by Perl. Anything within a pair of curly braces will be treated as Perl, and you can use this facility to generate Scheme code. This is useful, because Perl has access to all sorts of data that the Scheme interpreter inside the GIMP does not. It is also safe, because the Perl code is only executed on the client-side and all the Script-Fu server will ever see is pure Scheme code. =head1 AUTHOR John BEPPU - beppu@ax9.org =cut