michael@0: /*
michael@0: ******************************************************************************
michael@0: *
michael@0: * Copyright (C) 2009-2012, International Business Machines
michael@0: * Corporation and others. All Rights Reserved.
michael@0: *
michael@0: ******************************************************************************
michael@0: *
michael@0: * FILE NAME : icuplug.h
michael@0: *
michael@0: * Date Name Description
michael@0: * 10/29/2009 sl New.
michael@0: ******************************************************************************
michael@0: */
michael@0:
michael@0: /**
michael@0: * \file
michael@0: * \brief C API: ICU Plugin API
michael@0: *
michael@0: *
C API: ICU Plugin API
michael@0: *
michael@0: * C API allowing run-time loadable modules that extend or modify ICU functionality.
michael@0: *
michael@0: * Loading and Configuration
michael@0: *
michael@0: * At ICU startup time, the environment variable "ICU_PLUGINS" will be
michael@0: * queried for a directory name. If it is not set, the preprocessor symbol
michael@0: * "DEFAULT_ICU_PLUGINS" will be checked for a default value.
michael@0: *
michael@0: * Within the above-named directory, the file "icuplugins##.txt" will be
michael@0: * opened, if present, where ## is the major+minor number of the currently
michael@0: * running ICU (such as, 44 for ICU 4.4, thus icuplugins44.txt)
michael@0: *
michael@0: * The configuration file has this format:
michael@0: *
michael@0: *
michael@0: * - Hash (#) begins a comment line
michael@0: *
michael@0: * - Non-comment lines have two or three components:
michael@0: * LIBRARYNAME ENTRYPOINT [ CONFIGURATION .. ]
michael@0: *
michael@0: * - Tabs or spaces separate the three items.
michael@0: *
michael@0: * - LIBRARYNAME is the name of a shared library, either a short name if
michael@0: * it is on the loader path, or a full pathname.
michael@0: *
michael@0: * - ENTRYPOINT is the short (undecorated) symbol name of the plugin's
michael@0: * entrypoint, as above.
michael@0: *
michael@0: * - CONFIGURATION is the entire rest of the line . It's passed as-is to
michael@0: * the plugin.
michael@0: *
michael@0: *
michael@0: * An example configuration file is, in its entirety:
michael@0: *
michael@0: * \code
michael@0: * # this is icuplugins44.txt
michael@0: * testplug.dll myPlugin hello=world
michael@0: * \endcode
michael@0: * Plugins are categorized as "high" or "low" level. Low level are those
michael@0: * which must be run BEFORE high level plugins, and before any operations
michael@0: * which cause ICU to be 'initialized'. If a plugin is low level but
michael@0: * causes ICU to allocate memory or become initialized, that plugin is said
michael@0: * to cause a 'level change'.
michael@0: *
michael@0: * At load time, ICU first queries all plugins to determine their level,
michael@0: * then loads all 'low' plugins first, and then loads all 'high' plugins.
michael@0: * Plugins are otherwise loaded in the order listed in the configuration file.
michael@0: *
michael@0: * Implementing a Plugin
michael@0: * \code
michael@0: * U_CAPI UPlugTokenReturn U_EXPORT2
michael@0: * myPlugin (UPlugData *plug, UPlugReason reason, UErrorCode *status) {
michael@0: * if(reason==UPLUG_REASON_QUERY) {
michael@0: * uplug_setPlugName(plug, "Simple Plugin");
michael@0: * uplug_setPlugLevel(plug, UPLUG_LEVEL_HIGH);
michael@0: * } else if(reason==UPLUG_REASON_LOAD) {
michael@0: * ... Set up some ICU things here....
michael@0: * } else if(reason==UPLUG_REASON_UNLOAD) {
michael@0: * ... unload, clean up ...
michael@0: * }
michael@0: * return UPLUG_TOKEN;
michael@0: * }
michael@0: * \endcode
michael@0: *
michael@0: * The UPlugData* is an opaque pointer to the plugin-specific data, and is
michael@0: * used in all other API calls.
michael@0: *
michael@0: * The API contract is:
michael@0: * - The plugin MUST always return UPLUG_TOKEN as a return value- to
michael@0: * indicate that it is a valid plugin.
michael@0: *
michael@0: * - When the 'reason' parameter is set to UPLUG_REASON_QUERY, the
michael@0: * plugin MUST call uplug_setPlugLevel() to indicate whether it is a high
michael@0: * level or low level plugin.
michael@0: *
michael@0: * - When the 'reason' parameter is UPLUG_REASON_QUERY, the plugin
michael@0: * SHOULD call uplug_setPlugName to indicate a human readable plugin name.
michael@0: *
michael@0: *
michael@0: * \internal ICU 4.4 Technology Preview
michael@0: */
michael@0:
michael@0:
michael@0: #ifndef ICUPLUG_H
michael@0: #define ICUPLUG_H
michael@0:
michael@0: #include "unicode/utypes.h"
michael@0:
michael@0:
michael@0: /* === Basic types === */
michael@0:
michael@0: #ifndef U_HIDE_INTERNAL_API
michael@0: /**
michael@0: * @{
michael@0: * Opaque structure passed to/from a plugin.
michael@0: * use the APIs to access it.
michael@0: * @internal ICU 4.4 Technology Preview
michael@0: */
michael@0:
michael@0: struct UPlugData;
michael@0: typedef struct UPlugData UPlugData;
michael@0:
michael@0: /** @} */
michael@0:
michael@0: /**
michael@0: * Random Token to identify a valid ICU plugin. Plugins must return this
michael@0: * from the entrypoint.
michael@0: * @internal ICU 4.4 Technology Preview
michael@0: */
michael@0: #define UPLUG_TOKEN 0x54762486
michael@0:
michael@0: /**
michael@0: * Max width of names, symbols, and configuration strings
michael@0: * @internal ICU 4.4 Technology Preview
michael@0: */
michael@0: #define UPLUG_NAME_MAX 100
michael@0:
michael@0:
michael@0: /**
michael@0: * Return value from a plugin entrypoint.
michael@0: * Must always be set to UPLUG_TOKEN
michael@0: * @see UPLUG_TOKEN
michael@0: * @internal ICU 4.4 Technology Preview
michael@0: */
michael@0: typedef uint32_t UPlugTokenReturn;
michael@0:
michael@0: /**
michael@0: * Reason code for the entrypoint's call
michael@0: * @internal ICU 4.4 Technology Preview
michael@0: */
michael@0: typedef enum {
michael@0: UPLUG_REASON_QUERY = 0, /**< The plugin is being queried for info. **/
michael@0: UPLUG_REASON_LOAD = 1, /**< The plugin is being loaded. **/
michael@0: UPLUG_REASON_UNLOAD = 2, /**< The plugin is being unloaded. **/
michael@0: UPLUG_REASON_COUNT /**< count of known reasons **/
michael@0: } UPlugReason;
michael@0:
michael@0:
michael@0: /**
michael@0: * Level of plugin loading
michael@0: * INITIAL: UNKNOWN
michael@0: * QUERY: INVALID -> { LOW | HIGH }
michael@0: * ERR -> INVALID
michael@0: * @internal ICU 4.4 Technology Preview
michael@0: */
michael@0: typedef enum {
michael@0: UPLUG_LEVEL_INVALID = 0, /**< The plugin is invalid, hasn't called uplug_setLevel, or can't load. **/
michael@0: UPLUG_LEVEL_UNKNOWN = 1, /**< The plugin is waiting to be installed. **/
michael@0: UPLUG_LEVEL_LOW = 2, /**< The plugin must be called before u_init completes **/
michael@0: UPLUG_LEVEL_HIGH = 3, /**< The plugin can run at any time. **/
michael@0: UPLUG_LEVEL_COUNT /**< count of known reasons **/
michael@0: } UPlugLevel;
michael@0:
michael@0: /**
michael@0: * Entrypoint for an ICU plugin.
michael@0: * @param plug the UPlugData handle.
michael@0: * @param status the plugin's extended status code.
michael@0: * @return A valid plugin must return UPLUG_TOKEN
michael@0: * @internal ICU 4.4 Technology Preview
michael@0: */
michael@0: typedef UPlugTokenReturn (U_EXPORT2 UPlugEntrypoint) (
michael@0: UPlugData *plug,
michael@0: UPlugReason reason,
michael@0: UErrorCode *status);
michael@0:
michael@0: /* === Needed for Implementing === */
michael@0:
michael@0: /**
michael@0: * Request that this plugin not be unloaded at cleanup time.
michael@0: * This is appropriate for plugins which cannot be cleaned up.
michael@0: * @see u_cleanup()
michael@0: * @param plug plugin
michael@0: * @param dontUnload set true if this plugin can't be unloaded
michael@0: * @internal ICU 4.4 Technology Preview
michael@0: */
michael@0: U_INTERNAL void U_EXPORT2
michael@0: uplug_setPlugNoUnload(UPlugData *plug, UBool dontUnload);
michael@0:
michael@0: /**
michael@0: * Set the level of this plugin.
michael@0: * @param plug plugin data handle
michael@0: * @param level the level of this plugin
michael@0: * @internal ICU 4.4 Technology Preview
michael@0: */
michael@0: U_INTERNAL void U_EXPORT2
michael@0: uplug_setPlugLevel(UPlugData *plug, UPlugLevel level);
michael@0:
michael@0: /**
michael@0: * Get the level of this plugin.
michael@0: * @param plug plugin data handle
michael@0: * @return the level of this plugin
michael@0: * @internal ICU 4.4 Technology Preview
michael@0: */
michael@0: U_INTERNAL UPlugLevel U_EXPORT2
michael@0: uplug_getPlugLevel(UPlugData *plug);
michael@0:
michael@0: /**
michael@0: * Get the lowest level of plug which can currently load.
michael@0: * For example, if UPLUG_LEVEL_LOW is returned, then low level plugins may load
michael@0: * if UPLUG_LEVEL_HIGH is returned, then only high level plugins may load.
michael@0: * @return the lowest level of plug which can currently load
michael@0: * @internal ICU 4.4 Technology Preview
michael@0: */
michael@0: U_INTERNAL UPlugLevel U_EXPORT2
michael@0: uplug_getCurrentLevel(void);
michael@0:
michael@0:
michael@0: /**
michael@0: * Get plug load status
michael@0: * @return The error code of this plugin's load attempt.
michael@0: * @internal ICU 4.4 Technology Preview
michael@0: */
michael@0: U_INTERNAL UErrorCode U_EXPORT2
michael@0: uplug_getPlugLoadStatus(UPlugData *plug);
michael@0:
michael@0: /**
michael@0: * Set the human-readable name of this plugin.
michael@0: * @param plug plugin data handle
michael@0: * @param name the name of this plugin. The first UPLUG_NAME_MAX characters willi be copied into a new buffer.
michael@0: * @internal ICU 4.4 Technology Preview
michael@0: */
michael@0: U_INTERNAL void U_EXPORT2
michael@0: uplug_setPlugName(UPlugData *plug, const char *name);
michael@0:
michael@0: /**
michael@0: * Get the human-readable name of this plugin.
michael@0: * @param plug plugin data handle
michael@0: * @return the name of this plugin
michael@0: * @internal ICU 4.4 Technology Preview
michael@0: */
michael@0: U_INTERNAL const char * U_EXPORT2
michael@0: uplug_getPlugName(UPlugData *plug);
michael@0:
michael@0: /**
michael@0: * Return the symbol name for this plugin, if known.
michael@0: * @param plug plugin data handle
michael@0: * @return the symbol name, or NULL
michael@0: * @internal ICU 4.4 Technology Preview
michael@0: */
michael@0: U_INTERNAL const char * U_EXPORT2
michael@0: uplug_getSymbolName(UPlugData *plug);
michael@0:
michael@0: /**
michael@0: * Return the library name for this plugin, if known.
michael@0: * @param plug plugin data handle
michael@0: * @param status error code
michael@0: * @return the library name, or NULL
michael@0: * @internal ICU 4.4 Technology Preview
michael@0: */
michael@0: U_INTERNAL const char * U_EXPORT2
michael@0: uplug_getLibraryName(UPlugData *plug, UErrorCode *status);
michael@0:
michael@0: /**
michael@0: * Return the library used for this plugin, if known.
michael@0: * Plugins could use this to load data out of their
michael@0: * @param plug plugin data handle
michael@0: * @return the library, or NULL
michael@0: * @internal ICU 4.4 Technology Preview
michael@0: */
michael@0: U_INTERNAL void * U_EXPORT2
michael@0: uplug_getLibrary(UPlugData *plug);
michael@0:
michael@0: /**
michael@0: * Return the plugin-specific context data.
michael@0: * @param plug plugin data handle
michael@0: * @return the context, or NULL if not set
michael@0: * @internal ICU 4.4 Technology Preview
michael@0: */
michael@0: U_INTERNAL void * U_EXPORT2
michael@0: uplug_getContext(UPlugData *plug);
michael@0:
michael@0: /**
michael@0: * Set the plugin-specific context data.
michael@0: * @param plug plugin data handle
michael@0: * @param context new context to set
michael@0: * @internal ICU 4.4 Technology Preview
michael@0: */
michael@0: U_INTERNAL void U_EXPORT2
michael@0: uplug_setContext(UPlugData *plug, void *context);
michael@0:
michael@0:
michael@0: /**
michael@0: * Get the configuration string, if available.
michael@0: * The string is in the platform default codepage.
michael@0: * @param plug plugin data handle
michael@0: * @return configuration string, or else null.
michael@0: * @internal ICU 4.4 Technology Preview
michael@0: */
michael@0: U_INTERNAL const char * U_EXPORT2
michael@0: uplug_getConfiguration(UPlugData *plug);
michael@0:
michael@0: /**
michael@0: * Return all currently installed plugins, from newest to oldest
michael@0: * Usage Example:
michael@0: * \code
michael@0: * UPlugData *plug = NULL;
michael@0: * while(plug=uplug_nextPlug(plug)) {
michael@0: * ... do something with 'plug' ...
michael@0: * }
michael@0: * \endcode
michael@0: * Not thread safe- do not call while plugs are added or removed.
michael@0: * @param prior pass in 'NULL' to get the first (most recent) plug,
michael@0: * otherwise pass the value returned on a prior call to uplug_nextPlug
michael@0: * @return the next oldest plugin, or NULL if no more.
michael@0: * @internal ICU 4.4 Technology Preview
michael@0: */
michael@0: U_INTERNAL UPlugData* U_EXPORT2
michael@0: uplug_nextPlug(UPlugData *prior);
michael@0:
michael@0: /**
michael@0: * Inject a plugin as if it were loaded from a library.
michael@0: * This is useful for testing plugins.
michael@0: * Note that it will have a 'NULL' library pointer associated
michael@0: * with it, and therefore no llibrary will be closed at cleanup time.
michael@0: * Low level plugins may not be able to load, as ordering can't be enforced.
michael@0: * @param entrypoint entrypoint to install
michael@0: * @param config user specified configuration string, if available, or NULL.
michael@0: * @param status error result
michael@0: * @return the new UPlugData associated with this plugin, or NULL if error.
michael@0: * @internal ICU 4.4 Technology Preview
michael@0: */
michael@0: U_INTERNAL UPlugData* U_EXPORT2
michael@0: uplug_loadPlugFromEntrypoint(UPlugEntrypoint *entrypoint, const char *config, UErrorCode *status);
michael@0:
michael@0:
michael@0: /**
michael@0: * Inject a plugin from a library, as if the information came from a config file.
michael@0: * Low level plugins may not be able to load, and ordering can't be enforced.
michael@0: * @param libName DLL name to load
michael@0: * @param sym symbol of plugin (UPlugEntrypoint function)
michael@0: * @param config configuration string, or NULL
michael@0: * @param status error result
michael@0: * @return the new UPlugData associated with this plugin, or NULL if error.
michael@0: * @internal ICU 4.4 Technology Preview
michael@0: */
michael@0: U_INTERNAL UPlugData* U_EXPORT2
michael@0: uplug_loadPlugFromLibrary(const char *libName, const char *sym, const char *config, UErrorCode *status);
michael@0:
michael@0: /**
michael@0: * Remove a plugin.
michael@0: * Will request the plugin to be unloaded, and close the library if needed
michael@0: * @param plug plugin handle to close
michael@0: * @param status error result
michael@0: * @internal ICU 4.4 Technology Preview
michael@0: */
michael@0: U_INTERNAL void U_EXPORT2
michael@0: uplug_removePlug(UPlugData *plug, UErrorCode *status);
michael@0: #endif /* U_HIDE_INTERNAL_API */
michael@0:
michael@0: #endif