keylogin(1): Fix a warning and raise WARNS to 6.
[dragonfly.git] / lib / libc / locale / runeglue.c
1 /*      $NetBSD: src/lib/libc/locale/runeglue.c,v 1.10 2003/03/10 21:18:49 tshiozak Exp $       */
2 /*      $DragonFly: src/lib/libc/locale/runeglue.c,v 1.3 2005/09/17 14:39:44 joerg Exp $ */
3
4 /*-
5  * Copyright (c)1999 Citrus Project,
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *      Id: runeglue.c,v 1.7 2000/12/22 22:52:29 itojun Exp
30  */
31
32 /*
33  * Glue code to hide "rune" facility from user programs.
34  * This is important to keep backward/future compatibility.
35  */
36
37 #define _CTYPE_PRIVATE
38
39 #include <sys/types.h>
40 #include <assert.h>
41 #include <ctype.h>
42 #include <limits.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <wchar.h>
47 #include "rune.h"
48 #include "rune_local.h"
49
50 #if EOF != -1
51 #error "EOF != -1"
52 #endif
53 #if _CACHED_RUNES != 256
54 #error "_CACHED_RUNES != 256"
55 #endif
56
57 int
58 __runetable_to_netbsd_ctype(const char *locale)
59 {
60         int i;
61         uint16_t *new_ctype;
62         int16_t *new_toupper, *new_tolower;
63
64         _DIAGASSERT(locale != NULL);
65
66         /* set to C locale, to ease failure case handling */
67         if (__libc_ctype_ != __libc_C_ctype_) {
68                 /* LINTED const castaway */
69                 free(__DECONST(void *, __libc_ctype_));
70                 __libc_ctype_ = __libc_C_ctype_;
71         }
72         if (__libc_toupper_tab_ != __libc_C_toupper_) {
73                 /* LINTED const castaway */
74                 free(__DECONST(void *, __libc_toupper_tab_));
75                 __libc_toupper_tab_ = __libc_C_toupper_;
76         }
77         if (__libc_tolower_tab_ != __libc_C_tolower_) {
78                 /* LINTED const castaway */
79                 free(__DECONST(void *, __libc_tolower_tab_));
80                 __libc_tolower_tab_ = __libc_C_tolower_;
81         }
82
83         if (strcmp(locale, "C") == 0 || strcmp(locale, "POSIX") == 0)
84                 return(0);
85
86         new_ctype = malloc(sizeof(*new_ctype) * (1 + _CTYPE_NUM_CHARS));
87         if (new_ctype == NULL)
88                 return(-1);
89         new_toupper = malloc(sizeof(*new_toupper) * (1 + 256));
90         if (new_toupper == NULL) {
91                 free(new_ctype);
92                 return(-1);
93         }
94         new_tolower = malloc(sizeof(*new_tolower) * (1 + 256));
95         if (new_tolower == NULL) {
96                 free(new_ctype);
97                 free(new_toupper);
98                 return(-1);
99         }
100
101         memset(new_ctype, 0, sizeof(*new_ctype) * (1 + _CTYPE_NUM_CHARS));
102         memset(new_toupper, 0, sizeof(*new_toupper) * (1 + 256));
103         memset(new_tolower, 0, sizeof(*new_tolower) * (1 + 256));
104
105         new_ctype[0] = 0;
106         new_toupper[0] = EOF;
107         new_tolower[0] = EOF;
108         for (i = 0; i < _CTYPE_NUM_CHARS; i++) {
109                 new_ctype[i + 1] = 0;
110                 if (_CurrentRuneLocale->rl_runetype[i] & _CTYPE_U)
111                         new_ctype[i + 1] |= _CTYPEMASK_U;
112                 if (_CurrentRuneLocale->rl_runetype[i] & _CTYPE_L)
113                         new_ctype[i + 1] |= _CTYPEMASK_L;
114                 if (_CurrentRuneLocale->rl_runetype[i] & _CTYPE_D)
115                         new_ctype[i + 1] |= _CTYPEMASK_D;
116                 if (_CurrentRuneLocale->rl_runetype[i] & _CTYPE_S)
117                         new_ctype[i + 1] |= _CTYPEMASK_S;
118                 if (_CurrentRuneLocale->rl_runetype[i] & _CTYPE_P)
119                         new_ctype[i + 1] |= _CTYPEMASK_P;
120                 if (_CurrentRuneLocale->rl_runetype[i] & _CTYPE_C)
121                         new_ctype[i + 1] |= _CTYPEMASK_C;
122                 if (_CurrentRuneLocale->rl_runetype[i] & _CTYPE_X)
123                         new_ctype[i + 1] |= _CTYPEMASK_X;
124                 if (_CurrentRuneLocale->rl_runetype[i] & _CTYPE_B)
125                         new_ctype[i + 1] |= _CTYPEMASK_B;
126                 if (_CurrentRuneLocale->rl_runetype[i] & _CTYPE_A)
127                         new_ctype[i + 1] |= _CTYPEMASK_A;
128                 if (_CurrentRuneLocale->rl_runetype[i] & _CTYPE_G)
129                         new_ctype[i + 1] |= _CTYPEMASK_G;
130                 if (_CurrentRuneLocale->rl_runetype[i] & _CTYPE_R)
131                         new_ctype[i + 1] |= _CTYPEMASK_R;
132
133                 new_toupper[i + 1] = (int16_t)_CurrentRuneLocale->rl_mapupper[i];
134                 new_tolower[i + 1] = (int16_t)_CurrentRuneLocale->rl_maplower[i];
135         }
136
137         __libc_ctype_ = new_ctype;
138         __libc_toupper_tab_ = new_toupper;
139         __libc_tolower_tab_ = new_tolower;
140
141         return(0);
142 }