1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
|
# should be call by FvwmScript-ComExample as:
# perl [-w] fvwm-script-ComExample --com-name ComExample-pid
# where pid is the process id of FvwmScript-ComExample
use strict;
use Getopt::Long;
my $comName = "";
my $userHome = $ENV{'HOME'} || "./.";
my $userDir = $ENV{'FVWM_USERDIR'} || "$userHome/.fvwm";
my $scriptName = ($0 =~ m:([^/]+)$:, $1);
GetOptions(
"help|h" => \&showHelp,
"com-name=s" => \$comName,
) || showHelp();
showHelp() if ($comName eq "");
# the fifos for the communication with FvwmScript
my $outFifo = ".tmp-com-out-" . $comName;
my $inFifo = ".tmp-com-in-" . $comName;
# extract the the pid of FvwmScript-ComExample
my $comPid = $comName;
$comPid =~ s/ComExample-//;
showHelp() if ($comPid !~ /^\d+$/);
# startup stuff:
# nothing :o)
# go to the communication loop (never return)
comLoop();
#-------------------------------------------------------------------------------
# usage
sub showHelp {
print "\n";
print "$scriptName must be run by FvwmScript-ComExample as follow:\n\n";
print "\tperl $scriptName --com-name ComExample-pid\n\n";
print "where pid is the process id of FvwmScript-ComExample\n";
print "\n";
exit 0;
}
#-------------------------------------------------------------------------------
# the communication loop
sub comLoop {
my $command = "";
my $return = "";
my $count=0;
chdir($userDir) || die "No FvwmConfigHome $userDir";
# paranoia
unlink($outFifo) if -p "$outFifo";
unlink($inFifo) if -p "$inFifo";
while(1) {
# read the command and check every 10 sec if FvwmScript-ComExemple
# is still alive
myMakeFifo($inFifo) if ! -p "$inFifo";
eval {
local $SIG{ALRM} = \&checkScript;
alarm(10);
# block unless FvwmScript write on $inFifo
open(IN,"$inFifo") || die "cannot open $inFifo";
alarm(0);
# read the message
($command)=(<IN>);
chomp($command);
close(IN);
};
if ($@ =~ /^cannot/) {
print STDERR "$comName: cannot read in fifo $inFifo\n";
unlink("$inFifo") if -p "$inFifo";
exit(1);
}
if ($@ =~ /^NoScript/) {
print STDERR "$comName: No more FvwmScript-ComExample: exit!\n";
unlink("$inFifo") if -p "$inFifo";
exit(0);
}
if ($@ =~ /^Script/) {
next;
}
unlink($inFifo);
# build the answer
$return = "";
if ($command eq "startup")
{
$return = "fvwm-script-ComExample.pl perl script is up";
}
elsif ($command eq "count")
{
$count++;
$return = "Get $count count messages";
}
elsif ($command eq "multilines")
{
# return an answer ready for the FvwmScript Parse function
my @r = ();
$r[0] = "line1 (" . int(rand 20) . ")";
$r[1] = "Second Line (" . int(rand 20) . ")";
$r[2] = "An other Line (" . int(rand 20) . ")";
$return = buildAnswerForParse(@r);
}
# ...etc.
# --------------------------
elsif ($command eq "exit") {
exit(0);
}
else {
print STDERR "$comName: unknown command $command\n";
$return = "0";
}
# send the answer to FvwmScript, we wait 10 sec
myMakeFifo($outFifo);
eval {
local $SIG{ALRM} = sub { die "Timeout" };
alarm(10);
# this line block until FvwmScript take the answer
open(OUT,">$outFifo") || die "$comName: cannot write on fifo $outFifo";
alarm(0);
print OUT $return;
close(OUT);
};
if ($@ =~ /cannot/) {
print STDERR "$comName: cannot write on fifo $outFifo\n";
unlink($outFifo);
exit(1);
}
if ($@ =~ /Timeout/) {
print STDERR "$comName: FvwmScript do not read my answer!\n";
}
unlink($outFifo);
} # while
}
#-----------------------------------------------------------------------------
# multi-lines to one line for the Parse FvwmScript function
sub buildAnswerForParse {
my @r = @_;
my $out = "";
my $l = 0;
foreach (@r) {
$l = length($_);
$out .= "0" x (4 - length($l)) . "$l" . $_;
}
return $out;
}
#-----------------------------------------------------------------------------
# make a fifo
sub myMakeFifo {
my ($fifo) = @_;
system("mkfifo '$fifo'");
}
#----------------------------------------------------------------------------
# An alarm handler to check if FvwmScript-ComExample is still alive
sub checkScript {
die "Script" unless ($comPid);
my $test = 0;
$test = 1 if kill 0 => $comPid;
if ($test) { die "Script"; }
else {
unlink($outFifo) if -p "$outFifo";
unlink($inFifo) if -p "$inFifo";
die "NoScript";
}
}
#-----------------------------------------------------------------------------
# For killing the FvwmScript-ComExample if an error happen in this script
END {
if ($?) {
my $message = "$scriptName: internal error $?\n";
$message .= "\tOS error: $!\n" if $!;
# actually the following is never executed on unix
$message .= "\tOS error 2: $^E\n" if $^E && !($! && $^E eq $!);
unlink($outFifo) if -p "$outFifo";
unlink($inFifo) if -p "$inFifo";
if ($comPid) {
kill(9, $comPid);
$message .= "\tkilling FvwmScript-ComExample";
}
print STDERR "$message\n";
}
}
|