Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/make Make :mtime operate on each word in variable va...



details:   https://anonhg.NetBSD.org/src/rev/a25ad3ca0c84
branches:  trunk
changeset: 374723:a25ad3ca0c84
user:      sjg <sjg%NetBSD.org@localhost>
date:      Wed May 10 18:22:33 2023 +0000

description:
Make :mtime operate on each word in variable value.

Reviewed by: rillig

diffstat:

 usr.bin/make/make.1 |   8 ++--
 usr.bin/make/var.c  |  82 ++++++++++++++++++++++++++++++++--------------------
 2 files changed, 55 insertions(+), 35 deletions(-)

diffs (147 lines):

diff -r 3b2d5e306193 -r a25ad3ca0c84 usr.bin/make/make.1
--- a/usr.bin/make/make.1       Wed May 10 16:46:39 2023 +0000
+++ b/usr.bin/make/make.1       Wed May 10 18:22:33 2023 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: make.1,v 1.365 2023/05/09 20:53:23 sjg Exp $
+.\"    $NetBSD: make.1,v 1.366 2023/05/10 18:22:33 sjg Exp $
 .\"
 .\" Copyright (c) 1990, 1993
 .\"    The Regents of the University of California.  All rights reserved.
@@ -29,7 +29,7 @@
 .\"
 .\"    from: @(#)make.1        8.4 (Berkeley) 3/19/94
 .\"
-.Dd May 9, 2023
+.Dd May 10, 2023
 .Dt MAKE 1
 .Os
 .Sh NAME
@@ -1511,9 +1511,9 @@ If a
 .Ar timestamp
 value is not provided or is 0, the current time is used.
 .It Cm \&:mtime Ns Oo Cm = Ns Ar timestamp Oc
-call
+Call
 .Xr stat 2
-with the value as pathname;
+with each word as pathname;
 use
 .Ql st_mtime
 as the new value.
diff -r 3b2d5e306193 -r a25ad3ca0c84 usr.bin/make/var.c
--- a/usr.bin/make/var.c        Wed May 10 16:46:39 2023 +0000
+++ b/usr.bin/make/var.c        Wed May 10 18:22:33 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: var.c,v 1.1053 2023/05/09 21:24:56 rillig Exp $        */
+/*     $NetBSD: var.c,v 1.1054 2023/05/10 18:22:33 sjg Exp $   */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -139,7 +139,7 @@
 #include "metachar.h"
 
 /*     "@(#)var.c      8.3 (Berkeley) 3/19/94" */
-MAKE_RCSID("$NetBSD: var.c,v 1.1053 2023/05/09 21:24:56 rillig Exp $");
+MAKE_RCSID("$NetBSD: var.c,v 1.1054 2023/05/10 18:22:33 sjg Exp $");
 
 /*
  * Variables are defined using one of the VAR=value assignments.  Their
@@ -2833,49 +2833,69 @@ ApplyModifier_Match(const char **pp, Mod
        return AMR_OK;
 }
 
+struct ModifyWord_MtimeArgs {
+       bool error;
+       bool fallback;
+       ApplyModifierResult rc;
+       time_t t;
+};
+
+static void
+ModifyWord_Mtime(Substring word, SepBuf *buf, void *data)
+{
+       char tbuf[BUFSIZ];
+       struct stat st;
+       struct ModifyWord_MtimeArgs *args = data;
+
+       if (Substring_IsEmpty(word))
+               return;
+       assert(word.end[0] == '\0');    /* assume null-terminated word */
+       if (stat(word.start, &st) < 0) {
+               if (args->error) {
+                       Parse_Error(PARSE_FATAL,
+                           "Cannot determine mtime for '%s': %s",
+                           word.start, strerror(errno));
+                       args->rc = AMR_CLEANUP;
+                       return;
+               }
+               if (args->fallback)
+                       st.st_mtime = args->t;
+               else
+                       time(&st.st_mtime);
+       }
+       snprintf(tbuf, sizeof(tbuf), "%u", (unsigned)st.st_mtime);
+       SepBuf_AddStr(buf, tbuf);
+}
+
 /* :mtime */
 static ApplyModifierResult
 ApplyModifier_Mtime(const char **pp, ModChain *ch)
 {
-       char buf[BUFSIZ];
-       Expr *expr = ch->expr;
-       const char *args, *mod = *pp;
-       struct stat st;
-       bool error = false;
-       int i = -1;
+       const char *p, *mod = *pp;
+       struct ModifyWord_MtimeArgs args;
 
        if (!ModMatchEq(mod, "mtime", ch))
                return AMR_UNKNOWN;
        *pp += 5;
-       args = *pp;
-       if (args[0] == '=') {
-               args++;
-               if (!TryParseIntBase0(&args, &i)) {
-                       if (strncmp(args, "error", 5) == 0) {
-                               error = true;
-                               args += 5;
+       p = *pp;
+       args.error = args.fallback = false;
+       args.rc = AMR_OK;
+       if (p[0] == '=') {
+               p++;
+               args.fallback = true;
+               if (!TryParseTime(&p, &args.t)) {
+                       if (strncmp(p, "error", 5) == 0) {
+                               args.error = true;
+                               p += 5;
                        } else
                                return AMR_BAD;
                }
-               *pp = args;
+               *pp = p;
        }
        if (!ModChain_ShouldEval(ch))
                return AMR_OK;
-       if (stat(Expr_Str(expr), &st) < 0) {
-               if (error) {
-                       Parse_Error(PARSE_FATAL,
-                           "Cannot determine mtime for '%s': %s",
-                           Expr_Str(expr), strerror(errno));
-                       return AMR_CLEANUP;
-               }
-               if (i < 0)
-                       time(&st.st_mtime);
-               else
-                       st.st_mtime = (time_t)i;
-       }
-       snprintf(buf, sizeof(buf), "%u", (unsigned)st.st_mtime);
-       Expr_SetValueOwn(expr, bmake_strdup(buf));
-       return AMR_OK;
+       ModifyWords(ch, ModifyWord_Mtime, &args, ch->oneBigWord);
+       return args.rc;
 }
 
 static void



Home | Main Index | Thread Index | Old Index