system.pp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2000 by the Free Pascal development team.
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. { no stack check in system }
  12. {$S-}
  13. unit system;
  14. interface
  15. {$define StdErrToConsole}
  16. {$ifdef SYSTEMDEBUG}
  17. {$define SYSTEMEXCEPTIONDEBUG}
  18. {$endif SYSTEMDEBUG}
  19. {$ifdef cpui386}
  20. {$define Set_i386_Exception_handler}
  21. {$endif cpui386}
  22. { include system-independent routine headers }
  23. {$I systemh.inc}
  24. type
  25. THandle = cardinal;
  26. {Platform specific information}
  27. const
  28. LineEnding = #13#10;
  29. LFNSupport = false; { ??? - that's how it was declared in dos.pp! }
  30. DirectorySeparator = '\';
  31. DriveSeparator = ':';
  32. PathSeparator = ';';
  33. { FileNameCaseSensitive is defined separately below!!! }
  34. type
  35. { the fields of this record are os dependent }
  36. { and they shouldn't be used in a program }
  37. { only the type TCriticalSection is important }
  38. TRTLCriticalSection = packed record
  39. SemaHandle : LONGINT;
  40. SemaIsOpen : BOOLEAN;
  41. end;
  42. { include threading stuff }
  43. { i threadh.inc}
  44. { include heap support headers }
  45. {$I heaph.inc}
  46. CONST
  47. { Default filehandles }
  48. UnusedHandle : longint = -1;
  49. StdInputHandle : longint = 0;
  50. StdOutputHandle : longint = 0;
  51. StdErrorHandle : longint = 0;
  52. FileNameCaseSensitive : boolean = false;
  53. sLineBreak = LineEnding;
  54. DefaultTextLineBreakStyle : TTextLineBreakStyle = tlbsCRLF;
  55. TYPE
  56. TNWCheckFunction = procedure (var code : longint);
  57. VAR
  58. ArgC : INTEGER;
  59. ArgV : ppchar;
  60. NetwareCheckFunction : TNWCheckFunction;
  61. NetwareMainThreadGroupID: longint;
  62. NetwareCodeStartAddress : dword;
  63. CONST
  64. envp : ppchar = nil; {dummy to make heaptrc happy}
  65. PROCEDURE ConsolePrintf (FormatStr : PCHAR; Param : LONGINT); CDecl;
  66. PROCEDURE ConsolePrintf3 (FormatStr : PCHAR; P1,P2,P3 : LONGINT); CDecl;
  67. PROCEDURE ConsolePrintf (FormatStr : PCHAR); CDecl;
  68. type
  69. TSysCloseAllRemainingSemaphores = procedure;
  70. TSysReleaseThreadVars = procedure;
  71. TSysSetThreadDataAreaPtr = function (newPtr:pointer):pointer;
  72. procedure NWSysSetThreadFunctions (crs:TSysCloseAllRemainingSemaphores;
  73. rtv:TSysReleaseThreadVars;
  74. stdata:TSysSetThreadDataAreaPtr);
  75. implementation
  76. { Indicate that stack checking is taken care by OS}
  77. {$DEFINE NO_GENERIC_STACK_CHECK}
  78. { include system independent routines }
  79. {$I system.inc}
  80. { some declarations for Netware API calls }
  81. {$I nwsys.inc}
  82. {$I errno.inc}
  83. {procedure setup_arguments;
  84. begin
  85. end;
  86. }
  87. {procedure setup_environment;
  88. begin
  89. end;
  90. }
  91. var
  92. CloseAllRemainingSemaphores : TSysCloseAllRemainingSemaphores = nil;
  93. ReleaseThreadVars : TSysReleaseThreadVars = nil;
  94. SetThreadDataAreaPtr : TSysSetThreadDataAreaPtr = nil;
  95. procedure NWSysSetThreadFunctions (crs:TSysCloseAllRemainingSemaphores;
  96. rtv:TSysReleaseThreadVars;
  97. stdata:TSysSetThreadDataAreaPtr);
  98. begin
  99. CloseAllRemainingSemaphores := crs;
  100. ReleaseThreadVars := rtv;
  101. SetThreadDataAreaPtr := stdata;
  102. end;
  103. procedure PASCALMAIN;external name 'PASCALMAIN';
  104. procedure fpc_do_exit;external name 'FPC_DO_EXIT';
  105. {*****************************************************************************
  106. Startup
  107. *****************************************************************************}
  108. function __GetBssStart : pointer; external name '__getBssStart';
  109. function __getUninitializedDataSize : longint; external name '__getUninitializedDataSize';
  110. //function __getDataStart : longint; external name '__getDataStart';
  111. function __GetTextStart : longint; external name '__getTextStart';
  112. PROCEDURE nlm_main (_ArgC : LONGINT; _ArgV : ppchar); CDECL; [public,alias: '_nlm_main'];
  113. BEGIN
  114. // Initialize BSS
  115. if __getUninitializedDataSize > 0 then
  116. fillchar (__getBssStart^,__getUninitializedDataSize,0);
  117. NetwareCodeStartAddress := __GetTextStart;
  118. ArgC := _ArgC;
  119. ArgV := _ArgV;
  120. fpc_threadvar_relocate_proc := nil;
  121. PASCALMAIN;
  122. END;
  123. {*****************************************************************************
  124. System Dependent Exit code
  125. *****************************************************************************}
  126. procedure FreeSbrkMem; forward;
  127. var SigTermHandlerActive : boolean;
  128. Procedure system_exit;
  129. begin
  130. if assigned (CloseAllRemainingSemaphores) then CloseAllRemainingSemaphores;
  131. if assigned (ReleaseThreadVars) then ReleaseThreadVars;
  132. FreeSbrkMem; { free memory allocated by heapmanager }
  133. if not SigTermHandlerActive then
  134. begin
  135. if ExitCode <> 0 Then { otherwise we dont see runtime-errors }
  136. _SetAutoScreenDestructionMode (false);
  137. _exit (ExitCode);
  138. end;
  139. end;
  140. {*****************************************************************************
  141. Stack check code
  142. *****************************************************************************}
  143. procedure int_stackcheck(stack_size:longint);[public,alias:'FPC_STACKCHECK'];
  144. {
  145. called when trying to get local stack if the compiler directive $S
  146. is set this function must preserve esi !!!! because esi is set by
  147. the calling proc for methods it must preserve all registers !!
  148. With a 2048 byte safe area used to write to StdIo without crossing
  149. the stack boundary
  150. }
  151. begin
  152. IF _stackavail > stack_size + 2048 THEN EXIT;
  153. HandleError (202);
  154. end;
  155. {*****************************************************************************
  156. ParamStr/Randomize
  157. *****************************************************************************}
  158. { number of args }
  159. function paramcount : longint;
  160. begin
  161. paramcount := argc - 1;
  162. end;
  163. { argument number l }
  164. function paramstr(l : longint) : string;
  165. begin
  166. if (l>=0) and (l+1<=argc) then
  167. paramstr:=strpas(argv[l])
  168. else
  169. paramstr:='';
  170. end;
  171. { set randseed to a new pseudo random value }
  172. procedure randomize;
  173. begin
  174. randseed := _time (NIL);
  175. end;
  176. {*****************************************************************************
  177. Heap Management
  178. *****************************************************************************}
  179. var
  180. heap : longint;external name 'HEAP';
  181. intern_heapsize : longint;external name 'HEAPSIZE';
  182. { first address of heap }
  183. function getheapstart:pointer;
  184. assembler;
  185. asm
  186. leal HEAP,%eax
  187. end ['EAX'];
  188. { current length of heap }
  189. function getheapsize:longint;
  190. assembler;
  191. asm
  192. movl intern_HEAPSIZE,%eax
  193. end ['EAX'];
  194. const HeapInitialMaxBlocks = 32;
  195. type THeapSbrkBlockList = array [1.. HeapInitialMaxBlocks] of pointer;
  196. var HeapSbrkBlockList : ^THeapSbrkBlockList = nil;
  197. HeapSbrkLastUsed : dword = 0;
  198. HeapSbrkAllocated : dword = 0;
  199. { function to allocate size bytes more for the program }
  200. { must return the first address of new data space or nil if fail }
  201. { for netware all allocated blocks are saved to free them at }
  202. { exit (to avoid message "Module did not release xx resources") }
  203. Function Sbrk(size : longint):pointer;
  204. var P2 : POINTER;
  205. begin
  206. Sbrk := _malloc (size);
  207. if Sbrk <> nil then begin
  208. if HeapSbrkBlockList = nil then
  209. begin
  210. Pointer (HeapSbrkBlockList) := _malloc (sizeof (HeapSbrkBlockList^));
  211. if HeapSbrkBlockList = nil then
  212. begin
  213. _free (Sbrk);
  214. Sbrk := nil;
  215. exit;
  216. end;
  217. fillchar (HeapSbrkBlockList^,sizeof(HeapSbrkBlockList^),0);
  218. HeapSbrkAllocated := HeapInitialMaxBlocks;
  219. end;
  220. if (HeapSbrkLastUsed = HeapSbrkAllocated) then
  221. begin { grow }
  222. p2 := _realloc (HeapSbrkBlockList, HeapSbrkAllocated + HeapInitialMaxBlocks);
  223. if p2 = nil then
  224. begin
  225. _free (Sbrk);
  226. Sbrk := nil;
  227. exit;
  228. end;
  229. inc (HeapSbrkAllocated, HeapInitialMaxBlocks);
  230. end;
  231. inc (HeapSbrkLastUsed);
  232. HeapSbrkBlockList^[HeapSbrkLastUsed] := Sbrk;
  233. end;
  234. end;
  235. procedure FreeSbrkMem;
  236. var i : longint;
  237. begin
  238. if HeapSbrkBlockList <> nil then
  239. begin
  240. for i := 1 to HeapSbrkLastUsed do
  241. _free (HeapSbrkBlockList^[i]);
  242. _free (HeapSbrkBlockList);
  243. HeapSbrkAllocated := 0;
  244. HeapSbrkLastUsed := 0;
  245. HeapSbrkBlockList := nil;
  246. end;
  247. end;
  248. { include standard heap management }
  249. {$I heap.inc}
  250. {****************************************************************************
  251. Low level File Routines
  252. All these functions can set InOutRes on errors
  253. ****************************************************************************}
  254. PROCEDURE NW2PASErr (Err : LONGINT);
  255. BEGIN
  256. if Err = 0 then { Else it will go through all the cases }
  257. exit;
  258. case Err of
  259. Sys_ENFILE,
  260. Sys_EMFILE : Inoutres:=4;
  261. Sys_ENOENT : Inoutres:=2;
  262. Sys_EBADF : Inoutres:=6;
  263. Sys_ENOMEM,
  264. Sys_EFAULT : Inoutres:=217;
  265. Sys_EINVAL : Inoutres:=218;
  266. Sys_EPIPE,
  267. Sys_EINTR,
  268. Sys_EIO,
  269. Sys_EAGAIN,
  270. Sys_ENOSPC : Inoutres:=101;
  271. Sys_ENAMETOOLONG,
  272. Sys_ELOOP,
  273. Sys_ENOTDIR : Inoutres:=3;
  274. Sys_EROFS,
  275. Sys_EEXIST,
  276. Sys_EACCES : Inoutres:=5;
  277. Sys_EBUSY : Inoutres:=162;
  278. end;
  279. END;
  280. FUNCTION errno : LONGINT;
  281. BEGIN
  282. errno := __get_errno_ptr^;
  283. END;
  284. PROCEDURE Errno2Inoutres;
  285. BEGIN
  286. NW2PASErr (errno);
  287. END;
  288. PROCEDURE SetFileError (VAR Err : LONGINT);
  289. BEGIN
  290. IF Err >= 0 THEN
  291. InOutRes := 0
  292. ELSE
  293. BEGIN
  294. Err := errno;
  295. NW2PASErr (Err);
  296. Err := 0;
  297. END;
  298. END;
  299. { close a file from the handle value }
  300. procedure do_close(handle : longint);
  301. VAR res : LONGINT;
  302. begin
  303. res := _close (handle);
  304. IF res <> 0 THEN
  305. SetFileError (res)
  306. ELSE
  307. InOutRes := 0;
  308. end;
  309. procedure do_erase(p : pchar);
  310. VAR res : LONGINT;
  311. begin
  312. res := _unlink (p);
  313. IF Res < 0 THEN
  314. SetFileError (res)
  315. ELSE
  316. InOutRes := 0;
  317. end;
  318. procedure do_rename(p1,p2 : pchar);
  319. VAR res : LONGINT;
  320. begin
  321. res := _rename (p1,p2);
  322. IF Res < 0 THEN
  323. SetFileError (res)
  324. ELSE
  325. InOutRes := 0
  326. end;
  327. function do_write(h,addr,len : longint) : longint;
  328. VAR res : LONGINT;
  329. begin
  330. res := _write (h,POINTER(addr),len);
  331. IF res > 0 THEN
  332. InOutRes := 0
  333. ELSE
  334. SetFileError (res);
  335. do_write := res;
  336. end;
  337. function do_read(h,addr,len : longint) : longint;
  338. VAR res : LONGINT;
  339. begin
  340. res := _read (h,POINTER(addr),len);
  341. IF res > 0 THEN
  342. InOutRes := 0
  343. ELSE
  344. SetFileError (res);
  345. do_read := res;
  346. end;
  347. function do_filepos(handle : longint) : longint;
  348. VAR res : LONGINT;
  349. begin
  350. InOutRes:=1;
  351. res := _tell (handle);
  352. IF res < 0 THEN
  353. SetFileError (res)
  354. ELSE
  355. InOutRes := 0;
  356. do_filepos := res;
  357. end;
  358. CONST SEEK_SET = 0; // Seek from beginning of file.
  359. SEEK_CUR = 1; // Seek from current position.
  360. SEEK_END = 2; // Seek from end of file.
  361. procedure do_seek(handle,pos : longint);
  362. VAR res : LONGINT;
  363. begin
  364. res := _lseek (handle,pos, SEEK_SET);
  365. IF res >= 0 THEN
  366. InOutRes := 0
  367. ELSE
  368. SetFileError (res);
  369. end;
  370. function do_seekend(handle:longint):longint;
  371. VAR res : LONGINT;
  372. begin
  373. res := _lseek (handle,0, SEEK_END);
  374. IF res >= 0 THEN
  375. InOutRes := 0
  376. ELSE
  377. SetFileError (res);
  378. do_seekend := res;
  379. end;
  380. function do_filesize(handle : longint) : longint;
  381. VAR res : LONGINT;
  382. begin
  383. res := _filelength (handle);
  384. IF res < 0 THEN
  385. BEGIN
  386. SetFileError (Res);
  387. do_filesize := -1;
  388. END ELSE
  389. BEGIN
  390. InOutRes := 0;
  391. do_filesize := res;
  392. END;
  393. end;
  394. { truncate at a given position }
  395. procedure do_truncate (handle,pos:longint);
  396. VAR res : LONGINT;
  397. begin
  398. res := _chsize (handle,pos);
  399. IF res <> 0 THEN
  400. SetFileError (res)
  401. ELSE
  402. InOutRes := 0;
  403. end;
  404. // mostly stolen from syslinux
  405. procedure do_open(var f;p:pchar;flags:longint);
  406. {
  407. filerec and textrec have both handle and mode as the first items so
  408. they could use the same routine for opening/creating.
  409. when (flags and $10) the file will be append
  410. when (flags and $100) the file will be truncate/rewritten
  411. when (flags and $1000) there is no check for close (needed for textfiles)
  412. }
  413. var
  414. oflags : longint;
  415. Begin
  416. { close first if opened }
  417. if ((flags and $10000)=0) then
  418. begin
  419. case FileRec(f).mode of
  420. fminput,fmoutput,fminout : Do_Close(FileRec(f).Handle);
  421. fmclosed : ;
  422. else
  423. begin
  424. inoutres:=102; {not assigned}
  425. exit;
  426. end;
  427. end;
  428. end;
  429. { reset file Handle }
  430. FileRec(f).Handle:=UnusedHandle;
  431. { We do the conversion of filemodes here, concentrated on 1 place }
  432. case (flags and 3) of
  433. 0 : begin
  434. oflags := O_RDONLY;
  435. filerec(f).mode := fminput;
  436. end;
  437. 1 : begin
  438. oflags := O_WRONLY;
  439. filerec(f).mode := fmoutput;
  440. end;
  441. 2 : begin
  442. oflags := O_RDWR;
  443. filerec(f).mode := fminout;
  444. end;
  445. end;
  446. if (flags and $1000)=$1000 then
  447. oflags:=oflags or (O_CREAT or O_TRUNC)
  448. else
  449. if (flags and $100)=$100 then
  450. oflags:=oflags or (O_APPEND);
  451. { empty name is special }
  452. if p[0]=#0 then
  453. begin
  454. case FileRec(f).mode of
  455. fminput :
  456. FileRec(f).Handle:=StdInputHandle;
  457. fminout, { this is set by rewrite }
  458. fmoutput :
  459. FileRec(f).Handle:=StdOutputHandle;
  460. fmappend :
  461. begin
  462. FileRec(f).Handle:=StdOutputHandle;
  463. FileRec(f).mode:=fmoutput; {fool fmappend}
  464. end;
  465. end;
  466. exit;
  467. end;
  468. { real open call }
  469. FileRec(f).Handle := _open(p,oflags,438);
  470. //WriteLn ('_open (',p,') liefert ',ErrNo, 'Handle: ',FileRec(f).Handle);
  471. // errno does not seem to be set on succsess ??
  472. IF FileRec(f).Handle < 0 THEN
  473. if (ErrNo=Sys_EROFS) and ((OFlags and O_RDWR)<>0) then
  474. begin // i.e. for cd-rom
  475. Oflags:=Oflags and not(O_RDWR);
  476. FileRec(f).Handle := _open(p,oflags,438);
  477. end;
  478. IF FileRec(f).Handle < 0 THEN
  479. Errno2Inoutres
  480. ELSE
  481. InOutRes := 0;
  482. End;
  483. function do_isdevice(handle:longint):boolean;
  484. begin
  485. do_isdevice := (_isatty (handle) > 0);
  486. end;
  487. {*****************************************************************************
  488. UnTyped File Handling
  489. *****************************************************************************}
  490. {$i file.inc}
  491. {*****************************************************************************
  492. Typed File Handling
  493. *****************************************************************************}
  494. {$i typefile.inc}
  495. {*****************************************************************************
  496. Text File Handling
  497. *****************************************************************************}
  498. { should we consider #26 as the end of a file ? }
  499. {?? $DEFINE EOF_CTRLZ}
  500. {$i text.inc}
  501. {*****************************************************************************
  502. Directory Handling
  503. *****************************************************************************}
  504. procedure mkdir(const s : string);[IOCheck];
  505. VAR S2 : STRING;
  506. Res: LONGINT;
  507. BEGIN
  508. S2 := S;
  509. IF Length (S2) = 255 THEN DEC (BYTE(S2[0]));
  510. S2 := S2 + #0;
  511. Res := _mkdir (@S2[1]);
  512. IF Res = 0 THEN
  513. InOutRes:=0
  514. ELSE
  515. SetFileError (Res);
  516. END;
  517. procedure rmdir(const s : string);[IOCheck];
  518. VAR S2 : STRING;
  519. Res: LONGINT;
  520. BEGIN
  521. S2 := S;
  522. IF Length (S2) = 255 THEN DEC (BYTE(S2[0]));
  523. S2 := S2 + #0;
  524. Res := _rmdir (@S2[1]);
  525. IF Res = 0 THEN
  526. InOutRes:=0
  527. ELSE
  528. SetFileError (Res);
  529. end;
  530. procedure chdir(const s : string);[IOCheck];
  531. VAR S2 : STRING;
  532. Res: LONGINT;
  533. begin
  534. S2 := S;
  535. IF Length (S2) = 255 THEN DEC (BYTE(S2[0]));
  536. S2 := S2 + #0;
  537. Res := _chdir (@S2[1]);
  538. IF Res = 0 THEN
  539. InOutRes:=0
  540. ELSE
  541. SetFileError (Res);
  542. end;
  543. procedure getdir(drivenr : byte;var dir : shortstring);
  544. VAR P : ARRAY [0..255] OF CHAR;
  545. Len: LONGINT;
  546. begin
  547. P[0] := #0;
  548. _getcwd (@P, SIZEOF (P));
  549. Len := _strlen (P);
  550. IF Len > 0 THEN
  551. BEGIN
  552. Move (P, dir[1], Len);
  553. BYTE(dir[0]) := Len;
  554. END ELSE
  555. InOutRes := 1;
  556. end;
  557. {*****************************************************************************
  558. Thread Handling
  559. *****************************************************************************}
  560. procedure InitFPU;assembler;
  561. asm
  562. fninit
  563. fldcw fpucw
  564. end;
  565. { if return-value is <> 0, netware shows the message
  566. Unload Anyway ?
  567. To Disable unload at all, SetNLMDontUnloadFlag can be used on
  568. Netware >= 4.0 }
  569. function CheckFunction : longint; CDECL; [public,alias: 'FPC_NW_CHECKFUNCTION'];
  570. var oldTG:longint;
  571. oldPtr: pointer;
  572. begin
  573. if assigned (NetwareCheckFunction) then
  574. begin
  575. { this function is called without clib context, to allow clib
  576. calls, we set the thread group id before calling the
  577. user-function }
  578. oldTG := _SetThreadGroupID (NetwareMainThreadGroupID);
  579. { to allow use of threadvars, we simply set the threadvar-memory
  580. from the main thread }
  581. if assigned (SetThreadDataAreaPtr) then
  582. oldPtr := SetThreadDataAreaPtr (NIL); { nil means main threadvars }
  583. result := 0;
  584. NetwareCheckFunction (result);
  585. if assigned (SetThreadDataAreaPtr) then
  586. SetThreadDataAreaPtr (oldPtr);
  587. _SetThreadGroupID (oldTG);
  588. end else
  589. result := 0;
  590. end;
  591. {$ifdef StdErrToConsole}
  592. var ConsoleBuff : array [0..512] of char;
  593. Function ConsoleWrite(Var F: TextRec): Integer;
  594. var
  595. i : longint;
  596. Begin
  597. if F.BufPos>0 then
  598. begin
  599. if F.BufPos>sizeof(ConsoleBuff)-1 then
  600. i:=sizeof(ConsoleBuff)-1
  601. else
  602. i:=F.BufPos;
  603. Move(F.BufPtr^,ConsoleBuff,i);
  604. ConsoleBuff[i] := #0;
  605. ConsolePrintf(@ConsoleBuff[0]);
  606. end;
  607. F.BufPos:=0;
  608. ConsoleWrite := 0;
  609. End;
  610. Function ConsoleClose(Var F: TextRec): Integer;
  611. begin
  612. ConsoleClose:=0;
  613. end;
  614. Function ConsoleOpen(Var F: TextRec): Integer;
  615. Begin
  616. TextRec(F).InOutFunc:=@ConsoleWrite;
  617. TextRec(F).FlushFunc:=@ConsoleWrite;
  618. TextRec(F).CloseFunc:=@ConsoleClose;
  619. ConsoleOpen:=0;
  620. End;
  621. procedure AssignStdErrConsole(Var T: Text);
  622. begin
  623. Assign(T,'');
  624. TextRec(T).OpenFunc:=@ConsoleOpen;
  625. Rewrite(T);
  626. end;
  627. {$endif}
  628. { this will be called if the nlm is unloaded. It will NOT be
  629. called if the program exits i.e. with halt.
  630. Halt (or _exit) can not be called from this callback procedure }
  631. procedure TermSigHandler (Sig:longint); CDecl;
  632. var oldTG : longint;
  633. oldPtr: pointer;
  634. begin
  635. oldTG := _SetThreadGroupID (NetwareMainThreadGroupID); { this is only needed for nw 3.11 }
  636. { _GetThreadDataAreaPtr will not be valid because the signal
  637. handler is called by netware with a differnt thread. To avoid
  638. problems in the exit routines, we set the data of the main thread
  639. here }
  640. if assigned (SetThreadDataAreaPtr) then
  641. oldPtr := SetThreadDataAreaPtr (NIL); { nil means main thread }
  642. SigTermHandlerActive := true; { to avoid that system_exit calls _exit }
  643. do_exit; { calls finalize units }
  644. if assigned (SetThreadDataAreaPtr) then
  645. SetThreadDataAreaPtr (oldPtr);
  646. _SetThreadGroupID (oldTG);
  647. end;
  648. procedure SysInitStdIO;
  649. begin
  650. { Setup stdin, stdout and stderr }
  651. StdInputHandle := _fileno (LONGINT (_GetStdIn^)); // GetStd** returns **FILE !
  652. StdOutputHandle:= _fileno (LONGINT (_GetStdOut^));
  653. StdErrorHandle := _fileno (LONGINT (_GetStdErr^));
  654. OpenStdIO(Input,fmInput,StdInputHandle);
  655. OpenStdIO(Output,fmOutput,StdOutputHandle);
  656. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  657. {$ifdef StdErrToConsole}
  658. AssignStdErrConsole(StdErr);
  659. {$else}
  660. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  661. {$endif}
  662. end;
  663. {*****************************************************************************
  664. SystemUnit Initialization
  665. *****************************************************************************}
  666. Begin
  667. StackBottom := SPtr - StackLength;
  668. SigTermHandlerActive := false;
  669. NetwareCheckFunction := nil;
  670. NetwareMainThreadGroupID := _GetThreadGroupID;
  671. _Signal (_SIGTERM, @TermSigHandler);
  672. { Setup heap }
  673. InitHeap;
  674. SysInitExceptions;
  675. { Reset IO Error }
  676. InOutRes:=0;
  677. SysInitStdIO;
  678. {Delphi Compatible}
  679. IsLibrary := FALSE;
  680. IsConsole := TRUE;
  681. ExitCode := 0;
  682. {$ifdef HASVARIANT}
  683. initvariantmanager;
  684. {$endif HASVARIANT}
  685. End.
  686. {
  687. $Log$
  688. Revision 1.20 2003-10-25 23:43:59 hajny
  689. * THandle in sysutils common using System.THandle
  690. Revision 1.19 2003/10/17 22:12:02 olle
  691. * changed i386 to cpui386
  692. Revision 1.18 2003/09/27 11:52:35 peter
  693. * sbrk returns pointer
  694. Revision 1.17 2003/03/25 18:17:54 armin
  695. * support for fcl, support for linking without debug info
  696. * renamed winsock2 to winsock for win32 compatinility
  697. * new sockets unit for netware
  698. * changes for compiler warnings
  699. Revision 1.16 2003/02/15 19:12:54 armin
  700. * changes for new threadvar support
  701. Revision 1.15 2002/10/13 09:28:45 florian
  702. + call to initvariantmanager inserted
  703. Revision 1.14 2002/09/07 16:01:21 peter
  704. * old logs removed and tabs fixed
  705. Revision 1.13 2002/07/01 16:29:05 peter
  706. * sLineBreak changed to normal constant like Kylix
  707. Revision 1.12 2002/04/15 18:47:34 carl
  708. + reinstate novell stack checking
  709. Revision 1.11 2002/04/12 17:40:11 carl
  710. + generic stack checking
  711. Revision 1.10 2002/04/01 15:20:08 armin
  712. + unload module no longer shows: Module did not release...
  713. + check-function will no longer be removed when smartlink is on
  714. Revision 1.9 2002/04/01 10:47:31 armin
  715. makefile.fpc for netware
  716. stderr to netware console
  717. free all memory (threadvars and heap) to avoid error message while unloading nlm
  718. Revision 1.8 2002/03/30 09:09:47 armin
  719. + support check-function for netware
  720. Revision 1.7 2002/03/17 17:57:33 armin
  721. + threads and winsock2 implemented
  722. }