2 ******************************************************************************
4 * Copyright (C) 2009-2015, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 ******************************************************************************
9 * FILE NAME : icuplug.h
11 * Date Name Description
13 ******************************************************************************
18 * \brief C API: ICU Plugin API
20 * <h2>C API: ICU Plugin API</h2>
22 * <p>C API allowing run-time loadable modules that extend or modify ICU functionality.</p>
24 * <h3>Loading and Configuration</h3>
26 * <p>At ICU startup time, the environment variable "ICU_PLUGINS" will be
27 * queried for a directory name. If it is not set, the preprocessor symbol
28 * "DEFAULT_ICU_PLUGINS" will be checked for a default value.</p>
30 * <p>Within the above-named directory, the file "icuplugins##.txt" will be
31 * opened, if present, where ## is the major+minor number of the currently
32 * running ICU (such as, 44 for ICU 4.4, thus icuplugins44.txt)</p>
34 * <p>The configuration file has this format:</p>
37 * <li>Hash (#) begins a comment line</li>
39 * <li>Non-comment lines have two or three components:
40 * LIBRARYNAME ENTRYPOINT [ CONFIGURATION .. ]</li>
42 * <li>Tabs or spaces separate the three items.</li>
44 * <li>LIBRARYNAME is the name of a shared library, either a short name if
45 * it is on the loader path, or a full pathname.</li>
47 * <li>ENTRYPOINT is the short (undecorated) symbol name of the plugin's
48 * entrypoint, as above.</li>
50 * <li>CONFIGURATION is the entire rest of the line . It's passed as-is to
54 * <p>An example configuration file is, in its entirety:</p>
57 * # this is icuplugins44.txt
58 * testplug.dll myPlugin hello=world
60 * <p>Plugins are categorized as "high" or "low" level. Low level are those
61 * which must be run BEFORE high level plugins, and before any operations
62 * which cause ICU to be 'initialized'. If a plugin is low level but
63 * causes ICU to allocate memory or become initialized, that plugin is said
64 * to cause a 'level change'. </p>
66 * <p>At load time, ICU first queries all plugins to determine their level,
67 * then loads all 'low' plugins first, and then loads all 'high' plugins.
68 * Plugins are otherwise loaded in the order listed in the configuration file.</p>
70 * <h3>Implementing a Plugin</h3>
72 * U_CAPI UPlugTokenReturn U_EXPORT2
73 * myPlugin (UPlugData *plug, UPlugReason reason, UErrorCode *status) {
74 * if(reason==UPLUG_REASON_QUERY) {
75 * uplug_setPlugName(plug, "Simple Plugin");
76 * uplug_setPlugLevel(plug, UPLUG_LEVEL_HIGH);
77 * } else if(reason==UPLUG_REASON_LOAD) {
78 * ... Set up some ICU things here....
79 * } else if(reason==UPLUG_REASON_UNLOAD) {
80 * ... unload, clean up ...
86 * <p>The UPlugData* is an opaque pointer to the plugin-specific data, and is
87 * used in all other API calls.</p>
89 * <p>The API contract is:</p>
90 * <ol><li>The plugin MUST always return UPLUG_TOKEN as a return value- to
91 * indicate that it is a valid plugin.</li>
93 * <li>When the 'reason' parameter is set to UPLUG_REASON_QUERY, the
94 * plugin MUST call uplug_setPlugLevel() to indicate whether it is a high
95 * level or low level plugin.</li>
97 * <li>When the 'reason' parameter is UPLUG_REASON_QUERY, the plugin
98 * SHOULD call uplug_setPlugName to indicate a human readable plugin name.</li></ol>
101 * \internal ICU 4.4 Technology Preview
108 #include "unicode/utypes.h"
111 #if UCONFIG_ENABLE_PLUGINS
115 /* === Basic types === */
117 #ifndef U_HIDE_INTERNAL_API
120 * Opaque structure passed to/from a plugin.
121 * use the APIs to access it.
122 * @internal ICU 4.4 Technology Preview
126 typedef struct UPlugData UPlugData
;
131 * Random Token to identify a valid ICU plugin. Plugins must return this
132 * from the entrypoint.
133 * @internal ICU 4.4 Technology Preview
135 #define UPLUG_TOKEN 0x54762486
138 * Max width of names, symbols, and configuration strings
139 * @internal ICU 4.4 Technology Preview
141 #define UPLUG_NAME_MAX 100
145 * Return value from a plugin entrypoint.
146 * Must always be set to UPLUG_TOKEN
148 * @internal ICU 4.4 Technology Preview
150 typedef uint32_t UPlugTokenReturn
;
153 * Reason code for the entrypoint's call
154 * @internal ICU 4.4 Technology Preview
157 UPLUG_REASON_QUERY
= 0, /**< The plugin is being queried for info. **/
158 UPLUG_REASON_LOAD
= 1, /**< The plugin is being loaded. **/
159 UPLUG_REASON_UNLOAD
= 2, /**< The plugin is being unloaded. **/
160 UPLUG_REASON_COUNT
/**< count of known reasons **/
165 * Level of plugin loading
167 * QUERY: INVALID -> { LOW | HIGH }
169 * @internal ICU 4.4 Technology Preview
172 UPLUG_LEVEL_INVALID
= 0, /**< The plugin is invalid, hasn't called uplug_setLevel, or can't load. **/
173 UPLUG_LEVEL_UNKNOWN
= 1, /**< The plugin is waiting to be installed. **/
174 UPLUG_LEVEL_LOW
= 2, /**< The plugin must be called before u_init completes **/
175 UPLUG_LEVEL_HIGH
= 3, /**< The plugin can run at any time. **/
176 UPLUG_LEVEL_COUNT
/**< count of known reasons **/
180 * Entrypoint for an ICU plugin.
181 * @param plug the UPlugData handle.
182 * @param status the plugin's extended status code.
183 * @return A valid plugin must return UPLUG_TOKEN
184 * @internal ICU 4.4 Technology Preview
186 typedef UPlugTokenReturn (U_EXPORT2 UPlugEntrypoint
) (
191 /* === Needed for Implementing === */
194 * Request that this plugin not be unloaded at cleanup time.
195 * This is appropriate for plugins which cannot be cleaned up.
198 * @param dontUnload set true if this plugin can't be unloaded
199 * @internal ICU 4.4 Technology Preview
201 U_INTERNAL
void U_EXPORT2
202 uplug_setPlugNoUnload(UPlugData
*plug
, UBool dontUnload
);
205 * Set the level of this plugin.
206 * @param plug plugin data handle
207 * @param level the level of this plugin
208 * @internal ICU 4.4 Technology Preview
210 U_INTERNAL
void U_EXPORT2
211 uplug_setPlugLevel(UPlugData
*plug
, UPlugLevel level
);
214 * Get the level of this plugin.
215 * @param plug plugin data handle
216 * @return the level of this plugin
217 * @internal ICU 4.4 Technology Preview
219 U_INTERNAL UPlugLevel U_EXPORT2
220 uplug_getPlugLevel(UPlugData
*plug
);
223 * Get the lowest level of plug which can currently load.
224 * For example, if UPLUG_LEVEL_LOW is returned, then low level plugins may load
225 * if UPLUG_LEVEL_HIGH is returned, then only high level plugins may load.
226 * @return the lowest level of plug which can currently load
227 * @internal ICU 4.4 Technology Preview
229 U_INTERNAL UPlugLevel U_EXPORT2
230 uplug_getCurrentLevel(void);
234 * Get plug load status
235 * @return The error code of this plugin's load attempt.
236 * @internal ICU 4.4 Technology Preview
238 U_INTERNAL UErrorCode U_EXPORT2
239 uplug_getPlugLoadStatus(UPlugData
*plug
);
242 * Set the human-readable name of this plugin.
243 * @param plug plugin data handle
244 * @param name the name of this plugin. The first UPLUG_NAME_MAX characters willi be copied into a new buffer.
245 * @internal ICU 4.4 Technology Preview
247 U_INTERNAL
void U_EXPORT2
248 uplug_setPlugName(UPlugData
*plug
, const char *name
);
251 * Get the human-readable name of this plugin.
252 * @param plug plugin data handle
253 * @return the name of this plugin
254 * @internal ICU 4.4 Technology Preview
256 U_INTERNAL
const char * U_EXPORT2
257 uplug_getPlugName(UPlugData
*plug
);
260 * Return the symbol name for this plugin, if known.
261 * @param plug plugin data handle
262 * @return the symbol name, or NULL
263 * @internal ICU 4.4 Technology Preview
265 U_INTERNAL
const char * U_EXPORT2
266 uplug_getSymbolName(UPlugData
*plug
);
269 * Return the library name for this plugin, if known.
270 * @param plug plugin data handle
271 * @param status error code
272 * @return the library name, or NULL
273 * @internal ICU 4.4 Technology Preview
275 U_INTERNAL
const char * U_EXPORT2
276 uplug_getLibraryName(UPlugData
*plug
, UErrorCode
*status
);
279 * Return the library used for this plugin, if known.
280 * Plugins could use this to load data out of their
281 * @param plug plugin data handle
282 * @return the library, or NULL
283 * @internal ICU 4.4 Technology Preview
285 U_INTERNAL
void * U_EXPORT2
286 uplug_getLibrary(UPlugData
*plug
);
289 * Return the plugin-specific context data.
290 * @param plug plugin data handle
291 * @return the context, or NULL if not set
292 * @internal ICU 4.4 Technology Preview
294 U_INTERNAL
void * U_EXPORT2
295 uplug_getContext(UPlugData
*plug
);
298 * Set the plugin-specific context data.
299 * @param plug plugin data handle
300 * @param context new context to set
301 * @internal ICU 4.4 Technology Preview
303 U_INTERNAL
void U_EXPORT2
304 uplug_setContext(UPlugData
*plug
, void *context
);
308 * Get the configuration string, if available.
309 * The string is in the platform default codepage.
310 * @param plug plugin data handle
311 * @return configuration string, or else null.
312 * @internal ICU 4.4 Technology Preview
314 U_INTERNAL
const char * U_EXPORT2
315 uplug_getConfiguration(UPlugData
*plug
);
318 * Return all currently installed plugins, from newest to oldest
321 * UPlugData *plug = NULL;
322 * while(plug=uplug_nextPlug(plug)) {
323 * ... do something with 'plug' ...
326 * Not thread safe- do not call while plugs are added or removed.
327 * @param prior pass in 'NULL' to get the first (most recent) plug,
328 * otherwise pass the value returned on a prior call to uplug_nextPlug
329 * @return the next oldest plugin, or NULL if no more.
330 * @internal ICU 4.4 Technology Preview
332 U_INTERNAL UPlugData
* U_EXPORT2
333 uplug_nextPlug(UPlugData
*prior
);
336 * Inject a plugin as if it were loaded from a library.
337 * This is useful for testing plugins.
338 * Note that it will have a 'NULL' library pointer associated
339 * with it, and therefore no llibrary will be closed at cleanup time.
340 * Low level plugins may not be able to load, as ordering can't be enforced.
341 * @param entrypoint entrypoint to install
342 * @param config user specified configuration string, if available, or NULL.
343 * @param status error result
344 * @return the new UPlugData associated with this plugin, or NULL if error.
345 * @internal ICU 4.4 Technology Preview
347 U_INTERNAL UPlugData
* U_EXPORT2
348 uplug_loadPlugFromEntrypoint(UPlugEntrypoint
*entrypoint
, const char *config
, UErrorCode
*status
);
352 * Inject a plugin from a library, as if the information came from a config file.
353 * Low level plugins may not be able to load, and ordering can't be enforced.
354 * @param libName DLL name to load
355 * @param sym symbol of plugin (UPlugEntrypoint function)
356 * @param config configuration string, or NULL
357 * @param status error result
358 * @return the new UPlugData associated with this plugin, or NULL if error.
359 * @internal ICU 4.4 Technology Preview
361 U_INTERNAL UPlugData
* U_EXPORT2
362 uplug_loadPlugFromLibrary(const char *libName
, const char *sym
, const char *config
, UErrorCode
*status
);
366 * Will request the plugin to be unloaded, and close the library if needed
367 * @param plug plugin handle to close
368 * @param status error result
369 * @internal ICU 4.4 Technology Preview
371 U_INTERNAL
void U_EXPORT2
372 uplug_removePlug(UPlugData
*plug
, UErrorCode
*status
);
373 #endif /* U_HIDE_INTERNAL_API */
375 #endif /* UCONFIG_ENABLE_PLUGINS */
377 #endif /* _ICUPLUG */