From f019e786788385888db4ff63f650e8d3bad167ce Mon Sep 17 00:00:00 2001 From: Cameron Katri Date: Sun, 23 May 2021 11:51:28 -0400 Subject: text_cmds: Fix compilation for lower targets --- text_cmds/tr/collate-fbsd.c | 158 +++++++++++++++++++++++++++++++++++++++++ text_cmds/tr/collate.h | 121 +++++++++++++++++++++++++++++++ text_cmds/tr/setlocale.h | 42 +++++++++++ text_cmds/tr/str.c | 4 ++ text_cmds/tr/xlocale_private.h | 97 +++++++++++++++++++++++++ 5 files changed, 422 insertions(+) create mode 100644 text_cmds/tr/collate-fbsd.c create mode 100644 text_cmds/tr/collate.h create mode 100644 text_cmds/tr/setlocale.h create mode 100644 text_cmds/tr/xlocale_private.h diff --git a/text_cmds/tr/collate-fbsd.c b/text_cmds/tr/collate-fbsd.c new file mode 100644 index 0000000..c4d8997 --- /dev/null +++ b/text_cmds/tr/collate-fbsd.c @@ -0,0 +1,158 @@ +/*- + * Copyright (c) 1995 Alex Tatmanjants + * at Electronni Visti IA, Kiev, Ukraine. + * All rights reserved. + * + * 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 AUTHOR ``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 AUTHOR 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 +__FBSDID("$FreeBSD: src/lib/libc/locale/collate.c,v 1.33 2004/09/22 16:56:48 stefanf Exp $"); + +#include "xlocale_private.h" +#define __collate_chain_equiv_table (loc->__lc_collate->__chain_equiv_table) +#define __collate_chain_pri_table (loc->__lc_collate->__chain_pri_table) +#define __collate_char_pri_table (loc->__lc_collate->__char_pri_table) +#define __collate_info (&loc->__lc_collate->__info) +#define __collate_large_char_pri_table (loc->__lc_collate->__large_char_pri_table) +#define __collate_substitute_table (loc->__lc_collate->__substitute_table) + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#if __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN +static void wntohl(wchar_t *, int); +#endif /* __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN */ +void __collate_err(int ex, const char *f) __dead2; + +/* + * Normally, the __collate_* routines should all be __private_extern__, + * but grep is using them (3715846). Until we can provide an alternative, + * we leave them public, and provide a read-only __collate_load_error variable + */ +#undef __collate_load_error +int __collate_load_error = 1; + +static int +__collate_wcsnlen(const wchar_t *s, int len) +{ + int n = 0; + while (*s && n < len) { + s++; + n++; + } + return n; +} + +static struct __collate_st_chain_pri * +chainsearch(const wchar_t *key, int *len, locale_t loc) +{ + int low = 0; + int high = __collate_info->chain_count - 1; + int next, compar, l; + struct __collate_st_chain_pri *p; + struct __collate_st_chain_pri *tab = __collate_chain_pri_table; + + while (low <= high) { + next = (low + high) / 2; + p = tab + next; + compar = *key - *p->str; + if (compar == 0) { + l = __collate_wcsnlen(p->str, STR_LEN); + compar = wcsncmp(key, p->str, l); + if (compar == 0) { + *len = l; + return p; + } + } + if (compar > 0) + low = next + 1; + else + high = next - 1; + } + return NULL; +} + +static struct __collate_st_large_char_pri * +largesearch(const wchar_t key, locale_t loc) +{ + int low = 0; + int high = __collate_info->large_pri_count - 1; + int next, compar; + struct __collate_st_large_char_pri *p; + struct __collate_st_large_char_pri *tab = __collate_large_char_pri_table; + + while (low <= high) { + next = (low + high) / 2; + p = tab + next; + compar = key - p->val; + if (compar == 0) + return p; + if (compar > 0) + low = next + 1; + else + high = next - 1; + } + return NULL; +} + +void +__collate_lookup_l(const wchar_t *t, int *len, int *prim, int *sec, locale_t loc) +{ + struct __collate_st_chain_pri *p2; + int l; + + *len = 1; + *prim = *sec = 0; + p2 = chainsearch(t, &l, loc); + /* use the chain if prim >= 0 */ + if (p2 && p2->pri[0] >= 0) { + *len = l; + *prim = p2->pri[0]; + *sec = p2->pri[1]; + return; + } + if (*t <= UCHAR_MAX) { + *prim = __collate_char_pri_table[*t].pri[0]; + *sec = __collate_char_pri_table[*t].pri[1]; + return; + } + if (__collate_info->large_pri_count > 0) { + struct __collate_st_large_char_pri *match; + match = largesearch(*t, loc); + if (match) { + *prim = match->pri.pri[0]; + *sec = match->pri.pri[1]; + return; + } + } + *prim = (l = __collate_info->undef_pri[0]) >= 0 ? l : *t - l; + *sec = (l = __collate_info->undef_pri[1]) >= 0 ? l : *t - l; +} diff --git a/text_cmds/tr/collate.h b/text_cmds/tr/collate.h new file mode 100644 index 0000000..494e231 --- /dev/null +++ b/text_cmds/tr/collate.h @@ -0,0 +1,121 @@ +/*- + * Copyright (c) 1995 Alex Tatmanjants + * at Electronni Visti IA, Kiev, Ukraine. + * All rights reserved. + * + * 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 AUTHOR ``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 AUTHOR 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. + * + * $FreeBSD: src/lib/libc/locale/collate.h,v 1.14 2002/08/30 20:26:02 ache Exp $ + */ + +#ifndef _COLLATE_H_ +#define _COLLATE_H_ + +#include +#ifndef __LIBC__ +#include +#endif /* !__LIBC__ */ +#include + +#define STR_LEN 10 +#define TABLE_SIZE 100 +#define COLLATE_VERSION "1.0\n" +#define COLLATE_VERSION1_1 "1.1\n" +#define COLLATE_VERSION1_1A "1.1A\n" +/* see discussion in string/FreeBSD/strxfrm for this value */ +#define COLLATE_MAX_PRIORITY ((1 << 24) - 1) + +#define DIRECTIVE_UNDEF 0x00 +#define DIRECTIVE_FORWARD 0x01 +#define DIRECTIVE_BACKWARD 0x02 +#define DIRECTIVE_POSITION 0x04 + +#define DIRECTIVE_DIRECTION_MASK (DIRECTIVE_FORWARD | DIRECTIVE_BACKWARD) + +#define COLLATE_SUBST_DUP 0x0001 + +#define IGNORE_EQUIV_CLASS 1 + +struct __collate_st_info { + __uint8_t directive[COLL_WEIGHTS_MAX]; + __uint8_t flags; +#if __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN + __uint8_t directive_count:4; + __uint8_t chain_max_len:4; +#else + __uint8_t chain_max_len:4; + __uint8_t directive_count:4; +#endif + __int32_t undef_pri[COLL_WEIGHTS_MAX]; + __int32_t subst_count[COLL_WEIGHTS_MAX]; + __int32_t chain_count; + __int32_t large_pri_count; +}; + +struct __collate_st_char_pri { + __int32_t pri[COLL_WEIGHTS_MAX]; +}; +struct __collate_st_chain_pri { + __darwin_wchar_t str[STR_LEN]; + __int32_t pri[COLL_WEIGHTS_MAX]; +}; +struct __collate_st_large_char_pri { + __int32_t val; + struct __collate_st_char_pri pri; +}; +struct __collate_st_subst { + __int32_t val; + __darwin_wchar_t str[STR_LEN]; +}; + +#ifndef __LIBC__ +extern int __collate_load_error; +extern int __collate_substitute_nontrivial; +#define __collate_char_pri_table (*__collate_char_pri_table_ptr) +extern struct __collate_st_char_pri __collate_char_pri_table[UCHAR_MAX + 1]; +extern struct __collate_st_chain_pri *__collate_chain_pri_table; +extern __int32_t *__collate_chain_equiv_table; +extern struct __collate_st_info __collate_info; +#endif /* !__LIBC__ */ + +__BEGIN_DECLS +#ifdef __LIBC__ +__darwin_wchar_t *__collate_mbstowcs(const char *, locale_t); +__darwin_wchar_t *__collate_wcsdup(const __darwin_wchar_t *); +__darwin_wchar_t *__collate_substitute(const __darwin_wchar_t *, int, locale_t); +int __collate_load_tables(const char *, locale_t); +void __collate_lookup_l(const __darwin_wchar_t *, int *, int *, int *, locale_t); +void __collate_lookup_which(const __darwin_wchar_t *, int *, int *, int, locale_t); +void __collate_xfrm(const __darwin_wchar_t *, __darwin_wchar_t **, locale_t); +int __collate_range_cmp(__darwin_wchar_t, __darwin_wchar_t, locale_t); +size_t __collate_collating_symbol(__darwin_wchar_t *, size_t, const char *, size_t, __darwin_mbstate_t *, locale_t); +int __collate_equiv_class(const char *, size_t, __darwin_mbstate_t *, locale_t); +size_t __collate_equiv_match(int, __darwin_wchar_t *, size_t, __darwin_wchar_t, const char *, size_t, __darwin_mbstate_t *, size_t *, locale_t); +#else /* !__LIBC__ */ +void __collate_lookup(const unsigned char *, int *, int *, int *); +#endif /* __LIBC__ */ +#ifdef COLLATE_DEBUG +void __collate_print_tables(void); +#endif +__END_DECLS + +#endif /* !_COLLATE_H_ */ diff --git a/text_cmds/tr/setlocale.h b/text_cmds/tr/setlocale.h new file mode 100644 index 0000000..b21f6f7 --- /dev/null +++ b/text_cmds/tr/setlocale.h @@ -0,0 +1,42 @@ +/*- + * Copyright (C) 1997 by Andrey A. Chernov, Moscow, Russia. + * All rights reserved. + * + * 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 AUTHOR ``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 REGENTS 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. + * + * $FreeBSD: src/lib/libc/locale/setlocale.h,v 1.6 2003/07/06 02:03:37 ache Exp $ + */ + +#ifndef _SETLOCALE_H_ +#define _SETLOCALE_H_ + +#include + +#define ENCODING_LEN 31 +#define CATEGORY_LEN 11 + +extern char *_PathLocale; + +int __detect_path_locale(void); +int __wrap_setrunelocale(const char *, locale_t); + +#endif /* !_SETLOCALE_H_ */ diff --git a/text_cmds/tr/str.c b/text_cmds/tr/str.c index e7e6757..6971e68 100644 --- a/text_cmds/tr/str.c +++ b/text_cmds/tr/str.c @@ -66,8 +66,12 @@ static void genseq(STR *); * Using libc internal function __collate_lookup_l for character * equivalence */ +#if __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__ < 140000 +#include "collate-fbsd.c" +#else void __collate_lookup_l(const __darwin_wchar_t *, int *, int *, int *, locale_t); +#endif /* * Cache for primary collation weight of each single byte character * used in static void genequiv(s) diff --git a/text_cmds/tr/xlocale_private.h b/text_cmds/tr/xlocale_private.h new file mode 100644 index 0000000..8cb81f9 --- /dev/null +++ b/text_cmds/tr/xlocale_private.h @@ -0,0 +1,97 @@ +/* + * Copyright (c) 2005 Apple Computer, Inc. All rights reserved. + * + * @APPLE_LICENSE_HEADER_START@ + * + * This file contains Original Code and/or Modifications of Original Code + * as defined in and that are subject to the Apple Public Source License + * Version 2.0 (the 'License'). You may not use this file except in + * compliance with the License. Please obtain a copy of the License at + * http://www.opensource.apple.com/apsl/ and read it before using this + * file. + * + * The Original Code and all software distributed under the License are + * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER + * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, + * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. + * Please see the License for the specific language governing rights and + * limitations under the License. + * + * @APPLE_LICENSE_HEADER_END@ + */ + +#ifndef _XLOCALE_PRIVATE_H_ +#define _XLOCALE_PRIVATE_H_ + +#include +#define __DARWIN_XLOCALE_PRIVATE +#include +#undef __DARWIN_XLOCALE_PRIVATE +#include +#include +#include +#include +#include +#include "setlocale.h" +#include "collate.h" + +typedef void (*__free_extra_t)(void *); + +#define __STRUCT_COMMON \ + int32_t __refcount; \ + __free_extra_t __free_extra; + +struct __xlocale_st_collate { + __STRUCT_COMMON + char __encoding[ENCODING_LEN + 1]; + struct __collate_st_info __info; + struct __collate_st_subst *__substitute_table[COLL_WEIGHTS_MAX]; + struct __collate_st_chain_pri *__chain_pri_table; + struct __collate_st_large_char_pri *__large_char_pri_table; + struct __collate_st_char_pri __char_pri_table[UCHAR_MAX + 1]; +}; +struct _xlocale { +/* The item(s) before __magic are not copied when duplicating locale_t's */ + __STRUCT_COMMON /* only used for locale_t's in __lc_numeric_loc */ + /* 10 independent mbstate_t buffers! */ + __darwin_mbstate_t __mbs_mblen; + __darwin_mbstate_t __mbs_mbrlen; + __darwin_mbstate_t __mbs_mbrtowc; + __darwin_mbstate_t __mbs_mbsnrtowcs; + __darwin_mbstate_t __mbs_mbsrtowcs; + __darwin_mbstate_t __mbs_mbtowc; + __darwin_mbstate_t __mbs_wcrtomb; + __darwin_mbstate_t __mbs_wcsnrtombs; + __darwin_mbstate_t __mbs_wcsrtombs; + __darwin_mbstate_t __mbs_wctomb; +/* magic (Here up to the end is copied when duplicating locale_t's) */ + int64_t __magic; +/* flags */ + unsigned char __collate_load_error; + unsigned char __collate_substitute_nontrivial; + unsigned char _messages_using_locale; + unsigned char _monetary_using_locale; + unsigned char _numeric_using_locale; + unsigned char _time_using_locale; + unsigned char __mlocale_changed; + unsigned char __nlocale_changed; + unsigned char __numeric_fp_cvt; +/* collate */ + struct __xlocale_st_collate *__lc_collate; +/* ctype */ + struct __xlocale_st_runelocale *__lc_ctype; +/* messages */ + struct __xlocale_st_messages *__lc_messages; +/* monetary */ + struct __xlocale_st_monetary *__lc_monetary; +/* numeric */ + struct __xlocale_st_numeric *__lc_numeric; + struct _xlocale *__lc_numeric_loc; +/* time */ + struct __xlocale_st_time *__lc_time; +/* localeconv */ + struct __xlocale_st_localeconv *__lc_localeconv; +}; + +#endif /* _XLOCALE_PRIVATE_H_ */ -- cgit v1.2.3-56-ge451