svn commit: r339489 - in head: lib/libc/locale share/colldef share/ctypedef share/mk targets/pseudo/userland tools/tools/locale/tools usr.bin/localedef

Yuri Pankov yuripv at FreeBSD.org
Sat Oct 20 20:51:08 UTC 2018


Author: yuripv
Date: Sat Oct 20 20:51:05 2018
New Revision: 339489
URL: https://svnweb.freebsd.org/changeset/base/339489

Log:
  Add -b/-l options to localedef(1) to specify output endianness and use
  it appropriately when building share/ctypedef and share/colldef.
  
  This makes the resulting locale data in EL->EB (amd64->powerpc64) cross
  build and in the native EB build match.  Revert the changes done to libc
  in r308170 as they are no longer needed.
  
  PR:		231965
  Reviewed by:	bapt, emaste, sbruno, 0mp
  Approved by:	kib (mentor)
  Differential Revision:	https://reviews.freebsd.org/D17603

Deleted:
  head/lib/libc/locale/endian.h
Modified:
  head/lib/libc/locale/collate.c
  head/lib/libc/locale/rune.c
  head/share/colldef/Makefile
  head/share/ctypedef/Makefile
  head/share/mk/bsd.endian.mk
  head/targets/pseudo/userland/Makefile.depend
  head/tools/tools/locale/tools/cldr2def.pl
  head/usr.bin/localedef/collate.c
  head/usr.bin/localedef/ctype.c
  head/usr.bin/localedef/localedef.1
  head/usr.bin/localedef/localedef.c
  head/usr.bin/localedef/localedef.h

Modified: head/lib/libc/locale/collate.c
==============================================================================
--- head/lib/libc/locale/collate.c	Sat Oct 20 20:49:46 2018	(r339488)
+++ head/lib/libc/locale/collate.c	Sat Oct 20 20:51:05 2018	(r339489)
@@ -55,7 +55,6 @@ __FBSDID("$FreeBSD$");
 #include <fcntl.h>
 #include "un-namespace.h"
 
-#include "endian.h"
 #include "collate.h"
 #include "setlocale.h"
 #include "ldpart.h"
@@ -162,7 +161,7 @@ __collate_load_tables_l(const char *encoding, struct x
 
 	if ((info->directive_count < 1) ||
 	    (info->directive_count >= COLL_WEIGHTS_MAX) ||
-	    ((chains = BSWAP(info->chain_count)) < 0)) {
+	    ((chains = info->chain_count) < 0)) {
 		(void) munmap(map, sbuf.st_size);
 		errno = EINVAL;
 		return (_LDP_ERROR);
@@ -170,9 +169,9 @@ __collate_load_tables_l(const char *encoding, struct x
 
 	i = (sizeof (collate_char_t) * (UCHAR_MAX + 1)) +
 	    (sizeof (collate_chain_t) * chains) +
-	    (sizeof (collate_large_t) * BSWAP(info->large_count));
+	    (sizeof (collate_large_t) * info->large_count);
 	for (z = 0; z < info->directive_count; z++) {
-		i += sizeof (collate_subst_t) * BSWAP(info->subst_count[z]);
+		i += sizeof (collate_subst_t) * info->subst_count[z];
 	}
 	if (i != (sbuf.st_size - (TMP - map))) {
 		(void) munmap(map, sbuf.st_size);
@@ -185,9 +184,9 @@ __collate_load_tables_l(const char *encoding, struct x
 	TMP += sizeof (collate_char_t) * (UCHAR_MAX + 1);
 
 	for (z = 0; z < info->directive_count; z++) {
-		if (BSWAP(info->subst_count[z]) > 0) {
+		if (info->subst_count[z] > 0) {
 			table->subst_table[z] = (void *)TMP;
-			TMP += BSWAP(info->subst_count[z]) * sizeof (collate_subst_t);
+			TMP += info->subst_count[z] * sizeof (collate_subst_t);
 		} else {
 			table->subst_table[z] = NULL;
 		}
@@ -198,7 +197,7 @@ __collate_load_tables_l(const char *encoding, struct x
 		TMP += chains * sizeof (collate_chain_t);
 	} else
 		table->chain_pri_table = NULL;
-	if (BSWAP(info->large_count) > 0)
+	if (info->large_count > 0)
 		table->large_pri_table = (void *)TMP;
 	else
 		table->large_pri_table = NULL;
@@ -211,7 +210,7 @@ static const int32_t *
 substsearch(struct xlocale_collate *table, const wchar_t key, int pass)
 {
 	const collate_subst_t *p;
-	int n = BSWAP(table->info->subst_count[pass]);
+	int n = table->info->subst_count[pass];
 
 	if (n == 0)
 		return (NULL);
@@ -223,7 +222,7 @@ substsearch(struct xlocale_collate *table, const wchar
 		return (NULL);
 
 	p = table->subst_table[pass] + (key & ~COLLATE_SUBST_PRIORITY);
-	assert(BSWAP(p->key) == key);
+	assert(p->key == key);
 
 	return (p->pri);
 }
@@ -232,7 +231,7 @@ static collate_chain_t *
 chainsearch(struct xlocale_collate *table, const wchar_t *key, int *len)
 {
 	int low = 0;
-	int high = BSWAP(table->info->chain_count) - 1;
+	int high = table->info->chain_count - 1;
 	int next, compar, l;
 	collate_chain_t *p;
 	collate_chain_t *tab = table->chain_pri_table;
@@ -243,7 +242,7 @@ chainsearch(struct xlocale_collate *table, const wchar
 	while (low <= high) {
 		next = (low + high) / 2;
 		p = tab + next;
-		compar = *key - le16toh(*p->str);
+		compar = *key - *p->str;
 		if (compar == 0) {
 			l = wcsnlen(p->str, COLLATE_STR_LEN);
 			compar = wcsncmp(key, p->str, l);
@@ -264,7 +263,7 @@ static collate_large_t *
 largesearch(struct xlocale_collate *table, const wchar_t key)
 {
 	int low = 0;
-	int high = BSWAP(table->info->large_count) - 1;
+	int high = table->info->large_count - 1;
 	int next, compar;
 	collate_large_t *p;
 	collate_large_t *tab = table->large_pri_table;
@@ -275,7 +274,7 @@ largesearch(struct xlocale_collate *table, const wchar
 	while (low <= high) {
 		next = (low + high) / 2;
 		p = tab + next;
-		compar = key - BSWAP(p->val);
+		compar = key - p->val;
 		if (compar == 0)
 			return (p);
 		if (compar > 0)
@@ -340,15 +339,15 @@ _collate_lookup(struct xlocale_collate *table, const w
 		 * Character is a small (8-bit) character.
 		 * We just look these up directly for speed.
 		 */
-		*pri = BSWAP(table->char_pri_table[*t].pri[which]);
+		*pri = table->char_pri_table[*t].pri[which];
 
-	} else if ((BSWAP(table->info->large_count) > 0) &&
+	} else if ((table->info->large_count > 0) &&
 	    ((match = largesearch(table, *t)) != NULL)) {
 
 		/*
 		 * Character was found in the extended table.
 		 */
-		*pri = BSWAP(match->pri.pri[which]);
+		*pri = match->pri.pri[which];
 
 	} else {
 		/*
@@ -358,7 +357,7 @@ _collate_lookup(struct xlocale_collate *table, const w
 			/* Mask off sign bit to prevent ordering confusion. */
 			*pri = (*t & COLLATE_MAX_PRIORITY);
 		} else {
-			*pri = BSWAP(table->info->undef_pri[which]);
+			*pri = table->info->undef_pri[which];
 		}
 		/* No substitutions for undefined characters! */
 		return;
@@ -377,9 +376,9 @@ _collate_lookup(struct xlocale_collate *table, const w
 	 * code ensures this for us.
 	 */
 	if ((sptr = substsearch(table, *pri, which)) != NULL) {
-		if ((*pri = BSWAP(*sptr)) > 0) {
+		if ((*pri = *sptr) > 0) {
 			sptr++;
-			*state = BSWAP(*sptr) ? sptr : NULL;
+			*state = *sptr ? sptr : NULL;
 		}
 	}
 
@@ -521,7 +520,7 @@ static int
 xfrm(struct xlocale_collate *table, unsigned char *p, int pri, int pass)
 {
 	/* we use unsigned to ensure zero fill on right shift */
-	uint32_t val = BSWAP((uint32_t)table->info->pri_count[pass]);
+	uint32_t val = (uint32_t)table->info->pri_count[pass];
 	int nc = 0;
 
 	while (val) {
@@ -681,7 +680,7 @@ __collate_equiv_value(locale_t locale, const wchar_t *
 		e = -1;
 		if (*str <= UCHAR_MAX)
 			e = table->char_pri_table[*str].pri[0];
-		else if (BSWAP(table->info->large_count) > 0) {
+		else if (table->info->large_count > 0) {
 			collate_large_t *match_large;
 			match_large = largesearch(table, *str);
 			if (match_large)
@@ -691,7 +690,7 @@ __collate_equiv_value(locale_t locale, const wchar_t *
 			return (1);
 		return (e > 0 ? e : 0);
 	}
-	if (BSWAP(table->info->chain_count) > 0) {
+	if (table->info->chain_count > 0) {
 		wchar_t name[COLLATE_STR_LEN];
 		collate_chain_t *match_chain;
 		int clen;

Modified: head/lib/libc/locale/rune.c
==============================================================================
--- head/lib/libc/locale/rune.c	Sat Oct 20 20:49:46 2018	(r339488)
+++ head/lib/libc/locale/rune.c	Sat Oct 20 20:51:05 2018	(r339489)
@@ -54,7 +54,6 @@ __FBSDID("$FreeBSD$");
 #include <unistd.h>
 #include "un-namespace.h"
 
-#include "endian.h"
 #include "runefile.h"
 
 _RuneLocale *
@@ -110,29 +109,29 @@ _Read_RuneMagi(const char *fname)
 	}
 
 	runetype_ext_ranges = (_FileRuneEntry *)variable;
-	variable = runetype_ext_ranges + BSWAP(frl->runetype_ext_nranges);
+	variable = runetype_ext_ranges + frl->runetype_ext_nranges;
 	if (variable > lastp) {
 		goto invalid;
 	}
 
 	maplower_ext_ranges = (_FileRuneEntry *)variable;
-	variable = maplower_ext_ranges + BSWAP(frl->maplower_ext_nranges);
+	variable = maplower_ext_ranges + frl->maplower_ext_nranges;
 	if (variable > lastp) {
 		goto invalid;
 	}
 
 	mapupper_ext_ranges = (_FileRuneEntry *)variable;
-	variable = mapupper_ext_ranges + BSWAP(frl->mapupper_ext_nranges);
+	variable = mapupper_ext_ranges + frl->mapupper_ext_nranges;
 	if (variable > lastp) {
 		goto invalid;
 	}
 
 	frr = runetype_ext_ranges;
-	for (x = 0; x < BSWAP(frl->runetype_ext_nranges); ++x) {
+	for (x = 0; x < frl->runetype_ext_nranges; ++x) {
 		uint32_t *types;
 
-		if (BSWAP(frr[x].map) == 0) {
-			int len = BSWAP(frr[x].max) - BSWAP(frr[x].min) + 1;
+		if (frr[x].map == 0) {
+			int len = frr[x].max - frr[x].min + 1;
 			types = variable;
 			variable = types + len;
 			runetype_ext_len += len;
@@ -142,7 +141,7 @@ _Read_RuneMagi(const char *fname)
 		}
 	}
 
-	if ((char *)variable + BSWAP(frl->variable_len) > (char *)lastp) {
+	if ((char *)variable + frl->variable_len > (char *)lastp) {
 		goto invalid;
 	}
 
@@ -150,10 +149,9 @@ _Read_RuneMagi(const char *fname)
 	 * Convert from disk format to host format.
 	 */
 	data = malloc(sizeof(_RuneLocale) +
-	    (BSWAP(frl->runetype_ext_nranges) + BSWAP(frl->maplower_ext_nranges) +
-	    BSWAP(frl->mapupper_ext_nranges)) * sizeof(_RuneEntry) +
-	    runetype_ext_len * sizeof(*rr->__types) +
-	    BSWAP(frl->variable_len));
+	    (frl->runetype_ext_nranges + frl->maplower_ext_nranges +
+	    frl->mapupper_ext_nranges) * sizeof(_RuneEntry) +
+	    runetype_ext_len * sizeof(*rr->__types) + frl->variable_len);
 	if (data == NULL) {
 		saverr = errno;
 		munmap(fdata, sb.st_size);
@@ -167,15 +165,15 @@ _Read_RuneMagi(const char *fname)
 	memcpy(rl->__magic, _RUNE_MAGIC_1, sizeof(rl->__magic));
 	memcpy(rl->__encoding, frl->encoding, sizeof(rl->__encoding));
 
-	rl->__variable_len = BSWAP(frl->variable_len);
-	rl->__runetype_ext.__nranges = BSWAP(frl->runetype_ext_nranges);
-	rl->__maplower_ext.__nranges = BSWAP(frl->maplower_ext_nranges);
-	rl->__mapupper_ext.__nranges = BSWAP(frl->mapupper_ext_nranges);
+	rl->__variable_len = frl->variable_len;
+	rl->__runetype_ext.__nranges = frl->runetype_ext_nranges;
+	rl->__maplower_ext.__nranges = frl->maplower_ext_nranges;
+	rl->__mapupper_ext.__nranges = frl->mapupper_ext_nranges;
 
 	for (x = 0; x < _CACHED_RUNES; ++x) {
-		rl->__runetype[x] = BSWAP(frl->runetype[x]);
-		rl->__maplower[x] = BSWAP(frl->maplower[x]);
-		rl->__mapupper[x] = BSWAP(frl->mapupper[x]);
+		rl->__runetype[x] = frl->runetype[x];
+		rl->__maplower[x] = frl->maplower[x];
+		rl->__mapupper[x] = frl->mapupper[x];
 	}
 
 	rl->__runetype_ext.__ranges = (_RuneEntry *)rl->__variable;
@@ -190,15 +188,15 @@ _Read_RuneMagi(const char *fname)
 	rl->__variable = rl->__mapupper_ext.__ranges +
 	    rl->__mapupper_ext.__nranges;
 
-	variable = mapupper_ext_ranges + BSWAP(frl->mapupper_ext_nranges);
+	variable = mapupper_ext_ranges + frl->mapupper_ext_nranges;
 	frr = runetype_ext_ranges;
 	rr = rl->__runetype_ext.__ranges;
 	for (x = 0; x < rl->__runetype_ext.__nranges; ++x) {
 		uint32_t *types;
 
-		rr[x].__min = BSWAP(frr[x].min);
-		rr[x].__max = BSWAP(frr[x].max);
-		rr[x].__map = BSWAP(frr[x].map);
+		rr[x].__min = frr[x].min;
+		rr[x].__max = frr[x].max;
+		rr[x].__map = frr[x].map;
 		if (rr[x].__map == 0) {
 			int len = rr[x].__max - rr[x].__min + 1;
 			types = variable;
@@ -214,17 +212,17 @@ _Read_RuneMagi(const char *fname)
 	frr = maplower_ext_ranges;
 	rr = rl->__maplower_ext.__ranges;
 	for (x = 0; x < rl->__maplower_ext.__nranges; ++x) {
-		rr[x].__min = BSWAP(frr[x].min);
-		rr[x].__max = BSWAP(frr[x].max);
-		rr[x].__map = BSWAP(frr[x].map);
+		rr[x].__min = frr[x].min;
+		rr[x].__max = frr[x].max;
+		rr[x].__map = frr[x].map;
 	}
 
 	frr = mapupper_ext_ranges;
 	rr = rl->__mapupper_ext.__ranges;
 	for (x = 0; x < rl->__mapupper_ext.__nranges; ++x) {
-		rr[x].__min = BSWAP(frr[x].min);
-		rr[x].__max = BSWAP(frr[x].max);
-		rr[x].__map = BSWAP(frr[x].map);
+		rr[x].__min = frr[x].min;
+		rr[x].__max = frr[x].max;
+		rr[x].__map = frr[x].map;
 	}
 
 	memcpy(rl->__variable, variable, rl->__variable_len);

Modified: head/share/colldef/Makefile
==============================================================================
--- head/share/colldef/Makefile	Sat Oct 20 20:49:46 2018	(r339488)
+++ head/share/colldef/Makefile	Sat Oct 20 20:51:05 2018	(r339489)
@@ -7,8 +7,10 @@ FILESNAME=	LC_COLLATE
 .SUFFIXES:	.src .LC_COLLATE
 MAPLOC=		${.CURDIR}/../../tools/tools/locale/etc/final-maps
 
+.include <bsd.endian.mk>
+
 .src.LC_COLLATE:
-	localedef -D -U -i ${.IMPSRC} \
+	localedef ${LOCALEDEF_ENDIAN} -D -U -i ${.IMPSRC} \
 	-f ${MAPLOC}/map.${.TARGET:T:R:E:C/@.*//} ${.OBJDIR}/${.IMPSRC:T:R}
 
 LOCALES+=	af_ZA.UTF-8
@@ -223,7 +225,7 @@ FILESDIR_${f}.LC_COLLATE= ${LOCALEDIR}/${f}
 FILES+=	$t.LC_COLLATE
 FILESDIR_$t.LC_COLLATE=	${LOCALEDIR}/$t
 $t.LC_COLLATE: ${.CURDIR}/$f.src
-	localedef -D -U -i ${.ALLSRC} \
+	localedef ${LOCALEDEF_ENDIAN} -D -U -i ${.ALLSRC} \
 		-f ${MAPLOC}/map.${.TARGET:T:R:E:C/@.*//} \
 		${.OBJDIR}/${.TARGET:T:R}
 .endfor

Modified: head/share/ctypedef/Makefile
==============================================================================
--- head/share/ctypedef/Makefile	Sat Oct 20 20:49:46 2018	(r339488)
+++ head/share/ctypedef/Makefile	Sat Oct 20 20:51:05 2018	(r339489)
@@ -7,8 +7,10 @@ FILESNAME=	LC_CTYPE
 .SUFFIXES:	.src .LC_CTYPE
 MAPLOC=		${.CURDIR}/../../tools/tools/locale/etc/final-maps
 
+.include <bsd.endian.mk>
+
 .src.LC_CTYPE:
-	localedef -D -U -c -w ${MAPLOC}/widths.txt \
+	localedef ${LOCALEDEF_ENDIAN} -D -U -c -w ${MAPLOC}/widths.txt \
 	-f ${MAPLOC}/map.${.IMPSRC:T:R:E} \
 	-i ${.IMPSRC} ${.OBJDIR}/${.IMPSRC:T:R}  || true
 
@@ -238,7 +240,7 @@ SYMPAIRS+=	be_BY.CP1131.src ru_RU.KOI8-R.src
 
 .for s t in ${SYMPAIRS}
 ${t:S/src$/LC_CTYPE/}: $s
-	localedef -D -U -c -w ${MAPLOC}/widths.txt \
+	localedef ${LOCALEDEF_ENDIAN} -D -U -c -w ${MAPLOC}/widths.txt \
 	-f ${MAPLOC}/map.${.TARGET:T:R:C/^.*\.//} \
 	-i ${.ALLSRC} ${.OBJDIR}/${.TARGET:T:R}  || true
 .endfor

Modified: head/share/mk/bsd.endian.mk
==============================================================================
--- head/share/mk/bsd.endian.mk	Sat Oct 20 20:49:46 2018	(r339488)
+++ head/share/mk/bsd.endian.mk	Sat Oct 20 20:51:05 2018	(r339489)
@@ -8,6 +8,7 @@
     ${MACHINE_ARCH:Mmips*el*} != ""
 TARGET_ENDIANNESS= 1234
 CAP_MKDB_ENDIAN= -l
+LOCALEDEF_ENDIAN= -l
 .elif ${MACHINE_ARCH} == "powerpc" || \
     ${MACHINE_ARCH} == "powerpc64" || \
     ${MACHINE_ARCH} == "powerpcspe" || \
@@ -16,4 +17,5 @@ CAP_MKDB_ENDIAN= -l
     ${MACHINE_ARCH:Mmips*} != ""
 TARGET_ENDIANNESS= 4321
 CAP_MKDB_ENDIAN= -b
+LOCALEDEF_ENDIAN= -b
 .endif

Modified: head/targets/pseudo/userland/Makefile.depend
==============================================================================
--- head/targets/pseudo/userland/Makefile.depend	Sat Oct 20 20:49:46 2018	(r339488)
+++ head/targets/pseudo/userland/Makefile.depend	Sat Oct 20 20:51:05 2018	(r339489)
@@ -246,6 +246,7 @@ DIRDEPS+= \
 	usr.bin/lex/lib \
 	usr.bin/limits \
 	usr.bin/locale \
+	usr.bin/localedef \
 	usr.bin/locate/bigram \
 	usr.bin/locate/code \
 	usr.bin/locate/locate \

Modified: head/tools/tools/locale/tools/cldr2def.pl
==============================================================================
--- head/tools/tools/locale/tools/cldr2def.pl	Sat Oct 20 20:49:46 2018	(r339488)
+++ head/tools/tools/locale/tools/cldr2def.pl	Sat Oct 20 20:51:05 2018	(r339489)
@@ -857,7 +857,8 @@ sub make_makefile {
 	my $SRCOUT4 = "";
 	my $MAPLOC;
 	if ($TYPE eq "colldef") {
-		$SRCOUT = "localedef -D -U -i \${.IMPSRC} \\\n" .
+		$SRCOUT = "localedef \${LOCALEDEF_ENDIAN} -D -U " .
+			"-i \${.IMPSRC} \\\n" .
 			"\t-f \${MAPLOC}/map.\${.TARGET:T:R:E:C/@.*//} " .
 			"\${.OBJDIR}/\${.IMPSRC:T:R}";
 		$MAPLOC = "MAPLOC=\t\t\${.CURDIR}/../../tools/tools/" .
@@ -868,14 +869,16 @@ sub make_makefile {
 			"FILES+=\t\$t.LC_COLLATE\n" .
 			"FILESDIR_\$t.LC_COLLATE=\t\${LOCALEDIR}/\$t\n" .
 			"\$t.LC_COLLATE: \${.CURDIR}/\$f.src\n" .
-			"\tlocaledef -D -U -i \${.ALLSRC} \\\n" .
+			"\tlocaledef \${LOCALEDEF_ENDIAN} -D -U " .
+			"-i \${.ALLSRC} \\\n" .
 			"\t\t-f \${MAPLOC}/map.\${.TARGET:T:R:E:C/@.*//} \\\n" .
 			"\t\t\${.OBJDIR}/\${.TARGET:T:R}\n" .
 			".endfor\n\n";
 		$SRCOUT4 = "## LOCALES_MAPPED\n";
 	}
 	elsif ($TYPE eq "ctypedef") {
-		$SRCOUT = "localedef -D -U -c -w \${MAPLOC}/widths.txt \\\n" .
+		$SRCOUT = "localedef \${LOCALEDEF_ENDIAN} -D -U -c " .
+			"-w \${MAPLOC}/widths.txt \\\n" .
 			"\t-f \${MAPLOC}/map.\${.IMPSRC:T:R:E} " .
 			"\\\n\t-i \${.IMPSRC} \${.OBJDIR}/\${.IMPSRC:T:R} " .
 			" || true";
@@ -886,7 +889,8 @@ sub make_makefile {
 			".for s t in \${SYMPAIRS}\n" .
 			"\${t:S/src\$/LC_CTYPE/}: " .
 			"\$s\n" .
-			"\tlocaledef -D -U -c -w \${MAPLOC}/widths.txt \\\n" .
+			"\tlocaledef \${LOCALEDEF_ENDIAN} -D -U -c " .
+			"-w \${MAPLOC}/widths.txt \\\n" .
 			"\t-f \${MAPLOC}/map.\${.TARGET:T:R:C/^.*\\.//} " .
 			"\\\n\t-i \${.ALLSRC} \${.OBJDIR}/\${.TARGET:T:R} " .
 			" || true\n" .
@@ -907,6 +911,8 @@ LOCALEDIR=	\${SHAREDIR}/locale
 FILESNAME=	$FILESNAMES{$TYPE}
 .SUFFIXES:	.src .${SRCOUT2}
 ${MAPLOC}
+.include <bsd.endian.mk>
+
 .src.${SRCOUT2}:
 	$SRCOUT
 

Modified: head/usr.bin/localedef/collate.c
==============================================================================
--- head/usr.bin/localedef/collate.c	Sat Oct 20 20:49:46 2018	(r339488)
+++ head/usr.bin/localedef/collate.c	Sat Oct 20 20:51:05 2018	(r339489)
@@ -1,5 +1,5 @@
 /*-
- * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
+ * Copyright 2018 Nexenta Systems, Inc.
  * Copyright 2015 John Marino <draco at marino.st>
  *
  * This source code is derived from the illumos localedef command, and
@@ -253,6 +253,9 @@ static int32_t pri_undefined[COLL_WEIGHTS_MAX];
 static int32_t pri_ignore;
 
 static collate_info_t collinfo;
+static int32_t subst_count[COLL_WEIGHTS_MAX];
+static int32_t chain_count;
+static int32_t large_count;
 
 static collpri_t	*prilist = NULL;
 static int		numpri = 0;
@@ -1080,7 +1083,7 @@ wsncpy(wchar_t *s1, const wchar_t *s2, size_t n)
 	wchar_t *os1 = s1;
 
 	n++;
-	while (--n > 0 && (*s1++ = *s2++) != 0)
+	while (--n > 0 && (*s1++ = htote(*s2++)) != 0)
 		continue;
 	if (n > 0)
 		while (--n > 0)
@@ -1165,13 +1168,13 @@ dump_collate(void)
 		if (resolve_pri(pri_undefined[i]) == -1) {
 			set_pri(pri_undefined[i], -1, RESOLVED);
 			/* they collate at the end of everything else */
-			collinfo.undef_pri[i] = COLLATE_MAX_PRIORITY;
+			collinfo.undef_pri[i] = htote(COLLATE_MAX_PRIORITY);
 		}
-		collinfo.pri_count[i] = nweight[i];
+		collinfo.pri_count[i] = htote(nweight[i]);
 	}
 
-	collinfo.pri_count[NUM_WT] = max_wide();
-	collinfo.undef_pri[NUM_WT] = COLLATE_MAX_PRIORITY;
+	collinfo.pri_count[NUM_WT] = htote(max_wide());
+	collinfo.undef_pri[NUM_WT] = htote(COLLATE_MAX_PRIORITY);
 	collinfo.directive[NUM_WT] = DIRECTIVE_UNDEFINED;
 
 	/*
@@ -1180,19 +1183,20 @@ dump_collate(void)
 	for (i = 0; i <= UCHAR_MAX; i++) {
 		if ((cc = get_collchar(i, 0)) != NULL) {
 			for (j = 0; j < NUM_WT; j++) {
-				chars[i].pri[j] = get_weight(cc->ref[j], j);
+				chars[i].pri[j] =
+				    htote(get_weight(cc->ref[j], j));
 			}
 		} else {
 			for (j = 0; j < NUM_WT; j++) {
 				chars[i].pri[j] =
-				    get_weight(pri_undefined[j], j);
+				    htote(get_weight(pri_undefined[j], j));
 			}
 			/*
 			 * Per POSIX, for undefined characters, we
 			 * also have to add a last item, which is the
 			 * character code.
 			 */
-			chars[i].pri[NUM_WT] = i;
+			chars[i].pri[NUM_WT] = htote(i);
 		}
 	}
 
@@ -1203,7 +1207,7 @@ dump_collate(void)
 		collate_subst_t *st = NULL;
 		subst_t *temp;
 		RB_COUNT(temp, substs, &substs[i], n);
-		collinfo.subst_count[i] = n;
+		subst_count[i] = n;
 		if ((st = calloc(n, sizeof(collate_subst_t))) == NULL) {
 			fprintf(stderr, "out of memory");
 			return;
@@ -1217,12 +1221,14 @@ dump_collate(void)
 			if (st[n].key != (n | COLLATE_SUBST_PRIORITY)) {
 				INTERR;
 			}
+			st[n].key = htote(st[n].key);
 			for (j = 0; sb->ref[j]; j++) {
-				st[n].pri[j] = get_weight(sb->ref[j], i);
+				st[n].pri[j] = htote(get_weight(sb->ref[j],
+				    i));
 			}
 			n++;
 		}
-		if (n != collinfo.subst_count[i])
+		if (n != subst_count[i])
 			INTERR;
 		subst[i] = st;
 	}
@@ -1231,9 +1237,8 @@ dump_collate(void)
 	/*
 	 * Chains, i.e. collating elements
 	 */
-	RB_NUMNODES(collelem_t, elem_by_expand, &elem_by_expand,
-	    collinfo.chain_count);
-	chain = calloc(collinfo.chain_count, sizeof(collate_chain_t));
+	RB_NUMNODES(collelem_t, elem_by_expand, &elem_by_expand, chain_count);
+	chain = calloc(chain_count, sizeof(collate_chain_t));
 	if (chain == NULL) {
 		fprintf(stderr, "out of memory");
 		return;
@@ -1242,11 +1247,11 @@ dump_collate(void)
 	RB_FOREACH(ce, elem_by_expand, &elem_by_expand) {
 		(void) wsncpy(chain[n].str, ce->expand, COLLATE_STR_LEN);
 		for (i = 0; i < NUM_WT; i++) {
-			chain[n].pri[i] = get_weight(ce->ref[i], i);
+			chain[n].pri[i] = htote(get_weight(ce->ref[i], i));
 		}
 		n++;
 	}
-	if (n != collinfo.chain_count)
+	if (n != chain_count)
 		INTERR;
 
 	/*
@@ -1273,12 +1278,12 @@ dump_collate(void)
 				/* if undefined, then all priorities are */
 				INTERR;
 			} else {
-				large[i].pri.pri[j] = pri;
+				large[i].pri.pri[j] = htote(pri);
 			}
 		}
 		if (!undef) {
-			large[i].val = cc->wc;
-			collinfo.large_count = i++;
+			large[i].val = htote(cc->wc);
+			large_count = i++;
 		}
 	}
 
@@ -1288,6 +1293,11 @@ dump_collate(void)
 
 	/* Time to write the entire data set out */
 
+	for (i = 0; i < NUM_WT; i++)
+		collinfo.subst_count[i] = htote(subst_count[i]);
+	collinfo.chain_count = htote(chain_count);
+	collinfo.large_count = htote(large_count);
+
 	if ((wr_category(vers, COLLATE_STR_LEN, f) < 0) ||
 	    (wr_category(&collinfo, sizeof (collinfo), f) < 0) ||
 	    (wr_category(&chars, sizeof (chars), f) < 0)) {
@@ -1295,16 +1305,16 @@ dump_collate(void)
 	}
 
 	for (i = 0; i < NUM_WT; i++) {
-		sz =  sizeof (collate_subst_t) * collinfo.subst_count[i];
+		sz = sizeof (collate_subst_t) * subst_count[i];
 		if (wr_category(subst[i], sz, f) < 0) {
 			return;
 		}
 	}
-	sz = sizeof (collate_chain_t) * collinfo.chain_count;
+	sz = sizeof (collate_chain_t) * chain_count;
 	if (wr_category(chain, sz, f) < 0) {
 		return;
 	}
-	sz = sizeof (collate_large_t) * collinfo.large_count;
+	sz = sizeof (collate_large_t) * large_count;
 	if (wr_category(large, sz, f) < 0) {
 		return;
 	}

Modified: head/usr.bin/localedef/ctype.c
==============================================================================
--- head/usr.bin/localedef/ctype.c	Sat Oct 20 20:49:46 2018	(r339488)
+++ head/usr.bin/localedef/ctype.c	Sat Oct 20 20:51:05 2018	(r339489)
@@ -1,5 +1,5 @@
 /*-
- * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
+ * Copyright 2018 Nexenta Systems, Inc.
  * Copyright 2012 Garrett D'Amore <garrett at damore.org>  All rights reserved.
  * Copyright 2015 John Marino <draco at marino.st>
  *
@@ -296,10 +296,16 @@ dump_ctype(void)
 	_FileRuneEntry	*lo = NULL;
 	_FileRuneEntry	*up = NULL;
 	wchar_t		wc;
+	uint32_t	runetype_ext_nranges;
+	uint32_t	maplower_ext_nranges;
+	uint32_t	mapupper_ext_nranges;
 
 	(void) memset(&rl, 0, sizeof (rl));
+	runetype_ext_nranges = 0;
 	last_ct = NULL;
+	maplower_ext_nranges = 0;
 	last_lo = NULL;
+	mapupper_ext_nranges = 0;
 	last_up = NULL;
 
 	if ((f = open_category()) == NULL)
@@ -312,8 +318,8 @@ dump_ctype(void)
 	 * Initialize the identity map.
 	 */
 	for (wc = 0; (unsigned)wc < _CACHED_RUNES; wc++) {
-		rl.maplower[wc] = wc;
-		rl.mapupper[wc] = wc;
+		rl.maplower[wc] = htote(wc);
+		rl.mapupper[wc] = htote(wc);
 	}
 
 	RB_FOREACH(ctn, ctypes, &ctypes) {
@@ -399,39 +405,39 @@ dump_ctype(void)
 		 * upper/lower case, then we identity map it.
 		 */
 		if ((unsigned)wc < _CACHED_RUNES) {
-			rl.runetype[wc] = ctn->ctype;
+			rl.runetype[wc] = htote(ctn->ctype);
 			if (ctn->tolower)
-				rl.maplower[wc] = ctn->tolower;
+				rl.maplower[wc] = htote(ctn->tolower);
 			if (ctn->toupper)
-				rl.mapupper[wc] = ctn->toupper;
+				rl.mapupper[wc] = htote(ctn->toupper);
 			continue;
 		}
 
 		if ((last_ct != NULL) && (last_ct->ctype == ctn->ctype) &&
 		    (last_ct->wc + 1 == wc)) {
-			ct[rl.runetype_ext_nranges-1].max = wc;
+			ct[runetype_ext_nranges - 1].max = htote(wc);
 		} else {
-			rl.runetype_ext_nranges++;
-			ct = realloc(ct,
-			    sizeof (*ct) * rl.runetype_ext_nranges);
-			ct[rl.runetype_ext_nranges - 1].min = wc;
-			ct[rl.runetype_ext_nranges - 1].max = wc;
-			ct[rl.runetype_ext_nranges - 1].map = ctn->ctype;
+			runetype_ext_nranges++;
+			ct = realloc(ct, sizeof (*ct) * runetype_ext_nranges);
+			ct[runetype_ext_nranges - 1].min = htote(wc);
+			ct[runetype_ext_nranges - 1].max = htote(wc);
+			ct[runetype_ext_nranges - 1].map =
+			    htote(ctn->ctype);
 		}
 		last_ct = ctn;
 		if (ctn->tolower == 0) {
 			last_lo = NULL;
 		} else if ((last_lo != NULL) &&
 		    (last_lo->tolower + 1 == ctn->tolower)) {
-			lo[rl.maplower_ext_nranges-1].max = wc;
+			lo[maplower_ext_nranges - 1].max = htote(wc);
 			last_lo = ctn;
 		} else {
-			rl.maplower_ext_nranges++;
-			lo = realloc(lo,
-			    sizeof (*lo) * rl.maplower_ext_nranges);
-			lo[rl.maplower_ext_nranges - 1].min = wc;
-			lo[rl.maplower_ext_nranges - 1].max = wc;
-			lo[rl.maplower_ext_nranges - 1].map = ctn->tolower;
+			maplower_ext_nranges++;
+			lo = realloc(lo, sizeof (*lo) * maplower_ext_nranges);
+			lo[maplower_ext_nranges - 1].min = htote(wc);
+			lo[maplower_ext_nranges - 1].max = htote(wc);
+			lo[maplower_ext_nranges - 1].map =
+			    htote(ctn->tolower);
 			last_lo = ctn;
 		}
 
@@ -439,23 +445,26 @@ dump_ctype(void)
 			last_up = NULL;
 		} else if ((last_up != NULL) &&
 		    (last_up->toupper + 1 == ctn->toupper)) {
-			up[rl.mapupper_ext_nranges-1].max = wc;
+			up[mapupper_ext_nranges-1].max = htote(wc);
 			last_up = ctn;
 		} else {
-			rl.mapupper_ext_nranges++;
-			up = realloc(up,
-			    sizeof (*up) * rl.mapupper_ext_nranges);
-			up[rl.mapupper_ext_nranges - 1].min = wc;
-			up[rl.mapupper_ext_nranges - 1].max = wc;
-			up[rl.mapupper_ext_nranges - 1].map = ctn->toupper;
+			mapupper_ext_nranges++;
+			up = realloc(up, sizeof (*up) * mapupper_ext_nranges);
+			up[mapupper_ext_nranges - 1].min = htote(wc);
+			up[mapupper_ext_nranges - 1].max = htote(wc);
+			up[mapupper_ext_nranges - 1].map =
+			    htote(ctn->toupper);
 			last_up = ctn;
 		}
 	}
 
+	rl.runetype_ext_nranges = htote(runetype_ext_nranges);
+	rl.maplower_ext_nranges = htote(maplower_ext_nranges);
+	rl.mapupper_ext_nranges = htote(mapupper_ext_nranges);
 	if ((wr_category(&rl, sizeof (rl), f) < 0) ||
-	    (wr_category(ct, sizeof (*ct) * rl.runetype_ext_nranges, f) < 0) ||
-	    (wr_category(lo, sizeof (*lo) * rl.maplower_ext_nranges, f) < 0) ||
-	    (wr_category(up, sizeof (*up) * rl.mapupper_ext_nranges, f) < 0)) {
+	    (wr_category(ct, sizeof (*ct) * runetype_ext_nranges, f) < 0) ||
+	    (wr_category(lo, sizeof (*lo) * maplower_ext_nranges, f) < 0) ||
+	    (wr_category(up, sizeof (*up) * mapupper_ext_nranges, f) < 0)) {
 		return;
 	}
 

Modified: head/usr.bin/localedef/localedef.1
==============================================================================
--- head/usr.bin/localedef/localedef.1	Sat Oct 20 20:49:46 2018	(r339488)
+++ head/usr.bin/localedef/localedef.1	Sat Oct 20 20:51:05 2018	(r339489)
@@ -33,7 +33,7 @@
 .\"
 .\" $FreeBSD$
 .\"
-.Dd July 28, 2015
+.Dd October 18, 2018
 .Dt LOCALEDEF 1
 .Os
 .Sh NAME
@@ -41,7 +41,7 @@
 .Nd define locale environment
 .Sh SYNOPSIS
 .Nm
-.Op Fl cDUv
+.Op Fl bcDlUv
 .Op Fl f Ar charmap
 .Op Fl i Ar sourcefile
 .Op Fl u Ar codeset
@@ -84,6 +84,8 @@ Defines the format and values of affirmative and negat
 .Pp
 The following options are supported:
 .Bl -tag -width indent
+.It Fl b
+Use big-endian byte order for output.
 .It Fl c
 Creates permanent output even if warning messages have been issued.
 .It Fl D
@@ -112,6 +114,8 @@ option is not present, the default character mapping w
 The path name of a file containing the source definitions.
 If this option is not present, source definitions will be read from
 standard input.
+.It Fl l
+Use little-endian byte order for output.
 .It Fl u Ar codeset
 Specifies the name of a codeset used as the target mapping of character symbols
 and collating element symbols whose encoding values are defined in terms of the

Modified: head/usr.bin/localedef/localedef.c
==============================================================================
--- head/usr.bin/localedef/localedef.c	Sat Oct 20 20:49:46 2018	(r339488)
+++ head/usr.bin/localedef/localedef.c	Sat Oct 20 20:51:05 2018	(r339489)
@@ -1,5 +1,5 @@
 /*-
- * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
+ * Copyright 2018 Nexenta Systems, Inc.
  * Copyright 2015 John Marino <draco at marino.st>
  *
  * This source code is derived from the illumos localedef command, and
@@ -34,11 +34,13 @@
 #include <sys/cdefs.h>
 __FBSDID("$FreeBSD$");
 
+#include <sys/endian.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <errno.h>
-#include <sys/types.h>
-#include <sys/stat.h>
 #include <string.h>
 #include <libgen.h>
 #include <stddef.h>
@@ -54,6 +56,7 @@ __FBSDID("$FreeBSD$");
 #endif
 
 static int bsd = 0;
+static int byteorder = 0;
 int verbose = 0;
 int undefok = 0;
 int warnok = 0;
@@ -221,6 +224,18 @@ wr_category(void *buf, size_t sz, FILE *f)
 	return (0);
 }
 
+uint32_t
+htote(uint32_t arg)
+{
+
+	if (byteorder == 4321)
+		return (htobe32(arg));
+	else if (byteorder == 1234)
+		return (htole32(arg));
+	else
+		return (arg);
+}
+
 int yyparse(void);
 
 static void
@@ -229,7 +244,9 @@ usage(void)
 	(void) fprintf(stderr, "Usage: localedef [options] localename\n");
 	(void) fprintf(stderr, "[options] are:\n");
 	(void) fprintf(stderr, "  -D          : BSD-style output\n");
+	(void) fprintf(stderr, "  -b          : big-endian output\n");
 	(void) fprintf(stderr, "  -c          : ignore warnings\n");
+	(void) fprintf(stderr, "  -l          : little-endian output\n");
 	(void) fprintf(stderr, "  -v          : verbose output\n");
 	(void) fprintf(stderr, "  -U          : ignore undefined symbols\n");
 	(void) fprintf(stderr, "  -f charmap  : use given charmap file\n");
@@ -260,10 +277,16 @@ main(int argc, char **argv)
 
 	(void) setlocale(LC_ALL, "");
 
-	while ((c = getopt(argc, argv, "w:i:cf:u:vUD")) != -1) {
+	while ((c = getopt(argc, argv, "blw:i:cf:u:vUD")) != -1) {
 		switch (c) {
 		case 'D':
 			bsd = 1;
+			break;
+		case 'b':
+		case 'l':
+			if (byteorder != 0)
+				usage();
+			byteorder = c == 'b' ? 4321 : 1234;
 			break;
 		case 'v':
 			verbose++;

Modified: head/usr.bin/localedef/localedef.h
==============================================================================
--- head/usr.bin/localedef/localedef.h	Sat Oct 20 20:49:46 2018	(r339488)
+++ head/usr.bin/localedef/localedef.h	Sat Oct 20 20:51:05 2018	(r339489)
@@ -1,5 +1,5 @@
 /*-
- * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
+ * Copyright 2018 Nexenta Systems, Inc.
  * Copyright 2015 John Marino <draco at marino.st>
  *
  * This source code is derived from the illumos localedef command, and
@@ -74,6 +74,8 @@ void scan_to_eol(void);
 void add_wcs(wchar_t);
 void add_tok(int);
 wchar_t *get_wcs(void);
+
+uint32_t htote(uint32_t);
 
 /* charmap.c - CHARMAP handling */
 void init_charmap(void);


More information about the svn-src-head mailing list