system.inc 35 KB

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