system.inc 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341
  1. {
  2. This file is part of the Free Pascal Run time library.
  3. Copyright (c) 1999-2000 by the Free Pascal development team
  4. See the file COPYING.FPC, included in this distribution,
  5. For details about the copyright.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. **********************************************************************}
  10. {****************************************************************************
  11. Local types
  12. ****************************************************************************}
  13. {
  14. TextRec and FileRec are put in a separate file to make it available to other
  15. units without putting it explicitly in systemh.
  16. This way we keep TP compatibility, and the TextRec definition is available
  17. for everyone who needs it.
  18. }
  19. {$i filerec.inc}
  20. {$i textrec.inc}
  21. {$ifdef FPC_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);{$ifdef SYSTEMINLINE}inline;{$endif}
  154. begin
  155. fillchar(x,count,byte(value));
  156. end;
  157. procedure fillchar(var x;count : SizeInt;value : char);{$ifdef SYSTEMINLINE}inline;{$endif}
  158. begin
  159. fillchar(x,count,byte(value));
  160. end;
  161. procedure FillByte (var x;count : SizeInt;value : byte ); {$ifdef SYSTEMINLINE}inline;{$endif}
  162. begin
  163. FillChar (X,Count,VALUE);
  164. end;
  165. function IndexChar(Const buf;len:SizeInt;b:char):SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  166. begin
  167. IndexChar:=IndexByte(Buf,Len,byte(B));
  168. end;
  169. function CompareChar(Const buf1,buf2;len:SizeInt):SizeInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  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:=(X and $ff) shl 8 + (X shr 8)
  198. End;
  199. Function Swap (X : Integer) : Integer;{$ifdef SYSTEMINLINE}inline;{$endif}
  200. Begin
  201. { the extra 'and $ff' in the right term is necessary because the }
  202. { 'X shr 8' is turned into "longint(X) shr 8", so if x < 0 then }
  203. { the sign bits from the upper 16 bits are shifted in rather than }
  204. { zeroes. Another bug for TP/Delphi compatibility... }
  205. swap:=(X and $ff) shl 8 + ((X shr 8) and $ff)
  206. End;
  207. Function swap (X : Longint) : Longint;{$ifdef SYSTEMINLINE}inline;{$endif}
  208. Begin
  209. Swap:=(X and $ffff) shl 16 + (X shr 16)
  210. End;
  211. Function Swap (X : Cardinal) : Cardinal;{$ifdef SYSTEMINLINE}inline;{$endif}
  212. Begin
  213. Swap:=(X and $ffff) shl 16 + (X shr 16)
  214. End;
  215. Function Swap (X : QWord) : QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  216. Begin
  217. Swap:=(X and $ffffffff) shl 32 + (X shr 32);
  218. End;
  219. Function swap (X : Int64) : Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  220. Begin
  221. Swap:=(X and $ffffffff) shl 32 + (X shr 32);
  222. End;
  223. {$ifdef SUPPORT_DOUBLE}
  224. operator := (b:real48) d:double;{$ifdef SYSTEMINLINE}inline;{$endif}
  225. begin
  226. D:=real2double(b);
  227. end;
  228. {$endif SUPPORT_DOUBLE}
  229. {$ifdef SUPPORT_EXTENDED}
  230. operator := (b:real48) e:extended;{$ifdef SYSTEMINLINE}inline;{$endif}
  231. begin
  232. e:=real2double(b);
  233. end;
  234. {$endif SUPPORT_EXTENDED}
  235. {$ifndef FPUNONE}
  236. {$ifdef FPC_USE_LIBC}
  237. { Include libc versions }
  238. {$i cgenmath.inc}
  239. {$endif FPC_USE_LIBC}
  240. { Include processor specific routines }
  241. {$I math.inc}
  242. { Include generic version }
  243. {$I genmath.inc}
  244. {$endif}
  245. {$i gencurr.inc}
  246. function aligntoptr(p : pointer) : pointer;inline;
  247. begin
  248. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  249. result:=align(p,sizeof(p));
  250. {$else FPC_REQUIRES_PROPER_ALIGNMENT}
  251. result:=p;
  252. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  253. end;
  254. {****************************************************************************
  255. Subroutines for String handling
  256. ****************************************************************************}
  257. { Needs to be before RTTI handling }
  258. {$i sstrings.inc}
  259. { requires sstrings.inc for initval }
  260. {$I int64p.inc}
  261. {$I int64.inc}
  262. {Requires int64.inc, since that contains the VAL functions for int64 and qword}
  263. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  264. {$i astrings.inc}
  265. {$endif FPC_HAS_FEATURE_ANSISTRINGS}
  266. {$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
  267. {$i wstrings.inc}
  268. {$endif FPC_HAS_FEATURE_WIDESTRINGS}
  269. {$i aliases.inc}
  270. {*****************************************************************************
  271. Dynamic Array support
  272. *****************************************************************************}
  273. {$ifdef FPC_HAS_FEATURE_DYNARRAYS}
  274. {$i dynarr.inc}
  275. {$endif FPC_HAS_FEATURE_DYNARRAYS}
  276. {*****************************************************************************
  277. Object Pascal support
  278. *****************************************************************************}
  279. {$ifdef FPC_HAS_FEATURE_CLASSES}
  280. {$i objpas.inc}
  281. {$endif FPC_HAS_FEATURE_CLASSES}
  282. {*****************************************************************************
  283. Variant support
  284. *****************************************************************************}
  285. {$ifdef FPC_HAS_FEATURE_VARIANTS}
  286. {$i variant.inc}
  287. {$endif FPC_HAS_FEATURE_VARIANTS}
  288. {****************************************************************************
  289. Run-Time Type Information (RTTI)
  290. ****************************************************************************}
  291. {$ifdef FPC_HAS_FEATURE_RTTI}
  292. {$i rtti.inc}
  293. {$endif FPC_HAS_FEATURE_VARIANTS}
  294. {----------------------------------------------------------------------
  295. Mersenne Twister: A 623-Dimensionally Equidistributed Uniform
  296. Pseudo-Random Number Generator.
  297. What is Mersenne Twister?
  298. Mersenne Twister(MT) is a pseudorandom number generator developped by
  299. Makoto Matsumoto and Takuji Nishimura (alphabetical order) during
  300. 1996-1997. MT has the following merits:
  301. It is designed with consideration on the flaws of various existing
  302. generators.
  303. Far longer period and far higher order of equidistribution than any
  304. other implemented generators. (It is proved that the period is 2^19937-1,
  305. and 623-dimensional equidistribution property is assured.)
  306. Fast generation. (Although it depends on the system, it is reported that
  307. MT is sometimes faster than the standard ANSI-C library in a system
  308. with pipeline and cache memory.)
  309. Efficient use of the memory. (The implemented C-code mt19937.c
  310. consumes only 624 words of working area.)
  311. home page
  312. http://www.math.keio.ac.jp/~matumoto/emt.html
  313. original c source
  314. http://www.math.keio.ac.jp/~nisimura/random/int/mt19937int.c
  315. Coded by Takuji Nishimura, considering the suggestions by
  316. Topher Cooper and Marc Rieffel in July-Aug. 1997.
  317. This library is free software; you can redistribute it and/or
  318. modify it under the terms of the GNU Library General Public
  319. License as published by the Free Software Foundation; either
  320. version 2 of the License, or (at your option) any later
  321. version.
  322. This library is distributed in the hope that it will be useful,
  323. but WITHOUT ANY WARRANTY; without even the implied warranty of
  324. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  325. See the GNU Library General Public License for more details.
  326. You should have received a copy of the GNU Library General
  327. Public License along with this library; if not, write to the
  328. Free Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  329. 02111-1307 USA
  330. Copyright (C) 1997, 1999 Makoto Matsumoto and Takuji Nishimura.
  331. When you use this, send an email to: [email protected]
  332. with an appropriate reference to your work.
  333. REFERENCE
  334. M. Matsumoto and T. Nishimura,
  335. "Mersenne Twister: A 623-Dimensionally Equidistributed Uniform
  336. Pseudo-Random Number Generator",
  337. ACM Transactions on Modeling and Computer Simulation,
  338. Vol. 8, No. 1, January 1998, pp 3--30.
  339. Translated to OP and Delphi interface added by Roman Krejci (6.12.1999)
  340. http://www.rksolution.cz/delphi/tips.htm
  341. Revised 21.6.2000: Bug in the function RandInt_MT19937 fixed
  342. 2003/10/26: adapted to use the improved intialisation mentioned at
  343. <http://www.math.keio.ac.jp/~matumoto/MT2002/emt19937ar.html> and
  344. removed the assembler code
  345. ----------------------------------------------------------------------}
  346. {$R-} {range checking off}
  347. {$Q-} {overflow checking off}
  348. { Period parameter }
  349. Const
  350. MT19937N=624;
  351. Type
  352. tMT19937StateArray = array [0..MT19937N-1] of longint; // the array for the state vector
  353. { Period parameters }
  354. const
  355. MT19937M=397;
  356. MT19937MATRIX_A =$9908b0df; // constant vector a
  357. MT19937UPPER_MASK=$80000000; // most significant w-r bits
  358. MT19937LOWER_MASK=$7fffffff; // least significant r bits
  359. { Tempering parameters }
  360. TEMPERING_MASK_B=$9d2c5680;
  361. TEMPERING_MASK_C=$efc60000;
  362. VAR
  363. mt : tMT19937StateArray;
  364. const
  365. mti: longint=MT19937N+1; // mti=MT19937N+1 means mt[] is not initialized
  366. { Initializing the array with a seed }
  367. procedure sgenrand_MT19937(seed: longint);
  368. var
  369. i: longint;
  370. begin
  371. mt[0] := seed;
  372. for i := 1 to MT19937N-1 do
  373. begin
  374. mt[i] := 1812433253 * (mt[i-1] xor (mt[i-1] shr 30)) + i;
  375. { See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. }
  376. { In the previous versions, MSBs of the seed affect }
  377. { only MSBs of the array mt[]. }
  378. { 2002/01/09 modified by Makoto Matsumoto }
  379. end;
  380. mti := MT19937N;
  381. end;
  382. function genrand_MT19937: longint;
  383. const
  384. mag01 : array [0..1] of longint =(0, longint(MT19937MATRIX_A));
  385. var
  386. y: longint;
  387. kk: longint;
  388. begin
  389. if RandSeed<>OldRandSeed then
  390. mti:=MT19937N+1;
  391. if (mti >= MT19937N) { generate MT19937N longints at one time }
  392. then begin
  393. if mti = (MT19937N+1) then // if sgenrand_MT19937() has not been called,
  394. begin
  395. sgenrand_MT19937(randseed); // default initial seed is used
  396. { hack: randseed is not used more than once in this algorithm. Most }
  397. { user changes are re-initialising reandseed with the value it had }
  398. { at the start -> with the "not", we will detect this change. }
  399. { Detecting other changes is not useful, since the generated }
  400. { numbers will be different anyway. }
  401. randseed := not(randseed);
  402. oldrandseed := randseed;
  403. end;
  404. for kk:=0 to MT19937N-MT19937M-1 do begin
  405. y := (mt[kk] and MT19937UPPER_MASK) or (mt[kk+1] and MT19937LOWER_MASK);
  406. mt[kk] := mt[kk+MT19937M] xor (y shr 1) xor mag01[y and $00000001];
  407. end;
  408. for kk:= MT19937N-MT19937M to MT19937N-2 do begin
  409. y := (mt[kk] and MT19937UPPER_MASK) or (mt[kk+1] and MT19937LOWER_MASK);
  410. mt[kk] := mt[kk+(MT19937M-MT19937N)] xor (y shr 1) xor mag01[y and $00000001];
  411. end;
  412. y := (mt[MT19937N-1] and MT19937UPPER_MASK) or (mt[0] and MT19937LOWER_MASK);
  413. mt[MT19937N-1] := mt[MT19937M-1] xor (y shr 1) xor mag01[y and $00000001];
  414. mti := 0;
  415. end;
  416. y := mt[mti]; inc(mti);
  417. y := y xor (y shr 11);
  418. y := y xor (y shl 7) and TEMPERING_MASK_B;
  419. y := y xor (y shl 15) and TEMPERING_MASK_C;
  420. y := y xor (y shr 18);
  421. Result := y;
  422. end;
  423. function random(l:longint): longint;
  424. begin
  425. { otherwise we can return values = l (JM) }
  426. if (l < 0) then
  427. inc(l);
  428. random := longint((int64(cardinal(genrand_MT19937))*l) shr 32);
  429. end;
  430. function random(l:int64): int64;
  431. begin
  432. { always call random, so the random generator cycles (TP-compatible) (JM) }
  433. random := int64((qword(cardinal(genrand_MT19937)) or ((qword(cardinal(genrand_MT19937)) shl 32))) and $7fffffffffffffff);
  434. if (l<>0) then
  435. random := random mod l
  436. else
  437. random := 0;
  438. end;
  439. {$ifndef FPUNONE}
  440. function random: extended;
  441. begin
  442. random := cardinal(genrand_MT19937) * (1.0/(int64(1) shl 32));
  443. end;
  444. {$endif}
  445. {****************************************************************************
  446. Memory Management
  447. ****************************************************************************}
  448. Function Ptr(sel,off : Longint) : farpointer;{$ifdef SYSTEMINLINE}inline;{$endif}
  449. Begin
  450. ptr:=farpointer((sel shl 4)+off);
  451. End;
  452. Function CSeg : Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  453. Begin
  454. Cseg:=0;
  455. End;
  456. Function DSeg : Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  457. Begin
  458. Dseg:=0;
  459. End;
  460. Function SSeg : Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  461. Begin
  462. Sseg:=0;
  463. End;
  464. {*****************************************************************************
  465. Directory support.
  466. *****************************************************************************}
  467. {$if defined(FPC_HAS_FEATURE_FILEIO) and defined(FPC_HAS_FEATURE_ANSISTRINGS)}
  468. Procedure getdir(drivenr:byte;Var dir:ansistring);
  469. { this is needed to also allow ansistrings, the shortstring version is
  470. OS dependent }
  471. var
  472. s : shortstring;
  473. begin
  474. getdir(drivenr,s);
  475. dir:=s;
  476. end;
  477. {$endif}
  478. {$ifopt R+}
  479. {$define RangeCheckWasOn}
  480. {$R-}
  481. {$endif opt R+}
  482. {$ifopt I+}
  483. {$define IOCheckWasOn}
  484. {$I-}
  485. {$endif opt I+}
  486. {$ifopt Q+}
  487. {$define OverflowCheckWasOn}
  488. {$Q-}
  489. {$endif opt Q+}
  490. {*****************************************************************************
  491. Miscellaneous
  492. *****************************************************************************}
  493. procedure fpc_rangeerror;[public,alias:'FPC_RANGEERROR']; compilerproc;
  494. begin
  495. HandleErrorFrame(201,get_frame);
  496. end;
  497. procedure fpc_divbyzero;[public,alias:'FPC_DIVBYZERO']; compilerproc;
  498. begin
  499. HandleErrorFrame(200,get_frame);
  500. end;
  501. procedure fpc_overflow;[public,alias:'FPC_OVERFLOW']; compilerproc;
  502. begin
  503. HandleErrorFrame(215,get_frame);
  504. end;
  505. procedure fpc_threaderror; [public,alias:'FPC_THREADERROR'];
  506. begin
  507. HandleErrorFrame(6,get_frame);
  508. end;
  509. procedure fpc_iocheck;[public,alias:'FPC_IOCHECK']; compilerproc;
  510. var
  511. l : longint;
  512. HInoutRes : PWord;
  513. begin
  514. HInOutRes:=@InoutRes;
  515. if HInOutRes^<>0 then
  516. begin
  517. l:=HInOutRes^;
  518. HInOutRes^:=0;
  519. HandleErrorFrame(l,get_frame);
  520. end;
  521. end;
  522. Function IOResult:Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  523. var
  524. HInoutRes : PWord;
  525. Begin
  526. HInoutRes:=@InoutRes;
  527. IOResult:=HInOutRes^;
  528. HInOutRes^:=0;
  529. End;
  530. Function GetThreadID:TThreadID;{$ifdef SYSTEMINLINE}inline;{$endif}
  531. begin
  532. (* ThreadID is stored in a threadvar and made available in interface *)
  533. (* to allow setup of this value during thread initialization. *)
  534. GetThreadID := ThreadID;
  535. end;
  536. function fpc_safecallcheck(res : hresult) : hresult;[public,alias:'FPC_SAFECALLCHECK']; compilerproc;
  537. begin
  538. if res<0 then
  539. begin
  540. if assigned(SafeCallErrorProc) then
  541. SafeCallErrorProc(res,get_frame);
  542. HandleErrorFrame(229,get_frame);
  543. end;
  544. result:=res;
  545. end;
  546. {*****************************************************************************
  547. Stack check code
  548. *****************************************************************************}
  549. { be compatible with old code }
  550. {$ifdef FPC_NO_GENERIC_STACK_CHECK}
  551. {$define NO_GENERIC_STACK_CHECK}
  552. {$endif FPC_NO_GENERIC_STACK_CHECK}
  553. {$IFNDEF NO_GENERIC_STACK_CHECK}
  554. {$IFOPT S+}
  555. {$DEFINE STACKCHECK}
  556. {$ENDIF}
  557. {$S-}
  558. procedure fpc_stackcheck(stack_size:SizeUInt);[public,alias:'FPC_STACKCHECK'];
  559. var
  560. c : Pointer;
  561. begin
  562. { Avoid recursive calls when called from the exit routines }
  563. if StackError then
  564. exit;
  565. c := Sptr - (stack_size + STACK_MARGIN);
  566. if (c <= StackBottom) then
  567. begin
  568. StackError:=true;
  569. HandleError(202);
  570. end;
  571. end;
  572. {$IFDEF STACKCHECK}
  573. {$S+}
  574. {$ENDIF}
  575. {$UNDEF STACKCHECK}
  576. {$ENDIF NO_GENERIC_STACK_CHECK}
  577. {*****************************************************************************
  578. Initialization / Finalization
  579. *****************************************************************************}
  580. const
  581. maxunits=1024; { See also files.pas of the compiler source }
  582. type
  583. TInitFinalRec=record
  584. InitProc,
  585. FinalProc : TProcedure;
  586. end;
  587. TInitFinalTable = record
  588. TableCount,
  589. InitCount : longint;
  590. Procs : array[1..maxunits] of TInitFinalRec;
  591. end;
  592. PInitFinalTable = ^TInitFinalTable;
  593. {$ifndef FPC_HAS_INDIRECT_MAIN_INFORMATION}
  594. var
  595. InitFinalTable : TInitFinalTable;external name 'INITFINAL';
  596. {$endif FPC_HAS_INDIRECT_MAIN_INFORMATION}
  597. procedure fpc_InitializeUnits;[public,alias:'FPC_INITIALIZEUNITS']; compilerproc;
  598. var
  599. i : longint;
  600. begin
  601. { call cpu/fpu initialisation routine }
  602. fpc_cpuinit;
  603. with {$ifdef FPC_HAS_INDIRECT_MAIN_INFORMATION}PInitFinalTable(EntryInformation.{$endif FPC_HAS_INDIRECT_MAIN_INFORMATION}
  604. InitFinalTable
  605. {$ifdef FPC_HAS_INDIRECT_MAIN_INFORMATION})^{$endif FPC_HAS_INDIRECT_MAIN_INFORMATION} do
  606. begin
  607. for i:=1 to TableCount do
  608. begin
  609. if assigned(Procs[i].InitProc) then
  610. Procs[i].InitProc();
  611. InitCount:=i;
  612. end;
  613. end;
  614. if assigned(InitProc) then
  615. TProcedure(InitProc)();
  616. end;
  617. procedure FinalizeUnits;[public,alias:'FPC_FINALIZEUNITS'];
  618. begin
  619. with {$ifdef FPC_HAS_INDIRECT_MAIN_INFORMATION}PInitFinalTable(EntryInformation.{$endif FPC_HAS_INDIRECT_MAIN_INFORMATION}
  620. InitFinalTable
  621. {$ifdef FPC_HAS_INDIRECT_MAIN_INFORMATION})^{$endif FPC_HAS_INDIRECT_MAIN_INFORMATION} do
  622. begin
  623. while (InitCount>0) do
  624. begin
  625. // we've to decrement the cound before calling the final. code
  626. // else a halt in the final. code leads to a endless loop
  627. dec(InitCount);
  628. if assigned(Procs[InitCount+1].FinalProc) then
  629. Procs[InitCount+1].FinalProc();
  630. end;
  631. end;
  632. end;
  633. {*****************************************************************************
  634. Error / Exit / ExitProc
  635. *****************************************************************************}
  636. Procedure system_exit;forward;
  637. {$ifdef FPC_HAS_FEATURE_HEAP}
  638. {$ifndef HAS_MEMORYMANAGER}
  639. //not needed if independant memory manager
  640. Procedure FinalizeHeap;forward;
  641. {$endif HAS_MEMORYMANAGER}
  642. {$endif FPC_HAS_FEATURE_HEAP}
  643. Procedure InternalExit;
  644. var
  645. current_exit : Procedure;
  646. pstdout : ^Text;
  647. {$if defined(MSWINDOWS) or defined(OS2)}
  648. i : longint;
  649. {$endif}
  650. Begin
  651. {$ifdef SYSTEMDEBUG}
  652. writeln('InternalExit');
  653. {$endif SYSTEMDEBUG}
  654. while exitProc<>nil Do
  655. Begin
  656. InOutRes:=0;
  657. current_exit:=tProcedure(exitProc);
  658. exitProc:=nil;
  659. current_exit();
  660. End;
  661. { Finalize units }
  662. FinalizeUnits;
  663. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  664. { Show runtime error and exit }
  665. pstdout:=@stdout;
  666. If erroraddr<>nil Then
  667. Begin
  668. Writeln(pstdout^,'Runtime error ',Errorcode,' at $',hexstr(erroraddr));
  669. { to get a nice symify }
  670. Writeln(pstdout^,BackTraceStrFunc(Erroraddr));
  671. dump_stack(pstdout^,ErrorBase);
  672. Writeln(pstdout^,'');
  673. End;
  674. { Make sure that all output is written to the redirected file }
  675. Flush(Output);
  676. Flush(ErrOutput);
  677. Flush(pstdout^);
  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'];
  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. {$ifndef HAS_RESOURCES}
  1081. {$i sysres.inc}
  1082. {$endif}
  1083. {$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
  1084. Function FindResource(ModuleHandle: HMODULE; ResourceName, ResourceType: AnsiString): TResourceHandle;
  1085. begin
  1086. Result:=FindResource(ModuleHandle,PChar(ResourceName),PChar(ResourceType));
  1087. end;
  1088. {$endif}
  1089. const
  1090. CtrlBreakHandler: TCtrlBreakHandler = nil;
  1091. {$IFNDEF FPC_HAS_SETCTRLBREAKHANDLER}
  1092. (* It is possible to provide platform specific implementation performing *)
  1093. (* special initialization; default implementation just sets the procedural *)
  1094. (* variable to make it available for use from the exception handler. *)
  1095. function SysSetCtrlBreakHandler (Handler: TCtrlBreakHandler): TCtrlBreakHandler;
  1096. begin
  1097. (* Return either nil or previous handler *)
  1098. SysSetCtrlBreakHandler := CtrlBreakHandler;
  1099. CtrlBreakHandler := Handler;
  1100. end;
  1101. {$ENDIF FPC_HAS_SETCTRLBREAKHANDLER}