jpayne@69: // © 2016 and later: Unicode, Inc. and others. jpayne@69: // License & terms of use: http://www.unicode.org/copyright.html jpayne@69: /* jpayne@69: ****************************************************************************** jpayne@69: * jpayne@69: * Copyright (C) 2009-2015, International Business Machines jpayne@69: * Corporation and others. All Rights Reserved. jpayne@69: * jpayne@69: ****************************************************************************** jpayne@69: * jpayne@69: * FILE NAME : icuplug.h jpayne@69: * jpayne@69: * Date Name Description jpayne@69: * 10/29/2009 sl New. jpayne@69: ****************************************************************************** jpayne@69: */ jpayne@69: jpayne@69: /** jpayne@69: * \file jpayne@69: * \brief C API: ICU Plugin API jpayne@69: * jpayne@69: *

C API: ICU Plugin API

jpayne@69: * jpayne@69: *

C API allowing run-time loadable modules that extend or modify ICU functionality.

jpayne@69: * jpayne@69: *

Loading and Configuration

jpayne@69: * jpayne@69: *

At ICU startup time, the environment variable "ICU_PLUGINS" will be jpayne@69: * queried for a directory name. If it is not set, the preprocessor symbol jpayne@69: * "DEFAULT_ICU_PLUGINS" will be checked for a default value.

jpayne@69: * jpayne@69: *

Within the above-named directory, the file "icuplugins##.txt" will be jpayne@69: * opened, if present, where ## is the major+minor number of the currently jpayne@69: * running ICU (such as, 44 for ICU 4.4, thus icuplugins44.txt)

jpayne@69: * jpayne@69: *

The configuration file has this format:

jpayne@69: * jpayne@69: * jpayne@69: * jpayne@69: *

An example configuration file is, in its entirety:

jpayne@69: * jpayne@69: * \code jpayne@69: * # this is icuplugins44.txt jpayne@69: * testplug.dll myPlugin hello=world jpayne@69: * \endcode jpayne@69: *

Plugins are categorized as "high" or "low" level. Low level are those jpayne@69: * which must be run BEFORE high level plugins, and before any operations jpayne@69: * which cause ICU to be 'initialized'. If a plugin is low level but jpayne@69: * causes ICU to allocate memory or become initialized, that plugin is said jpayne@69: * to cause a 'level change'.

jpayne@69: * jpayne@69: *

At load time, ICU first queries all plugins to determine their level, jpayne@69: * then loads all 'low' plugins first, and then loads all 'high' plugins. jpayne@69: * Plugins are otherwise loaded in the order listed in the configuration file.

jpayne@69: * jpayne@69: *

Implementing a Plugin

jpayne@69: * \code jpayne@69: * U_CAPI UPlugTokenReturn U_EXPORT2 jpayne@69: * myPlugin (UPlugData *plug, UPlugReason reason, UErrorCode *status) { jpayne@69: * if(reason==UPLUG_REASON_QUERY) { jpayne@69: * uplug_setPlugName(plug, "Simple Plugin"); jpayne@69: * uplug_setPlugLevel(plug, UPLUG_LEVEL_HIGH); jpayne@69: * } else if(reason==UPLUG_REASON_LOAD) { jpayne@69: * ... Set up some ICU things here.... jpayne@69: * } else if(reason==UPLUG_REASON_UNLOAD) { jpayne@69: * ... unload, clean up ... jpayne@69: * } jpayne@69: * return UPLUG_TOKEN; jpayne@69: * } jpayne@69: * \endcode jpayne@69: * jpayne@69: *

The UPlugData* is an opaque pointer to the plugin-specific data, and is jpayne@69: * used in all other API calls.

jpayne@69: * jpayne@69: *

The API contract is:

jpayne@69: *
  1. The plugin MUST always return UPLUG_TOKEN as a return value- to jpayne@69: * indicate that it is a valid plugin.
  2. jpayne@69: * jpayne@69: *
  3. When the 'reason' parameter is set to UPLUG_REASON_QUERY, the jpayne@69: * plugin MUST call uplug_setPlugLevel() to indicate whether it is a high jpayne@69: * level or low level plugin.
  4. jpayne@69: * jpayne@69: *
  5. When the 'reason' parameter is UPLUG_REASON_QUERY, the plugin jpayne@69: * SHOULD call uplug_setPlugName to indicate a human readable plugin name.
jpayne@69: * jpayne@69: * jpayne@69: * \internal ICU 4.4 Technology Preview jpayne@69: */ jpayne@69: jpayne@69: jpayne@69: #ifndef ICUPLUG_H jpayne@69: #define ICUPLUG_H jpayne@69: jpayne@69: #include "unicode/utypes.h" jpayne@69: jpayne@69: jpayne@69: #if UCONFIG_ENABLE_PLUGINS || defined(U_IN_DOXYGEN) jpayne@69: jpayne@69: jpayne@69: jpayne@69: /* === Basic types === */ jpayne@69: jpayne@69: #ifndef U_HIDE_INTERNAL_API jpayne@69: /** jpayne@69: * @{ jpayne@69: * Opaque structure passed to/from a plugin. jpayne@69: * use the APIs to access it. jpayne@69: * @internal ICU 4.4 Technology Preview jpayne@69: */ jpayne@69: jpayne@69: struct UPlugData; jpayne@69: typedef struct UPlugData UPlugData; jpayne@69: jpayne@69: /** @} */ jpayne@69: jpayne@69: /** jpayne@69: * Random Token to identify a valid ICU plugin. Plugins must return this jpayne@69: * from the entrypoint. jpayne@69: * @internal ICU 4.4 Technology Preview jpayne@69: */ jpayne@69: #define UPLUG_TOKEN 0x54762486 jpayne@69: jpayne@69: /** jpayne@69: * Max width of names, symbols, and configuration strings jpayne@69: * @internal ICU 4.4 Technology Preview jpayne@69: */ jpayne@69: #define UPLUG_NAME_MAX 100 jpayne@69: jpayne@69: jpayne@69: /** jpayne@69: * Return value from a plugin entrypoint. jpayne@69: * Must always be set to UPLUG_TOKEN jpayne@69: * @see UPLUG_TOKEN jpayne@69: * @internal ICU 4.4 Technology Preview jpayne@69: */ jpayne@69: typedef uint32_t UPlugTokenReturn; jpayne@69: jpayne@69: /** jpayne@69: * Reason code for the entrypoint's call jpayne@69: * @internal ICU 4.4 Technology Preview jpayne@69: */ jpayne@69: typedef enum { jpayne@69: UPLUG_REASON_QUERY = 0, /**< The plugin is being queried for info. **/ jpayne@69: UPLUG_REASON_LOAD = 1, /**< The plugin is being loaded. **/ jpayne@69: UPLUG_REASON_UNLOAD = 2, /**< The plugin is being unloaded. **/ jpayne@69: /** jpayne@69: * Number of known reasons. jpayne@69: * @internal The numeric value may change over time, see ICU ticket #12420. jpayne@69: */ jpayne@69: UPLUG_REASON_COUNT jpayne@69: } UPlugReason; jpayne@69: jpayne@69: jpayne@69: /** jpayne@69: * Level of plugin loading jpayne@69: * INITIAL: UNKNOWN jpayne@69: * QUERY: INVALID -> { LOW | HIGH } jpayne@69: * ERR -> INVALID jpayne@69: * @internal ICU 4.4 Technology Preview jpayne@69: */ jpayne@69: typedef enum { jpayne@69: UPLUG_LEVEL_INVALID = 0, /**< The plugin is invalid, hasn't called uplug_setLevel, or can't load. **/ jpayne@69: UPLUG_LEVEL_UNKNOWN = 1, /**< The plugin is waiting to be installed. **/ jpayne@69: UPLUG_LEVEL_LOW = 2, /**< The plugin must be called before u_init completes **/ jpayne@69: UPLUG_LEVEL_HIGH = 3, /**< The plugin can run at any time. **/ jpayne@69: /** jpayne@69: * Number of known levels. jpayne@69: * @internal The numeric value may change over time, see ICU ticket #12420. jpayne@69: */ jpayne@69: UPLUG_LEVEL_COUNT jpayne@69: } UPlugLevel; jpayne@69: jpayne@69: /** jpayne@69: * Entrypoint for an ICU plugin. jpayne@69: * @param plug the UPlugData handle. jpayne@69: * @param status the plugin's extended status code. jpayne@69: * @return A valid plugin must return UPLUG_TOKEN jpayne@69: * @internal ICU 4.4 Technology Preview jpayne@69: */ jpayne@69: typedef UPlugTokenReturn (U_EXPORT2 UPlugEntrypoint) ( jpayne@69: UPlugData *plug, jpayne@69: UPlugReason reason, jpayne@69: UErrorCode *status); jpayne@69: jpayne@69: /* === Needed for Implementing === */ jpayne@69: jpayne@69: /** jpayne@69: * Request that this plugin not be unloaded at cleanup time. jpayne@69: * This is appropriate for plugins which cannot be cleaned up. jpayne@69: * @see u_cleanup() jpayne@69: * @param plug plugin jpayne@69: * @param dontUnload set true if this plugin can't be unloaded jpayne@69: * @internal ICU 4.4 Technology Preview jpayne@69: */ jpayne@69: U_INTERNAL void U_EXPORT2 jpayne@69: uplug_setPlugNoUnload(UPlugData *plug, UBool dontUnload); jpayne@69: jpayne@69: /** jpayne@69: * Set the level of this plugin. jpayne@69: * @param plug plugin data handle jpayne@69: * @param level the level of this plugin jpayne@69: * @internal ICU 4.4 Technology Preview jpayne@69: */ jpayne@69: U_INTERNAL void U_EXPORT2 jpayne@69: uplug_setPlugLevel(UPlugData *plug, UPlugLevel level); jpayne@69: jpayne@69: /** jpayne@69: * Get the level of this plugin. jpayne@69: * @param plug plugin data handle jpayne@69: * @return the level of this plugin jpayne@69: * @internal ICU 4.4 Technology Preview jpayne@69: */ jpayne@69: U_INTERNAL UPlugLevel U_EXPORT2 jpayne@69: uplug_getPlugLevel(UPlugData *plug); jpayne@69: jpayne@69: /** jpayne@69: * Get the lowest level of plug which can currently load. jpayne@69: * For example, if UPLUG_LEVEL_LOW is returned, then low level plugins may load jpayne@69: * if UPLUG_LEVEL_HIGH is returned, then only high level plugins may load. jpayne@69: * @return the lowest level of plug which can currently load jpayne@69: * @internal ICU 4.4 Technology Preview jpayne@69: */ jpayne@69: U_INTERNAL UPlugLevel U_EXPORT2 jpayne@69: uplug_getCurrentLevel(void); jpayne@69: jpayne@69: jpayne@69: /** jpayne@69: * Get plug load status jpayne@69: * @return The error code of this plugin's load attempt. jpayne@69: * @internal ICU 4.4 Technology Preview jpayne@69: */ jpayne@69: U_INTERNAL UErrorCode U_EXPORT2 jpayne@69: uplug_getPlugLoadStatus(UPlugData *plug); jpayne@69: jpayne@69: /** jpayne@69: * Set the human-readable name of this plugin. jpayne@69: * @param plug plugin data handle jpayne@69: * @param name the name of this plugin. The first UPLUG_NAME_MAX characters willi be copied into a new buffer. jpayne@69: * @internal ICU 4.4 Technology Preview jpayne@69: */ jpayne@69: U_INTERNAL void U_EXPORT2 jpayne@69: uplug_setPlugName(UPlugData *plug, const char *name); jpayne@69: jpayne@69: /** jpayne@69: * Get the human-readable name of this plugin. jpayne@69: * @param plug plugin data handle jpayne@69: * @return the name of this plugin jpayne@69: * @internal ICU 4.4 Technology Preview jpayne@69: */ jpayne@69: U_INTERNAL const char * U_EXPORT2 jpayne@69: uplug_getPlugName(UPlugData *plug); jpayne@69: jpayne@69: /** jpayne@69: * Return the symbol name for this plugin, if known. jpayne@69: * @param plug plugin data handle jpayne@69: * @return the symbol name, or NULL jpayne@69: * @internal ICU 4.4 Technology Preview jpayne@69: */ jpayne@69: U_INTERNAL const char * U_EXPORT2 jpayne@69: uplug_getSymbolName(UPlugData *plug); jpayne@69: jpayne@69: /** jpayne@69: * Return the library name for this plugin, if known. jpayne@69: * @param plug plugin data handle jpayne@69: * @param status error code jpayne@69: * @return the library name, or NULL jpayne@69: * @internal ICU 4.4 Technology Preview jpayne@69: */ jpayne@69: U_INTERNAL const char * U_EXPORT2 jpayne@69: uplug_getLibraryName(UPlugData *plug, UErrorCode *status); jpayne@69: jpayne@69: /** jpayne@69: * Return the library used for this plugin, if known. jpayne@69: * Plugins could use this to load data out of their jpayne@69: * @param plug plugin data handle jpayne@69: * @return the library, or NULL jpayne@69: * @internal ICU 4.4 Technology Preview jpayne@69: */ jpayne@69: U_INTERNAL void * U_EXPORT2 jpayne@69: uplug_getLibrary(UPlugData *plug); jpayne@69: jpayne@69: /** jpayne@69: * Return the plugin-specific context data. jpayne@69: * @param plug plugin data handle jpayne@69: * @return the context, or NULL if not set jpayne@69: * @internal ICU 4.4 Technology Preview jpayne@69: */ jpayne@69: U_INTERNAL void * U_EXPORT2 jpayne@69: uplug_getContext(UPlugData *plug); jpayne@69: jpayne@69: /** jpayne@69: * Set the plugin-specific context data. jpayne@69: * @param plug plugin data handle jpayne@69: * @param context new context to set jpayne@69: * @internal ICU 4.4 Technology Preview jpayne@69: */ jpayne@69: U_INTERNAL void U_EXPORT2 jpayne@69: uplug_setContext(UPlugData *plug, void *context); jpayne@69: jpayne@69: jpayne@69: /** jpayne@69: * Get the configuration string, if available. jpayne@69: * The string is in the platform default codepage. jpayne@69: * @param plug plugin data handle jpayne@69: * @return configuration string, or else null. jpayne@69: * @internal ICU 4.4 Technology Preview jpayne@69: */ jpayne@69: U_INTERNAL const char * U_EXPORT2 jpayne@69: uplug_getConfiguration(UPlugData *plug); jpayne@69: jpayne@69: /** jpayne@69: * Return all currently installed plugins, from newest to oldest jpayne@69: * Usage Example: jpayne@69: * \code jpayne@69: * UPlugData *plug = NULL; jpayne@69: * while(plug=uplug_nextPlug(plug)) { jpayne@69: * ... do something with 'plug' ... jpayne@69: * } jpayne@69: * \endcode jpayne@69: * Not thread safe- do not call while plugs are added or removed. jpayne@69: * @param prior pass in 'NULL' to get the first (most recent) plug, jpayne@69: * otherwise pass the value returned on a prior call to uplug_nextPlug jpayne@69: * @return the next oldest plugin, or NULL if no more. jpayne@69: * @internal ICU 4.4 Technology Preview jpayne@69: */ jpayne@69: U_INTERNAL UPlugData* U_EXPORT2 jpayne@69: uplug_nextPlug(UPlugData *prior); jpayne@69: jpayne@69: /** jpayne@69: * Inject a plugin as if it were loaded from a library. jpayne@69: * This is useful for testing plugins. jpayne@69: * Note that it will have a 'NULL' library pointer associated jpayne@69: * with it, and therefore no llibrary will be closed at cleanup time. jpayne@69: * Low level plugins may not be able to load, as ordering can't be enforced. jpayne@69: * @param entrypoint entrypoint to install jpayne@69: * @param config user specified configuration string, if available, or NULL. jpayne@69: * @param status error result jpayne@69: * @return the new UPlugData associated with this plugin, or NULL if error. jpayne@69: * @internal ICU 4.4 Technology Preview jpayne@69: */ jpayne@69: U_INTERNAL UPlugData* U_EXPORT2 jpayne@69: uplug_loadPlugFromEntrypoint(UPlugEntrypoint *entrypoint, const char *config, UErrorCode *status); jpayne@69: jpayne@69: jpayne@69: /** jpayne@69: * Inject a plugin from a library, as if the information came from a config file. jpayne@69: * Low level plugins may not be able to load, and ordering can't be enforced. jpayne@69: * @param libName DLL name to load jpayne@69: * @param sym symbol of plugin (UPlugEntrypoint function) jpayne@69: * @param config configuration string, or NULL jpayne@69: * @param status error result jpayne@69: * @return the new UPlugData associated with this plugin, or NULL if error. jpayne@69: * @internal ICU 4.4 Technology Preview jpayne@69: */ jpayne@69: U_INTERNAL UPlugData* U_EXPORT2 jpayne@69: uplug_loadPlugFromLibrary(const char *libName, const char *sym, const char *config, UErrorCode *status); jpayne@69: jpayne@69: /** jpayne@69: * Remove a plugin. jpayne@69: * Will request the plugin to be unloaded, and close the library if needed jpayne@69: * @param plug plugin handle to close jpayne@69: * @param status error result jpayne@69: * @internal ICU 4.4 Technology Preview jpayne@69: */ jpayne@69: U_INTERNAL void U_EXPORT2 jpayne@69: uplug_removePlug(UPlugData *plug, UErrorCode *status); jpayne@69: #endif /* U_HIDE_INTERNAL_API */ jpayne@69: jpayne@69: #endif /* UCONFIG_ENABLE_PLUGINS */ jpayne@69: jpayne@69: #endif /* _ICUPLUG */ jpayne@69: