From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fee84,552b7059e033056c X-Google-Attributes: gidfee84,public From: john@iastate.edu (John Hascall) Subject: Re: Directory Prompt Date: 1996/05/11 Message-ID: <4n236l$mkf@news.iastate.edu> X-Deja-AN: 154287981 references: <4lt1oa$294@random.tpgi.com.au> <4mt0vt$4cu@csugrad.cs.vt.edu> <4mvvso$o92@david.wheaton.edu> <4n098r$it0@gingko.cc.swarthmore.edu> organization: Iowa State University, Ames, Iowa USA newsgroups: comp.unix.osf.osf1 John P. Speno wrote: }>Well gosh, in tcsh, there is a predefined alias, precmd, which gets run }>every time just before the prompt comes up. Thus instead of realiasing }>cd, popd, and pushd, just do the following: }>alias precmd 'set prompt="\n%B%n@%m%b:%/\n%% "' }>That will reset the prompt just before every time the prompt comes up. }Why bother aliasing precmd when just: }set prompt="\n%B%n@%m%b:%/\n%% " }works? YMMV. I, for one, hate a big stupid prompt, so I use the tcsh 'cwdcmd' alias (much better choice than 'precmd'): alias cwdcmd 'title "%#%c04 (%n@%m)"' and my title program (see below) to put all that crud in the xterm title (added benefit, it's in the icon title too). John ------------------------------ title.c --------------------------------------- /* * title: put stuff in xterm title using tcsh'ish escapes * : typically used as tcsh's `cwdcmd' * - John Hascall, ISU Comp Ctr * Freely distributable as long as ISU and I am credited. * * Note: may need local frobbing in function itshome() */ #include #define FALSE 0 #define TRUE 1 static void add_cdir(); static void add_host(); static void add_line(); static void add_time(); static void add_user(); static int itshome(); static char * itoa(); static int match(); static int levels(); static char * lastnofm(); extern char * getenv(); extern int getuid(); extern int isatty(); extern char * ttyname(); extern int write(); int main(argc, argv) int argc; char ** argv; { char buffer[8192]; char * p = buffer; char * q; int a = 0; int c; int n; int z; /* * if stdout is not a tty quit so we don't send junk down a pipe like: * (cd /src; tar cf - .) | tar xpf - */ if (!isatty(0)) return(0); /* * first, the special xterm escape sequence initiator */ *p++ = '\033'; *p++ = ']'; *p++ = '0'; *p++ = ';'; /* * now get either their format string or the default */ if (argc == 1) argv[argc++] = "%#%~ (%n@%m)"; while (++a < argc) { q = argv[a]; /* * process the format string... */ while (*q != '\0') { if ((c = *q++) != '%') { *p++ = c; continue; } switch (c = *q++) { case '%': /* %% inserts % */ *p++ = '%'; break; case '#': /* root indicator */ if (getuid() == 0) { /* note difference */ *p++ = '#'; /* from tcsh def'n */ *p++ = ' '; } break; case 'l': add_line(c, &p); break; case 'n': add_user(c, &p); break; case 'M': case 'm': add_host(c, &p); break; case '/': case '~': add_cdir(c, &p, (c == '~'), FALSE, FALSE, 0); break; case 'c': case '.': case 'C': z = FALSE; n = 1; if (*q == '0') { ++q; z = TRUE; } if ((*q >= '1') && (*q <= '9')) n = *q++ - '0'; add_cdir(c, &p, (c != 'C'), TRUE, z, n); break; case 't': case '@': case 'T': case 'd': case 'D': case 'w': case 'W': case 'y': case 'Y': add_time(c, &p); break; default: /* %X, if X unknown, then nothing */ break; } } *p++ = ' '; } /* * a hex 0x07 terminates the escape sequence (put it over last space) */ p[-1] = '\007'; /* * splat it out */ (void)write(1, buffer, p - buffer); return (0); } /* * get current dir with much twiddling and such... */ static void add_cdir(c, pp, subs, abbrev, before, n) int c; char ** pp; int subs; int abbrev; int before; int n; { char * p = *pp; char * pwd; char * w; char * u; char * ldr; int m; int tilde = FALSE; if ((pwd = getenv("PWD")) == NULL) pwd = "(Where?)"; /* * do ~[user] substitution if requested */ if (subs) { if ((m = itshome(pwd)) != 0) { /* it's a home */ pwd += m; if ((u = getenv("USER")) == NULL) u = ""; for (w = pwd; *u != '\0'; ++w,++u) { if (*u != *w) break; } if (*u == '\0') pwd = w; /* it's our home */ tilde = TRUE; } } /* * do they only want a trailing few components? */ if (abbrev) { ldr = pwd; m = levels(pwd); pwd = lastnofm(pwd, n, m); /* * if we actually trimmed the pwd... */ if (pwd != ldr) { /* * if they want a count of trimmed levels... */ if (before) { /* * leader (upto+including first /) comes first */ if (tilde) *p++ = '~'; /* add tilde */ while (*ldr != '\0') { if ((*p++ = *ldr++) == '/') break; } *p++ = '<'; w = itoa(m - n); while (*w != '\0') *p++ = *w++; *p++ = '>'; } tilde = FALSE; /* would be spurious now */ } } if (tilde) *p++ = '~'; /* add the tilde if needed */ /* * and the rest... */ while (*pwd != '\0') *p++ = *pwd++; *pp = p; } /* * FQDN or short hostname */ static void add_host(c, pp) int c; char ** pp; { char * p = *pp; char host[64] = "(Which?)"; char * hst = host; char * h; (void)gethostname(host, sizeof(host)); if (c == 'm') { for (h = hst; *h != '\0'; ++h) { if (*h == '.') { *h = '\0'; break; } } } while (*hst != '\0') *p++ = *hst++; *pp = p; } /* * ttyline (less /dev/ prefix) */ static void add_line(c, pp) int c; char ** pp; { char * p = *pp; char * t; int m; if ((t = ttyname(1)) == NULL) t = "(How?)"; if ((m = match(t, "/dev/")) != 0) t += m; while (*t != '\0') *p++ = *t++; *pp = p; } /* * various time/date bits */ static void add_time(c, pp) int c; char ** pp; { char * p = *pp; char * t; int hr; static time_t now = 0; static struct tm tm; static char * day[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; static char * mon[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; if (now == 0) { now = time(NULL); tm = (*localtime(&now)); } switch (c) { case 't': case '@': hr = tm.tm_hour; if (hr >= 12) hr -= 12; t = itoa(hr); while (*t != '\0') *p++ = *t++; *p++ = ':'; t = itoa(tm.tm_min); while (*t != '\0') *p++ = *t++; t = (hr == tm.tm_hour) ? "am" : "pm"; break; case 'T': t = itoa(tm.tm_hour); while (*t != '\0') *p++ = *t++; *p++ = ':'; t = itoa(tm.tm_min); break; case 'd': t = day[tm.tm_wday]; break; case 'D': t = itoa(tm.tm_mday); break; case 'w': t = mon[tm.tm_mon]; break; case 'W': t = itoa(tm.tm_mon + 1); break; case 'y': t = itoa(tm.tm_year); break; case 'Y': t = itoa(1900 + tm.tm_year); break; } while (*t != '\0') *p++ = *t++; *pp = p; } /* * username (in case they forget :) */ static void add_user(c, pp) int c; char ** pp; { char * p = *pp; char * u; if ((u = getenv("USER")) == NULL) u = "(Who?)"; while (*u != '\0') *p++ = *u++; *pp = p; } /* ====================== HELPER FUNCTIONS =========================== */ static int itshome(path) char * path; { int m; int m0; int m1; /* * first, some hopefully reasonably generic tests */ if ((m = match(path, "/home/")) != 0) return (m); if ((m = match(path, "/usr/users/")) != 0) return (m); if ((m = match(path, "/usr/people")) != 0) return (m); #ifdef ISU /* * now, some very local frobbing... */ if ((m = match(path, "/nfs/*fs-*/u*/")) != 0) return (m); if (((m0 = match(path, "/afs/iastate.edu/")) != 0) || ((m0 = match(path, "/afs/isu/")) != 0)) { if ((m1 = match(path + m0, "users/*/*/")) != 0) return (m0+m1); if ((m1 = match(path + m0, "group/")) != 0) return (m0+m1); if ((m1 = match(path + m0, "public/")) != 0) return (m0+m1); if ((m1 = match(path + m0, "private/")) != 0) return (m0+m1); if ((m1 = match(path + m0, "rental/")) != 0) return (m0+m1); if ((m1 = match(path + m0, "project/")) != 0) return (m0+m1); if ((m1 = match(path + m0, "academic/")) != 0) return (m0+m1); } #endif return (0); } /* * a not-very powerfull, but small wildcard matcher */ static int match(s, p) char * s; char * p; { char * q = s; while (*p != '\0') { if (*p == '*') { /* match stuff */ ++p; while (*q != *p) { if (*q == '\0') return (0); ++q; } continue; } if (*p++ != *q++) return (0); } return (q - s); } /* * int-to-ascii (note: static buffer) */ static char * itoa(i) int i; { static char buffer[16]; char * a = buffer + 16; *--a = '\0'; do { *--a = '0' + (i % 10); i /= 10; } while (i != 0); return (a); } /* * how many levels in a path? * (note: doesn't handle a trailing slash!) */ static int levels(path) char * path; { int lvls = 0; char * p = path; while (*p != '\0') { if (*p++ == '/') ++lvls; } return (lvls); } /* * point to last n levels of the m levels present in a path * lastnofm("/afs/iastate.edu/system/rw/dec/v4/usr", 3, 7) == "dec/v4/usr" * lastnofm("/usr/tmp", 2, 2) == "/usr/tmp" */ static char * lastnofm(path, n, m) char * path; int n; int m; { if (n >= m) return (path); /* all of it (or more :) */ for (m -= n; m >= 0; --m) { do {} while (*path++ != '/'); } return (path); } ---------------------------- title.1 ---------------------------------- .TH title 1 "31 December 1995" .ds ]W ISU Project Vincent .\" .\" First define macros "SB (small&bold)", "TY(typewriter)", & "M(man ref)" .\" .de SB .if !"\\$1"" \\s-1\\fB\&\\$1\\s0\\fR\\$2 \\$3 \\$4 \\$5 .. .\" .de T .ft CW .. .\" .de TY .if t .T .if n ``\c \\$1\c .if t .ft P .if n \&''\c \\$2 .. .\" .de M \\fI\\$1\\fR\\|(\\$2)\\$3 .. .\" .\" The manual page .\" .SH NAME title \- set the title of an xterm .SH SYNTAX .NXR "title command" .B title [ .I escapestring ] .SH DESCRIPTION The .PN title command sets an xterm's title and icon strings using .I escapestring . Typically, these strings are displayed by the window manager (e.g., in the wide bar at the top of border). The .I escapestring is scanned for \fB%\fIc\fR escape sequences. A subset of the .PN tcsh shell's prompt escape sequences is recognized. .PP If .I escapestring is omitted a default string of "\fB%#%~ (%n@%m)\fR" is used. .PP The title command can be invoked directly or, more commonly, through the .PN tcsh shell's special .I cwdcmd alias. .SH ESCAPES .EX \fB%%\fR A single % character. \fB%/\fR Current working directory. \fB%~\fR Current working directory, does \fB~\fR and \fB~\fIuser\fR substitution. \fB%c\fR[\fB0\fR][\fIn\fR] Trailing [\fIn\fR] component(s) of current working directory. Include \fB0\fR for a count of elided components. Does ~ and ~\fIuser\fR substitution. \fB%.\fR[\fB0\fR][\fIn\fR] Identical to \fB%c\fR[\fB0\fR][\fIn\fR] above. \fB%C\fR[\fB0\fR][\fIn\fR] As above, no \fB~\fR or \fB~\fIuser\fR substitution. \fB%#\fR If root, "\fB#\fR ", else "". \fB%n\fR The username. \fB%M\fR The full hostname. \fB%m\fR The short hostname. \fB%l\fR The line (tty) the user is logged on. \fB%t\fR Current time of day, in 12-hour, am/pm format. \fB%@\fR Identical to \fB%t\fR above. \fB%T\fR Current time of day, in 24-hour format. \fB%d\fR The weekday in format. \fB%D\fR The day in dd format. \fB%w\fR The month in format. \fB%W\fR The month in mm format. \fB%y\fR The year in yy format. \fB%Y\fR The year in yyyy format. .EE .SH EXAMPLES .EX title "this is a title" alias cwdcmd 'title "%c04 (%n@%m)"' alias telnet 'title \\!:*; /usr/ucb/telnet -ax \\!:*;cd .' .EX .SH ENVIRONMENT .EX $PWD Used for %/ %~ %c %. and %C escapes. $USER Used for %n escape. .EE .SH SEE ALSO .M xterm 1 , .M tcsh 1 , .M mwm 1 .SH AUTHOR John Hascall .I \c , \s-1Project Vincent\s0, ISU Computation Ctr. .SH COPYRIGHT Copyright 1995, \s-1Iowa State University\s0. All rights reserved. ---------------------------------- end ------------------------------------- -- John Hascall ``An ill-chosen word is the fool's messenger.'' Moderator, comp.unix.wizards Systems Software Engineer, ISU Comp Center + Ames, IA 50011 + 515/294-9551 My Homepage