2
0

sysutils.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. Function FileSearch (Const Name, DirList : String) : String;
  25. Var
  26. I : longint;
  27. Temp : String;
  28. begin
  29. Result:='';
  30. temp:=Dirlist;
  31. repeat
  32. While (Length(Temp)>0) and (Temp[1]=PathSeparator) do
  33. Delete(Temp,1,1);
  34. I:=pos(PathSep,Temp);
  35. If I<>0 then
  36. begin
  37. Result:=Copy (Temp,1,i-1);
  38. system.Delete(Temp,1,I);
  39. end
  40. else
  41. begin
  42. Result:=Temp;
  43. Temp:='';
  44. end;
  45. If (Length(Result)>0) and (result[length(result)]<>DirectorySeparator) then
  46. Result:=Result+DirectorySeparator;
  47. Result:=Result+name;
  48. If not FileExists(Result) Then
  49. Result:='';
  50. until (length(temp)=0) or (length(result)<>0);
  51. end;
  52. { Read String Handling functions implementation }
  53. {$i sysstr.inc}
  54. { Read date & Time function implementations }
  55. {$i dati.inc}
  56. { Read pchar handling functions implementation }
  57. {$i syspch.inc}
  58. { MCBS functions }
  59. {$i sysansi.inc}
  60. { CPU Specific code }
  61. {$i sysutilp.inc}
  62. procedure FreeAndNil(var obj);
  63. var
  64. temp: tobject;
  65. begin
  66. temp:=tobject(obj);
  67. pointer(obj):=nil;
  68. temp.free;
  69. end;
  70. {$ifdef HASINTF}
  71. { Interfaces support }
  72. {$i intf.inc}
  73. {$endif HASINTF}
  74. constructor Exception.Create(const msg : string);
  75. begin
  76. inherited create;
  77. fmessage:=msg;
  78. end;
  79. constructor Exception.CreateFmt(const msg : string; const args : array of const);
  80. begin
  81. inherited create;
  82. fmessage:=Format(msg,args);
  83. end;
  84. constructor Exception.CreateRes(ResString: PString);
  85. begin
  86. inherited create;
  87. fmessage:=ResString^;
  88. end;
  89. constructor Exception.CreateResFmt(ResString: PString; const Args: array of const);
  90. begin
  91. inherited create;
  92. fmessage:=Format(ResString^,args);
  93. end;
  94. constructor Exception.CreateHelp(const Msg: string; AHelpContext: Integer);
  95. begin
  96. inherited create;
  97. fmessage:=Msg;
  98. fhelpcontext:=AHelpContext;
  99. end;
  100. constructor Exception.CreateFmtHelp(const Msg: string; const Args: array of const;
  101. AHelpContext: Integer);
  102. begin
  103. inherited create;
  104. fmessage:=Format(Msg,args);
  105. fhelpcontext:=AHelpContext;
  106. end;
  107. constructor Exception.CreateResHelp(ResString: PString; AHelpContext: Integer);
  108. begin
  109. inherited create;
  110. fmessage:=ResString^;
  111. fhelpcontext:=AHelpContext;
  112. end;
  113. constructor Exception.CreateResFmtHelp(ResString: PString; const Args: array of const;
  114. AHelpContext: Integer);
  115. begin
  116. inherited create;
  117. fmessage:=Format(ResString^,args);
  118. fhelpcontext:=AHelpContext;
  119. end;
  120. procedure EHeapMemoryError.FreeInstance;
  121. begin
  122. if AllowFree then
  123. inherited FreeInstance;
  124. end;
  125. {$ifopt S+}
  126. {$define STACKCHECK_WAS_ON}
  127. {$S-}
  128. {$endif OPT S }
  129. Procedure CatchUnhandledException (Obj : TObject; Addr,Frame: Pointer);
  130. Var
  131. Message : String;
  132. {$IFDEF VIRTUALPASCAL}
  133. stdout:text absolute output;
  134. {$ENDIF}
  135. begin
  136. Writeln(stdout,'An unhandled exception occurred at 0x',HexStr(Longint(Addr),8),' :');
  137. if Obj is exception then
  138. begin
  139. Message:=Exception(Obj).ClassName+' : '+Exception(Obj).Message;
  140. Writeln(stdout,Message);
  141. end
  142. else
  143. Writeln(stdout,'Exception object ',Obj.ClassName,' is not of class Exception.');
  144. { to get a nice symify }
  145. {$IFNDEF VIRTUALPASCAL}
  146. Writeln(stdout,BackTraceStrFunc(Addr));
  147. Dump_Stack(stdout,frame);
  148. {$ENDIF}
  149. Writeln(stdout,'');
  150. Halt(217);
  151. end;
  152. Var OutOfMemory : EOutOfMemory;
  153. InValidPointer : EInvalidPointer;
  154. Procedure RunErrorToExcept (ErrNo : Longint; Address,Frame : Pointer);
  155. Var E : Exception;
  156. S : String;
  157. begin
  158. Case Errno of
  159. 1,203 : E:=OutOfMemory;
  160. 204 : E:=InvalidPointer;
  161. 2,3,4,5,6,100,101,102,103,105,106 : { I/O errors }
  162. begin
  163. Case Errno of
  164. 2 : S:=SFileNotFound;
  165. 3 : S:=SInvalidFileName;
  166. 4 : S:=STooManyOpenFiles;
  167. 5 : S:=SAccessDenied;
  168. 6 : S:=SInvalidFileHandle;
  169. 15 : S:=SInvalidDrive;
  170. 100 : S:=SEndOfFile;
  171. 101 : S:=SDiskFull;
  172. 102 : S:=SFileNotAssigned;
  173. 103 : S:=SFileNotOpen;
  174. 104 : S:=SFileNotOpenForInput;
  175. 105 : S:=SFileNotOpenForOutput;
  176. 106 : S:=SInvalidInput;
  177. end;
  178. E:=EinOutError.Create (S);
  179. EInoutError(E).ErrorCode:=IOresult; // Clears InOutRes !!
  180. end;
  181. // We don't set abstracterrorhandler, but we do it here.
  182. // Unless the use sets another handler we'll get here anyway...
  183. 200 : E:=EDivByZero.Create(SDivByZero);
  184. 201 : E:=ERangeError.Create(SRangeError);
  185. 205 : E:=EOverflow.Create(SOverflow);
  186. 206 : E:=EOverflow.Create(SUnderflow);
  187. 207 : E:=EInvalidOp.Create(SInvalidOp);
  188. 211 : E:=EAbstractError.Create(SAbstractError);
  189. 215 : E:=EIntOverflow.Create(SIntOverflow);
  190. 216 : E:=EAccessViolation.Create(SAccessViolation);
  191. 217 : E:=EPrivilege.Create(SPrivilege);
  192. 218 : E:=EControlC.Create(SControlC);
  193. 219 : E:=EInvalidCast.Create(SInvalidCast);
  194. 220 : E:=EVariantError.Create(SInvalidVarCast);
  195. 221 : E:=EVariantError.Create(SInvalidVarOp);
  196. 222 : E:=EVariantError.Create(SDispatchError);
  197. 223 : E:=EVariantError.Create(SVarArrayCreate);
  198. 224 : E:=EVariantError.Create(SVarNotArray);
  199. 225 : E:=EVariantError.Create(SVarArrayBounds);
  200. 227 : E:=EAssertionFailed.Create(SAssertionFailed);
  201. 228 : E:=EExternalException.Create(SExternalException);
  202. 229 : E:=EIntfCastError.Create(SIntfCastError);
  203. 230 : E:=ESafecallException.Create(SSafecallException);
  204. else
  205. E:=Exception.CreateFmt (SUnKnownRunTimeError,[Errno]);
  206. end;
  207. {$ifdef VER1_0}
  208. Raise E at longint(Address){$ifdef ENHANCEDRAISE},longint(Frame){$endif};
  209. {$else VER1_0}
  210. Raise E at Address{$ifdef ENHANCEDRAISE},Frame){$endif};
  211. {$endif VER1_0}
  212. end;
  213. Procedure AssertErrorHandler (Const Msg,FN : ShortString;LineNo:longint; TheAddr : pointer);
  214. Var
  215. S : String;
  216. begin
  217. If Msg='' then
  218. S:=SAssertionFailed
  219. else
  220. S:=Msg;
  221. Raise EAssertionFailed.Createfmt(SAssertError,[S,Fn,LineNo]); // at Pointer(theAddr);
  222. end;
  223. {$ifdef STACKCHECK_WAS_ON}
  224. {$S+}
  225. {$endif}
  226. Procedure InitExceptions;
  227. {
  228. Must install uncaught exception handler (ExceptProc)
  229. and install exceptions for system exceptions or signals.
  230. (e.g: SIGSEGV -> ESegFault or so.)
  231. }
  232. begin
  233. ExceptProc:=@CatchUnhandledException;
  234. // Create objects that may have problems when there is no memory.
  235. OutOfMemory:=EOutOfMemory.Create(SOutOfMemory);
  236. OutOfMemory.AllowFree:=false;
  237. InvalidPointer:=EInvalidPointer.Create(SInvalidPointer);
  238. InvalidPointer.AllowFree:=false;
  239. AssertErrorProc:=@AssertErrorHandler;
  240. ErrorProc:=@RunErrorToExcept;
  241. OnShowException:=Nil;
  242. end;
  243. Procedure DoneExceptions;
  244. begin
  245. OutOfMemory.AllowFree:=true;
  246. OutOfMemory.Free;
  247. InValidPointer.AllowFree:=true;
  248. InValidPointer.Free;
  249. end;
  250. { Exception handling routines }
  251. function ExceptObject: TObject;
  252. begin
  253. {$IFDEF VIRTUALPASCAL}
  254. // vpascal does exceptions more the delphi way...
  255. // this needs to be written from scratch.
  256. {$ELSE}
  257. If RaiseList=Nil then
  258. Result:=Nil
  259. else
  260. Result:=RaiseList^.FObject;
  261. {$ENDIF}
  262. end;
  263. function ExceptAddr: Pointer;
  264. begin
  265. {$IFDEF VIRTUALPASCAL}
  266. // vpascal does exceptions more the delphi way...
  267. // this needs to be written from scratch.
  268. {$ELSE}
  269. If RaiseList=Nil then
  270. Result:=Nil
  271. else
  272. Result:=RaiseList^.Addr;
  273. {$ENDIF}
  274. end;
  275. function ExceptionErrorMessage(ExceptObject: TObject; ExceptAddr: Pointer;
  276. Buffer: PChar; Size: Integer): Integer;
  277. Var
  278. S : AnsiString;
  279. Len : Integer;
  280. begin
  281. S:=Format(SExceptionErrorMessage,[ExceptObject.ClassName,ExceptAddr]);
  282. If ExceptObject is Exception then
  283. S:=Format('%s:'#10'%s',[S,Exception(ExceptObject).Message]);
  284. Len:=Length(S);
  285. If S[Len]<>'.' then
  286. begin
  287. S:=S+'.';
  288. Inc(len);
  289. end;
  290. If Len>Size then
  291. Len:=Size;
  292. if Len > 0 then
  293. Move(S[1],Buffer^,Len);
  294. Result:=Len;
  295. end;
  296. procedure ShowException(ExceptObject: TObject; ExceptAddr: Pointer);
  297. // use shortstring. On exception, the heap may be corrupt.
  298. Var
  299. Buf : ShortString;
  300. begin
  301. SetLength(Buf,ExceptionErrorMessage(ExceptObject,ExceptAddr,@Buf[1],255));
  302. If IsConsole Then
  303. writeln(Buf)
  304. else
  305. If Assigned(OnShowException) Then
  306. OnShowException (Buf);
  307. end;
  308. procedure Abort;
  309. begin
  310. {$ifdef VER1_0}
  311. Raise EAbort.Create(SAbortError) at Longint(Get_Caller_addr(Get_Frame));
  312. {$else VER1_0}
  313. Raise EAbort.Create(SAbortError)
  314. {$IFNDEF VIRTUALPASCAL}
  315. at Pointer(Get_Caller_addr(Get_Frame));
  316. {$ENDIF}
  317. {$endif VER1_0}
  318. end;
  319. procedure OutOfMemoryError;
  320. begin
  321. Raise OutOfMemory;
  322. end;
  323. {
  324. $Log$
  325. Revision 1.1 2003-10-06 21:01:06 peter
  326. * moved classes unit to rtl
  327. Revision 1.17 2003/09/06 20:46:07 marco
  328. * 3 small VP fixes from Noah Silva. One (OutOfMemory error) failed.
  329. Revision 1.16 2003/04/06 11:06:39 michael
  330. + Added exception classname to output of unhandled exception for better identification
  331. Revision 1.15 2003/03/18 08:28:23 michael
  332. Patch from peter for Abort routine
  333. Revision 1.14 2003/03/17 15:11:51 armin
  334. + someone AssertErrorHandler, BackTraceFunc and Dump_Stack so that pointer instead of longint is needed
  335. Revision 1.13 2003/01/01 20:58:07 florian
  336. + added invalid instruction exception
  337. Revision 1.12 2002/10/07 19:43:24 florian
  338. + empty prototypes for the AnsiStr* multi byte functions added
  339. Revision 1.11 2002/09/07 16:01:22 peter
  340. * old logs removed and tabs fixed
  341. Revision 1.10 2002/07/16 13:57:39 florian
  342. * raise takes now a void pointer as at and frame address
  343. instead of a longint, fixed
  344. Revision 1.9 2002/01/25 17:42:03 peter
  345. * interface helpers
  346. Revision 1.8 2002/01/25 16:23:03 peter
  347. * merged filesearch() fix
  348. }