icuplug.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. ******************************************************************************
  3. *
  4. * Copyright (C) 2009-2015, 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. #if UCONFIG_ENABLE_PLUGINS
  106. /* === Basic types === */
  107. #ifndef U_HIDE_INTERNAL_API
  108. /**
  109. * @{
  110. * Opaque structure passed to/from a plugin.
  111. * use the APIs to access it.
  112. * @internal ICU 4.4 Technology Preview
  113. */
  114. struct UPlugData;
  115. typedef struct UPlugData UPlugData;
  116. /** @} */
  117. /**
  118. * Random Token to identify a valid ICU plugin. Plugins must return this
  119. * from the entrypoint.
  120. * @internal ICU 4.4 Technology Preview
  121. */
  122. #define UPLUG_TOKEN 0x54762486
  123. /**
  124. * Max width of names, symbols, and configuration strings
  125. * @internal ICU 4.4 Technology Preview
  126. */
  127. #define UPLUG_NAME_MAX 100
  128. /**
  129. * Return value from a plugin entrypoint.
  130. * Must always be set to UPLUG_TOKEN
  131. * @see UPLUG_TOKEN
  132. * @internal ICU 4.4 Technology Preview
  133. */
  134. typedef uint32_t UPlugTokenReturn;
  135. /**
  136. * Reason code for the entrypoint's call
  137. * @internal ICU 4.4 Technology Preview
  138. */
  139. typedef enum {
  140. UPLUG_REASON_QUERY = 0, /**< The plugin is being queried for info. **/
  141. UPLUG_REASON_LOAD = 1, /**< The plugin is being loaded. **/
  142. UPLUG_REASON_UNLOAD = 2, /**< The plugin is being unloaded. **/
  143. UPLUG_REASON_COUNT /**< count of known reasons **/
  144. } UPlugReason;
  145. /**
  146. * Level of plugin loading
  147. * INITIAL: UNKNOWN
  148. * QUERY: INVALID -> { LOW | HIGH }
  149. * ERR -> INVALID
  150. * @internal ICU 4.4 Technology Preview
  151. */
  152. typedef enum {
  153. UPLUG_LEVEL_INVALID = 0, /**< The plugin is invalid, hasn't called uplug_setLevel, or can't load. **/
  154. UPLUG_LEVEL_UNKNOWN = 1, /**< The plugin is waiting to be installed. **/
  155. UPLUG_LEVEL_LOW = 2, /**< The plugin must be called before u_init completes **/
  156. UPLUG_LEVEL_HIGH = 3, /**< The plugin can run at any time. **/
  157. UPLUG_LEVEL_COUNT /**< count of known reasons **/
  158. } UPlugLevel;
  159. /**
  160. * Entrypoint for an ICU plugin.
  161. * @param plug the UPlugData handle.
  162. * @param status the plugin's extended status code.
  163. * @return A valid plugin must return UPLUG_TOKEN
  164. * @internal ICU 4.4 Technology Preview
  165. */
  166. typedef UPlugTokenReturn (U_EXPORT2 UPlugEntrypoint) (
  167. UPlugData *plug,
  168. UPlugReason reason,
  169. UErrorCode *status);
  170. /* === Needed for Implementing === */
  171. /**
  172. * Request that this plugin not be unloaded at cleanup time.
  173. * This is appropriate for plugins which cannot be cleaned up.
  174. * @see u_cleanup()
  175. * @param plug plugin
  176. * @param dontUnload set true if this plugin can't be unloaded
  177. * @internal ICU 4.4 Technology Preview
  178. */
  179. U_INTERNAL void U_EXPORT2
  180. uplug_setPlugNoUnload(UPlugData *plug, UBool dontUnload);
  181. /**
  182. * Set the level of this plugin.
  183. * @param plug plugin data handle
  184. * @param level the level of this plugin
  185. * @internal ICU 4.4 Technology Preview
  186. */
  187. U_INTERNAL void U_EXPORT2
  188. uplug_setPlugLevel(UPlugData *plug, UPlugLevel level);
  189. /**
  190. * Get the level of this plugin.
  191. * @param plug plugin data handle
  192. * @return the level of this plugin
  193. * @internal ICU 4.4 Technology Preview
  194. */
  195. U_INTERNAL UPlugLevel U_EXPORT2
  196. uplug_getPlugLevel(UPlugData *plug);
  197. /**
  198. * Get the lowest level of plug which can currently load.
  199. * For example, if UPLUG_LEVEL_LOW is returned, then low level plugins may load
  200. * if UPLUG_LEVEL_HIGH is returned, then only high level plugins may load.
  201. * @return the lowest level of plug which can currently load
  202. * @internal ICU 4.4 Technology Preview
  203. */
  204. U_INTERNAL UPlugLevel U_EXPORT2
  205. uplug_getCurrentLevel(void);
  206. /**
  207. * Get plug load status
  208. * @return The error code of this plugin's load attempt.
  209. * @internal ICU 4.4 Technology Preview
  210. */
  211. U_INTERNAL UErrorCode U_EXPORT2
  212. uplug_getPlugLoadStatus(UPlugData *plug);
  213. /**
  214. * Set the human-readable name of this plugin.
  215. * @param plug plugin data handle
  216. * @param name the name of this plugin. The first UPLUG_NAME_MAX characters willi be copied into a new buffer.
  217. * @internal ICU 4.4 Technology Preview
  218. */
  219. U_INTERNAL void U_EXPORT2
  220. uplug_setPlugName(UPlugData *plug, const char *name);
  221. /**
  222. * Get the human-readable name of this plugin.
  223. * @param plug plugin data handle
  224. * @return the name of this plugin
  225. * @internal ICU 4.4 Technology Preview
  226. */
  227. U_INTERNAL const char * U_EXPORT2
  228. uplug_getPlugName(UPlugData *plug);
  229. /**
  230. * Return the symbol name for this plugin, if known.
  231. * @param plug plugin data handle
  232. * @return the symbol name, or NULL
  233. * @internal ICU 4.4 Technology Preview
  234. */
  235. U_INTERNAL const char * U_EXPORT2
  236. uplug_getSymbolName(UPlugData *plug);
  237. /**
  238. * Return the library name for this plugin, if known.
  239. * @param plug plugin data handle
  240. * @param status error code
  241. * @return the library name, or NULL
  242. * @internal ICU 4.4 Technology Preview
  243. */
  244. U_INTERNAL const char * U_EXPORT2
  245. uplug_getLibraryName(UPlugData *plug, UErrorCode *status);
  246. /**
  247. * Return the library used for this plugin, if known.
  248. * Plugins could use this to load data out of their
  249. * @param plug plugin data handle
  250. * @return the library, or NULL
  251. * @internal ICU 4.4 Technology Preview
  252. */
  253. U_INTERNAL void * U_EXPORT2
  254. uplug_getLibrary(UPlugData *plug);
  255. /**
  256. * Return the plugin-specific context data.
  257. * @param plug plugin data handle
  258. * @return the context, or NULL if not set
  259. * @internal ICU 4.4 Technology Preview
  260. */
  261. U_INTERNAL void * U_EXPORT2
  262. uplug_getContext(UPlugData *plug);
  263. /**
  264. * Set the plugin-specific context data.
  265. * @param plug plugin data handle
  266. * @param context new context to set
  267. * @internal ICU 4.4 Technology Preview
  268. */
  269. U_INTERNAL void U_EXPORT2
  270. uplug_setContext(UPlugData *plug, void *context);
  271. /**
  272. * Get the configuration string, if available.
  273. * The string is in the platform default codepage.
  274. * @param plug plugin data handle
  275. * @return configuration string, or else null.
  276. * @internal ICU 4.4 Technology Preview
  277. */
  278. U_INTERNAL const char * U_EXPORT2
  279. uplug_getConfiguration(UPlugData *plug);
  280. /**
  281. * Return all currently installed plugins, from newest to oldest
  282. * Usage Example:
  283. * \code
  284. * UPlugData *plug = NULL;
  285. * while(plug=uplug_nextPlug(plug)) {
  286. * ... do something with 'plug' ...
  287. * }
  288. * \endcode
  289. * Not thread safe- do not call while plugs are added or removed.
  290. * @param prior pass in 'NULL' to get the first (most recent) plug,
  291. * otherwise pass the value returned on a prior call to uplug_nextPlug
  292. * @return the next oldest plugin, or NULL if no more.
  293. * @internal ICU 4.4 Technology Preview
  294. */
  295. U_INTERNAL UPlugData* U_EXPORT2
  296. uplug_nextPlug(UPlugData *prior);
  297. /**
  298. * Inject a plugin as if it were loaded from a library.
  299. * This is useful for testing plugins.
  300. * Note that it will have a 'NULL' library pointer associated
  301. * with it, and therefore no llibrary will be closed at cleanup time.
  302. * Low level plugins may not be able to load, as ordering can't be enforced.
  303. * @param entrypoint entrypoint to install
  304. * @param config user specified configuration string, if available, or NULL.
  305. * @param status error result
  306. * @return the new UPlugData associated with this plugin, or NULL if error.
  307. * @internal ICU 4.4 Technology Preview
  308. */
  309. U_INTERNAL UPlugData* U_EXPORT2
  310. uplug_loadPlugFromEntrypoint(UPlugEntrypoint *entrypoint, const char *config, UErrorCode *status);
  311. /**
  312. * Inject a plugin from a library, as if the information came from a config file.
  313. * Low level plugins may not be able to load, and ordering can't be enforced.
  314. * @param libName DLL name to load
  315. * @param sym symbol of plugin (UPlugEntrypoint function)
  316. * @param config configuration string, or NULL
  317. * @param status error result
  318. * @return the new UPlugData associated with this plugin, or NULL if error.
  319. * @internal ICU 4.4 Technology Preview
  320. */
  321. U_INTERNAL UPlugData* U_EXPORT2
  322. uplug_loadPlugFromLibrary(const char *libName, const char *sym, const char *config, UErrorCode *status);
  323. /**
  324. * Remove a plugin.
  325. * Will request the plugin to be unloaded, and close the library if needed
  326. * @param plug plugin handle to close
  327. * @param status error result
  328. * @internal ICU 4.4 Technology Preview
  329. */
  330. U_INTERNAL void U_EXPORT2
  331. uplug_removePlug(UPlugData *plug, UErrorCode *status);
  332. #endif /* U_HIDE_INTERNAL_API */
  333. #endif /* UCONFIG_ENABLE_PLUGINS */
  334. #endif /* _ICUPLUG */