system.pp 17 KB

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