Module Name:    src
Committed By:   martin
Date:           Tue Oct 15 18:34:41 UTC 2024

Modified Files:
        src/bin/date [netbsd-9]: date.c
        src/external/gpl2/gmake/dist [netbsd-9]: main.c
        src/sys/sys [netbsd-9]: signal.h
        src/usr.bin/sed [netbsd-9]: process.c

Log Message:
Pull up following revision(s) (requested by kre in ticket #1914):

        bin/date/date.c (apply patch)
        usr.bin/sed/process.c: revision 1.54
        sys/sys/signal.h: revision 1.77
        sys/sys/signal.h: revision 1.78
        external/gpl2/gmake/dist/main.c: revision 1.2

PR lib/58674

When building the tools version of sed, treat all wide characters
as if they occupy just one column for the purposes of sed's 'l'
command (which it is very unlikely to be used from the tools sed).
wdwidth() is another XSI function, not necessarily available everywhere.

PR lib/58674
bsd_signal should only be visible with _NETBSD_SOURCE - it isn't any kind of
standard function (despite also existing in other systems).

This change inspired by the PR, but doesn't fix it in any way, the tools
config script for gmake doesn't care if the function is visible in any
header, merely if present in libc.

PR lib/58674 (not really so much any more)
Correct previous.
bsd_signal should only be visible with _NETBSD_SOURCE - it isn't any kind of
standard function (despite also existing in other systems).

Turns out that it used to be an XSI function, back in the dark ages
('twas removed in POSIX issue 7, back in 2008, after being marked
obsolete in issue 6 (2001)).

So, make it visible to any applications that request a suitable
X/Open version (and of course, for _NETBSD_SOURCE).
Still no effect on the issue for the PR.

PR lib/58674
Hopefully allow the tools gmake to build (everywhere).

Don't use the system bsd_signal() function, even if one is
defined, use a locally defined one instead.   Note that it
cannot be declared static (which the code would do) as it
is possible that system header files might define the function,
if it exists on the host system, and that prototype would not
(cannot) be static.

This is a horrible hack, feel free to do something better.

Note: this version of gmake is (currently anyway) used only
as part of the tools used for building NetBSD - apart from that
it is used for nothing.


To generate a diff of this commit:
cvs rdiff -u -r1.61.18.1 -r1.61.18.2 src/bin/date/date.c
cvs rdiff -u -r1.1.1.1 -r1.1.1.1.18.1 src/external/gpl2/gmake/dist/main.c
cvs rdiff -u -r1.72 -r1.72.16.1 src/sys/sys/signal.h
cvs rdiff -u -r1.52 -r1.52.18.1 src/usr.bin/sed/process.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/bin/date/date.c
diff -u src/bin/date/date.c:1.61.18.1 src/bin/date/date.c:1.61.18.2
--- src/bin/date/date.c:1.61.18.1	Sat Jun  3 15:27:13 2023
+++ src/bin/date/date.c	Tue Oct 15 18:34:40 2024
@@ -1,4 +1,4 @@
-/* $NetBSD: date.c,v 1.61.18.1 2023/06/03 15:27:13 martin Exp $ */
+/* $NetBSD: date.c,v 1.61.18.2 2024/10/15 18:34:40 martin Exp $ */
 
 /*
  * Copyright (c) 1985, 1987, 1988, 1993
@@ -44,7 +44,7 @@ __COPYRIGHT(
 #if 0
 static char sccsid[] = "@(#)date.c	8.2 (Berkeley) 4/28/95";
 #else
-__RCSID("$NetBSD: date.c,v 1.61.18.1 2023/06/03 15:27:13 martin Exp $");
+__RCSID("$NetBSD: date.c,v 1.61.18.2 2024/10/15 18:34:40 martin Exp $");
 #endif
 #endif /* not lint */
 
@@ -72,12 +72,21 @@ __RCSID("$NetBSD: date.c,v 1.61.18.1 202
 
 static time_t tval;
 static int Rflag, aflag, jflag, rflag, nflag;
-static char *fmt;
 
 __dead static void badcanotime(const char *, const char *, size_t);
 static void setthetime(const char *);
 __dead static void usage(void);
 
+#if HAVE_NBTOOL_CONFIG_H
+static int parse_iso_datetime(time_t *, const char *);
+#else
+static char *fmt;
+#endif
+
+#if !defined(isleap)
+# define isleap(y)   (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
+#endif
+
 int
 main(int argc, char *argv[])
 {
@@ -98,21 +107,25 @@ main(int argc, char *argv[])
 			nflag = 1;
 			break;
 		case 'd':
-#ifndef HAVE_NBTOOL_CONFIG_H
 			rflag = 1;
+#ifdef HAVE_NBTOOL_CONFIG_H
+			if (parse_iso_datetime(&tval, optarg))
+				break;
+			errx(EXIT_FAILURE,
+			    "-d only supports ISO format in the tool version");
+			break;
+#else
+			errno = 0;
 			tval = parsedate(optarg, NULL, NULL);
-			if (tval == -1) {
+			if (tval == -1 && errno != 0) {
 				errx(EXIT_FAILURE,
 				    "%s: Unrecognized date format", optarg);
 			}
 			break;
-#else
-			errx(EXIT_FAILURE,
-			    "-d not supported in the tool version");
-#endif
 		case 'f':
 			fmt = optarg;
 			break;
+#endif
 		case 'j':		/* don't set time */
 			jflag = 1;
 			break;
@@ -165,8 +178,11 @@ main(int argc, char *argv[])
 	if (*argv) {
 		setthetime(*argv);
 		++argv;
-	} else if (fmt)
+#ifndef HAVE_NBTOOL_CONFIG_H
+	} else if (fmt) {
 		usage();
+#endif
+	}
 
 	if (*argv && **argv == '+')
 		format = *argv;
@@ -199,6 +215,122 @@ badcanotime(const char *msg, const char 
 
 #define ATOI2(s) ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0'))
 
+#if HAVE_NBTOOL_CONFIG_H
+
+inline static int
+digitstring(const char *s, int len)
+{
+	while (--len > 0) {
+		if (!isdigit(*(unsigned char *)s))
+			return 0;
+		s++;
+	}
+	return 1;
+}
+
+static int
+parse_iso_datetime(time_t * res, const char * string)
+{
+	struct tm tm;
+	time_t t;
+
+	memset(&tm, 0, sizeof tm);
+
+	if (!digitstring(string, 4))
+		return 0;
+	tm.tm_year = ATOI2(string) * 100;
+	tm.tm_year += ATOI2(string);
+	tm.tm_year -= 1900;
+
+	if (*string == '-')
+		string++;
+
+	if (!digitstring(string, 2))
+		return 0;
+
+	tm.tm_mon = ATOI2(string);
+	if (tm.tm_mon < 1 || tm.tm_mon > 12)
+		return 0;
+	tm.tm_mon--;
+
+	if (*string == '-')
+		string++;
+
+	if (!digitstring(string, 2))
+		return 0;
+
+	tm.tm_mday = ATOI2(string);
+	if (tm.tm_mday < 1)
+		return 0;
+	switch (tm.tm_mon) {
+	case 0: case 2: case 4: case 6: case 7: case 9: case 11:
+		if (tm.tm_mday > 31)
+			return 0;
+		break;
+	case 3: case 5: case 8: case 10:
+		if (tm.tm_mday > 30)
+			return 0;
+		break;
+	case 1:
+		if (tm.tm_mday > 28 + isleap(tm.tm_year + 1900))
+			return 0;
+		break;
+	default:
+		abort();
+	}
+
+	do {
+		if (*string == '\0')
+			break;
+		if (*string == 'T' || *string == 't' || *string == ' ' ||
+		    *string == '-')
+			string++;
+
+		if (!digitstring(string, 2))
+			return 0;
+		tm.tm_hour = ATOI2(string);
+		if (tm.tm_hour > 23)
+			return 0;
+
+		if (*string == '\0')
+			break;
+		if (*string == ':')
+			string++;
+
+		if (!digitstring(string, 2))
+			return 0;
+		tm.tm_min = ATOI2(string);
+		if (tm.tm_min >= 60)
+			return 0;
+
+		if (*string == '\0')
+			break;
+		if (*string == ':')
+			string++;
+
+		if (!digitstring(string, 2))
+			return 0;
+		tm.tm_sec = ATOI2(string);
+		if (tm.tm_sec >= 60)
+			return 0;
+	} while (0);
+
+	if (*string != '\0')
+		return 0;
+
+	tm.tm_isdst = -1;
+	tm.tm_wday = -1;
+
+	t = mktime(&tm);
+	if (tm.tm_wday == -1)
+		return 0;
+
+	*res = t;
+	return 1;
+}
+
+#endif	/*NBTOOL*/
+
 static void
 setthetime(const char *p)
 {
@@ -214,6 +346,7 @@ setthetime(const char *p)
 
 	lt->tm_isdst = -1;			/* Divine correct DST */
 
+#ifndef HAVE_NBTOOL_CONFIG_H
 	if (fmt) {
 		t = strptime(p, fmt, lt);
 		if (t == NULL) {
@@ -225,6 +358,7 @@ setthetime(const char *p)
 				strlen(t), t);
 		goto setit;
 	}
+#endif
 	for (t = p, dot = NULL; *t; ++t) {
 		if (*t == '.') {
 			if (dot == NULL) {

Index: src/external/gpl2/gmake/dist/main.c
diff -u src/external/gpl2/gmake/dist/main.c:1.1.1.1 src/external/gpl2/gmake/dist/main.c:1.1.1.1.18.1
--- src/external/gpl2/gmake/dist/main.c:1.1.1.1	Mon Aug 18 06:47:02 2014
+++ src/external/gpl2/gmake/dist/main.c	Tue Oct 15 18:34:40 2024
@@ -511,13 +511,16 @@ int fatal_signal_mask;
 # endif
 #endif
 
+#undef HAVE_BSD_SIGNAL		/* PR lib/58674 .. tools build fails */
+#undef bsd_signal		/* bsd_signal() has a weird history. skip it */
+
 #if !defined HAVE_BSD_SIGNAL && !defined bsd_signal
 # if !defined HAVE_SIGACTION
 #  define bsd_signal signal
 # else
 typedef RETSIGTYPE (*bsd_signal_ret_t) ();
 
-static bsd_signal_ret_t
+/*static*/ bsd_signal_ret_t
 bsd_signal (int sig, bsd_signal_ret_t func)
 {
   struct sigaction act, oact;

Index: src/sys/sys/signal.h
diff -u src/sys/sys/signal.h:1.72 src/sys/sys/signal.h:1.72.16.1
--- src/sys/sys/signal.h:1.72	Fri Apr 21 15:10:35 2017
+++ src/sys/sys/signal.h	Tue Oct 15 18:34:40 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: signal.h,v 1.72 2017/04/21 15:10:35 christos Exp $	*/
+/*	$NetBSD: signal.h,v 1.72.16.1 2024/10/15 18:34:40 martin Exp $	*/
 
 /*
  * Copyright (c) 1982, 1986, 1989, 1991, 1993
@@ -237,10 +237,23 @@ struct	sigevent {
  */
 __BEGIN_DECLS
 void	(*signal(int, void (*)(int)))(int);
-void	(*bsd_signal(int, void (*)(int)))(int);
 #if (_POSIX_C_SOURCE - 0) >= 200112L || defined(_NETBSD_SOURCE)
 int	sigqueue(pid_t, int, const union sigval);
 #endif
+
+#if defined(_NETBSD_SOURCE) ||					\
+    (!defined (_XOPEN_SOURCE) && defined(_XOPEN_VERSION) &&	\
+	(_XOPEN_VERSION - 0) >= 4) ||				\
+    (defined(_XOPEN_SOURCE) && (_XOPEN_SOURCE - 0) <= 600)
+/*
+ * bsd_signal() was added to the standards in POSIX issue 4 (SusV4)
+ * (release 2 of SusV4) and then removed in POSIX issue 7 (2008),
+ * after being marked obsolete in POSIX issue 6 (2001).  It was
+ * always an X/Open extension function (though was moved to the
+ * base POSIX spec in issue 5, but still as an extension).
+ */
+void	(*bsd_signal(int, void (*)(int)))(int);
+#endif
 #if defined(_NETBSD_SOURCE)
 int	sigqueueinfo(pid_t, const siginfo_t *);
 #endif

Index: src/usr.bin/sed/process.c
diff -u src/usr.bin/sed/process.c:1.52 src/usr.bin/sed/process.c:1.52.18.1
--- src/usr.bin/sed/process.c:1.52	Thu Mar 12 12:40:41 2015
+++ src/usr.bin/sed/process.c	Tue Oct 15 18:34:40 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: process.c,v 1.52 2015/03/12 12:40:41 christos Exp $	*/
+/*	$NetBSD: process.c,v 1.52.18.1 2024/10/15 18:34:40 martin Exp $	*/
 
 /*-
  * Copyright (c) 1992 Diomidis Spinellis.
@@ -38,7 +38,7 @@
 #endif
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: process.c,v 1.52 2015/03/12 12:40:41 christos Exp $");
+__RCSID("$NetBSD: process.c,v 1.52.18.1 2024/10/15 18:34:40 martin Exp $");
 #ifdef __FBSDID
 __FBSDID("$FreeBSD: head/usr.bin/sed/process.c 192732 2009-05-25 06:45:33Z brian $");
 #endif
@@ -621,7 +621,11 @@ lputs(char *s, size_t len)
 			fputc('\n', outfile);
 			col = 0;
 		} else if (iswprint(wc)) {
+#ifdef HAVE_NBTOOL_CONFIG_H
+			width = 1;	/* wcwidth is an XSI function */
+#else
 			width = (size_t)wcwidth(wc);
+#endif
 			if (col + width >= termwidth) {
 				fprintf(outfile, "\\\n");
 				col = 0;

Reply via email to