sysutils.pp 7.1 KB

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