Module Name:    src
Committed By:   christos
Date:           Thu Feb  6 19:35:29 UTC 2025

Modified Files:
        src/usr.bin/getaddrinfo: Makefile getaddrinfo.c tables.awk
Added Files:
        src/usr.bin/getaddrinfo: Makefile.inc support.c support.h

Log Message:
Split table functions to a separate file.


To generate a diff of this commit:
cvs rdiff -u -r1.2 -r1.3 src/usr.bin/getaddrinfo/Makefile \
    src/usr.bin/getaddrinfo/tables.awk
cvs rdiff -u -r0 -r1.1 src/usr.bin/getaddrinfo/Makefile.inc \
    src/usr.bin/getaddrinfo/support.c src/usr.bin/getaddrinfo/support.h
cvs rdiff -u -r1.5 -r1.6 src/usr.bin/getaddrinfo/getaddrinfo.c

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

Modified files:

Index: src/usr.bin/getaddrinfo/Makefile
diff -u src/usr.bin/getaddrinfo/Makefile:1.2 src/usr.bin/getaddrinfo/Makefile:1.3
--- src/usr.bin/getaddrinfo/Makefile:1.2	Mon Apr 28 21:21:02 2014
+++ src/usr.bin/getaddrinfo/Makefile	Thu Feb  6 14:35:28 2025
@@ -1,22 +1,14 @@
-#	$NetBSD: Makefile,v 1.2 2014/04/29 01:21:02 christos Exp $
+#	$NetBSD: Makefile,v 1.3 2025/02/06 19:35:28 christos Exp $
 
 .include <bsd.own.mk>
+.include "Makefile.inc"
 
 PROG=	getaddrinfo
+SRCS=	getaddrinfo.c support.c
 
 DPADD+=	${LIBUTIL}
 LDADD+=	-lutil
 
 WARNS=	5
 
-SYS_SOCKET_H?=	${NETBSDSRCDIR}/sys/sys/socket.h
-
-CPPFLAGS+=	-I.
-DPSRCS+=	tables.h
-CLEANFILES+=	tables.h
-tables.h: tables.awk ${SYS_SOCKET_H}
-	${_MKTARGET_CREATE}
-	${TOOL_AWK} -f ${.ALLSRC} > ${.TARGET}.tmp \
-	&& mv -f -- ${.TARGET}.tmp ${.TARGET}
-
 .include <bsd.prog.mk>
Index: src/usr.bin/getaddrinfo/tables.awk
diff -u src/usr.bin/getaddrinfo/tables.awk:1.2 src/usr.bin/getaddrinfo/tables.awk:1.3
--- src/usr.bin/getaddrinfo/tables.awk:1.2	Wed Feb 26 20:17:13 2014
+++ src/usr.bin/getaddrinfo/tables.awk	Thu Feb  6 14:35:28 2025
@@ -1,6 +1,6 @@
 #!/usr/bin/awk -f
 
-#	$NetBSD: tables.awk,v 1.2 2014/02/27 01:17:13 ginsbach Exp $
+#	$NetBSD: tables.awk,v 1.3 2025/02/06 19:35:28 christos Exp $
 
 # Copyright (c) 2013 The NetBSD Foundation, Inc.
 # All rights reserved.
@@ -48,6 +48,7 @@ $2 ~ /^SOCK_[A-Z0-9_]*$/ {
 
 END {
 	printf("/* Do not edit!  This file was generated automagically! */\n");
+	printf("#include <sys/socket.h>\n");
 
 	printf("\nstatic const char *const address_families[] = {\n");
 	for (i = 0; i < n_afs; i++)

Index: src/usr.bin/getaddrinfo/getaddrinfo.c
diff -u src/usr.bin/getaddrinfo/getaddrinfo.c:1.5 src/usr.bin/getaddrinfo/getaddrinfo.c:1.6
--- src/usr.bin/getaddrinfo/getaddrinfo.c:1.5	Tue Jan  9 20:48:16 2024
+++ src/usr.bin/getaddrinfo/getaddrinfo.c	Thu Feb  6 14:35:28 2025
@@ -1,4 +1,4 @@
-/*	$NetBSD: getaddrinfo.c,v 1.5 2024/01/10 01:48:16 riastradh Exp $	*/
+/*	$NetBSD: getaddrinfo.c,v 1.6 2025/02/06 19:35:28 christos Exp $	*/
 
 /*-
  * Copyright (c) 2013 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: getaddrinfo.c,v 1.5 2024/01/10 01:48:16 riastradh Exp $");
+__RCSID("$NetBSD: getaddrinfo.c,v 1.6 2025/02/06 19:35:28 christos Exp $");
 
 #include <sys/types.h>
 #include <sys/socket.h>
@@ -49,14 +49,10 @@ __RCSID("$NetBSD: getaddrinfo.c,v 1.5 20
 #include <util.h>
 
 #include "tables.h"
+#include "support.h"
 
 static void	usage(void) __dead;
 static void	printaddrinfo(struct addrinfo *);
-static bool	parse_af(const char *, int *);
-static bool	parse_protocol(const char *, int *);
-static bool	parse_socktype(const char *, int *);
-static bool	parse_numeric_tabular(const char *, int *, const char *const *,
-		    size_t);
 
 int
 main(int argc, char **argv)
@@ -64,7 +60,8 @@ main(int argc, char **argv)
 	static const struct addrinfo zero_addrinfo;
 	struct addrinfo hints = zero_addrinfo;
 	struct addrinfo *addrinfo;
-	const char *hostname = NULL, *service = NULL;
+	const char *hostname = NULL;
+	char *service = NULL;
 	int ch;
 	int error;
 
@@ -169,88 +166,6 @@ usage(void)
 	exit(1);
 }
 
-static bool
-parse_af(const char *string, int *afp)
-{
-
-	return parse_numeric_tabular(string, afp, address_families,
-	    __arraycount(address_families));
-}
-
-static bool
-parse_protocol(const char *string, int *protop)
-{
-	struct protoent *protoent;
-	char *end;
-	long value;
-
-	errno = 0;
-	value = strtol(string, &end, 0);
-	if ((string[0] == '\0') || (*end != '\0'))
-		goto numeric_failed;
-	if ((errno == ERANGE) && ((value == LONG_MAX) || (value == LONG_MIN)))
-		goto numeric_failed;
-	if ((value > INT_MAX) || (value < INT_MIN))
-		goto numeric_failed;
-
-	*protop = value;
-	return true;
-
-numeric_failed:
-	protoent = getprotobyname(string);
-	if (protoent == NULL)
-		goto protoent_failed;
-
-	*protop = protoent->p_proto;
-	return true;
-
-protoent_failed:
-	return false;
-}
-
-static bool
-parse_socktype(const char *string, int *typep)
-{
-
-	return parse_numeric_tabular(string, typep, socket_types,
-	    __arraycount(socket_types));
-}
-
-static bool
-parse_numeric_tabular(const char *string, int *valuep,
-    const char *const *table, size_t n)
-{
-	char *end;
-	long value;
-	size_t i;
-
-	assert((uintmax_t)n <= (uintmax_t)INT_MAX);
-
-	errno = 0;
-	value = strtol(string, &end, 0);
-	if ((string[0] == '\0') || (*end != '\0'))
-		goto numeric_failed;
-	if ((errno == ERANGE) && ((value == LONG_MAX) || (value == LONG_MIN)))
-		goto numeric_failed;
-	if ((value > INT_MAX) || (value < INT_MIN))
-		goto numeric_failed;
-
-	*valuep = value;
-	return true;
-
-numeric_failed:
-	for (i = 0; i < n; i++)
-		if ((table[i] != NULL) && (strcmp(string, table[i]) == 0))
-			break;
-	if (i == n)
-		goto table_failed;
-	*valuep = i;
-	return true;
-
-table_failed:
-	return false;
-}
-
 static void
 printaddrinfo(struct addrinfo *addrinfo)
 {

Added files:

Index: src/usr.bin/getaddrinfo/Makefile.inc
diff -u /dev/null src/usr.bin/getaddrinfo/Makefile.inc:1.1
--- /dev/null	Thu Feb  6 14:35:29 2025
+++ src/usr.bin/getaddrinfo/Makefile.inc	Thu Feb  6 14:35:28 2025
@@ -0,0 +1,11 @@
+#	$NetBSD: Makefile.inc,v 1.1 2025/02/06 19:35:28 christos Exp $
+
+SYS_SOCKET_H?=	${NETBSDSRCDIR}/sys/sys/socket.h
+
+CPPFLAGS+=	-I.
+DPSRCS+=	tables.h
+CLEANFILES+=	tables.h
+tables.h: ${.PARSEDIR}/tables.awk ${SYS_SOCKET_H}
+	${_MKTARGET_CREATE}
+	${TOOL_AWK} -f ${.ALLSRC} > ${.TARGET}.tmp \
+	&& mv -f -- ${.TARGET}.tmp ${.TARGET}
Index: src/usr.bin/getaddrinfo/support.c
diff -u /dev/null src/usr.bin/getaddrinfo/support.c:1.1
--- /dev/null	Thu Feb  6 14:35:29 2025
+++ src/usr.bin/getaddrinfo/support.c	Thu Feb  6 14:35:28 2025
@@ -0,0 +1,114 @@
+/*	$NetBSD: support.c,v 1.1 2025/02/06 19:35:28 christos Exp $	*/
+
+/*-
+ * Copyright (c) 2013 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Taylor R. Campbell.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__RCSID("$NetBSD: support.c,v 1.1 2025/02/06 19:35:28 christos Exp $");
+
+#include <assert.h>
+#include <limits.h>
+#include <errno.h>
+#include <netdb.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "tables.h"
+#include "support.h"
+
+static bool
+parse_numeric(const char *string, int *rv)
+{
+	char *end;
+	long value;
+
+	errno = 0;
+	value = strtol(string, &end, 0);
+	if ((string[0] == '\0') || (*end != '\0'))
+		return false;
+	if ((errno == ERANGE) && ((value == LONG_MAX) || (value == LONG_MIN)))
+		return false;
+	if ((value > INT_MAX) || (value < INT_MIN))
+		return false;
+	*rv = (int)value;
+	return true;
+}
+
+bool
+parse_af(const char *string, int *afp)
+{
+
+	return parse_numeric_tabular(string, afp, address_families,
+	    __arraycount(address_families));
+}
+
+bool
+parse_protocol(const char *string, int *protop)
+{
+	struct protoent *protoent;
+
+	if (parse_numeric(string, protop))
+		return true;
+
+	protoent = getprotobyname(string);
+	if (protoent == NULL)
+		return false;
+
+	*protop = protoent->p_proto;
+	return true;
+}
+
+bool
+parse_socktype(const char *string, int *typep)
+{
+
+	return parse_numeric_tabular(string, typep, socket_types,
+	    __arraycount(socket_types));
+}
+
+bool
+parse_numeric_tabular(const char *string, int *valuep,
+    const char *const *table, size_t n)
+{
+
+	assert((uintmax_t)n <= (uintmax_t)INT_MAX);
+
+	if (parse_numeric(string, valuep))
+		return true;
+
+	for (size_t i = 0; i < n; i++)
+		if ((table[i] != NULL) && (strcmp(string, table[i]) == 0)) {
+			*valuep = (int)i;
+			return true;
+		}
+	return false;
+}
Index: src/usr.bin/getaddrinfo/support.h
diff -u /dev/null src/usr.bin/getaddrinfo/support.h:1.1
--- /dev/null	Thu Feb  6 14:35:29 2025
+++ src/usr.bin/getaddrinfo/support.h	Thu Feb  6 14:35:28 2025
@@ -0,0 +1,35 @@
+/*	$NetBSD: support.h,v 1.1 2025/02/06 19:35:28 christos Exp $	*/
+
+/*-
+ * Copyright (c) 2013 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Taylor R. Campbell.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+bool	parse_af(const char *, int *);
+bool	parse_protocol(const char *, int *);
+bool	parse_socktype(const char *, int *);
+bool	parse_numeric_tabular(const char *, int *, const char *const *, size_t);

Reply via email to