sysutils.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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. Procedure AssertErrorHandler (Const Msg,FN : ShortString;LineNo:longint; TheAddr : pointer);
  205. Var
  206. S : String;
  207. begin
  208. If Msg='' then
  209. S:=SAssertionFailed
  210. else
  211. S:=Msg;
  212. Raise EAssertionFailed.Createfmt(SAssertError,[S,Fn,LineNo]); // at Pointer(theAddr);
  213. end;
  214. {$ifdef STACKCHECK_WAS_ON}
  215. {$S+}
  216. {$endif}
  217. Procedure InitExceptions;
  218. {
  219. Must install uncaught exception handler (ExceptProc)
  220. and install exceptions for system exceptions or signals.
  221. (e.g: SIGSEGV -> ESegFault or so.)
  222. }
  223. begin
  224. ExceptProc:=@CatchUnhandledException;
  225. // Create objects that may have problems when there is no memory.
  226. OutOfMemory:=EOutOfMemory.Create(SOutOfMemory);
  227. OutOfMemory.AllowFree:=false;
  228. InvalidPointer:=EInvalidPointer.Create(SInvalidPointer);
  229. InvalidPointer.AllowFree:=false;
  230. AssertErrorProc:=@AssertErrorHandler;
  231. ErrorProc:=@RunErrorToExcept;
  232. OnShowException:=Nil;
  233. end;
  234. Procedure DoneExceptions;
  235. begin
  236. OutOfMemory.AllowFree:=true;
  237. OutOfMemory.Free;
  238. InValidPointer.AllowFree:=true;
  239. InValidPointer.Free;
  240. end;
  241. { Exception handling routines }
  242. function ExceptObject: TObject;
  243. begin
  244. {$IFDEF VIRTUALPASCAL}
  245. // vpascal does exceptions more the delphi way...
  246. // this needs to be written from scratch.
  247. {$ELSE}
  248. If RaiseList=Nil then
  249. Result:=Nil
  250. else
  251. Result:=RaiseList^.FObject;
  252. {$ENDIF}
  253. end;
  254. function ExceptAddr: Pointer;
  255. begin
  256. {$IFDEF VIRTUALPASCAL}
  257. // vpascal does exceptions more the delphi way...
  258. // this needs to be written from scratch.
  259. {$ELSE}
  260. If RaiseList=Nil then
  261. Result:=Nil
  262. else
  263. Result:=RaiseList^.Addr;
  264. {$ENDIF}
  265. end;
  266. function ExceptionErrorMessage(ExceptObject: TObject; ExceptAddr: Pointer;
  267. Buffer: PChar; Size: Integer): Integer;
  268. Var
  269. S : AnsiString;
  270. Len : Integer;
  271. begin
  272. S:=Format(SExceptionErrorMessage,[ExceptObject.ClassName,ExceptAddr]);
  273. If ExceptObject is Exception then
  274. S:=Format('%s:'#10'%s',[S,Exception(ExceptObject).Message]);
  275. Len:=Length(S);
  276. If S[Len]<>'.' then
  277. begin
  278. S:=S+'.';
  279. Inc(len);
  280. end;
  281. If Len>Size then
  282. Len:=Size;
  283. if Len > 0 then
  284. Move(S[1],Buffer^,Len);
  285. Result:=Len;
  286. end;
  287. procedure ShowException(ExceptObject: TObject; ExceptAddr: Pointer);
  288. // use shortstring. On exception, the heap may be corrupt.
  289. Var
  290. Buf : ShortString;
  291. begin
  292. SetLength(Buf,ExceptionErrorMessage(ExceptObject,ExceptAddr,@Buf[1],255));
  293. If IsConsole Then
  294. writeln(Buf)
  295. else
  296. If Assigned(OnShowException) Then
  297. OnShowException (Buf);
  298. end;
  299. procedure Abort;
  300. begin
  301. {$ifdef VER1_0}
  302. Raise EAbort.Create(SAbortError) at Longint(Get_Caller_addr(Get_Frame));
  303. {$else VER1_0}
  304. Raise EAbort.Create(SAbortError)
  305. {$IFNDEF VIRTUALPASCAL}
  306. at Pointer(Get_Caller_addr(Get_Frame));
  307. {$ENDIF}
  308. {$endif VER1_0}
  309. end;
  310. procedure OutOfMemoryError;
  311. begin
  312. Raise OutOfMemory;
  313. end;
  314. { ---------------------------------------------------------------------
  315. Initialization/Finalization/exit code
  316. ---------------------------------------------------------------------}
  317. Type
  318. PPRecord = ^TPRecord;
  319. TPRecord = Record
  320. Func : TTerminateProc;
  321. NextFunc : PPRecord;
  322. end;
  323. Const
  324. TPList : PPRecord = Nil;
  325. procedure AddTerminateProc(TermProc: TTerminateProc);
  326. Var
  327. TPR : PPRecord;
  328. begin
  329. New(TPR);
  330. With TPR^ do
  331. begin
  332. NextFunc:=TPList;
  333. Func:=TermProc;
  334. end;
  335. TPList:=TPR;
  336. end;
  337. function CallTerminateProcs: Boolean;
  338. Var
  339. TPR : PPRecord;
  340. begin
  341. Result:=True;
  342. TPR:=TPList;
  343. While Result and (TPR<>Nil) do
  344. begin
  345. Result:=TPR^.Func();
  346. TPR:=TPR^.NextFunc;
  347. end;
  348. end;
  349. {
  350. Revision 1.1 2003/10/06 21:01:06 peter
  351. * moved classes unit to rtl
  352. Revision 1.17 2003/09/06 20:46:07 marco
  353. * 3 small VP fixes from Noah Silva. One (OutOfMemory error) failed.
  354. Revision 1.16 2003/04/06 11:06:39 michael
  355. + Added exception classname to output of unhandled exception for better identification
  356. Revision 1.15 2003/03/18 08:28:23 michael
  357. Patch from peter for Abort routine
  358. Revision 1.14 2003/03/17 15:11:51 armin
  359. + someone AssertErrorHandler, BackTraceFunc and Dump_Stack so that pointer instead of longint is needed
  360. Revision 1.13 2003/01/01 20:58:07 florian
  361. + added invalid instruction exception
  362. Revision 1.12 2002/10/07 19:43:24 florian
  363. + empty prototypes for the AnsiStr* multi byte functions added
  364. Revision 1.11 2002/09/07 16:01:22 peter
  365. * old logs removed and tabs fixed
  366. Revision 1.10 2002/07/16 13:57:39 florian
  367. * raise takes now a void pointer as at and frame address
  368. instead of a longint, fixed
  369. Revision 1.9 2002/01/25 17:42:03 peter
  370. * interface helpers
  371. Revision 1.8 2002/01/25 16:23:03 peter
  372. * merged filesearch() fix
  373. }