1*0d5acd74SJohn Marino /*
2*0d5acd74SJohn Marino * Copyright (c) 2001 Alexey Zelkin <phantom@FreeBSD.org>
3*0d5acd74SJohn Marino * All rights reserved.
4*0d5acd74SJohn Marino *
5*0d5acd74SJohn Marino * Copyright (c) 2011 The FreeBSD Foundation
6*0d5acd74SJohn Marino * All rights reserved.
7*0d5acd74SJohn Marino * Portions of this software were developed by David Chisnall
8*0d5acd74SJohn Marino * under sponsorship from the FreeBSD Foundation.
9*0d5acd74SJohn Marino *
10*0d5acd74SJohn Marino * Redistribution and use in source and binary forms, with or without
11*0d5acd74SJohn Marino * modification, are permitted provided that the following conditions
12*0d5acd74SJohn Marino * are met:
13*0d5acd74SJohn Marino * 1. Redistributions of source code must retain the above copyright
14*0d5acd74SJohn Marino * notice, this list of conditions and the following disclaimer.
15*0d5acd74SJohn Marino * 2. Redistributions in binary form must reproduce the above copyright
16*0d5acd74SJohn Marino * notice, this list of conditions and the following disclaimer in the
17*0d5acd74SJohn Marino * documentation and/or other materials provided with the distribution.
18*0d5acd74SJohn Marino *
19*0d5acd74SJohn Marino * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20*0d5acd74SJohn Marino * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21*0d5acd74SJohn Marino * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22*0d5acd74SJohn Marino * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23*0d5acd74SJohn Marino * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24*0d5acd74SJohn Marino * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25*0d5acd74SJohn Marino * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26*0d5acd74SJohn Marino * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27*0d5acd74SJohn Marino * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28*0d5acd74SJohn Marino * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29*0d5acd74SJohn Marino * SUCH DAMAGE.
30*0d5acd74SJohn Marino *
31*0d5acd74SJohn Marino * $FreeBSD: head/lib/libc/locale/lmessages.c 227753 2011-11-20 14:45:42Z theraven $
32*0d5acd74SJohn Marino */
33*0d5acd74SJohn Marino
34*0d5acd74SJohn Marino
35*0d5acd74SJohn Marino #include <stddef.h>
36*0d5acd74SJohn Marino
37*0d5acd74SJohn Marino #include "ldpart.h"
38*0d5acd74SJohn Marino #include "lmessages.h"
39*0d5acd74SJohn Marino
40*0d5acd74SJohn Marino #define LCMESSAGES_SIZE_FULL (sizeof(struct lc_messages_T) / sizeof(char *))
41*0d5acd74SJohn Marino #define LCMESSAGES_SIZE_MIN \
42*0d5acd74SJohn Marino (offsetof(struct lc_messages_T, yesstr) / sizeof(char *))
43*0d5acd74SJohn Marino
44*0d5acd74SJohn Marino struct xlocale_messages {
45*0d5acd74SJohn Marino struct xlocale_component header;
46*0d5acd74SJohn Marino char *buffer;
47*0d5acd74SJohn Marino struct lc_messages_T locale;
48*0d5acd74SJohn Marino };
49*0d5acd74SJohn Marino
50*0d5acd74SJohn Marino struct xlocale_messages __xlocale_global_messages;
51*0d5acd74SJohn Marino
52*0d5acd74SJohn Marino static char empty[] = "";
53*0d5acd74SJohn Marino
54*0d5acd74SJohn Marino static const struct lc_messages_T _C_messages_locale = {
55*0d5acd74SJohn Marino "^[yY]" , /* yesexpr */
56*0d5acd74SJohn Marino "^[nN]" , /* noexpr */
57*0d5acd74SJohn Marino "yes" , /* yesstr */
58*0d5acd74SJohn Marino "no" /* nostr */
59*0d5acd74SJohn Marino };
60*0d5acd74SJohn Marino
destruct_messages(void * v)61*0d5acd74SJohn Marino static void destruct_messages(void *v)
62*0d5acd74SJohn Marino {
63*0d5acd74SJohn Marino struct xlocale_messages *l = v;
64*0d5acd74SJohn Marino if (l->buffer)
65*0d5acd74SJohn Marino free(l->buffer);
66*0d5acd74SJohn Marino free(l);
67*0d5acd74SJohn Marino }
68*0d5acd74SJohn Marino
69*0d5acd74SJohn Marino static int
messages_load_locale(struct xlocale_messages * loc,int * using_locale,const char * name)70*0d5acd74SJohn Marino messages_load_locale(struct xlocale_messages *loc, int *using_locale, const char *name)
71*0d5acd74SJohn Marino {
72*0d5acd74SJohn Marino int ret;
73*0d5acd74SJohn Marino struct lc_messages_T *l = &loc->locale;
74*0d5acd74SJohn Marino
75*0d5acd74SJohn Marino ret = __part_load_locale(name, using_locale,
76*0d5acd74SJohn Marino &loc->buffer, "LC_MESSAGES",
77*0d5acd74SJohn Marino LCMESSAGES_SIZE_FULL, LCMESSAGES_SIZE_MIN,
78*0d5acd74SJohn Marino (const char **)l);
79*0d5acd74SJohn Marino if (ret == _LDP_LOADED) {
80*0d5acd74SJohn Marino if (l->yesstr == NULL)
81*0d5acd74SJohn Marino l->yesstr = empty;
82*0d5acd74SJohn Marino if (l->nostr == NULL)
83*0d5acd74SJohn Marino l->nostr = empty;
84*0d5acd74SJohn Marino }
85*0d5acd74SJohn Marino return (ret);
86*0d5acd74SJohn Marino }
87*0d5acd74SJohn Marino int
__messages_load_locale(const char * name)88*0d5acd74SJohn Marino __messages_load_locale(const char *name)
89*0d5acd74SJohn Marino {
90*0d5acd74SJohn Marino return messages_load_locale(&__xlocale_global_messages,
91*0d5acd74SJohn Marino &__xlocale_global_locale.using_messages_locale, name);
92*0d5acd74SJohn Marino }
93*0d5acd74SJohn Marino void *
__messages_load(const char * name,locale_t l)94*0d5acd74SJohn Marino __messages_load(const char *name, locale_t l)
95*0d5acd74SJohn Marino {
96*0d5acd74SJohn Marino struct xlocale_messages *new = calloc(sizeof(struct xlocale_messages), 1);
97*0d5acd74SJohn Marino new->header.header.destructor = destruct_messages;
98*0d5acd74SJohn Marino if (messages_load_locale(new, &l->using_messages_locale, name) == _LDP_ERROR) {
99*0d5acd74SJohn Marino xlocale_release(new);
100*0d5acd74SJohn Marino return NULL;
101*0d5acd74SJohn Marino }
102*0d5acd74SJohn Marino return new;
103*0d5acd74SJohn Marino }
104*0d5acd74SJohn Marino
105*0d5acd74SJohn Marino struct lc_messages_T *
__get_current_messages_locale(locale_t loc)106*0d5acd74SJohn Marino __get_current_messages_locale(locale_t loc)
107*0d5acd74SJohn Marino {
108*0d5acd74SJohn Marino return (loc->using_messages_locale
109*0d5acd74SJohn Marino ? &((struct xlocale_messages *)loc->components[XLC_MESSAGES])->locale
110*0d5acd74SJohn Marino : (struct lc_messages_T *)&_C_messages_locale);
111*0d5acd74SJohn Marino }
112*0d5acd74SJohn Marino
113*0d5acd74SJohn Marino #ifdef LOCALE_DEBUG
114*0d5acd74SJohn Marino void
msgdebug()115*0d5acd74SJohn Marino msgdebug() {
116*0d5acd74SJohn Marino printf( "yesexpr = %s\n"
117*0d5acd74SJohn Marino "noexpr = %s\n"
118*0d5acd74SJohn Marino "yesstr = %s\n"
119*0d5acd74SJohn Marino "nostr = %s\n",
120*0d5acd74SJohn Marino _messages_locale.yesexpr,
121*0d5acd74SJohn Marino _messages_locale.noexpr,
122*0d5acd74SJohn Marino _messages_locale.yesstr,
123*0d5acd74SJohn Marino _messages_locale.nostr
124*0d5acd74SJohn Marino );
125*0d5acd74SJohn Marino }
126*0d5acd74SJohn Marino #endif /* LOCALE_DEBUG */
127