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