sysutils.pp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2000 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. { force ansistrings }
  16. {$H+}
  17. uses
  18. {$ifdef linux}
  19. linux
  20. {$endif}
  21. {$ifdef win32}
  22. dos,windows
  23. {$endif}
  24. {$ifdef go32v1}
  25. go32,dos
  26. {$endif}
  27. {$ifdef go32v2}
  28. go32,dos
  29. {$endif}
  30. {$ifdef os2}
  31. doscalls,dos
  32. {$endif}
  33. ;
  34. type
  35. { some helpful data types }
  36. tprocedure = procedure;
  37. tfilename = string;
  38. longrec = packed record
  39. lo,hi : word;
  40. end;
  41. wordrec = packed record
  42. lo,hi : byte;
  43. end;
  44. TMethod = packed record
  45. Code, Data: Pointer;
  46. end;
  47. { exceptions }
  48. exception = class(TObject)
  49. private
  50. fmessage : string;
  51. fhelpcontext : longint;
  52. public
  53. constructor create(const msg : string);
  54. constructor createfmt(const msg : string; const args : array of const);
  55. constructor createres(ident : longint);
  56. { !!!! }
  57. property helpcontext : longint read fhelpcontext write fhelpcontext;
  58. property message : string read fmessage write fmessage;
  59. end;
  60. exceptclass = class of exception;
  61. { integer math exceptions }
  62. EInterror = Class(Exception);
  63. EDivByZero = Class(EIntError);
  64. ERangeError = Class(EIntError);
  65. EIntOverflow = Class(EIntError);
  66. { General math errors }
  67. EMathError = Class(Exception);
  68. EInvalidOp = Class(EMathError);
  69. EZeroDivide = Class(EMathError);
  70. EOverflow = Class(EMathError);
  71. EUnderflow = Class(EMathError);
  72. { Run-time and I/O Errors }
  73. EInOutError = class(Exception)
  74. public
  75. ErrorCode : Longint;
  76. end;
  77. EInvalidPointer = Class(Exception);
  78. EOutOfMemory = Class(Exception);
  79. EAccessViolation = Class(Exception);
  80. EInvalidCast = Class(Exception);
  81. { String conversion errors }
  82. EConvertError = class(Exception);
  83. { Other errors }
  84. EAbort = Class(Exception);
  85. EAbstractError = Class(Exception);
  86. EAssertionFailed = Class(Exception);
  87. { FileRec/TextRec }
  88. {$i filerec.inc}
  89. {$i textrec.inc}
  90. { Read internationalization settings }
  91. {$i sysinth.inc}
  92. { Read date & Time function declarations }
  93. {$i datih.inc}
  94. { Read String Handling functions declaration }
  95. {$i sysstrh.inc}
  96. { Read pchar handling functions declration }
  97. {$i syspchh.inc}
  98. { Read filename handling functions declaration }
  99. {$i finah.inc}
  100. { Read other file handling function declarations }
  101. {$i filutilh.inc}
  102. { Read disk function declarations }
  103. {$i diskh.inc}
  104. implementation
  105. { Read message string definitions }
  106. {
  107. Add a language with IFDEF LANG_NAME
  108. just befor the final ELSE. This way English will always be the default.
  109. }
  110. {$IFDEF LANG_GERMAN}
  111. {$i strg.inc} // Does not exist yet !!
  112. {$ELSE}
  113. {$i stre.inc}
  114. {$ENDIF}
  115. { Read filename handling functions implementation }
  116. {$i fina.inc}
  117. { Read String Handling functions implementation }
  118. {$i sysstr.inc}
  119. { Read other file handling function implementations }
  120. {$i filutil.inc}
  121. { Read disk function implementations }
  122. {$i disk.inc}
  123. { Read date & Time function implementations }
  124. {$i dati.inc}
  125. { Read pchar handling functions implementation }
  126. {$i syspch.inc}
  127. constructor exception.create(const msg : string);
  128. begin
  129. inherited create;
  130. fmessage:=msg;
  131. end;
  132. constructor exception.createfmt(const msg : string; const args : array of const);
  133. begin
  134. inherited create;
  135. fmessage:=Format(msg,args);
  136. end;
  137. constructor exception.createres(ident : longint);
  138. begin
  139. inherited create;
  140. {!!!!!}
  141. end;
  142. Procedure CatchUnhandledException (Obj : TObject; Addr: Pointer);
  143. Var
  144. Message : String;
  145. begin
  146. Writeln(stdout,'An unhandled exception occurred at 0x',HexStr(Longint(Addr),8),' :');
  147. if Obj is exception then
  148. begin
  149. Message:=Exception(Obj).Message;
  150. Writeln(stdout,Message);
  151. end
  152. else
  153. Writeln(stdout,'Exception object ',Obj.ClassName,' is not of class Exception.');
  154. Writeln(stdout,'');
  155. Halt(217);
  156. end;
  157. Var OutOfMemory : EOutOfMemory;
  158. InValidPointer : EInvalidPointer;
  159. Procedure RunErrorToExcept (ErrNo : Longint; Address : Pointer);
  160. Var E : Exception;
  161. S : String;
  162. begin
  163. Case Errno of
  164. 1,203 : E:=OutOfMemory;
  165. 204 : E:=InvalidPointer;
  166. 2,3,4,5,6,100,101,102,103,105,106 : { I/O errors }
  167. begin
  168. Case Errno of
  169. 2 : S:=SFileNotFound;
  170. 3 : S:=SInvalidFileName;
  171. 4 : S:=STooManyOpenFiles;
  172. 5 : S:=SAccessDenied;
  173. 6 : S:=SInvalidFileHandle;
  174. 15 : S:=SInvalidDrive;
  175. 100 : S:=SEndOfFile;
  176. 101 : S:=SDiskFull;
  177. 102 : S:=SFileNotAssigned;
  178. 103 : S:=SFileNotOpen;
  179. 104 : S:=SFileNotOpenForInput;
  180. 105 : S:=SFileNotOpenForOutput;
  181. 106 : S:=SInvalidInput;
  182. end;
  183. E:=EinOutError.Create (S);
  184. EInoutError(E).ErrorCode:=IOresult; // Clears InOutRes !!
  185. end;
  186. // We don't set abstracterrorhandler, but we do it here.
  187. // Unless the use sets another handler we'll get here anyway...
  188. 200 : E:=EDivByZero.Create(SDivByZero);
  189. 201 : E:=ERangeError.Create(SRangeError);
  190. 205 : E:=EOverflow.Create(SOverflow);
  191. 206 : E:=EOverflow.Create(SUnderflow);
  192. 207 : E:=EInvalidOp.Create(SInvalidOp);
  193. 211 : E:=EAbstractError.Create(SAbstractError);
  194. 215 : E:=EIntOverflow.Create(SIntOverflow);
  195. 216 : E:=EAccessViolation.Create(SAccessViolation);
  196. 219 : E:=EInvalidCast.Create(SInvalidCast);
  197. 227 : E:=EAssertionFailed.Create(SAssertionFailed);
  198. else
  199. E:=Exception.CreateFmt (SUnKnownRunTimeError,[Errno]);
  200. end;
  201. Raise E at longint(Address);
  202. end;
  203. Procedure AssertErrorHandler (Const Msg,FN : ShortString;LineNo,TheAddr : Longint);
  204. Var
  205. S : String;
  206. begin
  207. If Msg='' then
  208. S:=SAssertionFailed
  209. else
  210. S:=Msg;
  211. Raise EAssertionFailed.Createfmt(SAssertError,[S,Fn,LineNo]); // at Pointer(theAddr);
  212. end;
  213. Procedure InitExceptions;
  214. {
  215. Must install uncaught exception handler (ExceptProc)
  216. and install exceptions for system exceptions or signals.
  217. (e.g: SIGSEGV -> ESegFault or so.)
  218. }
  219. begin
  220. ExceptProc:=@CatchUnhandledException;
  221. // Create objects that may have problems when there is no memory.
  222. OutOfMemory:=EOutOfMemory.Create(SOutOfMemory);
  223. InvalidPointer:=EInvalidPointer.Create(SInvalidPointer);
  224. AssertErrorProc:=@AssertErrorHandler;
  225. ErrorProc:=@RunErrorToExcept;
  226. end;
  227. { Initialization code. }
  228. Initialization
  229. InitExceptions; { Initialize exceptions. OS independent }
  230. InitInternational; { Initialize internationalization settings }
  231. Finalization
  232. OutOfMemory.Free;
  233. InValidPointer.Free;
  234. end.
  235. {
  236. $Log$
  237. Revision 1.41 2000-02-09 16:59:33 peter
  238. * truncated log
  239. Revision 1.40 2000/01/16 19:10:25 hajny
  240. * 'uses Dos' added for OS/2 target
  241. Revision 1.39 2000/01/07 16:41:44 daniel
  242. * copyright 2000
  243. Revision 1.38 1999/12/26 19:30:53 hajny
  244. * OS/2 target added to the uses clause
  245. Revision 1.36 1999/11/15 21:49:47 peter
  246. * exception address fixes
  247. Revision 1.35 1999/11/06 14:41:31 peter
  248. * truncated log
  249. Revision 1.34 1999/10/30 17:39:05 peter
  250. * memorymanager expanded with allocmem/reallocmem
  251. Revision 1.33 1999/10/26 12:29:07 peter
  252. * assert handler must use shortstring
  253. Revision 1.32 1999/09/15 20:26:30 florian
  254. * patch from Sebastian Guenther applied: TMethod implementation
  255. Revision 1.31 1999/08/28 14:53:27 florian
  256. * bug 471 fixed: run time error 2 is now converted into a file not
  257. found exception
  258. Revision 1.30 1999/08/18 11:28:24 michael
  259. * Fixed reallocmem bug 535
  260. Revision 1.29 1999/07/27 13:01:12 peter
  261. + filerec,textrec declarations
  262. }