[comp.sources.misc] v03i061: A prompt generator program

d4a-las@psi.sm.luth.se (Anders S Lindb{ck) (06/29/88)

comp.sources.misc: Volume 3, Issue 61
Submitted-By: "Anders S Lindb{ck" <d4a-las@psi.sm.luth.se>
Archive-Name: p

Here are somthing to send out to the rest of the net.

This is a promptgenerator program that may be of value for those with 
access to a number of different host and often go forth and back between them.

The termcap code should not be to difficult to ifdef out if one wants to.

Please dont send mail to me after mid-august because then I will lose 
this account and I don't know if I ever get a new one with access to usenet.

Anders S Lindb{ck, Pors|g}rden 10, S-951 65 Lulea, Sweden  
UUCP:    {Outher space}!uunet!mcvax!enea!sm.luth.se!d4a-las
Internet: d4a-las@psi.sm.luth.se			   
When I get time I'm gonna write this witty disclaimer and put it right here.

--------cut-slice-tear-or-whatever----right----at----this---line-----------
: This is a shar archive.  Extract with sh, not csh.
: The rest of this file will extract:
: p.man makefile p.c
#! /bin/sh
echo extracting - p.man
sed 's/^X//' > p.man << '/*EOF'
X.TH P LuTH 
X.UC 4
X.SH NAME
Xp \- the prompt generator 
X.SH SYNOPSIS
X.B p [\-lpiuqHnh]
X
X.SH DESCRIPTION
X
XThis program are supposed to be called as an alias
X alias cd 'cd !* ;set prompt=`p`'
X alias popd 'popd !* ; set prompt=`p`'
X alias pushd 'pushd !* ; set prompt=`p`'
X and generates a prompt:
X  '<hostname>:<path>[!]>'
X <hostname> is your local hosts name 
X <path> is in three differ modes depending where you are
X the absolute pwd if you are high up in the three
X ~<user>/... if you are at another users homedirectory
X ~/... if you are in your own homedirectory
X the [!] will (t)csh transform to the history number.
X.SH OPTIONS
XThe program recognize the following options:
X
X.B \-l
XGive whole path from root.
X
X.B \-p
XPath in prompt, default.
X
X.B \-i
XDo inverse printing of prompt.
X
X.B \-u
XDo underscore printing of prompt.
X
X.B \-q
XRevoke all defaults and present flags.
X
X
X.B \-H
XHostname in the prompt, default.
X
X.B \-n
XNormal font mode in the prompt, default.
X
X.B \-h
XHistory in the prompt, default.
X
X.SH BUGS
XDoesn\'t give right path to user d-sekt on tau.sm.luth.se.
XAnd probably at numerous other in the world.
XIn the absence of a fast generic way to recognize all users homedirectory,
Xone just have to fix the code by oneself.
XHas to many flags.
X.SH AUTHOR
XAnders S Lindb{ck.
X
XDon't send flames or flowers to d4a-las@psi.sm.luth.se since
XI\'m losing this account in august \'88.
X
X
X
/*EOF
echo extracting - makefile
sed 's/^X//' > makefile << '/*EOF'
Xp: p.c
X	cc -O p.c -o p -ltermcap -s
Xlint:
X	lint p.c
/*EOF
echo extracting - p.c
sed 's/^X//' > p.c << '/*EOF'
X/*
X * Generate a nice promt with optional setting
X *                               
X * Made by Anders S Lindb{ck 1987-09-23
X * Public Domain. Hack in it as much as you like.
X * Last edit 1988-06-22  Made it more readable and some minor changes.
X * Version 1.1
X * Compile using:
X * cc -O p.c -o p -ltermcap -s
X *
X * Intended to use with tcsh or csh.
X * Put in the following in your .login, .alias or .aliases or whatever
X * alias cd 'cd \!* ;set prompt="`p`"'
X * alias pop 'popd \!* ; set prompt="`p`"'
X * alias push 'pushd \!* ; set prompt="`p`"'
X *
X */
X
X#include <stdio.h>
X#include <strings.h>
X
X#define NORMAL       0000
X
X/* Different prompt type_modes */
X#define HOSTNAME     0001
X#define PATH         0002
X#define LONGPATH     0004
X#define HISTORY      0010
X
X/* Different font modes */
X#define INVERSE      0020
X#define UNDERLINE    0040
X
X/* Different path printouts */
X#define HOME         0100
X
Xextern char *getwd();
Xextern gethostname();
Xextern char *tgetstr();
Xextern char *getenv();
X
Xchar tcapbuf[1024];
Xchar *FontS, *FontE;
Xchar *p;
Xchar tcbuf[1024];
Xchar homedir[64];
Xchar host[64];
Xchar dirs[64];
Xchar *cp;
Xchar *p1, *p2, *p3;
X
Xqputc(c)
Xint c;
X{
X    (void)putchar(c);
X}
X
Xmain(argc, argv)
Xint argc;
Xchar *argv[];
X{
X    short int k, l;
X    short int font_mode = NORMAL;
X    short int type_mode = PATH | HISTORY | HOSTNAME;
X    short int home = NORMAL;
X    /* Ok check what strange options they want */
X    for (k = 1; k < argc; k++)
X	if (argv[k][0] == '-')
X	    for (l = 1; argv[k][l]; l++)
X		switch (argv[k][l])
X		{
X		case 'i':
X		    font_mode |= INVERSE;
X		    break;
X		case 'u':
X		    font_mode |= UNDERLINE;
X		    break;
X		case 'n':
X		    font_mode = NORMAL;
X		    break;
X		case 'l':
X		    type_mode |= (PATH | LONGPATH);
X		    break;
X		case 'h':
X		    type_mode |= HISTORY;
X		    break;
X		case 'H':
X		    type_mode |= HOSTNAME;
X		    break;
X		case 'p':
X		    type_mode |= PATH;
X		    break;
X		case 'q':
X		    type_mode = NORMAL;
X		    break;
X		default:
X		    (void) printf("Usage %s [ -iunlhHpq ]", argv[0]);
X		}
X
X/* Get termcap entry to underline or inverse  */
X    if (font_mode)
X    {
X	tgetent(tcbuf, getenv("TERM"));
X	p = tcapbuf;
X	if (font_mode & INVERSE)
X	{
X	    if ((FontS = tgetstr("so", &p)) == NULL)
X		FontS = "\0";
X	    if ((FontE = tgetstr("se", &p)) == NULL)
X		FontE = "\0";
X	} else if (font_mode & UNDERLINE)
X	{
X	    if ((FontS = tgetstr("us", &p)) == NULL)
X		FontS = "\0";
X	    if ((FontE = tgetstr("ue", &p)) == NULL)
X		FontE = "\0";
X	}
X    }
X    /* Time for the path */
X    if (type_mode & PATH)
X    {
X	(void) strcat(homedir, getenv("HOME"));
X	(void) getwd(dirs);
X	cp = dirs;
X	if (!(type_mode & LONGPATH))
X	    if (strncmp(dirs, homedir, strlen(homedir)) == 0)
X	    {
X		cp = dirs + strlen(homedir);
X		home = HOME;
X	    } else
X	    {
X		/*
X		 * A kludge to se if it are a real user and not /usr/local/lib
X		 * Here it are so that almost everybody has an adress of type
X		 * /u1/d4a/d4a-las  or somthing similar so one must check that
X		 * it are more than 3 levels deep.
X		 */
X		if ((strncmp(dirs, "/u1", 3) == 0) ||
X		    (strncmp(dirs, "/u2", 3) == 0) ||
X		    (strncmp(dirs, "/u0", 3) == 0) ||
X		    (strncmp(dirs, "/usr/staff", 10) == 0))
X		    if ((p3 = index(cp, '/')) != (char *) NULL)
X			if ((p2 = index(++p3, '/')) != (char *) NULL)
X			    if ((p1 = index(++p2, '/')) != (char *) NULL)
X			    {
X				home = HOME;
X				cp = p1 + 1;
X			    }
X	    }
X    }
X    /* Well we need some hostnames */
X    if (type_mode & HOSTNAME)
X	(void) gethostname(host, (sizeof host) - 2);
X
X    /* Now we are ready, to write out the whole thing */
X    if (font_mode)
X	tputs(FontS, 1, qputc);
X    if (type_mode & HOSTNAME)
X	(void) printf("%s:", host);
X    if (type_mode & PATH)
X    {
X	if (home)
X	    (void) putchar('~');
X	(void) printf("%s", cp);
X    }
X    if (type_mode & HISTORY)
X	(void) printf("[!]");
X    (void) putchar('>');		/* Change if you like  */
X    if (font_mode)
X	tputs(FontE, 1, qputc);
X    return 0;
X}
/*EOF
exit 0