2
0

sysutils.pp 8.7 KB

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