[net.sources] Prompt Wars

waynet@tolerant.UUCP (03/27/87)

Stick this in your ~/.cshrc!!

#following must be last
if (! $?prompt) exit
set host=`hostname`
a sp 'set prompt="$host":"$cwd % "'
sp
a cd 'cd \!*;sp'
a pushd 'pushd \!*;sp'
a popd 'popd \!*;sp'
--
Which is worse, ignorance or apathy? Who knows, who cares.

Wayne Thompson
..{bene,mordor,nsc,oliveb,pyramid,ucbvax}!tolerant!waynet

Your mission, should you decide to accept it, is to discern who is
responsible for these opinions, the poster or the company he works for.
Good luck!!

earleh@dartvax.UUCP (04/07/87)

This is a shar file, for a typical prompt-generating C program.  This
one uses inverse video, if it can get it.  Challenge: Do this with a
shell script!  The system here is BSD.

: This is a shar archive.  Extract with sh, not csh.
echo x - pgen.c
sed -e 's/^X//' > pgen.c << '!Funky!Stuff!'
X/* Generate prompt string
X * Ben Cranston 3/14/87
X *
X * designed to be called as:
X *
X * alias cd 'cd \!*; set prompt="`pgen`"'
X *
X * builds and outputs string:  <hostname> [!] <workdir>
X *
X * where <hostname> is the name of the current host sans trailing domains
X *       <workdir>  is the current directory, with all but the last two
X *                  directory names replaced by ... for brevity.
X *
X * I had all this working with "awk" scripts, but it was taking over
X * four SECONDS to switch directories.  This was considered wasteful.
X *
X * SYSTEM V version by J.R. Stoner (asgard@cpro.UUCP) 03/24/87
X *
X * Converted back to BSD by Earle Horton, adding inverse video for
X * increased tackiness 04/06/87.  Also changed name to avoid 
X * over-writing any genpro.c, in case you change your mind.
X */
X
X#include <stdio.h>
X#include "pgen.h"
Xttputc(c)
Xint c;
X{
X	putchar(c);
X}
X
Xmain()
X
X{
X
X	tgetent(tcbuf,getenv("TERM"));
X
X	p = tcapbuf;
X	if((SE = tgetstr("se", &p))==NULL) SE = "\0";
X	if((SO = tgetstr("so", &p))==NULL) SO = "\0";
X
X	gethostname(hostname,(sizeof hostname) - 2);
X	getwd(dirs);
X	if((cp=index(dirs,' ')) != (char *)0)
X		*cp = 0;     /* truncate dir string at first space (if any) */
X
X	/*
X	 * search backwards for slash.  if found, temporarily make null
X	 * and search backwards for slash again.  if found again, replace
X	 * string to the left of the second slash with "..."   The
X	 * additional test of (p1!=dirs) is for a special case, a
X	 * two-string explicitly rooted.  that is, "cd /etc/ns" will
X	 * display as /etc/ns rather than ...etc/ns
X	 */
X
X	buff[0] = 0;
X	cp = dirs;
X	if ( 0 != (p2 = rindex(dirs,'/')) ) {
X		*p2 = 0;
X		if ( (0 != (p1 = rindex(dirs,'/'))) && (p1 != dirs) ) {
X			strcpy(buff,"...");
X			cp = p1 + 1;
X		}
X		*p2 = '/';
X	}
X	strcat(buff,cp);
X	tputs(SO,1,ttputc);
X	printf("-[!]%s:%s%% ",hostname,buff);
X	tputs(SE,1,ttputc);
X}
!Funky!Stuff!
echo x - pgen.h
sed -e 's/^X//' > pgen.h << '!Funky!Stuff!'
X
X#define TCAPSLEN 315
X#define BUFFSIZE 512
X
Xchar tcapbuf[TCAPSLEN];
Xchar *SO, *SE;
Xextern char *getwd();
Xextern char *index();
Xextern char *rindex();
Xchar *getenv();
Xchar *p, *tgetstr();
Xchar tcbuf[1024];
Xchar hostname[BUFFSIZE];
Xchar dirs[BUFFSIZE], buff[BUFFSIZE];
Xchar *cp;
Xchar *p1, *p2;
!Funky!Stuff!
echo x - Makefile
sed -e 's/^X//' > Makefile << '!Funky!Stuff!'
Xpgen: pgen.c pgen.h
X	cc pgen.c -o pgen -ltermcap
X
Xshar: 
X	shar pgen.c pgen.h Makefile > pgen.sh
!Funky!Stuff!
exit

woolsey@nsc.UUCP (04/09/87)

So, you want inverse video in a shell script, eh?  Try this:

# to underline the entire hostname is a bit of a challenge.
# it works with the history number because when underlining is set
# into the prompt, only one character is underlined.  csh later converts
# that to a number, and the start/end underline characters are intact
echo "set prompt='`hostname` \_\!_:_ '" | ul >! /tmp/$$
source /tmp/$$
rm /tmp/$$
-- 
				Jeff Woolsey
				...!nsc!pubs!woolsey
				woolsey@nsc.COM
				woolsey@umn-cs.ARPA

brandon@tdi2.UUCP (Brandon Allbery) (04/13/87)

Don't you think this is getting a bit ridiculous?  BTW, here's mine, for a
csh such as Xenix and System III/V resellers like to include:  (change paths
as necessary)

-------------------------------------------------------------------------------
if ("`/usr/plx/printenv CSHLEVEL`" == "") then
	setenv CSHLEVEL 1
	setenv CSHTYPE ""
	set cwd=(`/bin/pwd`)
else if ($?prompt) then
	@ level=$CSHLEVEL + 1
	setenv CSHLEVEL $level
	set uid=`/appl/u32/bin/uid`		# well, it's easiest
	if ($uid == 0) then
		set w=\#
	else
		set w=\%
	endif
	if ( "$CSHTYPE" == "" ) then
		set prompt="${level}${w} "
	else
		set prompt="$CSHTYPE@${level}${w} "
	endif
	unset level w
	set cwd=(`/bin/pwd`)
endif
-------------------------------------------------------------------------------

Change the reference to the "uid" program; I used to use "id" through a sed
command, but I have to admit that the above is faster...  (u32 == UNIFY 3.2)

This works with the following, which is VERY useful (dunno how useful if you
have sxt's or job control; we have neither) for preserving environments:

-------------------------------------------------------------------------------
#! /bin/sh
#
# Generate a new environment and run a subshell with it.  $1 names the
# environment file to be used.
#

[ $# -gt 1 ] && {
	echo "usage: $0 [environment]" >&2
	exit 1
}
[ $# -eq 0 ] && exec ${SHELL:-/bin/sh}
envspec="$HOME/.pushenv/$1"
[ -f "$envspec" ] || {
	echo "$0: Unknown environment." >&2
	exit 1
}
env | fgrep "PATH
HOME
TERM
TZ
CSHLEVEL
CSHTYPE
EDITOR
LOGNAME
MAIL
SHELL" | fgrep -v "`sed -n '/^!/s///p' < $envspec`" > /tmp/env.$$
env | fgrep "`sed -n '/^=/s///p' < $envspec`" >> /tmp/env.$$
ed - /tmp/env.$$ << eof
g/=/s/=\(.*\)$/='\1'/
w
q
eof
sed -f /u/brandon/.pushenv/.push.sed < $envspec >> /tmp/env.$$
# cat /tmp/env.$$
env="`sed -n '/^cd/!s/=.*//p' < /tmp/env.$$`"
unset `env | sed -n '/^PATH=/!s/=.*//p'`
. /tmp/en[vw].$$
export $env
/bin/rm -f /tmp/env.$$
# echo '---'
# exec env
exec ${SHELL:-/bin/sh}
-------------------------------------------------------------------------------

This is used with files in ~/.pushenv containing lines of the following form:

-------------------------------------------------------------------------------
#
# Environment for UNICOE
#

@UNICOE				# ID to put in prompt

$UNIFY = /appl/u32/lib		# set some useful environment variables
$BUDEV = /dev/rrm/0m
$DBPATH = /coesys/bin
$UUID = 0
$UUACL = 15
$DATETP = US
$PATH = $DBPATH:$UNIFY/../bin:$PATH
$EDIT = $EDITOR

>$DBPATH			# and change directory
-------------------------------------------------------------------------------

By default a small set of environment variables is retained and the rest are
junked, so you can start out "clean" on each "push".  (I also alias "pop" to
"exit".)

RV and the like in the prompt?  Well, there's always "tput" under System V.

Hopefully this article has a little more useful "meat" than the previous ones.
(Of course, ``meat'' is relative; we don't use hostnames (1 machine), I don't
want the current directory in my prompt and dislike flashing lights except to
flag disasters.)

++Brando
-- 
Brandon S. Allbery	           UUCP: cbatt!cwruecmp!ncoast!tdi2!brandon
Tridelta Industries, Inc.         CSNET: ncoast!allbery@Case
7350 Corporate Blvd.	       INTERNET: ncoast!allbery%Case.CSNET@relay.CS.NET
Mentor, Ohio 44060		  PHONE: +1 216 255 1080 (home +1 216 974 9210)