1/* $NetBSD: citrus_lc_messages.c,v 1.3 2010/05/22 08:13:18 tnozaki 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_messages.c,v 1.3 2010/05/22 08:13:18 tnozaki 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/localedef.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
50#include "setlocale_local.h"
51
52#include "citrus_namespace.h"
53#include "citrus_types.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_mmap.h"
60#include "citrus_hash.h"
61#include "citrus_db.h"
62#include "citrus_db_hash.h"
63#include "citrus_memstream.h"
64#include "runetype_local.h"
65
66/*
67 * macro required by all template headers
68 */
69#define _PREFIX(name)		__CONCAT(_citrus_LC_MESSAGES_, name)
70
71#include "nb_lc_messages_misc.h"
72#include "citrus_lc_template_decl.h"
73
74static __inline void
75_citrus_LC_MESSAGES_uninit(_MessagesLocale *data)
76{
77	free(__UNCONST(data->yesexpr));
78	free(__UNCONST(data->noexpr));
79	free(__UNCONST(data->yesstr));
80	free(__UNCONST(data->nostr));
81}
82
83#include "citrus_lc_messages.h"
84
85struct _citrus_LC_MESSAGES_key {
86	const char *name;
87	size_t offset;
88};
89
90#define OFFSET(field) (offsetof(_MessagesLocale, field))
91static const struct _citrus_LC_MESSAGES_key keys[] = {
92  { _CITRUS_LC_MESSAGES_SYM_YESEXPR, OFFSET(yesexpr) },
93  { _CITRUS_LC_MESSAGES_SYM_NOEXPR,  OFFSET(noexpr ) },
94  { _CITRUS_LC_MESSAGES_SYM_YESSTR,  OFFSET(yesstr ) },
95  { _CITRUS_LC_MESSAGES_SYM_NOSTR,   OFFSET(nostr  ) },
96  { NULL, 0 }
97};
98
99static __inline int
100_citrus_LC_MESSAGES_init_normal(_MessagesLocale * __restrict data,
101    struct _citrus_db * __restrict db)
102{
103	const struct _citrus_LC_MESSAGES_key *key;
104	char **p;
105	const char *s;
106
107	_DIAGASSERT(data != NULL);
108	_DIAGASSERT(db != NULL);
109
110	memset(data, 0, sizeof(*data));
111	for (key = &keys[0]; key->name != NULL; ++key) {
112		if (_db_lookupstr_by_s(db, key->name, &s, NULL))
113			goto fatal;
114		p = (char **)(void *)
115		    (((char *)(void *)data) + key->offset);
116		*p = strdup(s);
117		if (*p == NULL)
118			goto fatal;
119	}
120	return 0;
121
122fatal:
123	_citrus_LC_MESSAGES_uninit(data);
124	return EFTYPE;
125}
126
127static __inline int
128_citrus_LC_MESSAGES_init_fallback(_MessagesLocale * __restrict data,
129    struct _memstream * __restrict ms)
130{
131	const struct _citrus_LC_MESSAGES_key *key;
132	char **p;
133	const char *s;
134	size_t n;
135
136	_DIAGASSERT(data != NULL);
137	_DIAGASSERT(ms != NULL);
138
139	memset(data, 0, sizeof(*data));
140	for (key = &keys[0]; key->name != NULL; ++key) {
141		if ((s = _memstream_getln(ms, &n)) == NULL)
142			goto fatal;
143		p = (char **)(void *)
144		    (((char *)(void *)data) + key->offset);
145		*p = strndup(s, n - 1);
146		if (*p == NULL)
147			goto fatal;
148	}
149	return 0;
150
151fatal:
152	_citrus_LC_MESSAGES_uninit(data);
153	return EFTYPE;
154}
155
156/*
157 * macro required by citrus_lc_template.h
158 */
159#define _CATEGORY_DB		"LC_MESSAGES/SYS_LC_MESSAGES"
160#define _CATEGORY_MAGIC		_CITRUS_LC_MESSAGES_MAGIC_1
161
162#include "citrus_lc_template.h"
163_LOCALE_CATEGORY_ENTRY(_citrus_LC_MESSAGES_);
164