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(api_posix_nl_catd catd)
108 if(API_POSIX_NL_MSGMAX < nls_max_message)
110 nls_max_message = API_POSIX_NL_MSGMAX;
114 for(i = 1; i <= (size_t) nls_max_message; ++i)
119 rv = api_posix_catgets(catd, 1, (
int) i, NULL);
121 while(NULL == rv && API_POSIX_EINTR == api_posix_errno);
122 len =
sizeof(
const char*) * i;
123 array = (
const char**) api_posix_realloc(nls_message, len);
126 PRINT_ERROR(
"Out of memory while creating message array");
133 nls_last_message = (int) i;
138 len = strlen(rv) + (size_t) 1;
139 s = (
const char*) api_posix_malloc(len);
140 if(s) { memcpy((
void*) s, (
void*) rv, len); }
142 nls_message[i - (size_t) 1] = s;
178 #if CFG_USE_XSI && !CFG_NLS_DISABLE
181 enum nls_locale rv = NLS_LOCALE_VALID;
182 api_posix_nl_catd catd = (api_posix_nl_catd) -1;
186 const char* subdir =
"/";
187 const char* catext =
".cat";
188 char* catname = NULL;
200 loc_sys = api_posix_setlocale(API_POSIX_LC_ALL,
"");
204 PRINT_ERROR(
"Cannot set locale (check 'LANG' and 'LC_*' variables)");
215 loc_sys = api_posix_setlocale(API_POSIX_LC_MESSAGES,
"");
216 printf(
"%s: %sMessage locale: %s\n", CFG_NAME,
MAIN_ERR_PREFIX, loc_sys);
223 rv = NLS_LOCALE_DEFAULT;
229 if(!islower((
unsigned char)
nls_loc[0]))
231 if(isupper((
unsigned char)
nls_loc[0]))
235 else rv = NLS_LOCALE_INVALID;
237 if(!islower((
unsigned char)
nls_loc[1]))
239 if (isupper((
unsigned char)
nls_loc[1]))
243 else rv = NLS_LOCALE_INVALID;
245 if(!isupper((
unsigned char)
nls_loc[3]))
247 if(islower((
unsigned char)
nls_loc[3]))
251 else rv = NLS_LOCALE_INVALID;
253 if(!isupper((
unsigned char)
nls_loc[4]))
255 if(islower((
unsigned char)
nls_loc[4]))
259 else rv = NLS_LOCALE_INVALID;
266 if(!islower((
unsigned char)
nls_loc[0]))
268 if(isupper((
unsigned char)
nls_loc[0]))
272 else rv = NLS_LOCALE_INVALID;
274 if(!islower((
unsigned char)
nls_loc[1]))
276 if (isupper((
unsigned char)
nls_loc[1]))
280 else rv = NLS_LOCALE_INVALID;
282 if(NLS_LOCALE_VALID == rv)
290 else rv = NLS_LOCALE_INVALID;
294 case NLS_LOCALE_INVALID:
298 PRINT_ERROR(
"Format must be 'xx' or start with 'xx_YY'");
302 case NLS_LOCALE_VALID:
303 case NLS_LOCALE_DEFAULT:
305 printf(
"%s: %sCooked message locale: %s\n",
307 if(NLS_LOCALE_DEFAULT == rv)
328 len = strlen(CFG_NLS_PATH);
329 len += strlen(subdir);
331 len += strlen(catext);
334 catname = api_posix_malloc(len);
337 PRINT_ERROR(
"Out of memory while creating catalog pathname");
342 strcpy(catname, CFG_NLS_PATH);
343 strcat(catname, subdir);
345 strcat(catname, catext);
350 len_max = api_posix_pathconf(CFG_NLS_PATH, API_POSIX_PC_PATH_MAX);
357 else if((
size_t) len_max < len)
374 rv2 = api_posix_open(catname, API_POSIX_O_RDONLY);
377 PRINT_ERROR(
"Catalog not found, using default strings");
382 api_posix_close(rv2);
383 catd = api_posix_catopen(catname, API_POSIX_NL_CAT_LOCALE);
384 if((api_posix_nl_catd) -1 == catd)
386 switch(api_posix_errno)
388 case API_POSIX_EACCES:
393 case API_POSIX_ENOENT:
398 case API_POSIX_ENOMEM:
400 PRINT_ERROR(
"Out of memory while opening catalog");
415 api_posix_free(catname);
420 res = nls_import_catalog(catd);
426 rv2 = api_posix_catclose(catd);
428 while(-1 == rv2 && API_POSIX_EINTR == api_posix_errno);
452 #if CFG_USE_XSI && !CFG_NLS_DISABLE
455 if(nls_ready && nls_last_message)
457 for(i = 0; i < (size_t) nls_last_message; ++i)
459 api_posix_free((
void*) nls_message[i]);
461 api_posix_free((
void*) nls_message);
485 #if CFG_USE_XSI && !CFG_NLS_DISABLE
490 if(1 > n || n > nls_last_message)
492 PRINT_ERROR(
"Value of NLS_MAX_MESSAGE too low (Bug)");
497 if(nls_message[--n]) { res = nls_message[n]; }