1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1988 AT&T */
/* All Rights Reserved */
/* __gtxt(): Common part to gettxt() and pfmt() */
#pragma weak _setcat = setcat
#include "lint.h"
#include "libc.h"
#include <mtlib.h>
#include <sys/types.h>
#include <string.h>
#include <locale.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <synch.h>
#include <pfmt.h>
#include <thread.h>
#include <unistd.h>
#include <errno.h>
#include <limits.h>
#include "../i18n/_locale.h"
#include "../i18n/_loc_path.h"
#define MESSAGES "/LC_MESSAGES/"
static const char *def_locale = "C";
static const char *not_found = "Message not found!!\n";
static struct db_info *db_info;
static int db_count, maxdb;
struct db_info {
char db_name[DB_NAME_LEN]; /* Name of the message file */
uintptr_t addr; /* Virtual memory address */
size_t length;
char *saved_locale;
char flag;
};
#define DB_EXIST 1 /* The catalogue exists */
#define DB_OPEN 2 /* Already tried to open */
/* Minimum number of open catalogues */
#define MINDB 3
char cur_cat[DB_NAME_LEN];
rwlock_t _rw_cur_cat = DEFAULTRWLOCK;
/*
* setcat(cat): Specify the default catalogue.
* Return a pointer to the local copy of the default catalogue
*/
const char *
setcat(const char *cat)
{
lrw_wrlock(&_rw_cur_cat);
if (cat) {
if (((strchr(cat, '/') != NULL)) ||
((strchr(cat, ':') != NULL))) {
cur_cat[0] = '\0';
goto out;
}
(void) strncpy(cur_cat, cat, sizeof (cur_cat) - 1);
cur_cat[sizeof (cur_cat) - 1] = '\0';
}
out:
lrw_unlock(&_rw_cur_cat);
return (cur_cat[0] ? cur_cat : NULL);
}
/*
* load a message catalog which specified with current locale,
* and catalog name.
*/
static struct db_info *
load_db(const char *curloc, const char *catname, int *err)
{
char pathname[PATH_MAX];
struct stat64 sb;
caddr_t addr;
struct db_info *db;
int fd;
int i;
*err = 0;
/* First time called, allocate space */
if (!db_info) {
if ((db_info =
libc_malloc(MINDB * sizeof (struct db_info))) == NULL) {
*err = 1;
return (NULL);
}
maxdb = MINDB;
}
for (i = 0; i < db_count; i++) {
if (db_info[i].flag == 0)
break;
}
/* New catalogue */
if (i == db_count) {
if (db_count == maxdb) {
if ((db = libc_realloc(db_info,
++maxdb * sizeof (struct db_info))) == NULL) {
*err = 1;
return (NULL);
}
db_info = db;
}
db_count++;
}
db = &db_info[i];
db->flag = 0;
(void) strcpy(db->db_name, catname);
db->saved_locale = libc_strdup(curloc);
if (db->saved_locale == NULL) {
*err = 1;
return (NULL);
}
db->flag = DB_OPEN;
if (snprintf(pathname, sizeof (pathname),
_DFLT_LOC_PATH "%s" MESSAGES "%s",
db->saved_locale, db->db_name) >= sizeof (pathname)) {
/*
* We won't set err here, because an invalid locale is not
* the fatal condition, but we can fall back to "C"
* locale.
*/
return (NULL);
}
if ((fd = open(pathname, O_RDONLY)) != -1 &&
fstat64(fd, &sb) != -1 &&
(addr = mmap(0, (size_t)sb.st_size, PROT_READ, MAP_SHARED,
fd, 0)) != MAP_FAILED) {
db->flag |= DB_EXIST;
db->addr = (uintptr_t)addr;
db->length = (size_t)sb.st_size;
}
if (fd != -1)
(void) close(fd);
return (db);
}
/*
* unmap the message catalog, and release the db_info slot.
*/
static void
unload_db(struct db_info *db)
{
if ((db->flag & (DB_OPEN|DB_EXIST)) ==
(DB_OPEN|DB_EXIST)) {
(void) munmap((caddr_t)db->addr, db->length);
}
db->flag = 0;
if (db->saved_locale)
libc_free(db->saved_locale);
db->saved_locale = NULL;
}
/*
* go through the db_info, and find out a db_info slot regarding
* the given current locale and catalog name.
* If db is not NULL, then search will start from top of the array,
* otherwise it will start from the next of given db.
* If curloc is set to NULL, then return a cache without regards of
* locale.
*/
static struct db_info *
lookup_cache(struct db_info *db, const char *curloc, const char *catname)
{
if (db_info == NULL)
return (NULL);
if (db == NULL)
db = db_info;
else
db++;
for (; db < &db_info[db_count]; db++) {
if (db->flag == 0)
continue;
if (strcmp(db->db_name, catname) == 0) {
if (curloc == NULL ||
(db->saved_locale != NULL &&
strcmp(db->saved_locale, curloc) == 0)) {
return (db);
}
}
}
return (NULL);
}
static int
valid_msg(struct db_info *db, int id)
{
if (db == NULL || (db->flag & DB_EXIST) == 0)
return (0);
/* catalog has been loaded */
if (id != 0 && id <= *(int *)(db->addr))
return (1);
/* not a valid id */
return (0);
}
static char *
msg(struct db_info *db, int id)
{
return ((char *)(db->addr + *(int *)(db->addr +
id * sizeof (int))));
}
/*
* __gtxt(catname, id, dflt): Return a pointer to a message.
* catname is the name of the catalog. If null, the default catalog is
* used.
* id is the numeric id of the message in the catalogue
* dflt is the default message.
*
* Information about non-existent catalogues is kept in db_info, in
* such a way that subsequent calls with the same catalogue do not
* try to open the catalogue again.
*/
const char *
__gtxt(const char *catname, int id, const char *dflt)
{
char *curloc;
struct db_info *db;
int err;
locale_t loc;
/* Check for invalid message id */
if (id < 0)
return (not_found);
if (id == 0)
return ((dflt && *dflt) ? dflt : not_found);
/*
* If catalogue is unspecified, use default catalogue.
* No catalogue at all is an error
*/
if (!catname || !*catname) {
lrw_rdlock(&_rw_cur_cat);
if (cur_cat == NULL || !*cur_cat) {
lrw_unlock(&_rw_cur_cat);
return (not_found);
}
catname = cur_cat;
lrw_unlock(&_rw_cur_cat);
}
loc = uselocale(NULL);
curloc = current_locale(loc, LC_MESSAGES);
/* First look up the cache */
db = lookup_cache(NULL, curloc, catname);
if (db != NULL) {
/*
* The catalog has been loaded, and if id seems valid,
* then just return.
*/
if (valid_msg(db, id))
return (msg(db, id));
/*
* seems given id is out of bound or does not exist. In this
* case, we need to look up a message for the "C" locale as
* documented in the man page.
*/
db = lookup_cache(NULL, def_locale, catname);
if (db == NULL) {
/*
* Even the message catalog for the "C" has not been
* loaded.
*/
db = load_db(def_locale, catname, &err);
if (err)
return (not_found);
}
if (valid_msg(db, id))
return (msg(db, id));
/* no message found */
return ((dflt && *dflt) ? dflt : not_found);
}
/*
* The catalog has not been loaded or even has not
* attempted to be loaded, invalidate all caches related to
* the catname for possibly different locale.
*/
db = NULL;
while ((db = lookup_cache(db, NULL, catname)) != NULL)
unload_db(db);
/*
* load a message catalog for the requested locale.
*/
db = load_db(curloc, catname, &err);
if (err)
return (not_found);
if (valid_msg(db, id))
return (msg(db, id));
/*
* If the requested catalog is either not exist or message
* id is invalid, then try to load from "C" locale.
*/
db = load_db(def_locale, catname, &err);
if (err)
return (not_found);
if (valid_msg(db, id))
return (msg(db, id));
/* no message found */
return ((dflt && *dflt) ? dflt : not_found);
}
|