sysutils.pp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. {$ifopt S+}
  143. {$define STACKCHECK_WAS_ON}
  144. {$S-}
  145. {$endif OPT S }
  146. Procedure CatchUnhandledException (Obj : TObject; Addr: Pointer);
  147. Var
  148. Message : String;
  149. begin
  150. Writeln(stdout,'An unhandled exception occurred at 0x',HexStr(Longint(Addr),8),' :');
  151. if Obj is exception then
  152. begin
  153. Message:=Exception(Obj).Message;
  154. Writeln(stdout,Message);
  155. end
  156. else
  157. Writeln(stdout,'Exception object ',Obj.ClassName,' is not of class Exception.');
  158. Writeln(stdout,'');
  159. Runerror(217);
  160. end;
  161. Var OutOfMemory : EOutOfMemory;
  162. InValidPointer : EInvalidPointer;
  163. Procedure RunErrorToExcept (ErrNo : Longint; Address : Pointer);
  164. Var E : Exception;
  165. S : String;
  166. begin
  167. Case Errno of
  168. 1,203 : E:=OutOfMemory;
  169. 204 : E:=InvalidPointer;
  170. 2,3,4,5,6,100,101,102,103,105,106 : { I/O errors }
  171. begin
  172. Case Errno of
  173. 2 : S:=SFileNotFound;
  174. 3 : S:=SInvalidFileName;
  175. 4 : S:=STooManyOpenFiles;
  176. 5 : S:=SAccessDenied;
  177. 6 : S:=SInvalidFileHandle;
  178. 15 : S:=SInvalidDrive;
  179. 100 : S:=SEndOfFile;
  180. 101 : S:=SDiskFull;
  181. 102 : S:=SFileNotAssigned;
  182. 103 : S:=SFileNotOpen;
  183. 104 : S:=SFileNotOpenForInput;
  184. 105 : S:=SFileNotOpenForOutput;
  185. 106 : S:=SInvalidInput;
  186. end;
  187. E:=EinOutError.Create (S);
  188. EInoutError(E).ErrorCode:=IOresult; // Clears InOutRes !!
  189. end;
  190. // We don't set abstracterrorhandler, but we do it here.
  191. // Unless the use sets another handler we'll get here anyway...
  192. 200 : E:=EDivByZero.Create(SDivByZero);
  193. 201 : E:=ERangeError.Create(SRangeError);
  194. 205 : E:=EOverflow.Create(SOverflow);
  195. 206 : E:=EOverflow.Create(SUnderflow);
  196. 207 : E:=EInvalidOp.Create(SInvalidOp);
  197. 211 : E:=EAbstractError.Create(SAbstractError);
  198. 215 : E:=EIntOverflow.Create(SIntOverflow);
  199. 216 : E:=EAccessViolation.Create(SAccessViolation);
  200. 219 : E:=EInvalidCast.Create(SInvalidCast);
  201. 227 : E:=EAssertionFailed.Create(SAssertionFailed);
  202. else
  203. E:=Exception.CreateFmt (SUnKnownRunTimeError,[Errno]);
  204. end;
  205. Raise E at longint(Address);
  206. end;
  207. Procedure AssertErrorHandler (Const Msg,FN : ShortString;LineNo,TheAddr : Longint);
  208. Var
  209. S : String;
  210. begin
  211. If Msg='' then
  212. S:=SAssertionFailed
  213. else
  214. S:=Msg;
  215. Raise EAssertionFailed.Createfmt(SAssertError,[S,Fn,LineNo]); // at Pointer(theAddr);
  216. end;
  217. {$ifdef STACKCHECK_WAS_ON}
  218. {$S+}
  219. {$endif}
  220. Procedure InitExceptions;
  221. {
  222. Must install uncaught exception handler (ExceptProc)
  223. and install exceptions for system exceptions or signals.
  224. (e.g: SIGSEGV -> ESegFault or so.)
  225. }
  226. begin
  227. ExceptProc:=@CatchUnhandledException;
  228. // Create objects that may have problems when there is no memory.
  229. OutOfMemory:=EOutOfMemory.Create(SOutOfMemory);
  230. InvalidPointer:=EInvalidPointer.Create(SInvalidPointer);
  231. AssertErrorProc:=@AssertErrorHandler;
  232. ErrorProc:=@RunErrorToExcept;
  233. end;
  234. { Initialization code. }
  235. Initialization
  236. InitExceptions; { Initialize exceptions. OS independent }
  237. InitInternational; { Initialize internationalization settings }
  238. Finalization
  239. OutOfMemory.Free;
  240. InValidPointer.Free;
  241. end.
  242. {
  243. $Log$
  244. Revision 1.43 2000-03-30 13:54:15 pierre
  245. No stack check inside CatchUnhandledException
  246. Revision 1.42 2000/02/10 22:56:43 florian
  247. * quick hack for stack trace in the case of an unhandled exception
  248. Revision 1.41 2000/02/09 16:59:33 peter
  249. * truncated log
  250. Revision 1.40 2000/01/16 19:10:25 hajny
  251. * 'uses Dos' added for OS/2 target
  252. Revision 1.39 2000/01/07 16:41:44 daniel
  253. * copyright 2000
  254. Revision 1.38 1999/12/26 19:30:53 hajny
  255. * OS/2 target added to the uses clause
  256. Revision 1.36 1999/11/15 21:49:47 peter
  257. * exception address fixes
  258. Revision 1.35 1999/11/06 14:41:31 peter
  259. * truncated log
  260. Revision 1.34 1999/10/30 17:39:05 peter
  261. * memorymanager expanded with allocmem/reallocmem
  262. Revision 1.33 1999/10/26 12:29:07 peter
  263. * assert handler must use shortstring
  264. Revision 1.32 1999/09/15 20:26:30 florian
  265. * patch from Sebastian Guenther applied: TMethod implementation
  266. Revision 1.31 1999/08/28 14:53:27 florian
  267. * bug 471 fixed: run time error 2 is now converted into a file not
  268. found exception
  269. Revision 1.30 1999/08/18 11:28:24 michael
  270. * Fixed reallocmem bug 535
  271. Revision 1.29 1999/07/27 13:01:12 peter
  272. + filerec,textrec declarations
  273. }