icuplug.h 12 KB

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