From nobody Sat Sep 23 18:28:28 2023 X-Original-To: dev-commits-src-all@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 4RtHhF24cjz4v3Xc; Sat, 23 Sep 2023 18:28:29 +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 4RtHhF1ZXXz4pD4; Sat, 23 Sep 2023 18:28:29 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1695493709; 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=6kz5R3b98IUitzVDIuD3KI4ZjHAiKdscSkhx8kn3+uQ=; b=qH9/TIQYRJB1s8MvxqEIJMP124RyuDTbH5Y4a19n50Uom/qcCK+iYiBtV3Tyg/qyszvz9i 91eXrdAMxcGjFD10NJFyVN0ARoaGUfFqr+owTLm9HMXbTibdOnVGr0iUVNvGw4Noy1NU5I mPYog2A6rppNYgtdTObD2OI/HSyKA4g7Jgxz6P10pu01xuVsAIlSE5cw9gsQqHimSkTpqw eBgvMshE85lrobz4Zpe+GCCIePnRM8IhMX6jFqYhI5jb6Kt25aHbxkyc8YjvTcXGtvnpVw d8rHWpGuwCqZ5Q7mzvbCr5YvFPoYKdF5KbbjIr1uSlp7nEYBWzB0RKE0d60yLg== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1695493709; a=rsa-sha256; cv=none; b=BuBeKDGl4EhfsYOauKU4OspPsWPF3HsDL1AzFIsBT0R/njhhQ/zYryVgtxmPQFockkydvT HeNds5/pRGllNKhz6sL8tXMg2aA554FuXin+3BhggQlTOt6e+68tgupnZaS9jRt96IbCN3 6x3Pc19cml4E9LHPXeXQ0knKlNbqHvmAyDPd4X0pEnsDatq/NrBM9XMmHw+cn1IQl7o7b2 yWRRHPatUN2sbg2TBXdn4wgrJ1EI7OQbSp9V8mfMGi2wSvnjhin8dcK45SCKJ20B2h1brD 4woRHfnZb0xrIvx6EKeKa9siIvYTYZJ6yq/x3ymzprVdEWWlFMD4OXA4Ea+9rA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1695493709; 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=6kz5R3b98IUitzVDIuD3KI4ZjHAiKdscSkhx8kn3+uQ=; b=hno/GY5TRDILPm3ix/bWOqfP2iiZmkQVUzdxLu4g80/qlyfwAyy9tBNGa0KcalsJARZasG ZmgDJMl5zNoqVWxtMVXnCjFuxFMC67e8LBfbthAX8iEhOkp7zTI6hP4FHmhWtcfYBDaVQ4 GlbakLHDrTloBxZ0FRZcGwsUOqINdTgp7MPRBWGqFobSiUUw+T9sLARKrEu6sEG64XTzOW Kz+RLd+oA14wKWkucAE0AQrfrdb/sw/e9qkks+I9CBCtPfnWcclt6+JuhoG7aRxQiQcXbR qP1rEm4ei4gJ1Ba7kvbPxVs2v8YSyH8GZBpxQe4nPFHBlwtl8tThe3kgwU6j1w== 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 4RtHhF0KvNzVc; Sat, 23 Sep 2023 18:28:29 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 38NISSwW005232; Sat, 23 Sep 2023 18:28:28 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 38NISShU005229; Sat, 23 Sep 2023 18:28:28 GMT (envelope-from git) Date: Sat, 23 Sep 2023 18:28:28 GMT Message-Id: <202309231828.38NISShU005229@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Piotr Pawel Stefaniak Subject: git: a675eaec5aef - main - sh: implement PS1 \D to print current time List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: pstef X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: a675eaec5aef0089b6838aca8fd432fea0bd883b Auto-Submitted: auto-generated The branch main has been updated by pstef: URL: https://cgit.FreeBSD.org/src/commit/?id=a675eaec5aef0089b6838aca8fd432fea0bd883b commit a675eaec5aef0089b6838aca8fd432fea0bd883b Author: Piotr Pawel Stefaniak AuthorDate: 2023-01-01 19:38:35 +0000 Commit: Piotr Pawel Stefaniak CommitDate: 2023-09-23 18:26:45 +0000 sh: implement PS1 \D to print current time \D{format} yields the result of calling strftime(3) with the provided format and the current time. When PS4 can use this, it will enable us to easily generate timestamps when tracing script execution. Differential Revision: https://reviews.freebsd.org/D35840 --- bin/sh/parser.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ bin/sh/sh.1 | 10 ++++++++++ 2 files changed, 54 insertions(+) diff --git a/bin/sh/parser.c b/bin/sh/parser.c index 121c367c601c..8e959b46596b 100644 --- a/bin/sh/parser.c +++ b/bin/sh/parser.c @@ -43,6 +43,7 @@ static char sccsid[] = "@(#)parser.c 8.7 (Berkeley) 5/16/95"; #include #include #include +#include #include "shell.h" #include "parser.h" @@ -2100,6 +2101,49 @@ getprompt(void *unused __unused) } break; + /* + * Print the current time as per provided strftime format. + */ + case 'D': { + char tfmt[128] = "%X"; /* \D{} means %X. */ + struct tm *now; + + if (fmt[1] != '{') { + /* + * "\D" but not "\D{", so treat the '\' + * literally and rewind fmt to treat 'D' + * literally next iteration. + */ + ps[i] = '\\'; + fmt--; + break; + } + fmt += 2; /* Consume "D{". */ + if (fmt[0] != '}') { + char *end; + + end = memccpy(tfmt, fmt, '}', sizeof(tfmt)); + if (end == NULL) { + /* + * Format too long or no '}', so + * ignore "\D{" altogether. + * The loop will do i++, but nothing + * was written to ps, so do i-- here. + * Rewind fmt for similar reason. + */ + i--; + fmt--; + break; + } + *--end = '\0'; /* Ignore the copy of '}'. */ + fmt += end - tfmt; + } + now = localtime(&(time_t){time(NULL)}); + i += strftime(&ps[i], PROMPTLEN - i - 1, tfmt, now); + i--; /* The loop will do i++. */ + break; + } + /* * Hostname. * diff --git a/bin/sh/sh.1 b/bin/sh/sh.1 index fb3afc7d3d4d..adbc32827046 100644 --- a/bin/sh/sh.1 +++ b/bin/sh/sh.1 @@ -1427,6 +1427,16 @@ unless you are the superuser, in which case it defaults to may include any of the following formatting sequences, which are replaced by the given information: .Bl -tag -width indent +.It Li \eD{format} +The current time in +.Xr strftime 3 +.Ar format . +The braces are required. +Empty +.Ar format +is equivalent to +\&%X, +national representation of the time. .It Li \eH This system's fully-qualified hostname (FQDN). .It Li \eh