mxml-private.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * "$Id: mxml-private.c 451 2014-01-04 21:50:06Z msweet $"
  3. *
  4. * Private functions for Mini-XML, a small XML-like file parsing library.
  5. *
  6. * Copyright 2003-2014 by Michael R Sweet.
  7. *
  8. * These coded instructions, statements, and computer programs are the
  9. * property of Michael R Sweet and are protected by Federal copyright
  10. * law. Distribution and use rights are outlined in the file "COPYING"
  11. * which should have been included with this file. If this file is
  12. * missing or damaged, see the license at:
  13. *
  14. * http://www.msweet.org/projects.php/Mini-XML
  15. */
  16. /*
  17. * Include necessary headers...
  18. */
  19. #include "mxml-private.h"
  20. /*
  21. * Some crazy people think that unloading a shared object is a good or safe
  22. * thing to do. Unfortunately, most objects are simply *not* safe to unload
  23. * and bad things *will* happen.
  24. *
  25. * The following mess of conditional code allows us to provide a destructor
  26. * function in Mini-XML for our thread-global storage so that it can possibly
  27. * be unloaded safely, although since there is no standard way to do so I
  28. * can't even provide any guarantees that you can do it safely on all platforms.
  29. *
  30. * This code currently supports AIX, HP-UX, Linux, Mac OS X, Solaris, and
  31. * Windows. It might work on the BSDs and IRIX, but I haven't tested that.
  32. */
  33. #if defined(__sun) || defined(_AIX)
  34. # pragma fini(_mxml_fini)
  35. # define _MXML_FINI _mxml_fini
  36. #elif defined(__hpux)
  37. # pragma FINI _mxml_fini
  38. # define _MXML_FINI _mxml_fini
  39. #elif defined(__GNUC__) /* Linux and Mac OS X */
  40. # define _MXML_FINI __attribute((destructor)) _mxml_fini
  41. #else
  42. # define _MXML_FINI _fini
  43. #endif /* __sun */
  44. /*
  45. * 'mxml_error()' - Display an error message.
  46. */
  47. void
  48. mxml_error(const char *format, /* I - Printf-style format string */
  49. ...) /* I - Additional arguments as needed */
  50. {
  51. va_list ap; /* Pointer to arguments */
  52. char s[1024]; /* Message string */
  53. _mxml_global_t *global = _mxml_global();
  54. /* Global data */
  55. /*
  56. * Range check input...
  57. */
  58. if (!format)
  59. return;
  60. /*
  61. * Format the error message string...
  62. */
  63. va_start(ap, format);
  64. vsnprintf(s, sizeof(s), format, ap);
  65. va_end(ap);
  66. /*
  67. * And then display the error message...
  68. */
  69. if (global->error_cb)
  70. (*global->error_cb)(s);
  71. else
  72. fprintf(stderr, "mxml: %s\n", s);
  73. }
  74. /*
  75. * 'mxml_ignore_cb()' - Default callback for ignored values.
  76. */
  77. mxml_type_t /* O - Node type */
  78. mxml_ignore_cb(mxml_node_t *node) /* I - Current node */
  79. {
  80. (void)node;
  81. return (MXML_IGNORE);
  82. }
  83. /*
  84. * 'mxml_integer_cb()' - Default callback for integer values.
  85. */
  86. mxml_type_t /* O - Node type */
  87. mxml_integer_cb(mxml_node_t *node) /* I - Current node */
  88. {
  89. (void)node;
  90. return (MXML_INTEGER);
  91. }
  92. /*
  93. * 'mxml_opaque_cb()' - Default callback for opaque values.
  94. */
  95. mxml_type_t /* O - Node type */
  96. mxml_opaque_cb(mxml_node_t *node) /* I - Current node */
  97. {
  98. (void)node;
  99. return (MXML_OPAQUE);
  100. }
  101. /*
  102. * 'mxml_real_cb()' - Default callback for real number values.
  103. */
  104. mxml_type_t /* O - Node type */
  105. mxml_real_cb(mxml_node_t *node) /* I - Current node */
  106. {
  107. (void)node;
  108. return (MXML_REAL);
  109. }
  110. #ifdef HAVE_PTHREAD_H /**** POSIX threading ****/
  111. # include <pthread.h>
  112. static pthread_key_t _mxml_key = -1; /* Thread local storage key */
  113. static pthread_once_t _mxml_key_once = PTHREAD_ONCE_INIT;
  114. /* One-time initialization object */
  115. static void _mxml_init(void);
  116. static void _mxml_destructor(void *g);
  117. /*
  118. * '_mxml_destructor()' - Free memory used for globals...
  119. */
  120. static void
  121. _mxml_destructor(void *g) /* I - Global data */
  122. {
  123. free(g);
  124. }
  125. /*
  126. * '_mxml_fini()' - Clean up when unloaded.
  127. */
  128. static void
  129. _MXML_FINI(void)
  130. {
  131. _mxml_global_t *global; /* Global data */
  132. if (_mxml_key != -1)
  133. {
  134. if ((global = (_mxml_global_t *)pthread_getspecific(_mxml_key)) != NULL)
  135. _mxml_destructor(global);
  136. pthread_key_delete(_mxml_key);
  137. _mxml_key = -1;
  138. }
  139. }
  140. /*
  141. * '_mxml_global()' - Get global data.
  142. */
  143. _mxml_global_t * /* O - Global data */
  144. _mxml_global(void)
  145. {
  146. _mxml_global_t *global; /* Global data */
  147. pthread_once(&_mxml_key_once, _mxml_init);
  148. if ((global = (_mxml_global_t *)pthread_getspecific(_mxml_key)) == NULL)
  149. {
  150. global = (_mxml_global_t *)calloc(1, sizeof(_mxml_global_t));
  151. pthread_setspecific(_mxml_key, global);
  152. global->num_entity_cbs = 1;
  153. global->entity_cbs[0] = _mxml_entity_cb;
  154. global->wrap = 72;
  155. }
  156. return (global);
  157. }
  158. /*
  159. * '_mxml_init()' - Initialize global data...
  160. */
  161. static void
  162. _mxml_init(void)
  163. {
  164. pthread_key_create(&_mxml_key, _mxml_destructor);
  165. }
  166. #elif defined(WIN32) && defined(MXML1_EXPORTS) /**** WIN32 threading ****/
  167. # include <windows.h>
  168. static DWORD _mxml_tls_index; /* Index for global storage */
  169. /*
  170. * 'DllMain()' - Main entry for library.
  171. */
  172. BOOL WINAPI /* O - Success/failure */
  173. DllMain(HINSTANCE hinst, /* I - DLL module handle */
  174. DWORD reason, /* I - Reason */
  175. LPVOID reserved) /* I - Unused */
  176. {
  177. _mxml_global_t *global; /* Global data */
  178. (void)hinst;
  179. (void)reserved;
  180. switch (reason)
  181. {
  182. case DLL_PROCESS_ATTACH : /* Called on library initialization */
  183. if ((_mxml_tls_index = TlsAlloc()) == TLS_OUT_OF_INDEXES)
  184. return (FALSE);
  185. break;
  186. case DLL_THREAD_DETACH : /* Called when a thread terminates */
  187. if ((global = (_mxml_global_t *)TlsGetValue(_mxml_tls_index)) != NULL)
  188. free(global);
  189. break;
  190. case DLL_PROCESS_DETACH : /* Called when library is unloaded */
  191. if ((global = (_mxml_global_t *)TlsGetValue(_mxml_tls_index)) != NULL)
  192. free(global);
  193. TlsFree(_mxml_tls_index);
  194. break;
  195. default:
  196. break;
  197. }
  198. return (TRUE);
  199. }
  200. /*
  201. * '_mxml_global()' - Get global data.
  202. */
  203. _mxml_global_t * /* O - Global data */
  204. _mxml_global(void)
  205. {
  206. _mxml_global_t *global; /* Global data */
  207. if ((global = (_mxml_global_t *)TlsGetValue(_mxml_tls_index)) == NULL)
  208. {
  209. global = (_mxml_global_t *)calloc(1, sizeof(_mxml_global_t));
  210. global->num_entity_cbs = 1;
  211. global->entity_cbs[0] = _mxml_entity_cb;
  212. global->wrap = 72;
  213. TlsSetValue(_mxml_tls_index, (LPVOID)global);
  214. }
  215. return (global);
  216. }
  217. #else /**** No threading ****/
  218. /*
  219. * '_mxml_global()' - Get global data.
  220. */
  221. _mxml_global_t * /* O - Global data */
  222. _mxml_global(void)
  223. {
  224. static _mxml_global_t global = /* Global data */
  225. {
  226. NULL, /* error_cb */
  227. 1, /* num_entity_cbs */
  228. { _mxml_entity_cb }, /* entity_cbs */
  229. 72, /* wrap */
  230. NULL, /* custom_load_cb */
  231. NULL /* custom_save_cb */
  232. };
  233. return (&global);
  234. }
  235. #endif /* HAVE_PTHREAD_H */
  236. /*
  237. * End of "$Id: mxml-private.c 451 2014-01-04 21:50:06Z msweet $".
  238. */