56 #define MAIN_ERR_PREFIX "NLS: "
69 #define NLS_MAX_MESSAGE 384
77 #if CFG_USE_XSI && !CFG_NLS_DISABLE
78 static int nls_ready = 0;
79 static int nls_last_message = 0;
80 static const char** nls_message = NULL;
96 #if CFG_USE_XSI && !CFG_NLS_DISABLE
97 static int nls_import_catalog(posix_nl_catd catd)
108 if(POSIX_NL_MSGMAX < nls_max_message) { nls_max_message = POSIX_NL_MSGMAX; }
111 for(i = 1; i <= (size_t) nls_max_message; ++i)
116 rv = posix_catgets(catd, 1, (
int) i, NULL);
118 while(NULL == rv && POSIX_EINTR == posix_errno);
119 len =
sizeof(
const char*) * i;
120 array = (
const char**) posix_realloc(nls_message, len);
123 PRINT_ERROR(
"Out of memory while creating message array");
130 nls_last_message = (int) i;
135 len = strlen(rv) + (size_t) 1;
136 s = (
const char*) posix_malloc(len);
137 if(s) { memcpy((
void*) s, (
void*) rv, len); }
139 nls_message[i - (size_t) 1] = s;
175 #if CFG_USE_XSI && !CFG_NLS_DISABLE
178 enum nls_locale rv = NLS_LOCALE_VALID;
179 posix_nl_catd catd = (posix_nl_catd) -1;
183 const char* subdir =
"/";
184 const char* catext =
".cat";
185 char* catname = NULL;
197 loc_sys = posix_setlocale(POSIX_LC_ALL,
"");
201 PRINT_ERROR(
"Cannot set locale (check 'LANG' and 'LC_*' variables)");
212 loc_sys = posix_setlocale(POSIX_LC_MESSAGES,
"");
213 printf(
"%s: %sMessage locale: %s\n", CFG_NAME,
MAIN_ERR_PREFIX, loc_sys);
220 rv = NLS_LOCALE_DEFAULT;
226 if(!islower((
unsigned char)
nls_loc[0]))
228 if(isupper((
unsigned char)
nls_loc[0]))
232 else rv = NLS_LOCALE_INVALID;
234 if(!islower((
unsigned char)
nls_loc[1]))
236 if (isupper((
unsigned char)
nls_loc[1]))
240 else rv = NLS_LOCALE_INVALID;
242 if(!isupper((
unsigned char)
nls_loc[3]))
244 if(islower((
unsigned char)
nls_loc[3]))
248 else rv = NLS_LOCALE_INVALID;
250 if(!isupper((
unsigned char)
nls_loc[4]))
252 if(islower((
unsigned char)
nls_loc[4]))
256 else rv = NLS_LOCALE_INVALID;
263 if(!islower((
unsigned char)
nls_loc[0]))
265 if(isupper((
unsigned char)
nls_loc[0]))
269 else rv = NLS_LOCALE_INVALID;
271 if(!islower((
unsigned char)
nls_loc[1]))
273 if (isupper((
unsigned char)
nls_loc[1]))
277 else rv = NLS_LOCALE_INVALID;
279 if(NLS_LOCALE_VALID == rv)
287 else rv = NLS_LOCALE_INVALID;
291 case NLS_LOCALE_INVALID:
295 PRINT_ERROR(
"Format must be 'xx' or start with 'xx_YY'");
299 case NLS_LOCALE_VALID:
300 case NLS_LOCALE_DEFAULT:
302 printf(
"%s: %sCooked message locale: %s\n",
304 if(NLS_LOCALE_DEFAULT == rv)
325 len = strlen(CFG_NLS_PATH);
326 len += strlen(subdir);
328 len += strlen(catext);
331 catname = posix_malloc(len);
334 PRINT_ERROR(
"Out of memory while creating catalog pathname");
339 strcpy(catname, CFG_NLS_PATH);
340 strcat(catname, subdir);
342 strcat(catname, catext);
347 len_max = posix_pathconf(CFG_NLS_PATH, POSIX_PC_PATH_MAX);
354 else if((
size_t) len_max < len)
371 rv2 = posix_open(catname, POSIX_O_RDONLY);
374 PRINT_ERROR(
"Catalog not found, using default strings");
380 catd = posix_catopen(catname, POSIX_NL_CAT_LOCALE);
381 if((posix_nl_catd) -1 == catd)
397 PRINT_ERROR(
"Out of memory while opening catalog");
417 res = nls_import_catalog(catd);
423 rv2 = posix_catclose(catd);
425 while(-1 == rv2 && POSIX_EINTR == posix_errno);
449 #if CFG_USE_XSI && !CFG_NLS_DISABLE
452 if(nls_ready && nls_last_message)
454 for(i = 0; i < (size_t) nls_last_message; ++i)
456 posix_free((
void*) nls_message[i]);
458 posix_free((
void*) nls_message);
482 #if CFG_USE_XSI && !CFG_NLS_DISABLE
487 if(1 > n || n > nls_last_message)
489 PRINT_ERROR(
"Value of NLS_MAX_MESSAGE too low (Bug)");
494 if(nls_message[--n]) { res = nls_message[n]; }