[comp.windows.x] automatically setting DISPLAY variable

thomas@BACH.CS.BYU.EDU (Thomas McNeill) (08/25/90)

Here is a csh script/C program combination that will automatically
set the DISPLAY environment variable to the proper value, whether
you log on to the console or through rlogin.  However, it will not
work for more than one level of rlogin (that being beyond my
meager abilities).

The csh script "setdisplay" is:
==================================================================
#! /bin/csh -f
# setdisplay
# Sets the DISPLAY environment variable to <host>:0.0, or if
# logged in remotely, to the remote machine.  (Unfortunately
# will not handle more than one level of rlogin.)
#
# written by Thomas G. McNeill
# August 23, 1990
# Brigham Young University, Provo, UT
#
set t = `tty`
set d = `who | grep $t:t | glogin`
if ($d == "" || $d == ":0.0") then
	setenv DISPLAY "`hostname`:0.0"
else
	setenv DISPLAY $d\:0.0
endif
# end of setdisplay
==================================================================

The C program "glogin.c" is:
==================================================================
/*
* glogin.c
* written by Thomas G. McNeill
* August 23, 1990
* Brigham Young University, Provo, UT
*
* glogin expects a line from the output of who as its standard input.
* If there is a parenthesized string in its input, glogin copies it
* to the output (without the parentheses).  If there is no such string
* in the input, glogin copies nothing to the output.
*
* There is probably a way to do this with awk, but writing this program
* was easier than figuring out how to do it with awk.
*/
#include <stdio.h>

#ifndef TRUE
#define TRUE	1
#define FALSE	0
#endif

void main(argc,argv)
int argc;
char **argv;
{
	int c;
	int flag;

	while ((c = getchar()) != EOF) {
		if (c == ')') {
			flag = FALSE;
		}
		if (flag) {
			putchar(c);
		}
		if (c == '(') {
			flag = TRUE;
		}
	}
}
==================================================================

To use, first compile glogin, e.g.,

	cc -o glogin glogin.c

Next, make setdisplay executable:

	chmod +x setdisplay

Finally, put the following line in your .login:

	setdisplay

You must do this on every machine to which you log in, either on
the console or remotely.

Thomas G. McNeill
Graduate Student, Computer Science Dept.
Brigham Young University, Provo, UT
thomas@bach.cs.byu.edu

brb@myrias.com (Brian Baird) (08/26/90)

Thomas McNeill <thomas@BACH.CS.BYU.EDU> provides a Csh script, C
program combination to set the DISPLAY environment variable from a
Csh .login.  Unfortunately, the directions are incorrect.  DISPLAY
will be set for the "setdisplay" script, but not for the parent shell.

Here's my (somewhat simpler) version.

: sh this file
echo >whatdisplay <<\EOF
#! /bin/sh
# whatdisplay: which X display am I running from?
#
# 90 08 25	Brian Baird, Myrias Research, Edmonton
#		<brb@myrias.com>
#
# Make this script executable, place it in some directory in your
# PATH, and, depending on your shell, add the following line to the
# named file on each machine you rlogin to.  This script doesn't
# handle nested rlogins.
#
# Shell		File		Line
# -----		----		----
# csh		.login		setenv DISPLAY `whatdisplay`
# sh		.profile	DISPLAY=`whatdisplay`; 	export DISPLAY
# bash		.bash_login	export DISPLAY=`whatdisplay`

case $DISPLAY in
"")	;;
*)	echo "$DISPLAY"		# display already set
	exit 0 ;;
esac

HOST=`who am i | sed 's/.*(\(.*\)).*/\1/'`

case "$HOST" in
"")	echo ":0" ;;		# local host
*:*)	echo "$HOST" ;;		# remote host includes display
*)	echo "$HOST:0" ;;	# simple remote host
esac
EOF
exit

--
Brian Baird				brb@myrias.com
Myrias Research, Edmonton		{uunet,alberta}!myrias!brb