[comp.windows.x] Setting up DISPLAY environment variable

bar@uswat.uswest.com (Bala Ramakrishnan 595-2868) (06/29/90)

I run X11R4 on a sun 3/80. Also, I run some Xclients on a Sparc server by
rlogin to the sparc machine. My home directories are mounted on both the
Sun 3/80 and the sparc machine.

My .cshrc file has : setenv DISPLAY localhost:0

However, when I rlogin to the sparc server, the same .cshrc file gets
executed and DISPLAY is set to localhost:0. When I am on the sparc
server, my .cshrc file should set DISPLAY to <internet addr of sun3/80>:0

Is there a way (say by executing a program) to inquire the sparc machine
where Iam rlogged from ,so that I can set the DISPLAY variable
to the internet address of the machine from which I rlogged automatically.

Right now, I type in the setenv DISPLAY <internet addr of sun 3/80>:0
manually after the rlogin window comes up.

--
:-):-):-):-):-):-):-):-):-):-):-):-):-):-):-):-):-):-):-):-):-):-):-)
:-<    E-mail: bar@uswest.com       USWEST Advanced Technologies  :->
:-<    303-595-2868(w)              Denver Co 80202               :->
:-(:-(:-(:-(:-(:-(:-(:-(:-(:-(:-(:-(:-(:-(:-(:-(:-(:-(:-(:-(:-(:-(:-(

stripes@eng.umd.edu (Joshua Osborne) (06/30/90)

In article <9173@uswat.UUCP> bar@uswat.uswest.com (Bala Ramakrishnan 595-2868) writes:
>I run X11R4 on a sun 3/80. Also, I run some Xclients on a Sparc server by
>rlogin to the sparc machine. My home directories are mounted on both the
>Sun 3/80 and the sparc machine.
>
>My .cshrc file has : setenv DISPLAY localhost:0
Why not just set it once in .login?

[...]
>Is there a way (say by executing a program) to inquire the sparc machine
>where Iam rlogged from ,so that I can set the DISPLAY variable
>to the internet address of the machine from which I rlogged automatically.
From my .login:

set from=`who am i | cut -d\( -f2 | cut -d. -f1`
if (`tty` == /dev/console) then
		setenv DISPLAY `hostname`:0
else
		setenv DISPLAY "${from}":0
endif

This doesn't allways work.  It works for one hop & for a console login.
-- 
           stripes@eng.umd.edu          "Security for Unix is like
      Josh_Osborne@Real_World,The          Mutitasking for MS-DOS"
      "The dyslexic porgramer"                  - Kevin Lockwood
"Don't try to change C into some nice, safe, portable programming language
 with all sharp edges removed, pick another language."  - John Limpert

prc@erbe.se (Robert Claeson) (06/30/90)

In article <9173@uswat.UUCP>, bar@uswat.uswest.com (Bala Ramakrishnan 595-2868) writes:

> Is there a way (say by executing a program) to inquire the sparc machine
> where Iam rlogged from ,so that I can set the DISPLAY variable
> to the internet address of the machine from which I rlogged automatically.

I believe that there's a TELNET option for this -- X Display Location or
something like that. Don't know how many that really have implemented it
yet, if any.

-- 
Robert Claeson                  |Reasonable mailers: rclaeson@erbe.se
ERBE DATA AB                    |      Dumb mailers: rclaeson%erbe.se@sunet.se
                                |  Perverse mailers: rclaeson%erbe.se@encore.com
These opinions reflect my personal views and not those of my employer (ask him).

narten@percival.albany.edu (Thomas Narten) (06/30/90)

In article  <9173@uswat.UUCP> bar@uswat.uswest.com (Bala Ramakrishnan 595-2868) writes:
>My .cshrc file has : setenv DISPLAY localhost:0
>
>However, when I rlogin to the sparc server, the same .cshrc file gets
>executed and DISPLAY is set to localhost:0. When I am on the sparc
>server, my .cshrc file should set DISPLAY to <internet addr of sun3/80>:0

Setting DISPLAY in either .cshrc or .login is asking for trouble.
Here is what I do.  It works well, and handles all the nice cases
(e.g., when I'm logged in through an ascii terminal, DISPLAY does not
get set).  First, you only want to set DISPLAY to be the name of where
your X server is running.  The best place to set it is in your
.xinitrc file, which is executed only when you start up the X server.
Here are relevant lines from my .xinitrc:

# Check to see if the DISPLAY environment variable is set.  If not,
# then set it to "unix:0"
DISPLAY=${DISPLAY-unix:0}; export DISPLAY

In addition, you may also want to be able to run other X applications
on remote machines that open connections directly with your server.
To start emacs on a remote machine for instance, my .twmrc looks like:

"Emacs@atanasoff"	!"xrsh atanasoff emacs -in Emacs@atansoff -wn Emacs@atanasoff &"

Finally, xrsh does the same thing as rsh, except that:
1) it uses xhosts to allow the remote machine to create windows on
the machine running your server
2) it sets the DISPLAY variable on the remote machine to point back to
your server before running the actual program. 


#!/bin/sh
#
# A frontend to rsh to start an X client on another host.
#
# A version of this was originally posted to Usenet in
# 12/88 by Chris Torek (chris@mimsy.umd.edu).
# Enhanced by Marion Hakanson (hakanson@cse.ogc.edu).
# $Id: xrsh,v 1.8 89/10/13 14:06:35 hakanson Rel $
#
# Simply exports $DISPLAY, and runs the given command.
#
# Uses /bin/sh to factor out the user's default remote shell.
# Redirects I/O to allow rsh & rshd to exit immediately, and uses
# exec, both in order to minimize the number of processes involved.
#
# Set "out" to /dev/null if you don't care to have access to
# diagnostics from the last xrsh invocation on target machine.

case $# in
0|1) echo "usage: $0 host cmd [args]" 1>&2; exit 1;;
esac

: ${DISPLAY?"environment variable DISPLAY not set"}

case "X$DISPLAY" in
Xunix:*)
  : ${HOSTNAME=`hostname`}
  DISPLAY=`echo "$DISPLAY" | sed -e "s/^unix:/${HOSTNAME}:/"`
  ;;
X:*)
  : ${HOSTNAME=`hostname`}
  DISPLAY="$HOSTNAME$DISPLAY"
  ;;
esac

host="$1"
shift
out=".xrsh.out"
xhost $host >/dev/null
rsh "$host" -n exec /bin/sh -c \
   "'DISPLAY=\"$DISPLAY\"; export DISPLAY; exec $@ </dev/null >$out 2>&1'"
-- 
Thomas Narten
narten@cs.albany.edu

bjl@freyr.pttrnl.nl (Ben Lippolt) (07/12/90)

narten@percival.albany.edu (Thomas Narten) writes:
>Setting DISPLAY in either .cshrc or .login is asking for trouble.
Not always (see below).

>First, you only want to set DISPLAY to be the name of where
>your X server is running.  The best place to set it is in your
>.xinitrc file, which is executed only when you start up the X server.
This won't work if you use an X-terminal.

[example xrsh deleted]

Another approach is the following: (this is part of my .cshrc)

 || #!/bin/csh
 || # PASS ON the DISPLAY environment variable
 || # (containing the machine name) in case of rlogin
 || # but ONLY IF IT HAS BEEN SET ALREADY (by xinit or something alike)
 || 
 || if ( $TERM =~ xterm%* ) then # XTERM emulator has been started
 ||    setenv DISPLAY `echo $TERM | sed '/xterm%/s///'`
 ||    set term = xterm
 || endif
 || 
 || if ( $TERM == 'xterm' ) then
 ||     if ( $?DISPLAY ) then
 ||         switch( $DISPLAY )
 ||             case "unix:0":
 ||             case "unix:0.0":
 ||             case "local:0.0":
 ||             alias rlogin "(set term=xterm%`hostname`:0;/usr/ucb/rlogin \!*)"
 ||             alias su "(set term = xterm%`hostname`:0; /usr/bin/su \!*)"
 ||             breaksw
 ||         default:
 ||             alias rlogin "(set term=xterm%${DISPLAY}; /usr/ucb/rlogin \!*)"
 ||             alias su "(set term=xterm%${DISPLAY}; /usr/bin/su \|*)"
 ||         endsw
 ||     endif
 || endif

    (The trick is that the environment variable $TERM is not altered during
    a rlogin, so you use it to pass information across rlogins).
This has the advantage that you can run it from X-terminals and it also
works fine with rlogin. In combination with xrsh it should work in all 
conceivable situations.

>-- 
>Thomas Narten
>narten@cs.albany.edu

PS. I didn't invent this. I just copied it from someone else, who copied
it from someone else, who copied it ...

Ben J. Lippolt    
---
PTT Research, Dr. Neher Laboratories    [ E-mail : BJ_Lippolt@pttrnl.nl ]
P.O. Box 421, 2260 AK Leidschendam,     [ BITnet : LIPPOLT@HLSDNL5      ]
The Netherlands. Tel: +31 70 3325439    [ UUCP   : hp4nl!dnlunx!bjl     ]

thor@stout.atd.ucar.edu (Rich Neitzel) (07/12/90)

In article <143@percival.albany.edu>, narten@percival.albany.edu (Thomas
Narten) writes:
|>In article  <9173@uswat.UUCP> bar@uswat.uswest.com (Bala Ramakrishnan
595-2868) writes:
|>>My .cshrc file has : setenv DISPLAY localhost:0
|>>
|>>However, when I rlogin to the sparc server, the same .cshrc file gets
|>>executed and DISPLAY is set to localhost:0. When I am on the sparc
|>>server, my .cshrc file should set DISPLAY to <internet addr of sun3/80>:0
|>
|>Setting DISPLAY in either .cshrc or .login is asking for trouble.

Well, I disagree. I do set DISPLAY in my .login, but only if the TERM is
an xterm. Then I know that the original host is running X (of course, if
you rlogin from the second host to a third it fails!). Here's what I do:

if ($TERM == "xterm") then
   if ($?DISPLAY) then
      exit 0
   else
      setenv DISPLAY  `who am i | tr -s '('')'' ''\011' ' ' | cut -f6 -d" " \
	| cut -d"." -f1`":0"
   endif
endif

The first tr removes multiple spaces and any tabs (SunOs 4.1 who am i
puts out both!). The second cut is used to correctly parse fully
qualified names and simple names (i.e, it handles surt &
surt.atd.ucar.edu correctly). I've only done this on Sun-3/4s under
SunOs 4.0.3 & 4.1.
                                                                            
Richard Neitzel thor@thor.atd.ucar.edu	     	Torren med sitt skjegg
National Center For Atmospheric Research	lokkar borni under sole-vegg
Box 3000 Boulder, CO 80307-3000			Gjo'i med sitt shinn
303-497-2057					jagar borni inn.