michael@0: /* michael@0: ******************************************************************************* michael@0: * michael@0: * Copyright (C) 1999-2012, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: * michael@0: ******************************************************************************* michael@0: * file name: icuinfo.cpp michael@0: * encoding: US-ASCII michael@0: * tab size: 8 (not used) michael@0: * indentation:4 michael@0: * michael@0: * created on: 2009-2010 michael@0: * created by: Steven R. Loomis michael@0: * michael@0: * This program shows some basic info about the current ICU. michael@0: */ michael@0: michael@0: #include michael@0: #include michael@0: #include "unicode/utypes.h" michael@0: #include "unicode/putil.h" michael@0: #include "unicode/uclean.h" michael@0: #include "udbgutil.h" michael@0: #include "unewdata.h" michael@0: #include "cmemory.h" michael@0: #include "cstring.h" michael@0: #include "uoptions.h" michael@0: #include "toolutil.h" michael@0: #include "icuplugimp.h" michael@0: #include michael@0: #include michael@0: #include "unicode/ucal.h" michael@0: #include michael@0: #include "putilimp.h" michael@0: #include "unicode/uchar.h" michael@0: michael@0: static UOption options[]={ michael@0: /*0*/ UOPTION_HELP_H, michael@0: /*1*/ UOPTION_HELP_QUESTION_MARK, michael@0: /*2*/ UOPTION_ICUDATADIR, michael@0: /*3*/ UOPTION_VERBOSE, michael@0: /*4*/ UOPTION_DEF("list-plugins", 'L', UOPT_NO_ARG), michael@0: /*5*/ UOPTION_DEF("milisecond-time", 'm', UOPT_NO_ARG), michael@0: /*6*/ UOPTION_DEF("cleanup", 'K', UOPT_NO_ARG), michael@0: /*7*/ UOPTION_DEF("xml", 'x', UOPT_REQUIRES_ARG), michael@0: }; michael@0: michael@0: static UErrorCode initStatus = U_ZERO_ERROR; michael@0: static UBool icuInitted = FALSE; michael@0: michael@0: static void do_init() { michael@0: if(!icuInitted) { michael@0: u_init(&initStatus); michael@0: icuInitted = TRUE; michael@0: } michael@0: } michael@0: michael@0: michael@0: void cmd_millis() michael@0: { michael@0: printf("Milliseconds since Epoch: %.0f\n", uprv_getUTCtime()); michael@0: } michael@0: michael@0: void cmd_version(UBool /* noLoad */, UErrorCode &errorCode) michael@0: { michael@0: michael@0: do_init(); michael@0: michael@0: udbg_writeIcuInfo(stdout); /* print the XML format */ michael@0: michael@0: union { michael@0: uint8_t byte; michael@0: uint16_t word; michael@0: } u; michael@0: u.word=0x0100; michael@0: if(U_IS_BIG_ENDIAN==u.byte) { michael@0: //printf("U_IS_BIG_ENDIAN: %d\n", U_IS_BIG_ENDIAN); michael@0: } else { michael@0: fprintf(stderr, " error: U_IS_BIG_ENDIAN=%d != %d=actual 'is big endian'\n", michael@0: U_IS_BIG_ENDIAN, u.byte); michael@0: errorCode=U_INTERNAL_PROGRAM_ERROR; michael@0: } michael@0: michael@0: if(U_SIZEOF_WCHAR_T==sizeof(wchar_t)) { michael@0: //printf("U_SIZEOF_WCHAR_T: %d\n", U_SIZEOF_WCHAR_T); michael@0: } else { michael@0: fprintf(stderr, " error: U_SIZEOF_WCHAR_T=%d != %d=sizeof(wchar_t)\n", michael@0: U_SIZEOF_WCHAR_T, (int)sizeof(wchar_t)); michael@0: errorCode=U_INTERNAL_PROGRAM_ERROR; michael@0: } michael@0: michael@0: int charsetFamily; michael@0: if('A'==0x41) { michael@0: charsetFamily=U_ASCII_FAMILY; michael@0: } else if('A'==0xc1) { michael@0: charsetFamily=U_EBCDIC_FAMILY; michael@0: } else { michael@0: charsetFamily=-1; // unknown michael@0: } michael@0: if(U_CHARSET_FAMILY==charsetFamily) { michael@0: //printf("U_CHARSET_FAMILY: %d\n", U_CHARSET_FAMILY); michael@0: } else { michael@0: fprintf(stderr, " error: U_CHARSET_FAMILY=%d != %d=actual charset family\n", michael@0: U_CHARSET_FAMILY, charsetFamily); michael@0: errorCode=U_INTERNAL_PROGRAM_ERROR; michael@0: } michael@0: michael@0: printf("\n\nICU Initialization returned: %s\n", u_errorName(initStatus)); michael@0: michael@0: michael@0: #if U_ENABLE_DYLOAD michael@0: const char *pluginFile = uplug_getPluginFile(); michael@0: printf("Plugin file is: %s\n", (pluginFile&&*pluginFile)?pluginFile:"(not set. try setting ICU_PLUGINS to a directory.)"); michael@0: #else michael@0: fprintf(stderr, "Dynamic Loading: is disabled. No plugins will be loaded at start-up.\n"); michael@0: #endif michael@0: } michael@0: michael@0: void cmd_cleanup() michael@0: { michael@0: u_cleanup(); michael@0: fprintf(stdout, "ICU u_cleanup() called.\n"); michael@0: } michael@0: michael@0: michael@0: void cmd_listplugins() { michael@0: int32_t i; michael@0: UPlugData *plug; michael@0: michael@0: do_init(); michael@0: printf("ICU Initialized: u_init() returned %s\n", u_errorName(initStatus)); michael@0: michael@0: printf("Plugins: \n"); michael@0: printf( "# %6s %s \n", michael@0: "Level", michael@0: "Name" ); michael@0: printf( " %10s:%-10s\n", michael@0: "Library", michael@0: "Symbol" michael@0: ); michael@0: michael@0: michael@0: printf( " config| (configuration string)\n"); michael@0: printf( " >>> Error | Explanation \n"); michael@0: printf( "-----------------------------------\n"); michael@0: michael@0: for(i=0;(plug=uplug_getPlugInternal(i))!=NULL;i++) { michael@0: UErrorCode libStatus = U_ZERO_ERROR; michael@0: const char *name = uplug_getPlugName(plug); michael@0: const char *sym = uplug_getSymbolName(plug); michael@0: const char *lib = uplug_getLibraryName(plug, &libStatus); michael@0: const char *config = uplug_getConfiguration(plug); michael@0: UErrorCode loadStatus = uplug_getPlugLoadStatus(plug); michael@0: const char *message = NULL; michael@0: michael@0: printf("\n#%d %-6s %s \n", michael@0: i+1, michael@0: udbg_enumName(UDBG_UPlugLevel,(int32_t)uplug_getPlugLevel(plug)), michael@0: name!=NULL?(*name?name:"this plugin did not call uplug_setPlugName()"):"(null)" michael@0: ); michael@0: printf(" plugin| %10s:%-10s\n", michael@0: (U_SUCCESS(libStatus)?(lib!=NULL?lib:"(null)"):u_errorName(libStatus)), michael@0: sym!=NULL?sym:"(null)" michael@0: ); michael@0: michael@0: if(config!=NULL&&*config) { michael@0: printf(" config| %s\n", config); michael@0: } michael@0: michael@0: switch(loadStatus) { michael@0: case U_PLUGIN_CHANGED_LEVEL_WARNING: michael@0: message = "Note: This plugin changed the system level (by allocating memory or calling something which does). Later plugins may not load."; michael@0: break; michael@0: michael@0: case U_PLUGIN_DIDNT_SET_LEVEL: michael@0: message = "Error: This plugin did not call uplug_setPlugLevel during QUERY."; michael@0: break; michael@0: michael@0: case U_PLUGIN_TOO_HIGH: michael@0: message = "Error: This plugin couldn't load because the system level was too high. Try loading this plugin earlier."; michael@0: break; michael@0: michael@0: case U_ZERO_ERROR: michael@0: message = NULL; /* no message */ michael@0: break; michael@0: default: michael@0: if(U_FAILURE(loadStatus)) { michael@0: message = "error loading:"; michael@0: } else { michael@0: message = "warning during load:"; michael@0: } michael@0: } michael@0: michael@0: if(message!=NULL) { michael@0: printf("\\\\\\ status| %s\n" michael@0: "/// %s\n", u_errorName(loadStatus), message); michael@0: } michael@0: michael@0: } michael@0: if(i==0) { michael@0: printf("No plugins loaded.\n"); michael@0: } michael@0: michael@0: } michael@0: michael@0: michael@0: michael@0: extern int michael@0: main(int argc, char* argv[]) { michael@0: UErrorCode errorCode = U_ZERO_ERROR; michael@0: UBool didSomething = FALSE; michael@0: michael@0: /* preset then read command line options */ michael@0: argc=u_parseArgs(argc, argv, sizeof(options)/sizeof(options[0]), options); michael@0: michael@0: /* error handling, printing usage message */ michael@0: if(argc<0) { michael@0: fprintf(stderr, michael@0: "error in command line argument \"%s\"\n", michael@0: argv[-argc]); michael@0: } michael@0: if( options[0].doesOccur || options[1].doesOccur) { michael@0: fprintf(stderr, "%s: Output information about the current ICU\n", argv[0]); michael@0: fprintf(stderr, "Options:\n" michael@0: " -h or --help - Print this help message.\n" michael@0: " -m or --millisecond-time - Print the current UTC time in milliseconds.\n" michael@0: " -d or --icudatadir - Set the ICU Data Directory\n" michael@0: " -v - Print version and configuration information about ICU\n" michael@0: " -L or --list-plugins - List and diagnose issues with ICU Plugins\n" michael@0: " -K or --cleanup - Call u_cleanup() before exitting (will attempt to unload plugins)\n" michael@0: "\n" michael@0: "If no arguments are given, the tool will print ICU version and configuration information.\n" michael@0: ); michael@0: fprintf(stderr, "International Components for Unicode %s\n%s\n", U_ICU_VERSION, U_COPYRIGHT_STRING ); michael@0: return argc<0 ? U_ILLEGAL_ARGUMENT_ERROR : U_ZERO_ERROR; michael@0: } michael@0: michael@0: if(options[2].doesOccur) { michael@0: u_setDataDirectory(options[2].value); michael@0: } michael@0: michael@0: if(options[5].doesOccur) { michael@0: cmd_millis(); michael@0: didSomething=TRUE; michael@0: } michael@0: if(options[4].doesOccur) { michael@0: cmd_listplugins(); michael@0: didSomething = TRUE; michael@0: } michael@0: michael@0: if(options[3].doesOccur) { michael@0: cmd_version(FALSE, errorCode); michael@0: didSomething = TRUE; michael@0: } michael@0: michael@0: if(options[7].doesOccur) { /* 2nd part of version: cleanup */ michael@0: FILE *out = fopen(options[7].value, "w"); michael@0: if(out==NULL) { michael@0: fprintf(stderr,"ERR: can't write to XML file %s\n", options[7].value); michael@0: return 1; michael@0: } michael@0: /* todo: API for writing DTD? */ michael@0: fprintf(out, "\n"); michael@0: udbg_writeIcuInfo(out); michael@0: fclose(out); michael@0: didSomething = TRUE; michael@0: } michael@0: michael@0: if(options[6].doesOccur) { /* 2nd part of version: cleanup */ michael@0: cmd_cleanup(); michael@0: didSomething = TRUE; michael@0: } michael@0: michael@0: if(!didSomething) { michael@0: cmd_version(FALSE, errorCode); /* at least print the version # */ michael@0: } michael@0: michael@0: return U_FAILURE(errorCode); michael@0: }