system.pp 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228
  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;external '!netware' name 'EnterDebugger';
  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. var NWUts : Tutsname;
  814. procedure getCodeAddresses;
  815. begin
  816. if Fpuname(NWUts) < 0 then
  817. FillChar(NWuts,sizeof(NWUts),0);
  818. end;
  819. function NWGetCodeStart : pointer;
  820. begin
  821. NWGetCodeStart := NWUts.codeoffset;
  822. NXThreadYield;
  823. end;
  824. function NWGetCodeLength : dword;
  825. begin
  826. NWGetCodeLength := NWUts.codelength;
  827. NXThreadYield;
  828. end;
  829. function NWGetDataStart : pointer;
  830. begin
  831. NWGetDataStart := NWUts.dataoffset;
  832. NXThreadYield;
  833. end;
  834. function NWGetDataLength : dword;
  835. begin
  836. NWGetDataLength := NWUts.datalength;
  837. NXThreadYield;
  838. end;
  839. {$ifdef StdErrToConsole}
  840. var ConsoleBuff : array [0..512] of char;
  841. Function ConsoleWrite(Var F: TextRec): Integer;
  842. var
  843. i : longint;
  844. Begin
  845. if F.BufPos>0 then
  846. begin
  847. if F.BufPos>sizeof(ConsoleBuff)-1 then
  848. i:=sizeof(ConsoleBuff)-1
  849. else
  850. i:=F.BufPos;
  851. Move(F.BufPtr^,ConsoleBuff,i);
  852. ConsoleBuff[i] := #0;
  853. screenprintf (NWLoggerScreen,@ConsoleBuff);
  854. end;
  855. F.BufPos:=0;
  856. ConsoleWrite := 0;
  857. NXThreadYield;
  858. End;
  859. Function ConsoleClose(Var F: TextRec): Integer;
  860. begin
  861. ConsoleClose:=0;
  862. end;
  863. Function ConsoleOpen(Var F: TextRec): Integer;
  864. Begin
  865. TextRec(F).InOutFunc:=@ConsoleWrite;
  866. TextRec(F).FlushFunc:=@ConsoleWrite;
  867. TextRec(F).CloseFunc:=@ConsoleClose;
  868. ConsoleOpen:=0;
  869. End;
  870. procedure AssignStdErrConsole(Var T: Text);
  871. begin
  872. Assign(T,'');
  873. TextRec(T).OpenFunc:=@ConsoleOpen;
  874. Rewrite(T);
  875. end;
  876. {$endif}
  877. function GetProcessID: SizeUInt;
  878. begin
  879. {$WARNING GetProcessID implementation missing}
  880. GetProcessID := 1;
  881. end;
  882. { this will be called if the nlm is unloaded. It will NOT be
  883. called if the program exits i.e. with halt.
  884. Halt (or _exit) can not be called from this callback procedure }
  885. procedure TermSigHandler (Sig:longint); CDecl;
  886. var oldPtr : pointer;
  887. current_exit : procedure;
  888. begin
  889. { Threadvar Pointer will not be valid because the signal
  890. handler is called by netware with a differnt thread. To avoid
  891. problems in the exit routines, we set the data of the main thread
  892. here }
  893. if assigned (SetThreadDataAreaPtr) then
  894. oldPtr := SetThreadDataAreaPtr (NIL); { nil means main thread }
  895. TerminatingThreadID := dword(pthread_self);
  896. {we need to finalize winock to release threads
  897. waiting on a blocking socket call. If that thread
  898. calls halt, we have to avoid that unit finalization
  899. is called by that thread because we are doing it
  900. here
  901. like the old exitProc, mainly to allow winsock to release threads
  902. blocking in a winsock calls }
  903. while NetwareUnloadProc<>nil Do
  904. Begin
  905. InOutRes:=0;
  906. current_exit:=tProcedure(NetwareUnloadProc);
  907. NetwareUnloadProc:=nil;
  908. current_exit();
  909. NXThreadYield;
  910. //hadExitProc := true;
  911. End;
  912. SigTermHandlerActive := true; { to avoid that system_exit calls _exit }
  913. do_exit; { calls finalize units }
  914. if assigned (SetThreadDataAreaPtr) then
  915. SetThreadDataAreaPtr (oldPtr);
  916. end;
  917. procedure SysInitStdIO;
  918. begin
  919. { Setup stdin, stdout and stderr }
  920. {$ifdef IOpossix}
  921. StdInputHandle := THandle (fileno (___stdin^)); // GetStd** returns **FILE !
  922. StdOutputHandle:= THandle (fileno (___stdout^));
  923. StdErrorHandle := THandle (fileno (___stderr^));
  924. {$else}
  925. StdInputHandle := THandle (___stdin^); // GetStd** returns **FILE !
  926. StdOutputHandle:= THandle (___stdout^);
  927. StdErrorHandle := THandle (___stderr^);
  928. {$endif}
  929. OpenStdIO(Input,fmInput,StdInputHandle);
  930. OpenStdIO(Output,fmOutput,StdOutputHandle);
  931. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  932. {$ifdef StdErrToConsole}
  933. AssignStdErrConsole(StdErr);
  934. AssignStdErrConsole(ErrOutput);
  935. {$else}
  936. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  937. OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
  938. {$endif}
  939. end;
  940. // this is called by main.as, setup args and call PASCALMAIN
  941. procedure nlm_main (_ArgC : LONGINT; _ArgV : ppchar); cdecl; [public,alias: '_FPC_NLM_Entry'];
  942. BEGIN
  943. ArgC := _ArgC;
  944. ArgV := _ArgV;
  945. isLibrary := false;
  946. PASCALMAIN;
  947. do_exit; // currently not needed
  948. END;
  949. function _DLLMain (hInstDLL:pointer; fdwReason:dword; DLLParam:longint):longbool; cdecl;
  950. [public, alias : '_FPC_DLL_Entry'];
  951. var res : longbool;
  952. begin
  953. {$ifdef DEBUG_MT}
  954. ConsolePrintf ('_FPC_DLL_Entry called');
  955. {$endif}
  956. _DLLMain := false;
  957. isLibrary := true;
  958. case fdwReason of
  959. DLL_ACTUAL_DLLMAIN : _DLLMain := true;
  960. DLL_NLM_STARTUP : begin
  961. //__ConsolePrintf ('DLL_NLM_STARTUP');
  962. if assigned(Dll_Process_Attach_Hook) then
  963. begin
  964. res:=Dll_Process_Attach_Hook(DllParam);
  965. if not res then
  966. exit(false);
  967. end;
  968. PASCALMAIN;
  969. _DLLMain := true;
  970. end;
  971. DLL_NLM_SHUTDOWN : begin
  972. //__ConsolePrintf ('DLL_NLM_SHUTDOWN');
  973. TermSigHandler(0);
  974. _DLLMain := true;
  975. end;
  976. { standard DllMain() messages... }
  977. DLL_THREAD_ATTACH,
  978. DLL_PROCESS_ATTACH : begin
  979. //__ConsolePrintf ('DLL_PROCESS/THREAD_ATTACH');
  980. if assigned(AllocateThreadVars) then
  981. AllocateThreadVars;
  982. if assigned(Dll_Thread_Attach_Hook) then
  983. Dll_Thread_Attach_Hook(DllParam);
  984. _DLLMain := true;
  985. end;
  986. DLL_THREAD_DETACH,
  987. DLL_PROCESS_DETACH : begin
  988. //__ConsolePrintf ('DLL_PROCESS/THREAD_DETACH');
  989. if assigned(Dll_Thread_Detach_Hook) then
  990. Dll_Thread_Detach_Hook(DllParam);
  991. if assigned(ReleaseThreadVars) then
  992. ReleaseThreadVars;
  993. _DLLMain := true;
  994. end;
  995. end;
  996. end;
  997. {*****************************************************************************
  998. SystemUnit Initialization
  999. *****************************************************************************}
  1000. Begin
  1001. getCodeAddresses;
  1002. StackBottom := SPtr - StackLength;
  1003. SigTermHandlerActive := false;
  1004. NetwareCheckFunction := nil;
  1005. {$ifdef StdErrToConsole}
  1006. NWLoggerScreen := getnetwarelogger;
  1007. {$endif}
  1008. CheckFunction; // avoid check function to be removed by the linker
  1009. envp := ____environ^;
  1010. NLMHandle := getnlmhandle;
  1011. { allocate resource tags to see what kind of memory i forgot to release }
  1012. HeapAllocResourceTag :=
  1013. AllocateResourceTag(NLMHandle,'Heap Memory',AllocSignature);
  1014. {$ifdef autoHeapRelease}
  1015. HeapListAllocResourceTag :=
  1016. AllocateResourceTag(NLMHandle,'Heap Memory List',AllocSignature);
  1017. {$endif}
  1018. FpSignal (SIGTERM, @TermSigHandler);
  1019. { Setup heap }
  1020. InitHeap;
  1021. SysInitExceptions;
  1022. { Reset IO Error }
  1023. InOutRes:=0;
  1024. ThreadID := dword(pthread_self);
  1025. SysInitStdIO;
  1026. {Delphi Compatible}
  1027. IsConsole := TRUE;
  1028. ExitCode := 0;
  1029. {$ifdef HASVARIANT}
  1030. initvariantmanager;
  1031. {$endif HASVARIANT}
  1032. End.
  1033. {
  1034. $Log$
  1035. Revision 1.9 2004-12-05 14:36:38 hajny
  1036. + GetProcessID added
  1037. Revision 1.8 2004/11/25 12:38:17 armin
  1038. * adapted to new compiler check for externals
  1039. Revision 1.7 2004/11/04 09:32:31 peter
  1040. ErrOutput added
  1041. Revision 1.6 2004/10/25 18:11:49 armin
  1042. * saveregisters no longer supported by compiler, for now save all registers in stackcheck
  1043. Revision 1.5 2004/10/25 15:38:59 peter
  1044. * compiler defined HEAP and HEAPSIZE removed
  1045. Revision 1.4 2004/09/26 19:23:34 armin
  1046. * exiting threads at nlm unload
  1047. * renamed some libc functions
  1048. Revision 1.3 2004/09/19 20:06:37 armin
  1049. * removed get/free video buf from video.pp
  1050. * implemented sockets
  1051. * basic library support
  1052. * threadvar memory leak removed
  1053. * fixes (ide now starts and editor is usable)
  1054. * support for lineinfo
  1055. Revision 1.2 2004/09/12 20:51:22 armin
  1056. * added keyboard and video
  1057. * a lot of fixes
  1058. Revision 1.1 2004/09/05 20:58:47 armin
  1059. * first rtl version for netwlibc
  1060. }