From 6fe37d1365dfe4bf9383cb5674e42dd6fadfd136 Mon Sep 17 00:00:00 2001
From: Ruslan Ermilov <ru@FreeBSD.org>
Date: Tue, 22 Feb 2005 23:29:54 +0000
Subject: [PATCH] Add endianness support to cap_mkdb(1), useful for cross
 builds.

---
 Makefile.inc1               |  5 +++++
 share/mk/Makefile           |  4 ++--
 share/mk/bsd.endian.mk      | 11 +++++++++++
 share/termcap/Makefile      | 11 ++++++++++-
 usr.bin/cap_mkdb/cap_mkdb.1 | 17 ++++++++++++++---
 usr.bin/cap_mkdb/cap_mkdb.c | 16 +++++++++++++---
 usr.bin/vgrind/Makefile     | 11 ++++++++++-
 7 files changed, 65 insertions(+), 10 deletions(-)
 create mode 100644 share/mk/bsd.endian.mk

diff --git a/Makefile.inc1 b/Makefile.inc1
index 0359e60d163e..9c58c55065f2 100644
--- a/Makefile.inc1
+++ b/Makefile.inc1
@@ -760,6 +760,10 @@ _groff=		gnu/usr.bin/groff/tmac
 .endif
 .endif
 
+.if ${BOOTSTRAPPING} < 600015
+_cap_mkdb=	usr.bin/cap_mkdb
+.endif
+
 .if ${BOOTSTRAPPING} < 502102
 _lex=		usr.bin/lex
 .endif
@@ -798,6 +802,7 @@ bootstrap-tools:
     ${_gperf} \
     ${_groff} \
     gnu/usr.bin/texinfo \
+    ${_cap_mkdb} \
     usr.bin/colldef \
     ${_lex} \
     usr.bin/lorder \
diff --git a/share/mk/Makefile b/share/mk/Makefile
index 326048e00f38..44b0a9b08467 100644
--- a/share/mk/Makefile
+++ b/share/mk/Makefile
@@ -2,8 +2,8 @@
 #	@(#)Makefile	8.1 (Berkeley) 6/8/93
 
 FILES=	bsd.README
-FILES+=	bsd.compat.mk bsd.cpu.mk bsd.dep.mk bsd.doc.mk bsd.files.mk
-FILES+=	bsd.incs.mk bsd.info.mk bsd.init.mk
+FILES+=	bsd.compat.mk bsd.cpu.mk bsd.dep.mk bsd.doc.mk bsd.endian.mk
+FILES+=	bsd.files.mk bsd.incs.mk bsd.info.mk bsd.init.mk
 FILES+=	bsd.kmod.mk
 FILES+=	bsd.lib.mk bsd.libnames.mk bsd.links.mk bsd.man.mk bsd.nls.mk
 FILES+=	bsd.obj.mk bsd.own.mk
diff --git a/share/mk/bsd.endian.mk b/share/mk/bsd.endian.mk
new file mode 100644
index 000000000000..a9bdd6def551
--- /dev/null
+++ b/share/mk/bsd.endian.mk
@@ -0,0 +1,11 @@
+# $FreeBSD$
+
+.if ${MACHINE_ARCH} == "alpha" || \
+    ${MACHINE_ARCH} == "amd64" || \
+    ${MACHINE_ARCH} == "i386" || \
+    ${MACHINE_ARCH} == "ia64"
+TARGET_ENDIANNESS= 1234
+.elif ${MACHINE_ARCH} == "powerpc" || \
+    ${MACHINE_ARCH} == "sparc64"
+TARGET_ENDIANNESS= 4321
+.endif
diff --git a/share/termcap/Makefile b/share/termcap/Makefile
index 2d9b9ff75687..c44a17e25bd3 100644
--- a/share/termcap/Makefile
+++ b/share/termcap/Makefile
@@ -14,8 +14,17 @@ CLEANFILES+=	termcap termcap.db
 termcap: reorder termcap.src
 	TERM=dumb TERMCAP=dumb: ex - ${.CURDIR}/termcap.src < ${.CURDIR}/reorder
 
+.include <bsd.endian.mk>
+.if ${TARGET_ENDIANNESS} == "1234"
+CAP_MKDB_ENDIAN= -l
+.elif ${TARGET_ENDIANNESS} == "4321"
+CAP_MKDB_ENDIAN= -b
+.else
+CAP_MKDB_ENDIAN=
+.endif
+
 termcap.db: termcap
-	cap_mkdb termcap
+	cap_mkdb ${CAP_MKDB_ENDIAN} termcap
 
 etc-termcap:
 	ln -fs ${BINDIR}/misc/termcap ${DESTDIR}/etc/termcap
diff --git a/usr.bin/cap_mkdb/cap_mkdb.1 b/usr.bin/cap_mkdb/cap_mkdb.1
index 63c06efb9755..2ac09e35bc84 100644
--- a/usr.bin/cap_mkdb/cap_mkdb.1
+++ b/usr.bin/cap_mkdb/cap_mkdb.1
@@ -32,7 +32,7 @@
 .\"	@(#)cap_mkdb.1	8.1 (Berkeley) 6/6/93
 .\" $FreeBSD$
 .\"
-.Dd June 6, 1993
+.Dd February 22, 2005
 .Dt CAP_MKDB 1
 .Os
 .Sh NAME
@@ -40,10 +40,10 @@
 .Nd create capability database
 .Sh SYNOPSIS
 .Nm
+.Op Fl b | l
 .Op Fl v
 .Op Fl f Ar outfile
-.Ar file
-.Op Ar
+.Ar
 .Sh DESCRIPTION
 The
 .Nm
@@ -65,11 +65,22 @@ record is stored into the database.
 .Pp
 The following options are available:
 .Bl -tag -width indent
+.It Fl b
+Use big-endian byte order for database metadata.
 .It Fl f Ar outfile
 Specify a different database basename.
+.It Fl l
+Use little-endian byte order for database metadata.
 .It Fl v
 Print out the number of capability records in the database.
 .El
+.Pp
+The
+.Fl b
+and
+.Fl l
+flags are mutually exclusive.
+The default byte ordering is the current host order.
 .Sh FORMAT
 Each record is stored in the database using two different types of keys.
 .Pp
diff --git a/usr.bin/cap_mkdb/cap_mkdb.c b/usr.bin/cap_mkdb/cap_mkdb.c
index 2cb0bed276c1..32413c88b4b6 100644
--- a/usr.bin/cap_mkdb/cap_mkdb.c
+++ b/usr.bin/cap_mkdb/cap_mkdb.c
@@ -84,11 +84,18 @@ HASHINFO openinfo = {
 int
 main(int argc, char *argv[])
 {
-	int c;
+	int byteorder, c;
 
 	capname = NULL;
-	while ((c = getopt(argc, argv, "f:v")) != -1) {
+	byteorder = 0;
+	while ((c = getopt(argc, argv, "bf:lv")) != -1) {
 		switch(c) {
+		case 'b':
+		case 'l':
+			if (byteorder != 0)
+				usage();
+			byteorder = c == 'b' ? 4321 : 1234;
+			break;
 		case 'f':
 			capname = optarg;
 			break;
@@ -106,6 +113,9 @@ main(int argc, char *argv[])
 	if (*argv == NULL)
 		usage();
 
+	/* Set byte order. */
+	openinfo.lorder = byteorder;
+
 	/*
 	 * The database file is the first argument if no name is specified.
 	 * Make arrangements to unlink it if exit badly.
@@ -257,6 +267,6 @@ void
 usage(void)
 {
 	(void)fprintf(stderr,
-	    "usage: cap_mkdb [-v] [-f outfile] file [file ...]\n");
+	    "usage: cap_mkdb [-b | -l] [-v] [-f outfile] file ...\n");
 	exit(1);
 }
diff --git a/usr.bin/vgrind/Makefile b/usr.bin/vgrind/Makefile
index 59cd062b9f18..1a64c1c9f01f 100644
--- a/usr.bin/vgrind/Makefile
+++ b/usr.bin/vgrind/Makefile
@@ -16,7 +16,16 @@ SCRIPTSDIR=/usr/bin
 
 CLEANFILES= vgrindefs.src.db
 
+.include <bsd.endian.mk>
+.if ${TARGET_ENDIANNESS} == "1234"
+CAP_MKDB_ENDIAN= -l
+.elif ${TARGET_ENDIANNESS} == "4321"
+CAP_MKDB_ENDIAN= -b
+.else
+CAP_MKDB_ENDIAN=
+.endif
+
 vgrindefs.src.db: vgrindefs.src
-	cap_mkdb -f vgrindefs.src ${.ALLSRC}
+	cap_mkdb ${CAP_MKDB_ENDIAN} -f vgrindefs.src ${.ALLSRC}
 
 .include <bsd.prog.mk>