system.pp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. unit system;
  2. interface
  3. {$DEFINE FPC_NO_DEFAULT_HEAP}
  4. {$DEFINE FPC_INCLUDE_SOFTWARE_MUL}
  5. {$DEFINE FPC_INCLUDE_SOFTWARE_MOD_DIV}
  6. {$DEFINE FPC_USE_SMALL_DEFAULTSTACKSIZE}
  7. { To avoid warnings in thread.inc code,
  8. but value must be really given after
  9. systemh.inc is included otherwise the
  10. $mode switch is not effective }
  11. {$DEFINE HAS_CMDLINE}
  12. {$I systemh.inc}
  13. {$IFDEF FPC_X86_DATA_NEAR}
  14. {$I locheaph.inc}
  15. {$ELSE FPC_X86_DATA_NEAR}
  16. { todo: implement a working win16 heap manager for the far data models }
  17. {$I tnyheaph.inc}
  18. {$ENDIF FPC_X86_DATA_NEAR}
  19. const
  20. LineEnding = #13#10;
  21. { LFNSupport is a variable here, defined below!!! }
  22. DirectorySeparator = '\';
  23. DriveSeparator = ':';
  24. ExtensionSeparator = '.';
  25. PathSeparator = ';';
  26. AllowDirectorySeparators : set of char = ['\','/'];
  27. AllowDriveSeparators : set of char = [':'];
  28. { FileNameCaseSensitive and FileNameCasePreserving are defined separately below!!! }
  29. maxExitCode = 255;
  30. MaxPathLen = 256;
  31. const
  32. { Default filehandles }
  33. UnusedHandle = $ffff;{ instead of -1, as it is a word value}
  34. StdInputHandle = 0;
  35. StdOutputHandle = 1;
  36. StdErrorHandle = 2;
  37. FileNameCaseSensitive : boolean = false;
  38. FileNameCasePreserving: boolean = false;
  39. CtrlZMarksEOF: boolean = true; (* #26 is considered as end of file *)
  40. sLineBreak = LineEnding;
  41. DefaultTextLineBreakStyle : TTextLineBreakStyle = tlbsCRLF;
  42. { Default memory segments (Tp7 compatibility) }
  43. { seg0040: Word = $0040;
  44. segA000: Word = $A000;
  45. segB000: Word = $B000;
  46. segB800: Word = $B800;}
  47. type
  48. LPSTR = ^Char;far;
  49. PFarChar = ^Char;far;
  50. PHugeChar = ^Char;huge;
  51. var
  52. { Mem[] support }
  53. mem : array[0..$7fff-1] of byte absolute $0:$0;
  54. memw : array[0..($7fff div sizeof(word))-1] of word absolute $0:$0;
  55. meml : array[0..($7fff div sizeof(longint))-1] of longint absolute $0:$0;
  56. { C-compatible arguments and environment }
  57. argc:longint; //!! public name 'operatingsystem_parameter_argc';
  58. argv:PPchar; //!! public name 'operatingsystem_parameter_argv';
  59. envp:PPchar; //!! public name 'operatingsystem_parameter_envp';
  60. dos_argv0 : pchar; //!! public name 'dos_argv0';
  61. { The DOS Program Segment Prefix segment (TP7 compatibility) }
  62. PrefixSeg:Word;public name '__fpc_PrefixSeg';
  63. { BP7 compatible windows variables }
  64. { In C, these are the parameters to WinMain }
  65. CmdLine: LPSTR;public name '__fpc_CmdLine';
  66. CmdShow: SmallInt;public name '__fpc_CmdShow';
  67. HInstance: Word{HINST};public name '__fpc_HInstance';
  68. HPrevInst: Word{HINST};public name '__fpc_HPrevInst';
  69. { The value that needs to be added to the segment to move the pointer by
  70. 64K bytes (BP7 compatibility) }
  71. SelectorInc: Word;public name '__fpc_SelectorInc';
  72. { SaveInt00: FarPointer;public name '__SaveInt00';}
  73. AllFilesMask: string [3];
  74. {$ifndef RTLLITE}
  75. { System info }
  76. LFNSupport : boolean;
  77. {$ELSE RTLLITE}
  78. const
  79. LFNSupport = false;
  80. {$endif RTLLITE}
  81. implementation
  82. const
  83. fCarry = 1;
  84. { used for an offset fixup for accessing the proc parameters in asm routines
  85. that use nostackframe. We can't use the parameter name directly, because
  86. i8086 doesn't support sp relative addressing. }
  87. {$ifdef FPC_X86_CODE_FAR}
  88. extra_param_offset = 2;
  89. {$else FPC_X86_CODE_FAR}
  90. extra_param_offset = 0;
  91. {$endif FPC_X86_CODE_FAR}
  92. {$if defined(FPC_X86_DATA_FAR) or defined(FPC_X86_DATA_HUGE)}
  93. extra_data_offset = 2;
  94. {$else}
  95. extra_data_offset = 0;
  96. {$endif}
  97. type
  98. PFarByte = ^Byte;far;
  99. PFarWord = ^Word;far;
  100. { structure, located at DS:0, initialized by InitTask }
  101. PAutoDataSegHeader = ^TAutoDataSegHeader;
  102. TAutoDataSegHeader = record
  103. null: Word;
  104. oOldSP: Word;
  105. hOldSS: Word;
  106. pLocalHeap: Word;
  107. pAtomTable: Word;
  108. pStackTop: Word;
  109. pStackMin: Word;
  110. pStackBot: Word;
  111. end;
  112. {$I registers.inc}
  113. procedure MsDos(var Regs: Registers); external name 'FPC_MSDOS';
  114. { invokes int 21h with the carry flag set on entry; used for the LFN functions
  115. to ensure that the carry flag is set on exit on older DOS versions which don't
  116. support them }
  117. procedure MsDos_Carry(var Regs: Registers); external name 'FPC_MSDOS_CARRY';
  118. {$define SYSTEMUNIT}
  119. {$I wintypes.inc}
  120. {$I winprocsh.inc}
  121. {$I winprocs.inc}
  122. {$I system.inc}
  123. {$IFDEF FPC_X86_DATA_NEAR}
  124. {$I locheap.inc}
  125. {$ELSE FPC_X86_DATA_NEAR}
  126. { todo: implement a working win16 heap manager for the far data models }
  127. {$I tinyheap.inc}
  128. {$ENDIF FPC_X86_DATA_NEAR}
  129. {*****************************************************************************
  130. ParamStr/Randomize
  131. *****************************************************************************}
  132. {function GetProgramName: string;
  133. var
  134. dos_env_seg: Word;
  135. ofs: Word;
  136. Ch, Ch2: Char;
  137. begin
  138. if dos_version < $300 then
  139. begin
  140. GetProgramName := '';
  141. exit;
  142. end;
  143. dos_env_seg := PFarWord(Ptr(PrefixSeg, $2C))^;
  144. ofs := 1;
  145. repeat
  146. Ch := PFarChar(Ptr(dos_env_seg,ofs - 1))^;
  147. Ch2 := PFarChar(Ptr(dos_env_seg,ofs))^;
  148. if (Ch = #0) and (Ch2 = #0) then
  149. begin
  150. Inc(ofs, 3);
  151. GetProgramName := '';
  152. repeat
  153. Ch := PFarChar(Ptr(dos_env_seg,ofs))^;
  154. if Ch <> #0 then
  155. GetProgramName := GetProgramName + Ch;
  156. Inc(ofs);
  157. if ofs = 0 then
  158. begin
  159. GetProgramName := '';
  160. exit;
  161. end;
  162. until Ch = #0;
  163. exit;
  164. end;
  165. Inc(ofs);
  166. if ofs = 0 then
  167. begin
  168. GetProgramName := '';
  169. exit;
  170. end;
  171. until false;
  172. end;}
  173. function GetArg(ArgNo: Integer; out ArgResult: string): Integer;
  174. var
  175. I: Integer;
  176. InArg: Boolean;
  177. begin
  178. ArgResult := '';
  179. I := 0;
  180. InArg := False;
  181. GetArg := 0;
  182. while CmdLine[I]<>#0 do
  183. begin
  184. if not InArg and (CmdLine[I] <> ' ') then
  185. begin
  186. InArg := True;
  187. Inc(GetArg);
  188. end;
  189. if InArg and (CmdLine[I] = ' ') then
  190. InArg := False;
  191. if InArg and (GetArg = ArgNo) then
  192. ArgResult := ArgResult + CmdLine[I];
  193. Inc(I);
  194. end;
  195. end;
  196. function paramcount : longint;
  197. var
  198. tmpstr: string;
  199. begin
  200. paramcount := GetArg(-1, tmpstr);
  201. end;
  202. function paramstr(l : longint) : string;
  203. begin
  204. if l = 0 then
  205. paramstr := ''{GetProgramName}
  206. else
  207. GetArg(l, paramstr);
  208. end;
  209. procedure randomize;
  210. {var
  211. hl : longint;
  212. regs : Registers;}
  213. begin
  214. { regs.AH:=$2C;
  215. MsDos(regs);
  216. hl:=regs.DX;
  217. randseed:=hl*$10000+ regs.CX;}
  218. end;
  219. {*****************************************************************************
  220. System Dependent Exit code
  221. *****************************************************************************}
  222. procedure system_exit;
  223. {var
  224. h : byte;}
  225. begin
  226. (* RestoreInterruptHandlers;
  227. for h:=0 to max_files-1 do
  228. if openfiles[h] then
  229. begin
  230. {$ifdef SYSTEMDEBUG}
  231. writeln(stderr,'file ',opennames[h],' not closed at exit');
  232. {$endif SYSTEMDEBUG}
  233. if h>=5 then
  234. do_close(h);
  235. end;
  236. {$ifndef FPC_MM_TINY}
  237. if not CheckNullArea then
  238. writeln(stderr, 'Nil pointer assignment');
  239. {$endif FPC_MM_TINY}*)
  240. asm
  241. mov al, byte [exitcode]
  242. mov ah, 4Ch
  243. int 21h
  244. end;
  245. end;
  246. {*****************************************************************************
  247. SystemUnit Initialization
  248. *****************************************************************************}
  249. procedure InitWin16Heap;
  250. begin
  251. {$ifdef FPC_X86_DATA_NEAR}
  252. SetMemoryManager(LocalHeapMemoryManager);
  253. {$else FPC_X86_DATA_NEAR}
  254. { todo: implement a working win16 heap manager for the far data models }
  255. {$endif FPC_X86_DATA_NEAR}
  256. end;
  257. function CheckLFN:boolean;
  258. var
  259. regs : Registers;
  260. RootName : pchar;
  261. buf : array [0..31] of char;
  262. begin
  263. { Check LFN API on drive c:\ }
  264. RootName:='C:\';
  265. { Call 'Get Volume Information' ($71A0) }
  266. FillChar(regs,SizeOf(regs),0);
  267. regs.AX:=$71a0;
  268. regs.ES:=Seg(buf);
  269. regs.DI:=Ofs(buf);
  270. regs.CX:=32;
  271. regs.DS:=Seg(RootName^);
  272. regs.DX:=Ofs(RootName^);
  273. MsDos_Carry(regs);
  274. { If carryflag=0 and LFN API bit in ebx is set then use Long file names }
  275. CheckLFN:=(regs.Flags and fCarry=0) and (regs.BX and $4000=$4000);
  276. end;
  277. procedure SysInitStdIO;
  278. begin
  279. OpenStdIO(Input,fmInput,StdInputHandle);
  280. OpenStdIO(Output,fmOutput,StdOutputHandle);
  281. OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
  282. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  283. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  284. end;
  285. function GetProcessID: SizeUInt;
  286. begin
  287. GetProcessID := PrefixSeg;
  288. end;
  289. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt;
  290. begin
  291. result := stklen;
  292. end;
  293. begin
  294. {$if defined(FPC_X86_DATA_FAR) or defined(FPC_X86_DATA_HUGE)}
  295. with PAutoDataSegHeader(Ptr(DSeg,0))^ do
  296. begin
  297. StackBottom := Ptr(SSeg,pStackTop);
  298. StackLength := pStackBot-pStackTop;
  299. end;
  300. {$else}
  301. with PAutoDataSegHeader(0)^ do
  302. begin
  303. StackBottom := NearPointer(pStackTop);
  304. StackLength := pStackBot-pStackTop;
  305. end;
  306. {$endif}
  307. { To be set if this is a GUI or console application }
  308. IsConsole := FALSE;
  309. { To be set if this is a library and not a program }
  310. IsLibrary := FALSE;
  311. { Setup heap }
  312. InitWin16Heap;
  313. SysInitExceptions;
  314. initunicodestringmanager;
  315. { Use LFNSupport LFN }
  316. LFNSupport:=CheckLFN;
  317. if LFNSupport then
  318. begin
  319. FileNameCasePreserving:=true;
  320. AllFilesMask := '*';
  321. end
  322. else
  323. AllFilesMask := '*.*';
  324. { Reset IO Error }
  325. InOutRes:=0;
  326. end.