Home | History | Annotate | Download | only in icuinfo
      1 /*
      2 *******************************************************************************
      3 *
      4 *   Copyright (C) 1999-2015, 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 "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), // may be a no-op if disabled
     44   /*5*/ UOPTION_DEF("milisecond-time", 'm', UOPT_NO_ARG),
     45   /*6*/ UOPTION_DEF("cleanup", 'K', UOPT_NO_ARG),
     46   /*7*/ UOPTION_DEF("xml", 'x', UOPT_REQUIRES_ARG),
     47 };
     48 
     49 static UErrorCode initStatus = U_ZERO_ERROR;
     50 static UBool icuInitted = FALSE;
     51 
     52 static void do_init() {
     53     if(!icuInitted) {
     54       u_init(&initStatus);
     55       icuInitted = TRUE;
     56     }
     57 }
     58 
     59 
     60 void cmd_millis()
     61 {
     62   printf("Milliseconds since Epoch: %.0f\n", uprv_getUTCtime());
     63 }
     64 
     65 void cmd_version(UBool /* noLoad */, UErrorCode &errorCode)
     66 {
     67 
     68     do_init();
     69 
     70     udbg_writeIcuInfo(stdout); /* print the XML format */
     71 
     72     union {
     73         uint8_t byte;
     74         uint16_t word;
     75     } u;
     76     u.word=0x0100;
     77     if(U_IS_BIG_ENDIAN==u.byte) {
     78       //printf("U_IS_BIG_ENDIAN: %d\n", U_IS_BIG_ENDIAN);
     79     } else {
     80         fprintf(stderr, "  error: U_IS_BIG_ENDIAN=%d != %d=actual 'is big endian'\n",
     81                 U_IS_BIG_ENDIAN, u.byte);
     82         errorCode=U_INTERNAL_PROGRAM_ERROR;
     83     }
     84 
     85     if(U_SIZEOF_WCHAR_T==sizeof(wchar_t)) {
     86       //printf("U_SIZEOF_WCHAR_T: %d\n", U_SIZEOF_WCHAR_T);
     87     } else {
     88         fprintf(stderr, "  error: U_SIZEOF_WCHAR_T=%d != %d=sizeof(wchar_t)\n",
     89                 U_SIZEOF_WCHAR_T, (int)sizeof(wchar_t));
     90         errorCode=U_INTERNAL_PROGRAM_ERROR;
     91     }
     92 
     93     int charsetFamily;
     94     if('A'==0x41) {
     95         charsetFamily=U_ASCII_FAMILY;
     96     } else if('A'==0xc1) {
     97         charsetFamily=U_EBCDIC_FAMILY;
     98     } else {
     99         charsetFamily=-1;  // unknown
    100     }
    101     if(U_CHARSET_FAMILY==charsetFamily) {
    102       //printf("U_CHARSET_FAMILY: %d\n", U_CHARSET_FAMILY);
    103     } else {
    104         fprintf(stderr, "  error: U_CHARSET_FAMILY=%d != %d=actual charset family\n",
    105                 U_CHARSET_FAMILY, charsetFamily);
    106         errorCode=U_INTERNAL_PROGRAM_ERROR;
    107     }
    108 
    109     printf("\n\nICU Initialization returned: %s\n", u_errorName(initStatus));
    110 
    111 
    112 #if UCONFIG_ENABLE_PLUGINS
    113 #if U_ENABLE_DYLOAD
    114     const char *pluginFile = uplug_getPluginFile();
    115     printf("Plugin file is: %s\n", (pluginFile&&*pluginFile)?pluginFile:"(not set. try setting ICU_PLUGINS to a directory.)");
    116 #else
    117     fprintf(stderr, "Dynamic Loading: is disabled. No plugins will be loaded at start-up.\n");
    118 #endif
    119 #else
    120     fprintf(stderr, "Plugins are disabled.\n");
    121 #endif
    122 }
    123 
    124 void cmd_cleanup()
    125 {
    126     u_cleanup();
    127     fprintf(stdout, "ICU u_cleanup() called.\n");
    128 }
    129 
    130 
    131 void cmd_listplugins() {
    132 #if UCONFIG_ENABLE_PLUGINS
    133     int32_t i;
    134     UPlugData *plug;
    135 
    136     do_init();
    137     printf("ICU Initialized: u_init() returned %s\n", u_errorName(initStatus));
    138 
    139     printf("Plugins: \n");
    140     printf(    "# %6s   %s \n",
    141                        "Level",
    142                        "Name" );
    143     printf(    "    %10s:%-10s\n",
    144                        "Library",
    145                        "Symbol"
    146             );
    147 
    148 
    149     printf(    "       config| (configuration string)\n");
    150     printf(    " >>>   Error          | Explanation \n");
    151     printf(    "-----------------------------------\n");
    152 
    153     for(i=0;(plug=uplug_getPlugInternal(i))!=NULL;i++) {
    154         UErrorCode libStatus = U_ZERO_ERROR;
    155         const char *name = uplug_getPlugName(plug);
    156         const char *sym = uplug_getSymbolName(plug);
    157         const char *lib = uplug_getLibraryName(plug, &libStatus);
    158         const char *config = uplug_getConfiguration(plug);
    159         UErrorCode loadStatus = uplug_getPlugLoadStatus(plug);
    160         const char *message = NULL;
    161 
    162         printf("\n#%d  %-6s %s \n",
    163             i+1,
    164             udbg_enumName(UDBG_UPlugLevel,(int32_t)uplug_getPlugLevel(plug)),
    165             name!=NULL?(*name?name:"this plugin did not call uplug_setPlugName()"):"(null)"
    166         );
    167         printf("    plugin| %10s:%-10s\n",
    168             (U_SUCCESS(libStatus)?(lib!=NULL?lib:"(null)"):u_errorName(libStatus)),
    169             sym!=NULL?sym:"(null)"
    170         );
    171 
    172         if(config!=NULL&&*config) {
    173             printf("    config| %s\n", config);
    174         }
    175 
    176         switch(loadStatus) {
    177             case U_PLUGIN_CHANGED_LEVEL_WARNING:
    178                 message = "Note: This plugin changed the system level (by allocating memory or calling something which does). Later plugins may not load.";
    179                 break;
    180 
    181             case U_PLUGIN_DIDNT_SET_LEVEL:
    182                 message = "Error: This plugin did not call uplug_setPlugLevel during QUERY.";
    183                 break;
    184 
    185             case U_PLUGIN_TOO_HIGH:
    186                 message = "Error: This plugin couldn't load because the system level was too high. Try loading this plugin earlier.";
    187                 break;
    188 
    189             case U_ZERO_ERROR:
    190                 message = NULL; /* no message */
    191                 break;
    192             default:
    193                 if(U_FAILURE(loadStatus)) {
    194                     message = "error loading:";
    195                 } else {
    196                     message = "warning during load:";
    197                 }
    198         }
    199 
    200         if(message!=NULL) {
    201             printf("\\\\\\ status| %s\n"
    202                    "/// %s\n", u_errorName(loadStatus), message);
    203         }
    204 
    205     }
    206 	if(i==0) {
    207 		printf("No plugins loaded.\n");
    208 	}
    209 #endif
    210 }
    211 
    212 
    213 
    214 extern int
    215 main(int argc, char* argv[]) {
    216     UErrorCode errorCode = U_ZERO_ERROR;
    217     UBool didSomething = FALSE;
    218 
    219     /* preset then read command line options */
    220     argc=u_parseArgs(argc, argv, sizeof(options)/sizeof(options[0]), options);
    221 
    222     /* error handling, printing usage message */
    223     if(argc<0) {
    224         fprintf(stderr,
    225             "error in command line argument \"%s\"\n",
    226             argv[-argc]);
    227     }
    228     if( options[0].doesOccur || options[1].doesOccur) {
    229       fprintf(stderr, "%s: Output information about the current ICU\n", argv[0]);
    230       fprintf(stderr, "Options:\n"
    231               " -h     or  --help                 - Print this help message.\n"
    232               " -m     or  --millisecond-time     - Print the current UTC time in milliseconds.\n"
    233               " -d <dir>   or  --icudatadir <dir> - Set the ICU Data Directory\n"
    234               " -v                                - Print version and configuration information about ICU\n"
    235 #if UCONFIG_ENABLE_PLUGINS
    236               " -L         or  --list-plugins     - List and diagnose issues with ICU Plugins\n"
    237 #endif
    238               " -K         or  --cleanup          - Call u_cleanup() before exitting (will attempt to unload plugins)\n"
    239               "\n"
    240               "If no arguments are given, the tool will print ICU version and configuration information.\n"
    241               );
    242       fprintf(stderr, "International Components for Unicode %s\n%s\n", U_ICU_VERSION, U_COPYRIGHT_STRING );
    243       return argc<0 ? U_ILLEGAL_ARGUMENT_ERROR : U_ZERO_ERROR;
    244     }
    245 
    246     if(options[2].doesOccur) {
    247       u_setDataDirectory(options[2].value);
    248     }
    249 
    250     if(options[5].doesOccur) {
    251       cmd_millis();
    252       didSomething=TRUE;
    253     }
    254     if(options[4].doesOccur) {
    255       cmd_listplugins();
    256       didSomething = TRUE;
    257     }
    258 
    259     if(options[3].doesOccur) {
    260       cmd_version(FALSE, errorCode);
    261       didSomething = TRUE;
    262     }
    263 
    264     if(options[7].doesOccur) {  /* 2nd part of version: cleanup */
    265       FILE *out = fopen(options[7].value, "w");
    266       if(out==NULL) {
    267         fprintf(stderr,"ERR: can't write to XML file %s\n", options[7].value);
    268         return 1;
    269       }
    270       /* todo: API for writing DTD? */
    271       fprintf(out, "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
    272       udbg_writeIcuInfo(out);
    273       fclose(out);
    274       didSomething = TRUE;
    275     }
    276 
    277     if(options[6].doesOccur) {  /* 2nd part of version: cleanup */
    278       cmd_cleanup();
    279       didSomething = TRUE;
    280     }
    281 
    282     if(!didSomething) {
    283       cmd_version(FALSE, errorCode);  /* at least print the version # */
    284     }
    285 
    286     return U_FAILURE(errorCode);
    287 }
    288