expat_lib.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. ** Copyright (c) 2001-2009 Expat maintainers.
  3. **
  4. ** Permission is hereby granted, free of charge, to any person obtaining
  5. ** a copy of this software and associated documentation files (the
  6. ** "Software"), to deal in the Software without restriction, including
  7. ** without limitation the rights to use, copy, modify, merge, publish,
  8. ** distribute, sublicense, and/or sell copies of the Software, and to
  9. ** permit persons to whom the Software is furnished to do so, subject to
  10. ** the following conditions:
  11. **
  12. ** The above copyright notice and this permission notice shall be included
  13. ** in all copies or substantial portions of the Software.
  14. **
  15. ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. ** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #ifdef __USE_INLINE__
  24. #undef __USE_INLINE__
  25. #endif
  26. #define __NOLIBBASE__
  27. #define __NOGLOBALIFACE__
  28. #include <dos/dos.h>
  29. #include <proto/exec.h>
  30. #include "expat_base.h"
  31. #define LIBNAME "expat.library"
  32. #define LIBPRI 0
  33. #define VERSION 53
  34. #define REVISION 1
  35. #define VSTRING "expat.library 53.1 (7.8.2009)" /* dd.mm.yyyy */
  36. static const char* __attribute__((used)) verstag = "\0$VER: " VSTRING;
  37. struct Interface *INewlib = 0;
  38. struct ExpatBase * libInit(struct ExpatBase *libBase, BPTR seglist, struct ExecIFace *ISys);
  39. uint32 libObtain (struct LibraryManagerInterface *Self);
  40. uint32 libRelease (struct LibraryManagerInterface *Self);
  41. struct ExpatBase *libOpen (struct LibraryManagerInterface *Self, uint32 version);
  42. BPTR libClose (struct LibraryManagerInterface *Self);
  43. BPTR libExpunge (struct LibraryManagerInterface *Self);
  44. struct Interface *openInterface(struct ExecIFace *IExec, CONST_STRPTR libName, uint32 libVer);
  45. void closeInterface(struct ExecIFace *IExec, struct Interface *iface);
  46. static APTR lib_manager_vectors[] = {
  47. libObtain,
  48. libRelease,
  49. NULL,
  50. NULL,
  51. libOpen,
  52. libClose,
  53. libExpunge,
  54. NULL,
  55. (APTR)-1,
  56. };
  57. static struct TagItem lib_managerTags[] = {
  58. { MIT_Name, (uint32)"__library" },
  59. { MIT_VectorTable, (uint32)lib_manager_vectors },
  60. { MIT_Version, 1 },
  61. { TAG_END, 0 }
  62. };
  63. extern void *main_vectors[];
  64. static struct TagItem lib_mainTags[] = {
  65. { MIT_Name, (uint32)"main" },
  66. { MIT_VectorTable, (uint32)main_vectors },
  67. { MIT_Version, 1 },
  68. { TAG_END, 0 }
  69. };
  70. static APTR libInterfaces[] = {
  71. lib_managerTags,
  72. lib_mainTags,
  73. NULL
  74. };
  75. extern void *VecTable68K[];
  76. static struct TagItem libCreateTags[] = {
  77. { CLT_DataSize, sizeof(struct ExpatBase) },
  78. { CLT_InitFunc, (uint32)libInit },
  79. { CLT_Interfaces, (uint32)libInterfaces },
  80. { CLT_Vector68K, (uint32)VecTable68K },
  81. { TAG_END, 0 }
  82. };
  83. static struct Resident __attribute__((used)) lib_res = {
  84. RTC_MATCHWORD, // rt_MatchWord
  85. &lib_res, // rt_MatchTag
  86. &lib_res+1, // rt_EndSkip
  87. RTF_NATIVE | RTF_AUTOINIT, // rt_Flags
  88. VERSION, // rt_Version
  89. NT_LIBRARY, // rt_Type
  90. LIBPRI, // rt_Pri
  91. LIBNAME, // rt_Name
  92. VSTRING, // rt_IdString
  93. libCreateTags // rt_Init
  94. };
  95. int32 _start()
  96. {
  97. return RETURN_FAIL;
  98. }
  99. struct ExpatBase *libInit(struct ExpatBase *libBase, BPTR seglist, struct ExecIFace *iexec)
  100. {
  101. libBase->libNode.lib_Node.ln_Type = NT_LIBRARY;
  102. libBase->libNode.lib_Node.ln_Pri = LIBPRI;
  103. libBase->libNode.lib_Node.ln_Name = LIBNAME;
  104. libBase->libNode.lib_Flags = LIBF_SUMUSED|LIBF_CHANGED;
  105. libBase->libNode.lib_Version = VERSION;
  106. libBase->libNode.lib_Revision = REVISION;
  107. libBase->libNode.lib_IdString = VSTRING;
  108. libBase->SegList = seglist;
  109. libBase->IExec = iexec;
  110. INewlib = openInterface(iexec, "newlib.library", 0);
  111. if ( INewlib != 0 ) {
  112. return libBase;
  113. }
  114. closeInterface(iexec, INewlib);
  115. INewlib = 0;
  116. iexec->DeleteLibrary(&libBase->libNode);
  117. return NULL;
  118. }
  119. uint32 libObtain( struct LibraryManagerInterface *Self )
  120. {
  121. ++Self->Data.RefCount;
  122. return Self->Data.RefCount;
  123. }
  124. uint32 libRelease( struct LibraryManagerInterface *Self )
  125. {
  126. --Self->Data.RefCount;
  127. return Self->Data.RefCount;
  128. }
  129. struct ExpatBase *libOpen( struct LibraryManagerInterface *Self, uint32 version )
  130. {
  131. struct ExpatBase *libBase;
  132. libBase = (struct ExpatBase *)Self->Data.LibBase;
  133. ++libBase->libNode.lib_OpenCnt;
  134. libBase->libNode.lib_Flags &= ~LIBF_DELEXP;
  135. return libBase;
  136. }
  137. BPTR libClose( struct LibraryManagerInterface *Self )
  138. {
  139. struct ExpatBase *libBase;
  140. libBase = (struct ExpatBase *)Self->Data.LibBase;
  141. --libBase->libNode.lib_OpenCnt;
  142. if ( libBase->libNode.lib_OpenCnt ) {
  143. return 0;
  144. }
  145. if ( libBase->libNode.lib_Flags & LIBF_DELEXP ) {
  146. return (BPTR)Self->LibExpunge();
  147. }
  148. else {
  149. return ZERO;
  150. }
  151. }
  152. BPTR libExpunge( struct LibraryManagerInterface *Self )
  153. {
  154. struct ExpatBase *libBase = (struct ExpatBase *)Self->Data.LibBase;
  155. BPTR result = ZERO;
  156. if (libBase->libNode.lib_OpenCnt == 0) {
  157. libBase->IExec->Remove(&libBase->libNode.lib_Node);
  158. result = libBase->SegList;
  159. closeInterface(libBase->IExec, INewlib);
  160. INewlib = 0;
  161. libBase->IExec->DeleteLibrary(&libBase->libNode);
  162. }
  163. else {
  164. libBase->libNode.lib_Flags |= LIBF_DELEXP;
  165. }
  166. return result;
  167. }
  168. struct Interface *openInterface(struct ExecIFace *IExec, CONST_STRPTR libName, uint32 libVer)
  169. {
  170. struct Library *base = IExec->OpenLibrary(libName, libVer);
  171. struct Interface *iface = IExec->GetInterface(base, "main", 1, 0);
  172. if (iface == 0) {
  173. IExec->CloseLibrary(base);
  174. }
  175. return iface;
  176. }
  177. void closeInterface(struct ExecIFace *IExec, struct Interface *iface)
  178. {
  179. if (iface != 0)
  180. {
  181. struct Library *base = iface->Data.LibBase;
  182. IExec->DropInterface(iface);
  183. IExec->CloseLibrary(base);
  184. }
  185. }