From nobody Thu Dec 22 18:13:05 2022 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4NdJMQ0J8Vz1H9Wh; Thu, 22 Dec 2022 18:13:06 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4NdJMP6mdhz48vC; Thu, 22 Dec 2022 18:13:05 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1671732785; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=PIV3D4V+XmEODj3QIFItw7ZV2nohkxwmkZ2fPa+FLtM=; b=R7eBohThCrxdGByPzGNQXSRUGGWwM9vKlh84KygbCzQHJ3RnJ8KZbPrBhzHXFvWfeJ/GeZ NOn9SWd1bZ58PpZtPs3QzFDYBS05P9ZIkodiwFSfhZYN0u93lzG0TFLy8wk+6e3UGd3yeB wPSqBTqLPHoQkTXFTLZrqvDLgg4hDrs7C0H26dBe0sDomTlQORpwnmhI4XgUbttLQtWDE3 GboYxHx4qthIXri+QTqTybz8kwTDxxIOTjQ5ZlutLVxQeEh8grLU59xjTYYG9V2rElZoNy S5iUTMQZgSM/Zjh1SgFMkTqegQwjp1cFgxoxmz+2LKPUZTAWQI7OVLF6CS6OPQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1671732785; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=PIV3D4V+XmEODj3QIFItw7ZV2nohkxwmkZ2fPa+FLtM=; b=C9FCrnlJH9PTX4Uc31rvUzp7VdCZLGCOcECALqGs5VACefx4xOwFwJt0XPJ7RXj5dODINm X95PUPXZwmyMh/ejP4jEy1JE/71GidrNJv/QxuqZsMLZDZ+SOHx5lv7M7fRtTD3HzABqX9 Wqp+pRPXN+8DLsmcADkfzEebIaw4KyiPwCaj3omtMOTs9hYImm+il8LgsnOReBl1GGMroD BtJYlaS19W43p8ohxlxOVrvGCLf2ZX3uXe94aVp8GHPBl/RsppcmCV800cWUa3+7YLx9ux S5bC25OHzFHqd4SXmyNFljv1ciE5DTWUxLA5GU9P4ApULDIyjW0M97smLEoylQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1671732785; a=rsa-sha256; cv=none; b=IrtS1xc+et4vtf/lVH4WY8OAMnCowy/ixJthlBpMRo3X1dv2dE3pYD/+6r3JUj1Dtwqoah 7zA8kgq8XsrMJilWAr8cKFID7qmreoE7bkRZ+uZ1CQJI3IfvM+TgWTfbFovDrnlKMA3rIO Tj9IByIVmrGb3pNOxm5MZa/2FEBXyR7y7NRbLGtN9WpYk92WuGLxMHNpFFVWibusVCHXVI a8xc/BrbpmGkUl92eGLpiK87mRczQHWvScfEh2pBtr7DD0Ik7vXTn81yAuOwmUSavfyXif nDiqVLNqhFvowyR/P6BqQ/IXMYQNSuFMCga11hsJizLolOaoI1hujKcu3xJwAQ== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4NdJMP5qHNz1494; Thu, 22 Dec 2022 18:13:05 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 2BMID5mj097539; Thu, 22 Dec 2022 18:13:05 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 2BMID5fd097538; Thu, 22 Dec 2022 18:13:05 GMT (envelope-from git) Date: Thu, 22 Dec 2022 18:13:05 GMT Message-Id: <202212221813.2BMID5fd097538@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Juraj Lutter Subject: git: 3cf65f8a7f8e - main - sh(1): Allow non-printing characters in prompt strings List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: otis X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 3cf65f8a7f8ee993118fe06f3c187ad30f875132 Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by otis (ports committer): URL: https://cgit.FreeBSD.org/src/commit/?id=3cf65f8a7f8ee993118fe06f3c187ad30f875132 commit 3cf65f8a7f8ee993118fe06f3c187ad30f875132 Author: Juraj Lutter AuthorDate: 2022-12-08 20:30:26 +0000 Commit: Juraj Lutter CommitDate: 2022-12-22 18:10:48 +0000 sh(1): Allow non-printing characters in prompt strings Introduce new prompt format characters: - '\[' starts the sequence of non-printing chatacters - '\]' ends the sequence of non-printing characters Within these sequences, the following characters are now supported: - '\a' emits ASCII BEL (0x07, 007) character - '\e' emits ASCII ESC (0x1b, 033) character - '\r' emits ASCII CR (0x0d, 015) character - '\n' emits ASCII CRLF sequence These can be used to embed ANSI sequences into prompt strings. Example in .shrc: PS1="\[\e[7m\]\u@\h\[\e[0m\]:\w \\$ " This tries to maintain some degree of compatibility with GNU bash, that uses GNU readline library (which behaves slightly different from BSD editline): It has two "non-printing boundary" characters: - RL_PROMPT_START_IGNORE (\001) - RL_PROMPT_END_IGNORE (\002) while BSD editline only has one (when using EL_PROMPT_ESC setting), so for this purpose, ASCII \001 was chosen and both \[ and \] emits this character. And while here, enlarge PROMPTLEN from 128 to 192 characters. Reviewed by: jilles Approved by: jilles Differential Revision: https://reviews.freebsd.org/D37701 --- bin/sh/histedit.c | 2 +- bin/sh/parser.c | 47 +++++++++++++++++++++++++++++++++++++++-------- bin/sh/sh.1 | 20 +++++++++++++++++++- 3 files changed, 59 insertions(+), 10 deletions(-) diff --git a/bin/sh/histedit.c b/bin/sh/histedit.c index 8812200279f0..0eb8c6c1784f 100644 --- a/bin/sh/histedit.c +++ b/bin/sh/histedit.c @@ -190,7 +190,7 @@ histedit(void) if (el != NULL) { if (hist) el_set(el, EL_HIST, history, hist); - el_set(el, EL_PROMPT, getprompt); + el_set(el, EL_PROMPT_ESC, getprompt, '\001'); el_set(el, EL_ADDFN, "sh-complete", "Filename completion", sh_complete); diff --git a/bin/sh/parser.c b/bin/sh/parser.c index e75798800edf..7f8283ca4dff 100644 --- a/bin/sh/parser.c +++ b/bin/sh/parser.c @@ -70,7 +70,7 @@ __FBSDID("$FreeBSD$"); * Shell command parser. */ -#define PROMPTLEN 128 +#define PROMPTLEN 192 /* values of checkkwd variable */ #define CHKALIAS 0x1 @@ -2060,6 +2060,44 @@ getprompt(void *unused __unused) if (*fmt == '\\') switch (*++fmt) { + /* + * Non-printing sequence begin and end. + */ + case '[': + case ']': + ps[i] = '\001'; + break; + + /* + * Literal \ and some ASCII characters: + * \a BEL + * \e ESC + * \r CR + */ + case '\\': + case 'a': + case 'e': + case 'r': + if (*fmt == 'a') + ps[i] = '\007'; + else if (*fmt == 'e') + ps[i] = '\033'; + else if (*fmt == 'r') + ps[i] = '\r'; + else + ps[i] = '\\'; + break; + + /* + * CRLF sequence + */ + case 'n': + if (i < PROMPTLEN - 3) { + ps[i++] = '\r'; + ps[i] = '\n'; + } + break; + /* * Hostname. * @@ -2136,13 +2174,6 @@ getprompt(void *unused __unused) ps[i] = (geteuid() != 0) ? '$' : '#'; break; - /* - * A literal \. - */ - case '\\': - ps[i] = '\\'; - break; - /* * Emit unrecognized formats verbatim. */ diff --git a/bin/sh/sh.1 b/bin/sh/sh.1 index 7a30020f2f7f..330685a911e1 100644 --- a/bin/sh/sh.1 +++ b/bin/sh/sh.1 @@ -32,7 +32,7 @@ .\" from: @(#)sh.1 8.6 (Berkeley) 5/4/95 .\" $FreeBSD$ .\" -.Dd May 10, 2021 +.Dd December 14, 2022 .Dt SH 1 .Os .Sh NAME @@ -1446,6 +1446,24 @@ for normal users and for superusers. .It Li \e\e A literal backslash. +.It Li \e[ +Start of a sequence of non-printing characters (used, for example, +to embed ANSI CSI sequences into the prompt). +.It Li \e] +End of a sequence of non-printing characters. +.El +.Pp +The following special and non-printing characters are supported +within the sequence of non-printing characters: +.Bl -tag -width indent +.It Li \ea +Emits ASCII BEL (0x07, 007) character. +.It Li \ee +Emits ASCII ESC (0x1b, 033) character. +.It Li \er +Emits ASCII CR (0x0d, 015) character. +.It Li \en +Emits CRLF sequence. .El .It Va PS2 The secondary prompt string, which defaults to