Make not-so-space-eaten locale version:

split modules to bring only neccessary functions,
eliminate sprintf, make reduced startup_locale version.
This commit is contained in:
Andrey A. Chernov 1994-09-24 15:59:33 +00:00
parent cbe192eeb8
commit 2f04ec53cc
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=3050
12 changed files with 525 additions and 290 deletions

View File

@ -4,7 +4,9 @@
.PATH: ${.CURDIR}/${MACHINE}/locale ${.CURDIR}/locale
SRCS+= ansi.c ctype.c euc.c frune.c isctype.c lconv.c localeconv.c \
mbrune.c none.c rune.c setlocale.c table.c utf2.c
mbrune.c none.c rune.c common_setlocale.c \
startup_setlocale.c read_runemagi.c setlocale.c table.c utf2.c \
runetype.c tolower.c toupper.c nomacros.c
MAN3+= locale/ctype.3 locale/isalnum.3 locale/isalpha.3 locale/isascii.3 \
locale/isblank.3 locale/iscntrl.3 locale/isdigit.3 locale/isgraph.3 \

View File

@ -0,0 +1,3 @@
#define PathLocale _PathLocale
extern _RuneLocale *_Read_RuneMagi __P((FILE *));

View File

@ -0,0 +1,64 @@
#include <locale.h>
#include <string.h>
/*
* Category names for getenv()
*/
char *_categories[_LC_LAST] = {
"LC_ALL",
"LC_COLLATE",
"LC_CTYPE",
"LC_MONETARY",
"LC_NUMERIC",
"LC_TIME",
};
/*
* Current locales for each category
*/
char _current_categories[_LC_LAST][32] = {
"C",
"C",
"C",
"C",
"C",
"C",
};
/*
* The locales we are going to try and load
*/
char _new_categories[_LC_LAST][32];
char _current_locale_string[_LC_LAST * 33];
char *
_currentlocale()
{
int i, len;
(void)strcpy(_current_locale_string, _current_categories[1]);
for (i = 2; i < _LC_LAST; ++i)
if (strcmp(_current_categories[1], _current_categories[i])) {
len = strlen(_current_categories[1]) + 1 +
strlen(_current_categories[2]) + 1 +
strlen(_current_categories[3]) + 1 +
strlen(_current_categories[4]) + 1 +
strlen(_current_categories[5]) + 1;
if (len > sizeof(_current_locale_string))
return NULL;
(void) strcpy(_current_locale_string, _current_categories[1]);
(void) strcat(_current_locale_string, "/");
(void) strcat(_current_locale_string, _current_categories[2]);
(void) strcat(_current_locale_string, "/");
(void) strcat(_current_locale_string, _current_categories[3]);
(void) strcat(_current_locale_string, "/");
(void) strcat(_current_locale_string, _current_categories[4]);
(void) strcat(_current_locale_string, "/");
(void) strcat(_current_locale_string, _current_categories[5]);
break;
}
return (_current_locale_string);
}

View File

@ -0,0 +1,14 @@
#define categories _categories
extern char *_categories[_LC_LAST];
#define current_categories _current_categories
extern char _current_categories[_LC_LAST][32];
#define new_categories _new_categories
extern char _new_categories[_LC_LAST][32];
#define current_locale_string _current_locale_string
extern char _current_locale_string[_LC_LAST * 33];
#define currentlocale _currentlocale
extern char *_currentlocale __P((void));

View File

@ -0,0 +1,41 @@
#include <ctype.h>
#include <rune.h>
#if !defined(_USE_CTYPE_INLINE_) && !defined(_USE_CTYPE_MACROS_)
/*
* See comments in <machine/ansi.h>
*/
int
__istype(c, f)
_BSD_RUNE_T_ c;
unsigned long f;
{
return ((((c & _CRMASK) ? ___runetype(c)
: _CurrentRuneLocale->runetype[c]) & f) ? 1 : 0);
}
int
__isctype(_BSD_RUNE_T_ c, unsigned long f)
_BSD_RUNE_T_ c;
unsigned long f;
{
return ((((c & _CRMASK) ? 0
: _DefaultRuneLocale.runetype[c]) & f) ? 1 : 0);
}
_BSD_RUNE_T_
toupper(c)
_BSD_RUNE_T_ c;
{
return ((c & _CRMASK) ?
___toupper(c) : _CurrentRuneLocale->mapupper[c]);
}
_BSD_RUNE_T_
tolower(c)
_BSD_RUNE_T_ c;
{
return ((c & _CRMASK) ?
___tolower(c) : _CurrentRuneLocale->maplower[c]);
}
#endif

View File

@ -0,0 +1,134 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <rune.h>
#include <stdlib.h>
_RuneLocale *
_Read_RuneMagi(fp)
FILE *fp;
{
char *data;
void *np;
void *lastp;
_RuneLocale *rl;
_RuneEntry *rr;
struct stat sb;
int x;
if (fstat(fileno(fp), &sb) < 0)
return(0);
if (sb.st_size < sizeof(_RuneLocale))
return(0);
if ((data = malloc(sb.st_size)) == NULL)
return(0);
rewind(fp); /* Someone might have read the magic number once already */
if (fread(data, sb.st_size, 1, fp) != 1) {
free(data);
return(0);
}
rl = (_RuneLocale *)data;
lastp = data + sb.st_size;
rl->variable = rl + 1;
if (memcmp(rl->magic, _RUNE_MAGIC_1, sizeof(rl->magic))) {
free(data);
return(0);
}
rl->invalid_rune = ntohl(rl->invalid_rune);
rl->variable_len = ntohl(rl->variable_len);
rl->runetype_ext.nranges = ntohl(rl->runetype_ext.nranges);
rl->maplower_ext.nranges = ntohl(rl->maplower_ext.nranges);
rl->mapupper_ext.nranges = ntohl(rl->mapupper_ext.nranges);
for (x = 0; x < _CACHED_RUNES; ++x) {
rl->runetype[x] = ntohl(rl->runetype[x]);
rl->maplower[x] = ntohl(rl->maplower[x]);
rl->mapupper[x] = ntohl(rl->mapupper[x]);
}
rl->runetype_ext.ranges = (_RuneEntry *)rl->variable;
rl->variable = rl->runetype_ext.ranges + rl->runetype_ext.nranges;
if (rl->variable > lastp) {
free(data);
return(0);
}
rl->maplower_ext.ranges = (_RuneEntry *)rl->variable;
rl->variable = rl->maplower_ext.ranges + rl->maplower_ext.nranges;
if (rl->variable > lastp) {
free(data);
return(0);
}
rl->mapupper_ext.ranges = (_RuneEntry *)rl->variable;
rl->variable = rl->mapupper_ext.ranges + rl->mapupper_ext.nranges;
if (rl->variable > lastp) {
free(data);
return(0);
}
for (x = 0; x < rl->runetype_ext.nranges; ++x) {
rr = rl->runetype_ext.ranges;
rr[x].min = ntohl(rr[x].min);
rr[x].max = ntohl(rr[x].max);
if ((rr[x].map = ntohl(rr[x].map)) == 0) {
int len = rr[x].max - rr[x].min + 1;
rr[x].types = rl->variable;
rl->variable = rr[x].types + len;
if (rl->variable > lastp) {
free(data);
return(0);
}
while (len-- > 0)
rr[x].types[len] = ntohl(rr[x].types[len]);
} else
rr[x].types = 0;
}
for (x = 0; x < rl->maplower_ext.nranges; ++x) {
rr = rl->maplower_ext.ranges;
rr[x].min = ntohl(rr[x].min);
rr[x].max = ntohl(rr[x].max);
rr[x].map = ntohl(rr[x].map);
}
for (x = 0; x < rl->mapupper_ext.nranges; ++x) {
rr = rl->mapupper_ext.ranges;
rr[x].min = ntohl(rr[x].min);
rr[x].max = ntohl(rr[x].max);
rr[x].map = ntohl(rr[x].map);
}
if (((char *)rl->variable) + rl->variable_len > (char *)lastp) {
free(data);
return(0);
}
/*
* Go out and zero pointers that should be zero.
*/
if (!rl->variable_len)
rl->variable = 0;
if (!rl->runetype_ext.nranges)
rl->runetype_ext.ranges = 0;
if (!rl->maplower_ext.nranges)
rl->maplower_ext.ranges = 0;
if (!rl->mapupper_ext.nranges)
rl->mapupper_ext.ranges = 0;
return(rl);
}

View File

@ -38,22 +38,19 @@
static char sccsid[] = "@(#)rune.c 8.1 (Berkeley) 6/4/93";
#endif /* LIBC_SCCS and not lint */
#include <sys/types.h>
#include <sys/stat.h>
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <rune.h>
#include <stdio.h>
#include <stdlib.h>
#include "common_rune.h"
char *_PathLocale;
extern int _none_init __P((_RuneLocale *));
extern int _UTF2_init __P((_RuneLocale *));
extern int _EUC_init __P((_RuneLocale *));
static _RuneLocale *_Read_RuneMagi __P((FILE *));
static char *PathLocale = 0;
int
setrunelocale(encoding)
@ -77,7 +74,10 @@ setrunelocale(encoding)
if (!PathLocale && !(PathLocale = getenv("PATH_LOCALE")))
PathLocale = _PATH_LOCALE;
sprintf(name, "%s/%s/LC_CTYPE", PathLocale, encoding);
(void) strcpy(name, PathLocale);
(void) strcat(name, "/");
(void) strcat(name, encoding);
(void) strcat(name, "/LC_CTYPE");
if ((fp = fopen(name, "r")) == NULL)
return(ENOENT);
@ -104,231 +104,3 @@ setinvalidrune(ir)
_INVALID_RUNE = ir;
}
static _RuneLocale *
_Read_RuneMagi(fp)
FILE *fp;
{
char *data;
void *np;
void *lastp;
_RuneLocale *rl;
_RuneEntry *rr;
struct stat sb;
int x;
if (fstat(fileno(fp), &sb) < 0)
return(0);
if (sb.st_size < sizeof(_RuneLocale))
return(0);
if ((data = malloc(sb.st_size)) == NULL)
return(0);
rewind(fp); /* Someone might have read the magic number once already */
if (fread(data, sb.st_size, 1, fp) != 1) {
free(data);
return(0);
}
rl = (_RuneLocale *)data;
lastp = data + sb.st_size;
rl->variable = rl + 1;
if (memcmp(rl->magic, _RUNE_MAGIC_1, sizeof(rl->magic))) {
free(data);
return(0);
}
rl->invalid_rune = ntohl(rl->invalid_rune);
rl->variable_len = ntohl(rl->variable_len);
rl->runetype_ext.nranges = ntohl(rl->runetype_ext.nranges);
rl->maplower_ext.nranges = ntohl(rl->maplower_ext.nranges);
rl->mapupper_ext.nranges = ntohl(rl->mapupper_ext.nranges);
for (x = 0; x < _CACHED_RUNES; ++x) {
rl->runetype[x] = ntohl(rl->runetype[x]);
rl->maplower[x] = ntohl(rl->maplower[x]);
rl->mapupper[x] = ntohl(rl->mapupper[x]);
}
rl->runetype_ext.ranges = (_RuneEntry *)rl->variable;
rl->variable = rl->runetype_ext.ranges + rl->runetype_ext.nranges;
if (rl->variable > lastp) {
free(data);
return(0);
}
rl->maplower_ext.ranges = (_RuneEntry *)rl->variable;
rl->variable = rl->maplower_ext.ranges + rl->maplower_ext.nranges;
if (rl->variable > lastp) {
free(data);
return(0);
}
rl->mapupper_ext.ranges = (_RuneEntry *)rl->variable;
rl->variable = rl->mapupper_ext.ranges + rl->mapupper_ext.nranges;
if (rl->variable > lastp) {
free(data);
return(0);
}
for (x = 0; x < rl->runetype_ext.nranges; ++x) {
rr = rl->runetype_ext.ranges;
rr[x].min = ntohl(rr[x].min);
rr[x].max = ntohl(rr[x].max);
if ((rr[x].map = ntohl(rr[x].map)) == 0) {
int len = rr[x].max - rr[x].min + 1;
rr[x].types = rl->variable;
rl->variable = rr[x].types + len;
if (rl->variable > lastp) {
free(data);
return(0);
}
while (len-- > 0)
rr[x].types[len] = ntohl(rr[x].types[len]);
} else
rr[x].types = 0;
}
for (x = 0; x < rl->maplower_ext.nranges; ++x) {
rr = rl->maplower_ext.ranges;
rr[x].min = ntohl(rr[x].min);
rr[x].max = ntohl(rr[x].max);
rr[x].map = ntohl(rr[x].map);
}
for (x = 0; x < rl->mapupper_ext.nranges; ++x) {
rr = rl->mapupper_ext.ranges;
rr[x].min = ntohl(rr[x].min);
rr[x].max = ntohl(rr[x].max);
rr[x].map = ntohl(rr[x].map);
}
if (((char *)rl->variable) + rl->variable_len > (char *)lastp) {
free(data);
return(0);
}
/*
* Go out and zero pointers that should be zero.
*/
if (!rl->variable_len)
rl->variable = 0;
if (!rl->runetype_ext.nranges)
rl->runetype_ext.ranges = 0;
if (!rl->maplower_ext.nranges)
rl->maplower_ext.ranges = 0;
if (!rl->mapupper_ext.nranges)
rl->mapupper_ext.ranges = 0;
return(rl);
}
unsigned long
___runetype(c)
_BSD_RUNE_T_ c;
{
int x;
_RuneRange *rr = &_CurrentRuneLocale->runetype_ext;
_RuneEntry *re = rr->ranges;
if (c == EOF)
return(0);
for (x = 0; x < rr->nranges; ++x, ++re) {
if (c < re->min)
return(0L);
if (c <= re->max) {
if (re->types)
return(re->types[c - re->min]);
else
return(re->map);
}
}
return(0L);
}
_BSD_RUNE_T_
___toupper(c)
_BSD_RUNE_T_ c;
{
int x;
_RuneRange *rr = &_CurrentRuneLocale->mapupper_ext;
_RuneEntry *re = rr->ranges;
if (c == EOF)
return(EOF);
for (x = 0; x < rr->nranges; ++x, ++re) {
if (c < re->min)
return(c);
if (c <= re->max)
return(re->map + c - re->min);
}
return(c);
}
_BSD_RUNE_T_
___tolower(c)
_BSD_RUNE_T_ c;
{
int x;
_RuneRange *rr = &_CurrentRuneLocale->maplower_ext;
_RuneEntry *re = rr->ranges;
if (c == EOF)
return(EOF);
for (x = 0; x < rr->nranges; ++x, ++re) {
if (c < re->min)
return(c);
if (c <= re->max)
return(re->map + c - re->min);
}
return(c);
}
#if !defined(_USE_CTYPE_INLINE_) && !defined(_USE_CTYPE_MACROS_)
/*
* See comments in <machine/ansi.h>
*/
int
__istype(c, f)
_BSD_RUNE_T_ c;
unsigned long f;
{
return ((((c & _CRMASK) ? ___runetype(c)
: _CurrentRuneLocale->runetype[c]) & f) ? 1 : 0);
}
int
__isctype(_BSD_RUNE_T_ c, unsigned long f)
_BSD_RUNE_T_ c;
unsigned long f;
{
return ((((c & _CRMASK) ? 0
: _DefaultRuneLocale.runetype[c]) & f) ? 1 : 0);
}
_BSD_RUNE_T_
toupper(c)
_BSD_RUNE_T_ c;
{
return ((c & _CRMASK) ?
___toupper(c) : _CurrentRuneLocale->mapupper[c]);
}
_BSD_RUNE_T_
tolower(c)
_BSD_RUNE_T_ c;
{
return ((c & _CRMASK) ?
___tolower(c) : _CurrentRuneLocale->maplower[c]);
}
#endif

View File

@ -0,0 +1,26 @@
#include <stdio.h>
#include <rune.h>
unsigned long
___runetype(c)
_BSD_RUNE_T_ c;
{
int x;
_RuneRange *rr = &_CurrentRuneLocale->runetype_ext;
_RuneEntry *re = rr->ranges;
if (c == EOF)
return(0);
for (x = 0; x < rr->nranges; ++x, ++re) {
if (c < re->min)
return(0L);
if (c <= re->max) {
if (re->types)
return(re->types[c - re->min]);
else
return(re->map);
}
}
return(0L);
}

View File

@ -43,40 +43,11 @@ static char sccsid[] = "@(#)setlocale.c 8.1 (Berkeley) 7/4/93";
#include <rune.h>
#include <stdlib.h>
#include <string.h>
#include "common_setlocale.h"
#include "common_rune.h"
/*
* Category names for getenv()
*/
static char *categories[_LC_LAST] = {
"LC_ALL",
"LC_COLLATE",
"LC_CTYPE",
"LC_MONETARY",
"LC_NUMERIC",
"LC_TIME",
};
char *_PathLocale;
/*
* Current locales for each category
*/
static char current_categories[_LC_LAST][32] = {
"C",
"C",
"C",
"C",
"C",
"C",
};
/*
* The locales we are going to try and load
*/
static char new_categories[_LC_LAST][32];
static char current_locale_string[_LC_LAST * 33];
static char *PathLocale;
static char *currentlocale __P((void));
static char *loadlocale __P((int));
char *
@ -168,31 +139,13 @@ setlocale(category, locale)
return (NULL);
}
static char *
currentlocale()
{
int i;
(void)strcpy(current_locale_string, current_categories[1]);
for (i = 2; i < _LC_LAST; ++i)
if (strcmp(current_categories[1], current_categories[i])) {
(void)snprintf(current_locale_string,
sizeof(current_locale_string), "%s/%s/%s/%s/%s",
current_categories[1], current_categories[2],
current_categories[3], current_categories[4],
current_categories[5]);
break;
}
return (current_locale_string);
}
static char *
loadlocale(category)
int category;
{
#if 0
char name[PATH_MAX];
#endif
if (strcmp(new_categories[category],
current_categories[category]) == 0)
return (current_categories[category]);
@ -217,13 +170,13 @@ loadlocale(category)
new_categories[category]);
return (current_categories[category]);
}
#if 0
/*
* Some day we will actually look at this file.
*/
(void)snprintf(name, sizeof(name), "%s/%s/%s",
PathLocale, new_categories[category], categories[category]);
#endif
switch (category) {
case LC_COLLATE:
case LC_MONETARY:

View File

@ -0,0 +1,181 @@
/* It is reduced version for use in crt0.c (only 8 bit locales) */
#include <limits.h>
#include <locale.h>
#include <rune.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "common_setlocale.h"
#include "common_rune.h"
char *_PathLocale;
extern int _none_init __P((_RuneLocale *));
static char *loadlocale __P((int));
static int startup_setrunelocale __P((char *));
void
_startup_setlocale(category, locale)
int category;
const char *locale;
{
int found, i, len;
char *env, *r;
if (!PathLocale && !(PathLocale = getenv("PATH_LOCALE")))
PathLocale = _PATH_LOCALE;
if (category < 0 || category >= _LC_LAST)
return;
if (!locale) {
if (!category)
(void) currentlocale();
return;
}
/*
* Default to the current locale for everything.
*/
for (i = 1; i < _LC_LAST; ++i)
(void)strcpy(new_categories[i], current_categories[i]);
/*
* Now go fill up new_categories from the locale argument
*/
if (!*locale) {
env = getenv(categories[category]);
if (!env)
env = getenv(categories[0]);
if (!env)
env = getenv("LANG");
if (!env)
env = "C";
(void) strncpy(new_categories[category], env, 31);
new_categories[category][31] = 0;
if (!category) {
for (i = 1; i < _LC_LAST; ++i) {
if (!(env = getenv(categories[i])))
env = new_categories[0];
(void)strncpy(new_categories[i], env, 31);
new_categories[i][31] = 0;
}
}
} else if (category) {
(void)strncpy(new_categories[category], locale, 31);
new_categories[category][31] = 0;
} else {
if ((r = strchr(locale, '/')) == 0) {
for (i = 1; i < _LC_LAST; ++i) {
(void)strncpy(new_categories[i], locale, 31);
new_categories[i][31] = 0;
}
} else {
for (i = 1; r[1] == '/'; ++r);
if (!r[1])
return; /* Hmm, just slashes... */
do {
len = r - locale > 31 ? 31 : r - locale;
(void)strncpy(new_categories[i++], locale, len);
new_categories[i++][len] = 0;
locale = r;
while (*locale == '/')
++locale;
while (*++r && *r != '/');
} while (*locale);
while (i < _LC_LAST)
(void)strcpy(new_categories[i],
new_categories[i-1]);
}
}
if (category) {
(void) loadlocale(category);
return;
}
found = 0;
for (i = 1; i < _LC_LAST; ++i)
if (loadlocale(i) != NULL)
found = 1;
if (found)
(void) currentlocale();
}
static char *
loadlocale(category)
int category;
{
if (strcmp(new_categories[category],
current_categories[category]) == 0)
return (current_categories[category]);
if (category == LC_CTYPE) {
if (startup_setrunelocale(new_categories[LC_CTYPE]))
return (NULL);
(void)strcpy(current_categories[LC_CTYPE],
new_categories[LC_CTYPE]);
return (current_categories[LC_CTYPE]);
}
if (!strcmp(new_categories[category], "C") ||
!strcmp(new_categories[category], "POSIX")) {
/*
* Some day this will need to reset the locale to the default
* C locale. Since we have no way to change them as of yet,
* there is no need to reset them.
*/
(void)strcpy(current_categories[category],
new_categories[category]);
return (current_categories[category]);
}
return NULL;
}
static int
startup_setrunelocale(encoding)
char *encoding;
{
FILE *fp;
char name[PATH_MAX];
_RuneLocale *rl;
if (!encoding)
return(EFAULT);
/*
* The "C" and "POSIX" locale are always here.
*/
if (!strcmp(encoding, "C") || !strcmp(encoding, "POSIX")) {
_CurrentRuneLocale = &_DefaultRuneLocale;
return(0);
}
(void) strcpy(name, PathLocale);
(void) strcat(name, "/");
(void) strcat(name, encoding);
(void) strcat(name, "/LC_CTYPE");
if ((fp = fopen(name, "r")) == NULL)
return(ENOENT);
if ((rl = _Read_RuneMagi(fp)) == 0) {
fclose(fp);
return(EFTYPE);
}
if (!rl->encoding[0])
return(EINVAL);
else if (!strcmp(rl->encoding, "NONE"))
return(_none_init(rl));
else
return(EINVAL);
}

23
lib/libc/locale/tolower.c Normal file
View File

@ -0,0 +1,23 @@
#include <stdio.h>
#include <rune.h>
_BSD_RUNE_T_
___tolower(c)
_BSD_RUNE_T_ c;
{
int x;
_RuneRange *rr = &_CurrentRuneLocale->maplower_ext;
_RuneEntry *re = rr->ranges;
if (c == EOF)
return(EOF);
for (x = 0; x < rr->nranges; ++x, ++re) {
if (c < re->min)
return(c);
if (c <= re->max)
return(re->map + c - re->min);
}
return(c);
}

22
lib/libc/locale/toupper.c Normal file
View File

@ -0,0 +1,22 @@
#include <stdio.h>
#include <rune.h>
_BSD_RUNE_T_
___toupper(c)
_BSD_RUNE_T_ c;
{
int x;
_RuneRange *rr = &_CurrentRuneLocale->mapupper_ext;
_RuneEntry *re = rr->ranges;
if (c == EOF)
return(EOF);
for (x = 0; x < rr->nranges; ++x, ++re) {
if (c < re->min)
return(c);
if (c <= re->max)
return(re->map + c - re->min);
}
return(c);
}