xref: /netbsd-src/lib/libc/citrus/citrus_lc_ctype.c (revision 5236f0d55813e6abc3004ddddbc8ab690640f4a0)
1 /* $NetBSD: citrus_lc_ctype.c,v 1.16 2018/01/05 17:52:06 kamil 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.16 2018/01/05 17:52:06 kamil Exp $");
32 #endif /* LIBC_SCCS and not lint */
33 
34 #include "namespace.h"
35 #include "reentrant.h"
36 #include <sys/types.h>
37 #include <sys/ctype_bits.h>
38 #include <sys/queue.h>
39 #include <assert.h>
40 #include <errno.h>
41 #include <langinfo.h>
42 #include <limits.h>
43 #define __SETLOCALE_SOURCE__
44 #include <locale.h>
45 #include <stddef.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <wchar.h>
50 
51 #include "setlocale_local.h"
52 
53 #include "citrus_namespace.h"
54 #include "citrus_bcs.h"
55 #include "citrus_region.h"
56 #include "citrus_lookup.h"
57 #include "citrus_aliasname_local.h"
58 #include "citrus_module.h"
59 #include "citrus_ctype.h"
60 #include "citrus_mmap.h"
61 
62 #include "runetype_local.h"
63 #include "multibyte.h"
64 
65 /*
66  * macro required by all template headers
67  */
68 #define _PREFIX(name)		__CONCAT(_citrus_LC_CTYPE_, name)
69 
70 /*
71  * macro required by nb_lc_template(_decl).h
72  */
73 #define _CATEGORY_TYPE          _RuneLocale
74 
75 #include "nb_lc_template_decl.h"
76 
77 static int
_citrus_LC_CTYPE_create_impl(const char * __restrict root,const char * __restrict name,_RuneLocale ** __restrict pdata)78 _citrus_LC_CTYPE_create_impl(const char * __restrict root,
79     const char * __restrict name, _RuneLocale ** __restrict pdata)
80 {
81 	char path[PATH_MAX + 1];
82 	int ret;
83 	struct _region r;
84 
85 	_DIAGASSERT(root != NULL);
86 	_DIAGASSERT(name != NULL);
87 	_DIAGASSERT(pdata != NULL);
88 
89 	snprintf(path, sizeof(path),
90 	    "%s/%s/LC_CTYPE", root, name);
91 	ret = _citrus_map_file(&r, path);
92 	if (!ret) {
93 		ret = _rune_load((const char *)r.r_head, r.r_size, pdata);
94 		_citrus_unmap_file(&r);
95 	}
96 	return ret;
97 }
98 
99 static __inline void
_PREFIX(update_global)100 _PREFIX(update_global)(_RuneLocale *data)
101 {
102 	_DIAGASSERT(data != NULL);
103 
104 	__mb_cur_max = _citrus_ctype_get_mb_cur_max(data->rl_citrus_ctype);
105 	_ctype_tab_ = data->rl_ctype_tab;
106 	_tolower_tab_ = data->rl_tolower_tab;
107 	_toupper_tab_ = data->rl_toupper_tab;
108 
109 #ifdef __BUILD_LEGACY
110 	_ctype_ = data->rl_compat_bsdctype;
111 #endif
112 }
113 
114 /*
115  * macro required by nb_lc_template.h
116  */
117 #define _CATEGORY_ID		LC_CTYPE
118 #define _CATEGORY_NAME		"LC_CTYPE"
119 
120 #include "nb_lc_template.h"
121