sysutils.inc 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. { 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. procedure FreeAndNil(var obj);
  31. var
  32. temp: tobject;
  33. begin
  34. temp:=tobject(obj);
  35. pointer(obj):=nil;
  36. temp.free;
  37. end;
  38. constructor Exception.Create(const msg : string);
  39. begin
  40. inherited create;
  41. fmessage:=msg;
  42. end;
  43. constructor Exception.CreateFmt(const msg : string; const args : array of const);
  44. begin
  45. inherited create;
  46. fmessage:=Format(msg,args);
  47. end;
  48. constructor Exception.CreateRes(ResString: PString);
  49. begin
  50. inherited create;
  51. fmessage:=ResString^;
  52. end;
  53. constructor Exception.CreateResFmt(ResString: PString; const Args: array of const);
  54. begin
  55. inherited create;
  56. fmessage:=Format(ResString^,args);
  57. end;
  58. constructor Exception.CreateHelp(const Msg: string; AHelpContext: Integer);
  59. begin
  60. inherited create;
  61. fmessage:=Msg;
  62. fhelpcontext:=AHelpContext;
  63. end;
  64. constructor Exception.CreateFmtHelp(const Msg: string; const Args: array of const;
  65. AHelpContext: Integer);
  66. begin
  67. inherited create;
  68. fmessage:=Format(Msg,args);
  69. fhelpcontext:=AHelpContext;
  70. end;
  71. constructor Exception.CreateResHelp(ResString: PString; AHelpContext: Integer);
  72. begin
  73. inherited create;
  74. fmessage:=ResString^;
  75. fhelpcontext:=AHelpContext;
  76. end;
  77. constructor Exception.CreateResFmtHelp(ResString: PString; const Args: array of const;
  78. AHelpContext: Integer);
  79. begin
  80. inherited create;
  81. fmessage:=Format(ResString^,args);
  82. fhelpcontext:=AHelpContext;
  83. end;
  84. procedure EHeapMemoryError.FreeInstance;
  85. begin
  86. if AllowFree then
  87. inherited FreeInstance;
  88. end;
  89. {$ifopt S+}
  90. {$define STACKCHECK_WAS_ON}
  91. {$S-}
  92. {$endif OPT S }
  93. Procedure CatchUnhandledException (Obj : TObject; Addr,Frame: Pointer);
  94. Var
  95. Message : String;
  96. begin
  97. Writeln(stdout,'An unhandled exception occurred at 0x',HexStr(Longint(Addr),8),' :');
  98. if Obj is exception then
  99. begin
  100. Message:=Exception(Obj).Message;
  101. Writeln(stdout,Message);
  102. end
  103. else
  104. Writeln(stdout,'Exception object ',Obj.ClassName,' is not of class Exception.');
  105. { to get a nice symify }
  106. Writeln(stdout,BackTraceStrFunc(Longint(Addr)));
  107. Dump_Stack(stdout,longint(frame));
  108. Writeln(stdout,'');
  109. Halt(217);
  110. end;
  111. Var OutOfMemory : EOutOfMemory;
  112. InValidPointer : EInvalidPointer;
  113. Procedure RunErrorToExcept (ErrNo : Longint; Address,Frame : Pointer);
  114. Var E : Exception;
  115. S : String;
  116. begin
  117. Case Errno of
  118. 1,203 : E:=OutOfMemory;
  119. 204 : E:=InvalidPointer;
  120. 2,3,4,5,6,100,101,102,103,105,106 : { I/O errors }
  121. begin
  122. Case Errno of
  123. 2 : S:=SFileNotFound;
  124. 3 : S:=SInvalidFileName;
  125. 4 : S:=STooManyOpenFiles;
  126. 5 : S:=SAccessDenied;
  127. 6 : S:=SInvalidFileHandle;
  128. 15 : S:=SInvalidDrive;
  129. 100 : S:=SEndOfFile;
  130. 101 : S:=SDiskFull;
  131. 102 : S:=SFileNotAssigned;
  132. 103 : S:=SFileNotOpen;
  133. 104 : S:=SFileNotOpenForInput;
  134. 105 : S:=SFileNotOpenForOutput;
  135. 106 : S:=SInvalidInput;
  136. end;
  137. E:=EinOutError.Create (S);
  138. EInoutError(E).ErrorCode:=IOresult; // Clears InOutRes !!
  139. end;
  140. // We don't set abstracterrorhandler, but we do it here.
  141. // Unless the use sets another handler we'll get here anyway...
  142. 200 : E:=EDivByZero.Create(SDivByZero);
  143. 201 : E:=ERangeError.Create(SRangeError);
  144. 205 : E:=EOverflow.Create(SOverflow);
  145. 206 : E:=EOverflow.Create(SUnderflow);
  146. 207 : E:=EInvalidOp.Create(SInvalidOp);
  147. 211 : E:=EAbstractError.Create(SAbstractError);
  148. 215 : E:=EIntOverflow.Create(SIntOverflow);
  149. 216 : E:=EAccessViolation.Create(SAccessViolation);
  150. // !!!!! 217 : ;
  151. // !!!!! 218 : ;
  152. 219 : E:=EInvalidCast.Create(SInvalidCast);
  153. 220 : E:=EVariantError.Create(SInvalidVarCast);
  154. 221 : E:=EVariantError.Create(SInvalidVarOp);
  155. 222 : E:=EVariantError.Create(SDispatchError);
  156. 223 : E:=EVariantError.Create(SVarArrayCreate);
  157. 224 : E:=EVariantError.Create(SVarNotArray);
  158. 225 : E:=EVariantError.Create(SVarArrayBounds);
  159. 227 : E:=EAssertionFailed.Create(SAssertionFailed);
  160. // !!!!! 228 : ;
  161. // !!!!! 229 : ;
  162. else
  163. E:=Exception.CreateFmt (SUnKnownRunTimeError,[Errno]);
  164. end;
  165. Raise E at longint(Address){$ifdef ENHANCEDRAISE},longint(Frame){$endif};
  166. end;
  167. Procedure AssertErrorHandler (Const Msg,FN : ShortString;LineNo,TheAddr : Longint);
  168. Var
  169. S : String;
  170. begin
  171. If Msg='' then
  172. S:=SAssertionFailed
  173. else
  174. S:=Msg;
  175. Raise EAssertionFailed.Createfmt(SAssertError,[S,Fn,LineNo]); // at Pointer(theAddr);
  176. end;
  177. {$ifdef STACKCHECK_WAS_ON}
  178. {$S+}
  179. {$endif}
  180. Procedure InitExceptions;
  181. {
  182. Must install uncaught exception handler (ExceptProc)
  183. and install exceptions for system exceptions or signals.
  184. (e.g: SIGSEGV -> ESegFault or so.)
  185. }
  186. begin
  187. ExceptProc:=@CatchUnhandledException;
  188. // Create objects that may have problems when there is no memory.
  189. OutOfMemory:=EOutOfMemory.Create(SOutOfMemory);
  190. OutOfMemory.AllowFree:=false;
  191. InvalidPointer:=EInvalidPointer.Create(SInvalidPointer);
  192. InvalidPointer.AllowFree:=false;
  193. AssertErrorProc:=@AssertErrorHandler;
  194. ErrorProc:=@RunErrorToExcept;
  195. OnShowException:=Nil;
  196. end;
  197. Procedure DoneExceptions;
  198. begin
  199. OutOfMemory.AllowFree:=true;
  200. OutOfMemory.Free;
  201. InValidPointer.AllowFree:=true;
  202. InValidPointer.Free;
  203. end;
  204. { Exception handling routines }
  205. function ExceptObject: TObject;
  206. begin
  207. If RaiseList=Nil then
  208. Result:=Nil
  209. else
  210. Result:=RaiseList^.FObject;
  211. end;
  212. function ExceptAddr: Pointer;
  213. begin
  214. If RaiseList=Nil then
  215. Result:=Nil
  216. else
  217. Result:=RaiseList^.Addr;
  218. end;
  219. function ExceptionErrorMessage(ExceptObject: TObject; ExceptAddr: Pointer;
  220. Buffer: PChar; Size: Integer): Integer;
  221. Var
  222. S : AnsiString;
  223. Len : Integer;
  224. begin
  225. S:=Format(SExceptionErrorMessage,[ExceptObject.ClassName,ExceptAddr]);
  226. If ExceptObject is Exception then
  227. S:=Format('%s:'#10'%s',[S,Exception(ExceptObject).Message]);
  228. Len:=Length(S);
  229. If S[Len]<>'.' then
  230. begin
  231. S:=S+'.';
  232. Inc(len);
  233. end;
  234. If Len>Size then
  235. Len:=Size;
  236. if Len > 0 then
  237. Move(S[1],Buffer^,Len);
  238. Result:=Len;
  239. end;
  240. procedure ShowException(ExceptObject: TObject; ExceptAddr: Pointer);
  241. // use shortstring. On exception, the heap may be corrupt.
  242. Var
  243. Buf : ShortString;
  244. begin
  245. SetLength(Buf,ExceptionErrorMessage(ExceptObject,ExceptAddr,@Buf[1],255));
  246. If IsConsole Then
  247. writeln(Buf)
  248. else
  249. If Assigned(OnShowException) Then
  250. OnShowException (Buf);
  251. end;
  252. procedure Abort;
  253. begin
  254. Raise EAbort.Create(SAbortError) at Get_Caller_addr(Get_Frame)
  255. end;
  256. procedure OutOfMemoryError;
  257. begin
  258. Raise OutOfMemory;
  259. end;
  260. {
  261. $Log$
  262. Revision 1.6 2001-08-19 21:02:02 florian
  263. * fixed and added a lot of stuff to get the Jedi DX( headers
  264. compiled
  265. Revision 1.5 2001/08/12 22:11:48 peter
  266. * freeandnil added
  267. Revision 1.4 2001/06/03 15:18:01 peter
  268. * eoutofmemory and einvalidpointer fix
  269. Revision 1.3 2000/11/23 11:04:26 sg
  270. * Protected some Move()'s by 'if' clauses so that the Move won't be
  271. executed when the length would be 0. Otherwise, the corresponding
  272. routines might get an RTE when compiled with $R+.
  273. Revision 1.2 2000/08/20 15:46:46 peter
  274. * sysutils.pp moved to target and merged with disk.inc, filutil.inc
  275. }