sysutils.inc 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. begin
  133. Writeln(stdout,'An unhandled exception occurred at 0x',HexStr(Longint(Addr),8),' :');
  134. if Obj is exception then
  135. begin
  136. Message:=Exception(Obj).ClassName+' : '+Exception(Obj).Message;
  137. Writeln(stdout,Message);
  138. end
  139. else
  140. Writeln(stdout,'Exception object ',Obj.ClassName,' is not of class Exception.');
  141. { to get a nice symify }
  142. Writeln(stdout,BackTraceStrFunc(Addr));
  143. Dump_Stack(stdout,frame);
  144. Writeln(stdout,'');
  145. Halt(217);
  146. end;
  147. Var OutOfMemory : EOutOfMemory;
  148. InValidPointer : EInvalidPointer;
  149. Procedure RunErrorToExcept (ErrNo : Longint; Address,Frame : Pointer);
  150. Var E : Exception;
  151. S : String;
  152. begin
  153. Case Errno of
  154. 1,203 : E:=OutOfMemory;
  155. 204 : E:=InvalidPointer;
  156. 2,3,4,5,6,100,101,102,103,105,106 : { I/O errors }
  157. begin
  158. Case Errno of
  159. 2 : S:=SFileNotFound;
  160. 3 : S:=SInvalidFileName;
  161. 4 : S:=STooManyOpenFiles;
  162. 5 : S:=SAccessDenied;
  163. 6 : S:=SInvalidFileHandle;
  164. 15 : S:=SInvalidDrive;
  165. 100 : S:=SEndOfFile;
  166. 101 : S:=SDiskFull;
  167. 102 : S:=SFileNotAssigned;
  168. 103 : S:=SFileNotOpen;
  169. 104 : S:=SFileNotOpenForInput;
  170. 105 : S:=SFileNotOpenForOutput;
  171. 106 : S:=SInvalidInput;
  172. end;
  173. E:=EinOutError.Create (S);
  174. EInoutError(E).ErrorCode:=IOresult; // Clears InOutRes !!
  175. end;
  176. // We don't set abstracterrorhandler, but we do it here.
  177. // Unless the use sets another handler we'll get here anyway...
  178. 200 : E:=EDivByZero.Create(SDivByZero);
  179. 201 : E:=ERangeError.Create(SRangeError);
  180. 205 : E:=EOverflow.Create(SOverflow);
  181. 206 : E:=EOverflow.Create(SUnderflow);
  182. 207 : E:=EInvalidOp.Create(SInvalidOp);
  183. 211 : E:=EAbstractError.Create(SAbstractError);
  184. 215 : E:=EIntOverflow.Create(SIntOverflow);
  185. 216 : E:=EAccessViolation.Create(SAccessViolation);
  186. 217 : E:=EPrivilege.Create(SPrivilege);
  187. 218 : E:=EControlC.Create(SControlC);
  188. 219 : E:=EInvalidCast.Create(SInvalidCast);
  189. 220 : E:=EVariantError.Create(SInvalidVarCast);
  190. 221 : E:=EVariantError.Create(SInvalidVarOp);
  191. 222 : E:=EVariantError.Create(SDispatchError);
  192. 223 : E:=EVariantError.Create(SVarArrayCreate);
  193. 224 : E:=EVariantError.Create(SVarNotArray);
  194. 225 : E:=EVariantError.Create(SVarArrayBounds);
  195. 227 : E:=EAssertionFailed.Create(SAssertionFailed);
  196. 228 : E:=EExternalException.Create(SExternalException);
  197. 229 : E:=EIntfCastError.Create(SIntfCastError);
  198. 230 : E:=ESafecallException.Create(SSafecallException);
  199. else
  200. E:=Exception.CreateFmt (SUnKnownRunTimeError,[Errno]);
  201. end;
  202. {$ifdef VER1_0}
  203. Raise E at longint(Address){$ifdef ENHANCEDRAISE},longint(Frame){$endif};
  204. {$else VER1_0}
  205. Raise E at Address{$ifdef ENHANCEDRAISE},Frame){$endif};
  206. {$endif VER1_0}
  207. end;
  208. Procedure AssertErrorHandler (Const Msg,FN : ShortString;LineNo:longint; TheAddr : pointer);
  209. Var
  210. S : String;
  211. begin
  212. If Msg='' then
  213. S:=SAssertionFailed
  214. else
  215. S:=Msg;
  216. Raise EAssertionFailed.Createfmt(SAssertError,[S,Fn,LineNo]); // at Pointer(theAddr);
  217. end;
  218. {$ifdef STACKCHECK_WAS_ON}
  219. {$S+}
  220. {$endif}
  221. Procedure InitExceptions;
  222. {
  223. Must install uncaught exception handler (ExceptProc)
  224. and install exceptions for system exceptions or signals.
  225. (e.g: SIGSEGV -> ESegFault or so.)
  226. }
  227. begin
  228. ExceptProc:=@CatchUnhandledException;
  229. // Create objects that may have problems when there is no memory.
  230. OutOfMemory:=EOutOfMemory.Create(SOutOfMemory);
  231. OutOfMemory.AllowFree:=false;
  232. InvalidPointer:=EInvalidPointer.Create(SInvalidPointer);
  233. InvalidPointer.AllowFree:=false;
  234. AssertErrorProc:=@AssertErrorHandler;
  235. ErrorProc:=@RunErrorToExcept;
  236. OnShowException:=Nil;
  237. end;
  238. Procedure DoneExceptions;
  239. begin
  240. OutOfMemory.AllowFree:=true;
  241. OutOfMemory.Free;
  242. InValidPointer.AllowFree:=true;
  243. InValidPointer.Free;
  244. end;
  245. { Exception handling routines }
  246. function ExceptObject: TObject;
  247. begin
  248. If RaiseList=Nil then
  249. Result:=Nil
  250. else
  251. Result:=RaiseList^.FObject;
  252. end;
  253. function ExceptAddr: Pointer;
  254. begin
  255. If RaiseList=Nil then
  256. Result:=Nil
  257. else
  258. Result:=RaiseList^.Addr;
  259. end;
  260. function ExceptionErrorMessage(ExceptObject: TObject; ExceptAddr: Pointer;
  261. Buffer: PChar; Size: Integer): Integer;
  262. Var
  263. S : AnsiString;
  264. Len : Integer;
  265. begin
  266. S:=Format(SExceptionErrorMessage,[ExceptObject.ClassName,ExceptAddr]);
  267. If ExceptObject is Exception then
  268. S:=Format('%s:'#10'%s',[S,Exception(ExceptObject).Message]);
  269. Len:=Length(S);
  270. If S[Len]<>'.' then
  271. begin
  272. S:=S+'.';
  273. Inc(len);
  274. end;
  275. If Len>Size then
  276. Len:=Size;
  277. if Len > 0 then
  278. Move(S[1],Buffer^,Len);
  279. Result:=Len;
  280. end;
  281. procedure ShowException(ExceptObject: TObject; ExceptAddr: Pointer);
  282. // use shortstring. On exception, the heap may be corrupt.
  283. Var
  284. Buf : ShortString;
  285. begin
  286. SetLength(Buf,ExceptionErrorMessage(ExceptObject,ExceptAddr,@Buf[1],255));
  287. If IsConsole Then
  288. writeln(Buf)
  289. else
  290. If Assigned(OnShowException) Then
  291. OnShowException (Buf);
  292. end;
  293. procedure Abort;
  294. begin
  295. {$ifdef VER1_0}
  296. Raise EAbort.Create(SAbortError) at Longint(Get_Caller_addr(Get_Frame));
  297. {$else VER1_0}
  298. Raise EAbort.Create(SAbortError) at Pointer(Get_Caller_addr(Get_Frame));
  299. {$endif VER1_0}
  300. end;
  301. procedure OutOfMemoryError;
  302. begin
  303. Raise OutOfMemory;
  304. end;
  305. {
  306. $Log$
  307. Revision 1.16 2003-04-06 11:06:39 michael
  308. + Added exception classname to output of unhandled exception for better identification
  309. Revision 1.15 2003/03/18 08:28:23 michael
  310. Patch from peter for Abort routine
  311. Revision 1.14 2003/03/17 15:11:51 armin
  312. + someone AssertErrorHandler, BackTraceFunc and Dump_Stack so that pointer instead of longint is needed
  313. Revision 1.13 2003/01/01 20:58:07 florian
  314. + added invalid instruction exception
  315. Revision 1.12 2002/10/07 19:43:24 florian
  316. + empty prototypes for the AnsiStr* multi byte functions added
  317. Revision 1.11 2002/09/07 16:01:22 peter
  318. * old logs removed and tabs fixed
  319. Revision 1.10 2002/07/16 13:57:39 florian
  320. * raise takes now a void pointer as at and frame address
  321. instead of a longint, fixed
  322. Revision 1.9 2002/01/25 17:42:03 peter
  323. * interface helpers
  324. Revision 1.8 2002/01/25 16:23:03 peter
  325. * merged filesearch() fix
  326. }