icuplug.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /*
  2. ******************************************************************************
  3. *
  4. * Copyright (C) 2009-2012, International Business Machines
  5. * Corporation and others. All Rights Reserved.
  6. *
  7. ******************************************************************************
  8. *
  9. * FILE NAME : icuplug.h
  10. *
  11. * Date Name Description
  12. * 10/29/2009 sl New.
  13. ******************************************************************************
  14. */
  15. /**
  16. * \file
  17. * \brief C API: ICU Plugin API
  18. *
  19. * <h2>C API: ICU Plugin API</h2>
  20. *
  21. * <p>C API allowing run-time loadable modules that extend or modify ICU functionality.</p>
  22. *
  23. * <h3>Loading and Configuration</h3>
  24. *
  25. * <p>At ICU startup time, the environment variable "ICU_PLUGINS" will be
  26. * queried for a directory name. If it is not set, the preprocessor symbol
  27. * "DEFAULT_ICU_PLUGINS" will be checked for a default value.</p>
  28. *
  29. * <p>Within the above-named directory, the file "icuplugins##.txt" will be
  30. * opened, if present, where ## is the major+minor number of the currently
  31. * running ICU (such as, 44 for ICU 4.4, thus icuplugins44.txt)</p>
  32. *
  33. * <p>The configuration file has this format:</p>
  34. *
  35. * <ul>
  36. * <li>Hash (#) begins a comment line</li>
  37. *
  38. * <li>Non-comment lines have two or three components:
  39. * LIBRARYNAME ENTRYPOINT [ CONFIGURATION .. ]</li>
  40. *
  41. * <li>Tabs or spaces separate the three items.</li>
  42. *
  43. * <li>LIBRARYNAME is the name of a shared library, either a short name if
  44. * it is on the loader path, or a full pathname.</li>
  45. *
  46. * <li>ENTRYPOINT is the short (undecorated) symbol name of the plugin's
  47. * entrypoint, as above.</li>
  48. *
  49. * <li>CONFIGURATION is the entire rest of the line . It's passed as-is to
  50. * the plugin.</li>
  51. * </ul>
  52. *
  53. * <p>An example configuration file is, in its entirety:</p>
  54. *
  55. * \code
  56. * # this is icuplugins44.txt
  57. * testplug.dll myPlugin hello=world
  58. * \endcode
  59. * <p>Plugins are categorized as "high" or "low" level. Low level are those
  60. * which must be run BEFORE high level plugins, and before any operations
  61. * which cause ICU to be 'initialized'. If a plugin is low level but
  62. * causes ICU to allocate memory or become initialized, that plugin is said
  63. * to cause a 'level change'. </p>
  64. *
  65. * <p>At load time, ICU first queries all plugins to determine their level,
  66. * then loads all 'low' plugins first, and then loads all 'high' plugins.
  67. * Plugins are otherwise loaded in the order listed in the configuration file.</p>
  68. *
  69. * <h3>Implementing a Plugin</h3>
  70. * \code
  71. * U_CAPI UPlugTokenReturn U_EXPORT2
  72. * myPlugin (UPlugData *plug, UPlugReason reason, UErrorCode *status) {
  73. * if(reason==UPLUG_REASON_QUERY) {
  74. * uplug_setPlugName(plug, "Simple Plugin");
  75. * uplug_setPlugLevel(plug, UPLUG_LEVEL_HIGH);
  76. * } else if(reason==UPLUG_REASON_LOAD) {
  77. * ... Set up some ICU things here....
  78. * } else if(reason==UPLUG_REASON_UNLOAD) {
  79. * ... unload, clean up ...
  80. * }
  81. * return UPLUG_TOKEN;
  82. * }
  83. * \endcode
  84. *
  85. * <p>The UPlugData* is an opaque pointer to the plugin-specific data, and is
  86. * used in all other API calls.</p>
  87. *
  88. * <p>The API contract is:</p>
  89. * <ol><li>The plugin MUST always return UPLUG_TOKEN as a return value- to
  90. * indicate that it is a valid plugin.</li>
  91. *
  92. * <li>When the 'reason' parameter is set to UPLUG_REASON_QUERY, the
  93. * plugin MUST call uplug_setPlugLevel() to indicate whether it is a high
  94. * level or low level plugin.</li>
  95. *
  96. * <li>When the 'reason' parameter is UPLUG_REASON_QUERY, the plugin
  97. * SHOULD call uplug_setPlugName to indicate a human readable plugin name.</li></ol>
  98. *
  99. *
  100. * \internal ICU 4.4 Technology Preview
  101. */
  102. #ifndef ICUPLUG_H
  103. #define ICUPLUG_H
  104. #include "unicode/utypes.h"
  105. /* === Basic types === */
  106. #ifndef U_HIDE_INTERNAL_API
  107. /**
  108. * @{
  109. * Opaque structure passed to/from a plugin.
  110. * use the APIs to access it.
  111. * @internal ICU 4.4 Technology Preview
  112. */
  113. struct UPlugData;
  114. typedef struct UPlugData UPlugData;
  115. /** @} */
  116. /**
  117. * Random Token to identify a valid ICU plugin. Plugins must return this
  118. * from the entrypoint.
  119. * @internal ICU 4.4 Technology Preview
  120. */
  121. #define UPLUG_TOKEN 0x54762486
  122. /**
  123. * Max width of names, symbols, and configuration strings
  124. * @internal ICU 4.4 Technology Preview
  125. */
  126. #define UPLUG_NAME_MAX 100
  127. /**
  128. * Return value from a plugin entrypoint.
  129. * Must always be set to UPLUG_TOKEN
  130. * @see UPLUG_TOKEN
  131. * @internal ICU 4.4 Technology Preview
  132. */
  133. typedef uint32_t UPlugTokenReturn;
  134. /**
  135. * Reason code for the entrypoint's call
  136. * @internal ICU 4.4 Technology Preview
  137. */
  138. typedef enum {
  139. UPLUG_REASON_QUERY = 0, /**< The plugin is being queried for info. **/
  140. UPLUG_REASON_LOAD = 1, /**< The plugin is being loaded. **/
  141. UPLUG_REASON_UNLOAD = 2, /**< The plugin is being unloaded. **/
  142. UPLUG_REASON_COUNT /**< count of known reasons **/
  143. } UPlugReason;
  144. /**
  145. * Level of plugin loading
  146. * INITIAL: UNKNOWN
  147. * QUERY: INVALID -> { LOW | HIGH }
  148. * ERR -> INVALID
  149. * @internal ICU 4.4 Technology Preview
  150. */
  151. typedef enum {
  152. UPLUG_LEVEL_INVALID = 0, /**< The plugin is invalid, hasn't called uplug_setLevel, or can't load. **/
  153. UPLUG_LEVEL_UNKNOWN = 1, /**< The plugin is waiting to be installed. **/
  154. UPLUG_LEVEL_LOW = 2, /**< The plugin must be called before u_init completes **/
  155. UPLUG_LEVEL_HIGH = 3, /**< The plugin can run at any time. **/
  156. UPLUG_LEVEL_COUNT /**< count of known reasons **/
  157. } UPlugLevel;
  158. /**
  159. * Entrypoint for an ICU plugin.
  160. * @param plug the UPlugData handle.
  161. * @param status the plugin's extended status code.
  162. * @return A valid plugin must return UPLUG_TOKEN
  163. * @internal ICU 4.4 Technology Preview
  164. */
  165. typedef UPlugTokenReturn (U_EXPORT2 UPlugEntrypoint) (
  166. UPlugData *plug,
  167. UPlugReason reason,
  168. UErrorCode *status);
  169. /* === Needed for Implementing === */
  170. /**
  171. * Request that this plugin not be unloaded at cleanup time.
  172. * This is appropriate for plugins which cannot be cleaned up.
  173. * @see u_cleanup()
  174. * @param plug plugin
  175. * @param dontUnload set true if this plugin can't be unloaded
  176. * @internal ICU 4.4 Technology Preview
  177. */
  178. U_INTERNAL void U_EXPORT2
  179. uplug_setPlugNoUnload(UPlugData *plug, UBool dontUnload);
  180. /**
  181. * Set the level of this plugin.
  182. * @param plug plugin data handle
  183. * @param level the level of this plugin
  184. * @internal ICU 4.4 Technology Preview
  185. */
  186. U_INTERNAL void U_EXPORT2
  187. uplug_setPlugLevel(UPlugData *plug, UPlugLevel level);
  188. /**
  189. * Get the level of this plugin.
  190. * @param plug plugin data handle
  191. * @return the level of this plugin
  192. * @internal ICU 4.4 Technology Preview
  193. */
  194. U_INTERNAL UPlugLevel U_EXPORT2
  195. uplug_getPlugLevel(UPlugData *plug);
  196. /**
  197. * Get the lowest level of plug which can currently load.
  198. * For example, if UPLUG_LEVEL_LOW is returned, then low level plugins may load
  199. * if UPLUG_LEVEL_HIGH is returned, then only high level plugins may load.
  200. * @return the lowest level of plug which can currently load
  201. * @internal ICU 4.4 Technology Preview
  202. */
  203. U_INTERNAL UPlugLevel U_EXPORT2
  204. uplug_getCurrentLevel(void);
  205. /**
  206. * Get plug load status
  207. * @return The error code of this plugin's load attempt.
  208. * @internal ICU 4.4 Technology Preview
  209. */
  210. U_INTERNAL UErrorCode U_EXPORT2
  211. uplug_getPlugLoadStatus(UPlugData *plug);
  212. /**
  213. * Set the human-readable name of this plugin.
  214. * @param plug plugin data handle
  215. * @param name the name of this plugin. The first UPLUG_NAME_MAX characters willi be copied into a new buffer.
  216. * @internal ICU 4.4 Technology Preview
  217. */
  218. U_INTERNAL void U_EXPORT2
  219. uplug_setPlugName(UPlugData *plug, const char *name);
  220. /**
  221. * Get the human-readable name of this plugin.
  222. * @param plug plugin data handle
  223. * @return the name of this plugin
  224. * @internal ICU 4.4 Technology Preview
  225. */
  226. U_INTERNAL const char * U_EXPORT2
  227. uplug_getPlugName(UPlugData *plug);
  228. /**
  229. * Return the symbol name for this plugin, if known.
  230. * @param plug plugin data handle
  231. * @return the symbol name, or NULL
  232. * @internal ICU 4.4 Technology Preview
  233. */
  234. U_INTERNAL const char * U_EXPORT2
  235. uplug_getSymbolName(UPlugData *plug);
  236. /**
  237. * Return the library name for this plugin, if known.
  238. * @param plug plugin data handle
  239. * @param status error code
  240. * @return the library name, or NULL
  241. * @internal ICU 4.4 Technology Preview
  242. */
  243. U_INTERNAL const char * U_EXPORT2
  244. uplug_getLibraryName(UPlugData *plug, UErrorCode *status);
  245. /**
  246. * Return the library used for this plugin, if known.
  247. * Plugins could use this to load data out of their
  248. * @param plug plugin data handle
  249. * @return the library, or NULL
  250. * @internal ICU 4.4 Technology Preview
  251. */
  252. U_INTERNAL void * U_EXPORT2
  253. uplug_getLibrary(UPlugData *plug);
  254. /**
  255. * Return the plugin-specific context data.
  256. * @param plug plugin data handle
  257. * @return the context, or NULL if not set
  258. * @internal ICU 4.4 Technology Preview
  259. */
  260. U_INTERNAL void * U_EXPORT2
  261. uplug_getContext(UPlugData *plug);
  262. /**
  263. * Set the plugin-specific context data.
  264. * @param plug plugin data handle
  265. * @param context new context to set
  266. * @internal ICU 4.4 Technology Preview
  267. */
  268. U_INTERNAL void U_EXPORT2
  269. uplug_setContext(UPlugData *plug, void *context);
  270. /**
  271. * Get the configuration string, if available.
  272. * The string is in the platform default codepage.
  273. * @param plug plugin data handle
  274. * @return configuration string, or else null.
  275. * @internal ICU 4.4 Technology Preview
  276. */
  277. U_INTERNAL const char * U_EXPORT2
  278. uplug_getConfiguration(UPlugData *plug);
  279. /**
  280. * Return all currently installed plugins, from newest to oldest
  281. * Usage Example:
  282. * \code
  283. * UPlugData *plug = NULL;
  284. * while(plug=uplug_nextPlug(plug)) {
  285. * ... do something with 'plug' ...
  286. * }
  287. * \endcode
  288. * Not thread safe- do not call while plugs are added or removed.
  289. * @param prior pass in 'NULL' to get the first (most recent) plug,
  290. * otherwise pass the value returned on a prior call to uplug_nextPlug
  291. * @return the next oldest plugin, or NULL if no more.
  292. * @internal ICU 4.4 Technology Preview
  293. */
  294. U_INTERNAL UPlugData* U_EXPORT2
  295. uplug_nextPlug(UPlugData *prior);
  296. /**
  297. * Inject a plugin as if it were loaded from a library.
  298. * This is useful for testing plugins.
  299. * Note that it will have a 'NULL' library pointer associated
  300. * with it, and therefore no llibrary will be closed at cleanup time.
  301. * Low level plugins may not be able to load, as ordering can't be enforced.
  302. * @param entrypoint entrypoint to install
  303. * @param config user specified configuration string, if available, or NULL.
  304. * @param status error result
  305. * @return the new UPlugData associated with this plugin, or NULL if error.
  306. * @internal ICU 4.4 Technology Preview
  307. */
  308. U_INTERNAL UPlugData* U_EXPORT2
  309. uplug_loadPlugFromEntrypoint(UPlugEntrypoint *entrypoint, const char *config, UErrorCode *status);
  310. /**
  311. * Inject a plugin from a library, as if the information came from a config file.
  312. * Low level plugins may not be able to load, and ordering can't be enforced.
  313. * @param libName DLL name to load
  314. * @param sym symbol of plugin (UPlugEntrypoint function)
  315. * @param config configuration string, or NULL
  316. * @param status error result
  317. * @return the new UPlugData associated with this plugin, or NULL if error.
  318. * @internal ICU 4.4 Technology Preview
  319. */
  320. U_INTERNAL UPlugData* U_EXPORT2
  321. uplug_loadPlugFromLibrary(const char *libName, const char *sym, const char *config, UErrorCode *status);
  322. /**
  323. * Remove a plugin.
  324. * Will request the plugin to be unloaded, and close the library if needed
  325. * @param plug plugin handle to close
  326. * @param status error result
  327. * @internal ICU 4.4 Technology Preview
  328. */
  329. U_INTERNAL void U_EXPORT2
  330. uplug_removePlug(UPlugData *plug, UErrorCode *status);
  331. #endif /* U_HIDE_INTERNAL_API */
  332. #endif