system.pp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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. {$define useLongNamespaceByDefault}
  17. {$define autoHeapRelease}
  18. {$ifdef SYSTEMDEBUG}
  19. {$define SYSTEMEXCEPTIONDEBUG}
  20. {$endif SYSTEMDEBUG}
  21. {$ifdef cpui386}
  22. {$define Set_i386_Exception_handler}
  23. {$endif cpui386}
  24. { include system-independent routine headers }
  25. {$I systemh.inc}
  26. {Platform specific information}
  27. const
  28. LineEnding = #13#10;
  29. LFNSupport : boolean = false;
  30. DirectorySeparator = '/';
  31. DriveSeparator = ':';
  32. PathSeparator = ';';
  33. { FileNameCaseSensitive is defined separately below!!! }
  34. maxExitCode = 255;
  35. MaxPathLen = 256;
  36. CONST
  37. { Default filehandles }
  38. UnusedHandle : THandle = -1;
  39. StdInputHandle : THandle = 0;
  40. StdOutputHandle : THandle = 0;
  41. StdErrorHandle : THandle = 0;
  42. FileNameCaseSensitive : boolean = false;
  43. CtrlZMarksEOF: boolean = false; (* #26 not considered as end of file *)
  44. sLineBreak = LineEnding;
  45. DefaultTextLineBreakStyle : TTextLineBreakStyle = tlbsCRLF;
  46. TYPE
  47. TNWCheckFunction = procedure (var code : longint);
  48. VAR
  49. ArgC : INTEGER;
  50. ArgV : ppchar;
  51. NetwareCheckFunction : TNWCheckFunction;
  52. NetwareMainThreadGroupID: longint;
  53. NetwareCodeStartAddress : dword;
  54. NetwareUnloadProc : pointer = nil; {like exitProc but for nlm unload only}
  55. CONST
  56. envp : ppchar = nil; {dummy to make heaptrc happy}
  57. procedure ConsolePrintf (FormatStr : PCHAR; Param : LONGINT); CDecl; external 'clib' name 'printf';
  58. procedure ConsolePrintf (FormatStr : PCHAR; Param : pchar); CDecl; external 'clib' name 'printf';
  59. procedure ConsolePrintf (FormatStr : PCHAR; P1,P2 : LONGINT); CDecl; external 'clib' name 'printf';
  60. procedure ConsolePrintf (FormatStr : PCHAR; P1,P2,P3 : LONGINT); CDecl; external 'clib' name 'printf';
  61. procedure ConsolePrintf (FormatStr : PCHAR); CDecl; external 'clib' name 'printf';
  62. // this gives internal compiler error 200404181
  63. // procedure ConsolePrintf (FormatStr : PCHAR; Param : array of const); CDecl; EXTERNAL 'clib' name 'ConsolePrintf';
  64. procedure __EnterDebugger; cdecl; external 'clib' name 'EnterDebugger';
  65. type
  66. TSysCloseAllRemainingSemaphores = procedure;
  67. TSysReleaseThreadVars = procedure;
  68. TSysSetThreadDataAreaPtr = function (newPtr:pointer):pointer;
  69. procedure NWSysSetThreadFunctions (crs:TSysCloseAllRemainingSemaphores;
  70. rtv:TSysReleaseThreadVars;
  71. stdata:TSysSetThreadDataAreaPtr);
  72. function NWGetCodeStart : pointer; // needed for lineinfo
  73. implementation
  74. { Indicate that stack checking is taken care by OS}
  75. {$DEFINE NO_GENERIC_STACK_CHECK}
  76. { include system independent routines }
  77. {$I system.inc}
  78. //procedure __EnterDebugger; cdecl; external 'clib' name 'EnterDebugger';
  79. procedure PASCALMAIN;external name 'PASCALMAIN';
  80. procedure fpc_do_exit;external name 'FPC_DO_EXIT';
  81. {*****************************************************************************
  82. Startup
  83. *****************************************************************************}
  84. function __GetBssStart : pointer; external name '__getBssStart';
  85. function __getUninitializedDataSize : longint; external name '__getUninitializedDataSize';
  86. //function __getDataStart : longint; external name '__getDataStart';
  87. function __GetTextStart : longint; external name '__getTextStart';
  88. PROCEDURE nlm_main (_ArgC : LONGINT; _ArgV : ppchar); CDECL; [public,alias: '_nlm_main'];
  89. BEGIN
  90. // Initialize BSS
  91. if __getUninitializedDataSize > 0 then
  92. fillchar (__getBssStart^,__getUninitializedDataSize,0);
  93. NetwareCodeStartAddress := __GetTextStart;
  94. ArgC := _ArgC;
  95. ArgV := _ArgV;
  96. fpc_threadvar_relocate_proc := nil;
  97. PASCALMAIN;
  98. END;
  99. function NWGetCodeStart : pointer; // needed for lineinfo
  100. begin
  101. NWGetCodeStart := pointer(NetwareCodeStartAddress);
  102. end;
  103. {*****************************************************************************
  104. System Dependent Exit code
  105. *****************************************************************************}
  106. var SigTermHandlerActive : boolean;
  107. Procedure system_exit;
  108. begin
  109. if TerminatingThreadID <> 0 then
  110. if TerminatingThreadID <> ThreadId then
  111. if TerminatingThreadID <> _GetThreadID then
  112. begin
  113. {$ifdef DEBUG_MT}
  114. ConsolePrintf ('Terminating Thread %x because halt was called while Thread %x terminates nlm'#13#10,_GetThreadId,TerminatingThreadId);
  115. {$endif}
  116. ExitThread (EXIT_THREAD,0);
  117. // only for the case ExitThread fails
  118. while true do
  119. _ThreadSwitchWithDelay;
  120. end;
  121. if assigned (CloseAllRemainingSemaphores) then CloseAllRemainingSemaphores;
  122. if assigned (ReleaseThreadVars) then ReleaseThreadVars;
  123. {$ifdef autoHeapRelease}
  124. FreeSbrkMem; { free memory allocated by heapmanager }
  125. {$endif}
  126. if not SigTermHandlerActive then
  127. begin
  128. if ExitCode <> 0 Then { otherwise we dont see runtime-errors }
  129. _SetAutoScreenDestructionMode (false);
  130. _exit (ExitCode);
  131. end;
  132. end;
  133. {*****************************************************************************
  134. Stack check code
  135. *****************************************************************************}
  136. const StackErr : boolean = false;
  137. procedure int_stackcheck(stack_size:Cardinal);[public,alias:'FPC_STACKCHECK'];
  138. {
  139. called when trying to get local stack if the compiler directive $S
  140. is set this function must preserve all registers
  141. With a 2048 byte safe area used to write to StdIo without crossing
  142. the stack boundary
  143. }
  144. begin
  145. if StackErr then exit; // avoid recursive calls
  146. asm
  147. pusha
  148. end;
  149. stackerr := ( _stackavail < stack_size + 2048);
  150. asm
  151. popa
  152. end;
  153. if not StackErr then exit;
  154. StackErr := true;
  155. HandleError (202);
  156. end;
  157. {*****************************************************************************
  158. ParamStr/Randomize
  159. *****************************************************************************}
  160. { number of args }
  161. function paramcount : longint;
  162. begin
  163. paramcount := argc - 1;
  164. end;
  165. { argument number l }
  166. function paramstr(l : longint) : string;
  167. begin
  168. if (l>=0) and (l+1<=argc) then
  169. begin
  170. paramstr:=strpas(argv[l]);
  171. if l = 0 then // fix nlm path
  172. begin
  173. for l := 1 to length (paramstr) do
  174. if paramstr[l] = '\' then paramstr[l] := '/';
  175. end;
  176. end else
  177. paramstr:='';
  178. end;
  179. { set randseed to a new pseudo random value }
  180. procedure randomize;
  181. begin
  182. randseed := _time (NIL);
  183. end;
  184. {*****************************************************************************
  185. Thread Handling
  186. *****************************************************************************}
  187. procedure InitFPU;assembler;
  188. asm
  189. fninit
  190. fldcw fpucw
  191. end;
  192. { if return-value is <> 0, netware shows the message
  193. Unload Anyway ?
  194. To Disable unload at all, SetNLMDontUnloadFlag can be used on
  195. Netware >= 4.0 }
  196. function CheckFunction : longint; CDECL; [public,alias: 'FPC_NW_CHECKFUNCTION'];
  197. var oldTG:longint;
  198. oldPtr: pointer;
  199. begin
  200. if assigned (NetwareCheckFunction) then
  201. begin
  202. { this function is called without clib context, to allow clib
  203. calls, we set the thread group id before calling the
  204. user-function }
  205. oldTG := _SetThreadGroupID (NetwareMainThreadGroupID);
  206. { to allow use of threadvars, we simply set the threadvar-memory
  207. from the main thread }
  208. if assigned (SetThreadDataAreaPtr) then
  209. oldPtr := SetThreadDataAreaPtr (NIL); { nil means main threadvars }
  210. result := 0;
  211. NetwareCheckFunction (result);
  212. if assigned (SetThreadDataAreaPtr) then
  213. SetThreadDataAreaPtr (oldPtr);
  214. _SetThreadGroupID (oldTG);
  215. end else
  216. result := 0;
  217. end;
  218. {$ifdef StdErrToConsole}
  219. var ConsoleBuff : array [0..512] of char;
  220. Function ConsoleWrite(Var F: TextRec): Integer;
  221. var
  222. i : longint;
  223. Begin
  224. if F.BufPos>0 then
  225. begin
  226. if F.BufPos>sizeof(ConsoleBuff)-1 then
  227. i:=sizeof(ConsoleBuff)-1
  228. else
  229. i:=F.BufPos;
  230. Move(F.BufPtr^,ConsoleBuff,i);
  231. ConsoleBuff[i] := #0;
  232. ConsolePrintf(@ConsoleBuff[0]);
  233. end;
  234. F.BufPos:=0;
  235. ConsoleWrite := 0;
  236. End;
  237. Function ConsoleClose(Var F: TextRec): Integer;
  238. begin
  239. ConsoleClose:=0;
  240. end;
  241. Function ConsoleOpen(Var F: TextRec): Integer;
  242. Begin
  243. TextRec(F).InOutFunc:=@ConsoleWrite;
  244. TextRec(F).FlushFunc:=@ConsoleWrite;
  245. TextRec(F).CloseFunc:=@ConsoleClose;
  246. ConsoleOpen:=0;
  247. End;
  248. procedure AssignStdErrConsole(Var T: Text);
  249. begin
  250. Assign(T,'');
  251. TextRec(T).OpenFunc:=@ConsoleOpen;
  252. Rewrite(T);
  253. end;
  254. {$endif}
  255. { this will be called if the nlm is unloaded. It will NOT be
  256. called if the program exits i.e. with halt.
  257. Halt (or _exit) can not be called from this callback procedure }
  258. procedure TermSigHandler (Sig:longint); CDecl;
  259. var oldTG : longint;
  260. oldPtr: pointer;
  261. err : longint;
  262. current_exit : procedure;
  263. ThreadName : array [0..20] of char;
  264. HadExitProc : boolean;
  265. Count : longint;
  266. begin
  267. oldTG := _SetThreadGroupID (NetwareMainThreadGroupID); { this is only needed for nw 3.11 }
  268. { _GetThreadDataAreaPtr will not be valid because the signal
  269. handler is called by netware with a differnt thread. To avoid
  270. problems in the exit routines, we set the data of the main thread
  271. here }
  272. if assigned (SetThreadDataAreaPtr) then
  273. oldPtr := SetThreadDataAreaPtr (NIL); { nil means main thread }
  274. {this signal handler is called within the console command
  275. thread, the main thread is still running. Via NetwareUnloadProc
  276. running threads may terminate itself}
  277. TerminatingThreadID := _GetThreadID;
  278. {$ifdef DEBUG_MT}
  279. ConsolePrintf (#13'TermSigHandler Called, MainThread:%x, OurThread: %x'#13#10,ThreadId,TerminatingThreadId);
  280. if NetwareUnloadProc <> nil then
  281. ConsolePrintf (#13'Calling NetwareUnloadProcs'#13#10);
  282. {$endif}
  283. HadExitProc := false;
  284. {we need to finalize winock to release threads
  285. waiting on a blocking socket call. If that thread
  286. calls halt, we have to avoid that unit finalization
  287. is called by that thread because we are doing it
  288. here
  289. like the old exitProc, mainly to allow winsock to release threads
  290. blocking in a winsock calls }
  291. while NetwareUnloadProc<>nil Do
  292. Begin
  293. InOutRes:=0;
  294. current_exit:=tProcedure(NetwareUnloadProc);
  295. NetwareUnloadProc:=nil;
  296. current_exit();
  297. _ThreadSwitchWithDelay;
  298. hadExitProc := true;
  299. End;
  300. err := 0;
  301. if hadExitProc then
  302. begin {give the main thread a little bit of time to terminate}
  303. count := 0;
  304. repeat
  305. err := _GetThreadName(ThreadID,ThreadName);
  306. if err = 0 then _Delay (200);
  307. inc(count);
  308. until (err <> 0) or (count > 100); {about 20 seconds}
  309. {$ifdef DEBUG_MT}
  310. if err = 0 then
  311. ConsolePrintf (#13,'Main Thread not terminated'#13#10)
  312. else
  313. ConsolePrintf (#13'Main Thread has ended'#13#10);
  314. {$endif}
  315. end;
  316. if err = 0 then
  317. {$ifdef DEBUG_MT}
  318. begin
  319. err := _SuspendThread(ThreadId);
  320. ConsolePrintf (#13'SuspendThread(%x) returned %d'#13#10,ThreadId,err);
  321. end;
  322. {$else}
  323. _SuspendThread(ThreadId);
  324. {$endif}
  325. _ThreadSwitchWithDelay;
  326. {$ifdef DEBUG_MT}
  327. ConsolePrintf (#13'Calling do_exit'#13#10);
  328. {$endif}
  329. SigTermHandlerActive := true; { to avoid that system_exit calls _exit }
  330. do_exit; { calls finalize units }
  331. if assigned (SetThreadDataAreaPtr) then
  332. SetThreadDataAreaPtr (oldPtr);
  333. _SetThreadGroupID (oldTG);
  334. {$ifdef DEBUG_MT}
  335. ConsolePrintf (#13'TermSigHandler: all done'#13#10);
  336. {$endif}
  337. end;
  338. procedure SysInitStdIO;
  339. begin
  340. { Setup stdin, stdout and stderr }
  341. StdInputHandle := _fileno (LONGINT (_GetStdIn^)); // GetStd** returns **FILE
  342. StdOutputHandle:= _fileno (LONGINT (_GetStdOut^));
  343. StdErrorHandle := _fileno (LONGINT (_GetStdErr^));
  344. OpenStdIO(Input,fmInput,StdInputHandle);
  345. OpenStdIO(Output,fmOutput,StdOutputHandle);
  346. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  347. {$ifdef StdErrToConsole}
  348. AssignStdErrConsole(StdErr);
  349. AssignStdErrConsole(ErrOutput);
  350. {$else}
  351. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  352. OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
  353. {$endif}
  354. end;
  355. function GetProcessID: SizeUInt;
  356. begin
  357. GetProcessID := SizeUInt (GetNlmHandle);
  358. end;
  359. {*****************************************************************************
  360. SystemUnit Initialization
  361. *****************************************************************************}
  362. Begin
  363. StackBottom := SPtr - StackLength;
  364. SigTermHandlerActive := false;
  365. NetwareCheckFunction := nil;
  366. NetwareMainThreadGroupID := _GetThreadGroupID;
  367. _Signal (_SIGTERM, @TermSigHandler);
  368. {$ifdef useLongNamespaceByDefault}
  369. if _getenv ('FPC_DISABLE_LONG_NAMESPACE') = nil then
  370. begin
  371. if _SetCurrentNameSpace (NW_NS_LONG) <> 255 then
  372. begin
  373. if _SetTargetNamespace (NW_NS_LONG) <> 255 then
  374. LFNSupport := true
  375. else
  376. _SetCurrentNameSpace (NW_NS_DOS);
  377. end;
  378. end;
  379. {$endif useLongNamespaceByDefault}
  380. { Setup heap }
  381. InitHeap;
  382. SysInitExceptions;
  383. { Reset IO Error }
  384. InOutRes:=0;
  385. ThreadID := _GetThreadID;
  386. {$ifdef DEBUG_MT}
  387. ConsolePrintf (#13'Start system, ThreadID: %x'#13#10,ThreadID);
  388. {$endif}
  389. SysInitStdIO;
  390. {Delphi Compatible}
  391. IsLibrary := FALSE;
  392. IsConsole := TRUE;
  393. ExitCode := 0;
  394. InitSystemThreads;
  395. {$ifdef HASVARIANT}
  396. initvariantmanager;
  397. {$endif HASVARIANT}
  398. {$ifdef HASWIDESTRING}
  399. initwidestringmanager;
  400. {$endif HASWIDESTRING}
  401. End.
  402. {
  403. $Log$
  404. Revision 1.38 2005-05-12 20:29:04 michael
  405. + Added maxpathlen constant (maximum length of filename path)
  406. Revision 1.37 2005/04/03 21:10:59 hajny
  407. * EOF_CTRLZ conditional define replaced with CtrlZMarksEOF, #26 handling made more consistent (fix for bug 2453)
  408. Revision 1.36 2005/02/14 17:13:30 peter
  409. * truncate log
  410. Revision 1.35 2005/02/06 16:57:18 peter
  411. * threads for go32v2,os,emx,netware
  412. Revision 1.34 2005/02/01 20:22:49 florian
  413. * improved widestring infrastructure manager
  414. }