2 ******************************************************************************
4 * Copyright (C) 2009-2011, 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 /* === Basic types === */
113 #ifndef U_HIDE_INTERNAL_API
116 * Opaque structure passed to/from a plugin.
117 * use the APIs to access it.
118 * @internal ICU 4.4 Technology Preview
122 typedef struct UPlugData UPlugData
;
127 * Random Token to identify a valid ICU plugin. Plugins must return this
128 * from the entrypoint.
129 * @internal ICU 4.4 Technology Preview
131 #define UPLUG_TOKEN 0x54762486
134 * Max width of names, symbols, and configuration strings
135 * @internal ICU 4.4 Technology Preview
137 #define UPLUG_NAME_MAX 100
141 * Return value from a plugin entrypoint.
142 * Must always be set to UPLUG_TOKEN
144 * @internal ICU 4.4 Technology Preview
146 typedef uint32_t UPlugTokenReturn
;
149 * Reason code for the entrypoint's call
150 * @internal ICU 4.4 Technology Preview
153 UPLUG_REASON_QUERY
= 0, /**< The plugin is being queried for info. **/
154 UPLUG_REASON_LOAD
= 1, /**< The plugin is being loaded. **/
155 UPLUG_REASON_UNLOAD
= 2, /**< The plugin is being unloaded. **/
156 UPLUG_REASON_COUNT
/**< count of known reasons **/
161 * Level of plugin loading
163 * QUERY: INVALID -> { LOW | HIGH }
165 * @internal ICU 4.4 Technology Preview
168 UPLUG_LEVEL_INVALID
= 0, /**< The plugin is invalid, hasn't called uplug_setLevel, or can't load. **/
169 UPLUG_LEVEL_UNKNOWN
= 1, /**< The plugin is waiting to be installed. **/
170 UPLUG_LEVEL_LOW
= 2, /**< The plugin must be called before u_init completes **/
171 UPLUG_LEVEL_HIGH
= 3, /**< The plugin can run at any time. **/
172 UPLUG_LEVEL_COUNT
/**< count of known reasons **/
176 * Entrypoint for an ICU plugin.
177 * @param plug the UPlugData handle.
178 * @param status the plugin's extended status code.
179 * @return A valid plugin must return UPLUG_TOKEN
180 * @internal ICU 4.4 Technology Preview
182 typedef UPlugTokenReturn (U_EXPORT2 UPlugEntrypoint
) (
187 /* === Needed for Implementing === */
190 * Request that this plugin not be unloaded at cleanup time.
191 * This is appropriate for plugins which cannot be cleaned up.
194 * @param dontUnload set true if this plugin can't be unloaded
195 * @internal ICU 4.4 Technology Preview
197 U_CAPI
void U_EXPORT2
198 uplug_setPlugNoUnload(UPlugData
*plug
, UBool dontUnload
);
201 * Set the level of this plugin.
202 * @param plug plugin data handle
203 * @param level the level of this plugin
204 * @internal ICU 4.4 Technology Preview
206 U_CAPI
void U_EXPORT2
207 uplug_setPlugLevel(UPlugData
*plug
, UPlugLevel level
);
210 * Get the level of this plugin.
211 * @param plug plugin data handle
212 * @return the level of this plugin
213 * @internal ICU 4.4 Technology Preview
215 U_CAPI UPlugLevel U_EXPORT2
216 uplug_getPlugLevel(UPlugData
*plug
);
219 * Get the lowest level of plug which can currently load.
220 * For example, if UPLUG_LEVEL_LOW is returned, then low level plugins may load
221 * if UPLUG_LEVEL_HIGH is returned, then only high level plugins may load.
222 * @return the lowest level of plug which can currently load
223 * @internal ICU 4.4 Technology Preview
225 U_CAPI UPlugLevel U_EXPORT2
226 uplug_getCurrentLevel(void);
230 * Get plug load status
231 * @return The error code of this plugin's load attempt.
232 * @internal ICU 4.4 Technology Preview
234 U_CAPI UErrorCode U_EXPORT2
235 uplug_getPlugLoadStatus(UPlugData
*plug
);
238 * Set the human-readable name of this plugin.
239 * @param plug plugin data handle
240 * @param name the name of this plugin. The first UPLUG_NAME_MAX characters willi be copied into a new buffer.
241 * @internal ICU 4.4 Technology Preview
243 U_CAPI
void U_EXPORT2
244 uplug_setPlugName(UPlugData
*plug
, const char *name
);
247 * Get the human-readable name of this plugin.
248 * @param plug plugin data handle
249 * @return the name of this plugin
250 * @internal ICU 4.4 Technology Preview
252 U_CAPI
const char * U_EXPORT2
253 uplug_getPlugName(UPlugData
*plug
);
256 * Return the symbol name for this plugin, if known.
257 * @param plug plugin data handle
258 * @return the symbol name, or NULL
259 * @internal ICU 4.4 Technology Preview
261 U_CAPI
const char * U_EXPORT2
262 uplug_getSymbolName(UPlugData
*plug
);
265 * Return the library name for this plugin, if known.
266 * @param plug plugin data handle
267 * @param status error code
268 * @return the library name, or NULL
269 * @internal ICU 4.4 Technology Preview
271 U_CAPI
const char * U_EXPORT2
272 uplug_getLibraryName(UPlugData
*plug
, UErrorCode
*status
);
275 * Return the library used for this plugin, if known.
276 * Plugins could use this to load data out of their
277 * @param plug plugin data handle
278 * @return the library, or NULL
279 * @internal ICU 4.4 Technology Preview
281 U_CAPI
void * U_EXPORT2
282 uplug_getLibrary(UPlugData
*plug
);
285 * Return the plugin-specific context data.
286 * @param plug plugin data handle
287 * @return the context, or NULL if not set
288 * @internal ICU 4.4 Technology Preview
290 U_CAPI
void * U_EXPORT2
291 uplug_getContext(UPlugData
*plug
);
294 * Set the plugin-specific context data.
295 * @param plug plugin data handle
296 * @param context new context to set
297 * @internal ICU 4.4 Technology Preview
299 U_CAPI
void U_EXPORT2
300 uplug_setContext(UPlugData
*plug
, void *context
);
304 * Get the configuration string, if available.
305 * The string is in the platform default codepage.
306 * @param plug plugin data handle
307 * @return configuration string, or else null.
308 * @internal ICU 4.4 Technology Preview
310 U_CAPI
const char * U_EXPORT2
311 uplug_getConfiguration(UPlugData
*plug
);
314 * Return all currently installed plugins, from newest to oldest
317 * UPlugData *plug = NULL;
318 * while(plug=uplug_nextPlug(plug)) {
319 * ... do something with 'plug' ...
322 * Not thread safe- do not call while plugs are added or removed.
323 * @param prior pass in 'NULL' to get the first (most recent) plug,
324 * otherwise pass the value returned on a prior call to uplug_nextPlug
325 * @return the next oldest plugin, or NULL if no more.
326 * @internal ICU 4.4 Technology Preview
328 U_CAPI UPlugData
* U_EXPORT2
329 uplug_nextPlug(UPlugData
*prior
);
332 * Inject a plugin as if it were loaded from a library.
333 * This is useful for testing plugins.
334 * Note that it will have a 'NULL' library pointer associated
335 * with it, and therefore no llibrary will be closed at cleanup time.
336 * Low level plugins may not be able to load, as ordering can't be enforced.
337 * @param entrypoint entrypoint to install
338 * @param config user specified configuration string, if available, or NULL.
339 * @param status error result
340 * @return the new UPlugData associated with this plugin, or NULL if error.
341 * @internal ICU 4.4 Technology Preview
343 U_CAPI UPlugData
* U_EXPORT2
344 uplug_loadPlugFromEntrypoint(UPlugEntrypoint
*entrypoint
, const char *config
, UErrorCode
*status
);
348 * Inject a plugin from a library, as if the information came from a config file.
349 * Low level plugins may not be able to load, and ordering can't be enforced.
350 * @param libName DLL name to load
351 * @param sym symbol of plugin (UPlugEntrypoint function)
352 * @param config configuration string, or NULL
353 * @param status error result
354 * @return the new UPlugData associated with this plugin, or NULL if error.
355 * @internal ICU 4.4 Technology Preview
357 U_CAPI UPlugData
* U_EXPORT2
358 uplug_loadPlugFromLibrary(const char *libName
, const char *sym
, const char *config
, UErrorCode
*status
);
362 * Will request the plugin to be unloaded, and close the library if needed
363 * @param plug plugin handle to close
364 * @param status error result
365 * @internal ICU 4.4 Technology Preview
367 U_CAPI
void U_EXPORT2
368 uplug_removePlug(UPlugData
*plug
, UErrorCode
*status
);
369 #endif /* U_HIDE_INTERNAL_API */