sysutils.pp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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. { Read String Handling functions declaration }
  35. {$i sysstrh.inc}
  36. type
  37. { some helpful data types }
  38. tprocedure = procedure;
  39. tfilename = string;
  40. tsyscharset = set of char;
  41. longrec = packed record
  42. lo,hi : word;
  43. end;
  44. wordrec = packed record
  45. lo,hi : byte;
  46. end;
  47. TMethod = packed record
  48. Code, Data: Pointer;
  49. end;
  50. { exceptions }
  51. Exception = class(TObject)
  52. private
  53. fmessage : string;
  54. fhelpcontext : longint;
  55. public
  56. constructor Create(const msg : string);
  57. constructor CreateFmt(const msg : string; const args : array of const);
  58. constructor CreateRes(ResString: PString);
  59. constructor CreateResFmt(ResString: PString; const Args: array of const);
  60. constructor CreateHelp(const Msg: string; AHelpContext: Integer);
  61. constructor CreateFmtHelp(const Msg: string; const Args: array of const;
  62. AHelpContext: Integer);
  63. constructor CreateResHelp(ResString: PString; AHelpContext: Integer);
  64. constructor CreateResFmtHelp(ResString: PString; const Args: array of const;
  65. AHelpContext: Integer);
  66. { !!!! }
  67. property helpcontext : longint read fhelpcontext write fhelpcontext;
  68. property message : string read fmessage write fmessage;
  69. end;
  70. ExceptClass = class of Exception;
  71. { integer math exceptions }
  72. EInterror = Class(Exception);
  73. EDivByZero = Class(EIntError);
  74. ERangeError = Class(EIntError);
  75. EIntOverflow = Class(EIntError);
  76. { General math errors }
  77. EMathError = Class(Exception);
  78. EInvalidOp = Class(EMathError);
  79. EZeroDivide = Class(EMathError);
  80. EOverflow = Class(EMathError);
  81. EUnderflow = Class(EMathError);
  82. { Run-time and I/O Errors }
  83. EInOutError = class(Exception)
  84. public
  85. ErrorCode : Longint;
  86. end;
  87. EInvalidPointer = Class(Exception);
  88. EOutOfMemory = Class(Exception);
  89. EAccessViolation = Class(Exception);
  90. EInvalidCast = Class(Exception);
  91. { String conversion errors }
  92. EConvertError = class(Exception);
  93. { Other errors }
  94. EAbort = Class(Exception);
  95. EAbstractError = Class(Exception);
  96. EAssertionFailed = Class(Exception);
  97. { Exception handling routines }
  98. function ExceptObject: TObject;
  99. function ExceptAddr: Pointer;
  100. function ExceptionErrorMessage(ExceptObject: TObject; ExceptAddr: Pointer;
  101. Buffer: PChar; Size: Integer): Integer;
  102. procedure ShowException(ExceptObject: TObject; ExceptAddr: Pointer);
  103. procedure Abort;
  104. procedure OutOfMemoryError;
  105. procedure Beep;
  106. Var
  107. OnShowException : Procedure (Msg : ShortString);
  108. { FileRec/TextRec }
  109. {$i filerec.inc}
  110. {$i textrec.inc}
  111. { Read internationalization settings }
  112. {$i sysinth.inc}
  113. { Read date & Time function declarations }
  114. {$i datih.inc}
  115. { Read pchar handling functions declration }
  116. {$i syspchh.inc}
  117. { Read filename handling functions declaration }
  118. {$i finah.inc}
  119. { Read other file handling function declarations }
  120. {$i filutilh.inc}
  121. { Read disk function declarations }
  122. {$i diskh.inc}
  123. implementation
  124. { Read message string definitions }
  125. {
  126. Add a language with IFDEF LANG_NAME
  127. just befor the final ELSE. This way English will always be the default.
  128. }
  129. {$IFDEF LANG_GERMAN}
  130. {$i strg.inc} // Does not exist yet !!
  131. {$ELSE}
  132. {$i stre.inc}
  133. {$ENDIF}
  134. { Read filename handling functions implementation }
  135. {$i fina.inc}
  136. { Read String Handling functions implementation }
  137. {$i sysstr.inc}
  138. { Read other file handling function implementations }
  139. {$i filutil.inc}
  140. { Read disk function implementations }
  141. {$i disk.inc}
  142. { Read date & Time function implementations }
  143. {$i dati.inc}
  144. { Read pchar handling functions implementation }
  145. {$i syspch.inc}
  146. constructor Exception.Create(const msg : string);
  147. begin
  148. inherited create;
  149. fmessage:=msg;
  150. end;
  151. constructor Exception.CreateFmt(const msg : string; const args : array of const);
  152. begin
  153. inherited create;
  154. fmessage:=Format(msg,args);
  155. end;
  156. constructor Exception.CreateRes(ResString: PString);
  157. begin
  158. inherited create;
  159. fmessage:=ResString^;
  160. end;
  161. constructor Exception.CreateResFmt(ResString: PString; const Args: array of const); overload;
  162. begin
  163. inherited create;
  164. fmessage:=Format(ResString^,args);
  165. end;
  166. constructor Exception.CreateHelp(const Msg: string; AHelpContext: Integer);
  167. begin
  168. inherited create;
  169. fmessage:=Msg;
  170. fhelpcontext:=AHelpContext;
  171. end;
  172. constructor Exception.CreateFmtHelp(const Msg: string; const Args: array of const;
  173. AHelpContext: Integer);
  174. begin
  175. inherited create;
  176. fmessage:=Format(Msg,args);
  177. fhelpcontext:=AHelpContext;
  178. end;
  179. constructor Exception.CreateResHelp(ResString: PString; AHelpContext: Integer); overload;
  180. begin
  181. inherited create;
  182. fmessage:=ResString^;
  183. fhelpcontext:=AHelpContext;
  184. end;
  185. constructor Exception.CreateResFmtHelp(ResString: PString; const Args: array of const;
  186. AHelpContext: Integer); overload;
  187. begin
  188. inherited create;
  189. fmessage:=Format(ResString^,args);
  190. fhelpcontext:=AHelpContext;
  191. end;
  192. {$ifopt S+}
  193. {$define STACKCHECK_WAS_ON}
  194. {$S-}
  195. {$endif OPT S }
  196. Procedure CatchUnhandledException (Obj : TObject; Addr,Frame: Pointer);
  197. Var
  198. Message : String;
  199. begin
  200. Writeln(stdout,'An unhandled exception occurred at 0x',HexStr(Longint(Addr),8),' :');
  201. if Obj is exception then
  202. begin
  203. Message:=Exception(Obj).Message;
  204. Writeln(stdout,Message);
  205. end
  206. else
  207. Writeln(stdout,'Exception object ',Obj.ClassName,' is not of class Exception.');
  208. { to get a nice symify }
  209. Writeln(stdout,BackTraceStrFunc(Longint(Addr)));
  210. Dump_Stack(stdout,longint(frame));
  211. Writeln(stdout,'');
  212. Halt(217);
  213. end;
  214. Var OutOfMemory : EOutOfMemory;
  215. InValidPointer : EInvalidPointer;
  216. Procedure RunErrorToExcept (ErrNo : Longint; Address,Frame : Pointer);
  217. Var E : Exception;
  218. S : String;
  219. begin
  220. Case Errno of
  221. 1,203 : E:=OutOfMemory;
  222. 204 : E:=InvalidPointer;
  223. 2,3,4,5,6,100,101,102,103,105,106 : { I/O errors }
  224. begin
  225. Case Errno of
  226. 2 : S:=SFileNotFound;
  227. 3 : S:=SInvalidFileName;
  228. 4 : S:=STooManyOpenFiles;
  229. 5 : S:=SAccessDenied;
  230. 6 : S:=SInvalidFileHandle;
  231. 15 : S:=SInvalidDrive;
  232. 100 : S:=SEndOfFile;
  233. 101 : S:=SDiskFull;
  234. 102 : S:=SFileNotAssigned;
  235. 103 : S:=SFileNotOpen;
  236. 104 : S:=SFileNotOpenForInput;
  237. 105 : S:=SFileNotOpenForOutput;
  238. 106 : S:=SInvalidInput;
  239. end;
  240. E:=EinOutError.Create (S);
  241. EInoutError(E).ErrorCode:=IOresult; // Clears InOutRes !!
  242. end;
  243. // We don't set abstracterrorhandler, but we do it here.
  244. // Unless the use sets another handler we'll get here anyway...
  245. 200 : E:=EDivByZero.Create(SDivByZero);
  246. 201 : E:=ERangeError.Create(SRangeError);
  247. 205 : E:=EOverflow.Create(SOverflow);
  248. 206 : E:=EOverflow.Create(SUnderflow);
  249. 207 : E:=EInvalidOp.Create(SInvalidOp);
  250. 211 : E:=EAbstractError.Create(SAbstractError);
  251. 215 : E:=EIntOverflow.Create(SIntOverflow);
  252. 216 : E:=EAccessViolation.Create(SAccessViolation);
  253. 219 : E:=EInvalidCast.Create(SInvalidCast);
  254. 227 : E:=EAssertionFailed.Create(SAssertionFailed);
  255. else
  256. E:=Exception.CreateFmt (SUnKnownRunTimeError,[Errno]);
  257. end;
  258. Raise E at longint(Address),longint(Frame);
  259. end;
  260. Procedure AssertErrorHandler (Const Msg,FN : ShortString;LineNo,TheAddr : Longint);
  261. Var
  262. S : String;
  263. begin
  264. If Msg='' then
  265. S:=SAssertionFailed
  266. else
  267. S:=Msg;
  268. Raise EAssertionFailed.Createfmt(SAssertError,[S,Fn,LineNo]); // at Pointer(theAddr);
  269. end;
  270. {$ifdef STACKCHECK_WAS_ON}
  271. {$S+}
  272. {$endif}
  273. Procedure InitExceptions;
  274. {
  275. Must install uncaught exception handler (ExceptProc)
  276. and install exceptions for system exceptions or signals.
  277. (e.g: SIGSEGV -> ESegFault or so.)
  278. }
  279. begin
  280. ExceptProc:=@CatchUnhandledException;
  281. // Create objects that may have problems when there is no memory.
  282. OutOfMemory:=EOutOfMemory.Create(SOutOfMemory);
  283. InvalidPointer:=EInvalidPointer.Create(SInvalidPointer);
  284. AssertErrorProc:=@AssertErrorHandler;
  285. ErrorProc:=@RunErrorToExcept;
  286. OnShowException:=Nil;
  287. end;
  288. { Exception handling routines }
  289. function ExceptObject: TObject;
  290. begin
  291. If RaiseList=Nil then
  292. Result:=Nil
  293. else
  294. Result:=RaiseList^.FObject;
  295. end;
  296. function ExceptAddr: Pointer;
  297. begin
  298. If RaiseList=Nil then
  299. Result:=Nil
  300. else
  301. Result:=RaiseList^.Addr;
  302. end;
  303. function ExceptionErrorMessage(ExceptObject: TObject; ExceptAddr: Pointer;
  304. Buffer: PChar; Size: Integer): Integer;
  305. Var
  306. S : AnsiString;
  307. Len : Integer;
  308. begin
  309. S:=Format(SExceptionErrorMessage,[ExceptObject.ClassName,ExceptAddr]);
  310. If ExceptObject is Exception then
  311. S:=Format('%s:'#10'%s',[S,Exception(ExceptObject).Message]);
  312. Len:=Length(S);
  313. If S[Len]<>'.' then
  314. begin
  315. S:=S+'.';
  316. Inc(len);
  317. end;
  318. If Len>Size then
  319. Len:=Size;
  320. Move(S[1],Buffer^,Len);
  321. Result:=Len;
  322. end;
  323. procedure ShowException(ExceptObject: TObject; ExceptAddr: Pointer);
  324. // use shortstring. On exception, the heap may be corrupt.
  325. Var
  326. Buf : ShortString;
  327. begin
  328. SetLength(Buf,ExceptionErrorMessage(ExceptObject,ExceptAddr,@Buf[1],255));
  329. If IsConsole Then
  330. writeln(Buf)
  331. else
  332. If Assigned(OnShowException) Then
  333. OnShowException (Buf);
  334. end;
  335. procedure Abort;
  336. begin
  337. Raise EAbort.Create(SAbortError) at Get_Caller_addr(Get_Frame)
  338. end;
  339. procedure OutOfMemoryError;
  340. begin
  341. Raise OutOfMemory;
  342. end;
  343. procedure Beep;
  344. begin
  345. {$ifdef win32}
  346. MessageBeep(0);
  347. {$else}
  348. {$endif}
  349. end;
  350. { Initialization code. }
  351. Initialization
  352. InitExceptions; { Initialize exceptions. OS independent }
  353. InitInternational; { Initialize internationalization settings }
  354. Finalization
  355. OutOfMemory.Free;
  356. InValidPointer.Free;
  357. end.
  358. {
  359. $Log$
  360. Revision 1.4 2000-07-27 16:20:52 sg
  361. * Applied patch by Markus Kaemmerer with minor modifications: More methods
  362. of the Exception class are now implemented (in a manner so that they can
  363. be used as in Delphi, although the declarations are somewhat different)
  364. Revision 1.3 2000/07/14 10:33:10 michael
  365. + Conditionals fixed
  366. Revision 1.2 2000/07/13 11:33:51 michael
  367. + removed logs
  368. }