expat_lib.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. ** Copyright (c) 2001-2007 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. #include <dos/dos.h>
  24. #include <proto/exec.h>
  25. #define LIBNAME "expat.library"
  26. #define LIBPRI 0
  27. #define VERSION 4
  28. #define REVISION 2
  29. #define VSTRING "expat.library 4.2 (2.6.2007)" /* dd.mm.yyyy */
  30. static const char* __attribute__((used)) verstag = "\0$VER: " VSTRING;
  31. struct ExpatBase {
  32. struct Library libNode;
  33. uint16 pad;
  34. BPTR SegList;
  35. };
  36. struct ExpatBase * libInit(struct ExpatBase *libBase, BPTR seglist, struct ExecIFace *ISys);
  37. uint32 libObtain (struct LibraryManagerInterface *Self);
  38. uint32 libRelease (struct LibraryManagerInterface *Self);
  39. struct ExpatBase *libOpen (struct LibraryManagerInterface *Self, uint32 version);
  40. BPTR libClose (struct LibraryManagerInterface *Self);
  41. BPTR libExpunge (struct LibraryManagerInterface *Self);
  42. static APTR lib_manager_vectors[] = {
  43. libObtain,
  44. libRelease,
  45. NULL,
  46. NULL,
  47. libOpen,
  48. libClose,
  49. libExpunge,
  50. NULL,
  51. (APTR)-1,
  52. };
  53. static struct TagItem lib_managerTags[] = {
  54. { MIT_Name, (uint32)"__library" },
  55. { MIT_VectorTable, (uint32)lib_manager_vectors },
  56. { MIT_Version, 1 },
  57. { TAG_END, 0 }
  58. };
  59. extern void *main_vectors[];
  60. static struct TagItem lib_mainTags[] = {
  61. { MIT_Name, (uint32)"main" },
  62. { MIT_VectorTable, (uint32)main_vectors },
  63. { MIT_Version, 1 },
  64. { TAG_END, 0 }
  65. };
  66. static APTR libInterfaces[] = {
  67. lib_managerTags,
  68. lib_mainTags,
  69. NULL
  70. };
  71. static struct TagItem libCreateTags[] = {
  72. { CLT_DataSize, sizeof(struct ExpatBase) },
  73. { CLT_InitFunc, (uint32)libInit },
  74. { CLT_Interfaces, (uint32)libInterfaces },
  75. { TAG_END, 0 }
  76. };
  77. static struct Resident __attribute__((used)) lib_res = {
  78. RTC_MATCHWORD, // rt_MatchWord
  79. &lib_res, // rt_MatchTag
  80. &lib_res+1, // rt_EndSkip
  81. RTF_NATIVE | RTF_AUTOINIT, // rt_Flags
  82. VERSION, // rt_Version
  83. NT_LIBRARY, // rt_Type
  84. LIBPRI, // rt_Pri
  85. LIBNAME, // rt_Name
  86. VSTRING, // rt_IdString
  87. libCreateTags // rt_Init
  88. };
  89. struct Library *DOSLib = 0;
  90. struct Library *UtilityBase = 0;
  91. struct ExecIFace *IExec = 0;
  92. struct DOSIFace *IDOS = 0;
  93. struct UtilityIFace *IUtility = 0;
  94. void _start()
  95. {
  96. }
  97. struct ExpatBase *libInit(struct ExpatBase *libBase, BPTR seglist, struct ExecIFace *ISys)
  98. {
  99. libBase->libNode.lib_Node.ln_Type = NT_LIBRARY;
  100. libBase->libNode.lib_Node.ln_Pri = LIBPRI;
  101. libBase->libNode.lib_Node.ln_Name = LIBNAME;
  102. libBase->libNode.lib_Flags = LIBF_SUMUSED|LIBF_CHANGED;
  103. libBase->libNode.lib_Version = VERSION;
  104. libBase->libNode.lib_Revision = REVISION;
  105. libBase->libNode.lib_IdString = VSTRING;
  106. libBase->SegList = seglist;
  107. IExec = ISys;
  108. DOSLib = OpenLibrary("dos.library", 51);
  109. if ( DOSLib != 0 ) {
  110. IDOS = (struct DOSIFace *)GetInterface(DOSLib, "main", 1, NULL);
  111. if ( IDOS != 0 ) {
  112. UtilityBase = OpenLibrary("utility.library", 51);
  113. if ( UtilityBase != 0 ) {
  114. IUtility = (struct UtilityIFace*)GetInterface(UtilityBase, "main", 1, NULL);
  115. if ( IUtility != 0 ) {
  116. return libBase;
  117. }
  118. CloseLibrary(UtilityBase);
  119. }
  120. DropInterface((struct Interface *)IDOS);
  121. }
  122. CloseLibrary(DOSLib);
  123. }
  124. return NULL;
  125. }
  126. uint32 libObtain( struct LibraryManagerInterface *Self )
  127. {
  128. ++Self->Data.RefCount;
  129. return Self->Data.RefCount;
  130. }
  131. uint32 libRelease( struct LibraryManagerInterface *Self )
  132. {
  133. --Self->Data.RefCount;
  134. return Self->Data.RefCount;
  135. }
  136. struct ExpatBase *libOpen( struct LibraryManagerInterface *Self, uint32 version )
  137. {
  138. struct ExpatBase *libBase;
  139. libBase = (struct ExpatBase *)Self->Data.LibBase;
  140. ++libBase->libNode.lib_OpenCnt;
  141. libBase->libNode.lib_Flags &= ~LIBF_DELEXP;
  142. return libBase;
  143. }
  144. BPTR libClose( struct LibraryManagerInterface *Self )
  145. {
  146. struct ExpatBase *libBase;
  147. libBase = (struct ExpatBase *)Self->Data.LibBase;
  148. --libBase->libNode.lib_OpenCnt;
  149. if ( libBase->libNode.lib_OpenCnt ) {
  150. return 0;
  151. }
  152. if ( libBase->libNode.lib_Flags & LIBF_DELEXP ) {
  153. return (BPTR)Self->LibExpunge();
  154. }
  155. else {
  156. return 0;
  157. }
  158. }
  159. BPTR libExpunge( struct LibraryManagerInterface *Self )
  160. {
  161. struct ExpatBase *libBase;
  162. BPTR result = 0;
  163. libBase = (struct ExpatBase *)Self->Data.LibBase;
  164. if (libBase->libNode.lib_OpenCnt == 0) {
  165. Remove(&libBase->libNode.lib_Node);
  166. result = libBase->SegList;
  167. DropInterface((struct Interface *)IUtility);
  168. CloseLibrary(UtilityBase);
  169. DropInterface((struct Interface *)IDOS);
  170. CloseLibrary(DOSLib);
  171. DeleteLibrary(&libBase->libNode);
  172. }
  173. else {
  174. libBase->libNode.lib_Flags |= LIBF_DELEXP;
  175. }
  176. return result;
  177. }