system.inc 36 KB

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