Home | History | Annotate | Download | only in icuinfo
      1 /*
      2 *******************************************************************************
      3 *
      4 *   Copyright (C) 1999-2010, International Business Machines
      5 *   Corporation and others.  All Rights Reserved.
      6 *
      7 *******************************************************************************
      8 *   file name:  icuinfo.cpp
      9 *   encoding:   US-ASCII
     10 *   tab size:   8 (not used)
     11 *   indentation:4
     12 *
     13 *   created on: 2009-2010
     14 *   created by: Steven R. Loomis
     15 *
     16 *   This program shows some basic info about the current ICU.
     17 */
     18 
     19 #include <stdio.h>
     20 #include <stdlib.h>
     21 #include "unicode/utypes.h"
     22 #include "unicode/putil.h"
     23 #include "unicode/uclean.h"
     24 #include "unicode/udbgutil.h"
     25 #include "unewdata.h"
     26 #include "cmemory.h"
     27 #include "cstring.h"
     28 #include "uoptions.h"
     29 #include "toolutil.h"
     30 #include "icuplugimp.h"
     31 #include <unicode/uloc.h>
     32 #include <unicode/ucnv.h>
     33 #include "unicode/ucal.h"
     34 #include <unicode/ulocdata.h>
     35 #include "putilimp.h"
     36 #include "unicode/uchar.h"
     37 
     38 static UOption options[]={
     39   /*0*/ UOPTION_HELP_H,
     40   /*1*/ UOPTION_HELP_QUESTION_MARK,
     41   /*2*/ UOPTION_ICUDATADIR,
     42   /*3*/ UOPTION_VERBOSE,
     43   /*4*/ UOPTION_DEF("list-plugins", 'L', UOPT_NO_ARG),
     44   /*5*/ UOPTION_DEF("milisecond-time", 'm', UOPT_NO_ARG),
     45   /*6*/ UOPTION_DEF("cleanup", 'K', UOPT_NO_ARG),
     46 };
     47 
     48 static UErrorCode initStatus = U_ZERO_ERROR;
     49 static UBool icuInitted = FALSE;
     50 
     51 static void do_init() {
     52     if(!icuInitted) {
     53       u_init(&initStatus);
     54       icuInitted = TRUE;
     55     }
     56 }
     57 
     58 /**
     59  * Print the current platform
     60  */
     61 static const char *getPlatform()
     62 {
     63 #if defined(U_PLATFORM)
     64 	return U_PLATFORM;
     65 #elif defined(U_WINDOWS)
     66 	return "Windows";
     67 #elif defined(U_PALMOS)
     68 	return "PalmOS";
     69 #elif defined(_PLATFORM_H)
     70 	return "Other (POSIX-like)";
     71 #else
     72 	return "unknown"
     73 #endif
     74 }
     75 
     76 void cmd_millis()
     77 {
     78   printf("Milliseconds since Epoch: %.0f\n", uprv_getUTCtime());
     79 }
     80 
     81 void cmd_version(UBool noLoad)
     82 {
     83     UVersionInfo icu;
     84     UErrorCode status = U_ZERO_ERROR;
     85     char str[200];
     86     printf("<ICUINFO>\n");
     87     printf("International Components for Unicode for C/C++\n");
     88     printf("%s\n", U_COPYRIGHT_STRING);
     89     printf("Compiled-Version: %s\n", U_ICU_VERSION);
     90     u_getVersion(icu);
     91     u_versionToString(icu, str);
     92     printf("Runtime-Version: %s\n", str);
     93     printf("Compiled-Unicode-Version: %s\n", U_UNICODE_VERSION);
     94     u_getUnicodeVersion(icu);
     95     u_versionToString(icu, str);
     96     printf("Runtime-Unicode-Version: %s\n", U_UNICODE_VERSION);
     97     printf("Platform: %s\n", getPlatform());
     98 #if defined(U_BUILD)
     99     printf("Build: %s\n", U_BUILD);
    100 #if defined(U_HOST)
    101     if(strcmp(U_BUILD,U_HOST)) {
    102       printf("Host: %s\n", U_HOST);
    103     }
    104 #endif
    105 #endif
    106 #if defined(U_CC)
    107     printf("C compiler: %s\n", U_CC);
    108 #endif
    109 #if defined(U_CXX)
    110     printf("C++ compiler: %s\n", U_CXX);
    111 #endif
    112 #if defined(CYGWINMSVC)
    113     printf("Cygwin: CYGWINMSVC\n");
    114 #endif
    115     printf("ICUDATA: %s\n", U_ICUDATA_NAME);
    116     do_init();
    117     printf("Data Directory: %s\n", u_getDataDirectory());
    118     printf("ICU Initialization returned: %s\n", u_errorName(initStatus));
    119     printf( "Default locale: %s\n", uloc_getDefault());
    120     {
    121       UErrorCode subStatus = U_ZERO_ERROR;
    122       ulocdata_getCLDRVersion(icu, &subStatus);
    123       if(U_SUCCESS(subStatus)) {
    124 	u_versionToString(icu, str);
    125 	printf("CLDR-Version: %s\n", str);
    126       } else {
    127 	printf("CLDR-Version: %s\n", u_errorName(subStatus));
    128       }
    129     }
    130 
    131 #if !UCONFIG_NO_CONVERSION
    132     if(noLoad == FALSE)
    133     {
    134       printf("Default converter: %s\n", ucnv_getDefaultName());
    135     }
    136 #endif
    137 #if !UCONFIG_NO_FORMATTING
    138     {
    139       UChar buf[100];
    140       char buf2[100];
    141       UErrorCode subsubStatus= U_ZERO_ERROR;
    142       int32_t len;
    143 
    144       len = ucal_getDefaultTimeZone(buf, 100, &subsubStatus);
    145       if(U_SUCCESS(subsubStatus)&&len>0) {
    146 	u_UCharsToChars(buf, buf2, len+1);
    147 	printf("Default TZ: %s\n", buf2);
    148       } else {
    149 	printf("Default TZ: %s\n", u_errorName(subsubStatus));
    150       }
    151     }
    152     {
    153       UErrorCode subStatus = U_ZERO_ERROR;
    154       const char *tzVer = ucal_getTZDataVersion(&subStatus);
    155       if(U_FAILURE(subStatus)) {
    156 	tzVer = u_errorName(subStatus);
    157       }
    158       printf("TZ data version: %s\n", tzVer);
    159     }
    160 #endif
    161 
    162 #if U_ENABLE_DYLOAD
    163     const char *pluginFile = uplug_getPluginFile();
    164     printf("Plugin file is: %s\n", (pluginFile&&*pluginFile)?pluginFile:"(not set. try setting ICU_PLUGINS to a directory.)");
    165 #else
    166     fprintf(stderr, "Dynamic Loading: is disabled. No plugins will be loaded at start-up.\n");
    167 #endif
    168     printf("</ICUINFO>\n\n");
    169 }
    170 
    171 void cmd_cleanup()
    172 {
    173     u_cleanup();
    174     fprintf(stderr,"ICU u_cleanup() called.\n");
    175 }
    176 
    177 
    178 void cmd_listplugins() {
    179     int32_t i;
    180     UPlugData *plug;
    181 
    182     do_init();
    183     printf("ICU Initialized: u_init() returned %s\n", u_errorName(initStatus));
    184 
    185     printf("Plugins: \n");
    186     printf(    "# %6s   %s \n",
    187                        "Level",
    188                        "Name" );
    189     printf(    "    %10s:%-10s\n",
    190                        "Library",
    191                        "Symbol"
    192             );
    193 
    194 
    195     printf(    "       config| (configuration string)\n");
    196     printf(    " >>>   Error          | Explanation \n");
    197     printf(    "-----------------------------------\n");
    198 
    199     for(i=0;(plug=uplug_getPlugInternal(i))!=NULL;i++) {
    200         UErrorCode libStatus = U_ZERO_ERROR;
    201         const char *name = uplug_getPlugName(plug);
    202         const char *sym = uplug_getSymbolName(plug);
    203         const char *lib = uplug_getLibraryName(plug, &libStatus);
    204         const char *config = uplug_getConfiguration(plug);
    205         UErrorCode loadStatus = uplug_getPlugLoadStatus(plug);
    206         const char *message = NULL;
    207 
    208         printf("\n#%d  %-6s %s \n",
    209             i+1,
    210             udbg_enumName(UDBG_UPlugLevel,(int32_t)uplug_getPlugLevel(plug)),
    211             name!=NULL?(*name?name:"this plugin did not call uplug_setPlugName()"):"(null)"
    212         );
    213         printf("    plugin| %10s:%-10s\n",
    214             (U_SUCCESS(libStatus)?(lib!=NULL?lib:"(null)"):u_errorName(libStatus)),
    215             sym!=NULL?sym:"(null)"
    216         );
    217 
    218         if(config!=NULL&&*config) {
    219             printf("    config| %s\n", config);
    220         }
    221 
    222         switch(loadStatus) {
    223             case U_PLUGIN_CHANGED_LEVEL_WARNING:
    224                 message = "Note: This plugin changed the system level (by allocating memory or calling something which does). Later plugins may not load.";
    225                 break;
    226 
    227             case U_PLUGIN_DIDNT_SET_LEVEL:
    228                 message = "Error: This plugin did not call uplug_setPlugLevel during QUERY.";
    229                 break;
    230 
    231             case U_PLUGIN_TOO_HIGH:
    232                 message = "Error: This plugin couldn't load because the system level was too high. Try loading this plugin earlier.";
    233                 break;
    234 
    235             case U_ZERO_ERROR:
    236                 message = NULL; /* no message */
    237                 break;
    238             default:
    239                 if(U_FAILURE(loadStatus)) {
    240                     message = "error loading:";
    241                 } else {
    242                     message = "warning during load:";
    243                 }
    244         }
    245 
    246         if(message!=NULL) {
    247             printf("\\\\\\ status| %s\n"
    248                    "/// %s\n", u_errorName(loadStatus), message);
    249         }
    250 
    251     }
    252 	if(i==0) {
    253 		printf("No plugins loaded.\n");
    254 	}
    255 
    256 }
    257 
    258 
    259 
    260 extern int
    261 main(int argc, char* argv[]) {
    262     UErrorCode errorCode = U_ZERO_ERROR;
    263     UBool didSomething = FALSE;
    264 
    265     /* preset then read command line options */
    266     argc=u_parseArgs(argc, argv, sizeof(options)/sizeof(options[0]), options);
    267 
    268     /* error handling, printing usage message */
    269     if(argc<0) {
    270         fprintf(stderr,
    271             "error in command line argument \"%s\"\n",
    272             argv[-argc]);
    273     }
    274     if( options[0].doesOccur || options[1].doesOccur) {
    275       fprintf(stderr, "%s: Output information about the current ICU\n", argv[0]);
    276       fprintf(stderr, "Options:\n"
    277               " -h     or  --help                 - Print this help message.\n"
    278               " -m     or  --millisecond-time     - Print the current UTC time in milliseconds.\n"
    279               " -d <dir>   or  --icudatadir <dir> - Set the ICU Data Directory\n"
    280               " -v                                - Print version and configuration information about ICU\n"
    281               " -L         or  --list-plugins     - List and diagnose issues with ICU Plugins\n"
    282               " -K         or  --cleanup          - Call u_cleanup() before exitting (will attempt to unload plugins)\n"
    283               "\n"
    284               "If no arguments are given, the tool will print ICU version and configuration information.\n"
    285               );
    286       fprintf(stderr, "International Components for Unicode %s\n%s\n", U_ICU_VERSION, U_COPYRIGHT_STRING );
    287       return argc<0 ? U_ILLEGAL_ARGUMENT_ERROR : U_ZERO_ERROR;
    288     }
    289 
    290     if(options[2].doesOccur) {
    291       u_setDataDirectory(options[2].value);
    292     }
    293 
    294     if(options[5].doesOccur) {
    295       cmd_millis();
    296       didSomething=TRUE;
    297     }
    298     if(options[4].doesOccur) {
    299       cmd_listplugins();
    300       didSomething = TRUE;
    301     }
    302 
    303     if(options[3].doesOccur) {
    304       cmd_version(FALSE);
    305       didSomething = TRUE;
    306     }
    307 
    308     if(options[6].doesOccur) {  /* 2nd part of version: cleanup */
    309       cmd_cleanup();
    310       didSomething = TRUE;
    311     }
    312 
    313     if(!didSomething) {
    314       cmd_version(FALSE);  /* at least print the version # */
    315     }
    316 
    317     return U_FAILURE(errorCode);
    318 }
    319