0N/A/*
3876N/A * Copyright (c) 1998, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
4632N/A#if defined(__linux__) || defined(_ALLBSD_SOURCE)
0N/A#include <stdio.h>
0N/A#include <ctype.h>
0N/A#endif
0N/A#include <pwd.h>
0N/A#include <locale.h>
0N/A#ifndef ARCHPROPNAME
0N/A#error "The macro ARCHPROPNAME has not been defined"
0N/A#endif
0N/A#include <sys/utsname.h> /* For os_name and os_version */
0N/A#include <langinfo.h> /* For nl_langinfo */
0N/A#include <stdlib.h>
0N/A#include <string.h>
0N/A#include <sys/types.h>
0N/A#include <unistd.h>
0N/A#include <sys/param.h>
0N/A#include <time.h>
0N/A#include <errno.h>
0N/A
4632N/A#ifdef MACOSX
4632N/A#include "java_props_macosx.h"
4632N/A#endif
4632N/A
4632N/A#if defined(_ALLBSD_SOURCE)
4632N/A#if !defined(P_tmpdir)
4632N/A#include <paths.h>
4632N/A#define P_tmpdir _PATH_VARTMP
4632N/A#endif
4632N/A#endif
4632N/A
0N/A#include "locale_str.h"
0N/A#include "java_props.h"
0N/A
4632N/A#if !defined(_ALLBSD_SOURCE)
0N/A#ifdef __linux__
2884N/A #ifndef CODESET
2884N/A #define CODESET _NL_CTYPE_CODESET_NAME
2884N/A #endif
0N/A#else
0N/A#ifdef ALT_CODESET_KEY
0N/A#define CODESET ALT_CODESET_KEY
0N/A#endif
0N/A#endif
4632N/A#endif /* !_ALLBSD_SOURCE */
0N/A
3876N/A#ifdef JAVASE_EMBEDDED
3876N/A#include <dlfcn.h>
3876N/A#include <sys/stat.h>
3876N/A#endif
3876N/A
0N/A/* Take an array of string pairs (map of key->value) and a string (key).
0N/A * Examine each pair in the map to see if the first string (key) matches the
0N/A * string. If so, store the second string of the pair (value) in the value and
0N/A * return 1. Otherwise do nothing and return 0. The end of the map is
0N/A * indicated by an empty string at the start of a pair (key of "").
0N/A */
0N/Astatic int
0N/AmapLookup(char* map[], const char* key, char** value) {
0N/A int i;
0N/A for (i = 0; strcmp(map[i], ""); i += 2){
0N/A if (!strcmp(key, map[i])){
0N/A *value = map[i + 1];
0N/A return 1;
0N/A }
0N/A }
0N/A return 0;
0N/A}
0N/A
0N/A/* This function sets an environment variable using envstring.
0N/A * The format of envstring is "name=value".
0N/A * If the name has already existed, it will append value to the name.
0N/A */
0N/Astatic void
0N/AsetPathEnvironment(char *envstring)
0N/A{
0N/A char name[20], *value, *current;
0N/A
0N/A value = strchr(envstring, '='); /* locate name and value separator */
0N/A
0N/A if (! value)
0N/A return; /* not a valid environment setting */
0N/A
0N/A /* copy first part as environment name */
0N/A strncpy(name, envstring, value - envstring);
0N/A name[value-envstring] = '\0';
0N/A
0N/A value++; /* set value point to value of the envstring */
0N/A
0N/A current = getenv(name);
0N/A if (current) {
0N/A if (! strstr(current, value)) {
0N/A /* value is not found in current environment, append it */
0N/A char *temp = malloc(strlen(envstring) + strlen(current) + 2);
0N/A strcpy(temp, name);
0N/A strcat(temp, "=");
0N/A strcat(temp, current);
0N/A strcat(temp, ":");
0N/A strcat(temp, value);
0N/A putenv(temp);
0N/A }
0N/A /* else the value has already been set, do nothing */
0N/A }
0N/A else {
0N/A /* environment variable is not found */
0N/A putenv(envstring);
0N/A }
0N/A}
0N/A
0N/A#ifndef P_tmpdir
0N/A#define P_tmpdir "/var/tmp"
0N/A#endif
0N/A
2976N/Astatic int ParseLocale(int cat, char ** std_language, char ** std_script,
2976N/A char ** std_country, char ** std_variant, char ** std_encoding) {
2700N/A char temp[64];
2700N/A char *language = NULL, *country = NULL, *variant = NULL,
2700N/A *encoding = NULL;
2700N/A char *p, encoding_variant[64];
2700N/A char *lc;
2700N/A
2700N/A /* Query the locale set for the category */
4632N/A
4632N/A#ifdef MACOSX
4632N/A lc = setupMacOSXLocale(cat); // malloc'd memory, need to free
4632N/A#else
2700N/A lc = setlocale(cat, NULL);
4632N/A#endif
2700N/A
2700N/A#ifndef __linux__
2700N/A if (lc == NULL) {
2700N/A return 0;
2700N/A }
2700N/A
2700N/A if (cat == LC_CTYPE) {
2700N/A /*
2700N/A * Workaround for Solaris bug 4201684: Xlib doesn't like @euro
2700N/A * locales. Since we don't depend on the libc @euro behavior,
2700N/A * we just remove the qualifier.
2700N/A * On Linux, the bug doesn't occur; on the other hand, @euro
2700N/A * is needed there because it's a shortcut that also determines
2700N/A * the encoding - without it, we wouldn't get ISO-8859-15.
2700N/A * Therefore, this code section is Solaris-specific.
2700N/A */
2700N/A lc = strdup(lc); /* keep a copy, setlocale trashes original. */
2700N/A strcpy(temp, lc);
2700N/A p = strstr(temp, "@euro");
2700N/A if (p != NULL) {
2700N/A *p = '\0';
2700N/A setlocale(LC_ALL, temp);
2700N/A }
2700N/A }
2700N/A#else
2700N/A if (lc == NULL || !strcmp(lc, "C") || !strcmp(lc, "POSIX")) {
2700N/A lc = "en_US";
2700N/A }
2700N/A#endif
2700N/A
2700N/A /*
2700N/A * locale string format in Solaris is
2700N/A * <language name>_<country name>.<encoding name>@<variant name>
2700N/A * <country name>, <encoding name>, and <variant name> are optional.
2700N/A */
2700N/A
2700N/A strcpy(temp, lc);
4632N/A#ifdef MACOSX
4632N/A free(lc); // malloced memory
4632N/A#endif
2700N/A /* Parse the language, country, encoding, and variant from the
2700N/A * locale. Any of the elements may be missing, but they must occur
2700N/A * in the order language_country.encoding@variant, and must be
2700N/A * preceded by their delimiter (except for language).
2700N/A *
2700N/A * If the locale name (without .encoding@variant, if any) matches
2700N/A * any of the names in the locale_aliases list, map it to the
2700N/A * corresponding full locale name. Most of the entries in the
2700N/A * locale_aliases list are locales that include a language name but
2700N/A * no country name, and this facility is used to map each language
2700N/A * to a default country if that's possible. It's also used to map
2700N/A * the Solaris locale aliases to their proper Java locale IDs.
2700N/A */
2700N/A if ((p = strchr(temp, '.')) != NULL) {
2700N/A strcpy(encoding_variant, p); /* Copy the leading '.' */
2700N/A *p = '\0';
2700N/A } else if ((p = strchr(temp, '@')) != NULL) {
2976N/A strcpy(encoding_variant, p); /* Copy the leading '@' */
2976N/A *p = '\0';
2700N/A } else {
2700N/A *encoding_variant = '\0';
2700N/A }
2700N/A
2700N/A if (mapLookup(locale_aliases, temp, &p)) {
2700N/A strcpy(temp, p);
2976N/A // check the "encoding_variant" again, if any.
2976N/A if ((p = strchr(temp, '.')) != NULL) {
2976N/A strcpy(encoding_variant, p); /* Copy the leading '.' */
2976N/A *p = '\0';
2976N/A } else if ((p = strchr(temp, '@')) != NULL) {
2976N/A strcpy(encoding_variant, p); /* Copy the leading '@' */
2976N/A *p = '\0';
2976N/A }
2700N/A }
2700N/A
2700N/A language = temp;
2700N/A if ((country = strchr(temp, '_')) != NULL) {
2700N/A *country++ = '\0';
2700N/A }
2700N/A
2700N/A p = encoding_variant;
2700N/A if ((encoding = strchr(p, '.')) != NULL) {
2700N/A p[encoding++ - p] = '\0';
2700N/A p = encoding;
2700N/A }
2700N/A if ((variant = strchr(p, '@')) != NULL) {
2700N/A p[variant++ - p] = '\0';
2700N/A }
2700N/A
2700N/A /* Normalize the language name */
2700N/A if (std_language != NULL) {
2700N/A *std_language = "en";
3082N/A if (language != NULL && mapLookup(language_names, language, std_language) == 0) {
3082N/A *std_language = malloc(strlen(language)+1);
3082N/A strcpy(*std_language, language);
2700N/A }
2700N/A }
2700N/A
2700N/A /* Normalize the country name */
2700N/A if (std_country != NULL && country != NULL) {
3082N/A if (mapLookup(country_names, country, std_country) == 0) {
3082N/A *std_country = malloc(strlen(country)+1);
3082N/A strcpy(*std_country, country);
3082N/A }
2700N/A }
2700N/A
2976N/A /* Normalize the script and variant name. Note that we only use
2976N/A * variants listed in the mapping array; others are ignored.
2976N/A */
2976N/A if (variant != NULL) {
2976N/A if (std_script != NULL) {
2976N/A mapLookup(script_names, variant, std_script);
2976N/A }
2976N/A
2976N/A if (std_variant != NULL) {
2976N/A mapLookup(variant_names, variant, std_variant);
2976N/A }
2700N/A }
2700N/A
2700N/A /* Normalize the encoding name. Note that we IGNORE the string
2700N/A * 'encoding' extracted from the locale name above. Instead, we use the
2700N/A * more reliable method of calling nl_langinfo(CODESET). This function
2700N/A * returns an empty string if no encoding is set for the given locale
2700N/A * (e.g., the C or POSIX locales); we use the default ISO 8859-1
2700N/A * converter for such locales.
2700N/A */
2700N/A if (std_encoding != NULL) {
2700N/A /* OK, not so reliable - nl_langinfo() gives wrong answers on
2700N/A * Euro locales, in particular. */
2700N/A if (strcmp(p, "ISO8859-15") == 0)
2700N/A p = "ISO8859-15";
2700N/A else
2700N/A p = nl_langinfo(CODESET);
2700N/A
2700N/A /* Convert the bare "646" used on Solaris to a proper IANA name */
2700N/A if (strcmp(p, "646") == 0)
2700N/A p = "ISO646-US";
2700N/A
2700N/A /* return same result nl_langinfo would return for en_UK,
2700N/A * in order to use optimizations. */
2700N/A *std_encoding = (*p != '\0') ? p : "ISO8859-1";
2700N/A
2700N/A#ifdef __linux__
2700N/A /*
2700N/A * Remap the encoding string to a different value for japanese
2700N/A * locales on linux so that customized converters are used instead
2700N/A * of the default converter for "EUC-JP". The customized converters
2700N/A * omit support for the JIS0212 encoding which is not supported by
2700N/A * the variant of "EUC-JP" encoding used on linux
2700N/A */
2700N/A if (strcmp(p, "EUC-JP") == 0) {
2700N/A *std_encoding = "EUC-JP-LINUX";
2700N/A }
2700N/A#else
2700N/A if (strcmp(p,"eucJP") == 0) {
2700N/A /* For Solaris use customized vendor defined character
2700N/A * customized EUC-JP converter
2700N/A */
2700N/A *std_encoding = "eucJP-open";
2700N/A } else if (strcmp(p, "Big5") == 0 || strcmp(p, "BIG5") == 0) {
2700N/A /*
2700N/A * Remap the encoding string to Big5_Solaris which augments
2700N/A * the default converter for Solaris Big5 locales to include
2700N/A * seven additional ideographic characters beyond those included
2700N/A * in the Java "Big5" converter.
2700N/A */
2700N/A *std_encoding = "Big5_Solaris";
2700N/A } else if (strcmp(p, "Big5-HKSCS") == 0) {
2700N/A /*
2700N/A * Solaris uses HKSCS2001
2700N/A */
2700N/A *std_encoding = "Big5-HKSCS-2001";
2700N/A }
2700N/A#endif
2700N/A }
2700N/A
2700N/A return 1;
2700N/A}
2700N/A
3876N/A#ifdef JAVASE_EMBEDDED
3876N/A/* Determine the default embedded toolkit based on whether lib/xawt/
3876N/A * exists in the JRE. This can still be overridden by -Dawt.toolkit=XXX
3876N/A */
3876N/Astatic char* getEmbeddedToolkit() {
3876N/A Dl_info dlinfo;
3876N/A char buf[MAXPATHLEN];
3876N/A int32_t len;
3876N/A char *p;
3876N/A struct stat statbuf;
3876N/A
3876N/A /* Get address of this library and the directory containing it. */
3876N/A dladdr((void *)getEmbeddedToolkit, &dlinfo);
3876N/A realpath((char *)dlinfo.dli_fname, buf);
3876N/A len = strlen(buf);
3876N/A p = strrchr(buf, '/');
3876N/A /* Default AWT Toolkit on Linux and Solaris is XAWT. */
3876N/A strncpy(p, "/xawt/", MAXPATHLEN-len-1);
3876N/A /* Check if it exists */
3876N/A if (stat(buf, &statbuf) == -1 && errno == ENOENT) {
3876N/A /* No - this is a reduced-headless-jre so use special HToolkit */
3876N/A return "sun.awt.HToolkit";
3876N/A }
3876N/A else {
3876N/A /* Yes - this is a headful JRE so fallback to SE defaults */
3876N/A return NULL;
3876N/A }
3876N/A}
3876N/A#endif
3876N/A
0N/A/* This function gets called very early, before VM_CALLS are setup.
0N/A * Do not use any of the VM_CALLS entries!!!
0N/A */
0N/Ajava_props_t *
0N/AGetJavaProperties(JNIEnv *env)
0N/A{
2884N/A static java_props_t sprops;
0N/A char *v; /* tmp var */
0N/A
0N/A if (sprops.user_dir) {
0N/A return &sprops;
0N/A }
0N/A
0N/A /* tmp dir */
0N/A sprops.tmp_dir = P_tmpdir;
4632N/A#ifdef MACOSX
4632N/A /* darwin has a per-user temp dir */
4632N/A static char tmp_path[PATH_MAX];
4632N/A int pathSize = confstr(_CS_DARWIN_USER_TEMP_DIR, tmp_path, PATH_MAX);
4632N/A if (pathSize > 0 && pathSize <= PATH_MAX) {
4632N/A sprops.tmp_dir = tmp_path;
4632N/A }
4632N/A#endif /* MACOSX */
0N/A
0N/A /* Printing properties */
4632N/A#ifdef MACOSX
4632N/A sprops.printerJob = "sun.lwawt.macosx.CPrinterJob";
4632N/A#else
0N/A sprops.printerJob = "sun.print.PSPrinterJob";
4632N/A#endif
0N/A
0N/A /* patches/service packs installed */
0N/A sprops.patch_level = "unknown";
0N/A
0N/A /* Java 2D properties */
4632N/A#ifdef MACOSX
4632N/A PreferredToolkit prefToolkit = getPreferredToolkit();
4632N/A switch (prefToolkit) {
4632N/A case CToolkit:
5214N/A case HToolkit:
4632N/A sprops.graphics_env = "sun.awt.CGraphicsEnvironment";
4632N/A break;
4632N/A case XToolkit:
4632N/A#endif
0N/A sprops.graphics_env = "sun.awt.X11GraphicsEnvironment";
4632N/A#ifdef MACOSX
4632N/A break;
4632N/A }
4632N/A#endif
4632N/A /* AWT properties */
3876N/A#ifdef JAVASE_EMBEDDED
3876N/A sprops.awt_toolkit = getEmbeddedToolkit();
3876N/A if (sprops.awt_toolkit == NULL) // default as below
3876N/A#endif
4632N/A#ifdef MACOSX
4632N/A switch (prefToolkit) {
4632N/A case CToolkit:
4632N/A sprops.awt_toolkit = "sun.lwawt.macosx.LWCToolkit";
4632N/A break;
4632N/A case XToolkit:
4632N/A#endif
3876N/A sprops.awt_toolkit = "sun.awt.X11.XToolkit";
4632N/A#ifdef MACOSX
4632N/A break;
4632N/A default:
4642N/A sprops.awt_toolkit = "sun.awt.HToolkit";
4632N/A break;
4632N/A }
4632N/A#endif
0N/A
0N/A /* This is used only for debugging of font problems. */
0N/A v = getenv("JAVA2D_FONTPATH");
0N/A sprops.font_dir = v ? v : NULL;
0N/A
0N/A#ifdef SI_ISALIST
0N/A /* supported instruction sets */
0N/A {
0N/A char list[258];
0N/A sysinfo(SI_ISALIST, list, sizeof(list));
0N/A sprops.cpu_isalist = strdup(list);
0N/A }
0N/A#else
0N/A sprops.cpu_isalist = NULL;
0N/A#endif
0N/A
0N/A /* endianness of platform */
0N/A {
0N/A unsigned int endianTest = 0xff000000;
0N/A if (((char*)(&endianTest))[0] != 0)
0N/A sprops.cpu_endian = "big";
0N/A else
0N/A sprops.cpu_endian = "little";
0N/A }
0N/A
0N/A /* os properties */
0N/A {
4632N/A#ifdef MACOSX
4632N/A setOSNameAndVersion(&sprops);
4632N/A#else
0N/A struct utsname name;
0N/A uname(&name);
0N/A sprops.os_name = strdup(name.sysname);
0N/A sprops.os_version = strdup(name.release);
4632N/A#endif
0N/A
0N/A sprops.os_arch = ARCHPROPNAME;
0N/A
0N/A if (getenv("GNOME_DESKTOP_SESSION_ID") != NULL) {
0N/A sprops.desktop = "gnome";
0N/A }
0N/A else {
0N/A sprops.desktop = NULL;
0N/A }
0N/A }
0N/A
0N/A /* Determine the language, country, variant, and encoding from the host,
0N/A * and store these in the user.language, user.country, user.variant and
0N/A * file.encoding system properties. */
2700N/A setlocale(LC_ALL, "");
2700N/A if (ParseLocale(LC_CTYPE,
2700N/A &(sprops.format_language),
2976N/A &(sprops.format_script),
2700N/A &(sprops.format_country),
2700N/A &(sprops.format_variant),
2700N/A &(sprops.encoding))) {
2700N/A ParseLocale(LC_MESSAGES,
2700N/A &(sprops.language),
2976N/A &(sprops.script),
2700N/A &(sprops.country),
2700N/A &(sprops.variant),
2700N/A NULL);
2700N/A } else {
2700N/A sprops.language = "en";
2700N/A sprops.encoding = "ISO8859-1";
0N/A }
2700N/A sprops.display_language = sprops.language;
2976N/A sprops.display_script = sprops.script;
2700N/A sprops.display_country = sprops.country;
2700N/A sprops.display_variant = sprops.variant;
5859N/A
5859N/A#ifdef MACOSX
5859N/A sprops.sun_jnu_encoding = "UTF-8";
5859N/A#else
2700N/A sprops.sun_jnu_encoding = sprops.encoding;
5859N/A#endif
0N/A
4632N/A#ifdef _ALLBSD_SOURCE
4632N/A#if BYTE_ORDER == _LITTLE_ENDIAN
4632N/A sprops.unicode_encoding = "UnicodeLittle";
4632N/A #else
4632N/A sprops.unicode_encoding = "UnicodeBig";
4632N/A #endif
4632N/A#else /* !_ALLBSD_SOURCE */
0N/A#ifdef __linux__
0N/A#if __BYTE_ORDER == __LITTLE_ENDIAN
0N/A sprops.unicode_encoding = "UnicodeLittle";
0N/A#else
0N/A sprops.unicode_encoding = "UnicodeBig";
0N/A#endif
0N/A#else
0N/A sprops.unicode_encoding = "UnicodeBig";
0N/A#endif
4632N/A#endif /* _ALLBSD_SOURCE */
0N/A
0N/A /* user properties */
0N/A {
0N/A struct passwd *pwent = getpwuid(getuid());
0N/A sprops.user_name = pwent ? strdup(pwent->pw_name) : "?";
0N/A sprops.user_home = pwent ? strdup(pwent->pw_dir) : "?";
0N/A }
0N/A
0N/A /* User TIMEZONE */
0N/A {
0N/A /*
0N/A * We defer setting up timezone until it's actually necessary.
0N/A * Refer to TimeZone.getDefault(). However, the system
0N/A * property is necessary to be able to be set by the command
0N/A * line interface -D. Here temporarily set a null string to
0N/A * timezone.
0N/A */
0N/A tzset(); /* for compatibility */
0N/A sprops.timezone = "";
0N/A }
0N/A
0N/A /* Current directory */
0N/A {
0N/A char buf[MAXPATHLEN];
0N/A errno = 0;
0N/A if (getcwd(buf, sizeof(buf)) == NULL)
0N/A JNU_ThrowByName(env, "java/lang/Error",
0N/A "Properties init: Could not determine current working directory.");
0N/A else
0N/A sprops.user_dir = strdup(buf);
0N/A }
0N/A
0N/A sprops.file_separator = "/";
0N/A sprops.path_separator = ":";
0N/A sprops.line_separator = "\n";
0N/A
4632N/A#if !defined(_ALLBSD_SOURCE)
0N/A /* Append CDE message and resource search path to NLSPATH and
0N/A * XFILESEARCHPATH, in order to pick localized message for
0N/A * FileSelectionDialog window (Bug 4173641).
0N/A */
0N/A setPathEnvironment("NLSPATH=/usr/dt/lib/nls/msg/%L/%N.cat");
0N/A setPathEnvironment("XFILESEARCHPATH=/usr/dt/app-defaults/%L/Dt");
4632N/A#endif
4632N/A
4632N/A
4632N/A#ifdef MACOSX
4632N/A setProxyProperties(&sprops);
4632N/A#endif
0N/A
0N/A return &sprops;
0N/A}
2247N/A
2247N/Ajstring
2247N/AGetStringPlatform(JNIEnv *env, nchar* cstr)
2247N/A{
2247N/A return JNU_NewStringPlatform(env, cstr);
2247N/A}