sysutils.inc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. { Read message string definitions }
  13. {
  14. Add a language with IFDEF LANG_NAME
  15. just befor the final ELSE. This way English will always be the default.
  16. }
  17. {$IFDEF LANG_GERMAN}
  18. {$i strg.inc} // Does not exist yet !!
  19. {$ELSE}
  20. {$i stre.inc}
  21. {$ENDIF}
  22. { Read filename handling functions implementation }
  23. {$i fina.inc}
  24. { Read String Handling functions implementation }
  25. {$i sysstr.inc}
  26. { Read date & Time function implementations }
  27. {$i dati.inc}
  28. { Read pchar handling functions implementation }
  29. {$i syspch.inc}
  30. constructor Exception.Create(const msg : string);
  31. begin
  32. inherited create;
  33. fmessage:=msg;
  34. end;
  35. constructor Exception.CreateFmt(const msg : string; const args : array of const);
  36. begin
  37. inherited create;
  38. fmessage:=Format(msg,args);
  39. end;
  40. constructor Exception.CreateRes(ResString: PString);
  41. begin
  42. inherited create;
  43. fmessage:=ResString^;
  44. end;
  45. constructor Exception.CreateResFmt(ResString: PString; const Args: array of const);
  46. begin
  47. inherited create;
  48. fmessage:=Format(ResString^,args);
  49. end;
  50. constructor Exception.CreateHelp(const Msg: string; AHelpContext: Integer);
  51. begin
  52. inherited create;
  53. fmessage:=Msg;
  54. fhelpcontext:=AHelpContext;
  55. end;
  56. constructor Exception.CreateFmtHelp(const Msg: string; const Args: array of const;
  57. AHelpContext: Integer);
  58. begin
  59. inherited create;
  60. fmessage:=Format(Msg,args);
  61. fhelpcontext:=AHelpContext;
  62. end;
  63. constructor Exception.CreateResHelp(ResString: PString; AHelpContext: Integer);
  64. begin
  65. inherited create;
  66. fmessage:=ResString^;
  67. fhelpcontext:=AHelpContext;
  68. end;
  69. constructor Exception.CreateResFmtHelp(ResString: PString; const Args: array of const;
  70. AHelpContext: Integer);
  71. begin
  72. inherited create;
  73. fmessage:=Format(ResString^,args);
  74. fhelpcontext:=AHelpContext;
  75. end;
  76. {$ifopt S+}
  77. {$define STACKCHECK_WAS_ON}
  78. {$S-}
  79. {$endif OPT S }
  80. Procedure CatchUnhandledException (Obj : TObject; Addr,Frame: Pointer);
  81. Var
  82. Message : String;
  83. begin
  84. Writeln(stdout,'An unhandled exception occurred at 0x',HexStr(Longint(Addr),8),' :');
  85. if Obj is exception then
  86. begin
  87. Message:=Exception(Obj).Message;
  88. Writeln(stdout,Message);
  89. end
  90. else
  91. Writeln(stdout,'Exception object ',Obj.ClassName,' is not of class Exception.');
  92. { to get a nice symify }
  93. Writeln(stdout,BackTraceStrFunc(Longint(Addr)));
  94. Dump_Stack(stdout,longint(frame));
  95. Writeln(stdout,'');
  96. Halt(217);
  97. end;
  98. Var OutOfMemory : EOutOfMemory;
  99. InValidPointer : EInvalidPointer;
  100. Procedure RunErrorToExcept (ErrNo : Longint; Address,Frame : Pointer);
  101. Var E : Exception;
  102. S : String;
  103. begin
  104. Case Errno of
  105. 1,203 : E:=OutOfMemory;
  106. 204 : E:=InvalidPointer;
  107. 2,3,4,5,6,100,101,102,103,105,106 : { I/O errors }
  108. begin
  109. Case Errno of
  110. 2 : S:=SFileNotFound;
  111. 3 : S:=SInvalidFileName;
  112. 4 : S:=STooManyOpenFiles;
  113. 5 : S:=SAccessDenied;
  114. 6 : S:=SInvalidFileHandle;
  115. 15 : S:=SInvalidDrive;
  116. 100 : S:=SEndOfFile;
  117. 101 : S:=SDiskFull;
  118. 102 : S:=SFileNotAssigned;
  119. 103 : S:=SFileNotOpen;
  120. 104 : S:=SFileNotOpenForInput;
  121. 105 : S:=SFileNotOpenForOutput;
  122. 106 : S:=SInvalidInput;
  123. end;
  124. E:=EinOutError.Create (S);
  125. EInoutError(E).ErrorCode:=IOresult; // Clears InOutRes !!
  126. end;
  127. // We don't set abstracterrorhandler, but we do it here.
  128. // Unless the use sets another handler we'll get here anyway...
  129. 200 : E:=EDivByZero.Create(SDivByZero);
  130. 201 : E:=ERangeError.Create(SRangeError);
  131. 205 : E:=EOverflow.Create(SOverflow);
  132. 206 : E:=EOverflow.Create(SUnderflow);
  133. 207 : E:=EInvalidOp.Create(SInvalidOp);
  134. 211 : E:=EAbstractError.Create(SAbstractError);
  135. 215 : E:=EIntOverflow.Create(SIntOverflow);
  136. 216 : E:=EAccessViolation.Create(SAccessViolation);
  137. 219 : E:=EInvalidCast.Create(SInvalidCast);
  138. 227 : E:=EAssertionFailed.Create(SAssertionFailed);
  139. else
  140. E:=Exception.CreateFmt (SUnKnownRunTimeError,[Errno]);
  141. end;
  142. Raise E at longint(Address){$ifdef ENHANCEDRAISE},longint(Frame){$endif};
  143. end;
  144. Procedure AssertErrorHandler (Const Msg,FN : ShortString;LineNo,TheAddr : Longint);
  145. Var
  146. S : String;
  147. begin
  148. If Msg='' then
  149. S:=SAssertionFailed
  150. else
  151. S:=Msg;
  152. Raise EAssertionFailed.Createfmt(SAssertError,[S,Fn,LineNo]); // at Pointer(theAddr);
  153. end;
  154. {$ifdef STACKCHECK_WAS_ON}
  155. {$S+}
  156. {$endif}
  157. Procedure InitExceptions;
  158. {
  159. Must install uncaught exception handler (ExceptProc)
  160. and install exceptions for system exceptions or signals.
  161. (e.g: SIGSEGV -> ESegFault or so.)
  162. }
  163. begin
  164. ExceptProc:=@CatchUnhandledException;
  165. // Create objects that may have problems when there is no memory.
  166. OutOfMemory:=EOutOfMemory.Create(SOutOfMemory);
  167. InvalidPointer:=EInvalidPointer.Create(SInvalidPointer);
  168. AssertErrorProc:=@AssertErrorHandler;
  169. ErrorProc:=@RunErrorToExcept;
  170. OnShowException:=Nil;
  171. end;
  172. { Exception handling routines }
  173. function ExceptObject: TObject;
  174. begin
  175. If RaiseList=Nil then
  176. Result:=Nil
  177. else
  178. Result:=RaiseList^.FObject;
  179. end;
  180. function ExceptAddr: Pointer;
  181. begin
  182. If RaiseList=Nil then
  183. Result:=Nil
  184. else
  185. Result:=RaiseList^.Addr;
  186. end;
  187. function ExceptionErrorMessage(ExceptObject: TObject; ExceptAddr: Pointer;
  188. Buffer: PChar; Size: Integer): Integer;
  189. Var
  190. S : AnsiString;
  191. Len : Integer;
  192. begin
  193. S:=Format(SExceptionErrorMessage,[ExceptObject.ClassName,ExceptAddr]);
  194. If ExceptObject is Exception then
  195. S:=Format('%s:'#10'%s',[S,Exception(ExceptObject).Message]);
  196. Len:=Length(S);
  197. If S[Len]<>'.' then
  198. begin
  199. S:=S+'.';
  200. Inc(len);
  201. end;
  202. If Len>Size then
  203. Len:=Size;
  204. if Len > 0 then
  205. Move(S[1],Buffer^,Len);
  206. Result:=Len;
  207. end;
  208. procedure ShowException(ExceptObject: TObject; ExceptAddr: Pointer);
  209. // use shortstring. On exception, the heap may be corrupt.
  210. Var
  211. Buf : ShortString;
  212. begin
  213. SetLength(Buf,ExceptionErrorMessage(ExceptObject,ExceptAddr,@Buf[1],255));
  214. If IsConsole Then
  215. writeln(Buf)
  216. else
  217. If Assigned(OnShowException) Then
  218. OnShowException (Buf);
  219. end;
  220. procedure Abort;
  221. begin
  222. Raise EAbort.Create(SAbortError) at Get_Caller_addr(Get_Frame)
  223. end;
  224. procedure OutOfMemoryError;
  225. begin
  226. Raise OutOfMemory;
  227. end;
  228. {
  229. $Log$
  230. Revision 1.3 2000-11-23 11:04:26 sg
  231. * Protected some Move()'s by 'if' clauses so that the Move won't be
  232. executed when the length would be 0. Otherwise, the corresponding
  233. routines might get an RTE when compiled with $R+.
  234. Revision 1.2 2000/08/20 15:46:46 peter
  235. * sysutils.pp moved to target and merged with disk.inc, filutil.inc
  236. }