Index: lib/libc/locale/collate.h =================================================================== --- lib/libc/locale/collate.h +++ lib/libc/locale/collate.h @@ -112,8 +112,8 @@ int32_t pri[COLLATE_STR_LEN]; } collate_subst_t; -struct xlocale_collate { - struct xlocale_component header; +XLOCALE_STRUCT(collate) { + XLOCALE_STRUCT(component) header; int __collate_load_error; char * map; size_t maplen; @@ -127,14 +127,14 @@ __BEGIN_DECLS int __collate_load_tables(const char *); -int __collate_equiv_value(locale_t, const wchar_t *, size_t); -void _collate_lookup(struct xlocale_collate *,const wchar_t *, int *, int *, +int __collate_equiv_value(XLOCALE_NAMESPACED(locale_t), const wchar_t *, size_t); +void _collate_lookup(XLOCALE_STRUCT(collate) *,const wchar_t *, int *, int *, int, const int **); int __collate_range_cmp(char, char); int __wcollate_range_cmp(wchar_t, wchar_t); -size_t _collate_wxfrm(struct xlocale_collate *, const wchar_t *, wchar_t *, +size_t _collate_wxfrm(XLOCALE_STRUCT(collate) *, const wchar_t *, wchar_t *, size_t); -size_t _collate_sxfrm(struct xlocale_collate *, const wchar_t *, char *, +size_t _collate_sxfrm(XLOCALE_STRUCT(collate) *, const wchar_t *, char *, size_t); __END_DECLS Index: lib/libc/locale/lmessages.h =================================================================== --- lib/libc/locale/lmessages.h +++ lib/libc/locale/lmessages.h @@ -45,7 +45,7 @@ const char *nostr; }; -struct lc_messages_T *__get_current_messages_locale(locale_t); +struct lc_messages_T *__get_current_messages_locale(XLOCALE_NAMESPACED(locale_t)); int __messages_load_locale(const char *); #endif /* !_LMESSAGES_H_ */ Index: lib/libc/locale/lmonetary.h =================================================================== --- lib/libc/locale/lmonetary.h +++ lib/libc/locale/lmonetary.h @@ -61,12 +61,12 @@ const char *int_n_sign_posn; }; struct xlocale_monetary { - struct xlocale_component header; + XLOCALE_STRUCT(component) header; char *buffer; struct lc_monetary_T locale; }; -struct lc_monetary_T *__get_current_monetary_locale(locale_t loc); +struct lc_monetary_T *__get_current_monetary_locale(XLOCALE_NAMESPACED(locale_t) loc); int __monetary_load_locale(const char *); #endif /* !_LMONETARY_H_ */ Index: lib/libc/locale/lnumeric.h =================================================================== --- lib/libc/locale/lnumeric.h +++ lib/libc/locale/lnumeric.h @@ -43,12 +43,12 @@ const char *grouping; }; struct xlocale_numeric { - struct xlocale_component header; + XLOCALE_STRUCT(component) header; char *buffer; struct lc_numeric_T locale; }; -struct lc_numeric_T *__get_current_numeric_locale(locale_t loc); +struct lc_numeric_T *__get_current_numeric_locale(XLOCALE_NAMESPACED(locale_t) loc); int __numeric_load_locale(const char *); #endif /* !_LNUMERIC_H_ */ Index: lib/libc/locale/xlocale_private.h =================================================================== --- lib/libc/locale/xlocale_private.h +++ lib/libc/locale/xlocale_private.h @@ -42,6 +42,20 @@ #include #include "setlocale.h" + +/* + * When building the host tool localedef we can't use the definitions on the + * build system (they might be incompatible), but instead need the target + * system's definitions. The simplest solution for this problem is to namespace + * the structures and functions. + */ +#ifdef __BUILDING_LOCALEDEF_BOOTSTRAP +#define XLOCALE_NAMESPACED(name) __CONCAT(_bootstrap_xlocale_,name) +#else +#define XLOCALE_NAMESPACED(name) __CONCAT(xlocale_,name) +#endif +#define XLOCALE_STRUCT(name) struct XLOCALE_NAMESPACED(name) + /** * The XLC_ values are indexes into the components array. They are defined in * the same order as the LC_ values in locale.h, but without the LC_ALL zero @@ -61,6 +75,7 @@ }; _Static_assert(XLC_LAST - XLC_COLLATE == 6, "XLC values should be contiguous"); +#ifndef __BUILDING_LOCALEDEF_BOOTSTRAP _Static_assert(XLC_COLLATE == LC_COLLATE - 1, "XLC_COLLATE doesn't match the LC_COLLATE value."); _Static_assert(XLC_CTYPE == LC_CTYPE - 1, @@ -73,6 +88,7 @@ "XLC_TIME doesn't match the LC_TIME value."); _Static_assert(XLC_MESSAGES == LC_MESSAGES - 1, "XLC_MESSAGES doesn't match the LC_MESSAGES value."); +#endif /** * Header used for objects that are reference counted. Objects may optionally @@ -85,7 +101,7 @@ * count of 1 when they are created, but the retain count is 0. When the * retain count is less than 0, they are freed. */ -struct xlocale_refcounted { +XLOCALE_STRUCT(refcounted) { /** Number of references to this component. */ long retain_count; /** Function used to destroy this component, if one is required*/ @@ -95,8 +111,8 @@ * Header for a locale component. All locale components must begin with this * header. */ -struct xlocale_component { - struct xlocale_refcounted header; +XLOCALE_STRUCT(component) { + XLOCALE_STRUCT(refcounted) header; /** Name of the locale used for this component. */ char locale[ENCODING_LEN+1]; }; @@ -105,9 +121,9 @@ * xlocale structure, stores per-thread locale information. */ struct _xlocale { - struct xlocale_refcounted header; + XLOCALE_STRUCT(refcounted) header; /** Components for the locale. */ - struct xlocale_component *components[XLC_LAST]; + XLOCALE_STRUCT(component) *components[XLC_LAST]; /** Flag indicating if components[XLC_MONETARY] has changed since the * last call to localeconv_l() with this locale. */ int monetary_locale_changed; @@ -131,14 +147,15 @@ /** Buffer used by nl_langinfo_l() */ char *csym; }; +typedef struct _xlocale* XLOCALE_NAMESPACED(locale_t); /** * Increments the reference count of a reference-counted structure. */ __attribute__((unused)) static void* -xlocale_retain(void *val) +XLOCALE_NAMESPACED(retain)(void *val) { - struct xlocale_refcounted *obj = val; + XLOCALE_STRUCT(refcounted) *obj = val; atomic_add_long(&(obj->retain_count), 1); return (val); } @@ -147,9 +164,9 @@ * if this is the last reference, calling its destructor if it has one. */ __attribute__((unused)) static void -xlocale_release(void *val) +XLOCALE_NAMESPACED(release)(void *val) { - struct xlocale_refcounted *obj = val; + XLOCALE_STRUCT(refcounted) *obj = val; long count; count = atomic_fetchadd_long(&(obj->retain_count), -1) - 1; @@ -161,12 +178,12 @@ * Load functions. Each takes the name of a locale and a pointer to the data * to be initialised as arguments. Two special values are allowed for the */ -extern void* __collate_load(const char*, locale_t); -extern void* __ctype_load(const char*, locale_t); -extern void* __messages_load(const char*, locale_t); -extern void* __monetary_load(const char*, locale_t); -extern void* __numeric_load(const char*, locale_t); -extern void* __time_load(const char*, locale_t); +extern void* __collate_load(const char*, XLOCALE_NAMESPACED(locale_t)); +extern void* __ctype_load(const char*, XLOCALE_NAMESPACED(locale_t)); +extern void* __messages_load(const char*, XLOCALE_NAMESPACED(locale_t)); +extern void* __monetary_load(const char*, XLOCALE_NAMESPACED(locale_t)); +extern void* __numeric_load(const char*, XLOCALE_NAMESPACED(locale_t)); +extern void* __time_load(const char*, XLOCALE_NAMESPACED(locale_t)); extern struct _xlocale __xlocale_global_locale; extern struct _xlocale __xlocale_C_locale; @@ -174,7 +191,7 @@ /** * Caches the rune table in TLS for fast access. */ -void __set_thread_rune_locale(locale_t loc); +void __set_thread_rune_locale(XLOCALE_NAMESPACED(locale_t) loc); /** * Flag indicating whether a per-thread locale has been set. If no per-thread * locale has ever been set, then we always use the global locale. @@ -185,7 +202,7 @@ * The per-thread locale. Avoids the need to use pthread lookup functions when * getting the per-thread locale. */ -extern _Thread_local locale_t __thread_locale; +extern _Thread_local XLOCALE_NAMESPACED(locale_t) __thread_locale; /** * Returns the current locale for this thread, or the global locale if none is @@ -193,7 +210,7 @@ * this call is not guaranteed to remain valid after the locale changes. As * such, this should only be called within libc functions. */ -static inline locale_t __get_locale(void) +static inline XLOCALE_NAMESPACED(locale_t) __get_locale(void) { if (!__has_thread_locale) { @@ -202,14 +219,15 @@ return (__thread_locale ? __thread_locale : &__xlocale_global_locale); } #else -locale_t __get_locale(void); +XLOCALE_NAMESPACED(locale_t) __get_locale(void); #endif /** * Two magic values are allowed for locale_t objects. NULL and -1. This * function maps those to the real locales that they represent. */ -static inline locale_t get_real_locale(locale_t locale) +static inline XLOCALE_NAMESPACED(locale_t) +get_real_locale(XLOCALE_NAMESPACED(locale_t) locale) { switch ((intptr_t)locale) { case 0: return (&__xlocale_C_locale); Index: lib/libc/stdtime/timelocal.h =================================================================== --- lib/libc/stdtime/timelocal.h +++ lib/libc/stdtime/timelocal.h @@ -57,7 +57,7 @@ const char *ampm_fmt; }; -struct lc_time_T *__get_current_time_locale(locale_t); +struct lc_time_T *__get_current_time_locale(XLOCALE_NAMESPACED(locale_t)); int __time_load_locale(const char *); #endif /* !_TIMELOCAL_H_ */ Index: usr.bin/localedef/Makefile =================================================================== --- usr.bin/localedef/Makefile +++ usr.bin/localedef/Makefile @@ -20,6 +20,10 @@ IGNORE_PRAGMA= yes CFLAGS+= -I. -I${.CURDIR} +.if defined(BOOTSTRAPPING) +CFLAGS+= -D__BUILDING_LOCALEDEF_BOOTSTRAP=1 +CFLAGS+= -I${.CURDIR}/bootstrap +.endif CFLAGS+= -I${SRCTOP}/lib/libc/locale CFLAGS+= -I${SRCTOP}/lib/libc/stdtime Index: usr.bin/localedef/bootstrap/_ctype.h =================================================================== --- usr.bin/localedef/bootstrap/_ctype.h +++ usr.bin/localedef/bootstrap/_ctype.h @@ -1,13 +1,16 @@ /*- - * SPDX-License-Identifier: BSD-2-Clause-FreeBSD + * SPDX-License-Identifier: BSD-2-Clause * - * Copyright (c) 1997-2002 FreeBSD Project. - * All rights reserved. + * Copyright 2018-2020 Alex Richardson * - * Copyright (c) 2011 The FreeBSD Foundation - * All rights reserved. - * Portions of this software were developed by David Chisnall - * under sponsorship from the FreeBSD Foundation. + * This software was developed by SRI International and the University of + * Cambridge Computer Laboratory (Department of Computer Science and + * Technology) under DARPA contract HR0011-18-C-0016 ("ECATS"), as part of the + * DARPA SSITH research programme. + * + * This software was developed by SRI International and the University of + * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237) + * ("CTSRD"), as part of the DARPA CRASH research programme. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -33,31 +36,13 @@ * $FreeBSD$ */ -#ifndef _TIMELOCAL_H_ -#define _TIMELOCAL_H_ -#include "xlocale_private.h" - /* - * Private header file for the strftime and strptime localization - * stuff. + * We have to ensure that we use the same constants as the target system when + * bootstrapping localdef so that we generate compatible databases. */ -struct lc_time_T { - const char *mon[12]; - const char *month[12]; - const char *wday[7]; - const char *weekday[7]; - const char *X_fmt; - const char *x_fmt; - const char *c_fmt; - const char *am; - const char *pm; - const char *date_fmt; - const char *alt_month[12]; - const char *md_order; - const char *ampm_fmt; -}; - -struct lc_time_T *__get_current_time_locale(locale_t); -int __time_load_locale(const char *); - -#endif /* !_TIMELOCAL_H_ */ +#ifndef __FreeBSD__ +typedef int __ct_rune_t; /* arg type for ctype funcs */ +typedef __ct_rune_t __rune_t; /* rune_t (see above) */ +typedef __ct_rune_t __wint_t; /* wint_t (see above) */ +#endif +#include "../../include/_ctype.h" Index: usr.bin/localedef/bootstrap/ctype.h =================================================================== --- usr.bin/localedef/bootstrap/ctype.h +++ usr.bin/localedef/bootstrap/ctype.h @@ -1,13 +1,16 @@ /*- - * SPDX-License-Identifier: BSD-2-Clause-FreeBSD + * SPDX-License-Identifier: BSD-2-Clause * - * Copyright (c) 1997-2002 FreeBSD Project. - * All rights reserved. + * Copyright 2018-2020 Alex Richardson * - * Copyright (c) 2011 The FreeBSD Foundation - * All rights reserved. - * Portions of this software were developed by David Chisnall - * under sponsorship from the FreeBSD Foundation. + * This software was developed by SRI International and the University of + * Cambridge Computer Laboratory (Department of Computer Science and + * Technology) under DARPA contract HR0011-18-C-0016 ("ECATS"), as part of the + * DARPA SSITH research programme. + * + * This software was developed by SRI International and the University of + * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237) + * ("CTSRD"), as part of the DARPA CRASH research programme. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -32,32 +35,24 @@ * * $FreeBSD$ */ - -#ifndef _TIMELOCAL_H_ -#define _TIMELOCAL_H_ -#include "xlocale_private.h" +#pragma once +#include +#include_next /* - * Private header file for the strftime and strptime localization - * stuff. + * On macOS isxdigit(), etc are defined in _ctype.h, but we have to include the + * target _ctype.h so that localedef uses the correct values. + * To fix macOS bootstrap, provide a declaration of isxdigit(), etc. here. + * However, only do so if they are not already define as a macro (as is the + * case on Linux). */ -struct lc_time_T { - const char *mon[12]; - const char *month[12]; - const char *wday[7]; - const char *weekday[7]; - const char *X_fmt; - const char *x_fmt; - const char *c_fmt; - const char *am; - const char *pm; - const char *date_fmt; - const char *alt_month[12]; - const char *md_order; - const char *ampm_fmt; -}; - -struct lc_time_T *__get_current_time_locale(locale_t); -int __time_load_locale(const char *); - -#endif /* !_TIMELOCAL_H_ */ +#ifndef isxdigit +__BEGIN_DECLS +int isalpha(int); +int isascii(int); +int isdigit(int); +int isgraph(int); +int islower(int); +int isxdigit(int); +__END_DECLS +#endif Index: usr.bin/localedef/bootstrap/limits.h =================================================================== --- usr.bin/localedef/bootstrap/limits.h +++ usr.bin/localedef/bootstrap/limits.h @@ -1,13 +1,16 @@ /*- - * SPDX-License-Identifier: BSD-2-Clause-FreeBSD + * SPDX-License-Identifier: BSD-2-Clause * - * Copyright (c) 2000, 2001 Alexey Zelkin - * All rights reserved. + * Copyright 2018-2020 Alex Richardson * - * Copyright (c) 2011 The FreeBSD Foundation - * All rights reserved. - * Portions of this software were developed by David Chisnall - * under sponsorship from the FreeBSD Foundation. + * This software was developed by SRI International and the University of + * Cambridge Computer Laboratory (Department of Computer Science and + * Technology) under DARPA contract HR0011-18-C-0016 ("ECATS"), as part of the + * DARPA SSITH research programme. + * + * This software was developed by SRI International and the University of + * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237) + * ("CTSRD"), as part of the DARPA CRASH research programme. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -32,20 +35,14 @@ * * $FreeBSD$ */ +#include_next -#ifndef _LMESSAGES_H_ -#define _LMESSAGES_H_ - -#include "xlocale_private.h" - -struct lc_messages_T { - const char *yesexpr; - const char *noexpr; - const char *yesstr; - const char *nostr; -}; - -struct lc_messages_T *__get_current_messages_locale(locale_t); -int __messages_load_locale(const char *); +#ifndef COLL_WEIGHTS_MAX +#error "COLL_WEIGHTS_MAX missing" +#endif -#endif /* !_LMESSAGES_H_ */ +#if COLL_WEIGHTS_MAX != 10 +#pragma message("Changing value of COLL_WEIGHTS_MAX") +#undef COLL_WEIGHTS_MAX +#define COLL_WEIGHTS_MAX 10 +#endif Index: usr.bin/localedef/bootstrap/xlocale.h =================================================================== --- usr.bin/localedef/bootstrap/xlocale.h +++ usr.bin/localedef/bootstrap/xlocale.h @@ -1,13 +1,16 @@ /*- - * SPDX-License-Identifier: BSD-2-Clause-FreeBSD + * SPDX-License-Identifier: BSD-2-Clause * - * Copyright (c) 2000, 2001 Alexey Zelkin - * All rights reserved. + * Copyright 2018-2020 Alex Richardson * - * Copyright (c) 2011 The FreeBSD Foundation - * All rights reserved. - * Portions of this software were developed by David Chisnall - * under sponsorship from the FreeBSD Foundation. + * This software was developed by SRI International and the University of + * Cambridge Computer Laboratory (Department of Computer Science and + * Technology) under DARPA contract HR0011-18-C-0016 ("ECATS"), as part of the + * DARPA SSITH research programme. + * + * This software was developed by SRI International and the University of + * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237) + * ("CTSRD"), as part of the DARPA CRASH research programme. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -32,20 +35,8 @@ * * $FreeBSD$ */ - -#ifndef _LMESSAGES_H_ -#define _LMESSAGES_H_ - -#include "xlocale_private.h" - -struct lc_messages_T { - const char *yesexpr; - const char *noexpr; - const char *yesstr; - const char *nostr; -}; - -struct lc_messages_T *__get_current_messages_locale(locale_t); -int __messages_load_locale(const char *); - -#endif /* !_LMESSAGES_H_ */ +/* + * This header only exists to avoid pulling in the host xlocale.h from + * the libc-internal headers. This is required since newer Linux GLibc no + * longer includes xlocale.h and older versions include an incompatible header. + */ Index: usr.bin/localedef/collate.c =================================================================== --- usr.bin/localedef/collate.c +++ usr.bin/localedef/collate.c @@ -49,6 +49,8 @@ #include "parser.h" #include "collate.h" +_Static_assert(COLL_WEIGHTS_MAX == 10, "This code assumes a value of 10"); + /* * Design notes. * @@ -850,7 +852,8 @@ add_order_directive(void) { if (collinfo.directive_count >= COLL_WEIGHTS_MAX) { - fprintf(stderr,"too many directives (max %d)", COLL_WEIGHTS_MAX); + fprintf(stderr,"too many directives (max %d)\n", COLL_WEIGHTS_MAX); + return; } collinfo.directive_count++; } @@ -859,7 +862,7 @@ add_order_pri(int32_t ref) { if (curr_weight >= NUM_WT) { - fprintf(stderr,"too many weights (max %d)", NUM_WT); + fprintf(stderr,"too many weights (max %d)\n", NUM_WT); return; } order_weights[curr_weight] = ref; Index: usr.bin/localedef/ctype.c =================================================================== --- usr.bin/localedef/ctype.c +++ usr.bin/localedef/ctype.c @@ -43,11 +43,13 @@ #include #include #include -#include -#include #include #include "localedef.h" #include "parser.h" + +/* Always include the defines for the target: */ +#define _DONT_USE_CTYPE_INLINE_ /* Avoid dependencies on runetype.h" */ +#include "_ctype.h" #include "runefile.h"