Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watnot!watmath!clyde!rutgers!seismo!lll-lcc!ames!ucbcad!ucbvax!decvax!dartvax!earleh From: earleh@dartvax.UUCP Newsgroups: net.sources Subject: Re: prompt wars Message-ID: <5961@dartvax.UUCP> Date: Tue, 7-Apr-87 03:10:04 EST Article-I.D.: dartvax.5961 Posted: Tue Apr 7 03:10:04 1987 Date-Received: Sat, 11-Apr-87 08:17:32 EST Organization: Society for the Prevention of Cruelty to Graduate Students Lines: 107 Keywords: tacky, garrish 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: [!] X * X * where is the name of the current host sans trailing domains X * 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 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