system.pp 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423
  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. { include system-independent routine headers }
  23. {$I systemh.inc}
  24. { include heap support headers }
  25. {$I heaph.inc}
  26. const
  27. { Default filehandles }
  28. UnusedHandle = -1;
  29. StdInputHandle = 0;
  30. StdOutputHandle = 1;
  31. StdErrorHandle = 2;
  32. FileNameCaseSensitive : boolean = false;
  33. { Default memory segments (Tp7 compatibility) }
  34. seg0040 = $0040;
  35. segA000 = $A000;
  36. segB000 = $B000;
  37. segB800 = $B800;
  38. var
  39. { Mem[] support }
  40. mem : array[0..$7fffffff] of byte absolute $0:$0;
  41. memw : array[0..$7fffffff] of word absolute $0:$0;
  42. meml : array[0..$7fffffff] of longint absolute $0:$0;
  43. { C-compatible arguments and environment }
  44. argc : longint;
  45. argv : ppchar;
  46. envp : ppchar;
  47. dos_argv0 : pchar;
  48. {$ifndef RTLLITE}
  49. { System info }
  50. LFNSupport : boolean;
  51. {$endif RTLLITE}
  52. type
  53. { Dos Extender info }
  54. p_stub_info = ^t_stub_info;
  55. t_stub_info = packed record
  56. magic : array[0..15] of char;
  57. size : longint;
  58. minstack : longint;
  59. memory_handle : longint;
  60. initial_size : longint;
  61. minkeep : word;
  62. ds_selector : word;
  63. ds_segment : word;
  64. psp_selector : word;
  65. cs_selector : word;
  66. env_size : word;
  67. basename : array[0..7] of char;
  68. argv0 : array [0..15] of char;
  69. dpmi_server : array [0..15] of char;
  70. end;
  71. p_go32_info_block = ^t_go32_info_block;
  72. t_go32_info_block = packed record
  73. size_of_this_structure_in_bytes : longint; {offset 0}
  74. linear_address_of_primary_screen : longint; {offset 4}
  75. linear_address_of_secondary_screen : longint; {offset 8}
  76. linear_address_of_transfer_buffer : longint; {offset 12}
  77. size_of_transfer_buffer : longint; {offset 16}
  78. pid : longint; {offset 20}
  79. master_interrupt_controller_base : byte; {offset 24}
  80. slave_interrupt_controller_base : byte; {offset 25}
  81. selector_for_linear_memory : word; {offset 26}
  82. linear_address_of_stub_info_structure : longint; {offset 28}
  83. linear_address_of_original_psp : longint; {offset 32}
  84. run_mode : word; {offset 36}
  85. run_mode_info : word; {offset 38}
  86. end;
  87. var
  88. stub_info : p_stub_info;
  89. go32_info_block : t_go32_info_block;
  90. {
  91. necessary for objects.pas, should be removed (at least from the interface
  92. to the implementation)
  93. }
  94. type
  95. trealregs=record
  96. realedi,realesi,realebp,realres,
  97. realebx,realedx,realecx,realeax : longint;
  98. realflags,
  99. reales,realds,realfs,realgs,
  100. realip,realcs,realsp,realss : word;
  101. end;
  102. function do_write(h,addr,len : longint) : longint;
  103. function do_read(h,addr,len : longint) : longint;
  104. procedure syscopyfromdos(addr : longint; len : longint);
  105. procedure syscopytodos(addr : longint; len : longint);
  106. procedure sysrealintr(intnr : word;var regs : trealregs);
  107. function tb : longint;
  108. implementation
  109. { include system independent routines }
  110. {$I system.inc}
  111. const
  112. carryflag = 1;
  113. type
  114. tseginfo=packed record
  115. offset : pointer;
  116. segment : word;
  117. end;
  118. var
  119. doscmd : string[128]; { Dos commandline copied from PSP, max is 128 chars }
  120. old_int00 : tseginfo;cvar;
  121. old_int75 : tseginfo;cvar;
  122. {$asmmode ATT}
  123. {*****************************************************************************
  124. Go32 Helpers
  125. *****************************************************************************}
  126. function far_strlen(selector : word;linear_address : longint) : longint;
  127. begin
  128. asm
  129. movl linear_address,%edx
  130. movl %edx,%ecx
  131. movw selector,%gs
  132. .Larg19:
  133. movb %gs:(%edx),%al
  134. testb %al,%al
  135. je .Larg20
  136. incl %edx
  137. jmp .Larg19
  138. .Larg20:
  139. movl %edx,%eax
  140. subl %ecx,%eax
  141. movl %eax,__RESULT
  142. end;
  143. end;
  144. function tb : longint;
  145. begin
  146. tb:=go32_info_block.linear_address_of_transfer_buffer;
  147. end;
  148. function tb_segment : longint;
  149. begin
  150. tb_segment:=go32_info_block.linear_address_of_transfer_buffer shr 4;
  151. end;
  152. function tb_offset : longint;
  153. begin
  154. tb_offset:=go32_info_block.linear_address_of_transfer_buffer and $f;
  155. end;
  156. function tb_size : longint;
  157. begin
  158. tb_size:=go32_info_block.size_of_transfer_buffer;
  159. end;
  160. function dos_selector : word;
  161. begin
  162. dos_selector:=go32_info_block.selector_for_linear_memory;
  163. end;
  164. function get_ds : word;assembler;
  165. asm
  166. movw %ds,%ax
  167. end;
  168. function get_cs : word;assembler;
  169. asm
  170. movw %cs,%ax
  171. end;
  172. procedure sysseg_move(sseg : word;source : longint;dseg : word;dest : longint;count : longint);
  173. begin
  174. if count=0 then
  175. exit;
  176. if (sseg<>dseg) or ((sseg=dseg) and (source>dest)) then
  177. asm
  178. pushw %es
  179. pushw %ds
  180. cld
  181. movl count,%ecx
  182. movl source,%esi
  183. movl dest,%edi
  184. movw dseg,%ax
  185. movw %ax,%es
  186. movw sseg,%ax
  187. movw %ax,%ds
  188. movl %ecx,%eax
  189. shrl $2,%ecx
  190. rep
  191. movsl
  192. movl %eax,%ecx
  193. andl $3,%ecx
  194. rep
  195. movsb
  196. popw %ds
  197. popw %es
  198. end ['ESI','EDI','ECX','EAX']
  199. else if (source<dest) then
  200. { copy backward for overlapping }
  201. asm
  202. pushw %es
  203. pushw %ds
  204. std
  205. movl count,%ecx
  206. movl source,%esi
  207. movl dest,%edi
  208. movw dseg,%ax
  209. movw %ax,%es
  210. movw sseg,%ax
  211. movw %ax,%ds
  212. addl %ecx,%esi
  213. addl %ecx,%edi
  214. movl %ecx,%eax
  215. andl $3,%ecx
  216. orl %ecx,%ecx
  217. jz .LSEG_MOVE1
  218. { calculate esi and edi}
  219. decl %esi
  220. decl %edi
  221. rep
  222. movsb
  223. incl %esi
  224. incl %edi
  225. .LSEG_MOVE1:
  226. subl $4,%esi
  227. subl $4,%edi
  228. movl %eax,%ecx
  229. shrl $2,%ecx
  230. rep
  231. movsl
  232. cld
  233. popw %ds
  234. popw %es
  235. end ['ESI','EDI','ECX'];
  236. end;
  237. function atohex(s : pchar) : longint;
  238. var
  239. rv : longint;
  240. v : byte;
  241. begin
  242. rv:=0;
  243. while (s^ <>#0) do
  244. begin
  245. v:=byte(s^)-byte('0');
  246. if (v > 9) then
  247. dec(v,7);
  248. v:=v and 15; { in case it's lower case }
  249. rv:=(rv shl 4) or v;
  250. inc(longint(s));
  251. end;
  252. atohex:=rv;
  253. end;
  254. var
  255. _args : ppchar;external name '_args';
  256. procedure setup_arguments;
  257. type arrayword = array [0..0] of word;
  258. var psp : word;
  259. i,j : byte;
  260. quote : char;
  261. proxy_s : string[7];
  262. al,proxy_argc,proxy_seg,proxy_ofs,lin : longint;
  263. largs : array[0..127] of pchar;
  264. rm_argv : ^arrayword;
  265. argv0len : longint;
  266. begin
  267. for i := 1 to 127 do
  268. largs[i] := nil;
  269. psp:=stub_info^.psp_selector;
  270. largs[0]:=dos_argv0;
  271. argc := 1;
  272. sysseg_move(psp, 128, get_ds, longint(@doscmd), 128);
  273. {$IfDef SYSTEM_DEBUG_STARTUP}
  274. Writeln(stderr,'Dos command line is #',doscmd,'# size = ',length(doscmd));
  275. {$EndIf }
  276. { setup cmdline variable }
  277. argv0len:=strlen(dos_argv0);
  278. cmdline:=sysgetmem(argv0len+length(doscmd)+1);
  279. move(dos_argv0^,cmdline^,argv0len);
  280. move(doscmd[1],cmdline[argv0len],length(doscmd));
  281. cmdline[argv0len+length(doscmd)]:=#0;
  282. j := 1;
  283. quote := #0;
  284. for i:=1 to length(doscmd) do
  285. Begin
  286. if doscmd[i] = quote then
  287. begin
  288. quote := #0;
  289. if (i>1) and ((doscmd[i-1]='''') or (doscmd[i-1]='"')) then
  290. begin
  291. j := i+1;
  292. doscmd[i] := #0;
  293. continue;
  294. end;
  295. doscmd[i] := #0;
  296. largs[argc]:=@doscmd[j];
  297. inc(argc);
  298. j := i+1;
  299. end else
  300. if (quote = #0) and ((doscmd[i] = '''') or (doscmd[i]='"')) then
  301. begin
  302. quote := doscmd[i];
  303. j := i + 1;
  304. end else
  305. if (quote = #0) and ((doscmd[i] = ' ')
  306. or (doscmd[i] = #9) or (doscmd[i] = #10) or
  307. (doscmd[i] = #12) or (doscmd[i] = #9)) then
  308. begin
  309. doscmd[i]:=#0;
  310. if j<i then
  311. begin
  312. largs[argc]:=@doscmd[j];
  313. inc(argc);
  314. j := i+1;
  315. end else inc(j);
  316. end else
  317. if (i = length(doscmd)) then
  318. begin
  319. doscmd[i+1]:=#0;
  320. largs[argc]:=@doscmd[j];
  321. inc(argc);
  322. end;
  323. end;
  324. if (argc > 1) and (far_strlen(get_ds,longint(largs[1])) = 6) then
  325. begin
  326. move(largs[1]^,proxy_s[1],6);
  327. proxy_s[0] := #6;
  328. if (proxy_s = '!proxy') then
  329. begin
  330. {$IfDef SYSTEM_DEBUG_STARTUP}
  331. Writeln(stderr,'proxy command line ');
  332. {$EndIf SYSTEM_DEBUG_STARTUP}
  333. proxy_argc := atohex(largs[2]);
  334. proxy_seg := atohex(largs[3]);
  335. proxy_ofs := atohex(largs[4]);
  336. rm_argv := sysgetmem(proxy_argc*sizeof(word));
  337. sysseg_move(dos_selector,proxy_seg*16+proxy_ofs, get_ds,longint(rm_argv),proxy_argc*sizeof(word));
  338. for i:=0 to proxy_argc - 1 do
  339. begin
  340. lin := proxy_seg*16 + rm_argv^[i];
  341. al :=far_strlen(dos_selector, lin);
  342. largs[i] := sysgetmem(al+1);
  343. sysseg_move(dos_selector, lin, get_ds,longint(largs[i]), al+1);
  344. {$IfDef SYSTEM_DEBUG_STARTUP}
  345. Writeln(stderr,'arg ',i,' #',largs[i],'#');
  346. {$EndIf SYSTEM_DEBUG_STARTUP}
  347. end;
  348. argc := proxy_argc;
  349. end;
  350. end;
  351. argv := sysgetmem(argc shl 2);
  352. for i := 0 to argc-1 do
  353. argv[i] := largs[i];
  354. _args:=argv;
  355. end;
  356. function strcopy(dest,source : pchar) : pchar;
  357. begin
  358. asm
  359. cld
  360. movl 12(%ebp),%edi
  361. movl $0xffffffff,%ecx
  362. xorb %al,%al
  363. repne
  364. scasb
  365. not %ecx
  366. movl 8(%ebp),%edi
  367. movl 12(%ebp),%esi
  368. movl %ecx,%eax
  369. shrl $2,%ecx
  370. rep
  371. movsl
  372. movl %eax,%ecx
  373. andl $3,%ecx
  374. rep
  375. movsb
  376. movl 8(%ebp),%eax
  377. leave
  378. ret $8
  379. end;
  380. end;
  381. var
  382. __stubinfo : p_stub_info;external name '__stubinfo';
  383. ___dos_argv0 : pchar;external name '___dos_argv0';
  384. procedure setup_environment;
  385. var env_selector : word;
  386. env_count : longint;
  387. dos_env,cp : pchar;
  388. begin
  389. stub_info:=__stubinfo;
  390. dos_env := sysgetmem(stub_info^.env_size);
  391. env_count:=0;
  392. sysseg_move(stub_info^.psp_selector,$2c, get_ds, longint(@env_selector), 2);
  393. sysseg_move(env_selector, 0, get_ds, longint(dos_env), stub_info^.env_size);
  394. cp:=dos_env;
  395. while cp ^ <> #0 do
  396. begin
  397. inc(env_count);
  398. while (cp^ <> #0) do inc(longint(cp)); { skip to NUL }
  399. inc(longint(cp)); { skip to next character }
  400. end;
  401. envp := sysgetmem((env_count+1) * sizeof(pchar));
  402. if (envp = nil) then exit;
  403. cp:=dos_env;
  404. env_count:=0;
  405. while cp^ <> #0 do
  406. begin
  407. envp[env_count] := sysgetmem(strlen(cp)+1);
  408. strcopy(envp[env_count], cp);
  409. {$IfDef SYSTEM_DEBUG_STARTUP}
  410. Writeln(stderr,'env ',env_count,' = "',envp[env_count],'"');
  411. {$EndIf SYSTEM_DEBUG_STARTUP}
  412. inc(env_count);
  413. while (cp^ <> #0) do
  414. inc(longint(cp)); { skip to NUL }
  415. inc(longint(cp)); { skip to next character }
  416. end;
  417. envp[env_count]:=nil;
  418. longint(cp):=longint(cp)+3;
  419. dos_argv0 := sysgetmem(strlen(cp)+1);
  420. if (dos_argv0 = nil) then halt;
  421. strcopy(dos_argv0, cp);
  422. { update ___dos_argv0 also }
  423. ___dos_argv0:=dos_argv0
  424. end;
  425. procedure syscopytodos(addr : longint; len : longint);
  426. begin
  427. if len > tb_size then
  428. HandleError(217);
  429. sysseg_move(get_ds,addr,dos_selector,tb,len);
  430. end;
  431. procedure syscopyfromdos(addr : longint; len : longint);
  432. begin
  433. if len > tb_size then
  434. HandleError(217);
  435. sysseg_move(dos_selector,tb,get_ds,addr,len);
  436. end;
  437. procedure sysrealintr(intnr : word;var regs : trealregs);
  438. begin
  439. regs.realsp:=0;
  440. regs.realss:=0;
  441. asm
  442. movw intnr,%bx
  443. xorl %ecx,%ecx
  444. movl regs,%edi
  445. movw $0x300,%ax
  446. int $0x31
  447. end;
  448. end;
  449. procedure set_pm_interrupt(vector : byte;const intaddr : tseginfo);
  450. begin
  451. asm
  452. movl intaddr,%eax
  453. movl (%eax),%edx
  454. movw 4(%eax),%cx
  455. movl $0x205,%eax
  456. movb vector,%bl
  457. int $0x31
  458. end;
  459. end;
  460. procedure get_pm_interrupt(vector : byte;var intaddr : tseginfo);
  461. begin
  462. asm
  463. movb vector,%bl
  464. movl $0x204,%eax
  465. int $0x31
  466. movl intaddr,%eax
  467. movl %edx,(%eax)
  468. movw %cx,4(%eax)
  469. end;
  470. end;
  471. procedure getinoutres;
  472. var
  473. regs : trealregs;
  474. begin
  475. regs.realeax:=$5900;
  476. regs.realebx:=$0;
  477. sysrealintr($21,regs);
  478. InOutRes:=lo(regs.realeax);
  479. case InOutRes of
  480. 19 : InOutRes:=150;
  481. 21 : InOutRes:=152;
  482. end;
  483. end;
  484. { Keep Track of open files }
  485. const
  486. max_files = 50;
  487. var
  488. openfiles : array [0..max_files-1] of boolean;
  489. {$ifdef SYSTEMDEBUG}
  490. opennames : array [0..max_files-1] of pchar;
  491. const
  492. free_closed_names : boolean = true;
  493. {$endif SYSTEMDEBUG}
  494. {*****************************************************************************
  495. System Dependent Exit code
  496. *****************************************************************************}
  497. procedure ___exit(exitcode:byte);cdecl;external name '___exit';
  498. procedure do_close(handle : longint);forward;
  499. Procedure system_exit;
  500. var
  501. h : byte;
  502. begin
  503. for h:=0 to max_files-1 do
  504. if openfiles[h] then
  505. begin
  506. {$ifdef SYSTEMDEBUG}
  507. writeln(stderr,'file ',opennames[h],' not closed at exit');
  508. {$endif SYSTEMDEBUG}
  509. if h>=5 then
  510. do_close(h);
  511. end;
  512. { halt is not allways called !! }
  513. { not on normal exit !! PM }
  514. set_pm_interrupt($00,old_int00);
  515. set_pm_interrupt($75,old_int75);
  516. ___exit(exitcode);
  517. end;
  518. procedure halt(errnum : byte);
  519. begin
  520. exitcode:=errnum;
  521. do_exit;
  522. { do_exit should call system_exit but this does not hurt }
  523. System_exit;
  524. end;
  525. procedure new_int00;
  526. begin
  527. HandleError(200);
  528. end;
  529. procedure new_int75;
  530. begin
  531. asm
  532. xorl %eax,%eax
  533. outb %al,$0x0f0
  534. movb $0x20,%al
  535. outb %al,$0x0a0
  536. outb %al,$0x020
  537. end;
  538. HandleError(200);
  539. end;
  540. var
  541. __stkbottom : longint;external name '__stkbottom';
  542. procedure int_stackcheck(stack_size:longint);[public,alias:'FPC_STACKCHECK'];
  543. {
  544. called when trying to get local stack if the compiler directive $S
  545. is set this function must preserve esi !!!! because esi is set by
  546. the calling proc for methods it must preserve all registers !!
  547. With a 2048 byte safe area used to write to StdIo without crossing
  548. the stack boundary
  549. }
  550. begin
  551. asm
  552. pushl %eax
  553. pushl %ebx
  554. movl stack_size,%ebx
  555. addl $2048,%ebx
  556. movl %esp,%eax
  557. subl %ebx,%eax
  558. {$ifdef SYSTEMDEBUG}
  559. movl loweststack,%ebx
  560. cmpl %eax,%ebx
  561. jb .L_is_not_lowest
  562. movl %eax,loweststack
  563. .L_is_not_lowest:
  564. {$endif SYSTEMDEBUG}
  565. movl __stkbottom,%ebx
  566. cmpl %eax,%ebx
  567. jae .L__short_on_stack
  568. popl %ebx
  569. popl %eax
  570. leave
  571. ret $4
  572. .L__short_on_stack:
  573. { can be usefull for error recovery !! }
  574. popl %ebx
  575. popl %eax
  576. end['EAX','EBX'];
  577. HandleError(202);
  578. end;
  579. {*****************************************************************************
  580. ParamStr/Randomize
  581. *****************************************************************************}
  582. function paramcount : longint;
  583. begin
  584. paramcount := argc - 1;
  585. end;
  586. function paramstr(l : longint) : string;
  587. begin
  588. if (l>=0) and (l+1<=argc) then
  589. paramstr:=strpas(argv[l])
  590. else
  591. paramstr:='';
  592. end;
  593. procedure randomize;
  594. var
  595. hl : longint;
  596. regs : trealregs;
  597. begin
  598. regs.realeax:=$2c00;
  599. sysrealintr($21,regs);
  600. hl:=regs.realedx and $ffff;
  601. randseed:=hl*$10000+ (regs.realecx and $ffff);
  602. end;
  603. {*****************************************************************************
  604. Heap Management
  605. *****************************************************************************}
  606. var
  607. int_heap : longint;external name 'HEAP';
  608. int_heapsize : longint;external name 'HEAPSIZE';
  609. function getheapstart:pointer;
  610. begin
  611. getheapstart:=@int_heap;
  612. end;
  613. function getheapsize:longint;
  614. begin
  615. getheapsize:=int_heapsize;
  616. end;
  617. function ___sbrk(size:longint):longint;cdecl;external name '___sbrk';
  618. function Sbrk(size : longint):longint;assembler;
  619. asm
  620. movl size,%eax
  621. pushl %eax
  622. call ___sbrk
  623. addl $4,%esp
  624. end;
  625. { include standard heap management }
  626. {$I heap.inc}
  627. {****************************************************************************
  628. Low level File Routines
  629. ****************************************************************************}
  630. procedure AllowSlash(p:pchar);
  631. var
  632. i : longint;
  633. begin
  634. { allow slash as backslash }
  635. for i:=0 to strlen(p) do
  636. if p[i]='/' then p[i]:='\';
  637. end;
  638. procedure do_close(handle : longint);
  639. var
  640. regs : trealregs;
  641. begin
  642. regs.realebx:=handle;
  643. if handle<max_files then
  644. begin
  645. openfiles[handle]:=false;
  646. {$ifdef SYSTEMDEBUG}
  647. if assigned(opennames[handle]) and free_closed_names then
  648. begin
  649. sysfreememsize(opennames[handle],strlen(opennames[handle])+1);
  650. opennames[handle]:=nil;
  651. end;
  652. {$endif SYSTEMDEBUG}
  653. end;
  654. regs.realeax:=$3e00;
  655. sysrealintr($21,regs);
  656. if (regs.realflags and carryflag) <> 0 then
  657. GetInOutRes;
  658. end;
  659. procedure do_erase(p : pchar);
  660. var
  661. regs : trealregs;
  662. begin
  663. AllowSlash(p);
  664. syscopytodos(longint(p),strlen(p)+1);
  665. regs.realedx:=tb_offset;
  666. regs.realds:=tb_segment;
  667. {$ifndef RTLLITE}
  668. if LFNSupport then
  669. regs.realeax:=$7141
  670. else
  671. {$endif RTLLITE}
  672. regs.realeax:=$4100;
  673. regs.realesi:=0;
  674. regs.realecx:=0;
  675. sysrealintr($21,regs);
  676. if (regs.realflags and carryflag) <> 0 then
  677. GetInOutRes;
  678. end;
  679. procedure do_rename(p1,p2 : pchar);
  680. var
  681. regs : trealregs;
  682. begin
  683. AllowSlash(p1);
  684. AllowSlash(p2);
  685. if strlen(p1)+strlen(p2)+3>tb_size then
  686. HandleError(217);
  687. sysseg_move(get_ds,longint(p2),dos_selector,tb,strlen(p2)+1);
  688. sysseg_move(get_ds,longint(p1),dos_selector,tb+strlen(p2)+2,strlen(p1)+1);
  689. regs.realedi:=tb_offset;
  690. regs.realedx:=tb_offset + strlen(p2)+2;
  691. regs.realds:=tb_segment;
  692. regs.reales:=tb_segment;
  693. {$ifndef RTLLITE}
  694. if LFNSupport then
  695. regs.realeax:=$7156
  696. else
  697. {$endif RTLLITE}
  698. regs.realeax:=$5600;
  699. regs.realecx:=$ff; { attribute problem here ! }
  700. sysrealintr($21,regs);
  701. if (regs.realflags and carryflag) <> 0 then
  702. GetInOutRes;
  703. end;
  704. function do_write(h,addr,len : longint) : longint;
  705. var
  706. regs : trealregs;
  707. size,
  708. writesize : longint;
  709. begin
  710. writesize:=0;
  711. while len > 0 do
  712. begin
  713. if len>tb_size then
  714. size:=tb_size
  715. else
  716. size:=len;
  717. syscopytodos(addr+writesize,size);
  718. regs.realecx:=size;
  719. regs.realedx:=tb_offset;
  720. regs.realds:=tb_segment;
  721. regs.realebx:=h;
  722. regs.realeax:=$4000;
  723. sysrealintr($21,regs);
  724. if (regs.realflags and carryflag) <> 0 then
  725. begin
  726. GetInOutRes;
  727. exit(writesize);
  728. end;
  729. inc(writesize,regs.realeax);
  730. dec(len,regs.realeax);
  731. { stop when not the specified size is written }
  732. if regs.realeax<size then
  733. break;
  734. end;
  735. Do_Write:=WriteSize;
  736. end;
  737. function do_read(h,addr,len : longint) : longint;
  738. var
  739. regs : trealregs;
  740. size,
  741. readsize : longint;
  742. begin
  743. readsize:=0;
  744. while len > 0 do
  745. begin
  746. if len>tb_size then
  747. size:=tb_size
  748. else
  749. size:=len;
  750. regs.realecx:=size;
  751. regs.realedx:=tb_offset;
  752. regs.realds:=tb_segment;
  753. regs.realebx:=h;
  754. regs.realeax:=$3f00;
  755. sysrealintr($21,regs);
  756. if (regs.realflags and carryflag) <> 0 then
  757. begin
  758. GetInOutRes;
  759. do_read:=0;
  760. exit;
  761. end;
  762. syscopyfromdos(addr+readsize,regs.realeax);
  763. inc(readsize,regs.realeax);
  764. dec(len,regs.realeax);
  765. { stop when not the specified size is read }
  766. if regs.realeax<size then
  767. break;
  768. end;
  769. do_read:=readsize;
  770. end;
  771. function do_filepos(handle : longint) : longint;
  772. var
  773. regs : trealregs;
  774. begin
  775. regs.realebx:=handle;
  776. regs.realecx:=0;
  777. regs.realedx:=0;
  778. regs.realeax:=$4201;
  779. sysrealintr($21,regs);
  780. if (regs.realflags and carryflag) <> 0 then
  781. Begin
  782. GetInOutRes;
  783. do_filepos:=0;
  784. end
  785. else
  786. do_filepos:=lo(regs.realedx) shl 16+lo(regs.realeax);
  787. end;
  788. procedure do_seek(handle,pos : longint);
  789. var
  790. regs : trealregs;
  791. begin
  792. regs.realebx:=handle;
  793. regs.realecx:=pos shr 16;
  794. regs.realedx:=pos and $ffff;
  795. regs.realeax:=$4200;
  796. sysrealintr($21,regs);
  797. if (regs.realflags and carryflag) <> 0 then
  798. GetInOutRes;
  799. end;
  800. function do_seekend(handle:longint):longint;
  801. var
  802. regs : trealregs;
  803. begin
  804. regs.realebx:=handle;
  805. regs.realecx:=0;
  806. regs.realedx:=0;
  807. regs.realeax:=$4202;
  808. sysrealintr($21,regs);
  809. if (regs.realflags and carryflag) <> 0 then
  810. Begin
  811. GetInOutRes;
  812. do_seekend:=0;
  813. end
  814. else
  815. do_seekend:=lo(regs.realedx) shl 16+lo(regs.realeax);
  816. end;
  817. function do_filesize(handle : longint) : longint;
  818. var
  819. aktfilepos : longint;
  820. begin
  821. aktfilepos:=do_filepos(handle);
  822. do_filesize:=do_seekend(handle);
  823. do_seek(handle,aktfilepos);
  824. end;
  825. { truncate at a given position }
  826. procedure do_truncate (handle,pos:longint);
  827. var
  828. regs : trealregs;
  829. begin
  830. do_seek(handle,pos);
  831. regs.realecx:=0;
  832. regs.realedx:=tb_offset;
  833. regs.realds:=tb_segment;
  834. regs.realebx:=handle;
  835. regs.realeax:=$4000;
  836. sysrealintr($21,regs);
  837. if (regs.realflags and carryflag) <> 0 then
  838. GetInOutRes;
  839. end;
  840. {$ifndef RTLLITE}
  841. const
  842. FileHandleCount : longint = 20;
  843. function Increase_file_handle_count : boolean;
  844. var
  845. regs : trealregs;
  846. begin
  847. Inc(FileHandleCount,10);
  848. regs.realebx:=FileHandleCount;
  849. regs.realeax:=$6700;
  850. sysrealintr($21,regs);
  851. if (regs.realflags and carryflag) <> 0 then
  852. Increase_file_handle_count:=false
  853. else
  854. Increase_file_handle_count:=true;
  855. end;
  856. {$endif not RTLLITE}
  857. procedure do_open(var f;p:pchar;flags:longint);
  858. {
  859. filerec and textrec have both handle and mode as the first items so
  860. they could use the same routine for opening/creating.
  861. when (flags and $100) the file will be append
  862. when (flags and $1000) the file will be truncate/rewritten
  863. when (flags and $10000) there is no check for close (needed for textfiles)
  864. }
  865. var
  866. regs : trealregs;
  867. action : longint;
  868. begin
  869. AllowSlash(p);
  870. { close first if opened }
  871. if ((flags and $10000)=0) then
  872. begin
  873. case filerec(f).mode of
  874. fminput,fmoutput,fminout : Do_Close(filerec(f).handle);
  875. fmclosed : ;
  876. else
  877. begin
  878. inoutres:=102; {not assigned}
  879. exit;
  880. end;
  881. end;
  882. end;
  883. { reset file handle }
  884. filerec(f).handle:=UnusedHandle;
  885. action:=$1;
  886. { convert filemode to filerec modes }
  887. case (flags and 3) of
  888. 0 : filerec(f).mode:=fminput;
  889. 1 : filerec(f).mode:=fmoutput;
  890. 2 : filerec(f).mode:=fminout;
  891. end;
  892. if (flags and $1000)<>0 then
  893. action:=$12; {create file function}
  894. { empty name is special }
  895. if p[0]=#0 then
  896. begin
  897. case filerec(f).mode of
  898. fminput : filerec(f).handle:=StdInputHandle;
  899. fmappend,
  900. fmoutput : begin
  901. filerec(f).handle:=StdOutputHandle;
  902. filerec(f).mode:=fmoutput; {fool fmappend}
  903. end;
  904. end;
  905. exit;
  906. end;
  907. { real dos call }
  908. syscopytodos(longint(p),strlen(p)+1);
  909. {$ifndef RTLLITE}
  910. if LFNSupport then
  911. regs.realeax:=$716c
  912. else
  913. {$endif RTLLITE}
  914. regs.realeax:=$6c00;
  915. regs.realedx:=action;
  916. regs.realds:=tb_segment;
  917. regs.realesi:=tb_offset;
  918. regs.realebx:=$2000+(flags and $ff);
  919. regs.realecx:=$20;
  920. sysrealintr($21,regs);
  921. {$ifndef RTLLITE}
  922. if (regs.realflags and carryflag) <> 0 then
  923. if (regs.realeax and $ffff)=4 then
  924. if Increase_file_handle_count then
  925. begin
  926. { Try again }
  927. if LFNSupport then
  928. regs.realeax:=$716c
  929. else
  930. regs.realeax:=$6c00;
  931. regs.realedx:=action;
  932. regs.realds:=tb_segment;
  933. regs.realesi:=tb_offset;
  934. regs.realebx:=$2000+(flags and $ff);
  935. regs.realecx:=$20;
  936. sysrealintr($21,regs);
  937. end;
  938. {$endif RTLLITE}
  939. if (regs.realflags and carryflag) <> 0 then
  940. begin
  941. GetInOutRes;
  942. exit;
  943. end
  944. else
  945. begin
  946. filerec(f).handle:=regs.realeax;
  947. {$ifndef RTLLITE}
  948. { for systems that have more then 20 by default ! }
  949. if regs.realeax>FileHandleCount then
  950. FileHandleCount:=regs.realeax;
  951. {$endif RTLLITE}
  952. end;
  953. if regs.realeax<max_files then
  954. begin
  955. {$ifdef SYSTEMDEBUG}
  956. if openfiles[regs.realeax] and
  957. assigned(opennames[regs.realeax]) then
  958. begin
  959. Writeln(stderr,'file ',opennames[regs.realeax],'(',regs.realeax,') not closed but handle reused!');
  960. sysfreememsize(opennames[regs.realeax],strlen(opennames[regs.realeax])+1);
  961. end;
  962. {$endif SYSTEMDEBUG}
  963. openfiles[regs.realeax]:=true;
  964. {$ifdef SYSTEMDEBUG}
  965. opennames[regs.realeax] := sysgetmem(strlen(p)+1);
  966. move(p^,opennames[regs.realeax]^,strlen(p)+1);
  967. {$endif SYSTEMDEBUG}
  968. end;
  969. { append mode }
  970. if (flags and $100)<>0 then
  971. begin
  972. do_seekend(filerec(f).handle);
  973. filerec(f).mode:=fmoutput; {fool fmappend}
  974. end;
  975. end;
  976. function do_isdevice(handle:longint):boolean;
  977. var
  978. regs : trealregs;
  979. begin
  980. regs.realebx:=handle;
  981. regs.realeax:=$4400;
  982. sysrealintr($21,regs);
  983. do_isdevice:=(regs.realedx and $80)<>0;
  984. if (regs.realflags and carryflag) <> 0 then
  985. GetInOutRes;
  986. end;
  987. {*****************************************************************************
  988. UnTyped File Handling
  989. *****************************************************************************}
  990. {$i file.inc}
  991. {*****************************************************************************
  992. Typed File Handling
  993. *****************************************************************************}
  994. {$i typefile.inc}
  995. {*****************************************************************************
  996. Text File Handling
  997. *****************************************************************************}
  998. {$DEFINE EOF_CTRLZ}
  999. {$i text.inc}
  1000. {*****************************************************************************
  1001. Generic Handling
  1002. *****************************************************************************}
  1003. {$ifdef TEST_GENERIC}
  1004. {$i generic.inc}
  1005. {$endif TEST_GENERIC}
  1006. {*****************************************************************************
  1007. Directory Handling
  1008. *****************************************************************************}
  1009. procedure DosDir(func:byte;const s:string);
  1010. var
  1011. buffer : array[0..255] of char;
  1012. regs : trealregs;
  1013. begin
  1014. move(s[1],buffer,length(s));
  1015. buffer[length(s)]:=#0;
  1016. AllowSlash(pchar(@buffer));
  1017. syscopytodos(longint(@buffer),length(s)+1);
  1018. regs.realedx:=tb_offset;
  1019. regs.realds:=tb_segment;
  1020. {$ifndef RTLLITE}
  1021. if LFNSupport then
  1022. regs.realeax:=$7100+func
  1023. else
  1024. {$endif RTLLITE}
  1025. regs.realeax:=func shl 8;
  1026. sysrealintr($21,regs);
  1027. if (regs.realflags and carryflag) <> 0 then
  1028. GetInOutRes;
  1029. end;
  1030. procedure mkdir(const s : string);[IOCheck];
  1031. begin
  1032. If InOutRes <> 0 then
  1033. exit;
  1034. DosDir($39,s);
  1035. end;
  1036. procedure rmdir(const s : string);[IOCheck];
  1037. begin
  1038. If InOutRes <> 0 then
  1039. exit;
  1040. DosDir($3a,s);
  1041. end;
  1042. procedure chdir(const s : string);[IOCheck];
  1043. var
  1044. regs : trealregs;
  1045. begin
  1046. If InOutRes <> 0 then
  1047. exit;
  1048. { First handle Drive changes }
  1049. if (length(s)>=2) and (s[2]=':') then
  1050. begin
  1051. regs.realedx:=(ord(s[1]) and (not 32))-ord('A');
  1052. regs.realeax:=$0e00;
  1053. sysrealintr($21,regs);
  1054. regs.realeax:=$1900;
  1055. sysrealintr($21,regs);
  1056. if byte(regs.realeax)<>byte(regs.realedx) then
  1057. begin
  1058. Inoutres:=15;
  1059. exit;
  1060. end;
  1061. { DosDir($3b,'c:') give Path not found error on
  1062. pure DOS PM }
  1063. if length(s)=2 then
  1064. exit;
  1065. end;
  1066. { do the normal dos chdir }
  1067. DosDir($3b,s);
  1068. end;
  1069. procedure getdir(drivenr : byte;var dir : shortstring);
  1070. var
  1071. temp : array[0..255] of char;
  1072. i : longint;
  1073. regs : trealregs;
  1074. begin
  1075. regs.realedx:=drivenr;
  1076. regs.realesi:=tb_offset;
  1077. regs.realds:=tb_segment;
  1078. {$ifndef RTLLITE}
  1079. if LFNSupport then
  1080. regs.realeax:=$7147
  1081. else
  1082. {$endif RTLLITE}
  1083. regs.realeax:=$4700;
  1084. sysrealintr($21,regs);
  1085. if (regs.realflags and carryflag) <> 0 then
  1086. Begin
  1087. GetInOutRes;
  1088. exit;
  1089. end
  1090. else
  1091. syscopyfromdos(longint(@temp),251);
  1092. { conversion to Pascal string including slash conversion }
  1093. i:=0;
  1094. while (temp[i]<>#0) do
  1095. begin
  1096. if temp[i]='/' then
  1097. temp[i]:='\';
  1098. dir[i+4]:=temp[i];
  1099. inc(i);
  1100. end;
  1101. dir[2]:=':';
  1102. dir[3]:='\';
  1103. dir[0]:=char(i+3);
  1104. { upcase the string }
  1105. if not FileNameCaseSensitive then
  1106. dir:=upcase(dir);
  1107. if drivenr<>0 then { Drive was supplied. We know it }
  1108. dir[1]:=char(65+drivenr-1)
  1109. else
  1110. begin
  1111. { We need to get the current drive from DOS function 19H }
  1112. { because the drive was the default, which can be unknown }
  1113. regs.realeax:=$1900;
  1114. sysrealintr($21,regs);
  1115. i:= (regs.realeax and $ff) + ord('A');
  1116. dir[1]:=chr(i);
  1117. end;
  1118. end;
  1119. {*****************************************************************************
  1120. SystemUnit Initialization
  1121. *****************************************************************************}
  1122. {$ifndef RTLLITE}
  1123. function CheckLFN:boolean;
  1124. var
  1125. regs : TRealRegs;
  1126. RootName : pchar;
  1127. begin
  1128. { Check LFN API on drive c:\ }
  1129. RootName:='C:\';
  1130. syscopytodos(longint(RootName),strlen(RootName)+1);
  1131. { Call 'Get Volume Information' ($71A0) }
  1132. regs.realeax:=$71a0;
  1133. regs.reales:=tb_segment;
  1134. regs.realedi:=tb_offset;
  1135. regs.realecx:=32;
  1136. regs.realds:=tb_segment;
  1137. regs.realedx:=tb_offset;
  1138. regs.realflags:=carryflag;
  1139. sysrealintr($21,regs);
  1140. { If carryflag=0 and LFN API bit in ebx is set then use Long file names }
  1141. CheckLFN:=(regs.realflags and carryflag=0) and (regs.realebx and $4000=$4000);
  1142. end;
  1143. {$endif RTLLITE}
  1144. {$ifdef MT}
  1145. {$I thread.inc}
  1146. {$endif MT}
  1147. var
  1148. temp_int : tseginfo;
  1149. Begin
  1150. { save old int 0 and 75 }
  1151. get_pm_interrupt($00,old_int00);
  1152. get_pm_interrupt($75,old_int75);
  1153. temp_int.segment:=get_cs;
  1154. temp_int.offset:=@new_int00;
  1155. set_pm_interrupt($00,temp_int);
  1156. { temp_int.offset:=@new_int75;
  1157. set_pm_interrupt($75,temp_int); }
  1158. { to test stack depth }
  1159. loweststack:=maxlongint;
  1160. { Setup heap }
  1161. InitHeap;
  1162. {$ifdef MT}
  1163. { before this, you can't use thread vars !!!! }
  1164. { threadvarblocksize is calculate before the initialization }
  1165. { of the system unit }
  1166. mainprogramthreadblock := sysgetmem(threadvarblocksize);
  1167. {$endif MT}
  1168. InitExceptions;
  1169. { Setup stdin, stdout and stderr }
  1170. OpenStdIO(Input,fmInput,StdInputHandle);
  1171. OpenStdIO(Output,fmOutput,StdOutputHandle);
  1172. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  1173. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  1174. { Setup environment and arguments }
  1175. Setup_Environment;
  1176. Setup_Arguments;
  1177. { Use LFNSupport LFN }
  1178. LFNSupport:=CheckLFN;
  1179. if LFNSupport then
  1180. FileNameCaseSensitive:=true;
  1181. { Reset IO Error }
  1182. InOutRes:=0;
  1183. End.
  1184. {
  1185. $Log$
  1186. Revision 1.28 2000-01-07 16:41:32 daniel
  1187. * copyright 2000
  1188. Revision 1.27 2000/01/07 16:32:23 daniel
  1189. * copyright 2000 added
  1190. Revision 1.26 1999/12/20 22:22:41 pierre
  1191. * better closing of left open files
  1192. Revision 1.25 1999/12/17 23:11:48 pierre
  1193. * fix for bug754 : increase now dynamically max open handles
  1194. Revision 1.24 1999/12/01 22:57:30 peter
  1195. * cmdline support
  1196. Revision 1.23 1999/11/25 16:24:56 pierre
  1197. * avoid a problem with ChDir('c:') on pure DOS
  1198. Revision 1.22 1999/11/06 14:38:24 peter
  1199. * truncated log
  1200. Revision 1.21 1999/10/31 09:34:48 jonas
  1201. * updated for new syntax of sysgetmem
  1202. Revision 1.20 1999/10/28 09:53:19 peter
  1203. * create can also open file in fminout
  1204. Revision 1.19 1999/09/20 12:40:20 pierre
  1205. * adapted to new heaph
  1206. Revision 1.18 1999/09/10 17:14:09 peter
  1207. * better errorcode returning using int21h,5900
  1208. Revision 1.17 1999/09/10 15:40:33 peter
  1209. * fixed do_open flags to be > $100, becuase filemode can be upto 255
  1210. Revision 1.16 1999/09/08 16:09:18 peter
  1211. * do_isdevice not called if already error
  1212. Revision 1.15 1999/08/19 14:03:16 pierre
  1213. * use sysgetmem for startup and debug allocations
  1214. Revision 1.14 1999/07/19 07:57:49 michael
  1215. + Small fix from Michael Baikov in setup_params
  1216. Revision 1.13 1999/05/19 16:54:21 pierre
  1217. * closes all handles >+ 5
  1218. Revision 1.12 1999/05/17 21:52:33 florian
  1219. * most of the Object Pascal stuff moved to the system unit
  1220. Revision 1.11 1999/05/04 23:28:40 pierre
  1221. SYSTEM_DEBUG_STARTUP used to output args and env at start
  1222. Revision 1.10 1999/04/28 11:42:45 peter
  1223. + FileNameCaseSensetive boolean
  1224. Revision 1.9 1999/04/28 06:01:25 florian
  1225. * define MT for multithreading introduced
  1226. Revision 1.8 1999/04/08 12:23:02 peter
  1227. * removed os.inc
  1228. Revision 1.7 1999/03/10 22:15:28 florian
  1229. + system.cmdline variable for go32v2 and win32 added
  1230. Revision 1.6 1999/03/01 15:40:52 peter
  1231. * use external names
  1232. * removed all direct assembler modes
  1233. Revision 1.5 1999/01/18 10:05:50 pierre
  1234. + system_exit procedure added
  1235. Revision 1.4 1998/12/30 22:17:59 peter
  1236. * fixed mem decls to use $0:$0
  1237. Revision 1.3 1998/12/28 15:50:45 peter
  1238. + stdout, which is needed when you write something in the system unit
  1239. to the screen. Like the runtime error
  1240. Revision 1.2 1998/12/21 14:22:02 pierre
  1241. * old_int?? transformed to cvar to be readable by dpmiexcp
  1242. }