1 /* 2 * Copyright (c) 1998, 2011, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 #if defined(__linux__) || defined(_ALLBSD_SOURCE) 27 #include <stdio.h> 28 #include <ctype.h> 29 #endif 30 #include <pwd.h> 31 #include <locale.h> 32 #ifndef ARCHPROPNAME 33 #error "The macro ARCHPROPNAME has not been defined" 34 #endif 35 #include <sys/utsname.h> /* For os_name and os_version */ 36 #include <langinfo.h> /* For nl_langinfo */ 37 #include <stdlib.h> 38 #include <string.h> 39 #include <sys/types.h> 40 #include <unistd.h> 41 #include <sys/param.h> 42 #include <time.h> 43 #include <errno.h> 44 45 #ifdef MACOSX 46 #include "java_props_macosx.h" 47 #endif 48 49 #if defined(_ALLBSD_SOURCE) 50 #if !defined(P_tmpdir) 51 #include <paths.h> 52 #define P_tmpdir _PATH_VARTMP 53 #endif 54 #endif 55 56 #include "locale_str.h" 57 #include "java_props.h" 58 59 #if !defined(_ALLBSD_SOURCE) 60 #ifdef __linux__ 61 #ifndef CODESET 62 #define CODESET _NL_CTYPE_CODESET_NAME 63 #endif 64 #else 65 #ifdef ALT_CODESET_KEY 66 #define CODESET ALT_CODESET_KEY 67 #endif 68 #endif 69 #endif /* !_ALLBSD_SOURCE */ 70 71 #ifdef JAVASE_EMBEDDED 72 #include <dlfcn.h> 73 #include <sys/stat.h> 74 #endif 75 76 77 78 /* Take an array of string pairs (map of key->value) and a string (key). 79 * Examine each pair in the map to see if the first string (key) matches the 80 * string. If so, store the second string of the pair (value) in the value and 81 * return 1. Otherwise do nothing and return 0. The end of the map is 82 * indicated by an empty string at the start of a pair (key of ""). 83 */ 84 static int 85 mapLookup(char* map[], const char* key, char** value) { 86 int i; 87 for (i = 0; strcmp(map[i], ""); i += 2){ 88 if (!strcmp(key, map[i])){ 89 *value = map[i + 1]; 90 return 1; 91 } 92 } 93 return 0; 94 } 95 96 /* This function sets an environment variable using envstring. 97 * The format of envstring is "name=value". 98 * If the name has already existed, it will append value to the name. 99 */ 100 static void 101 setPathEnvironment(char *envstring) 102 { 103 char name[20], *value, *current; 104 105 value = strchr(envstring, '='); /* locate name and value separator */ 106 107 if (! value) 108 return; /* not a valid environment setting */ 109 110 /* copy first part as environment name */ 111 strncpy(name, envstring, value - envstring); 112 name[value-envstring] = '\0'; 113 114 value++; /* set value point to value of the envstring */ 115 116 current = getenv(name); 117 if (current) { 118 if (! strstr(current, value)) { 119 /* value is not found in current environment, append it */ 120 char *temp = malloc(strlen(envstring) + strlen(current) + 2); 121 strcpy(temp, name); 122 strcat(temp, "="); 123 strcat(temp, current); 124 strcat(temp, ":"); 125 strcat(temp, value); 126 putenv(temp); 127 } 128 /* else the value has already been set, do nothing */ 129 } 130 else { 131 /* environment variable is not found */ 132 putenv(envstring); 133 } 134 } 135 136 #ifndef P_tmpdir 137 #define P_tmpdir "/var/tmp" 138 #endif 139 140 static int ParseLocale(int cat, char ** std_language, char ** std_script, 141 char ** std_country, char ** std_variant, char ** std_encoding) { 142 char temp[64]; 143 char *language = NULL, *country = NULL, *variant = NULL, 144 *encoding = NULL; 145 char *p, encoding_variant[64]; 146 char *lc; 147 148 /* Query the locale set for the category */ 149 150 #ifdef MACOSX 151 lc = setupMacOSXLocale(cat); // malloc'd memory, need to free 152 #else 153 lc = setlocale(cat, NULL); 154 #endif 155 156 #ifndef __linux__ 157 if (lc == NULL) { 158 return 0; 159 } 160 161 if (cat == LC_CTYPE) { 162 /* 163 * Workaround for Solaris bug 4201684: Xlib doesn't like @euro 164 * locales. Since we don't depend on the libc @euro behavior, 165 * we just remove the qualifier. 166 * On Linux, the bug doesn't occur; on the other hand, @euro 167 * is needed there because it's a shortcut that also determines 168 * the encoding - without it, we wouldn't get ISO-8859-15. 169 * Therefore, this code section is Solaris-specific. 170 */ 171 lc = strdup(lc); /* keep a copy, setlocale trashes original. */ 172 strcpy(temp, lc); 173 p = strstr(temp, "@euro"); 174 if (p != NULL) { 175 *p = '\0'; 176 setlocale(LC_ALL, temp); 177 } 178 } 179 #else 180 if (lc == NULL || !strcmp(lc, "C") || !strcmp(lc, "POSIX")) { 181 lc = "en_US"; 182 } 183 #endif 184 185 /* 186 * locale string format in Solaris is 187 * <language name>_<country name>.<encoding name>@<variant name> 188 * <country name>, <encoding name>, and <variant name> are optional. 189 */ 190 191 strcpy(temp, lc); 192 #ifdef MACOSX 193 free(lc); // malloced memory 194 #endif 195 /* Parse the language, country, encoding, and variant from the 196 * locale. Any of the elements may be missing, but they must occur 197 * in the order language_country.encoding@variant, and must be 198 * preceded by their delimiter (except for language). 199 * 200 * If the locale name (without .encoding@variant, if any) matches 201 * any of the names in the locale_aliases list, map it to the 202 * corresponding full locale name. Most of the entries in the 203 * locale_aliases list are locales that include a language name but 204 * no country name, and this facility is used to map each language 205 * to a default country if that's possible. It's also used to map 206 * the Solaris locale aliases to their proper Java locale IDs. 207 */ 208 if ((p = strchr(temp, '.')) != NULL) { 209 strcpy(encoding_variant, p); /* Copy the leading '.' */ 210 *p = '\0'; 211 } else if ((p = strchr(temp, '@')) != NULL) { 212 strcpy(encoding_variant, p); /* Copy the leading '@' */ 213 *p = '\0'; 214 } else { 215 *encoding_variant = '\0'; 216 } 217 218 if (mapLookup(locale_aliases, temp, &p)) { 219 strcpy(temp, p); 220 // check the "encoding_variant" again, if any. 221 if ((p = strchr(temp, '.')) != NULL) { 222 strcpy(encoding_variant, p); /* Copy the leading '.' */ 223 *p = '\0'; 224 } else if ((p = strchr(temp, '@')) != NULL) { 225 strcpy(encoding_variant, p); /* Copy the leading '@' */ 226 *p = '\0'; 227 } 228 } 229 230 language = temp; 231 if ((country = strchr(temp, '_')) != NULL) { 232 *country++ = '\0'; 233 } 234 235 p = encoding_variant; 236 if ((encoding = strchr(p, '.')) != NULL) { 237 p[encoding++ - p] = '\0'; 238 p = encoding; 239 } 240 if ((variant = strchr(p, '@')) != NULL) { 241 p[variant++ - p] = '\0'; 242 } 243 244 /* Normalize the language name */ 245 if (std_language != NULL) { 246 *std_language = "en"; 247 if (language != NULL && mapLookup(language_names, language, std_language) == 0) { 248 *std_language = malloc(strlen(language)+1); 249 strcpy(*std_language, language); 250 } 251 } 252 253 /* Normalize the country name */ 254 if (std_country != NULL && country != NULL) { 255 if (mapLookup(country_names, country, std_country) == 0) { 256 *std_country = malloc(strlen(country)+1); 257 strcpy(*std_country, country); 258 } 259 } 260 261 /* Normalize the script and variant name. Note that we only use 262 * variants listed in the mapping array; others are ignored. 263 */ 264 if (variant != NULL) { 265 if (std_script != NULL) { 266 mapLookup(script_names, variant, std_script); 267 } 268 269 if (std_variant != NULL) { 270 mapLookup(variant_names, variant, std_variant); 271 } 272 } 273 274 /* Normalize the encoding name. Note that we IGNORE the string 275 * 'encoding' extracted from the locale name above. Instead, we use the 276 * more reliable method of calling nl_langinfo(CODESET). This function 277 * returns an empty string if no encoding is set for the given locale 278 * (e.g., the C or POSIX locales); we use the default ISO 8859-1 279 * converter for such locales. 280 */ 281 if (std_encoding != NULL) { 282 /* OK, not so reliable - nl_langinfo() gives wrong answers on 283 * Euro locales, in particular. */ 284 if (strcmp(p, "ISO8859-15") == 0) 285 p = "ISO8859-15"; 286 else 287 p = nl_langinfo(CODESET); 288 289 /* Convert the bare "646" used on Solaris to a proper IANA name */ 290 if (strcmp(p, "646") == 0) 291 p = "ISO646-US"; 292 293 /* return same result nl_langinfo would return for en_UK, 294 * in order to use optimizations. */ 295 *std_encoding = (*p != '\0') ? p : "ISO8859-1"; 296 297 #ifdef __linux__ 298 /* 299 * Remap the encoding string to a different value for japanese 300 * locales on linux so that customized converters are used instead 301 * of the default converter for "EUC-JP". The customized converters 302 * omit support for the JIS0212 encoding which is not supported by 303 * the variant of "EUC-JP" encoding used on linux 304 */ 305 if (strcmp(p, "EUC-JP") == 0) { 306 *std_encoding = "EUC-JP-LINUX"; 307 } 308 #else 309 if (strcmp(p,"eucJP") == 0) { 310 /* For Solaris use customized vendor defined character 311 * customized EUC-JP converter 312 */ 313 *std_encoding = "eucJP-open"; 314 } else if (strcmp(p, "Big5") == 0 || strcmp(p, "BIG5") == 0) { 315 /* 316 * Remap the encoding string to Big5_Solaris which augments 317 * the default converter for Solaris Big5 locales to include 318 * seven additional ideographic characters beyond those included 319 * in the Java "Big5" converter. 320 */ 321 *std_encoding = "Big5_Solaris"; 322 } else if (strcmp(p, "Big5-HKSCS") == 0) { 323 /* 324 * Solaris uses HKSCS2001 325 */ 326 *std_encoding = "Big5-HKSCS-2001"; 327 } 328 #endif 329 } 330 return 1; 331 } 332 333 #ifdef JAVASE_EMBEDDED 334 /* Determine the default embedded toolkit based on whether lib/xawt/ 335 * exists in the JRE. This can still be overridden by -Dawt.toolkit=XXX 336 */ 337 static char* getEmbeddedToolkit() { 338 Dl_info dlinfo; 339 char buf[MAXPATHLEN]; 340 int32_t len; 341 char *p; 342 struct stat statbuf; 343 344 /* Get address of this library and the directory containing it. */ 345 dladdr((void *)getEmbeddedToolkit, &dlinfo); 346 realpath((char *)dlinfo.dli_fname, buf); 347 len = strlen(buf); 348 p = strrchr(buf, '/'); 349 /* Default AWT Toolkit on Linux and Solaris is XAWT. */ 350 strncpy(p, "/xawt/", MAXPATHLEN-len-1); 351 /* Check if it exists */ 352 if (stat(buf, &statbuf) == -1 && errno == ENOENT) { 353 /* No - this is a reduced-headless-jre so use special HToolkit */ 354 return "sun.awt.HToolkit"; 355 } 356 else { 357 /* Yes - this is a headful JRE so fallback to SE defaults */ 358 return NULL; 359 } 360 } 361 #endif 362 363 364 /* This function gets called very early, before VM_CALLS are setup. 365 * Do not use any of the VM_CALLS entries!!! 366 */ 367 java_props_t * 368 GetJavaProperties(JNIEnv *env) 369 { 370 static java_props_t sprops; 371 char *v; /* tmp var */ 372 373 if (sprops.user_dir) { 374 return &sprops; 375 } 376 377 /* tmp dir */ 378 sprops.tmp_dir = P_tmpdir; 379 #ifdef MACOSX 380 /* darwin has a per-user temp dir */ 381 static char tmp_path[PATH_MAX]; 382 int pathSize = confstr(_CS_DARWIN_USER_TEMP_DIR, tmp_path, PATH_MAX); 383 if (pathSize > 0 && pathSize <= PATH_MAX) { 384 sprops.tmp_dir = tmp_path; 385 } 386 #endif /* MACOSX */ 387 388 /* Printing properties */ 389 #ifdef MACOSX 390 sprops.printerJob = "sun.lwawt.macosx.CPrinterJob"; 391 #else 392 sprops.printerJob = "sun.print.PSPrinterJob"; 393 #endif 394 395 /* patches/service packs installed */ 396 sprops.patch_level = "unknown"; 397 398 /* Java 2D properties */ 399 #ifdef MACOSX 400 PreferredToolkit prefToolkit = getPreferredToolkit(); 401 switch (prefToolkit) { 402 case CToolkit: 403 sprops.graphics_env = "sun.awt.CGraphicsEnvironment"; 404 break; 405 case XToolkit: 406 #endif 407 sprops.graphics_env = "sun.awt.X11GraphicsEnvironment"; 408 #ifdef MACOSX 409 break; 410 default: 411 sprops.graphics_env = "sun.awt.HeadlessGraphicsEnvironment"; 412 break; 413 } 414 #endif 415 /* AWT properties */ 416 #ifdef JAVASE_EMBEDDED 417 sprops.awt_toolkit = getEmbeddedToolkit(); 418 if (sprops.awt_toolkit == NULL) // default as below 419 #endif 420 #ifdef MACOSX 421 switch (prefToolkit) { 422 case CToolkit: 423 sprops.awt_toolkit = "sun.lwawt.macosx.LWCToolkit"; 424 break; 425 case XToolkit: 426 #endif 427 sprops.awt_toolkit = "sun.awt.X11.XToolkit"; 428 #ifdef MACOSX 429 break; 430 default: 431 sprops.graphics_env = "sun.awt.HToolkit"; 432 break; 433 } 434 #endif 435 436 /* This is used only for debugging of font problems. */ 437 v = getenv("JAVA2D_FONTPATH"); 438 sprops.font_dir = v ? v : NULL; 439 440 #ifdef SI_ISALIST 441 /* supported instruction sets */ 442 { 443 char list[258]; 444 sysinfo(SI_ISALIST, list, sizeof(list)); 445 sprops.cpu_isalist = strdup(list); 446 } 447 #else 448 sprops.cpu_isalist = NULL; 449 #endif 450 451 /* endianness of platform */ 452 { 453 unsigned int endianTest = 0xff000000; 454 if (((char*)(&endianTest))[0] != 0) 455 sprops.cpu_endian = "big"; 456 else 457 sprops.cpu_endian = "little"; 458 } 459 460 /* os properties */ 461 { 462 sprops.os_arch = ARCHPROPNAME; 463 464 #ifdef MACOSX 465 setOSNameAndVersion(&sprops); 466 #ifdef __x86_64__ 467 sprops.os_arch = "x86_64"; 468 #elif defined(__i386__) 469 sprops.os_arch = "i386"; 470 #endif 471 #else 472 struct utsname name; 473 uname(&name); 474 sprops.os_name = strdup(name.sysname); 475 sprops.os_version = strdup(name.release); 476 #endif 477 478 if (getenv("GNOME_DESKTOP_SESSION_ID") != NULL) { 479 sprops.desktop = "gnome"; 480 } 481 else { 482 sprops.desktop = NULL; 483 } 484 } 485 486 /* Determine the language, country, variant, and encoding from the host, 487 * and store these in the user.language, user.country, user.variant and 488 * file.encoding system properties. */ 489 setlocale(LC_ALL, ""); 490 if (ParseLocale(LC_CTYPE, 491 &(sprops.format_language), 492 &(sprops.format_script), 493 &(sprops.format_country), 494 &(sprops.format_variant), 495 &(sprops.encoding))) { 496 ParseLocale(LC_MESSAGES, 497 &(sprops.language), 498 &(sprops.script), 499 &(sprops.country), 500 &(sprops.variant), 501 NULL); 502 } else { 503 sprops.language = "en"; 504 sprops.encoding = "ISO8859-1"; 505 } 506 sprops.display_language = sprops.language; 507 sprops.display_script = sprops.script; 508 sprops.display_country = sprops.country; 509 sprops.display_variant = sprops.variant; 510 sprops.sun_jnu_encoding = sprops.encoding; 511 512 #ifdef _ALLBSD_SOURCE 513 #if BYTE_ORDER == _LITTLE_ENDIAN 514 sprops.unicode_encoding = "UnicodeLittle"; 515 #else 516 sprops.unicode_encoding = "UnicodeBig"; 517 #endif 518 #else /* !_ALLBSD_SOURCE */ 519 #ifdef __linux__ 520 #if __BYTE_ORDER == __LITTLE_ENDIAN 521 sprops.unicode_encoding = "UnicodeLittle"; 522 #else 523 sprops.unicode_encoding = "UnicodeBig"; 524 #endif 525 #else 526 sprops.unicode_encoding = "UnicodeBig"; 527 #endif 528 #endif /* _ALLBSD_SOURCE */ 529 530 /* user properties */ 531 { 532 struct passwd *pwent = getpwuid(getuid()); 533 sprops.user_name = pwent ? strdup(pwent->pw_name) : "?"; 534 sprops.user_home = pwent ? strdup(pwent->pw_dir) : "?"; 535 } 536 537 /* User TIMEZONE */ 538 { 539 /* 540 * We defer setting up timezone until it's actually necessary. 541 * Refer to TimeZone.getDefault(). However, the system 542 * property is necessary to be able to be set by the command 543 * line interface -D. Here temporarily set a null string to 544 * timezone. 545 */ 546 tzset(); /* for compatibility */ 547 sprops.timezone = ""; 548 } 549 550 /* Current directory */ 551 { 552 char buf[MAXPATHLEN]; 553 errno = 0; 554 if (getcwd(buf, sizeof(buf)) == NULL) 555 JNU_ThrowByName(env, "java/lang/Error", 556 "Properties init: Could not determine current working directory."); 557 else 558 sprops.user_dir = strdup(buf); 559 } 560 561 sprops.file_separator = "/"; 562 sprops.path_separator = ":"; 563 sprops.line_separator = "\n"; 564 565 #if !defined(_ALLBSD_SOURCE) 566 /* Append CDE message and resource search path to NLSPATH and 567 * XFILESEARCHPATH, in order to pick localized message for 568 * FileSelectionDialog window (Bug 4173641). 569 */ 570 setPathEnvironment("NLSPATH=/usr/dt/lib/nls/msg/%L/%N.cat"); 571 setPathEnvironment("XFILESEARCHPATH=/usr/dt/app-defaults/%L/Dt"); 572 #endif 573 574 575 #ifdef MACOSX 576 setProxyProperties(&sprops); 577 #endif 578 579 return &sprops; 580 } 581 582 jstring 583 GetStringPlatform(JNIEnv *env, nchar* cstr) 584 { 585 return JNU_NewStringPlatform(env, cstr); 586 }