system.pp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by the Free Pascal development team.
  4. See the file COPYING.FPC, included in this distribution,
  5. for details about the copyright.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. **********************************************************************}
  10. unit system;
  11. interface
  12. { two debug conditionnals can be used
  13. - SYSTEMDEBUG
  14. -for STACK checks
  15. -for non closed files at exit (or at any time with GDB)
  16. - SYSTEM_DEBUG_STARTUP
  17. specifically for
  18. - proxy command line (DJGPP feature)
  19. - list of args
  20. - list of env variables (PM) }
  21. {$ifndef NO_EXCEPTIONS_IN_SYSTEM}
  22. {$define EXCEPTIONS_IN_SYSTEM}
  23. {$endif NO_EXCEPTIONS_IN_SYSTEM}
  24. {$define USE_NOTHREADMANAGER}
  25. { include system-independent routine headers }
  26. {$I systemh.inc}
  27. const
  28. LineEnding = #13#10;
  29. { LFNSupport is a variable here, defined below!!! }
  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 = -1;
  39. StdInputHandle = 0;
  40. StdOutputHandle = 1;
  41. StdErrorHandle = 2;
  42. FileNameCaseSensitive : boolean = false;
  43. CtrlZMarksEOF: boolean = true; (* #26 is considered as end of file *)
  44. sLineBreak = LineEnding;
  45. DefaultTextLineBreakStyle : TTextLineBreakStyle = tlbsCRLF;
  46. { Default memory segments (Tp7 compatibility) }
  47. seg0040 = $0040;
  48. segA000 = $A000;
  49. segB000 = $B000;
  50. segB800 = $B800;
  51. var
  52. { Mem[] support }
  53. mem : array[0..$7fffffff-1] of byte absolute $0:$0;
  54. memw : array[0..($7fffffff div sizeof(word))-1] of word absolute $0:$0;
  55. meml : array[0..($7fffffff div sizeof(longint))-1] of longint absolute $0:$0;
  56. { C-compatible arguments and environment }
  57. argc : longint;
  58. argv : ppchar;
  59. envp : ppchar;
  60. dos_argv0 : pchar;
  61. {$ifndef RTLLITE}
  62. { System info }
  63. LFNSupport : boolean;
  64. {$ELSE RTLLITE}
  65. const
  66. LFNSupport = false;
  67. {$endif RTLLITE}
  68. type
  69. { Dos Extender info }
  70. p_stub_info = ^t_stub_info;
  71. t_stub_info = packed record
  72. magic : array[0..15] of char;
  73. size : longint;
  74. minstack : longint;
  75. memory_handle : longint;
  76. initial_size : longint;
  77. minkeep : word;
  78. ds_selector : word;
  79. ds_segment : word;
  80. psp_selector : word;
  81. cs_selector : word;
  82. env_size : word;
  83. basename : array[0..7] of char;
  84. argv0 : array [0..15] of char;
  85. dpmi_server : array [0..15] of char;
  86. end;
  87. p_go32_info_block = ^t_go32_info_block;
  88. t_go32_info_block = packed record
  89. size_of_this_structure_in_bytes : longint; {offset 0}
  90. linear_address_of_primary_screen : longint; {offset 4}
  91. linear_address_of_secondary_screen : longint; {offset 8}
  92. linear_address_of_transfer_buffer : longint; {offset 12}
  93. size_of_transfer_buffer : longint; {offset 16}
  94. pid : longint; {offset 20}
  95. master_interrupt_controller_base : byte; {offset 24}
  96. slave_interrupt_controller_base : byte; {offset 25}
  97. selector_for_linear_memory : word; {offset 26}
  98. linear_address_of_stub_info_structure : longint; {offset 28}
  99. linear_address_of_original_psp : longint; {offset 32}
  100. run_mode : word; {offset 36}
  101. run_mode_info : word; {offset 38}
  102. end;
  103. var
  104. stub_info : p_stub_info;
  105. go32_info_block : t_go32_info_block;
  106. {$ifdef SYSTEMDEBUG}
  107. const
  108. accept_sbrk : boolean = true;
  109. {$endif}
  110. {
  111. necessary for objects.pas, should be removed (at least from the interface
  112. to the implementation)
  113. }
  114. type
  115. trealregs=record
  116. realedi,realesi,realebp,realres,
  117. realebx,realedx,realecx,realeax : longint;
  118. realflags,
  119. reales,realds,realfs,realgs,
  120. realip,realcs,realsp,realss : word;
  121. end;
  122. function do_write(h:longint;addr:pointer;len : longint) : longint;
  123. function do_read(h:longint;addr:pointer;len : longint) : longint;
  124. procedure syscopyfromdos(addr : longint; len : longint);
  125. procedure syscopytodos(addr : longint; len : longint);
  126. procedure sysrealintr(intnr : word;var regs : trealregs);
  127. function tb : longint;
  128. implementation
  129. { include system independent routines }
  130. {$I system.inc}
  131. var
  132. _args : ppchar;external name '_args';
  133. __stubinfo : p_stub_info;external name '__stubinfo';
  134. ___dos_argv0 : pchar;external name '___dos_argv0';
  135. procedure setup_arguments;
  136. type
  137. arrayword = array [0..255] of word;
  138. var
  139. psp : word;
  140. proxy_s : string[50];
  141. proxy_argc,proxy_seg,proxy_ofs,lin : longint;
  142. rm_argv : ^arrayword;
  143. argv0len : longint;
  144. useproxy : boolean;
  145. hp : ppchar;
  146. doscmd : string[129]; { Dos commandline copied from PSP, max is 128 chars +1 for terminating zero }
  147. arglen,
  148. count : longint;
  149. argstart,
  150. pc,arg : pchar;
  151. quote : char;
  152. argvlen : longint;
  153. function atohex(s : pchar) : longint;
  154. var
  155. rv : longint;
  156. v : byte;
  157. begin
  158. rv:=0;
  159. while (s^<>#0) do
  160. begin
  161. v:=byte(s^)-byte('0');
  162. if (v > 9) then
  163. dec(v,7);
  164. v:=v and 15; { in case it's lower case }
  165. rv:=(rv shl 4) or v;
  166. inc(longint(s));
  167. end;
  168. atohex:=rv;
  169. end;
  170. procedure allocarg(idx,len:longint);
  171. var
  172. oldargvlen : longint;
  173. begin
  174. if idx>=argvlen then
  175. begin
  176. oldargvlen:=argvlen;
  177. argvlen:=(idx+8) and (not 7);
  178. sysreallocmem(argv,argvlen*sizeof(pointer));
  179. fillchar(argv[oldargvlen],(argvlen-oldargvlen)*sizeof(pointer),0);
  180. end;
  181. { use realloc to reuse already existing memory }
  182. { always allocate, even if length is zero, since }
  183. { the arg. is still present! }
  184. sysreallocmem(argv[idx],len+1);
  185. end;
  186. begin
  187. count:=0;
  188. argc:=1;
  189. argv:=nil;
  190. argvlen:=0;
  191. { load commandline from psp }
  192. psp:=stub_info^.psp_selector;
  193. sysseg_move(psp, 128, get_ds, longint(@doscmd), 128);
  194. doscmd[length(doscmd)+1]:=#0;
  195. {$IfDef SYSTEM_DEBUG_STARTUP}
  196. Writeln(stderr,'Dos command line is #',doscmd,'# size = ',length(doscmd));
  197. {$EndIf }
  198. { create argv[0] }
  199. argv0len:=strlen(dos_argv0);
  200. allocarg(count,argv0len+1);
  201. move(dos_argv0^,argv[count]^,argv0len+1);
  202. inc(count);
  203. { setup cmdline variable }
  204. cmdline:=Getmem(argv0len+length(doscmd)+2);
  205. move(dos_argv0^,cmdline^,argv0len);
  206. cmdline[argv0len]:=' ';
  207. inc(argv0len);
  208. move(doscmd[1],cmdline[argv0len],length(doscmd));
  209. cmdline[argv0len+length(doscmd)+1]:=#0;
  210. { parse dos commandline }
  211. pc:=@doscmd[1];
  212. while pc^<>#0 do
  213. begin
  214. { skip leading spaces }
  215. while pc^ in [#1..#32] do
  216. inc(pc);
  217. if pc^=#0 then
  218. break;
  219. { calc argument length }
  220. quote:=' ';
  221. argstart:=pc;
  222. arglen:=0;
  223. while (pc^<>#0) do
  224. begin
  225. case pc^ of
  226. #1..#32 :
  227. begin
  228. if quote<>' ' then
  229. inc(arglen)
  230. else
  231. break;
  232. end;
  233. '"' :
  234. begin
  235. if quote<>'''' then
  236. begin
  237. if pchar(pc+1)^<>'"' then
  238. begin
  239. if quote='"' then
  240. quote:=' '
  241. else
  242. quote:='"';
  243. end
  244. else
  245. inc(pc);
  246. end
  247. else
  248. inc(arglen);
  249. end;
  250. '''' :
  251. begin
  252. if quote<>'"' then
  253. begin
  254. if pchar(pc+1)^<>'''' then
  255. begin
  256. if quote='''' then
  257. quote:=' '
  258. else
  259. quote:='''';
  260. end
  261. else
  262. inc(pc);
  263. end
  264. else
  265. inc(arglen);
  266. end;
  267. else
  268. inc(arglen);
  269. end;
  270. inc(pc);
  271. end;
  272. { copy argument }
  273. allocarg(count,arglen);
  274. quote:=' ';
  275. pc:=argstart;
  276. arg:=argv[count];
  277. while (pc^<>#0) do
  278. begin
  279. case pc^ of
  280. #1..#32 :
  281. begin
  282. if quote<>' ' then
  283. begin
  284. arg^:=pc^;
  285. inc(arg);
  286. end
  287. else
  288. break;
  289. end;
  290. '"' :
  291. begin
  292. if quote<>'''' then
  293. begin
  294. if pchar(pc+1)^<>'"' then
  295. begin
  296. if quote='"' then
  297. quote:=' '
  298. else
  299. quote:='"';
  300. end
  301. else
  302. inc(pc);
  303. end
  304. else
  305. begin
  306. arg^:=pc^;
  307. inc(arg);
  308. end;
  309. end;
  310. '''' :
  311. begin
  312. if quote<>'"' then
  313. begin
  314. if pchar(pc+1)^<>'''' then
  315. begin
  316. if quote='''' then
  317. quote:=' '
  318. else
  319. quote:='''';
  320. end
  321. else
  322. inc(pc);
  323. end
  324. else
  325. begin
  326. arg^:=pc^;
  327. inc(arg);
  328. end;
  329. end;
  330. else
  331. begin
  332. arg^:=pc^;
  333. inc(arg);
  334. end;
  335. end;
  336. inc(pc);
  337. end;
  338. arg^:=#0;
  339. {$IfDef SYSTEM_DEBUG_STARTUP}
  340. Writeln(stderr,'dos arg ',count,' #',arglen,'#',argv[count],'#');
  341. {$EndIf SYSTEM_DEBUG_STARTUP}
  342. inc(count);
  343. end;
  344. argc:=count;
  345. { check for !proxy for long commandlines passed using environment }
  346. hp:=envp;
  347. useproxy:=false;
  348. while assigned(hp^) do
  349. begin
  350. if (hp^[0]=' ') then
  351. begin
  352. proxy_s:=strpas(hp^);
  353. if Copy(proxy_s,1,7)=' !proxy' then
  354. begin
  355. proxy_s[13]:=#0;
  356. proxy_s[18]:=#0;
  357. proxy_s[23]:=#0;
  358. argv[2]:=@proxy_s[9];
  359. argv[3]:=@proxy_s[14];
  360. argv[4]:=@proxy_s[19];
  361. useproxy:=true;
  362. break;
  363. end;
  364. end;
  365. inc(hp);
  366. end;
  367. { check for !proxy for long commandlines passed using commandline }
  368. if (not useproxy) and
  369. (argc > 1) and (far_strlen(get_ds,longint(argv[1])) = 6) then
  370. begin
  371. move(argv[1]^,proxy_s[1],6);
  372. proxy_s[0] := #6;
  373. if (proxy_s = '!proxy') then
  374. useproxy:=true;
  375. end;
  376. { use proxy when found }
  377. if useproxy then
  378. begin
  379. proxy_argc:=atohex(argv[2]);
  380. proxy_seg:=atohex(argv[3]);
  381. proxy_ofs:=atohex(argv[4]);
  382. {$IfDef SYSTEM_DEBUG_STARTUP}
  383. Writeln(stderr,'proxy command line found');
  384. writeln(stderr,'argc: ',proxy_argc,' seg: ',proxy_seg,' ofs: ',proxy_ofs);
  385. {$EndIf SYSTEM_DEBUG_STARTUP}
  386. rm_argv:=SysGetmem(proxy_argc*sizeof(word));
  387. sysseg_move(dos_selector,proxy_seg*16+proxy_ofs, get_ds,longint(rm_argv),proxy_argc*sizeof(word));
  388. for count:=0 to proxy_argc - 1 do
  389. begin
  390. lin:=proxy_seg*16+rm_argv^[count];
  391. arglen:=far_strlen(dos_selector,lin);
  392. allocarg(count,arglen);
  393. sysseg_move(dos_selector,lin,get_ds,longint(argv[count]),arglen+1);
  394. {$IfDef SYSTEM_DEBUG_STARTUP}
  395. Writeln(stderr,'arg ',count,' #',rm_argv^[count],'#',arglen,'#',argv[count],'#');
  396. {$EndIf SYSTEM_DEBUG_STARTUP}
  397. end;
  398. SysFreemem(rm_argv);
  399. argc:=proxy_argc;
  400. end;
  401. { create an nil entry }
  402. allocarg(argc,0);
  403. { free unused memory }
  404. sysreallocmem(argv,(argc+1)*sizeof(pointer));
  405. _args:=argv;
  406. end;
  407. procedure setup_environment;
  408. var env_selector : word;
  409. env_count : longint;
  410. dos_env,cp : pchar;
  411. begin
  412. stub_info:=__stubinfo;
  413. dos_env := sysgetmem(stub_info^.env_size);
  414. env_count:=0;
  415. sysseg_move(stub_info^.psp_selector,$2c, get_ds, longint(@env_selector), 2);
  416. sysseg_move(env_selector, 0, get_ds, longint(dos_env), stub_info^.env_size);
  417. cp:=dos_env;
  418. while cp ^ <> #0 do
  419. begin
  420. inc(env_count);
  421. while (cp^ <> #0) do inc(longint(cp)); { skip to NUL }
  422. inc(longint(cp)); { skip to next character }
  423. end;
  424. envp := sysgetmem((env_count+1) * sizeof(pchar));
  425. if (envp = nil) then HandleError (203);
  426. cp:=dos_env;
  427. env_count:=0;
  428. while cp^ <> #0 do
  429. begin
  430. envp[env_count] := sysgetmem(strlen(cp)+1);
  431. strcopy(envp[env_count], cp);
  432. {$IfDef SYSTEM_DEBUG_STARTUP}
  433. Writeln(stderr,'env ',env_count,' = "',envp[env_count],'"');
  434. {$EndIf SYSTEM_DEBUG_STARTUP}
  435. inc(env_count);
  436. while (cp^ <> #0) do
  437. inc(longint(cp)); { skip to NUL }
  438. inc(longint(cp)); { skip to next character }
  439. end;
  440. envp[env_count]:=nil;
  441. longint(cp):=longint(cp)+3;
  442. dos_argv0 := sysgetmem(strlen(cp)+1);
  443. if (dos_argv0 = nil) then HandleError (203);
  444. strcopy(dos_argv0, cp);
  445. { update ___dos_argv0 also }
  446. ___dos_argv0:=dos_argv0
  447. end;
  448. {*****************************************************************************
  449. System Dependent Exit code
  450. *****************************************************************************}
  451. procedure __exit(exitcode:longint);cdecl;external;
  452. Procedure system_exit;
  453. var
  454. h : byte;
  455. begin
  456. for h:=0 to max_files-1 do
  457. if openfiles[h] then
  458. begin
  459. {$ifdef SYSTEMDEBUG}
  460. writeln(stderr,'file ',opennames[h],' not closed at exit');
  461. {$endif SYSTEMDEBUG}
  462. if h>=5 then
  463. do_close(h);
  464. end;
  465. { halt is not allways called !! }
  466. { not on normal exit !! PM }
  467. set_pm_interrupt($00,old_int00);
  468. {$ifndef EXCEPTIONS_IN_SYSTEM}
  469. set_pm_interrupt($75,old_int75);
  470. {$endif EXCEPTIONS_IN_SYSTEM}
  471. __exit(exitcode);
  472. end;
  473. procedure new_int00;
  474. begin
  475. HandleError(200);
  476. end;
  477. {$ifndef EXCEPTIONS_IN_SYSTEM}
  478. procedure new_int75;
  479. begin
  480. asm
  481. xorl %eax,%eax
  482. outb %al,$0x0f0
  483. movb $0x20,%al
  484. outb %al,$0x0a0
  485. outb %al,$0x020
  486. end;
  487. HandleError(200);
  488. end;
  489. {$endif EXCEPTIONS_IN_SYSTEM}
  490. var
  491. __stkbottom : pointer;external name '__stkbottom';
  492. {*****************************************************************************
  493. ParamStr/Randomize
  494. *****************************************************************************}
  495. function paramcount : longint;
  496. begin
  497. paramcount := argc - 1;
  498. end;
  499. function paramstr(l : longint) : string;
  500. begin
  501. if (l>=0) and (l+1<=argc) then
  502. paramstr:=strpas(argv[l])
  503. else
  504. paramstr:='';
  505. end;
  506. procedure randomize;
  507. var
  508. hl : longint;
  509. regs : trealregs;
  510. begin
  511. regs.realeax:=$2c00;
  512. sysrealintr($21,regs);
  513. hl:=lo(regs.realedx);
  514. randseed:=hl*$10000+ lo(regs.realecx);
  515. end;
  516. {*****************************************************************************
  517. SystemUnit Initialization
  518. *****************************************************************************}
  519. function CheckLFN:boolean;
  520. var
  521. regs : TRealRegs;
  522. RootName : pchar;
  523. begin
  524. { Check LFN API on drive c:\ }
  525. RootName:='C:\';
  526. syscopytodos(longint(RootName),strlen(RootName)+1);
  527. { Call 'Get Volume Information' ($71A0) }
  528. regs.realeax:=$71a0;
  529. regs.reales:=tb_segment;
  530. regs.realedi:=tb_offset;
  531. regs.realecx:=32;
  532. regs.realds:=tb_segment;
  533. regs.realedx:=tb_offset;
  534. regs.realflags:=carryflag;
  535. sysrealintr($21,regs);
  536. { If carryflag=0 and LFN API bit in ebx is set then use Long file names }
  537. CheckLFN:=(regs.realflags and carryflag=0) and (regs.realebx and $4000=$4000);
  538. end;
  539. {$ifdef EXCEPTIONS_IN_SYSTEM}
  540. {$define IN_SYSTEM}
  541. {$i dpmiexcp.pp}
  542. {$endif EXCEPTIONS_IN_SYSTEM}
  543. procedure SysInitStdIO;
  544. begin
  545. OpenStdIO(Input,fmInput,StdInputHandle);
  546. OpenStdIO(Output,fmOutput,StdOutputHandle);
  547. OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
  548. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  549. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  550. end;
  551. function GetProcessID: SizeUInt;
  552. begin
  553. GetProcessID := SizeUInt (Go32_info_block.pid);
  554. end;
  555. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt;
  556. begin
  557. result := stklen;
  558. end;
  559. var
  560. temp_int : tseginfo;
  561. Begin
  562. StackLength := CheckInitialStkLen(InitialStkLen);
  563. StackBottom := __stkbottom;
  564. { To be set if this is a GUI or console application }
  565. IsConsole := TRUE;
  566. { To be set if this is a library and not a program }
  567. IsLibrary := FALSE;
  568. { save old int 0 and 75 }
  569. get_pm_interrupt($00,old_int00);
  570. get_pm_interrupt($75,old_int75);
  571. temp_int.segment:=get_cs;
  572. temp_int.offset:=@new_int00;
  573. set_pm_interrupt($00,temp_int);
  574. {$ifndef EXCEPTIONS_IN_SYSTEM}
  575. temp_int.offset:=@new_int75;
  576. set_pm_interrupt($75,temp_int);
  577. {$endif EXCEPTIONS_IN_SYSTEM}
  578. { Setup heap }
  579. InitHeap;
  580. SysInitExceptions;
  581. { Setup stdin, stdout and stderr }
  582. SysInitStdIO;
  583. { Setup environment and arguments }
  584. Setup_Environment;
  585. Setup_Arguments;
  586. { Use LFNSupport LFN }
  587. LFNSupport:=CheckLFN;
  588. if LFNSupport then
  589. FileNameCaseSensitive:=true;
  590. { Reset IO Error }
  591. InOutRes:=0;
  592. {$ifdef FPC_HAS_FEATURE_THREADING}
  593. InitSystemThreads;
  594. {$endif}
  595. {$ifdef EXCEPTIONS_IN_SYSTEM}
  596. InitDPMIExcp;
  597. InstallDefaultHandlers;
  598. {$endif EXCEPTIONS_IN_SYSTEM}
  599. initvariantmanager;
  600. End.