1 # should be call by FvwmScript-ComExample as:
2 # perl [-w] fvwm-script-ComExample --com-name ComExample-pid
3 # where pid is the process id of FvwmScript-ComExample
9 my $userHome = $ENV{'HOME'} || "./.";
10 my $userDir = $ENV{'FVWM_USERDIR'} || "$userHome/.fvwm";
11 my $scriptName = ($0 =~ m
:([^/]+)$:, $1);
14 "help|h" => \
&showHelp
,
15 "com-name=s" => \
$comName,
18 showHelp
() if ($comName eq "");
20 # the fifos for the communication with FvwmScript
21 my $outFifo = ".tmp-com-out-" . $comName;
22 my $inFifo = ".tmp-com-in-" . $comName;
24 # extract the the pid of FvwmScript-ComExample
25 my $comPid = $comName;
26 $comPid =~ s/ComExample-//;
27 showHelp
() if ($comPid !~ /^\d+$/);
32 # go to the communication loop (never return)
35 #-------------------------------------------------------------------------------
39 print "$scriptName must be run by FvwmScript-ComExample as follow:\n\n";
40 print "\tperl $scriptName --com-name ComExample-pid\n\n";
41 print "where pid is the process id of FvwmScript-ComExample\n";
46 #-------------------------------------------------------------------------------
47 # the communication loop
53 chdir($userDir) || die "No FvwmConfigHome $userDir";
55 unlink($outFifo) if -p
"$outFifo";
56 unlink($inFifo) if -p
"$inFifo";
59 # read the command and check every 10 sec if FvwmScript-ComExemple
61 myMakeFifo
($inFifo) if ! -p
"$inFifo";
63 local $SIG{ALRM
} = \
&checkScript
;
65 # block unless FvwmScript write on $inFifo
66 open(IN
,"$inFifo") || die "cannot open $inFifo";
73 if ($@
=~ /^cannot/) {
74 print STDERR
"$comName: cannot read in fifo $inFifo\n";
75 unlink("$inFifo") if -p
"$inFifo";
78 if ($@
=~ /^NoScript/) {
79 print STDERR
"$comName: No more FvwmScript-ComExample: exit!\n";
80 unlink("$inFifo") if -p
"$inFifo";
83 if ($@
=~ /^Script/) {
91 if ($command eq "startup")
93 $return = "fvwm-script-ComExample.pl perl script is up";
95 elsif ($command eq "count")
98 $return = "Get $count count messages";
100 elsif ($command eq "multilines")
102 # return an answer ready for the FvwmScript Parse function
104 $r[0] = "line1 (" . int(rand 20) . ")";
105 $r[1] = "Second Line (" . int(rand 20) . ")";
106 $r[2] = "An other Line (" . int(rand 20) . ")";
107 $return = buildAnswerForParse
(@r);
110 # --------------------------
111 elsif ($command eq "exit") {
115 print STDERR
"$comName: unknown command $command\n";
119 # send the answer to FvwmScript, we wait 10 sec
120 myMakeFifo
($outFifo);
122 local $SIG{ALRM
} = sub { die "Timeout" };
124 # this line block until FvwmScript take the answer
125 open(OUT
,">$outFifo") || die "$comName: cannot write on fifo $outFifo";
130 if ($@
=~ /cannot/) {
131 print STDERR
"$comName: cannot write on fifo $outFifo\n";
135 if ($@
=~ /Timeout/) {
136 print STDERR
"$comName: FvwmScript do not read my answer!\n";
142 #-----------------------------------------------------------------------------
143 # multi-lines to one line for the Parse FvwmScript function
144 sub buildAnswerForParse
{
151 $out .= "0" x
(4 - length($l)) . "$l" . $_;
156 #-----------------------------------------------------------------------------
160 system("mkfifo '$fifo'");
163 #----------------------------------------------------------------------------
164 # An alarm handler to check if FvwmScript-ComExample is still alive
167 die "Script" unless ($comPid);
171 $test = 1 if kill 0 => $comPid;
173 if ($test) { die "Script"; }
175 unlink($outFifo) if -p
"$outFifo";
176 unlink($inFifo) if -p
"$inFifo";
181 #-----------------------------------------------------------------------------
182 # For killing the FvwmScript-ComExample if an error happen in this script
185 my $message = "$scriptName: internal error $?\n";
186 $message .= "\tOS error: $!\n" if $!;
187 # actually the following is never executed on unix
188 $message .= "\tOS error 2: $^E\n" if $^E
&& !($! && $^E
eq $!);
190 unlink($outFifo) if -p
"$outFifo";
191 unlink($inFifo) if -p
"$inFifo";
194 $message .= "\tkilling FvwmScript-ComExample";
196 print STDERR
"$message\n";