sysutils.inc 11 KB

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