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