system.inc 35 KB

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