From: tb Date: Tue, 12 Dec 2017 00:18:58 +0000 (+0000) Subject: Include hostname in shell prompts by default X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=0a9a54a35dd51b7f9951e810334b28bf33b99e6a;p=openbsd Include hostname in shell prompts by default With tmux, ssh and vmd, we tend to open shells on many different hosts simultaneously and the default prompts '$ ' and '# ' for {,k}sh as well as '% ' and '# ' for csh become dangerous: it's very easy to issue a command on the wrong host. This can easily be avoided by displaying the hostname in the prompt. Everything beyond "hostname{$,#,%} " is going to be a matter of taste, so we left it at that. If you use an FQDN, only the first part (the output of 'hostname -s') will be printed. requested by and ok deraadt; mostly positive feedback many ok anton, brynet, bcallah and others --- diff --git a/bin/csh/const.c b/bin/csh/const.c index 9307f2d370d..532c74b63fa 100644 --- a/bin/csh/const.c +++ b/bin/csh/const.c @@ -1,4 +1,4 @@ -/* $OpenBSD: const.c,v 1.8 2015/10/26 16:27:04 naddy Exp $ */ +/* $OpenBSD: const.c,v 1.9 2017/12/12 00:18:58 tb Exp $ */ /* $NetBSD: const.c,v 1.6 1995/03/21 09:02:31 cgd Exp $ */ /*- @@ -114,6 +114,8 @@ Char STRprintexitvalue[] = { 'p', 'r', 'i', 'n', 't', 'e', 'x', 'i', 't', 'v', 'a', 'l', 'u', 'e', '\0' }; Char STRprompt[] = { 'p', 'r', 'o', 'm', 'p', 't', '\0' }; Char STRprompt2[] = { 'p', 'r', 'o', 'm', 'p', 't', '2', '\0' }; +Char STRpromptroot[] = { '%', 'm', '#', ' ', '\0' }; +Char STRpromptuser[] = { '%', 'm', '%', ' ', '\0' }; Char STRpushdsilent[] = { 'p', 'u', 's', 'h', 'd', 's', 'i', 'l', 'e', 'n', 't', '\0' }; Char STRret[] = { '\n', '\0' }; @@ -138,8 +140,6 @@ Char STRspor2sp[] = { ' ', '|', '|', ' ', '\0' }; Char STRsporsp[] = { ' ', '|', ' ', '\0' }; Char STRstar[] = { '*', '\0' }; Char STRstatus[] = { 's', 't', 'a', 't', 'u', 's', '\0' }; -Char STRsymcent[] = { '%', ' ', '\0' }; -Char STRsymhash[] = { '#', ' ', '\0' }; Char STRterm[] = { 't', 'e', 'r', 'm', '\0' }; Char STRthen[] = { 't', 'h', 'e', 'n', '\0' }; Char STRtilde[] = { '~', '\0' }; diff --git a/bin/csh/csh.c b/bin/csh/csh.c index 5df655ade5d..d737c31ab30 100644 --- a/bin/csh/csh.c +++ b/bin/csh/csh.c @@ -1,4 +1,4 @@ -/* $OpenBSD: csh.c,v 1.41 2017/08/30 06:42:21 anton Exp $ */ +/* $OpenBSD: csh.c,v 1.42 2017/12/12 00:18:58 tb Exp $ */ /* $NetBSD: csh.c,v 1.14 1995/04/29 23:21:28 mycroft Exp $ */ /*- @@ -401,7 +401,7 @@ main(int argc, char *argv[]) * Set up the prompt. */ if (prompt) { - set(STRprompt, Strsave(uid == 0 ? STRsymhash : STRsymcent)); + set(STRprompt, Strsave(uid == 0 ? STRpromptroot : STRpromptuser)); /* that's a meta-questionmark */ set(STRprompt2, Strsave(STRmquestion)); } @@ -1283,7 +1283,16 @@ printprompt(void) for (cp = value(STRprompt); *cp; cp++) if (*cp == HIST) (void) fprintf(cshout, "%d", eventno + 1); - else { + else if (*cp == '%' && *(cp + 1) == 'm') { + char hostname[HOST_NAME_MAX + 1]; + char *p; + + gethostname(hostname, sizeof hostname); + if ((p = strchr(hostname, '.')) != NULL) + *p = '\0'; + fprintf(cshout, "%s", hostname); + cp++; + } else { if (*cp == '\\' && cp[1] == HIST) cp++; (void) vis_fputc(*cp | QUOTE, cshout); diff --git a/bin/ksh/lex.c b/bin/ksh/lex.c index db32396fb92..858eb898cbf 100644 --- a/bin/ksh/lex.c +++ b/bin/ksh/lex.c @@ -1,4 +1,4 @@ -/* $OpenBSD: lex.c,v 1.72 2017/12/07 01:54:33 tb Exp $ */ +/* $OpenBSD: lex.c,v 1.73 2017/12/12 00:18:58 tb Exp $ */ /* * lexical analysis and source input @@ -1246,7 +1246,8 @@ dopprompt(const char *sp, int ntruncate, const char **spp, int doprint) cp++; if (!*cp) break; - if (Flag(FSH)) + /* Expand \h and \$ for both, sh(1) and ksh(1) */ + if (Flag(FSH) && !(*cp == 'h' || *cp == 'p')) snprintf(strbuf, sizeof strbuf, "\\%c", *cp); else switch (*cp) { case 'a': /* '\' 'a' bell */ diff --git a/bin/ksh/main.c b/bin/ksh/main.c index eef7e3041ae..5676d63133a 100644 --- a/bin/ksh/main.c +++ b/bin/ksh/main.c @@ -1,4 +1,4 @@ -/* $OpenBSD: main.c,v 1.84 2017/12/07 01:54:33 tb Exp $ */ +/* $OpenBSD: main.c,v 1.85 2017/12/12 00:18:58 tb Exp $ */ /* * startup, main loop, environments and error handling @@ -316,7 +316,7 @@ main(int argc, char *argv[]) /* Set PS1 if it isn't set */ if (!(vp->flag & ISSET)) { /* setstr can't fail here */ - setstr(vp, safe_prompt, KSH_RETURN_ERROR); + setstr(vp, "\\h\\$ ", KSH_RETURN_ERROR); } }