icuplug.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. // Copyright (C) 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
  108. /* === Basic types === */
  109. #ifndef U_HIDE_INTERNAL_API
  110. /**
  111. * @{
  112. * Opaque structure passed to/from a plugin.
  113. * use the APIs to access it.
  114. * @internal ICU 4.4 Technology Preview
  115. */
  116. struct UPlugData;
  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 status the plugin's extended status code.
  173. * @return A valid plugin must return UPLUG_TOKEN
  174. * @internal ICU 4.4 Technology Preview
  175. */
  176. typedef UPlugTokenReturn (U_EXPORT2 UPlugEntrypoint) (
  177. UPlugData *plug,
  178. UPlugReason reason,
  179. UErrorCode *status);
  180. /* === Needed for Implementing === */
  181. /**
  182. * Request that this plugin not be unloaded at cleanup time.
  183. * This is appropriate for plugins which cannot be cleaned up.
  184. * @see u_cleanup()
  185. * @param plug plugin
  186. * @param dontUnload set true if this plugin can't be unloaded
  187. * @internal ICU 4.4 Technology Preview
  188. */
  189. U_INTERNAL void U_EXPORT2
  190. uplug_setPlugNoUnload(UPlugData *plug, UBool dontUnload);
  191. /**
  192. * Set the level of this plugin.
  193. * @param plug plugin data handle
  194. * @param level the level of this plugin
  195. * @internal ICU 4.4 Technology Preview
  196. */
  197. U_INTERNAL void U_EXPORT2
  198. uplug_setPlugLevel(UPlugData *plug, UPlugLevel level);
  199. /**
  200. * Get the level of this plugin.
  201. * @param plug plugin data handle
  202. * @return the level of this plugin
  203. * @internal ICU 4.4 Technology Preview
  204. */
  205. U_INTERNAL UPlugLevel U_EXPORT2
  206. uplug_getPlugLevel(UPlugData *plug);
  207. /**
  208. * Get the lowest level of plug which can currently load.
  209. * For example, if UPLUG_LEVEL_LOW is returned, then low level plugins may load
  210. * if UPLUG_LEVEL_HIGH is returned, then only high level plugins may load.
  211. * @return the lowest level of plug which can currently load
  212. * @internal ICU 4.4 Technology Preview
  213. */
  214. U_INTERNAL UPlugLevel U_EXPORT2
  215. uplug_getCurrentLevel(void);
  216. /**
  217. * Get plug load status
  218. * @return The error code of this plugin's load attempt.
  219. * @internal ICU 4.4 Technology Preview
  220. */
  221. U_INTERNAL UErrorCode U_EXPORT2
  222. uplug_getPlugLoadStatus(UPlugData *plug);
  223. /**
  224. * Set the human-readable name of this plugin.
  225. * @param plug plugin data handle
  226. * @param name the name of this plugin. The first UPLUG_NAME_MAX characters willi be copied into a new buffer.
  227. * @internal ICU 4.4 Technology Preview
  228. */
  229. U_INTERNAL void U_EXPORT2
  230. uplug_setPlugName(UPlugData *plug, const char *name);
  231. /**
  232. * Get the human-readable name of this plugin.
  233. * @param plug plugin data handle
  234. * @return the name of this plugin
  235. * @internal ICU 4.4 Technology Preview
  236. */
  237. U_INTERNAL const char * U_EXPORT2
  238. uplug_getPlugName(UPlugData *plug);
  239. /**
  240. * Return the symbol name for this plugin, if known.
  241. * @param plug plugin data handle
  242. * @return the symbol name, or NULL
  243. * @internal ICU 4.4 Technology Preview
  244. */
  245. U_INTERNAL const char * U_EXPORT2
  246. uplug_getSymbolName(UPlugData *plug);
  247. /**
  248. * Return the library name for this plugin, if known.
  249. * @param plug plugin data handle
  250. * @param status error code
  251. * @return the library name, or NULL
  252. * @internal ICU 4.4 Technology Preview
  253. */
  254. U_INTERNAL const char * U_EXPORT2
  255. uplug_getLibraryName(UPlugData *plug, UErrorCode *status);
  256. /**
  257. * Return the library used for this plugin, if known.
  258. * Plugins could use this to load data out of their
  259. * @param plug plugin data handle
  260. * @return the library, or NULL
  261. * @internal ICU 4.4 Technology Preview
  262. */
  263. U_INTERNAL void * U_EXPORT2
  264. uplug_getLibrary(UPlugData *plug);
  265. /**
  266. * Return the plugin-specific context data.
  267. * @param plug plugin data handle
  268. * @return the context, or NULL if not set
  269. * @internal ICU 4.4 Technology Preview
  270. */
  271. U_INTERNAL void * U_EXPORT2
  272. uplug_getContext(UPlugData *plug);
  273. /**
  274. * Set the plugin-specific context data.
  275. * @param plug plugin data handle
  276. * @param context new context to set
  277. * @internal ICU 4.4 Technology Preview
  278. */
  279. U_INTERNAL void U_EXPORT2
  280. uplug_setContext(UPlugData *plug, void *context);
  281. /**
  282. * Get the configuration string, if available.
  283. * The string is in the platform default codepage.
  284. * @param plug plugin data handle
  285. * @return configuration string, or else null.
  286. * @internal ICU 4.4 Technology Preview
  287. */
  288. U_INTERNAL const char * U_EXPORT2
  289. uplug_getConfiguration(UPlugData *plug);
  290. /**
  291. * Return all currently installed plugins, from newest to oldest
  292. * Usage Example:
  293. * \code
  294. * UPlugData *plug = NULL;
  295. * while(plug=uplug_nextPlug(plug)) {
  296. * ... do something with 'plug' ...
  297. * }
  298. * \endcode
  299. * Not thread safe- do not call while plugs are added or removed.
  300. * @param prior pass in 'NULL' to get the first (most recent) plug,
  301. * otherwise pass the value returned on a prior call to uplug_nextPlug
  302. * @return the next oldest plugin, or NULL if no more.
  303. * @internal ICU 4.4 Technology Preview
  304. */
  305. U_INTERNAL UPlugData* U_EXPORT2
  306. uplug_nextPlug(UPlugData *prior);
  307. /**
  308. * Inject a plugin as if it were loaded from a library.
  309. * This is useful for testing plugins.
  310. * Note that it will have a 'NULL' library pointer associated
  311. * with it, and therefore no llibrary will be closed at cleanup time.
  312. * Low level plugins may not be able to load, as ordering can't be enforced.
  313. * @param entrypoint entrypoint to install
  314. * @param config user specified configuration string, if available, or NULL.
  315. * @param status error result
  316. * @return the new UPlugData associated with this plugin, or NULL if error.
  317. * @internal ICU 4.4 Technology Preview
  318. */
  319. U_INTERNAL UPlugData* U_EXPORT2
  320. uplug_loadPlugFromEntrypoint(UPlugEntrypoint *entrypoint, const char *config, UErrorCode *status);
  321. /**
  322. * Inject a plugin from a library, as if the information came from a config file.
  323. * Low level plugins may not be able to load, and ordering can't be enforced.
  324. * @param libName DLL name to load
  325. * @param sym symbol of plugin (UPlugEntrypoint function)
  326. * @param config configuration string, or NULL
  327. * @param status error result
  328. * @return the new UPlugData associated with this plugin, or NULL if error.
  329. * @internal ICU 4.4 Technology Preview
  330. */
  331. U_INTERNAL UPlugData* U_EXPORT2
  332. uplug_loadPlugFromLibrary(const char *libName, const char *sym, const char *config, UErrorCode *status);
  333. /**
  334. * Remove a plugin.
  335. * Will request the plugin to be unloaded, and close the library if needed
  336. * @param plug plugin handle to close
  337. * @param status error result
  338. * @internal ICU 4.4 Technology Preview
  339. */
  340. U_INTERNAL void U_EXPORT2
  341. uplug_removePlug(UPlugData *plug, UErrorCode *status);
  342. #endif /* U_HIDE_INTERNAL_API */
  343. #endif /* UCONFIG_ENABLE_PLUGINS */
  344. #endif /* _ICUPLUG */