system.inc 36 KB

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