Home | History | Annotate | Download | only in icuinfo
      1 /*
      2 *******************************************************************************
      3 *
      4 *   Copyright (C) 1999-2012, 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),
     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 U_ENABLE_DYLOAD
    113     const char *pluginFile = uplug_getPluginFile();
    114     printf("Plugin file is: %s\n", (pluginFile&&*pluginFile)?pluginFile:"(not set. try setting ICU_PLUGINS to a directory.)");
    115 #else
    116     fprintf(stderr, "Dynamic Loading: is disabled. No plugins will be loaded at start-up.\n");
    117 #endif
    118 }
    119 
    120 void cmd_cleanup()
    121 {
    122     u_cleanup();
    123     fprintf(stdout, "ICU u_cleanup() called.\n");
    124 }
    125 
    126 
    127 void cmd_listplugins() {
    128     int32_t i;
    129     UPlugData *plug;
    130 
    131     do_init();
    132     printf("ICU Initialized: u_init() returned %s\n", u_errorName(initStatus));
    133 
    134     printf("Plugins: \n");
    135     printf(    "# %6s   %s \n",
    136                        "Level",
    137                        "Name" );
    138     printf(    "    %10s:%-10s\n",
    139                        "Library",
    140                        "Symbol"
    141             );
    142 
    143 
    144     printf(    "       config| (configuration string)\n");
    145     printf(    " >>>   Error          | Explanation \n");
    146     printf(    "-----------------------------------\n");
    147 
    148     for(i=0;(plug=uplug_getPlugInternal(i))!=NULL;i++) {
    149         UErrorCode libStatus = U_ZERO_ERROR;
    150         const char *name = uplug_getPlugName(plug);
    151         const char *sym = uplug_getSymbolName(plug);
    152         const char *lib = uplug_getLibraryName(plug, &libStatus);
    153         const char *config = uplug_getConfiguration(plug);
    154         UErrorCode loadStatus = uplug_getPlugLoadStatus(plug);
    155         const char *message = NULL;
    156 
    157         printf("\n#%d  %-6s %s \n",
    158             i+1,
    159             udbg_enumName(UDBG_UPlugLevel,(int32_t)uplug_getPlugLevel(plug)),
    160             name!=NULL?(*name?name:"this plugin did not call uplug_setPlugName()"):"(null)"
    161         );
    162         printf("    plugin| %10s:%-10s\n",
    163             (U_SUCCESS(libStatus)?(lib!=NULL?lib:"(null)"):u_errorName(libStatus)),
    164             sym!=NULL?sym:"(null)"
    165         );
    166 
    167         if(config!=NULL&&*config) {
    168             printf("    config| %s\n", config);
    169         }
    170 
    171         switch(loadStatus) {
    172             case U_PLUGIN_CHANGED_LEVEL_WARNING:
    173                 message = "Note: This plugin changed the system level (by allocating memory or calling something which does). Later plugins may not load.";
    174                 break;
    175 
    176             case U_PLUGIN_DIDNT_SET_LEVEL:
    177                 message = "Error: This plugin did not call uplug_setPlugLevel during QUERY.";
    178                 break;
    179 
    180             case U_PLUGIN_TOO_HIGH:
    181                 message = "Error: This plugin couldn't load because the system level was too high. Try loading this plugin earlier.";
    182                 break;
    183 
    184             case U_ZERO_ERROR:
    185                 message = NULL; /* no message */
    186                 break;
    187             default:
    188                 if(U_FAILURE(loadStatus)) {
    189                     message = "error loading:";
    190                 } else {
    191                     message = "warning during load:";
    192                 }
    193         }
    194 
    195         if(message!=NULL) {
    196             printf("\\\\\\ status| %s\n"
    197                    "/// %s\n", u_errorName(loadStatus), message);
    198         }
    199 
    200     }
    201 	if(i==0) {
    202 		printf("No plugins loaded.\n");
    203 	}
    204 
    205 }
    206 
    207 
    208 
    209 extern int
    210 main(int argc, char* argv[]) {
    211     UErrorCode errorCode = U_ZERO_ERROR;
    212     UBool didSomething = FALSE;
    213 
    214     /* preset then read command line options */
    215     argc=u_parseArgs(argc, argv, sizeof(options)/sizeof(options[0]), options);
    216 
    217     /* error handling, printing usage message */
    218     if(argc<0) {
    219         fprintf(stderr,
    220             "error in command line argument \"%s\"\n",
    221             argv[-argc]);
    222     }
    223     if( options[0].doesOccur || options[1].doesOccur) {
    224       fprintf(stderr, "%s: Output information about the current ICU\n", argv[0]);
    225       fprintf(stderr, "Options:\n"
    226               " -h     or  --help                 - Print this help message.\n"
    227               " -m     or  --millisecond-time     - Print the current UTC time in milliseconds.\n"
    228               " -d <dir>   or  --icudatadir <dir> - Set the ICU Data Directory\n"
    229               " -v                                - Print version and configuration information about ICU\n"
    230               " -L         or  --list-plugins     - List and diagnose issues with ICU Plugins\n"
    231               " -K         or  --cleanup          - Call u_cleanup() before exitting (will attempt to unload plugins)\n"
    232               "\n"
    233               "If no arguments are given, the tool will print ICU version and configuration information.\n"
    234               );
    235       fprintf(stderr, "International Components for Unicode %s\n%s\n", U_ICU_VERSION, U_COPYRIGHT_STRING );
    236       return argc<0 ? U_ILLEGAL_ARGUMENT_ERROR : U_ZERO_ERROR;
    237     }
    238 
    239     if(options[2].doesOccur) {
    240       u_setDataDirectory(options[2].value);
    241     }
    242 
    243     if(options[5].doesOccur) {
    244       cmd_millis();
    245       didSomething=TRUE;
    246     }
    247     if(options[4].doesOccur) {
    248       cmd_listplugins();
    249       didSomething = TRUE;
    250     }
    251 
    252     if(options[3].doesOccur) {
    253       cmd_version(FALSE, errorCode);
    254       didSomething = TRUE;
    255     }
    256 
    257     if(options[7].doesOccur) {  /* 2nd part of version: cleanup */
    258       FILE *out = fopen(options[7].value, "w");
    259       if(out==NULL) {
    260         fprintf(stderr,"ERR: can't write to XML file %s\n", options[7].value);
    261         return 1;
    262       }
    263       /* todo: API for writing DTD? */
    264       fprintf(out, "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
    265       udbg_writeIcuInfo(out);
    266       fclose(out);
    267       didSomething = TRUE;
    268     }
    269 
    270     if(options[6].doesOccur) {  /* 2nd part of version: cleanup */
    271       cmd_cleanup();
    272       didSomething = TRUE;
    273     }
    274 
    275     if(!didSomething) {
    276       cmd_version(FALSE, errorCode);  /* at least print the version # */
    277     }
    278 
    279     return U_FAILURE(errorCode);
    280 }
    281