system.pp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2004 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. System.pp for Netware libc environment
  11. **********************************************************************}
  12. { no stack check in system }
  13. {$S-}
  14. unit system;
  15. interface
  16. {$define netware}
  17. {$define netware_libc}
  18. {$define StdErrToConsole}
  19. {$define autoHeapRelease}
  20. {$define IOpossix}
  21. {$define DisableArrayOfConst}
  22. {$ifdef SYSTEMDEBUG}
  23. {$define SYSTEMEXCEPTIONDEBUG}
  24. {$endif SYSTEMDEBUG}
  25. {$ifdef cpui386}
  26. {$define Set_i386_Exception_handler}
  27. {$endif cpui386}
  28. { include system-independent routine headers }
  29. {$I systemh.inc}
  30. type THandle = DWord;
  31. {Platform specific information}
  32. const
  33. LineEnding = #13#10;
  34. LFNSupport : boolean = false;
  35. DirectorySeparator = '/';
  36. DriveSeparator = ':';
  37. PathSeparator = ';';
  38. { FileNameCaseSensitive is defined separately below!!! }
  39. maxExitCode = $ffff;
  40. { include heap support headers }
  41. {$I heaph.inc}
  42. CONST
  43. { Default filehandles }
  44. UnusedHandle : THandle = -1;
  45. StdInputHandle : THandle = 0;
  46. StdOutputHandle : THandle = 0;
  47. StdErrorHandle : THandle = 0;
  48. FileNameCaseSensitive : boolean = false;
  49. sLineBreak = LineEnding;
  50. DefaultTextLineBreakStyle : TTextLineBreakStyle = tlbsCRLF;
  51. type
  52. TNWCheckFunction = procedure (var code : longint);
  53. TDLL_Process_Entry_Hook = function (dllparam : longint) : longbool;
  54. TDLL_Entry_Hook = procedure (dllparam : longint);
  55. VAR
  56. ArgC : INTEGER;
  57. ArgV : ppchar;
  58. NetwareCheckFunction: TNWCheckFunction;
  59. NWLoggerScreen : pointer = nil;
  60. const
  61. Dll_Process_Attach_Hook : TDLL_Process_Entry_Hook = nil;
  62. Dll_Process_Detach_Hook : TDLL_Entry_Hook = nil;
  63. Dll_Thread_Attach_Hook : TDLL_Entry_Hook = nil;
  64. Dll_Thread_Detach_Hook : TDLL_Entry_Hook = nil;
  65. NetwareUnloadProc : pointer = nil; {like exitProc but for nlm unload only}
  66. envp : ppchar = nil;
  67. type
  68. //TSysCloseAllRemainingSemaphores = procedure;
  69. TSysReleaseThreadVars = procedure;
  70. TSysSetThreadDataAreaPtr = function (newPtr:pointer):pointer;
  71. procedure NWSysSetThreadFunctions (atv:TSysReleaseThreadVars;
  72. rtv:TSysReleaseThreadVars;
  73. stdata:TSysSetThreadDataAreaPtr);
  74. procedure ConsolePrintf (s :shortstring);
  75. procedure ConsolePrintf (FormatStr : PCHAR; Param : LONGINT);
  76. procedure ConsolePrintf (FormatStr : PCHAR; Param : pchar);
  77. procedure ConsolePrintf (FormatStr : PCHAR; P1,P2 : LONGINT);
  78. procedure ConsolePrintf (FormatStr : PCHAR; P1,P2,P3 : LONGINT);
  79. procedure ConsolePrintf (FormatStr : PCHAR);
  80. procedure __EnterDebugger; cdecl;
  81. function NWGetCodeStart : pointer; // needed for Lineinfo
  82. function NWGetCodeLength : dword;
  83. function NWGetDataStart : pointer;
  84. function NWGetDataLength : dword;
  85. implementation
  86. { Indicate that stack checking is taken care by OS}
  87. {$DEFINE NO_GENERIC_STACK_CHECK}
  88. { include system independent routines }
  89. {$I system.inc}
  90. { some declarations for Netware API calls }
  91. { I nwlibc.inc}
  92. {$I errno.inc}
  93. {$define INCLUDED_FROM_SYSTEM}
  94. {$I libc.pp}
  95. var
  96. {$ifdef autoHeapRelease}
  97. HeapListAllocResourceTag,
  98. {$endif}
  99. HeapAllocResourceTag : rtag_t;
  100. NLMHandle : pointer;
  101. ReleaseThreadVars : TSysReleaseThreadVars = nil;
  102. AllocateThreadVars: TSysReleaseThreadVars = nil;
  103. SetThreadDataAreaPtr : TSysSetThreadDataAreaPtr = nil;
  104. TerminatingThreadID : dword = 0;
  105. procedure NWSysSetThreadFunctions (atv:TSysReleaseThreadVars;
  106. rtv:TSysReleaseThreadVars;
  107. stdata:TSysSetThreadDataAreaPtr);
  108. begin
  109. AllocateThreadVars := atv;
  110. ReleaseThreadVars := rtv;
  111. SetThreadDataAreaPtr := stdata;
  112. end;
  113. procedure PASCALMAIN;external name 'PASCALMAIN';
  114. procedure fpc_do_exit;external name 'FPC_DO_EXIT';
  115. {*****************************************************************************
  116. System Dependent Exit code
  117. *****************************************************************************}
  118. {$ifdef autoHeapRelease}
  119. procedure FreeSbrkMem; forward;
  120. {$endif}
  121. var SigTermHandlerActive : boolean;
  122. Procedure system_exit;
  123. begin
  124. if TerminatingThreadID <> 0 then
  125. if TerminatingThreadID <> ThreadId then
  126. if TerminatingThreadID <> dword(pthread_self) then
  127. begin
  128. {$ifdef DEBUG_MT}
  129. ConsolePrintf ('Terminating Thread %x because halt was called while Thread %x terminates nlm'#13#10,dword(pthread_self),TerminatingThreadId);
  130. {$endif}
  131. pthread_exit (nil);
  132. // only for the case ExitThread fails
  133. while true do
  134. NXThreadYield;
  135. end;
  136. if assigned (ReleaseThreadVars) then ReleaseThreadVars;
  137. {$ifdef autoHeapRelease}
  138. FreeSbrkMem; { free memory allocated by heapmanager }
  139. {$endif}
  140. if not SigTermHandlerActive then
  141. begin
  142. if Erroraddr <> nil then { otherwise we dont see runtime-errors }
  143. SetScreenMode (0);
  144. _exit (ExitCode);
  145. end;
  146. end;
  147. {*****************************************************************************
  148. Stack check code
  149. *****************************************************************************}
  150. const StackErr : boolean = false;
  151. procedure int_stackcheck(stack_size:Cardinal);[public,alias:'FPC_STACKCHECK'];
  152. {
  153. called when trying to get local stack if the compiler directive $S
  154. is set this function must preserve all registers
  155. With a 5k byte safe area used to write to StdIo and some libc
  156. functions without crossing the stack boundary
  157. }
  158. begin
  159. if StackErr then exit; // avoid recursive calls
  160. asm
  161. pusha
  162. end;
  163. stackerr := (stackavail < stack_size + 5120); // we really need that much, at least on nw6.5
  164. asm
  165. popa
  166. end;
  167. if not StackErr then exit;
  168. StackErr := true;
  169. HandleError (202);
  170. end;
  171. {*****************************************************************************
  172. ParamStr/Randomize
  173. *****************************************************************************}
  174. { number of args }
  175. function paramcount : longint;
  176. begin
  177. paramcount := argc - 1;
  178. end;
  179. { argument number l }
  180. function paramstr(l : longint) : string;
  181. begin
  182. if (l>=0) and (l+1<=argc) then
  183. begin
  184. paramstr:=strpas(argv[l]);
  185. if l = 0 then // fix nlm path
  186. begin
  187. for l := 1 to length (paramstr) do
  188. if paramstr[l] = '\' then paramstr[l] := '/';
  189. end;
  190. end else
  191. paramstr:='';
  192. end;
  193. { set randseed to a new pseudo random value }
  194. procedure randomize;
  195. begin
  196. randseed := time (NIL);
  197. end;
  198. {*****************************************************************************
  199. Heap Management
  200. *****************************************************************************}
  201. {$ifdef autoHeapRelease}
  202. const HeapInitialMaxBlocks = 32;
  203. type THeapSbrkBlockList = array [1.. HeapInitialMaxBlocks] of pointer;
  204. var HeapSbrkBlockList : ^THeapSbrkBlockList = nil;
  205. HeapSbrkLastUsed : dword = 0;
  206. HeapSbrkAllocated : dword = 0;
  207. HeapSbrkReleased : boolean = false;
  208. { function to allocate size bytes more for the program }
  209. { must return the first address of new data space or nil if fail }
  210. { for netware all allocated blocks are saved to free them at }
  211. { exit (to avoid message "Module did not release xx resources") }
  212. Function SysOSAlloc(size : longint):pointer;
  213. var P2 : POINTER;
  214. i : longint;
  215. Slept : longint;
  216. begin
  217. if HeapSbrkReleased then
  218. begin
  219. ConsolePrintf ('Error: SysOSFree called after all heap memory was released'#13#10);
  220. exit(nil);
  221. end;
  222. SysOSAlloc := _Alloc (size,HeapAllocResourceTag);
  223. if SysOSAlloc <> nil then begin
  224. if HeapSbrkBlockList = nil then
  225. begin
  226. Pointer (HeapSbrkBlockList) := _Alloc (sizeof (HeapSbrkBlockList^),HeapListAllocResourceTag);
  227. if HeapSbrkBlockList = nil then
  228. begin
  229. _free (SysOSAlloc);
  230. SysOSAlloc := nil;
  231. exit;
  232. end;
  233. fillchar (HeapSbrkBlockList^,sizeof(HeapSbrkBlockList^),0);
  234. HeapSbrkAllocated := HeapInitialMaxBlocks;
  235. end;
  236. if (HeapSbrkLastUsed > 0) then
  237. for i := 1 to HeapSbrkLastUsed do
  238. if (HeapSbrkBlockList^[i] = nil) then
  239. begin // reuse free slot
  240. HeapSbrkBlockList^[i] := SysOSAlloc;
  241. exit;
  242. end;
  243. if (HeapSbrkLastUsed = HeapSbrkAllocated) then
  244. begin { grow }
  245. slept := 0;
  246. p2 := _ReallocSleepOK (HeapSbrkBlockList, (HeapSbrkAllocated + HeapInitialMaxBlocks) * sizeof(pointer),HeapListAllocResourceTag,Slept);
  247. if p2 = nil then // should we better terminate with error ?
  248. begin
  249. _free (SysOSAlloc);
  250. SysOSAlloc := nil;
  251. exit;
  252. end;
  253. HeapSbrkBlockList := p2;
  254. inc (HeapSbrkAllocated, HeapInitialMaxBlocks);
  255. end;
  256. inc (HeapSbrkLastUsed);
  257. HeapSbrkBlockList^[HeapSbrkLastUsed] := SysOSAlloc;
  258. end;
  259. end;
  260. procedure FreeSbrkMem;
  261. var i : longint;
  262. begin
  263. if HeapSbrkBlockList <> nil then
  264. begin
  265. for i := 1 to HeapSbrkLastUsed do
  266. if (HeapSbrkBlockList^[i] <> nil) then
  267. _free (HeapSbrkBlockList^[i]);
  268. _free (HeapSbrkBlockList);
  269. HeapSbrkAllocated := 0;
  270. HeapSbrkLastUsed := 0;
  271. HeapSbrkBlockList := nil;
  272. end;
  273. HeapSbrkReleased := true;
  274. {ReturnResourceTag(HeapAllocResourceTag,1);
  275. ReturnResourceTag(HeapListAllocResourceTag,1); not in netware.imp, seems to be not needed}
  276. end;
  277. {*****************************************************************************
  278. OS Memory allocation / deallocation
  279. ****************************************************************************}
  280. {$define HAS_SYSOSFREE}
  281. procedure SysOSFree(p: pointer; size: ptrint);
  282. var i : longint;
  283. begin
  284. if HeapSbrkReleased then
  285. begin
  286. ConsolePrintf ('Error: SysOSFree called after all heap memory was released'#13#10);
  287. end else
  288. if (HeapSbrkLastUsed > 0) then
  289. for i := 1 to HeapSbrkLastUsed do
  290. if (HeapSbrkBlockList^[i] = p) then
  291. begin
  292. _free (p);
  293. HeapSbrkBlockList^[i] := nil;
  294. exit;
  295. end;
  296. HandleError (204); // invalid pointer operation
  297. end;
  298. {$else autoHeapRelease}
  299. {$define HAS_SYSOSFREE}
  300. procedure SysOSFree(p: pointer; size: ptrint);
  301. begin
  302. _free (p);
  303. end;
  304. function SysOSAlloc(size: ptrint): pointer;
  305. begin
  306. SysOSAlloc := _Alloc(size,HeapAllocResourceTag);
  307. end;
  308. {$endif autoHeapRelease}
  309. { include standard heap management }
  310. {$I heap.inc}
  311. {****************************************************************************
  312. Low level File Routines
  313. All these functions can set InOutRes on errors
  314. ****************************************************************************}
  315. PROCEDURE NW2PASErr (Err : LONGINT);
  316. BEGIN
  317. if Err = 0 then { Else it will go through all the cases }
  318. exit;
  319. case Err of
  320. Sys_ENFILE,
  321. Sys_EMFILE : Inoutres:=4;
  322. Sys_ENOENT : Inoutres:=2;
  323. Sys_EBADF : Inoutres:=6;
  324. Sys_ENOMEM,
  325. Sys_EFAULT : Inoutres:=217;
  326. Sys_EINVAL : Inoutres:=218;
  327. Sys_EPIPE,
  328. Sys_EINTR,
  329. Sys_EIO,
  330. Sys_EAGAIN,
  331. Sys_ENOSPC : Inoutres:=101;
  332. Sys_ENAMETOOLONG,
  333. Sys_ELOOP,
  334. Sys_ENOTDIR : Inoutres:=3;
  335. Sys_EROFS,
  336. Sys_EEXIST,
  337. Sys_EACCES : Inoutres:=5;
  338. Sys_EBUSY : Inoutres:=162
  339. else begin
  340. Writeln (stderr,'NW2PASErr: unknown error ',err);
  341. libc_perror('NW2PASErr');
  342. Inoutres := Err;
  343. end;
  344. end;
  345. END;
  346. procedure Errno2Inoutres;
  347. begin
  348. NW2PASErr (___errno^);
  349. end;
  350. procedure SetFileError (VAR Err : LONGINT);
  351. begin
  352. if Err >= 0 then
  353. InOutRes := 0
  354. else begin
  355. // libc_perror ('SetFileError');
  356. Err := ___errno^;
  357. NW2PASErr (Err);
  358. Err := 0;
  359. end;
  360. end;
  361. { close a file from the handle value }
  362. procedure do_close(handle : thandle);
  363. VAR res : LONGINT;
  364. begin
  365. {$ifdef IOpossix}
  366. res := FpClose (handle);
  367. {$else}
  368. res := _fclose (_TFILE(handle));
  369. {$endif}
  370. IF res <> 0 THEN
  371. SetFileError (res)
  372. ELSE
  373. InOutRes := 0;
  374. end;
  375. procedure do_erase(p : pchar);
  376. VAR res : LONGINT;
  377. begin
  378. res := unlink (p);
  379. IF Res < 0 THEN
  380. SetFileError (res)
  381. ELSE
  382. InOutRes := 0;
  383. end;
  384. procedure do_rename(p1,p2 : pchar);
  385. VAR res : LONGINT;
  386. begin
  387. res := rename (p1,p2);
  388. IF Res < 0 THEN
  389. SetFileError (res)
  390. ELSE
  391. InOutRes := 0
  392. end;
  393. function do_write(h:thandle;addr:pointer;len : longint) : longint;
  394. var res : LONGINT;
  395. begin
  396. {$ifdef IOpossix}
  397. res := Fpwrite (h,addr,len);
  398. {$else}
  399. res := _fwrite (addr,1,len,_TFILE(h));
  400. {$endif}
  401. if res > 0 then
  402. InOutRes := 0
  403. else
  404. SetFileError (res);
  405. do_write := res;
  406. NXThreadYield;
  407. end;
  408. function do_read(h:thandle;addr:pointer;len : longint) : longint;
  409. VAR res : LONGINT;
  410. begin
  411. {$ifdef IOpossix}
  412. res := Fpread (h,addr,len);
  413. {$else}
  414. res := _fread (addr,1,len,_TFILE(h));
  415. {$endif}
  416. IF res > 0 THEN
  417. InOutRes := 0
  418. ELSE
  419. SetFileError (res);
  420. do_read := res;
  421. NXThreadYield;
  422. end;
  423. function do_filepos(handle : thandle) : longint;
  424. var res : LONGINT;
  425. begin
  426. InOutRes:=1;
  427. {$ifdef IOpossix}
  428. res := Fptell (handle);
  429. {$else}
  430. res := _ftell (_TFILE(handle));
  431. {$endif}
  432. if res < 0 THEN
  433. SetFileError (res)
  434. else
  435. InOutRes := 0;
  436. do_filepos := res;
  437. end;
  438. procedure do_seek(handle:thandle;pos : longint);
  439. VAR res : LONGINT;
  440. begin
  441. {$ifdef IOpossix}
  442. res := Fplseek (handle,pos, SEEK_SET);
  443. {$else}
  444. res := _fseek (_TFILE(handle),pos, SEEK_SET);
  445. {$endif}
  446. IF res >= 0 THEN
  447. InOutRes := 0
  448. ELSE
  449. SetFileError (res);
  450. end;
  451. function do_seekend(handle:thandle):longint;
  452. VAR res : LONGINT;
  453. begin
  454. {$ifdef IOpossix}
  455. res := Fplseek (handle,0, SEEK_END);
  456. {$else}
  457. res := _fseek (_TFILE(handle),0, SEEK_END);
  458. {$endif}
  459. IF res >= 0 THEN
  460. InOutRes := 0
  461. ELSE
  462. SetFileError (res);
  463. do_seekend := res;
  464. end;
  465. function do_filesize(handle : thandle) : longint;
  466. VAR res : LONGINT;
  467. statbuf : TStat;
  468. begin
  469. {$ifdef IOpossix}
  470. res := Fpfstat (handle, statbuf);
  471. {$else}
  472. res := _fstat (_fileno (_TFILE(handle)), statbuf); // was _filelength for clib
  473. {$endif}
  474. if res <> 0 then
  475. begin
  476. SetFileError (Res);
  477. do_filesize := -1;
  478. end else
  479. begin
  480. InOutRes := 0;
  481. do_filesize := statbuf.st_size;
  482. end;
  483. end;
  484. { truncate at a given position }
  485. procedure do_truncate (handle:thandle;pos:longint);
  486. VAR res : LONGINT;
  487. begin
  488. {$ifdef IOpossix}
  489. res := ftruncate (handle,pos);
  490. {$else}
  491. res := _ftruncate (_fileno (_TFILE(handle)),pos);
  492. {$endif}
  493. IF res <> 0 THEN
  494. SetFileError (res)
  495. ELSE
  496. InOutRes := 0;
  497. end;
  498. {$ifdef IOpossix}
  499. // mostly stolen from syslinux
  500. procedure do_open(var f;p:pchar;flags:longint);
  501. {
  502. filerec and textrec have both handle and mode as the first items so
  503. they could use the same routine for opening/creating.
  504. when (flags and $10) the file will be append
  505. when (flags and $100) the file will be truncate/rewritten
  506. when (flags and $1000) there is no check for close (needed for textfiles)
  507. }
  508. var
  509. oflags : longint;
  510. Begin
  511. { close first if opened }
  512. if ((flags and $10000)=0) then
  513. begin
  514. case FileRec(f).mode of
  515. fminput,fmoutput,fminout : Do_Close(FileRec(f).Handle);
  516. fmclosed : ;
  517. else
  518. begin
  519. inoutres:=102; {not assigned}
  520. exit;
  521. end;
  522. end;
  523. end;
  524. { reset file Handle }
  525. FileRec(f).Handle:=UnusedHandle;
  526. { We do the conversion of filemodes here, concentrated on 1 place }
  527. case (flags and 3) of
  528. 0 : begin
  529. oflags := O_RDONLY;
  530. filerec(f).mode := fminput;
  531. end;
  532. 1 : begin
  533. oflags := O_WRONLY;
  534. filerec(f).mode := fmoutput;
  535. end;
  536. 2 : begin
  537. oflags := O_RDWR;
  538. filerec(f).mode := fminout;
  539. end;
  540. end;
  541. if (flags and $1000)=$1000 then
  542. oflags:=oflags or (O_CREAT or O_TRUNC)
  543. else
  544. if (flags and $100)=$100 then
  545. oflags:=oflags or (O_APPEND);
  546. { empty name is special }
  547. if p[0]=#0 then
  548. begin
  549. case FileRec(f).mode of
  550. fminput :
  551. FileRec(f).Handle:=StdInputHandle;
  552. fminout, { this is set by rewrite }
  553. fmoutput :
  554. FileRec(f).Handle:=StdOutputHandle;
  555. fmappend :
  556. begin
  557. FileRec(f).Handle:=StdOutputHandle;
  558. FileRec(f).mode:=fmoutput; {fool fmappend}
  559. end;
  560. end;
  561. exit;
  562. end;
  563. { real open call }
  564. ___errno^ := 0;
  565. FileRec(f).Handle := open(p,oflags,438);
  566. { open somtimes returns > -1 but errno was set }
  567. if (___errno^ <> 0) or (longint(FileRec(f).Handle) < 0) then
  568. if (___errno^=Sys_EROFS) and ((OFlags and O_RDWR)<>0) then
  569. begin // i.e. for cd-rom
  570. Oflags:=Oflags and not(O_RDWR);
  571. FileRec(f).Handle := open(p,oflags,438);
  572. end;
  573. if (___errno^ <> 0) or (longint(FileRec(f).Handle) < 0) then
  574. Errno2Inoutres
  575. else
  576. InOutRes := 0;
  577. end;
  578. {$else}
  579. procedure do_open(var f;p:pchar;flags:longint);
  580. {
  581. filerec and textrec have both handle and mode as the first items so
  582. they could use the same routine for opening/creating.
  583. when (flags and $10) the file will be append
  584. when (flags and $100) the file will be truncate/rewritten
  585. when (flags and $1000) there is no check for close (needed for textfiles)
  586. }
  587. var
  588. oflags : string[10];
  589. Begin
  590. { close first if opened }
  591. if ((flags and $10000)=0) then
  592. begin
  593. case FileRec(f).mode of
  594. fminput,fmoutput,fminout : Do_Close(FileRec(f).Handle);
  595. fmclosed : ;
  596. else
  597. begin
  598. inoutres:=102; {not assigned}
  599. exit;
  600. end;
  601. end;
  602. end;
  603. { reset file Handle }
  604. FileRec(f).Handle:=UnusedHandle;
  605. { We do the conversion of filemodes here, concentrated on 1 place }
  606. case (flags and 3) of
  607. 0 : begin
  608. oflags := 'rb'#0;
  609. filerec(f).mode := fminput;
  610. end;
  611. 1 : begin
  612. if (flags and $1000)=$1000 then
  613. oflags := 'w+b' else
  614. oflags := 'wb';
  615. filerec(f).mode := fmoutput;
  616. end;
  617. 2 : begin
  618. if (flags and $1000)=$1000 then
  619. oflags := 'w+' else
  620. oflags := 'r+';
  621. filerec(f).mode := fminout;
  622. end;
  623. end;
  624. {if (flags and $1000)=$1000 then
  625. oflags:=oflags or (O_CREAT or O_TRUNC)
  626. else
  627. if (flags and $100)=$100 then
  628. oflags:=oflags or (O_APPEND);}
  629. { empty name is special }
  630. if p[0]=#0 then
  631. begin
  632. case FileRec(f).mode of
  633. fminput :
  634. FileRec(f).Handle:=StdInputHandle;
  635. fminout, { this is set by rewrite }
  636. fmoutput :
  637. FileRec(f).Handle:=StdOutputHandle;
  638. fmappend :
  639. begin
  640. FileRec(f).Handle:=StdOutputHandle;
  641. FileRec(f).mode:=fmoutput; {fool fmappend}
  642. end;
  643. end;
  644. exit;
  645. end;
  646. { real open call }
  647. FileRec(f).Handle := THandle (_fopen (p,@oflags[1]));//_open(p,oflags,438);
  648. //WriteLn ('_open (',p,') returned ',ErrNo, 'Handle: ',FileRec(f).Handle);
  649. // errno does not seem to be set on succsess ??
  650. {IF FileRec(f).Handle < 0 THEN
  651. if (ErrNo=Sys_EROFS) and ((OFlags and O_RDWR)<>0) then
  652. begin // i.e. for cd-rom
  653. Oflags:=Oflags and not(O_RDWR);
  654. FileRec(f).Handle := _open(p,oflags,438);
  655. end;}
  656. if FileRec(f).Handle = 0 then
  657. Errno2Inoutres
  658. else
  659. InOutRes := 0;
  660. End;
  661. {$endif}
  662. function do_isdevice(handle:THandle):boolean;
  663. begin
  664. {$ifdef IOpossix}
  665. do_isdevice := (Fpisatty (handle) > 0);
  666. {$else}
  667. do_isdevice := (isatty (_fileno(_TFILE(handle))) > 0);
  668. {$endif}
  669. end;
  670. {*****************************************************************************
  671. UnTyped File Handling
  672. *****************************************************************************}
  673. {$i file.inc}
  674. {*****************************************************************************
  675. Typed File Handling
  676. *****************************************************************************}
  677. {$i typefile.inc}
  678. {*****************************************************************************
  679. Text File Handling
  680. *****************************************************************************}
  681. {$i text.inc}
  682. {*****************************************************************************
  683. Directory Handling
  684. *****************************************************************************}
  685. procedure mkdir(const s : string);[IOCheck];
  686. var S2 : STRING;
  687. Res: LONGINT;
  688. BEGIN
  689. S2 := S;
  690. IF Length (S2) = 255 THEN DEC (BYTE(S2[0]));
  691. S2 := S2 + #0;
  692. Res := FpMkdir (@S2[1],S_IRWXU);
  693. if Res = 0 then
  694. InOutRes:=0
  695. else
  696. SetFileError (Res);
  697. end;
  698. procedure rmdir(const s : string);[IOCheck];
  699. VAR S2 : STRING;
  700. Res: LONGINT;
  701. BEGIN
  702. S2 := S;
  703. IF Length (S2) = 255 THEN DEC (BYTE(S2[0]));
  704. S2 := S2 + #0;
  705. Res := FpRmdir (@S2[1]);
  706. IF Res = 0 THEN
  707. InOutRes:=0
  708. ELSE
  709. SetFileError (Res);
  710. end;
  711. procedure chdir(const s : string);[IOCheck];
  712. VAR S2 : STRING;
  713. Res: LONGINT;
  714. begin
  715. S2 := S;
  716. IF Length (S2) = 255 THEN DEC (BYTE(S2[0]));
  717. S2 := S2 + #0;
  718. Res := FpChdir (@S2[1]);
  719. IF Res = 0 THEN
  720. InOutRes:=0
  721. ELSE
  722. SetFileError (Res);
  723. end;
  724. procedure getdir(drivenr : byte;var dir : shortstring);
  725. var P : array [0..255] of CHAR;
  726. i : LONGINT;
  727. begin
  728. P[0] := #0;
  729. getcwdpath(@P,nil,0); // getcwd does not return volume, getcwdpath does
  730. i := libc_strlen (P);
  731. if i > 0 then
  732. begin
  733. Move (P, dir[1], i);
  734. BYTE(dir[0]) := i;
  735. For i := 1 to length (dir) do
  736. if dir[i] = '\' then dir [i] := '/';
  737. // fix / after volume, the compiler needs that
  738. // normaly root of a volumes is SERVERNAME/SYS:, change that
  739. // to SERVERNAME/SYS:/
  740. i := pos (':',dir);
  741. if (i > 0) then
  742. if i = Length (dir) then dir := dir + '/' else
  743. if dir [i+1] <> '/' then insert ('/',dir,i+1);
  744. end else
  745. InOutRes := 1;
  746. end;
  747. {*****************************************************************************
  748. Thread Handling
  749. *****************************************************************************}
  750. procedure InitFPU;assembler;
  751. asm
  752. fninit
  753. fldcw fpucw
  754. end;
  755. { if return-value is <> 0, netware shows the message
  756. Unload Anyway ?
  757. To Disable unload at all, SetNLMDontUnloadFlag can be used on
  758. Netware >= 4.0 }
  759. function CheckFunction : longint; CDECL; [public,alias: '_NonAppCheckUnload'];
  760. var oldPtr : pointer;
  761. begin
  762. //ConsolePrintf ('CheckFunction'#13#10);
  763. if assigned (NetwareCheckFunction) then
  764. begin
  765. if assigned (SetThreadDataAreaPtr) then
  766. oldPtr := SetThreadDataAreaPtr (NIL); { nil means main thread }
  767. result := 0;
  768. NetwareCheckFunction (result);
  769. if assigned (SetThreadDataAreaPtr) then
  770. SetThreadDataAreaPtr (oldPtr);
  771. end else
  772. result := 0;
  773. end;
  774. procedure ConsolePrintf (s : shortstring);
  775. begin
  776. if length(s) > 254 then
  777. byte(s[0]) := 254;
  778. s := s + #0;
  779. ConsolePrintf (@s[1]);
  780. end;
  781. procedure ConsolePrintf (FormatStr : PCHAR);
  782. begin
  783. if NWLoggerScreen = nil then
  784. NWLoggerScreen := getnetwarelogger;
  785. if NWLoggerScreen <> nil then
  786. screenprintf (NWLoggerScreen,FormatStr);
  787. end;
  788. procedure ConsolePrintf (FormatStr : PCHAR; Param : LONGINT);
  789. begin
  790. if NWLoggerScreen = nil then
  791. NWLoggerScreen := getnetwarelogger;
  792. if NWLoggerScreen <> nil then
  793. screenprintf (NWLoggerScreen,FormatStr,Param);
  794. end;
  795. procedure ConsolePrintf (FormatStr : PCHAR; Param : pchar);
  796. begin
  797. ConsolePrintf (FormatStr,longint(Param));
  798. end;
  799. procedure ConsolePrintf (FormatStr : PCHAR; P1,P2 : LONGINT);
  800. begin
  801. if NWLoggerScreen = nil then
  802. NWLoggerScreen := getnetwarelogger;
  803. if NWLoggerScreen <> nil then
  804. screenprintf (NWLoggerScreen,FormatStr,P1,P2);
  805. end;
  806. procedure ConsolePrintf (FormatStr : PCHAR; P1,P2,P3 : LONGINT);
  807. begin
  808. if NWLoggerScreen = nil then
  809. NWLoggerScreen := getnetwarelogger;
  810. if NWLoggerScreen <> nil then
  811. screenprintf (NWLoggerScreen,FormatStr,P1,P2,P3);
  812. end;
  813. procedure __EnterDebugger;cdecl;external '!netware' name 'EnterDebugger';
  814. var NWUts : Tutsname;
  815. procedure getCodeAddresses;
  816. begin
  817. if Fpuname(NWUts) < 0 then
  818. FillChar(NWuts,sizeof(NWUts),0);
  819. end;
  820. function NWGetCodeStart : pointer;
  821. begin
  822. NWGetCodeStart := NWUts.codeoffset;
  823. NXThreadYield;
  824. end;
  825. function NWGetCodeLength : dword;
  826. begin
  827. NWGetCodeLength := NWUts.codelength;
  828. NXThreadYield;
  829. end;
  830. function NWGetDataStart : pointer;
  831. begin
  832. NWGetDataStart := NWUts.dataoffset;
  833. NXThreadYield;
  834. end;
  835. function NWGetDataLength : dword;
  836. begin
  837. NWGetDataLength := NWUts.datalength;
  838. NXThreadYield;
  839. end;
  840. {$ifdef StdErrToConsole}
  841. var ConsoleBuff : array [0..512] of char;
  842. Function ConsoleWrite(Var F: TextRec): Integer;
  843. var
  844. i : longint;
  845. Begin
  846. if F.BufPos>0 then
  847. begin
  848. if F.BufPos>sizeof(ConsoleBuff)-1 then
  849. i:=sizeof(ConsoleBuff)-1
  850. else
  851. i:=F.BufPos;
  852. Move(F.BufPtr^,ConsoleBuff,i);
  853. ConsoleBuff[i] := #0;
  854. screenprintf (NWLoggerScreen,@ConsoleBuff);
  855. end;
  856. F.BufPos:=0;
  857. ConsoleWrite := 0;
  858. NXThreadYield;
  859. End;
  860. Function ConsoleClose(Var F: TextRec): Integer;
  861. begin
  862. ConsoleClose:=0;
  863. end;
  864. Function ConsoleOpen(Var F: TextRec): Integer;
  865. Begin
  866. TextRec(F).InOutFunc:=@ConsoleWrite;
  867. TextRec(F).FlushFunc:=@ConsoleWrite;
  868. TextRec(F).CloseFunc:=@ConsoleClose;
  869. ConsoleOpen:=0;
  870. End;
  871. procedure AssignStdErrConsole(Var T: Text);
  872. begin
  873. Assign(T,'');
  874. TextRec(T).OpenFunc:=@ConsoleOpen;
  875. Rewrite(T);
  876. end;
  877. {$endif}
  878. { this will be called if the nlm is unloaded. It will NOT be
  879. called if the program exits i.e. with halt.
  880. Halt (or _exit) can not be called from this callback procedure }
  881. procedure TermSigHandler (Sig:longint); CDecl;
  882. var oldPtr : pointer;
  883. current_exit : procedure;
  884. begin
  885. { Threadvar Pointer will not be valid because the signal
  886. handler is called by netware with a differnt thread. To avoid
  887. problems in the exit routines, we set the data of the main thread
  888. here }
  889. if assigned (SetThreadDataAreaPtr) then
  890. oldPtr := SetThreadDataAreaPtr (NIL); { nil means main thread }
  891. TerminatingThreadID := dword(pthread_self);
  892. {we need to finalize winock to release threads
  893. waiting on a blocking socket call. If that thread
  894. calls halt, we have to avoid that unit finalization
  895. is called by that thread because we are doing it
  896. here
  897. like the old exitProc, mainly to allow winsock to release threads
  898. blocking in a winsock calls }
  899. while NetwareUnloadProc<>nil Do
  900. Begin
  901. InOutRes:=0;
  902. current_exit:=tProcedure(NetwareUnloadProc);
  903. NetwareUnloadProc:=nil;
  904. current_exit();
  905. NXThreadYield;
  906. //hadExitProc := true;
  907. End;
  908. SigTermHandlerActive := true; { to avoid that system_exit calls _exit }
  909. do_exit; { calls finalize units }
  910. if assigned (SetThreadDataAreaPtr) then
  911. SetThreadDataAreaPtr (oldPtr);
  912. end;
  913. procedure SysInitStdIO;
  914. begin
  915. { Setup stdin, stdout and stderr }
  916. {$ifdef IOpossix}
  917. StdInputHandle := THandle (fileno (___stdin^)); // GetStd** returns **FILE !
  918. StdOutputHandle:= THandle (fileno (___stdout^));
  919. StdErrorHandle := THandle (fileno (___stderr^));
  920. {$else}
  921. StdInputHandle := THandle (___stdin^); // GetStd** returns **FILE !
  922. StdOutputHandle:= THandle (___stdout^);
  923. StdErrorHandle := THandle (___stderr^);
  924. {$endif}
  925. OpenStdIO(Input,fmInput,StdInputHandle);
  926. OpenStdIO(Output,fmOutput,StdOutputHandle);
  927. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  928. {$ifdef StdErrToConsole}
  929. AssignStdErrConsole(StdErr);
  930. AssignStdErrConsole(ErrOutput);
  931. {$else}
  932. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  933. OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
  934. {$endif}
  935. end;
  936. // this is called by main.as, setup args and call PASCALMAIN
  937. procedure nlm_main (_ArgC : LONGINT; _ArgV : ppchar); cdecl; [public,alias: '_FPC_NLM_Entry'];
  938. BEGIN
  939. ArgC := _ArgC;
  940. ArgV := _ArgV;
  941. isLibrary := false;
  942. PASCALMAIN;
  943. do_exit; // currently not needed
  944. END;
  945. function _DLLMain (hInstDLL:pointer; fdwReason:dword; DLLParam:longint):longbool; cdecl;
  946. [public, alias : '_FPC_DLL_Entry'];
  947. var res : longbool;
  948. begin
  949. {$ifdef DEBUG_MT}
  950. ConsolePrintf ('_FPC_DLL_Entry called');
  951. {$endif}
  952. _DLLMain := false;
  953. isLibrary := true;
  954. case fdwReason of
  955. DLL_ACTUAL_DLLMAIN : _DLLMain := true;
  956. DLL_NLM_STARTUP : begin
  957. //__ConsolePrintf ('DLL_NLM_STARTUP');
  958. if assigned(Dll_Process_Attach_Hook) then
  959. begin
  960. res:=Dll_Process_Attach_Hook(DllParam);
  961. if not res then
  962. exit(false);
  963. end;
  964. PASCALMAIN;
  965. _DLLMain := true;
  966. end;
  967. DLL_NLM_SHUTDOWN : begin
  968. //__ConsolePrintf ('DLL_NLM_SHUTDOWN');
  969. TermSigHandler(0);
  970. _DLLMain := true;
  971. end;
  972. { standard DllMain() messages... }
  973. DLL_THREAD_ATTACH,
  974. DLL_PROCESS_ATTACH : begin
  975. //__ConsolePrintf ('DLL_PROCESS/THREAD_ATTACH');
  976. if assigned(AllocateThreadVars) then
  977. AllocateThreadVars;
  978. if assigned(Dll_Thread_Attach_Hook) then
  979. Dll_Thread_Attach_Hook(DllParam);
  980. _DLLMain := true;
  981. end;
  982. DLL_THREAD_DETACH,
  983. DLL_PROCESS_DETACH : begin
  984. //__ConsolePrintf ('DLL_PROCESS/THREAD_DETACH');
  985. if assigned(Dll_Thread_Detach_Hook) then
  986. Dll_Thread_Detach_Hook(DllParam);
  987. if assigned(ReleaseThreadVars) then
  988. ReleaseThreadVars;
  989. _DLLMain := true;
  990. end;
  991. end;
  992. end;
  993. {*****************************************************************************
  994. SystemUnit Initialization
  995. *****************************************************************************}
  996. Begin
  997. getCodeAddresses;
  998. StackBottom := SPtr - StackLength;
  999. SigTermHandlerActive := false;
  1000. NetwareCheckFunction := nil;
  1001. {$ifdef StdErrToConsole}
  1002. NWLoggerScreen := getnetwarelogger;
  1003. {$endif}
  1004. CheckFunction; // avoid check function to be removed by the linker
  1005. envp := ____environ^;
  1006. NLMHandle := getnlmhandle;
  1007. { allocate resource tags to see what kind of memory i forgot to release }
  1008. HeapAllocResourceTag :=
  1009. AllocateResourceTag(NLMHandle,'Heap Memory',AllocSignature);
  1010. {$ifdef autoHeapRelease}
  1011. HeapListAllocResourceTag :=
  1012. AllocateResourceTag(NLMHandle,'Heap Memory List',AllocSignature);
  1013. {$endif}
  1014. FpSignal (SIGTERM, @TermSigHandler);
  1015. { Setup heap }
  1016. InitHeap;
  1017. SysInitExceptions;
  1018. { Reset IO Error }
  1019. InOutRes:=0;
  1020. ThreadID := dword(pthread_self);
  1021. SysInitStdIO;
  1022. {Delphi Compatible}
  1023. IsConsole := TRUE;
  1024. ExitCode := 0;
  1025. {$ifdef HASVARIANT}
  1026. initvariantmanager;
  1027. {$endif HASVARIANT}
  1028. End.
  1029. {
  1030. $Log$
  1031. Revision 1.7 2004-11-04 09:32:31 peter
  1032. ErrOutput added
  1033. Revision 1.6 2004/10/25 18:11:49 armin
  1034. * saveregisters no longer supported by compiler, for now save all registers in stackcheck
  1035. Revision 1.5 2004/10/25 15:38:59 peter
  1036. * compiler defined HEAP and HEAPSIZE removed
  1037. Revision 1.4 2004/09/26 19:23:34 armin
  1038. * exiting threads at nlm unload
  1039. * renamed some libc functions
  1040. Revision 1.3 2004/09/19 20:06:37 armin
  1041. * removed get/free video buf from video.pp
  1042. * implemented sockets
  1043. * basic library support
  1044. * threadvar memory leak removed
  1045. * fixes (ide now starts and editor is usable)
  1046. * support for lineinfo
  1047. Revision 1.2 2004/09/12 20:51:22 armin
  1048. * added keyboard and video
  1049. * a lot of fixes
  1050. Revision 1.1 2004/09/05 20:58:47 armin
  1051. * first rtl version for netwlibc
  1052. }