1 /* $NetBSD: citrus_lc_ctype.c,v 1.9 2010/06/19 13:26:51 tnozaki Exp $ */ 2 3 /*- 4 * Copyright (c)2008 Citrus Project, 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 #if defined(LIBC_SCCS) && !defined(lint) 31 __RCSID("$NetBSD: citrus_lc_ctype.c,v 1.9 2010/06/19 13:26:51 tnozaki Exp $"); 32 #endif /* LIBC_SCCS and not lint */ 33 34 #include "reentrant.h" 35 #include <sys/types.h> 36 #include <sys/ctype_bits.h> 37 #include <sys/queue.h> 38 #include <assert.h> 39 #include <errno.h> 40 #include <langinfo.h> 41 #include <limits.h> 42 #define __SETLOCALE_SOURCE__ 43 #include <locale.h> 44 #include <stddef.h> 45 #include <stdio.h> 46 #include <stdlib.h> 47 #include <string.h> 48 #include <wchar.h> 49 50 #include "setlocale_local.h" 51 52 #include "citrus_namespace.h" 53 #include "citrus_bcs.h" 54 #include "citrus_region.h" 55 #include "citrus_lookup.h" 56 #include "citrus_aliasname_local.h" 57 #include "citrus_module.h" 58 #include "citrus_ctype.h" 59 #include "citrus_mmap.h" 60 61 #include "runetype_local.h" 62 #include "multibyte.h" 63 64 /* 65 * macro required by all template headers 66 */ 67 #define _PREFIX(name) __CONCAT(_citrus_LC_CTYPE_, name) 68 69 /* 70 * macro required by nb_lc_template(_decl).h 71 */ 72 #define _CATEGORY_TYPE _RuneLocale 73 74 #include "nb_lc_template_decl.h" 75 76 static int 77 _citrus_LC_CTYPE_create_impl(const char * __restrict root, 78 const char * __restrict name, _RuneLocale ** __restrict pdata) 79 { 80 char path[PATH_MAX + 1]; 81 int ret; 82 struct _region r; 83 84 _DIAGASSERT(root != NULL); 85 _DIAGASSERT(name != NULL); 86 _DIAGASSERT(pdata != NULL); 87 88 snprintf(path, sizeof(path), 89 "%s/%s/LC_CTYPE", root, name); 90 ret = _citrus_map_file(&r, path); 91 if (!ret) { 92 ret = _rune_load((const char *)r.r_head, r.r_size, pdata); 93 _citrus_unmap_file(&r); 94 } 95 return ret; 96 } 97 98 static __inline void 99 _PREFIX(build_cache)(struct _locale_cache_t * __restrict cache, 100 _RuneLocale * __restrict data) 101 { 102 _DIAGASSERT(cache != NULL); 103 _DIAGASSERT(cache->items != NULL); 104 _DIAGASSERT(data != NULL); 105 106 cache->ctype_tab = data->rl_ctype_tab; 107 cache->tolower_tab = data->rl_tolower_tab; 108 cache->toupper_tab = data->rl_toupper_tab; 109 cache->mb_cur_max = _citrus_ctype_get_mb_cur_max(data->rl_citrus_ctype); 110 cache->items[(size_t)CODESET] = data->rl_codeset; 111 } 112 113 static __inline void 114 _PREFIX(fixup)(_RuneLocale *data) 115 { 116 _DIAGASSERT(data != NULL); 117 118 __mb_cur_max = _citrus_ctype_get_mb_cur_max(data->rl_citrus_ctype); 119 _ctype_ = data->rl_ctype_tab; 120 _tolower_tab_ = data->rl_tolower_tab; 121 _toupper_tab_ = data->rl_toupper_tab; 122 _CurrentRuneLocale = data; 123 } 124 125 /* 126 * macro required by nb_lc_template.h 127 */ 128 #define _CATEGORY_ID LC_CTYPE 129 #define _CATEGORY_NAME "LC_CTYPE" 130 #define _CATEGORY_DEFAULT _DefaultRuneLocale 131 132 #include "nb_lc_template.h" 133 _LOCALE_CATEGORY_ENTRY(_citrus_LC_CTYPE_); 134