system.inc 37 KB

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