From 770de75a334e93f17ad516b01825e54df85d0cbd Mon Sep 17 00:00:00 2001
From: Sascha Wildner <saw@online.de>
Date: Sun, 26 May 2019 14:54:53 +0200
Subject: [PATCH 2/2] route(8): Simplify the keywords handling.

Adapted from FreeBSD. This eliminates paste(1) as a bootstrap tool.
---
 Makefile.inc1       |  2 +-
 sbin/route/Makefile | 31 +++++++------------------------
 sbin/route/route.c  | 11 +++++++++--
 sbin/route/show.c   |  9 ++++++++-
 4 files changed, 25 insertions(+), 28 deletions(-)

diff --git a/Makefile.inc1 b/Makefile.inc1
index 7a0df90612..c4c54ca078 100644
--- a/Makefile.inc1
+++ b/Makefile.inc1
@@ -867,7 +867,7 @@ BSTRAPDIRS2= \
 	usr.bin/sed usr.bin/uname usr.bin/touch \
 	usr.bin/mkdep usr.bin/mktemp usr.bin/lorder usr.bin/file2c \
 	usr.bin/tsort usr.bin/tr usr.bin/join usr.bin/wc usr.bin/basename \
-	usr.bin/gencat usr.bin/chflags usr.bin/expand usr.bin/paste \
+	usr.bin/gencat usr.bin/chflags usr.bin/expand \
 	usr.bin/uuencode usr.bin/cap_mkdb usr.bin/true usr.bin/false \
 	usr.bin/cmp usr.bin/xargs usr.bin/id usr.bin/env usr.bin/dirname \
 	usr.bin/tail usr.bin/unifdef \
diff --git a/sbin/route/Makefile b/sbin/route/Makefile
index 601289e84c..045e56021a 100644
--- a/sbin/route/Makefile
+++ b/sbin/route/Makefile
@@ -1,35 +1,18 @@
 #	@(#)Makefile	8.1 (Berkeley) 6/5/93
 # $FreeBSD: src/sbin/route/Makefile,v 1.11.2.3 2002/12/04 14:05:42 ru Exp $
-# $DragonFly: src/sbin/route/Makefile,v 1.7 2008/11/03 00:25:45 pavalos Exp $
 
 PROG=	route
 MAN=	route.8
-SRCS=	route.c show.c keywords.h keywords.c
+SRCS=	route.c show.c keywords.h
 CFLAGS+=-I.
 CFLAGS+=-DINET6
-CLEANFILES+=keywords.h keywords.c _keywords.tmp
+CLEANFILES+=keywords.h
 NO_STRICT_ALIASING=
 
-keywords.h:
-	echo -e "extern struct keytab {\n\tconst char\t*kt_cp;\n\tint\t\t kt_i;\n} keywords[];\n\n" > ${.TARGET}
-	sed -e '/^#/d' -e '/^$$/d' ${.CURDIR}/keywords > _keywords.tmp
-	tr a-z A-Z < _keywords.tmp | paste _keywords.tmp - | \
-	    awk '{ \
-		if (NF > 1) \
-			printf "#define\tK_%s\t%d\n", $$2, NR }' \
-	    >> ${.TARGET}
-	rm -f _keywords.tmp
-
-keywords.c:	keywords.h
-	echo -e "#include \"keywords.h\"\n\nstruct keytab keywords[] = {\n\n" \
-	    > ${.TARGET}
-	sed -e '/^#/d' -e '/^$$/d' ${.CURDIR}/keywords > _keywords.tmp
-	tr a-z A-Z < _keywords.tmp | paste _keywords.tmp - | \
-	    awk '{ \
-		if (NF > 1) \
-			printf "\t{\"%s\", K_%s},\n", $$1, $$2 }' \
-	    >> ${.TARGET}
-	echo -e "\t{0, 0}\n};\n" >> ${.TARGET}
-	rm -f _keywords.tmp
+keywords.h: keywords
+	LC_ALL=C awk '!/^#|^$$/ { \
+		printf "#define\tK_%s\t%d\n\t{\"%s\", K_%s},\n", \
+		    toupper($$1), ++L, $$1, toupper($$1); \
+	}' < ${.CURDIR}/keywords > ${.TARGET} || (rm -f ${.TARGET}; false)
 
 .include <bsd.prog.mk>
diff --git a/sbin/route/route.c b/sbin/route/route.c
index 1e98f36440..4f1fcf8a6f 100644
--- a/sbin/route/route.c
+++ b/sbin/route/route.c
@@ -60,7 +60,6 @@
 #include <ifaddrs.h>
 
 #include "extern.h"
-#include "keywords.h"
 
 union	sockunion {
 	struct	sockaddr sa;
@@ -80,6 +79,14 @@ typedef union sockunion *sup;
 int	nflag, wflag;
 int	cpuflag = -1;
 
+static struct keytab {
+	const char	*kt_cp;
+	int	kt_i;
+} const keywords[] = {
+#include "keywords.h"
+	{0, 0}
+};
+
 static struct sockaddr_in so_gate_tried;
 static struct rt_metrics rt_metrics;
 static int	pid, rtm_addrs;
@@ -1619,7 +1626,7 @@ bprintf(FILE *fp, int b, u_char *str)
 int
 keyword(const char *cp)
 {
-	struct keytab *kt = keywords;
+	const struct keytab *kt = keywords;
 
 	while (kt->kt_cp != NULL && strcmp(kt->kt_cp, cp) != 0)
 		kt++;
diff --git a/sbin/route/show.c b/sbin/route/show.c
index a5ce662c10..9098081cf4 100644
--- a/sbin/route/show.c
+++ b/sbin/route/show.c
@@ -53,7 +53,6 @@
 #include <netdb.h>
 
 #include "extern.h"
-#include "keywords.h"
 
 /*
  * Definitions for showing gateway flags.
@@ -81,6 +80,14 @@ static const struct bits bits[] = {
 	{ 0, 0 }
 };
 
+static __unused struct keytab {
+	const char	*kt_cp;
+	int	kt_i;
+} const keywords[] = {
+#include "keywords.h"
+	{0, 0}
+};
+
 static void	p_rtentry(struct rt_msghdr *);
 static int	p_sockaddr(struct sockaddr *, int, int);
 static void	p_flags(int, const char *);
-- 
2.21.0

