sysutils.inc 12 KB

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