sysos.inc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2001 by Free Pascal development team
  4. This file implements all the base types and limits required
  5. for a minimal POSIX compliant subset required to port the compiler
  6. to a new OS.
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. **********************************************************************}
  13. {*****************************************************************************
  14. AmigaOS structures
  15. *****************************************************************************}
  16. {$include execd.inc}
  17. {$include timerd.inc}
  18. {$include doslibd.inc}
  19. {*****************************************************************************
  20. AmigaOS functions
  21. *****************************************************************************}
  22. {$include execf.inc}
  23. {$include doslibf.inc}
  24. {$ifdef cpum68k}
  25. {$if defined(amiga_v1_0_only) or defined(amiga_v1_2_only) or defined(amiga_v2_0_only)}
  26. {$include legacyexec.inc}
  27. {$if not defined(amiga_v2_0_only)}
  28. {$include legacydos.inc}
  29. {$include legacyutil.inc}
  30. {$endif}
  31. {$endif}
  32. {$endif}
  33. {*****************************************************************************
  34. CPU specific
  35. *****************************************************************************}
  36. {$ifdef cpum68k}
  37. {$include m68kamiga.inc}
  38. {$endif}
  39. {*****************************************************************************
  40. System Dependent Structures/Consts
  41. *****************************************************************************}
  42. {$packrecords default}
  43. {$ifndef FPC_SYSTEM_HAS_STACKTOP}
  44. {$define FPC_SYSTEM_HAS_STACKTOP}
  45. function StackTop: pointer;
  46. begin
  47. StackTop:=FindTask(nil)^.tc_SPUpper;
  48. end;
  49. {$endif FPC_SYSTEM_HAS_STACKTOP}
  50. const
  51. CTRL_C = 20; { Error code on CTRL-C press }
  52. { Used for CTRL_C checking in I/O calls }
  53. procedure checkCTRLC;
  54. begin
  55. if BreakOn then begin
  56. if (SetSignal(0,0) And SIGBREAKF_CTRL_C)<>0 then begin
  57. { Clear CTRL-C signal }
  58. SetSignal(0,SIGBREAKF_CTRL_C);
  59. Halt(CTRL_C);
  60. end;
  61. end;
  62. end;
  63. { Converts a AmigaOS dos.library error code to a TP compatible error code }
  64. { Based on 1.0.x Amiga RTL }
  65. procedure dosError2InOut(errno: LongInt);
  66. begin
  67. case errno of
  68. ERROR_BAD_NUMBER,
  69. ERROR_ACTION_NOT_KNOWN,
  70. ERROR_NOT_IMPLEMENTED : InOutRes := 1;
  71. ERROR_OBJECT_NOT_FOUND : InOutRes := 2;
  72. ERROR_DIR_NOT_FOUND : InOutRes := 3;
  73. ERROR_DISK_WRITE_PROTECTED : InOutRes := 150;
  74. ERROR_OBJECT_WRONG_TYPE : InOutRes := 151;
  75. ERROR_OBJECT_EXISTS,
  76. ERROR_DELETE_PROTECTED,
  77. ERROR_WRITE_PROTECTED,
  78. ERROR_READ_PROTECTED,
  79. ERROR_OBJECT_IN_USE,
  80. ERROR_DIRECTORY_NOT_EMPTY : InOutRes := 5;
  81. ERROR_NO_MORE_ENTRIES : InOutRes := 18;
  82. ERROR_RENAME_ACROSS_DEVICES : InOutRes := 17;
  83. ERROR_DISK_FULL : InOutRes := 101;
  84. ERROR_INVALID_RESIDENT_LIBRARY : InoutRes := 153;
  85. ERROR_BAD_HUNK : InOutRes := 153;
  86. ERROR_NOT_A_DOS_DISK : InOutRes := 157;
  87. ERROR_NO_DISK,
  88. ERROR_DISK_NOT_VALIDATED,
  89. ERROR_DEVICE_NOT_MOUNTED : InOutRes := 152;
  90. ERROR_SEEK_ERROR : InOutRes := 156;
  91. ERROR_LOCK_COLLISION,
  92. ERROR_LOCK_TIMEOUT,
  93. ERROR_UNLOCK_ERROR,
  94. ERROR_INVALID_LOCK,
  95. ERROR_INVALID_COMPONENT_NAME,
  96. ERROR_BAD_STREAM_NAME,
  97. ERROR_FILE_NOT_OBJECT : InOutRes := 6;
  98. else
  99. InOutRes := errno;
  100. end;
  101. end;
  102. { Converts an Unix-like path to Amiga-like path }
  103. function PathConv(path: shortstring): shortstring; alias: 'PATHCONV'; [public];
  104. var tmppos: longint;
  105. begin
  106. { check for short paths }
  107. if length(path)<=2 then begin
  108. if (path='.') or (path='./') then path:='' else
  109. if path='..' then path:='/' else
  110. if path='*' then path:='#?';
  111. end else begin
  112. { convert parent directories }
  113. tmppos:=pos('../',path);
  114. while tmppos<>0 do begin
  115. { delete .. to have / as parent dir sign }
  116. delete(path,tmppos,2);
  117. tmppos:=pos('../',path);
  118. end;
  119. { convert current directories }
  120. tmppos:=pos('./',path);
  121. while tmppos<>0 do begin
  122. { delete ./ since we doesn't need to sign current directory }
  123. delete(path,tmppos,2);
  124. tmppos:=pos('./',path);
  125. end;
  126. { convert wildstar to #? }
  127. tmppos:=pos('*',path);
  128. while tmppos<>0 do begin
  129. delete(path,tmppos,1);
  130. insert('#?',path,tmppos);
  131. tmppos:=pos('*',path);
  132. end;
  133. end;
  134. PathConv:=path;
  135. end;
  136. { Converts an Unix-like path to Amiga-like path }
  137. function PathConv(const path: rawbytestring): rawbytestring; alias: 'PATHCONVRBS'; [public];
  138. var tmppos: longint;
  139. begin
  140. { check for short paths }
  141. if length(path)<=2 then begin
  142. if (path='.') or (path='./') then PathConv:='' else
  143. if path='..' then PathConv:='/' else
  144. if path='*' then PathConv:='#?'
  145. else PathConv:=path;
  146. end else begin
  147. { convert parent directories }
  148. PathConv:=path;
  149. tmppos:=pos('../',PathConv);
  150. while tmppos<>0 do begin
  151. { delete .. to have / as parent dir sign }
  152. delete(PathConv,tmppos,2);
  153. tmppos:=pos('../',PathConv);
  154. end;
  155. { convert current directories }
  156. tmppos:=pos('./',PathConv);
  157. while tmppos<>0 do begin
  158. { delete ./ since we doesn't need to sign current directory }
  159. delete(PathConv,tmppos,2);
  160. tmppos:=pos('./',PathConv);
  161. end;
  162. { convert wildstar to #? }
  163. tmppos:=pos('*',PathConv);
  164. while tmppos<>0 do begin
  165. delete(PathConv,tmppos,1);
  166. insert('#?',PathConv,tmppos);
  167. tmppos:=pos('*',PathConv);
  168. end;
  169. end;
  170. end;
  171. { Thread Init/Exit Procedure support }
  172. Type
  173. PThreadProcInfo = ^TThreadProcInfo;
  174. TThreadProcInfo = Record
  175. Next : PThreadProcInfo;
  176. Proc : TProcedure;
  177. End;
  178. const
  179. threadInitProcList :PThreadProcInfo = nil;
  180. threadExitProcList :PThreadProcInfo = nil;
  181. Procedure DoThreadProcChain(p: PThreadProcInfo);
  182. Begin
  183. while p <> nil do
  184. begin
  185. p^.proc;
  186. p:=p^.next;
  187. end;
  188. End;
  189. Procedure AddThreadProc(var procList: PThreadProcInfo; Proc: TProcedure);
  190. var
  191. P : PThreadProcInfo;
  192. Begin
  193. // the ThreadProc chain is only freed after the memory manager has
  194. // shut down, therefore native OS allocation
  195. P := AllocPooled(ASYS_heapPool,sizeof(TThreadProcInfo));
  196. P^.Next:=procList;
  197. P^.Proc:=Proc;
  198. procList:=P;
  199. End;
  200. Procedure CleanupThreadProcChain(var procList: PThreadProcInfo);
  201. var
  202. P : PThreadProcInfo;
  203. Begin
  204. while procList <> nil do
  205. begin
  206. p:=procList;
  207. procList:=procList^.next;
  208. FreePooled(ASYS_heapPool,P,sizeof(TThreadProcInfo));
  209. end;
  210. End;
  211. Procedure AddThreadInitProc(Proc: TProcedure);
  212. Begin
  213. AddThreadProc(threadInitProcList,Proc);
  214. End;
  215. Procedure AddThreadExitProc(Proc: TProcedure);
  216. Begin
  217. AddThreadProc(threadExitProcList,Proc);
  218. End;
  219. Procedure DoThreadInitProcChain;
  220. Begin
  221. DoThreadProcChain(threadInitProcList);
  222. End;
  223. Procedure DoThreadExitProcChain;
  224. Begin
  225. DoThreadProcChain(threadExitProcList);
  226. End;
  227. {$ifdef FPC_SYSTEM_HAS_BACKTRACESTR}
  228. function SysBackTraceStr (Addr: CodePointer): ShortString;
  229. begin
  230. if (addr<codestart) or (addr>codeend) then
  231. SysBackTraceStr:=' Addr $'+hexstr(addr)
  232. else
  233. SysBackTraceStr:=' Offs $'+hexstr(pointer(addr-codestart));
  234. end;
  235. {$endif FPC_SYSTEM_HAS_BACKTRACESTR}