zsh-workers
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: Zsh hackers list <zsh-workers@zsh.org>
Subject: Re: Bug: time doesn't work on builtins
Date: Fri, 16 Aug 2024 12:20:11 -0700	[thread overview]
Message-ID: <CAH+w=7ZZ3frU9OjpkOmHzvL6Ws0YQPE=_JK+izmRR+gEcvSsDg@mail.gmail.com> (raw)
In-Reply-To: <CAA=-s3z3YMho0a4A3H4h3o75oY1fR36w2zz83zQKjyPi3mSTpQ@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 169 bytes --]

On Thu, Aug 15, 2024 at 3:08 PM Mark J. Reed <markjreed@gmail.com> wrote:
>
> Absent a rewrite of the functionality to actually work on builtins

Let's try this.

[-- Attachment #2: time-builtins.txt --]
[-- Type: text/plain, Size: 2956 bytes --]

diff --git a/Src/exec.c b/Src/exec.c
index 00278ac50..3c04c5efd 100644
--- a/Src/exec.c
+++ b/Src/exec.c
@@ -2938,6 +2938,11 @@ execcmd_exec(Estate state, Execcmd_params eparams,
      * in order to check for prefix commands.
      */
     LinkList preargs;
+    /*
+     * for the "time" builtin
+     */
+    child_times_t shti, chti;
+    struct timeval then;
 
     doneps4 = 0;
 
@@ -3621,6 +3626,8 @@ execcmd_exec(Estate state, Execcmd_params eparams,
 
     /* This is nonzero if the command is a current shell procedure? */
     is_cursh = (is_builtin || is_shfunc || nullexec || type >= WC_CURSH);
+    if (is_cursh && (how & Z_TIMED))
+	shelltime(&shti, &chti, &then, 0);
 
     /**************************************************************************
      * Do we need to fork?  We need to fork if:                               *
@@ -4377,6 +4384,8 @@ execcmd_exec(Estate state, Execcmd_params eparams,
 	    errflag |= ERRFLAG_ERROR;
 	}
     }
+    if (is_cursh && (how & Z_TIMED))
+	shelltime(&shti, is_builtin ? NULL : &chti, &then, 1);
     if (newxtrerr) {
 	int eno = errno;
 	fil = fileno(newxtrerr);
@@ -5265,7 +5274,7 @@ exectime(Estate state, UNUSED(int do_exec))
 
     jb = thisjob;
     if (WC_TIMED_TYPE(state->pc[-1]) == WC_TIMED_EMPTY) {
-	shelltime();
+	shelltime(NULL,NULL,NULL,0);
 	return 0;
     }
     execpline(state, *state->pc++, Z_TIMED|Z_SYNC, 0);
diff --git a/Src/jobs.c b/Src/jobs.c
index 07facc60b..39c664388 100644
--- a/Src/jobs.c
+++ b/Src/jobs.c
@@ -1894,7 +1894,7 @@ spawnjob(void)
 
 /**/
 void
-shelltime(void)
+shelltime(child_times_t *shell, child_times_t *kids, struct timeval *then, int delta)
 {
     struct timezone dummy_tz;
     struct timeval dtimeval, now;
@@ -1913,7 +1913,28 @@ shelltime(void)
     ti.ut = buf.tms_utime;
     ti.st = buf.tms_stime;
 #endif
-    printtime(dtime(&dtimeval, &shtimer, &now), &ti, "shell");
+    if (shell) {
+	if (delta) {
+#ifdef HAVE_GETRUSAGE
+	    dtime(&ti.ru_utime, &shell->ru_utime, &ti.ru_utime);
+	    dtime(&ti.ru_stime, &shell->ru_stime, &ti.ru_stime);
+#else
+	    ti.ut -= shell->ut;
+	    ti.st -= shell->st;
+#endif
+	} else
+	    *shell = ti;
+    }
+    if (delta)
+	dtime(&dtimeval, then, &now);
+    else {
+	if (then)
+	    *then = now;
+	dtime(&dtimeval, &shtimer, &now);
+    }
+
+    if (!delta == !shell)
+	printtime(&dtimeval, &ti, "shell");
 
 #ifdef HAVE_GETRUSAGE
     getrusage(RUSAGE_CHILDREN, &ti);
@@ -1921,8 +1942,20 @@ shelltime(void)
     ti.ut = buf.tms_cutime;
     ti.st = buf.tms_cstime;
 #endif
-    printtime(&dtimeval, &ti, "children");
-
+    if (kids) {
+	if (delta) {
+#ifdef HAVE_GETRUSAGE
+	    dtime(&ti.ru_utime, &kids->ru_utime, &ti.ru_utime);
+	    dtime(&ti.ru_stime, &kids->ru_stime, &ti.ru_stime);
+#else
+	    ti.ut -= shell->ut;
+	    ti.st -= shell->st;
+#endif
+	} else
+	    *kids = ti;
+    }
+    if (!delta == !kids)
+	printtime(&dtimeval, &ti, "children");
 }
 
 /* see if jobs need printing */

  reply	other threads:[~2024-08-16 19:21 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-08-15 13:15 Mark J. Reed
2024-08-15 18:52 ` Bart Schaefer
2024-08-15 22:08   ` Mark J. Reed
2024-08-16 19:20     ` Bart Schaefer [this message]
2024-09-04  0:27       ` Bart Schaefer
2024-09-04 14:32         ` Jun. T
2024-09-05 17:29           ` Jun. T
2024-09-05 23:37             ` Bart Schaefer
2024-09-06  9:41               ` Jun T
2024-09-06  0:10           ` Bart Schaefer
2024-09-06  9:52             ` Jun T
2024-09-06 19:13               ` Bart Schaefer
2024-09-14  2:10                 ` PATCH: " Bart Schaefer
2024-09-20  7:46                   ` Oliver Kiddle
2024-09-22  0:59                     ` Bart Schaefer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAH+w=7ZZ3frU9OjpkOmHzvL6Ws0YQPE=_JK+izmRR+gEcvSsDg@mail.gmail.com' \
    --to=schaefer@brasslantern.com \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).