system.inc 35 KB

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