sysutils.pp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1998 by Florian Klaempfl
  5. member of the Free Pascal development team
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. unit sysutils;
  13. interface
  14. {$MODE objfpc}
  15. uses
  16. {$ifdef linux}
  17. linux
  18. {$else}
  19. dos
  20. {$ifdef go32v2}
  21. ,go32
  22. {$endif go32v2}
  23. {$endif linux}
  24. ;
  25. type
  26. { some helpful data types }
  27. tprocedure = procedure;
  28. tfilename = string;
  29. longrec = packed record
  30. lo,hi : word;
  31. end;
  32. wordrec = packed record
  33. lo,hi : byte;
  34. end;
  35. { exceptions }
  36. exception = class(TObject)
  37. private
  38. fmessage : string;
  39. fhelpcontext : longint;
  40. public
  41. constructor create(const msg : string);
  42. constructor createfmt(const msg : string; const args : array of const);
  43. constructor createres(ident : longint);
  44. { !!!! }
  45. property helpcontext : longint read fhelpcontext write fhelpcontext;
  46. property message : string read fmessage write fmessage;
  47. end;
  48. exceptclass = class of exception;
  49. { integer math exceptions }
  50. EInterror = Class(Exception);
  51. EDivByZero = Class(EIntError);
  52. ERangeError = Class(EIntError);
  53. EIntOverflow = Class(EIntError);
  54. { General math errors }
  55. EMathError = Class(Exception);
  56. EInvalidOp = Class(EMathError);
  57. EZeroDivide = Class(EMathError);
  58. EOverflow = Class(EMathError);
  59. EUnderflow = Class(EMathError);
  60. { Run-time and I/O Errors }
  61. EInOutError = class(Exception)
  62. public
  63. ErrorCode : Longint;
  64. end;
  65. EInvalidPointer = Class(Exception);
  66. EOutOfMemory = Class(Exception);
  67. EAccessViolation = Class(Exception);
  68. EInvalidCast = Class(Exception);
  69. { String conversion errors }
  70. EConvertError = class(Exception);
  71. { Other errors }
  72. EAbort = Class(Exception);
  73. EAbstractError = Class(Exception);
  74. EAssertionFailed = Class(Exception);
  75. { Read date & Time function declarations }
  76. {$i datih.inc}
  77. { Read String Handling functions declaration }
  78. {$i sysstrh.inc}
  79. { Read pchar handling functions declration }
  80. {$i syspchh.inc}
  81. { Read filename handling functions declaration }
  82. {$i finah.inc}
  83. { Read other file handling function declarations }
  84. {$i filutilh.inc}
  85. { Read disk function declarations }
  86. {$i diskh.inc}
  87. implementation
  88. { Read message string definitions }
  89. {
  90. Add a language with IFDEF LANG_NAME
  91. just befor the final ELSE. This way English will always be the default.
  92. }
  93. {$IFDEF LANG_GERMAN}
  94. {$i strg.inc} // Does not exist yet !!
  95. {$ELSE}
  96. {$i stre.inc}
  97. {$ENDIF}
  98. { Read filename handling functions implementation }
  99. {$i fina.inc}
  100. { Read other file handling function implementations }
  101. {$i filutil.inc}
  102. { Read disk function implementations }
  103. {$i disk.inc}
  104. { Read date & Time function implementations }
  105. {$i dati.inc}
  106. { Read String Handling functions implementation }
  107. {$i sysstr.inc}
  108. { Read pchar handling functions implementation }
  109. {$i syspch.inc}
  110. constructor exception.create(const msg : string);
  111. begin
  112. inherited create;
  113. fmessage:=msg;
  114. end;
  115. constructor exception.createfmt(const msg : string; const args : array of const);
  116. begin
  117. inherited create;
  118. fmessage:=Format(msg,args);
  119. end;
  120. constructor exception.createres(ident : longint);
  121. begin
  122. inherited create;
  123. {!!!!!}
  124. end;
  125. Procedure CatchUnhandledException (Obj : TObject; Addr: Pointer);
  126. Var
  127. Message : String;
  128. begin
  129. {$ifndef USE_WINDOWS}
  130. Writeln ('An unhandled exception occurred at ',HexStr(Longint(Addr),8),' : ');
  131. if Obj is exception then
  132. begin
  133. Message:=Exception(Obj).Message;
  134. Writeln (Message);
  135. end
  136. else
  137. Writeln ('Exception object ',Obj.ClassName,' is not of class Exception.');
  138. Halt(217);
  139. {$else}
  140. {$endif}
  141. end;
  142. Var OutOfMemory : EOutOfMemory;
  143. InValidPointer : EInvalidPointer;
  144. Procedure RunErrorToExcept (ErrNo : Longint; Address : Pointer);
  145. Var E : Exception;
  146. S : String;
  147. begin
  148. Case Errno of
  149. 1,203 : E:=OutOfMemory;
  150. //!! ?? 2 is a 'file not found error' ??
  151. 2,204 : E:=InvalidPointer;
  152. 3,4,5,6,100,101,102,103,105,106 : { I/O errors }
  153. begin
  154. Case Errno of
  155. 3 : S:=SInvalidFileName;
  156. 4 : S:=STooManyOpenFiles;
  157. 5 : S:=SAccessDenied;
  158. 6 : S:=SInvalidFileHandle;
  159. 15 : S:=SInvalidDrive;
  160. 100 : S:=SEndOfFile;
  161. 101 : S:=SDiskFull;
  162. 102 : S:=SFileNotAssigned;
  163. 103 : S:=SFileNotOpen;
  164. 104 : S:=SFileNotOpenForInput;
  165. 105 : S:=SFileNotOpenForOutput;
  166. 106 : S:=SInvalidInput;
  167. end;
  168. E:=EinOutError.Create (S);
  169. EInoutError(E).ErrorCode:=IOresult; // Clears InOutRes !!
  170. end;
  171. // We don't set abstracterrorhandler, but we do it here.
  172. // Unless the use sets another handler we'll get here anyway...
  173. 200 : E:=EDivByZero.Create(SDivByZero);
  174. 201 : E:=ERangeError.Create(SRangeError);
  175. 211 : E:=EAbstractError.Create(SAbstractError);
  176. 216 : E:=EAccessViolation.Create(SAccessViolation);
  177. 219 : E:=EInvalidCast.Create(SInvalidCast);
  178. 227 : E:=EAssertionFailed.Create(SAssertionFailed);
  179. else
  180. E:=Exception.CreateFmt (SUnKnownRunTimeError,[Errno]);
  181. end;
  182. Raise E {at Address};
  183. end;
  184. Procedure AssertErrorHandler (Const Msg,FN : String;LineNo,TheAddr : Longint);
  185. Var
  186. S : String;
  187. begin
  188. If Msg='' then
  189. S:=SAssertionFailed
  190. else
  191. S:=Msg;
  192. Raise EAssertionFailed.Createfmt(SAssertError,[S,Fn,LineNo]); // at Pointer(theAddr);
  193. end;
  194. Procedure InitExceptions;
  195. {
  196. Must install uncaught exception handler (ExceptProc)
  197. and install exceptions for system exceptions or signals.
  198. (e.g: SIGSEGV -> ESegFault or so.)
  199. }
  200. begin
  201. ExceptProc:=@CatchUnhandledException;
  202. // Create objects that may have problems when there is no memory.
  203. OutOfMemory:=EOutOfMemory.Create(SOutOfMemory);
  204. InvalidPointer:=EInvalidPointer.Create(SInvalidPointer);
  205. AssertErrorProc:=@AssertErrorHandler;
  206. ErrorProc:=@RunErrorToExcept;
  207. end;
  208. {Initialization code.}
  209. begin
  210. InitExceptions;
  211. end.
  212. {
  213. $Log$
  214. Revision 1.18 1998-12-15 22:43:12 peter
  215. * removed temp symbols
  216. Revision 1.17 1998/10/20 19:26:37 michael
  217. + Forgot to include disk functions
  218. Revision 1.16 1998/10/11 12:23:41 michael
  219. + More sysutils calls.
  220. Revision 1.15 1998/10/10 09:53:10 michael
  221. Added assertion handling
  222. Revision 1.14 1998/10/03 15:08:05 florian
  223. * EInvalidCast added (from runerror 219)
  224. Revision 1.13 1998/10/02 13:00:11 michael
  225. + More RTL error handling
  226. Revision 1.12 1998/10/02 12:17:18 michael
  227. + Made sure it compiles with official 0.99.8
  228. Revision 1.11 1998/10/01 16:04:11 michael
  229. + Added RTL error handling
  230. Revision 1.10 1998/09/24 23:45:27 peter
  231. * updated for auto objpas loading
  232. Revision 1.9 1998/09/24 16:13:49 michael
  233. Changes in exception and open array handling
  234. Revision 1.8 1998/09/18 23:57:26 michael
  235. * Changed use_excepions to useexceptions
  236. Revision 1.7 1998/09/16 14:34:38 pierre
  237. * go32v2 did not compile
  238. * wrong code in systr.inc corrected
  239. Revision 1.6 1998/09/16 08:28:44 michael
  240. Update from gertjan Schouten, plus small fix for linux
  241. Revision 1.5 1998/09/04 08:49:07 peter
  242. * 0.99.5 doesn't compile a whole objpas anymore to overcome crashes
  243. Revision 1.4 1998/08/10 15:52:27 peter
  244. * fixed so 0.99.5 compiles it, but no exception class
  245. Revision 1.3 1998/07/29 15:44:32 michael
  246. included sysutils and math.pp as target. They compile now.
  247. }