1 /*
   2  * Copyright (c) 1998, 2006, 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 #ifdef __linux__
  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 #include "locale_str.h"
  46 #include "java_props.h"
  47 
  48 #ifdef __linux__
  49   #ifndef CODESET
  50   #define CODESET _NL_CTYPE_CODESET_NAME
  51   #endif
  52 #else
  53 #ifdef ALT_CODESET_KEY
  54 #define CODESET ALT_CODESET_KEY
  55 #endif
  56 #endif
  57 
  58 /* Take an array of string pairs (map of key->value) and a string (key).
  59  * Examine each pair in the map to see if the first string (key) matches the
  60  * string.  If so, store the second string of the pair (value) in the value and
  61  * return 1.  Otherwise do nothing and return 0.  The end of the map is
  62  * indicated by an empty string at the start of a pair (key of "").
  63  */
  64 static int
  65 mapLookup(char* map[], const char* key, char** value) {
  66     int i;
  67     for (i = 0; strcmp(map[i], ""); i += 2){
  68         if (!strcmp(key, map[i])){
  69             *value = map[i + 1];
  70             return 1;
  71         }
  72     }
  73     return 0;
  74 }
  75 
  76 /* This function sets an environment variable using envstring.
  77  * The format of envstring is "name=value".
  78  * If the name has already existed, it will append value to the name.
  79  */
  80 static void
  81 setPathEnvironment(char *envstring)
  82 {
  83     char name[20], *value, *current;
  84 
  85     value = strchr(envstring, '='); /* locate name and value separator */
  86 
  87     if (! value)
  88         return; /* not a valid environment setting */
  89 
  90     /* copy first part as environment name */
  91     strncpy(name, envstring, value - envstring);
  92     name[value-envstring] = '\0';
  93 
  94     value++; /* set value point to value of the envstring */
  95 
  96     current = getenv(name);
  97     if (current) {
  98         if (! strstr(current, value)) {
  99             /* value is not found in current environment, append it */
 100             char *temp = malloc(strlen(envstring) + strlen(current) + 2);
 101         strcpy(temp, name);
 102         strcat(temp, "=");
 103         strcat(temp, current);
 104         strcat(temp, ":");
 105         strcat(temp, value);
 106         putenv(temp);
 107         }
 108         /* else the value has already been set, do nothing */
 109     }
 110     else {
 111         /* environment variable is not found */
 112         putenv(envstring);
 113     }
 114 }
 115 
 116 #ifndef P_tmpdir
 117 #define P_tmpdir "/var/tmp"
 118 #endif
 119 
 120 static int ParseLocale(int cat, char ** std_language, char ** std_script,
 121                        char ** std_country, char ** std_variant, char ** std_encoding) {
 122     char temp[64];
 123     char *language = NULL, *country = NULL, *variant = NULL,
 124          *encoding = NULL;
 125     char *p, encoding_variant[64];
 126     char *lc;
 127 
 128     /* Query the locale set for the category */
 129     lc = setlocale(cat, NULL);
 130 
 131 #ifndef __linux__
 132     if (lc == NULL) {
 133         return 0;
 134     }
 135 
 136     if (cat == LC_CTYPE) {
 137         /*
 138          * Workaround for Solaris bug 4201684: Xlib doesn't like @euro
 139          * locales. Since we don't depend on the libc @euro behavior,
 140          * we just remove the qualifier.
 141          * On Linux, the bug doesn't occur; on the other hand, @euro
 142          * is needed there because it's a shortcut that also determines
 143          * the encoding - without it, we wouldn't get ISO-8859-15.
 144          * Therefore, this code section is Solaris-specific.
 145          */
 146         lc = strdup(lc);    /* keep a copy, setlocale trashes original. */
 147         strcpy(temp, lc);
 148         p = strstr(temp, "@euro");
 149         if (p != NULL) {
 150             *p = '\0';
 151             setlocale(LC_ALL, temp);
 152         }
 153     }
 154 #else
 155     if (lc == NULL || !strcmp(lc, "C") || !strcmp(lc, "POSIX")) {
 156         lc = "en_US";
 157     }
 158 #endif
 159 
 160     /*
 161      * locale string format in Solaris is
 162      * <language name>_<country name>.<encoding name>@<variant name>
 163      * <country name>, <encoding name>, and <variant name> are optional.
 164      */
 165 
 166     strcpy(temp, lc);
 167 
 168     /* Parse the language, country, encoding, and variant from the
 169      * locale.  Any of the elements may be missing, but they must occur
 170      * in the order language_country.encoding@variant, and must be
 171      * preceded by their delimiter (except for language).
 172      *
 173      * If the locale name (without .encoding@variant, if any) matches
 174      * any of the names in the locale_aliases list, map it to the
 175      * corresponding full locale name.  Most of the entries in the
 176      * locale_aliases list are locales that include a language name but
 177      * no country name, and this facility is used to map each language
 178      * to a default country if that's possible.  It's also used to map
 179      * the Solaris locale aliases to their proper Java locale IDs.
 180      */
 181     if ((p = strchr(temp, '.')) != NULL) {
 182         strcpy(encoding_variant, p); /* Copy the leading '.' */
 183         *p = '\0';
 184     } else if ((p = strchr(temp, '@')) != NULL) {
 185         strcpy(encoding_variant, p); /* Copy the leading '@' */
 186         *p = '\0';
 187     } else {
 188         *encoding_variant = '\0';
 189     }
 190 
 191     if (mapLookup(locale_aliases, temp, &p)) {
 192         strcpy(temp, p);
 193         // check the "encoding_variant" again, if any.
 194         if ((p = strchr(temp, '.')) != NULL) {
 195             strcpy(encoding_variant, p); /* Copy the leading '.' */
 196             *p = '\0';
 197         } else if ((p = strchr(temp, '@')) != NULL) {
 198             strcpy(encoding_variant, p); /* Copy the leading '@' */
 199             *p = '\0';
 200         }
 201     }
 202 
 203     language = temp;
 204     if ((country = strchr(temp, '_')) != NULL) {
 205         *country++ = '\0';
 206     }
 207 
 208     p = encoding_variant;
 209     if ((encoding = strchr(p, '.')) != NULL) {
 210         p[encoding++ - p] = '\0';
 211         p = encoding;
 212     }
 213     if ((variant = strchr(p, '@')) != NULL) {
 214         p[variant++ - p] = '\0';
 215     }
 216 
 217     /* Normalize the language name */
 218     if (std_language != NULL) {
 219         *std_language = "en";
 220         if (language != NULL && mapLookup(language_names, language, std_language) == 0) {
 221             *std_language = malloc(strlen(language)+1);
 222             strcpy(*std_language, language);
 223         }
 224     }
 225 
 226     /* Normalize the country name */
 227     if (std_country != NULL && country != NULL) {
 228         if (mapLookup(country_names, country, std_country) == 0) {
 229             *std_country = malloc(strlen(country)+1);
 230             strcpy(*std_country, country);
 231         }
 232     }
 233 
 234     /* Normalize the script and variant name.  Note that we only use
 235      * variants listed in the mapping array; others are ignored.
 236      */
 237     if (variant != NULL) {
 238         if (std_script != NULL) {
 239             mapLookup(script_names, variant, std_script);
 240         }
 241 
 242         if (std_variant != NULL) {
 243             mapLookup(variant_names, variant, std_variant);
 244         }
 245     }
 246 
 247     /* Normalize the encoding name.  Note that we IGNORE the string
 248      * 'encoding' extracted from the locale name above.  Instead, we use the
 249      * more reliable method of calling nl_langinfo(CODESET).  This function
 250      * returns an empty string if no encoding is set for the given locale
 251      * (e.g., the C or POSIX locales); we use the default ISO 8859-1
 252      * converter for such locales.
 253      */
 254     if (std_encoding != NULL) {
 255         /* OK, not so reliable - nl_langinfo() gives wrong answers on
 256          * Euro locales, in particular. */
 257         if (strcmp(p, "ISO8859-15") == 0)
 258             p = "ISO8859-15";
 259         else
 260             p = nl_langinfo(CODESET);
 261 
 262         /* Convert the bare "646" used on Solaris to a proper IANA name */
 263         if (strcmp(p, "646") == 0)
 264             p = "ISO646-US";
 265 
 266         /* return same result nl_langinfo would return for en_UK,
 267          * in order to use optimizations. */
 268         *std_encoding = (*p != '\0') ? p : "ISO8859-1";
 269 
 270 #ifdef __linux__
 271         /*
 272          * Remap the encoding string to a different value for japanese
 273          * locales on linux so that customized converters are used instead
 274          * of the default converter for "EUC-JP". The customized converters
 275          * omit support for the JIS0212 encoding which is not supported by
 276          * the variant of "EUC-JP" encoding used on linux
 277          */
 278         if (strcmp(p, "EUC-JP") == 0) {
 279             *std_encoding = "EUC-JP-LINUX";
 280         }
 281 #else
 282         if (strcmp(p,"eucJP") == 0) {
 283             /* For Solaris use customized vendor defined character
 284              * customized EUC-JP converter
 285              */
 286             *std_encoding = "eucJP-open";
 287         } else if (strcmp(p, "Big5") == 0 || strcmp(p, "BIG5") == 0) {
 288             /*
 289              * Remap the encoding string to Big5_Solaris which augments
 290              * the default converter for Solaris Big5 locales to include
 291              * seven additional ideographic characters beyond those included
 292              * in the Java "Big5" converter.
 293              */
 294             *std_encoding = "Big5_Solaris";
 295         } else if (strcmp(p, "Big5-HKSCS") == 0) {
 296             /*
 297              * Solaris uses HKSCS2001
 298              */
 299             *std_encoding = "Big5-HKSCS-2001";
 300         }
 301 #endif
 302     }
 303 
 304     return 1;
 305 }
 306 
 307 /* This function gets called very early, before VM_CALLS are setup.
 308  * Do not use any of the VM_CALLS entries!!!
 309  */
 310 java_props_t *
 311 GetJavaProperties(JNIEnv *env)
 312 {
 313     static java_props_t sprops;
 314     char *v; /* tmp var */
 315 
 316     if (sprops.user_dir) {
 317         return &sprops;
 318     }
 319 
 320     /* tmp dir */
 321     sprops.tmp_dir = P_tmpdir;
 322 
 323     /* Printing properties */
 324     sprops.printerJob = "sun.print.PSPrinterJob";
 325 
 326     /* patches/service packs installed */
 327     sprops.patch_level = "unknown";
 328 
 329     /* Java 2D properties */
 330     sprops.graphics_env = "sun.awt.X11GraphicsEnvironment";
 331     sprops.awt_toolkit = NULL;
 332 
 333     /* This is used only for debugging of font problems. */
 334     v = getenv("JAVA2D_FONTPATH");
 335     sprops.font_dir = v ? v : NULL;
 336 
 337 #ifdef SI_ISALIST
 338     /* supported instruction sets */
 339     {
 340         char list[258];
 341         sysinfo(SI_ISALIST, list, sizeof(list));
 342         sprops.cpu_isalist = strdup(list);
 343     }
 344 #else
 345     sprops.cpu_isalist = NULL;
 346 #endif
 347 
 348     /* endianness of platform */
 349     {
 350         unsigned int endianTest = 0xff000000;
 351         if (((char*)(&endianTest))[0] != 0)
 352             sprops.cpu_endian = "big";
 353         else
 354             sprops.cpu_endian = "little";
 355     }
 356 
 357     /* os properties */
 358     {
 359         struct utsname name;
 360         uname(&name);
 361         sprops.os_name = strdup(name.sysname);
 362         sprops.os_version = strdup(name.release);
 363 
 364         sprops.os_arch = ARCHPROPNAME;
 365 
 366         if (getenv("GNOME_DESKTOP_SESSION_ID") != NULL) {
 367             sprops.desktop = "gnome";
 368         }
 369         else {
 370             sprops.desktop = NULL;
 371         }
 372     }
 373 
 374     /* Determine the language, country, variant, and encoding from the host,
 375      * and store these in the user.language, user.country, user.variant and
 376      * file.encoding system properties. */
 377     setlocale(LC_ALL, "");
 378     if (ParseLocale(LC_CTYPE,
 379                     &(sprops.format_language),
 380                     &(sprops.format_script),
 381                     &(sprops.format_country),
 382                     &(sprops.format_variant),
 383                     &(sprops.encoding))) {
 384         ParseLocale(LC_MESSAGES,
 385                     &(sprops.language),
 386                     &(sprops.script),
 387                     &(sprops.country),
 388                     &(sprops.variant),
 389                     NULL);
 390     } else {
 391         sprops.language = "en";
 392         sprops.encoding = "ISO8859-1";
 393     }
 394     sprops.display_language = sprops.language;
 395     sprops.display_script = sprops.script;
 396     sprops.display_country = sprops.country;
 397     sprops.display_variant = sprops.variant;
 398     sprops.sun_jnu_encoding = sprops.encoding;
 399 
 400 #ifdef __linux__
 401 #if __BYTE_ORDER == __LITTLE_ENDIAN
 402     sprops.unicode_encoding = "UnicodeLittle";
 403 #else
 404     sprops.unicode_encoding = "UnicodeBig";
 405 #endif
 406 #else
 407     sprops.unicode_encoding = "UnicodeBig";
 408 #endif
 409 
 410     /* user properties */
 411     {
 412         struct passwd *pwent = getpwuid(getuid());
 413         sprops.user_name = pwent ? strdup(pwent->pw_name) : "?";
 414         sprops.user_home = pwent ? strdup(pwent->pw_dir) : "?";
 415     }
 416 
 417     /* User TIMEZONE */
 418     {
 419         /*
 420          * We defer setting up timezone until it's actually necessary.
 421          * Refer to TimeZone.getDefault(). However, the system
 422          * property is necessary to be able to be set by the command
 423          * line interface -D. Here temporarily set a null string to
 424          * timezone.
 425          */
 426         tzset();        /* for compatibility */
 427         sprops.timezone = "";
 428     }
 429 
 430     /* Current directory */
 431     {
 432         char buf[MAXPATHLEN];
 433         errno = 0;
 434         if (getcwd(buf, sizeof(buf))  == NULL)
 435             JNU_ThrowByName(env, "java/lang/Error",
 436              "Properties init: Could not determine current working directory.");
 437         else
 438             sprops.user_dir = strdup(buf);
 439     }
 440 
 441     sprops.file_separator = "/";
 442     sprops.path_separator = ":";
 443     sprops.line_separator = "\n";
 444 
 445     /* Append CDE message and resource search path to NLSPATH and
 446      * XFILESEARCHPATH, in order to pick localized message for
 447      * FileSelectionDialog window (Bug 4173641).
 448      */
 449     setPathEnvironment("NLSPATH=/usr/dt/lib/nls/msg/%L/%N.cat");
 450     setPathEnvironment("XFILESEARCHPATH=/usr/dt/app-defaults/%L/Dt");
 451 
 452     return &sprops;
 453 }
 454 
 455 jstring
 456 GetStringPlatform(JNIEnv *env, nchar* cstr)
 457 {
 458     return JNU_NewStringPlatform(env, cstr);
 459 }