Discussion:
FVWM: Extracting an Environment Variable from FVWM]
m***@crosswire.com
2011-08-26 18:08:15 UTC
Permalink
-------- Original Message --------
Subject: RE: FVWM: Extracting an Environment Variable from FVWM
From: <***@crosswire.com>
Date: Fri, August 26, 2011 2:07 pm
To: "Thomas Adam" <***@fvwm.org>


<SNIP>
As I said before: I'd rather not have to wrap FvwmCommand. So if there
is a programatic way of getting access to FVWM's environment variables
without spending a week doing it, I'm all ears.
At the moment there isn't -- and all FvwmCommand is doing is relaying your
commands via its own FIFO to FVWM.
-- Thomas Adam
Ok,

Heres the wrapper if anybody is interested. It seems to work.

Thanks in advance!
Matt


#####################################################################
#! /usr/bin/perl -w

# fvwm-getenv, a handy dandy tool for extracting environment variables
# from fvwm.

use strict ;

my $ev = shift @ARGV ;
chomp $ev ;
die ("usage: fvwm-getenv <environmentvariable> ") unless $ev =~ /\w+/ ;

# MAKE A FIFO
my $mkfifo = '/usr/bin/mkfifo -m 700' ; # the mkfifo command
my $tmppath = $ENV{'HOME'} . '/.fvwm/tmp' ; # you will need to create
this directory
my $rn = sprintf("%0.10d",rand(1000000000)) ; # a random number
my $fifofn = $tmppath . '/' . $rn . '.fifo' ; # becomes a filename
system("$mkfifo $fifofn") ; # and we create a temporary fifo

# FVWM PASSTHROUGH COMMAND
my $fvwmcommand = 'FvwmCommand ' . "'" . "Test \(EnvIsSet $ev\) Exec
echo \$[$ev] >> $fifofn" . "'" ;

my $childpid ; # know thyself

if ($childpid = fork) { # Parent
open (FIFOFN, $fifofn) ; #
my $line = readline(FIFOFN) ; # catch the input
print $line ; # print it
close(FIFOFN) ;
unlink $fifofn ; # delete the tmp file
exit ;
} else { # Child
system($fvwmcommand) ; # create the output
exit ;
}
##########################################################
Thomas Adam
2011-08-26 18:34:45 UTC
Permalink
Hi,
Post by m***@crosswire.com
Heres the wrapper if anybody is interested. It seems to work.
What I was getting at before, was something like this:

FvwmCommand -i2 'Test (EnvIsSet FOO) \
SendToModule FvwmCommandS $[FOO]' | awk '$2 ~ string {print $3}'

Which you could obviously wrap in a shell script, and get to accept input,
etc. It seems a little simpler than your perl example, although if there's
a need for this thing to persist in your case (you've still not actually
told me *what* use this is for, although at this point I suppose it doesn't
matter.)

Speaking of perl, I have some comments regarding your snippet. I don't know
if this is just a toy to you or not, but if you're thinking of using this
for anything long-term, which is supposed to provide a service and which you
don't want to have crash, you might want to cast your eye over this.
Post by m***@crosswire.com
Thanks in advance!
Matt
#####################################################################
#! /usr/bin/perl -w
It's bad practise to do this in perl since there's now no selective way of
turning this off for other modules which include warnings. It's better just
to use an explicit:

use warnings;
Post by m***@crosswire.com
# fvwm-getenv, a handy dandy tool for extracting environment variables
# from fvwm.
use strict ;
chomp $ev ;
die ("usage: fvwm-getenv <environmentvariable> ") unless $ev =~ /\w+/ ;
Perhaps better written as:

die "...." unless @ARGV;
chomp(my $ev = shift);
Post by m***@crosswire.com
# MAKE A FIFO
my $mkfifo = '/usr/bin/mkfifo -m 700' ; # the mkfifo command
Ouch! This should be using the module: File::Temp.
Post by m***@crosswire.com
my $tmppath = $ENV{'HOME'} . '/.fvwm/tmp' ; # you will need to create
this directory
Again, can use File::Temp for this.
Post by m***@crosswire.com
my $rn = sprintf("%0.10d",rand(1000000000)) ; # a random number
my $fifofn = $tmppath . '/' . $rn . '.fifo' ; # becomes a filename
system("$mkfifo $fifofn") ; # and we create a temporary fifo
See: POSIX qw/mkfifo/;

Also -- running *anything* through system in scalar content is a bad idea.
Post by m***@crosswire.com
# FVWM PASSTHROUGH COMMAND
my $fvwmcommand = 'FvwmCommand ' . "'" . "Test \(EnvIsSet $ev\) Exec
echo \$[$ev] >> $fifofn" . "'" ;
This is the point at which I would have considered using FvwmPerl here (as
opposed to perllib outright.)
Post by m***@crosswire.com
my $childpid ; # know thyself
if ($childpid = fork) { # Parent
open (FIFOFN, $fifofn) ; #
The three-args form of open() is preferred these days.
Post by m***@crosswire.com
my $line = readline(FIFOFN) ; # catch the input
print $line ; # print it
close(FIFOFN) ;
unlink $fifofn ; # delete the tmp file
exit ;
} else { # Child
system($fvwmcommand) ; # create the output
Again, heed advise about scalar context, and perhaps look in to FvwmPerl.

HTH,

-- Thomas Adam
--
"Deep in my heart I wish I was wrong. But deep in my heart I know I am
not." -- Morrissey ("Girl Least Likely To" -- off of Viva Hate.)
Loading...