java_props_md.c 19.4 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 1998, 2012, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
7
 * published by the Free Software Foundation.  Oracle designates this
D
duke 已提交
8
 * particular file as subject to the "Classpath" exception as provided
9
 * by Oracle in the LICENSE file that accompanied this code.
D
duke 已提交
10 11 12 13 14 15 16 17 18 19 20
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
21 22 23
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
24 25
 */

26
#if defined(__linux__) || defined(_ALLBSD_SOURCE)
D
duke 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
#include <stdio.h>
#include <ctype.h>
#endif
#include <pwd.h>
#include <locale.h>
#ifndef ARCHPROPNAME
#error "The macro ARCHPROPNAME has not been defined"
#endif
#include <sys/utsname.h>        /* For os_name and os_version */
#include <langinfo.h>           /* For nl_langinfo */
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/param.h>
#include <time.h>
#include <errno.h>

45 46 47 48 49 50 51 52 53 54 55
#ifdef MACOSX
#include "java_props_macosx.h"
#endif

#if defined(_ALLBSD_SOURCE)
#if !defined(P_tmpdir)
#include <paths.h>
#define P_tmpdir _PATH_VARTMP
#endif
#endif

D
duke 已提交
56 57 58
#include "locale_str.h"
#include "java_props.h"

59
#if !defined(_ALLBSD_SOURCE)
D
duke 已提交
60
#ifdef __linux__
61 62 63
  #ifndef CODESET
  #define CODESET _NL_CTYPE_CODESET_NAME
  #endif
D
duke 已提交
64 65 66 67 68
#else
#ifdef ALT_CODESET_KEY
#define CODESET ALT_CODESET_KEY
#endif
#endif
69
#endif /* !_ALLBSD_SOURCE */
D
duke 已提交
70

71 72 73 74 75
#ifdef JAVASE_EMBEDDED
#include <dlfcn.h>
#include <sys/stat.h>
#endif

D
duke 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
/* Take an array of string pairs (map of key->value) and a string (key).
 * Examine each pair in the map to see if the first string (key) matches the
 * string.  If so, store the second string of the pair (value) in the value and
 * return 1.  Otherwise do nothing and return 0.  The end of the map is
 * indicated by an empty string at the start of a pair (key of "").
 */
static int
mapLookup(char* map[], const char* key, char** value) {
    int i;
    for (i = 0; strcmp(map[i], ""); i += 2){
        if (!strcmp(key, map[i])){
            *value = map[i + 1];
            return 1;
        }
    }
    return 0;
}

/* This function sets an environment variable using envstring.
 * The format of envstring is "name=value".
 * If the name has already existed, it will append value to the name.
 */
static void
setPathEnvironment(char *envstring)
{
    char name[20], *value, *current;

    value = strchr(envstring, '='); /* locate name and value separator */

    if (! value)
        return; /* not a valid environment setting */

    /* copy first part as environment name */
    strncpy(name, envstring, value - envstring);
    name[value-envstring] = '\0';

    value++; /* set value point to value of the envstring */

    current = getenv(name);
    if (current) {
        if (! strstr(current, value)) {
            /* value is not found in current environment, append it */
            char *temp = malloc(strlen(envstring) + strlen(current) + 2);
        strcpy(temp, name);
        strcat(temp, "=");
        strcat(temp, current);
        strcat(temp, ":");
        strcat(temp, value);
        putenv(temp);
        }
        /* else the value has already been set, do nothing */
    }
    else {
        /* environment variable is not found */
        putenv(envstring);
    }
}

#ifndef P_tmpdir
#define P_tmpdir "/var/tmp"
#endif

138
static int ParseLocale(JNIEnv* env, int cat, char ** std_language, char ** std_script,
139
                       char ** std_country, char ** std_variant, char ** std_encoding) {
140
    char *temp = NULL;
141 142
    char *language = NULL, *country = NULL, *variant = NULL,
         *encoding = NULL;
143
    char *p, *encoding_variant, *old_temp, *old_ev;
144 145 146
    char *lc;

    /* Query the locale set for the category */
147 148 149 150

#ifdef MACOSX
    lc = setupMacOSXLocale(cat); // malloc'd memory, need to free
#else
151
    lc = setlocale(cat, NULL);
152
#endif
153 154 155 156 157 158

#ifndef __linux__
    if (lc == NULL) {
        return 0;
    }

159 160 161 162 163 164
    temp = malloc(strlen(lc) + 1);
    if (temp == NULL) {
        JNU_ThrowOutOfMemoryError(env, NULL);
        return 0;
    }

165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
    if (cat == LC_CTYPE) {
        /*
         * Workaround for Solaris bug 4201684: Xlib doesn't like @euro
         * locales. Since we don't depend on the libc @euro behavior,
         * we just remove the qualifier.
         * On Linux, the bug doesn't occur; on the other hand, @euro
         * is needed there because it's a shortcut that also determines
         * the encoding - without it, we wouldn't get ISO-8859-15.
         * Therefore, this code section is Solaris-specific.
         */
        lc = strdup(lc);    /* keep a copy, setlocale trashes original. */
        strcpy(temp, lc);
        p = strstr(temp, "@euro");
        if (p != NULL) {
            *p = '\0';
            setlocale(LC_ALL, temp);
        }
    }
#else
    if (lc == NULL || !strcmp(lc, "C") || !strcmp(lc, "POSIX")) {
        lc = "en_US";
    }
187 188 189 190 191 192 193

    temp = malloc(strlen(lc) + 1);
    if (temp == NULL) {
        JNU_ThrowOutOfMemoryError(env, NULL);
        return 0;
    }

194 195 196 197 198 199 200 201 202
#endif

    /*
     * locale string format in Solaris is
     * <language name>_<country name>.<encoding name>@<variant name>
     * <country name>, <encoding name>, and <variant name> are optional.
     */

    strcpy(temp, lc);
203 204 205
#ifdef MACOSX
    free(lc); // malloced memory
#endif
206 207 208 209 210 211 212 213 214 215 216 217 218
    /* Parse the language, country, encoding, and variant from the
     * locale.  Any of the elements may be missing, but they must occur
     * in the order language_country.encoding@variant, and must be
     * preceded by their delimiter (except for language).
     *
     * If the locale name (without .encoding@variant, if any) matches
     * any of the names in the locale_aliases list, map it to the
     * corresponding full locale name.  Most of the entries in the
     * locale_aliases list are locales that include a language name but
     * no country name, and this facility is used to map each language
     * to a default country if that's possible.  It's also used to map
     * the Solaris locale aliases to their proper Java locale IDs.
     */
219 220 221

    encoding_variant = malloc(strlen(temp)+1);
    if (encoding_variant == NULL) {
222
        free(temp);
223 224 225 226
        JNU_ThrowOutOfMemoryError(env, NULL);
        return 0;
    }

227 228 229 230
    if ((p = strchr(temp, '.')) != NULL) {
        strcpy(encoding_variant, p); /* Copy the leading '.' */
        *p = '\0';
    } else if ((p = strchr(temp, '@')) != NULL) {
231 232
        strcpy(encoding_variant, p); /* Copy the leading '@' */
        *p = '\0';
233 234 235 236 237
    } else {
        *encoding_variant = '\0';
    }

    if (mapLookup(locale_aliases, temp, &p)) {
238
        old_temp = temp;
239 240
        temp = realloc(temp, strlen(p)+1);
        if (temp == NULL) {
241 242
            free(old_temp);
            free(encoding_variant);
243 244 245
            JNU_ThrowOutOfMemoryError(env, NULL);
            return 0;
        }
246
        strcpy(temp, p);
247
        old_ev = encoding_variant;
248 249
        encoding_variant = realloc(encoding_variant, strlen(temp)+1);
        if (encoding_variant == NULL) {
250 251
            free(old_ev);
            free(temp);
252 253 254
            JNU_ThrowOutOfMemoryError(env, NULL);
            return 0;
        }
255 256 257 258 259 260 261 262
        // check the "encoding_variant" again, if any.
        if ((p = strchr(temp, '.')) != NULL) {
            strcpy(encoding_variant, p); /* Copy the leading '.' */
            *p = '\0';
        } else if ((p = strchr(temp, '@')) != NULL) {
            strcpy(encoding_variant, p); /* Copy the leading '@' */
            *p = '\0';
        }
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
    }

    language = temp;
    if ((country = strchr(temp, '_')) != NULL) {
        *country++ = '\0';
    }

    p = encoding_variant;
    if ((encoding = strchr(p, '.')) != NULL) {
        p[encoding++ - p] = '\0';
        p = encoding;
    }
    if ((variant = strchr(p, '@')) != NULL) {
        p[variant++ - p] = '\0';
    }

    /* Normalize the language name */
    if (std_language != NULL) {
        *std_language = "en";
282 283 284
        if (language != NULL && mapLookup(language_names, language, std_language) == 0) {
            *std_language = malloc(strlen(language)+1);
            strcpy(*std_language, language);
285 286 287 288 289
        }
    }

    /* Normalize the country name */
    if (std_country != NULL && country != NULL) {
290 291 292 293
        if (mapLookup(country_names, country, std_country) == 0) {
            *std_country = malloc(strlen(country)+1);
            strcpy(*std_country, country);
        }
294 295
    }

296 297 298 299 300 301 302 303 304 305 306
    /* Normalize the script and variant name.  Note that we only use
     * variants listed in the mapping array; others are ignored.
     */
    if (variant != NULL) {
        if (std_script != NULL) {
            mapLookup(script_names, variant, std_script);
        }

        if (std_variant != NULL) {
            mapLookup(variant_names, variant, std_variant);
        }
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
    }

    /* Normalize the encoding name.  Note that we IGNORE the string
     * 'encoding' extracted from the locale name above.  Instead, we use the
     * more reliable method of calling nl_langinfo(CODESET).  This function
     * returns an empty string if no encoding is set for the given locale
     * (e.g., the C or POSIX locales); we use the default ISO 8859-1
     * converter for such locales.
     */
    if (std_encoding != NULL) {
        /* OK, not so reliable - nl_langinfo() gives wrong answers on
         * Euro locales, in particular. */
        if (strcmp(p, "ISO8859-15") == 0)
            p = "ISO8859-15";
        else
            p = nl_langinfo(CODESET);

        /* Convert the bare "646" used on Solaris to a proper IANA name */
        if (strcmp(p, "646") == 0)
            p = "ISO646-US";

        /* return same result nl_langinfo would return for en_UK,
         * in order to use optimizations. */
        *std_encoding = (*p != '\0') ? p : "ISO8859-1";

#ifdef __linux__
        /*
         * Remap the encoding string to a different value for japanese
         * locales on linux so that customized converters are used instead
         * of the default converter for "EUC-JP". The customized converters
         * omit support for the JIS0212 encoding which is not supported by
         * the variant of "EUC-JP" encoding used on linux
         */
        if (strcmp(p, "EUC-JP") == 0) {
            *std_encoding = "EUC-JP-LINUX";
        }
#else
        if (strcmp(p,"eucJP") == 0) {
            /* For Solaris use customized vendor defined character
             * customized EUC-JP converter
             */
            *std_encoding = "eucJP-open";
        } else if (strcmp(p, "Big5") == 0 || strcmp(p, "BIG5") == 0) {
            /*
             * Remap the encoding string to Big5_Solaris which augments
             * the default converter for Solaris Big5 locales to include
             * seven additional ideographic characters beyond those included
             * in the Java "Big5" converter.
             */
            *std_encoding = "Big5_Solaris";
        } else if (strcmp(p, "Big5-HKSCS") == 0) {
            /*
             * Solaris uses HKSCS2001
             */
            *std_encoding = "Big5-HKSCS-2001";
        }
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
#endif
#ifdef MACOSX
        /*
         * For the case on MacOS X where encoding is set to US-ASCII, but we
         * don't have any encoding hints from LANG/LC_ALL/LC_CTYPE, use UTF-8
         * instead.
         *
         * The contents of ASCII files will still be read and displayed
         * correctly, but so will files containing UTF-8 characters beyond the
         * standard ASCII range.
         *
         * Specifically, this allows apps launched by double-clicking a .jar
         * file to correctly read UTF-8 files using the default encoding (see
         * 8011194).
         */
        if (strcmp(p,"US-ASCII") == 0 && getenv("LANG") == NULL &&
            getenv("LC_ALL") == NULL && getenv("LC_CTYPE") == NULL) {
            *std_encoding = "UTF-8";
        }
382 383 384
#endif
    }

385 386 387
    free(temp);
    free(encoding_variant);

388 389 390
    return 1;
}

391
#ifdef JAVASE_EMBEDDED
392
/* Determine the default embedded toolkit based on whether libawt_xawt
393 394 395 396 397 398 399 400 401 402 403 404 405 406
 * exists in the JRE. This can still be overridden by -Dawt.toolkit=XXX
 */
static char* getEmbeddedToolkit() {
    Dl_info dlinfo;
    char buf[MAXPATHLEN];
    int32_t len;
    char *p;
    struct stat statbuf;

    /* Get address of this library and the directory containing it. */
    dladdr((void *)getEmbeddedToolkit, &dlinfo);
    realpath((char *)dlinfo.dli_fname, buf);
    len = strlen(buf);
    p = strrchr(buf, '/');
407 408
    /* Default AWT Toolkit on Linux and Solaris is XAWT (libawt_xawt.so). */
    strncpy(p, "/libawt_xawt.so", MAXPATHLEN-len-1);
409 410 411 412 413 414 415 416 417 418 419 420
    /* Check if it exists */
    if (stat(buf, &statbuf) == -1 && errno == ENOENT) {
        /* No - this is a reduced-headless-jre so use special HToolkit */
        return "sun.awt.HToolkit";
    }
    else {
        /* Yes - this is a headful JRE so fallback to SE defaults */
        return NULL;
    }
}
#endif

D
duke 已提交
421 422 423 424 425 426
/* This function gets called very early, before VM_CALLS are setup.
 * Do not use any of the VM_CALLS entries!!!
 */
java_props_t *
GetJavaProperties(JNIEnv *env)
{
427
    static java_props_t sprops;
D
duke 已提交
428 429 430 431 432 433 434 435
    char *v; /* tmp var */

    if (sprops.user_dir) {
        return &sprops;
    }

    /* tmp dir */
    sprops.tmp_dir = P_tmpdir;
436 437 438 439 440 441 442 443
#ifdef MACOSX
    /* darwin has a per-user temp dir */
    static char tmp_path[PATH_MAX];
    int pathSize = confstr(_CS_DARWIN_USER_TEMP_DIR, tmp_path, PATH_MAX);
    if (pathSize > 0 && pathSize <= PATH_MAX) {
        sprops.tmp_dir = tmp_path;
    }
#endif /* MACOSX */
D
duke 已提交
444 445

    /* Printing properties */
446 447 448
#ifdef MACOSX
    sprops.printerJob = "sun.lwawt.macosx.CPrinterJob";
#else
D
duke 已提交
449
    sprops.printerJob = "sun.print.PSPrinterJob";
450
#endif
D
duke 已提交
451 452 453 454 455

    /* patches/service packs installed */
    sprops.patch_level = "unknown";

    /* Java 2D properties */
456 457 458 459
#ifdef MACOSX
    PreferredToolkit prefToolkit = getPreferredToolkit();
    switch (prefToolkit) {
        case CToolkit:
460
        case HToolkit:
461 462 463 464
            sprops.graphics_env = "sun.awt.CGraphicsEnvironment";
            break;
        case XToolkit:
#endif
D
duke 已提交
465
    sprops.graphics_env = "sun.awt.X11GraphicsEnvironment";
466 467 468 469 470
#ifdef MACOSX
            break;
    }
#endif
    /* AWT properties */
471 472 473
#ifdef JAVASE_EMBEDDED
    sprops.awt_toolkit = getEmbeddedToolkit();
    if (sprops.awt_toolkit == NULL) // default as below
474 475 476 477 478 479 480
#endif
#ifdef MACOSX
        switch (prefToolkit) {
            case CToolkit:
                sprops.awt_toolkit = "sun.lwawt.macosx.LWCToolkit";
                break;
            case XToolkit:
481 482
#endif
    sprops.awt_toolkit = "sun.awt.X11.XToolkit";
483 484 485 486 487 488 489
#ifdef MACOSX
                break;
            default:
                sprops.awt_toolkit = "sun.awt.HToolkit";
                break;
        }
#endif
D
duke 已提交
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516

    /* This is used only for debugging of font problems. */
    v = getenv("JAVA2D_FONTPATH");
    sprops.font_dir = v ? v : NULL;

#ifdef SI_ISALIST
    /* supported instruction sets */
    {
        char list[258];
        sysinfo(SI_ISALIST, list, sizeof(list));
        sprops.cpu_isalist = strdup(list);
    }
#else
    sprops.cpu_isalist = NULL;
#endif

    /* endianness of platform */
    {
        unsigned int endianTest = 0xff000000;
        if (((char*)(&endianTest))[0] != 0)
            sprops.cpu_endian = "big";
        else
            sprops.cpu_endian = "little";
    }

    /* os properties */
    {
517 518 519
#ifdef MACOSX
        setOSNameAndVersion(&sprops);
#else
D
duke 已提交
520 521 522 523
        struct utsname name;
        uname(&name);
        sprops.os_name = strdup(name.sysname);
        sprops.os_version = strdup(name.release);
524
#endif
D
duke 已提交
525 526 527 528 529 530 531 532 533 534 535

        sprops.os_arch = ARCHPROPNAME;

        if (getenv("GNOME_DESKTOP_SESSION_ID") != NULL) {
            sprops.desktop = "gnome";
        }
        else {
            sprops.desktop = NULL;
        }
    }

536 537 538 539 540
    /* ABI property (optional) */
#ifdef JDK_ARCH_ABI_PROP_NAME
    sprops.sun_arch_abi = JDK_ARCH_ABI_PROP_NAME;
#endif

D
duke 已提交
541 542 543
    /* Determine the language, country, variant, and encoding from the host,
     * and store these in the user.language, user.country, user.variant and
     * file.encoding system properties. */
544
    setlocale(LC_ALL, "");
545
    if (ParseLocale(env, LC_CTYPE,
546
                    &(sprops.format_language),
547
                    &(sprops.format_script),
548 549 550
                    &(sprops.format_country),
                    &(sprops.format_variant),
                    &(sprops.encoding))) {
551
        ParseLocale(env, LC_MESSAGES,
552
                    &(sprops.language),
553
                    &(sprops.script),
554 555 556 557 558 559
                    &(sprops.country),
                    &(sprops.variant),
                    NULL);
    } else {
        sprops.language = "en";
        sprops.encoding = "ISO8859-1";
D
duke 已提交
560
    }
561
    sprops.display_language = sprops.language;
562
    sprops.display_script = sprops.script;
563 564
    sprops.display_country = sprops.country;
    sprops.display_variant = sprops.variant;
565 566 567 568

#ifdef MACOSX
    sprops.sun_jnu_encoding = "UTF-8";
#else
569
    sprops.sun_jnu_encoding = sprops.encoding;
570
#endif
D
duke 已提交
571

572 573 574 575 576 577 578
#ifdef _ALLBSD_SOURCE
#if BYTE_ORDER == _LITTLE_ENDIAN
     sprops.unicode_encoding = "UnicodeLittle";
 #else
     sprops.unicode_encoding = "UnicodeBig";
 #endif
#else /* !_ALLBSD_SOURCE */
D
duke 已提交
579 580 581 582 583 584 585 586 587
#ifdef __linux__
#if __BYTE_ORDER == __LITTLE_ENDIAN
    sprops.unicode_encoding = "UnicodeLittle";
#else
    sprops.unicode_encoding = "UnicodeBig";
#endif
#else
    sprops.unicode_encoding = "UnicodeBig";
#endif
588
#endif /* _ALLBSD_SOURCE */
D
duke 已提交
589 590 591 592 593

    /* user properties */
    {
        struct passwd *pwent = getpwuid(getuid());
        sprops.user_name = pwent ? strdup(pwent->pw_name) : "?";
594 595 596 597 598 599 600 601
#ifdef MACOSX
        setUserHome(&sprops);
#else
        sprops.user_home = pwent ? strdup(pwent->pw_dir) : NULL;
#endif
        if (sprops.user_home == NULL) {
            sprops.user_home = "?";
        }
D
duke 已提交
602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
    }

    /* User TIMEZONE */
    {
        /*
         * We defer setting up timezone until it's actually necessary.
         * Refer to TimeZone.getDefault(). However, the system
         * property is necessary to be able to be set by the command
         * line interface -D. Here temporarily set a null string to
         * timezone.
         */
        tzset();        /* for compatibility */
        sprops.timezone = "";
    }

    /* Current directory */
    {
        char buf[MAXPATHLEN];
        errno = 0;
        if (getcwd(buf, sizeof(buf))  == NULL)
            JNU_ThrowByName(env, "java/lang/Error",
             "Properties init: Could not determine current working directory.");
        else
            sprops.user_dir = strdup(buf);
    }

    sprops.file_separator = "/";
    sprops.path_separator = ":";
    sprops.line_separator = "\n";

632
#if !defined(_ALLBSD_SOURCE)
D
duke 已提交
633 634 635 636 637 638
    /* Append CDE message and resource search path to NLSPATH and
     * XFILESEARCHPATH, in order to pick localized message for
     * FileSelectionDialog window (Bug 4173641).
     */
    setPathEnvironment("NLSPATH=/usr/dt/lib/nls/msg/%L/%N.cat");
    setPathEnvironment("XFILESEARCHPATH=/usr/dt/app-defaults/%L/Dt");
639 640 641 642 643 644
#endif


#ifdef MACOSX
    setProxyProperties(&sprops);
#endif
D
duke 已提交
645 646 647

    return &sprops;
}
648 649 650 651 652 653

jstring
GetStringPlatform(JNIEnv *env, nchar* cstr)
{
    return JNU_NewStringPlatform(env, cstr);
}