system.inc 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301
  1. {
  2. This file is part of the Free Pascal Run time library.
  3. Copyright (c) 1999-2000 by the Free Pascal development team
  4. See the file COPYING.FPC, included in this distribution,
  5. For details about the copyright.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. **********************************************************************}
  10. {****************************************************************************
  11. Local types
  12. ****************************************************************************}
  13. {
  14. TextRec and FileRec are put in a separate file to make it available to other
  15. units without putting it explicitly in systemh.
  16. This way we keep TP compatibility, and the TextRec definition is available
  17. for everyone who needs it.
  18. }
  19. {$i filerec.inc}
  20. {$i textrec.inc}
  21. {$ifdef FPC_OBJFPC_EXTENDED_IF}
  22. {$if High(errorcode)<>maxExitCode}
  23. {$define FPC_LIMITED_EXITCODE}
  24. {$endif}
  25. {$else}
  26. {$define FPC_LIMITED_EXITCODE}
  27. {$endif FPC_OBJFPC_EXTENDED_IF}
  28. Procedure HandleError (Errno : Longint); forward;
  29. Procedure HandleErrorFrame (Errno : longint;frame : Pointer); forward;
  30. type
  31. FileFunc = Procedure(var t : TextRec);
  32. const
  33. STACK_MARGIN = 16384; { Stack size margin for stack checking }
  34. { Random / Randomize constants }
  35. OldRandSeed : Cardinal = 0;
  36. { For Error Handling.}
  37. ErrorBase : Pointer = nil;
  38. { Used by the ansi/widestrings and maybe also other things in the future }
  39. var
  40. { widechar, because also used by widestring -> pwidechar conversions }
  41. emptychar : widechar;public name 'FPC_EMPTYCHAR';
  42. {$ifndef FPC_NO_GENERIC_STACK_CHECK}
  43. { if the OS does the stack checking, we don't need any stklen from the
  44. main program }
  45. initialstklen : SizeUint;external name '__stklen';
  46. {$endif FPC_NO_GENERIC_STACK_CHECK}
  47. { checks whether the given suggested size for the stack of the current
  48. thread is acceptable. If this is the case, returns it unaltered.
  49. Otherwise it should return an acceptable value.
  50. Operating systems that automatically expand their stack on demand, should
  51. simply return a very large value.
  52. Operating systems which do not have a possibility to retrieve stack size
  53. information, should simply return the given stklen value (This is the default
  54. implementation).
  55. }
  56. {$ifdef FPC_HAS_FEATURE_STACKCHECK}
  57. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt; forward;
  58. {$endif FPC_HAS_FEATURE_STACKCHECK}
  59. {*****************************************************************************
  60. OS dependent Helpers/Syscalls
  61. *****************************************************************************}
  62. { for some OSes do_isdevice is defined in sysos.inc, but for others (win32)
  63. it isn't, and is used before the actual definition is encountered }
  64. {$ifdef FPC_HAS_FEATURE_FILEIO}
  65. function do_isdevice(handle:thandle):boolean;forward;
  66. {$endif FPC_HAS_FEATURE_FILEIO}
  67. {$i sysos.inc}
  68. {****************************************************************************
  69. Include processor specific routines
  70. ****************************************************************************}
  71. {$ifdef FPC_USE_LIBC}
  72. { prefer libc implementations over our own, as they're most likely faster }
  73. {$i cgeneric.inc}
  74. { is now declared as external reference to another routine in the interface }
  75. {$i cgenstr.inc}
  76. {$endif FPC_USE_LIBC}
  77. {$ifdef cpui386}
  78. {$ifdef SYSPROCDEFINED}
  79. {$Error Can't determine processor type !}
  80. {$endif}
  81. {$i i386.inc} { Case dependent, don't change }
  82. {$endif cpui386}
  83. {$ifdef cpum68k}
  84. {$ifdef SYSPROCDEFINED}
  85. {$Error Can't determine processor type !}
  86. {$endif}
  87. {$i m68k.inc} { Case dependent, don't change }
  88. {$define SYSPROCDEFINED}
  89. {$endif cpum68k}
  90. {$ifdef cpux86_64}
  91. {$ifdef SYSPROCDEFINED}
  92. {$Error Can't determine processor type !}
  93. {$endif}
  94. {$i x86_64.inc} { Case dependent, don't change }
  95. {$define SYSPROCDEFINED}
  96. {$endif cpux86_64}
  97. {$ifdef cpupowerpc32}
  98. {$ifdef SYSPROCDEFINED}
  99. {$Error Can't determine processor type !}
  100. {$endif}
  101. {$i powerpc.inc} { Case dependent, don't change }
  102. {$define SYSPROCDEFINED}
  103. {$endif cpupowerpc32}
  104. {$ifdef cpupowerpc64}
  105. {$ifdef SYSPROCDEFINED}
  106. {$Error Can't determine processor type !}
  107. {$endif}
  108. {$i powerpc64.inc} { Case dependent, don't change }
  109. {$define SYSPROCDEFINED}
  110. {$endif cpupowerpc64}
  111. {$ifdef cpualpha}
  112. {$ifdef SYSPROCDEFINED}
  113. {$Error Can't determine processor type !}
  114. {$endif}
  115. {$i alpha.inc} { Case dependent, don't change }
  116. {$define SYSPROCDEFINED}
  117. {$endif cpualpha}
  118. {$ifdef cpuiA64}
  119. {$ifdef SYSPROCDEFINED}
  120. {$Error Can't determine processor type !}
  121. {$endif}
  122. {$i ia64.inc} { Case dependent, don't change }
  123. {$define SYSPROCDEFINED}
  124. {$endif cpuiA64}
  125. {$ifdef cpusparc}
  126. {$ifdef SYSPROCDEFINED}
  127. {$Error Can't determine processor type !}
  128. {$endif}
  129. {$i sparc.inc} { Case dependent, don't change }
  130. {$define SYSPROCDEFINED}
  131. {$endif cpusparc}
  132. {$ifdef cpuarm}
  133. {$ifdef SYSPROCDEFINED}
  134. {$Error Can't determine processor type !}
  135. {$endif}
  136. {$i arm.inc} { Case dependent, don't change }
  137. {$define SYSPROCDEFINED}
  138. {$endif cpuarm}
  139. procedure fillchar(var x;count : SizeInt;value : boolean);{$ifdef SYSTEMINLINE}inline;{$endif}
  140. begin
  141. fillchar(x,count,byte(value));
  142. end;
  143. procedure fillchar(var x;count : SizeInt;value : char);{$ifdef SYSTEMINLINE}inline;{$endif}
  144. begin
  145. fillchar(x,count,byte(value));
  146. end;
  147. procedure FillByte (var x;count : SizeInt;value : byte ); {$ifdef SYSTEMINLINE}inline;{$endif}
  148. begin
  149. FillChar (X,Count,VALUE);
  150. end;
  151. function IndexChar(Const buf;len:SizeInt;b:char):SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  152. begin
  153. IndexChar:=IndexByte(Buf,Len,byte(B));
  154. end;
  155. function CompareChar(Const buf1,buf2;len:SizeInt):SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  156. begin
  157. CompareChar:=CompareByte(buf1,buf2,len);
  158. end;
  159. { Include generic pascal only routines which are not defined in the processor
  160. specific include file }
  161. {$I generic.inc}
  162. {****************************************************************************
  163. Set Handling
  164. ****************************************************************************}
  165. { Include set support which is processor specific}
  166. {$i set.inc}
  167. { Include generic pascal routines for sets if the processor }
  168. { specific routines are not available. }
  169. {$i genset.inc}
  170. {****************************************************************************
  171. Math Routines
  172. ****************************************************************************}
  173. function Hi(b : byte): byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  174. begin
  175. Hi := b shr 4
  176. end;
  177. function Lo(b : byte): byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  178. begin
  179. Lo := b and $0f
  180. end;
  181. Function swap (X : Word) : Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  182. Begin
  183. swap:=(X and $ff) shl 8 + (X shr 8)
  184. End;
  185. Function Swap (X : Integer) : Integer;{$ifdef SYSTEMINLINE}inline;{$endif}
  186. Begin
  187. { the extra 'and $ff' in the right term is necessary because the }
  188. { 'X shr 8' is turned into "longint(X) shr 8", so if x < 0 then }
  189. { the sign bits from the upper 16 bits are shifted in rather than }
  190. { zeroes. Another bug for TP/Delphi compatibility... }
  191. swap:=(X and $ff) shl 8 + ((X shr 8) and $ff)
  192. End;
  193. Function swap (X : Longint) : Longint;{$ifdef SYSTEMINLINE}inline;{$endif}
  194. Begin
  195. Swap:=(X and $ffff) shl 16 + (X shr 16)
  196. End;
  197. Function Swap (X : Cardinal) : Cardinal;{$ifdef SYSTEMINLINE}inline;{$endif}
  198. Begin
  199. Swap:=(X and $ffff) shl 16 + (X shr 16)
  200. End;
  201. Function Swap (X : QWord) : QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  202. Begin
  203. Swap:=(X and $ffffffff) shl 32 + (X shr 32);
  204. End;
  205. Function swap (X : Int64) : Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  206. Begin
  207. Swap:=(X and $ffffffff) shl 32 + (X shr 32);
  208. End;
  209. {$ifdef SUPPORT_DOUBLE}
  210. operator := (b:real48) d:double;{$ifdef SYSTEMINLINE}inline;{$endif}
  211. begin
  212. D:=real2double(b);
  213. end;
  214. {$endif SUPPORT_DOUBLE}
  215. {$ifdef SUPPORT_EXTENDED}
  216. operator := (b:real48) e:extended;{$ifdef SYSTEMINLINE}inline;{$endif}
  217. begin
  218. e:=real2double(b);
  219. end;
  220. {$endif SUPPORT_EXTENDED}
  221. {$ifdef FPC_USE_LIBC}
  222. { Include libc versions }
  223. {$i cgenmath.inc}
  224. {$endif FPC_USE_LIBC}
  225. { Include processor specific routines }
  226. {$I math.inc}
  227. { Include generic version }
  228. {$I genmath.inc}
  229. function aligntoptr(p : pointer) : pointer;inline;
  230. begin
  231. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  232. result:=align(p,sizeof(p));
  233. {$else FPC_REQUIRES_PROPER_ALIGNMENT}
  234. result:=p;
  235. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  236. end;
  237. {****************************************************************************
  238. Subroutines for String handling
  239. ****************************************************************************}
  240. { Needs to be before RTTI handling }
  241. {$i sstrings.inc}
  242. { requires sstrings.inc for initval }
  243. {$I int64p.inc}
  244. {$I int64.inc}
  245. {Requires int64.inc, since that contains the VAL functions for int64 and qword}
  246. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  247. {$i astrings.inc}
  248. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  249. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  250. {$i wstrings.inc}
  251. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  252. {$i aliases.inc}
  253. {*****************************************************************************
  254. Dynamic Array support
  255. *****************************************************************************}
  256. {$ifdef FPC_HAS_FEATURE_DYNARRAYS}
  257. {$i dynarr.inc}
  258. {$endif FPC_HAS_FEATURE_DYNARRAYS}
  259. {*****************************************************************************
  260. Object Pascal support
  261. *****************************************************************************}
  262. {$ifdef FPC_HAS_FEATURE_CLASSES}
  263. {$i objpas.inc}
  264. {$endif FPC_HAS_FEATURE_CLASSES}
  265. {*****************************************************************************
  266. Variant support
  267. *****************************************************************************}
  268. {$ifdef FPC_HAS_FEATURE_VARIANTS}
  269. {$i variant.inc}
  270. {$endif FPC_HAS_FEATURE_VARIANTS}
  271. {****************************************************************************
  272. Run-Time Type Information (RTTI)
  273. ****************************************************************************}
  274. {$ifdef FPC_HAS_FEATURE_RTTI}
  275. {$i rtti.inc}
  276. {$endif FPC_HAS_FEATURE_VARIANTS}
  277. {----------------------------------------------------------------------
  278. Mersenne Twister: A 623-Dimensionally Equidistributed Uniform
  279. Pseudo-Random Number Generator.
  280. What is Mersenne Twister?
  281. Mersenne Twister(MT) is a pseudorandom number generator developped by
  282. Makoto Matsumoto and Takuji Nishimura (alphabetical order) during
  283. 1996-1997. MT has the following merits:
  284. It is designed with consideration on the flaws of various existing
  285. generators.
  286. Far longer period and far higher order of equidistribution than any
  287. other implemented generators. (It is proved that the period is 2^19937-1,
  288. and 623-dimensional equidistribution property is assured.)
  289. Fast generation. (Although it depends on the system, it is reported that
  290. MT is sometimes faster than the standard ANSI-C library in a system
  291. with pipeline and cache memory.)
  292. Efficient use of the memory. (The implemented C-code mt19937.c
  293. consumes only 624 words of working area.)
  294. home page
  295. http://www.math.keio.ac.jp/~matumoto/emt.html
  296. original c source
  297. http://www.math.keio.ac.jp/~nisimura/random/int/mt19937int.c
  298. Coded by Takuji Nishimura, considering the suggestions by
  299. Topher Cooper and Marc Rieffel in July-Aug. 1997.
  300. This library is free software; you can redistribute it and/or
  301. modify it under the terms of the GNU Library General Public
  302. License as published by the Free Software Foundation; either
  303. version 2 of the License, or (at your option) any later
  304. version.
  305. This library is distributed in the hope that it will be useful,
  306. but WITHOUT ANY WARRANTY; without even the implied warranty of
  307. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  308. See the GNU Library General Public License for more details.
  309. You should have received a copy of the GNU Library General
  310. Public License along with this library; if not, write to the
  311. Free Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  312. 02111-1307 USA
  313. Copyright (C) 1997, 1999 Makoto Matsumoto and Takuji Nishimura.
  314. When you use this, send an email to: [email protected]
  315. with an appropriate reference to your work.
  316. REFERENCE
  317. M. Matsumoto and T. Nishimura,
  318. "Mersenne Twister: A 623-Dimensionally Equidistributed Uniform
  319. Pseudo-Random Number Generator",
  320. ACM Transactions on Modeling and Computer Simulation,
  321. Vol. 8, No. 1, January 1998, pp 3--30.
  322. Translated to OP and Delphi interface added by Roman Krejci (6.12.1999)
  323. http://www.rksolution.cz/delphi/tips.htm
  324. Revised 21.6.2000: Bug in the function RandInt_MT19937 fixed
  325. 2003/10/26: adapted to use the improved intialisation mentioned at
  326. <http://www.math.keio.ac.jp/~matumoto/MT2002/emt19937ar.html> and
  327. removed the assembler code
  328. ----------------------------------------------------------------------}
  329. {$R-} {range checking off}
  330. {$Q-} {overflow checking off}
  331. { Period parameter }
  332. Const
  333. MT19937N=624;
  334. Type
  335. tMT19937StateArray = array [0..MT19937N-1] of longint; // the array for the state vector
  336. { Period parameters }
  337. const
  338. MT19937M=397;
  339. MT19937MATRIX_A =$9908b0df; // constant vector a
  340. MT19937UPPER_MASK=$80000000; // most significant w-r bits
  341. MT19937LOWER_MASK=$7fffffff; // least significant r bits
  342. { Tempering parameters }
  343. TEMPERING_MASK_B=$9d2c5680;
  344. TEMPERING_MASK_C=$efc60000;
  345. VAR
  346. mt : tMT19937StateArray;
  347. const
  348. mti: longint=MT19937N+1; // mti=MT19937N+1 means mt[] is not initialized
  349. { Initializing the array with a seed }
  350. procedure sgenrand_MT19937(seed: longint);
  351. var
  352. i: longint;
  353. begin
  354. mt[0] := seed;
  355. for i := 1 to MT19937N-1 do
  356. begin
  357. mt[i] := 1812433253 * (mt[i-1] xor (mt[i-1] shr 30)) + i;
  358. { See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. }
  359. { In the previous versions, MSBs of the seed affect }
  360. { only MSBs of the array mt[]. }
  361. { 2002/01/09 modified by Makoto Matsumoto }
  362. end;
  363. mti := MT19937N;
  364. end;
  365. function genrand_MT19937: longint;
  366. const
  367. mag01 : array [0..1] of longint =(0, longint(MT19937MATRIX_A));
  368. var
  369. y: longint;
  370. kk: longint;
  371. begin
  372. if RandSeed<>OldRandSeed then
  373. mti:=MT19937N+1;
  374. if (mti >= MT19937N) { generate MT19937N longints at one time }
  375. then begin
  376. if mti = (MT19937N+1) then // if sgenrand_MT19937() has not been called,
  377. begin
  378. sgenrand_MT19937(randseed); // default initial seed is used
  379. { hack: randseed is not used more than once in this algorithm. Most }
  380. { user changes are re-initialising reandseed with the value it had }
  381. { at the start -> with the "not", we will detect this change. }
  382. { Detecting other changes is not useful, since the generated }
  383. { numbers will be different anyway. }
  384. randseed := not(randseed);
  385. oldrandseed := randseed;
  386. end;
  387. for kk:=0 to MT19937N-MT19937M-1 do begin
  388. y := (mt[kk] and MT19937UPPER_MASK) or (mt[kk+1] and MT19937LOWER_MASK);
  389. mt[kk] := mt[kk+MT19937M] xor (y shr 1) xor mag01[y and $00000001];
  390. end;
  391. for kk:= MT19937N-MT19937M to MT19937N-2 do begin
  392. y := (mt[kk] and MT19937UPPER_MASK) or (mt[kk+1] and MT19937LOWER_MASK);
  393. mt[kk] := mt[kk+(MT19937M-MT19937N)] xor (y shr 1) xor mag01[y and $00000001];
  394. end;
  395. y := (mt[MT19937N-1] and MT19937UPPER_MASK) or (mt[0] and MT19937LOWER_MASK);
  396. mt[MT19937N-1] := mt[MT19937M-1] xor (y shr 1) xor mag01[y and $00000001];
  397. mti := 0;
  398. end;
  399. y := mt[mti]; inc(mti);
  400. y := y xor (y shr 11);
  401. y := y xor (y shl 7) and TEMPERING_MASK_B;
  402. y := y xor (y shl 15) and TEMPERING_MASK_C;
  403. y := y xor (y shr 18);
  404. Result := y;
  405. end;
  406. function random(l:longint): longint;
  407. begin
  408. { otherwise we can return values = l (JM) }
  409. if (l < 0) then
  410. inc(l);
  411. random := longint((int64(cardinal(genrand_MT19937))*l) shr 32);
  412. end;
  413. function random(l:int64): int64;
  414. begin
  415. { always call random, so the random generator cycles (TP-compatible) (JM) }
  416. random := int64((qword(cardinal(genrand_MT19937)) or ((qword(cardinal(genrand_MT19937)) shl 32))) and $7fffffffffffffff);
  417. if (l<>0) then
  418. random := random mod l
  419. else
  420. random := 0;
  421. end;
  422. function random: extended;
  423. begin
  424. random := cardinal(genrand_MT19937) * (1.0/(int64(1) shl 32));
  425. end;
  426. {****************************************************************************
  427. Memory Management
  428. ****************************************************************************}
  429. Function Ptr(sel,off : Longint) : farpointer;{$ifdef SYSTEMINLINE}inline;{$endif}
  430. Begin
  431. ptr:=farpointer((sel shl 4)+off);
  432. End;
  433. Function CSeg : Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  434. Begin
  435. Cseg:=0;
  436. End;
  437. Function DSeg : Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  438. Begin
  439. Dseg:=0;
  440. End;
  441. Function SSeg : Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  442. Begin
  443. Sseg:=0;
  444. End;
  445. {*****************************************************************************
  446. Directory support.
  447. *****************************************************************************}
  448. {$if defined(FPC_HAS_FEATURE_FILEIO) and defined(FPC_HAS_FEATURE_ANSISTRINGS)}
  449. Procedure getdir(drivenr:byte;Var dir:ansistring);
  450. { this is needed to also allow ansistrings, the shortstring version is
  451. OS dependent }
  452. var
  453. s : shortstring;
  454. begin
  455. getdir(drivenr,s);
  456. dir:=s;
  457. end;
  458. {$endif}
  459. {$ifopt R+}
  460. {$define RangeCheckWasOn}
  461. {$R-}
  462. {$endif opt R+}
  463. {$ifopt I+}
  464. {$define IOCheckWasOn}
  465. {$I-}
  466. {$endif opt I+}
  467. {$ifopt Q+}
  468. {$define OverflowCheckWasOn}
  469. {$Q-}
  470. {$endif opt Q+}
  471. {*****************************************************************************
  472. Miscellaneous
  473. *****************************************************************************}
  474. procedure fpc_rangeerror;[public,alias:'FPC_RANGEERROR']; compilerproc;
  475. begin
  476. HandleErrorFrame(201,get_frame);
  477. end;
  478. procedure fpc_divbyzero;[public,alias:'FPC_DIVBYZERO']; compilerproc;
  479. begin
  480. HandleErrorFrame(200,get_frame);
  481. end;
  482. procedure fpc_overflow;[public,alias:'FPC_OVERFLOW']; compilerproc;
  483. begin
  484. HandleErrorFrame(215,get_frame);
  485. end;
  486. procedure fpc_threaderror; [public,alias:'FPC_THREADERROR'];
  487. begin
  488. HandleErrorFrame(6,get_frame);
  489. end;
  490. procedure fpc_iocheck;[public,alias:'FPC_IOCHECK']; compilerproc;
  491. var
  492. l : longint;
  493. HInoutRes : PWord;
  494. begin
  495. HInOutRes:=@InoutRes;
  496. if HInOutRes^<>0 then
  497. begin
  498. l:=HInOutRes^;
  499. HInOutRes^:=0;
  500. HandleErrorFrame(l,get_frame);
  501. end;
  502. end;
  503. Function IOResult:Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  504. var
  505. HInoutRes : PWord;
  506. Begin
  507. HInoutRes:=@InoutRes;
  508. IOResult:=HInOutRes^;
  509. HInOutRes^:=0;
  510. End;
  511. Function GetThreadID:TThreadID;{$ifdef SYSTEMINLINE}inline;{$endif}
  512. begin
  513. (* ThreadID is stored in a threadvar and made available in interface *)
  514. (* to allow setup of this value during thread initialization. *)
  515. GetThreadID := ThreadID;
  516. end;
  517. function fpc_safecallcheck(res : hresult) : hresult;[public,alias:'FPC_SAFECALLCHECK']; compilerproc;
  518. begin
  519. if res<0 then
  520. begin
  521. if assigned(SafeCallErrorProc) then
  522. SafeCallErrorProc(res,get_frame);
  523. HandleErrorFrame(229,get_frame);
  524. end;
  525. result:=res;
  526. end;
  527. {*****************************************************************************
  528. Stack check code
  529. *****************************************************************************}
  530. { be compatible with old code }
  531. {$ifdef FPC_NO_GENERIC_STACK_CHECK}
  532. {$define NO_GENERIC_STACK_CHECK}
  533. {$endif FPC_NO_GENERIC_STACK_CHECK}
  534. {$IFNDEF NO_GENERIC_STACK_CHECK}
  535. {$IFOPT S+}
  536. {$DEFINE STACKCHECK}
  537. {$ENDIF}
  538. {$S-}
  539. procedure fpc_stackcheck(stack_size:SizeUInt);[public,alias:'FPC_STACKCHECK'];
  540. var
  541. c : Pointer;
  542. begin
  543. { Avoid recursive calls when called from the exit routines }
  544. if StackError then
  545. exit;
  546. c := Sptr - (stack_size + STACK_MARGIN);
  547. if (c <= StackBottom) then
  548. begin
  549. StackError:=true;
  550. HandleError(202);
  551. end;
  552. end;
  553. {$IFDEF STACKCHECK}
  554. {$S+}
  555. {$ENDIF}
  556. {$UNDEF STACKCHECK}
  557. {$ENDIF NO_GENERIC_STACK_CHECK}
  558. {*****************************************************************************
  559. Initialization / Finalization
  560. *****************************************************************************}
  561. const
  562. maxunits=1024; { See also files.pas of the compiler source }
  563. type
  564. TInitFinalRec=record
  565. InitProc,
  566. FinalProc : TProcedure;
  567. end;
  568. TInitFinalTable = record
  569. TableCount,
  570. InitCount : longint;
  571. Procs : array[1..maxunits] of TInitFinalRec;
  572. end;
  573. PInitFinalTable = ^TInitFinalTable;
  574. {$ifndef FPC_HAS_INDIRECT_MAIN_INFORMATION}
  575. var
  576. InitFinalTable : TInitFinalTable;external name 'INITFINAL';
  577. {$endif FPC_HAS_INDIRECT_MAIN_INFORMATION}
  578. procedure fpc_InitializeUnits;[public,alias:'FPC_INITIALIZEUNITS']; compilerproc;
  579. var
  580. i : longint;
  581. begin
  582. { call cpu/fpu initialisation routine }
  583. fpc_cpuinit;
  584. with {$ifdef FPC_HAS_INDIRECT_MAIN_INFORMATION}PInitFinalTable(EntryInformation.{$endif FPC_HAS_INDIRECT_MAIN_INFORMATION}
  585. InitFinalTable
  586. {$ifdef FPC_HAS_INDIRECT_MAIN_INFORMATION})^{$endif FPC_HAS_INDIRECT_MAIN_INFORMATION} do
  587. begin
  588. for i:=1 to TableCount do
  589. begin
  590. if assigned(Procs[i].InitProc) then
  591. Procs[i].InitProc();
  592. InitCount:=i;
  593. end;
  594. end;
  595. if assigned(InitProc) then
  596. TProcedure(InitProc)();
  597. end;
  598. procedure FinalizeUnits;[public,alias:'FPC_FINALIZEUNITS'];
  599. begin
  600. with {$ifdef FPC_HAS_INDIRECT_MAIN_INFORMATION}PInitFinalTable(EntryInformation.{$endif FPC_HAS_INDIRECT_MAIN_INFORMATION}
  601. InitFinalTable
  602. {$ifdef FPC_HAS_INDIRECT_MAIN_INFORMATION})^{$endif FPC_HAS_INDIRECT_MAIN_INFORMATION} do
  603. begin
  604. while (InitCount>0) do
  605. begin
  606. // we've to decrement the cound before calling the final. code
  607. // else a halt in the final. code leads to a endless loop
  608. dec(InitCount);
  609. if assigned(Procs[InitCount+1].FinalProc) then
  610. Procs[InitCount+1].FinalProc();
  611. end;
  612. end;
  613. end;
  614. {*****************************************************************************
  615. Error / Exit / ExitProc
  616. *****************************************************************************}
  617. Procedure system_exit;forward;
  618. {$ifdef FPC_HAS_FEATURE_HEAP}
  619. {$ifndef HAS_MEMORYMANAGER}
  620. //not needed if independant memory manager
  621. Procedure FinalizeHeap;forward;
  622. {$endif HAS_MEMORYMANAGER}
  623. {$endif FPC_HAS_FEATURE_HEAP}
  624. Procedure InternalExit;
  625. var
  626. current_exit : Procedure;
  627. pstdout : ^Text;
  628. {$if defined(MSWINDOWS) or defined(OS2)}
  629. i : longint;
  630. {$endif}
  631. Begin
  632. {$ifdef SYSTEMDEBUG}
  633. writeln('InternalExit');
  634. {$endif SYSTEMDEBUG}
  635. while exitProc<>nil Do
  636. Begin
  637. InOutRes:=0;
  638. current_exit:=tProcedure(exitProc);
  639. exitProc:=nil;
  640. current_exit();
  641. End;
  642. { Finalize units }
  643. FinalizeUnits;
  644. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  645. { Show runtime error and exit }
  646. pstdout:=@stdout;
  647. If erroraddr<>nil Then
  648. Begin
  649. Writeln(pstdout^,'Runtime error ',Errorcode,' at $',hexstr(erroraddr));
  650. { to get a nice symify }
  651. Writeln(pstdout^,BackTraceStrFunc(Erroraddr));
  652. dump_stack(pstdout^,ErrorBase);
  653. Writeln(pstdout^,'');
  654. End;
  655. { Make sure that all output is written to the redirected file }
  656. Flush(Output);
  657. Flush(ErrOutput);
  658. Flush(pstdout^);
  659. Flush(StdErr);
  660. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  661. {$if defined(MSWINDOWS) or defined(OS2)}
  662. { finally release the heap if possible, especially
  663. important for DLLs }
  664. for i:=0 to argc do
  665. sysfreemem(argv[i]);
  666. sysfreemem(argv);
  667. {$endif}
  668. {$ifdef LINUX}
  669. {sysfreemem already checks for nil}
  670. sysfreemem(calculated_cmdline);
  671. {$endif}
  672. {$ifdef BSD}
  673. sysfreemem(cmdline);
  674. {$endif}
  675. {$ifdef FPC_HAS_FEATURE_HEAP}
  676. {$ifndef HAS_MEMORYMANAGER}
  677. FinalizeHeap;
  678. {$endif HAS_MEMORYMANAGER}
  679. {$endif FPC_HAS_FEATURE_HEAP}
  680. End;
  681. Procedure do_exit;[Public,Alias:'FPC_DO_EXIT'];
  682. begin
  683. InternalExit;
  684. System_exit;
  685. end;
  686. Procedure lib_exit;[Public,Alias:'FPC_LIB_EXIT'];
  687. begin
  688. InternalExit;
  689. end;
  690. Procedure Halt(ErrNum: Byte);
  691. Begin
  692. ExitCode:=Errnum;
  693. Do_Exit;
  694. end;
  695. function SysBackTraceStr (Addr: Pointer): ShortString;
  696. begin
  697. SysBackTraceStr:=' $'+hexstr(addr);
  698. end;
  699. Procedure HandleErrorAddrFrame (Errno : longint;addr,frame : Pointer);[public,alias:'FPC_BREAK_ERROR'];
  700. begin
  701. If pointer(ErrorProc)<>Nil then
  702. ErrorProc(Errno,addr,frame);
  703. errorcode:=word(Errno);
  704. erroraddr:=addr;
  705. errorbase:=frame;
  706. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  707. if ExceptAddrStack <> nil then
  708. raise TObject(nil) at addr,frame;
  709. {$endif FPC_HAS_FEATURE_EXCEPTIONS}
  710. {$ifdef FPC_HAS_FEATURE_EXITCODE}
  711. {$ifdef FPC_LIMITED_EXITCODE}
  712. if errorcode > maxExitCode then
  713. halt(255)
  714. else
  715. {$endif FPC_LIMITED_EXITCODE}
  716. halt(errorcode);
  717. {$else FPC_HAS_FEATURE_EXITCODE}
  718. halt;
  719. {$endif FPC_HAS_FEATURE_EXITCODE}
  720. end;
  721. Procedure HandleErrorFrame (Errno : longint;frame : Pointer);
  722. {
  723. Procedure to handle internal errors, i.e. not user-invoked errors
  724. Internal function should ALWAYS call HandleError instead of RunError.
  725. Can be used for exception handlers to specify the frame
  726. }
  727. begin
  728. HandleErrorAddrFrame(Errno,get_caller_addr(frame),get_caller_frame(frame));
  729. end;
  730. Procedure HandleError (Errno : longint);[public,alias : 'FPC_HANDLEERROR'];
  731. {
  732. Procedure to handle internal errors, i.e. not user-invoked errors
  733. Internal function should ALWAYS call HandleError instead of RunError.
  734. }
  735. begin
  736. HandleErrorFrame(Errno,get_frame);
  737. end;
  738. procedure RunError(w : word);[alias: 'FPC_RUNERROR'];
  739. begin
  740. errorcode:=w;
  741. erroraddr:=get_caller_addr(get_frame);
  742. errorbase:=get_caller_frame(get_frame);
  743. {$ifdef FPC_HAS_FEATURE_EXITCODE}
  744. {$ifdef FPC_LIMITED_EXITCODE}
  745. if errorcode > maxExitCode then
  746. halt(255)
  747. else
  748. {$endif FPC_LIMITED_EXITCODE}
  749. halt(errorcode);
  750. {$else FPC_HAS_FEATURE_EXITCODE}
  751. halt;
  752. {$endif FPC_HAS_FEATURE_EXITCODE}
  753. end;
  754. Procedure RunError;{$ifdef SYSTEMINLINE}inline;{$endif}
  755. Begin
  756. RunError (0);
  757. End;
  758. Procedure Halt;{$ifdef SYSTEMINLINE}inline;{$endif}
  759. Begin
  760. Halt(0);
  761. End;
  762. Procedure Error(RunTimeError : TRunTimeError);
  763. begin
  764. RunError(RuntimeErrorExitCodes[RunTimeError]);
  765. end;
  766. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  767. Procedure dump_stack(var f : text;bp : Pointer);
  768. var
  769. i : Longint;
  770. prevbp : Pointer;
  771. is_dev : boolean;
  772. caller_frame,
  773. caller_addr : Pointer;
  774. Begin
  775. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  776. try
  777. {$endif FPC_HAS_FEATURE_EXCEPTIONS}
  778. prevbp:=bp-1;
  779. i:=0;
  780. is_dev:=do_isdevice(textrec(f).Handle);
  781. while bp > prevbp Do
  782. Begin
  783. caller_addr := get_caller_addr(bp);
  784. caller_frame := get_caller_frame(bp);
  785. if (caller_addr=nil) then
  786. break;
  787. Writeln(f,BackTraceStrFunc(caller_addr));
  788. if (caller_frame=nil) then
  789. break;
  790. Inc(i);
  791. If ((i>max_frame_dump) and is_dev) or (i>256) Then
  792. break;
  793. prevbp:=bp;
  794. bp:=caller_frame;
  795. End;
  796. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  797. except
  798. { prevent endless dump if an exception occured }
  799. end;
  800. {$endif FPC_HAS_FEATURE_EXCEPTIONS}
  801. End;
  802. {$ifdef FPC_HAS_FEATURE_EXCEPTIONS}
  803. procedure DumpExceptionBackTrace(var f:text);
  804. var
  805. FrameNumber,
  806. FrameCount : longint;
  807. Frames : PPointer;
  808. begin
  809. if RaiseList=nil then
  810. exit;
  811. WriteLn(f,BackTraceStrFunc(RaiseList^.Addr));
  812. FrameCount:=RaiseList^.Framecount;
  813. Frames:=RaiseList^.Frames;
  814. for FrameNumber := 0 to FrameCount-1 do
  815. WriteLn(f,BackTraceStrFunc(Frames[FrameNumber]));
  816. end;
  817. {$endif FPC_HAS_FEATURE_EXCEPTIONS}
  818. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  819. {$ifdef FPC_HAS_FEATURE_HEAP}
  820. Type
  821. PExitProcInfo = ^TExitProcInfo;
  822. TExitProcInfo = Record
  823. Next : PExitProcInfo;
  824. SaveExit : Pointer;
  825. Proc : TProcedure;
  826. End;
  827. const
  828. ExitProcList: PExitProcInfo = nil;
  829. Procedure DoExitProc;
  830. var
  831. P : PExitProcInfo;
  832. Proc : TProcedure;
  833. Begin
  834. P:=ExitProcList;
  835. ExitProcList:=P^.Next;
  836. ExitProc:=P^.SaveExit;
  837. Proc:=P^.Proc;
  838. DisPose(P);
  839. Proc();
  840. End;
  841. Procedure AddExitProc(Proc: TProcedure);
  842. var
  843. P : PExitProcInfo;
  844. Begin
  845. New(P);
  846. P^.Next:=ExitProcList;
  847. P^.SaveExit:=ExitProc;
  848. P^.Proc:=Proc;
  849. ExitProcList:=P;
  850. ExitProc:=@DoExitProc;
  851. End;
  852. {$endif FPC_HAS_FEATURE_HEAP}
  853. {$ifdef FPC_HAS_FEATURE_HEAP}
  854. function ArrayStringToPPchar(const S:Array of AnsiString;reserveentries:Longint):ppchar; // const ?
  855. // Extra allocate reserveentries pchar's at the beginning (default param=0 after 1.0.x ?)
  856. // Note: for internal use by skilled programmers only
  857. // if "s" goes out of scope in the parent procedure, the pointer is dangling.
  858. var p : ppchar;
  859. i : LongInt;
  860. begin
  861. if High(s)<Low(s) Then Exit(NIL);
  862. Getmem(p,sizeof(pchar)*(high(s)-low(s)+ReserveEntries+2)); // one more for NIL, one more
  863. // for cmd
  864. if p=nil then
  865. begin
  866. {$ifdef xunix}
  867. fpseterrno(ESysEnomem);
  868. {$endif}
  869. exit(NIL);
  870. end;
  871. for i:=low(s) to high(s) do
  872. p[i+Reserveentries]:=pchar(s[i]);
  873. p[high(s)+1+Reserveentries]:=nil;
  874. ArrayStringToPPchar:=p;
  875. end;
  876. Function StringToPPChar(Var S:AnsiString;ReserveEntries:integer):ppchar;
  877. {
  878. Create a PPChar to structure of pchars which are the arguments specified
  879. in the string S. Especially usefull for creating an ArgV for Exec-calls
  880. }
  881. begin
  882. StringToPPChar:=StringToPPChar(PChar(S),ReserveEntries);
  883. end;
  884. Function StringToPPChar(S: PChar;ReserveEntries:integer):ppchar;
  885. var
  886. i,nr : longint;
  887. Buf : ^char;
  888. p : ppchar;
  889. begin
  890. buf:=s;
  891. nr:=1;
  892. while (buf^<>#0) do // count nr of args
  893. begin
  894. while (buf^ in [' ',#9,#10]) do // Kill separators.
  895. inc(buf);
  896. inc(nr);
  897. if buf^='"' Then // quotes argument?
  898. begin
  899. inc(buf);
  900. while not (buf^ in [#0,'"']) do // then end of argument is end of string or next quote
  901. inc(buf);
  902. if buf^='"' then // skip closing quote.
  903. inc(buf);
  904. end
  905. else
  906. begin // else std
  907. while not (buf^ in [' ',#0,#9,#10]) do
  908. inc(buf);
  909. end;
  910. end;
  911. getmem(p,(ReserveEntries+nr)*sizeof(pchar));
  912. StringToPPChar:=p;
  913. if p=nil then
  914. begin
  915. {$ifdef xunix}
  916. fpseterrno(ESysEnomem);
  917. {$endif}
  918. exit;
  919. end;
  920. for i:=1 to ReserveEntries do inc(p); // skip empty slots
  921. buf:=s;
  922. while (buf^<>#0) do
  923. begin
  924. while (buf^ in [' ',#9,#10]) do // Kill separators.
  925. begin
  926. buf^:=#0;
  927. inc(buf);
  928. end;
  929. if buf^='"' Then // quotes argument?
  930. begin
  931. inc(buf);
  932. p^:=buf;
  933. inc(p);
  934. p^:=nil;
  935. while not (buf^ in [#0,'"']) do // then end of argument is end of string or next quote
  936. inc(buf);
  937. if buf^='"' then // skip closing quote.
  938. begin
  939. buf^:=#0;
  940. inc(buf);
  941. end;
  942. end
  943. else
  944. begin
  945. p^:=buf;
  946. inc(p);
  947. p^:=nil;
  948. while not (buf^ in [' ',#0,#9,#10]) do
  949. inc(buf);
  950. end;
  951. end;
  952. end;
  953. {$endif FPC_HAS_FEATURE_HEAP}
  954. {*****************************************************************************
  955. Abstract/Assert support.
  956. *****************************************************************************}
  957. procedure fpc_AbstractErrorIntern;compilerproc;[public,alias : 'FPC_ABSTRACTERROR'];
  958. begin
  959. If pointer(AbstractErrorProc)<>nil then
  960. AbstractErrorProc();
  961. HandleErrorFrame(211,get_frame);
  962. end;
  963. Procedure fpc_assert(Const Msg,FName:Shortstring;LineNo:Longint;ErrorAddr:Pointer); [Public,Alias : 'FPC_ASSERT']; compilerproc;
  964. begin
  965. if pointer(AssertErrorProc)<>nil then
  966. AssertErrorProc(Msg,FName,LineNo,ErrorAddr)
  967. else
  968. HandleErrorFrame(227,get_frame);
  969. end;
  970. Procedure SysAssert(Const Msg,FName:Shortstring;LineNo:Longint;ErrorAddr:Pointer);
  971. begin
  972. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  973. If msg='' then
  974. write(stderr,'Assertion failed')
  975. else
  976. write(stderr,msg);
  977. Writeln(stderr,' (',FName,', line ',LineNo,').');
  978. Writeln(stderr,'');
  979. {$ifdef FPC_HAS_FEATURE_EXITCODE}
  980. Halt(227);
  981. {$else FPC_HAS_FEATURE_EXITCODE}
  982. halt;
  983. {$endif FPC_HAS_FEATURE_EXITCODE}
  984. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  985. end;
  986. {*****************************************************************************
  987. SetJmp/LongJmp support.
  988. *****************************************************************************}
  989. {$i setjump.inc}
  990. {$ifdef IOCheckWasOn}
  991. {$I+}
  992. {$endif}
  993. {$ifdef RangeCheckWasOn}
  994. {$R+}
  995. {$endif}
  996. {$ifdef OverflowCheckWasOn}
  997. {$Q+}
  998. {$endif}
  999. {*****************************************************************************
  1000. Heap
  1001. *****************************************************************************}
  1002. {$ifdef FPC_HAS_FEATURE_HEAP}
  1003. {$i sysheap.inc}
  1004. {$i heap.inc}
  1005. {$endif FPC_HAS_FEATURE_HEAP}
  1006. {*****************************************************************************
  1007. Thread support
  1008. *****************************************************************************}
  1009. {$ifdef FPC_HAS_FEATURE_THREADING}
  1010. { Generic threadmanager }
  1011. {$i thread.inc}
  1012. { Generic threadvar support }
  1013. {$i threadvr.inc}
  1014. {$ifdef DISABLE_NO_THREAD_MANAGER}
  1015. { OS Dependent implementation }
  1016. {$i systhrd.inc}
  1017. {$endif DISABLE_NO_THREAD_MANAGER}
  1018. {$endif FPC_HAS_FEATURE_THREADING}
  1019. {*****************************************************************************
  1020. File Handling
  1021. *****************************************************************************}
  1022. { OS dependent low level file functions }
  1023. {$ifdef FPC_HAS_FEATURE_FILEIO}
  1024. {$i sysfile.inc}
  1025. {$endif FPC_HAS_FEATURE_FILEIO}
  1026. { Text file }
  1027. {$ifdef FPC_HAS_FEATURE_TEXTIO}
  1028. {$i text.inc}
  1029. {$endif FPC_HAS_FEATURE_TEXTIO}
  1030. {$ifdef FPC_HAS_FEATURE_FILEIO}
  1031. { Untyped file }
  1032. {$i file.inc}
  1033. { Typed file }
  1034. {$i typefile.inc}
  1035. {$endif FPC_HAS_FEATURE_FILEIO}
  1036. {*****************************************************************************
  1037. Directory Handling
  1038. *****************************************************************************}
  1039. { OS dependent dir functions }
  1040. {$i sysdir.inc}
  1041. {*****************************************************************************
  1042. Resources support
  1043. *****************************************************************************}
  1044. {$ifndef HAS_RESOURCES}
  1045. {$i sysres.inc}
  1046. {$endif}
  1047. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  1048. Function FindResource(ModuleHandle: HMODULE; ResourceName, ResourceType: AnsiString): TResourceHandle;
  1049. begin
  1050. Result:=FindResource(ModuleHandle,PChar(ResourceName),PChar(ResourceType));
  1051. end;
  1052. {$endif}
  1053. const
  1054. CtrlBreakHandler: TCtrlBreakHandler = nil;
  1055. {$IFNDEF FPC_HAS_SETCTRLBREAKHANDLER}
  1056. (* It is possible to provide platform specific implementation performing *)
  1057. (* special initialization; default implementation just sets the procedural *)
  1058. (* variable to make it available for use from the exception handler. *)
  1059. function SysSetCtrlBreakHandler (Handler: TCtrlBreakHandler): TCtrlBreakHandler;
  1060. begin
  1061. (* Return either nil or previous handler *)
  1062. SysSetCtrlBreakHandler := CtrlBreakHandler;
  1063. CtrlBreakHandler := Handler;
  1064. end;
  1065. {$ENDIF FPC_HAS_SETCTRLBREAKHANDLER}