Built-in String Processing?
Barr3y Jaspan
bjaspan at athena.mit.edu
Mon Oct 29 08:56:56 AEST 1990
In article <1990Oct27.215051.14085 at midway.uchicago.edu>, phd_ivo at gsbacd.uchicago.edu writes:
|> *************
|> I am running a csh with enhancements on my NeXT and on an HP-9000/8xx.
|> I have a macro to define my prompt to be the current directory, i.e.
|>
|> alias cd 'cd \!* && set prompt="\! ${cwd}> "'
|>
|> Unfortunately, this gives me a rather long string...
I wrote a short little hack to deal with this about a year ago. Basically, it
can do the following things:
1) Truncate the cwd to a specified number of directories.
2) Replace a specified directory prefix with a constant string.
3) Remove a specified number of characters from the remainder of the cwd
after replacing a prefix.
We use AFS extensively at Athena (which results in VERY long path names!) so
I can easily provide examples of all three of these "features":
cwd my prompt is
=================================== ==================================
/mit/bjaspan ~bjaspan%
/afs/athena/user/b/bjaspan ~bjaspan%
/afs/athena/astaff/FOO astaff: FOO%
/afs/athena/astaff/kerberos/FOO kerberos: FOO%
/one/two/three/four/five .../two/three/four/five%
The aliases that do all this magic for me are as follows:
alias pcmd '/mit/bjaspan/${bindir}/prompt 4 $cwd $home 0 ~ \
/afs/athena.mit.edu/user/b/bjaspan 0 ~ /afs/athena.mit.edu/user 3 ~ \
/afs/athena/user 3 ~ /afs/sipb.mit.edu/project/sipbsrc 1 "sipbsrc: " \
/afs/athena.mit.edu/astaff/project 1 "aproj: " \
/afs/rel-eng.athena.mit.edu/project/release/current/source 1 "source: " \
/source 1 "source: "'
alias cd_magic 'set noglob && set prompt = "<%m> `pcmd`% " && unset noglob'
alias cd 'cd \!* && cd_magic'
alias pushd 'pushd \!* && cd_magic'
alias popd 'popd \!* && cd_magic'
Note that I've just copied these from my .aliases file. The program that
actually does the real work, called "prompt", is included below. It is
a bare-bones program (compiles to 3K on a VAX) that does NO ERROR CHECKING.
This qualifies as a gross hack of the first order, which means I don't care
if it dumps core on you because it works for me. :-) Your mileage
may vary. Feel free to send me bugs, though.
Barr3y Jaspan, bjaspan at athena.mit.edu
PS: I don't read this newgroup regularly (someone forwarded your question to
me because he knew I had written a prompt hack) so send replies personally.
--- snip snip ---
#define MHT 10
#define USAGE "Usage: prompt max_dirs cwd [ hometop offset text ... ]\n"
int left_bcopy(src, dest, len)
char *src, *dest;
int len;
{
while (len--)
*dest++ = *src++;
}
main(argc, argv)
int argc;
char **argv;
{
char *cwd, *hometop[MHT], *hometop_text[MHT];
int cwd_len, hometop_len[MHT], hometop_offset[MHT];
int hometop_num, i, max_dirs, count;
if (argc < 3) {
write(1, USAGE, strlen(USAGE));
exit(1);
}
hometop_num = i = 0;
max_dirs = atoi(*++argv);
cwd = *++argv;
cwd_len = strlen(cwd);
while ((hometop_num < MHT) && (hometop[hometop_num] = *++argv)) {
hometop_offset[hometop_num] = atoi(*++argv);
hometop_text[hometop_num] = *++argv;
hometop_len[hometop_num] = strlen(hometop[hometop_num]);
++hometop_num;
}
while (i < hometop_num) {
if (strncmp(hometop[i], cwd, hometop_len[i])==0 &&
(cwd_len >= hometop_len[i]+hometop_offset[i])) {
left_bcopy(cwd+hometop_len[i]+hometop_offset[i], cwd,
cwd_len-hometop_len[i]);
cwd[cwd_len-hometop_len[i]] = '\0';
cwd_len = strlen(cwd);
write(1, hometop_text[i], strlen(hometop_text[i]));
break; }
++i;
}
if (max_dirs) {
for (i=cwd_len, count=0; i>0; i--) {
if (*(cwd+i) == '/' && (++count == max_dirs)) {
write(1, "...", 3);
write(1, cwd+i, cwd_len-i);
return 0; }
}
}
write(1, cwd, cwd_len);
return 0;
}
More information about the Comp.unix.shell
mailing list