1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 ******************************************************************************
6 * Copyright (C) 2009-2015, International Business Machines
7 * Corporation and others. All Rights Reserved.
9 ******************************************************************************
11 * FILE NAME : icuplug.h
13 * Date Name Description
15 ******************************************************************************
20 * \brief C API: ICU Plugin API
22 * <h2>C API: ICU Plugin API</h2>
24 * <p>C API allowing run-time loadable modules that extend or modify ICU functionality.</p>
26 * <h3>Loading and Configuration</h3>
28 * <p>At ICU startup time, the environment variable "ICU_PLUGINS" will be
29 * queried for a directory name. If it is not set, the preprocessor symbol
30 * "DEFAULT_ICU_PLUGINS" will be checked for a default value.</p>
32 * <p>Within the above-named directory, the file "icuplugins##.txt" will be
33 * opened, if present, where ## is the major+minor number of the currently
34 * running ICU (such as, 44 for ICU 4.4, thus icuplugins44.txt)</p>
36 * <p>The configuration file has this format:</p>
39 * <li>Hash (#) begins a comment line</li>
41 * <li>Non-comment lines have two or three components:
42 * LIBRARYNAME ENTRYPOINT [ CONFIGURATION .. ]</li>
44 * <li>Tabs or spaces separate the three items.</li>
46 * <li>LIBRARYNAME is the name of a shared library, either a short name if
47 * it is on the loader path, or a full pathname.</li>
49 * <li>ENTRYPOINT is the short (undecorated) symbol name of the plugin's
50 * entrypoint, as above.</li>
52 * <li>CONFIGURATION is the entire rest of the line . It's passed as-is to
56 * <p>An example configuration file is, in its entirety:</p>
59 * # this is icuplugins44.txt
60 * testplug.dll myPlugin hello=world
62 * <p>Plugins are categorized as "high" or "low" level. Low level are those
63 * which must be run BEFORE high level plugins, and before any operations
64 * which cause ICU to be 'initialized'. If a plugin is low level but
65 * causes ICU to allocate memory or become initialized, that plugin is said
66 * to cause a 'level change'. </p>
68 * <p>At load time, ICU first queries all plugins to determine their level,
69 * then loads all 'low' plugins first, and then loads all 'high' plugins.
70 * Plugins are otherwise loaded in the order listed in the configuration file.</p>
72 * <h3>Implementing a Plugin</h3>
74 * U_CAPI UPlugTokenReturn U_EXPORT2
75 * myPlugin (UPlugData *plug, UPlugReason reason, UErrorCode *status) {
76 * if(reason==UPLUG_REASON_QUERY) {
77 * uplug_setPlugName(plug, "Simple Plugin");
78 * uplug_setPlugLevel(plug, UPLUG_LEVEL_HIGH);
79 * } else if(reason==UPLUG_REASON_LOAD) {
80 * ... Set up some ICU things here....
81 * } else if(reason==UPLUG_REASON_UNLOAD) {
82 * ... unload, clean up ...
88 * <p>The UPlugData* is an opaque pointer to the plugin-specific data, and is
89 * used in all other API calls.</p>
91 * <p>The API contract is:</p>
92 * <ol><li>The plugin MUST always return UPLUG_TOKEN as a return value- to
93 * indicate that it is a valid plugin.</li>
95 * <li>When the 'reason' parameter is set to UPLUG_REASON_QUERY, the
96 * plugin MUST call uplug_setPlugLevel() to indicate whether it is a high
97 * level or low level plugin.</li>
99 * <li>When the 'reason' parameter is UPLUG_REASON_QUERY, the plugin
100 * SHOULD call uplug_setPlugName to indicate a human readable plugin name.</li></ol>
103 * \internal ICU 4.4 Technology Preview
110 #include "unicode/utypes.h"
113 #if UCONFIG_ENABLE_PLUGINS || defined(U_IN_DOXYGEN)
117 /* === Basic types === */
119 #ifndef U_HIDE_INTERNAL_API
123 * Typedef for opaque structure passed to/from a plugin.
124 * Use the APIs to access it.
125 * @internal ICU 4.4 Technology Preview
127 typedef struct UPlugData UPlugData;
132 * Random Token to identify a valid ICU plugin. Plugins must return this
133 * from the entrypoint.
134 * @internal ICU 4.4 Technology Preview
136 #define UPLUG_TOKEN 0x54762486
139 * Max width of names, symbols, and configuration strings
140 * @internal ICU 4.4 Technology Preview
142 #define UPLUG_NAME_MAX 100
146 * Return value from a plugin entrypoint.
147 * Must always be set to UPLUG_TOKEN
149 * @internal ICU 4.4 Technology Preview
151 typedef uint32_t UPlugTokenReturn;
154 * Reason code for the entrypoint's call
155 * @internal ICU 4.4 Technology Preview
158 UPLUG_REASON_QUERY = 0, /**< The plugin is being queried for info. **/
159 UPLUG_REASON_LOAD = 1, /**< The plugin is being loaded. **/
160 UPLUG_REASON_UNLOAD = 2, /**< The plugin is being unloaded. **/
162 * Number of known reasons.
163 * @internal The numeric value may change over time, see ICU ticket #12420.
170 * Level of plugin loading
172 * QUERY: INVALID -> { LOW | HIGH }
174 * @internal ICU 4.4 Technology Preview
177 UPLUG_LEVEL_INVALID = 0, /**< The plugin is invalid, hasn't called uplug_setLevel, or can't load. **/
178 UPLUG_LEVEL_UNKNOWN = 1, /**< The plugin is waiting to be installed. **/
179 UPLUG_LEVEL_LOW = 2, /**< The plugin must be called before u_init completes **/
180 UPLUG_LEVEL_HIGH = 3, /**< The plugin can run at any time. **/
182 * Number of known levels.
183 * @internal The numeric value may change over time, see ICU ticket #12420.
189 * Entrypoint for an ICU plugin.
190 * @param plug the UPlugData handle.
191 * @param status the plugin's extended status code.
192 * @return A valid plugin must return UPLUG_TOKEN
193 * @internal ICU 4.4 Technology Preview
195 typedef UPlugTokenReturn (U_EXPORT2 UPlugEntrypoint) (
200 /* === Needed for Implementing === */
203 * Request that this plugin not be unloaded at cleanup time.
204 * This is appropriate for plugins which cannot be cleaned up.
207 * @param dontUnload set true if this plugin can't be unloaded
208 * @internal ICU 4.4 Technology Preview
210 U_CAPI void U_EXPORT2
211 uplug_setPlugNoUnload(UPlugData *plug, UBool dontUnload);
214 * Set the level of this plugin.
215 * @param plug plugin data handle
216 * @param level the level of this plugin
217 * @internal ICU 4.4 Technology Preview
219 U_CAPI void U_EXPORT2
220 uplug_setPlugLevel(UPlugData *plug, UPlugLevel level);
223 * Get the level of this plugin.
224 * @param plug plugin data handle
225 * @return the level of this plugin
226 * @internal ICU 4.4 Technology Preview
228 U_CAPI UPlugLevel U_EXPORT2
229 uplug_getPlugLevel(UPlugData *plug);
232 * Get the lowest level of plug which can currently load.
233 * For example, if UPLUG_LEVEL_LOW is returned, then low level plugins may load
234 * if UPLUG_LEVEL_HIGH is returned, then only high level plugins may load.
235 * @return the lowest level of plug which can currently load
236 * @internal ICU 4.4 Technology Preview
238 U_CAPI UPlugLevel U_EXPORT2
239 uplug_getCurrentLevel(void);
243 * Get plug load status
244 * @return The error code of this plugin's load attempt.
245 * @internal ICU 4.4 Technology Preview
247 U_CAPI UErrorCode U_EXPORT2
248 uplug_getPlugLoadStatus(UPlugData *plug);
251 * Set the human-readable name of this plugin.
252 * @param plug plugin data handle
253 * @param name the name of this plugin. The first UPLUG_NAME_MAX characters willi be copied into a new buffer.
254 * @internal ICU 4.4 Technology Preview
256 U_CAPI void U_EXPORT2
257 uplug_setPlugName(UPlugData *plug, const char *name);
260 * Get the human-readable name of this plugin.
261 * @param plug plugin data handle
262 * @return the name of this plugin
263 * @internal ICU 4.4 Technology Preview
265 U_CAPI const char * U_EXPORT2
266 uplug_getPlugName(UPlugData *plug);
269 * Return the symbol name for this plugin, if known.
270 * @param plug plugin data handle
271 * @return the symbol name, or NULL
272 * @internal ICU 4.4 Technology Preview
274 U_CAPI const char * U_EXPORT2
275 uplug_getSymbolName(UPlugData *plug);
278 * Return the library name for this plugin, if known.
279 * @param plug plugin data handle
280 * @param status error code
281 * @return the library name, or NULL
282 * @internal ICU 4.4 Technology Preview
284 U_CAPI const char * U_EXPORT2
285 uplug_getLibraryName(UPlugData *plug, UErrorCode *status);
288 * Return the library used for this plugin, if known.
289 * Plugins could use this to load data out of their
290 * @param plug plugin data handle
291 * @return the library, or NULL
292 * @internal ICU 4.4 Technology Preview
294 U_CAPI void * U_EXPORT2
295 uplug_getLibrary(UPlugData *plug);
298 * Return the plugin-specific context data.
299 * @param plug plugin data handle
300 * @return the context, or NULL if not set
301 * @internal ICU 4.4 Technology Preview
303 U_CAPI void * U_EXPORT2
304 uplug_getContext(UPlugData *plug);
307 * Set the plugin-specific context data.
308 * @param plug plugin data handle
309 * @param context new context to set
310 * @internal ICU 4.4 Technology Preview
312 U_CAPI void U_EXPORT2
313 uplug_setContext(UPlugData *plug, void *context);
317 * Get the configuration string, if available.
318 * The string is in the platform default codepage.
319 * @param plug plugin data handle
320 * @return configuration string, or else null.
321 * @internal ICU 4.4 Technology Preview
323 U_CAPI const char * U_EXPORT2
324 uplug_getConfiguration(UPlugData *plug);
327 * Return all currently installed plugins, from newest to oldest
330 * UPlugData *plug = NULL;
331 * while(plug=uplug_nextPlug(plug)) {
332 * ... do something with 'plug' ...
335 * Not thread safe- do not call while plugs are added or removed.
336 * @param prior pass in 'NULL' to get the first (most recent) plug,
337 * otherwise pass the value returned on a prior call to uplug_nextPlug
338 * @return the next oldest plugin, or NULL if no more.
339 * @internal ICU 4.4 Technology Preview
341 U_CAPI UPlugData* U_EXPORT2
342 uplug_nextPlug(UPlugData *prior);
345 * Inject a plugin as if it were loaded from a library.
346 * This is useful for testing plugins.
347 * Note that it will have a 'NULL' library pointer associated
348 * with it, and therefore no llibrary will be closed at cleanup time.
349 * Low level plugins may not be able to load, as ordering can't be enforced.
350 * @param entrypoint entrypoint to install
351 * @param config user specified configuration string, if available, or NULL.
352 * @param status error result
353 * @return the new UPlugData associated with this plugin, or NULL if error.
354 * @internal ICU 4.4 Technology Preview
356 U_CAPI UPlugData* U_EXPORT2
357 uplug_loadPlugFromEntrypoint(UPlugEntrypoint *entrypoint, const char *config, UErrorCode *status);
361 * Inject a plugin from a library, as if the information came from a config file.
362 * Low level plugins may not be able to load, and ordering can't be enforced.
363 * @param libName DLL name to load
364 * @param sym symbol of plugin (UPlugEntrypoint function)
365 * @param config configuration string, or NULL
366 * @param status error result
367 * @return the new UPlugData associated with this plugin, or NULL if error.
368 * @internal ICU 4.4 Technology Preview
370 U_CAPI UPlugData* U_EXPORT2
371 uplug_loadPlugFromLibrary(const char *libName, const char *sym, const char *config, UErrorCode *status);
375 * Will request the plugin to be unloaded, and close the library if needed
376 * @param plug plugin handle to close
377 * @param status error result
378 * @internal ICU 4.4 Technology Preview
380 U_CAPI void U_EXPORT2
381 uplug_removePlug(UPlugData *plug, UErrorCode *status);
382 #endif /* U_HIDE_INTERNAL_API */
384 #endif /* UCONFIG_ENABLE_PLUGINS */
386 #endif /* _ICUPLUG */