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