gtxt.c revision e53d4db11f7999d0e42405e94c04e1c2e54be0b5
/*
* 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
* 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 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/* Copyright (c) 1988 AT&T */
/* All Rights Reserved */
/* __gtxt(): Common part to gettxt() and pfmt() */
#include "synonyms.h"
#include "libc.h"
#include <mtlib.h>
#include <string.h>
#include <locale.h>
#include <fcntl.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";
struct db_info {
char *saved_locale;
char flag;
};
/* Minimum number of open catalogues */
#define MINDB 3
char cur_cat[DB_NAME_LEN];
/*
* setcat(cat): Specify the default catalogue.
* Return a pointer to the local copy of the default catalogue
*/
const char *
{
if (cat) {
cur_cat[0] = '\0';
goto out;
}
}
out:
}
/*
* load a message catalog which specified with current locale,
* and catalog name.
*/
static struct db_info *
{
int fd;
int i;
*err = 0;
/* First time called, allocate space */
if (!db_info) {
if ((db_info =
*err = 1;
return (NULL);
}
}
for (i = 0; i < db_count; i++) {
break;
}
/* New catalogue */
if (i == db_count) {
*err = 1;
return (NULL);
}
}
db_count++;
}
*err = 1;
return (NULL);
}
/*
* 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);
}
fd, 0)) != MAP_FAILED) {
}
if (fd != -1)
return (db);
}
/*
* unmap the message catalog, and release the db_info slot.
*/
static void
{
}
if (db->saved_locale)
}
/*
* 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 *
{
return (NULL);
else
db++;
continue;
return (db);
}
}
}
return (NULL);
}
static int
{
return (0);
/* catalog has been loaded */
return (1);
/* not a valid id */
return (0);
}
static char *
{
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 *
{
char *curloc;
int err;
/* Check for invalid message id */
if (id < 0)
return (not_found);
if (id == 0)
/*
* If catalogue is unspecified, use default catalogue.
* No catalogue at all is an error
*/
return (not_found);
}
}
/* First look up the cache */
/*
* The catalog has been loaded, and if id seems valid,
* then just return.
*/
/*
* 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.
*/
/*
* Even the message catalog for the "C" has not been
* loaded.
*/
if (err)
return (not_found);
}
/* no message 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.
*/
/*
* load a message catalog for the requested locale.
*/
if (err)
return (not_found);
/*
* If the requested catalog is either not exist or message
* id is invalid, then try to load from "C" locale.
*/
if (err)
return (not_found);
/* no message found */
}