sysos2.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. {****************************************************************************
  2. FPK-Pascal -- OS/2 runtime library
  3. Copyright (c) 1993,95 by Florian Kl„mpfl
  4. Copyright (c) 1997 by Dani‰l Mantione
  5. FPK-Pascal is distributed under the GNU Public License v2. So is this unit.
  6. The GNU Public License requires you to distribute the source code of this
  7. unit with any product that uses it. We grant you an exception to this, and
  8. that is, when you compile a program with the FPK Pascal compiler, you do not
  9. need to ship source code with that program, AS LONG AS YOU ARE USING
  10. UNMODIFIED CODE! If you modify this code, you MUST change the next line:
  11. <This an official, unmodified FPK Pascal source code file.>
  12. Send us your modified files, we can work together if you want!
  13. FPK-Pascal is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. Library GNU General Public License for more details.
  17. You should have received a copy of the Library GNU General Public License
  18. along with FPK-Pascal; see the file COPYING.LIB. If not, write to
  19. the Free Software Foundation, 59 Temple Place - Suite 330,
  20. Boston, MA 02111-1307, USA.
  21. ****************************************************************************}
  22. unit sysos2;
  23. {Changelog:
  24. People:
  25. DM - Dani‰l Mantione
  26. Date: Description of change: Changed by:
  27. - First released version 0.1. DM
  28. Coding style:
  29. My coding style is a bit unusual for Pascal. Nevertheless I friendly ask
  30. you to try to make your changes not look all to different. In general,
  31. set your IDE to use tab characters, optimal fill on and a tabsize of 4.}
  32. {$I os.inc}
  33. interface
  34. {Link the startup code.}
  35. {$l prt1.oo2}
  36. {$I SYSTEMH.INC}
  37. {$I heaph.inc}
  38. type Tos=(osDOS,osOS2,osDPMI);
  39. var os_mode:Tos;
  40. first_meg:pointer;
  41. type Psysthreadib=^Tsysthreadib;
  42. Pthreadinfoblock=^Tthreadinfoblock;
  43. Pprocessinfoblock=^Tprocessinfoblock;
  44. Tbytearray=array[0..$ffff] of byte;
  45. Pbytearray=^Tbytearray;
  46. Tsysthreadib=record
  47. tid,
  48. priority,
  49. version:longint;
  50. MCcount,
  51. MCforceflag:word;
  52. end;
  53. Tthreadinfoblock=record
  54. pexchain,
  55. stack,
  56. stacklimit:pointer;
  57. tib2:Psysthreadib;
  58. version,
  59. ordinal:longint;
  60. end;
  61. Tprocessinfoblock=record
  62. pid,
  63. parentpid,
  64. hmte:longint;
  65. cmd,
  66. env:Pbytearray;
  67. flstatus,
  68. ttype:longint;
  69. end;
  70. const UnusedHandle=$ffff;
  71. StdInputHandle=0;
  72. StdOutputHandle=1;
  73. StdErrorHandle=2;
  74. implementation
  75. {$I SYSTEM.INC}
  76. procedure dosgetinfoblocks(var Atib:Pthreadinfoblock;
  77. var Apib:Pprocessinfoblock);
  78. external 'DOSCALLS' index 312;
  79. {This is the correct way to call external assembler procedures.}
  80. procedure syscall;external name '___SYSCALL';
  81. {***************************************************************************
  82. Runtime error checking related routines.
  83. ***************************************************************************}
  84. {$S-}
  85. procedure st1(stack_size:longint);[public,alias: 'STACKCHECK'];
  86. begin
  87. { called when trying to get local stack }
  88. { if the compiler directive $S is set }
  89. {$ASMMODE DIRECT}
  90. asm
  91. movl stack_size,%ebx
  92. movl %esp,%eax
  93. subl %ebx,%eax
  94. {$ifdef SYSTEMDEBUG}
  95. movl U_SYSOS2_LOWESTSTACK,%ebx
  96. cmpl %eax,%ebx
  97. jb Lis_not_lowest
  98. movl %eax,U_SYSOS2_LOWESTSTACK
  99. Lis_not_lowest:
  100. {$endif SYSTEMDEBUG}
  101. cmpb $2,U_SYSOS2_OS_MODE
  102. jne Lrunning_in_dos
  103. movl U_SYSOS2_STACKBOTTOM,%ebx
  104. jmp Lrunning_in_os2
  105. Lrunning_in_dos:
  106. movl __heap_brk,%ebx
  107. Lrunning_in_os2:
  108. cmpl %eax,%ebx
  109. jae Lshort_on_stack
  110. leave
  111. ret $4
  112. Lshort_on_stack:
  113. end ['EAX','EBX'];
  114. {$ASMMODE ATT}
  115. { this needs a local variable }
  116. { so the function called itself !! }
  117. { Writeln('low in stack ');}
  118. RunError(202);
  119. end;
  120. {no stack check in system }
  121. {****************************************************************************
  122. Miscelleanious related routines.
  123. ****************************************************************************}
  124. procedure halt(errnum:byte);
  125. begin
  126. asm
  127. movb $0x4c,%ah
  128. movb errnum,%al
  129. call syscall
  130. end;
  131. end;
  132. {$asmmode direct}
  133. function paramcount:longint;assembler;
  134. asm
  135. movl _argc,%eax
  136. decl %eax
  137. end ['EAX'];
  138. function paramstr(l:longint):string;
  139. function args:pointer;assembler;
  140. asm
  141. movl _argv,%eax
  142. end ['EAX'];
  143. var p:^Pchar;
  144. begin
  145. if (l>=0) and (l<=paramcount) then
  146. begin
  147. p:=args;
  148. paramstr:=strpas(p[l]);
  149. end
  150. else paramstr:='';
  151. end;
  152. {$asmmode att}
  153. procedure randomize;
  154. var hl:longint;
  155. begin
  156. asm
  157. movb $0x2c,%ah
  158. call syscall
  159. movw %cx,-4(%ebp)
  160. movw %dx,-2(%ebp)
  161. end;
  162. randseed:=hl;
  163. end;
  164. {****************************************************************************
  165. Heap management releated routines.
  166. ****************************************************************************}
  167. { this function allows to extend the heap by calling
  168. syscall $7f00 resizes the brk area}
  169. function sbrk(size:longint):longint;
  170. begin
  171. asm
  172. movl size,%edx
  173. movw $0x7f00,%ax
  174. call syscall
  175. movl %eax,__RESULT
  176. end;
  177. end;
  178. {$ASMMODE direct}
  179. function getheapstart:pointer;assembler;
  180. asm
  181. movl __heap_base,%eax
  182. end ['EAX'];
  183. {$ASMMODE att}
  184. {$i heap.inc}
  185. {****************************************************************************
  186. Low Level File Routines
  187. ****************************************************************************}
  188. procedure allowslash(p:Pchar);
  189. {Allow slash as backslash.}
  190. var i:longint;
  191. begin
  192. for i:=0 to strlen(p) do
  193. if p[i]='/' then p[i]:='\';
  194. end;
  195. procedure do_close(h:longint);
  196. begin
  197. asm
  198. movb $0x3e,%ah
  199. mov h,%ebx
  200. call syscall
  201. end;
  202. end;
  203. procedure do_erase(p:Pchar);
  204. begin
  205. allowslash(p);
  206. asm
  207. movl 8(%ebp),%edx
  208. movb $0x41,%ah
  209. call syscall
  210. jnc .LERASE1
  211. movw %ax,inoutres;
  212. .LERASE1:
  213. end;
  214. end;
  215. procedure do_rename(p1,p2:Pchar);
  216. begin
  217. allowslash(p1);
  218. allowslash(p2);
  219. asm
  220. movl 8(%ebp),%edx
  221. movl 12(%ebp),%edi
  222. movb $0x56,%ah
  223. call syscall
  224. jnc .LRENAME1
  225. movw %ax,inoutres;
  226. .LRENAME1:
  227. end;
  228. end;
  229. function do_read(h,addr,len:longint):longint;
  230. begin
  231. asm
  232. movl 16(%ebp),%ecx
  233. movl 12(%ebp),%edx
  234. movl 8(%ebp),%ebx
  235. movb $0x3f,%ah
  236. call syscall
  237. jnc .LDOSREAD1
  238. movw %ax,inoutres;
  239. xorl %eax,%eax
  240. .LDOSREAD1:
  241. leave
  242. ret $12
  243. end;
  244. end;
  245. function do_write(h,addr,len:longint) : longint;
  246. begin
  247. asm
  248. movl 16(%ebp),%ecx
  249. movl 12(%ebp),%edx
  250. movl 8(%ebp),%ebx
  251. movb $0x40,%ah
  252. call syscall
  253. jnc .LDOSWRITE1
  254. movw %ax,inoutres;
  255. .LDOSWRITE1:
  256. movl %eax,-4(%ebp)
  257. end;
  258. end;
  259. function do_filepos(handle:longint):longint;
  260. begin
  261. asm
  262. movw $0x4201,%ax
  263. movl 8(%ebp),%ebx
  264. xorl %edx,%edx
  265. call syscall
  266. jnc .LDOSFILEPOS
  267. movw %ax,inoutres;
  268. xorl %eax,%eax
  269. .LDOSFILEPOS:
  270. leave
  271. ret $4
  272. end;
  273. end;
  274. procedure do_seek(handle,pos:longint);
  275. begin
  276. asm
  277. movw $0x4200,%ax
  278. movl 8(%ebp),%ebx
  279. movl 12(%ebp),%edx
  280. call syscall
  281. jnc .LDOSSEEK1
  282. movw %ax,inoutres;
  283. .LDOSSEEK1:
  284. leave
  285. ret $8
  286. end;
  287. end;
  288. function do_seekend(handle:longint):longint;
  289. begin
  290. asm
  291. movw $0x4202,%ax
  292. movl 8(%ebp),%ebx
  293. xorl %edx,%edx
  294. call syscall
  295. jnc .Lset_at_end1
  296. movw %ax,inoutres;
  297. xorl %eax,%eax
  298. .Lset_at_end1:
  299. leave
  300. ret $4
  301. end;
  302. end;
  303. function do_filesize(handle:longint):longint;
  304. var aktfilepos:longint;
  305. begin
  306. aktfilepos:=do_filepos(handle);
  307. do_filesize:=do_seekend(handle);
  308. do_seek(handle,aktfilepos);
  309. end;
  310. procedure do_truncate(handle,pos:longint);
  311. begin
  312. asm
  313. movl $0x4200,%eax
  314. movl 8(%ebp),%ebx
  315. movl 12(%ebp),%edx
  316. call syscall
  317. jc .LTruncate1
  318. movl 8(%ebp),%ebx
  319. movl 12(%ebp),%edx
  320. movl %ebp,%edx
  321. xorl %ecx,%ecx
  322. movb $0x40,%ah
  323. call syscall
  324. jnc .LTruncate2
  325. .LTruncate1:
  326. movw %ax,inoutres;
  327. .LTruncate2:
  328. leave
  329. ret $8
  330. end;
  331. end;
  332. procedure do_open(var f;p:pchar;flags:longint);
  333. {
  334. filerec and textrec have both handle and mode as the first items so
  335. they could use the same routine for opening/creating.
  336. when (flags and $10) the file will be append
  337. when (flags and $100) the file will be truncate/rewritten
  338. when (flags and $1000) there is no check for close (needed for textfiles)
  339. }
  340. var oflags:byte;
  341. begin
  342. allowslash(p);
  343. { close first if opened }
  344. if ((flags and $1000)=0) then
  345. begin
  346. case filerec(f).mode of
  347. fminput,fmoutput,fminout : Do_Close(filerec(f).handle);
  348. fmclosed:;
  349. else
  350. begin
  351. inoutres:=102; {not assigned}
  352. exit;
  353. end;
  354. end;
  355. end;
  356. { reset file handle }
  357. filerec(f).handle:=high(word);
  358. oflags:=2;
  359. { convert filemode to filerec modes }
  360. case (flags and 3) of
  361. 0 : begin
  362. filerec(f).mode:=fminput;
  363. oflags:=0;
  364. end;
  365. 1 : filerec(f).mode:=fmoutput;
  366. 2 : filerec(f).mode:=fminout;
  367. end;
  368. if (flags and $100)<>0 then
  369. begin
  370. filerec(f).mode:=fmoutput;
  371. oflags:=2;
  372. end
  373. else
  374. if (flags and $10)<>0 then
  375. begin
  376. filerec(f).mode:=fmoutput;
  377. oflags:=2;
  378. end;
  379. { empty name is special }
  380. if p[0]=#0 then
  381. begin
  382. case filerec(f).mode of
  383. fminput:filerec(f).handle:=StdInputHandle;
  384. fmappend,fmoutput : begin
  385. filerec(f).handle:=StdOutputHandle;
  386. filerec(f).mode:=fmoutput; {fool fmappend}
  387. end;
  388. end;
  389. exit;
  390. end;
  391. if (flags and $100)<>0 then
  392. {Use create function.}
  393. asm
  394. movb $0x3c,%ah
  395. movl p,%edx
  396. xorw %cx,%cx
  397. call syscall
  398. jnc .LOPEN1
  399. movw %ax,inoutres;
  400. movw $0xffff,%ax
  401. .LOPEN1:
  402. movl f,%edx
  403. movw %ax,(%edx)
  404. end
  405. else
  406. {Use open function.}
  407. asm
  408. movb $0x3d,%ah
  409. movb oflags,%al
  410. movl p,%edx
  411. call syscall
  412. jnc .LOPEN2
  413. movw %ax,inoutres;
  414. movw $0xffff,%ax
  415. .LOPEN2:
  416. movl f,%edx
  417. movw %ax,(%edx)
  418. end;
  419. if (flags and $10)<>0 then
  420. do_seekend(filerec(f).handle);
  421. end;
  422. {*****************************************************************************
  423. UnTyped File Handling
  424. *****************************************************************************}
  425. {$i file.inc}
  426. {*****************************************************************************
  427. Typed File Handling
  428. *****************************************************************************}
  429. {$i typefile.inc}
  430. {*****************************************************************************
  431. Text File Handling
  432. *****************************************************************************}
  433. {$DEFINE EOF_CTRLZ}
  434. {$i text.inc}
  435. {****************************************************************************
  436. Directory related routines.
  437. ****************************************************************************}
  438. {*****************************************************************************
  439. Directory Handling
  440. *****************************************************************************}
  441. procedure dosdir(func:byte;const s:string);
  442. var buffer:array[0..255] of char;
  443. begin
  444. move(s[1],buffer,length(s));
  445. buffer[length(s)]:=#0;
  446. allowslash(Pchar(@buffer));
  447. asm
  448. leal buffer,%edx
  449. movb 8(%ebp),%ah
  450. call syscall
  451. jnc .LDOS_DIRS1
  452. movw %ax,inoutres;
  453. .LDOS_DIRS1:
  454. end;
  455. end;
  456. procedure mkdir(const s : string);
  457. begin
  458. DosDir($39,s);
  459. end;
  460. procedure rmdir(const s : string);
  461. begin
  462. DosDir($3a,s);
  463. end;
  464. procedure chdir(const s : string);
  465. begin
  466. DosDir($3b,s);
  467. end;
  468. procedure getdir(drivenr : byte;var dir : string);
  469. {Written by Michael Van Canneyt.}
  470. var temp:array[0..255] of char;
  471. sof:Pchar;
  472. i:byte;
  473. begin
  474. sof:=pchar(@dir[4]);
  475. { dir[1..3] will contain '[drivenr]:\', but is not }
  476. { supplied by DOS, so we let dos string start at }
  477. { dir[4] }
  478. { Get dir from drivenr : 0=default, 1=A etc... }
  479. asm
  480. movb drivenr,%dl
  481. movl sof,%esi
  482. mov $0x47,%ah
  483. call syscall
  484. end;
  485. { Now Dir should be filled with directory in ASCIIZ, }
  486. { starting from dir[4] }
  487. dir[0]:=#3;
  488. dir[2]:=':';
  489. dir[3]:='\';
  490. i:=4;
  491. {Conversion to Pascal string }
  492. while (dir[i]<>#0) do
  493. begin
  494. { convert path name to DOS }
  495. if dir[i]='/' then
  496. dir[i]:='\';
  497. dir[0]:=char(i);
  498. inc(i);
  499. end;
  500. { upcase the string (FPKPascal function) }
  501. dir:=upcase(dir);
  502. if drivenr<>0 then { Drive was supplied. We know it }
  503. dir[1]:=char(65+drivenr-1)
  504. else
  505. begin
  506. { We need to get the current drive from DOS function 19H }
  507. { because the drive was the default, which can be unknown }
  508. asm
  509. movb $0x19,%ah
  510. call syscall
  511. addb $65,%al
  512. movb %al,i
  513. end;
  514. dir[1]:=char(i);
  515. end;
  516. end;
  517. {****************************************************************************
  518. System unit initialization.
  519. ****************************************************************************}
  520. procedure OpenStdIO(var f:text;mode:word;hdl:longint);
  521. begin
  522. Assign(f,'');
  523. TextRec(f).Handle:=hdl;
  524. TextRec(f).Mode:=mode;
  525. TextRec(f).InOutFunc:=@FileInOutFunc;
  526. TextRec(f).FlushFunc:=@FileInOutFunc;
  527. TextRec(f).Closefunc:=@fileclosefunc;
  528. end;
  529. var pib:Pprocessinfoblock;
  530. tib:Pthreadinfoblock;
  531. begin
  532. {Determine the operating system we are running on.}
  533. asm
  534. movw $0x7f0a,%ax
  535. call syscall
  536. testw $512,%bx {Bit 9 is OS/2 flag.}
  537. setnzl os_mode
  538. testw $4096,%bx
  539. jz .LnoRSX
  540. movl $2,os_mode
  541. .LnoRSX:
  542. end;
  543. {$ASMMODE DIRECT}
  544. {Enable the brk area by initializing it with the initial heap size.}
  545. asm
  546. movw $0x7f01,%ax
  547. movl HEAPSIZE,%edx
  548. addl __heap_base,%edx
  549. call ___SYSCALL
  550. cmpl $-1,%eax
  551. jnz Lheapok
  552. pushl $204
  553. {call RUNERROR$$WORD}
  554. Lheapok:
  555. end;
  556. {$ASMMODE ATT}
  557. {Now request, if we are running under DOS,
  558. read-access to the first meg. of memory.}
  559. if os_mode in [osDOS,osDPMI] then
  560. asm
  561. movw $0x7f13,%ax
  562. xorl %ebx,%ebx
  563. movl $0xfff,%ecx
  564. xorl %edx,%edx
  565. call syscall
  566. movl %eax,first_meg
  567. end
  568. else
  569. first_meg:=nil;
  570. {At 0.9.2, case for enumeration does not work.}
  571. case os_mode of
  572. osDOS:
  573. stackbottom:=0; {In DOS mode, heap_brk is also the
  574. stack bottom.}
  575. osOS2:
  576. begin
  577. dosgetinfoblocks(tib,pib);
  578. stackbottom:=longint(tib^.stack);
  579. end;
  580. osDPMI:
  581. stackbottom:=0; {Not sure how to get it, but seems to be
  582. always zero.}
  583. end;
  584. exitproc:=nil;
  585. {Initialize the heap.}
  586. initheap;
  587. { to test stack depth }
  588. loweststack:=maxlongint;
  589. OpenStdIO(Input,fmInput,StdInputHandle);
  590. OpenStdIO(Output,fmOutput,StdOutputHandle);
  591. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  592. { kein Ein- Ausgabefehler }
  593. inoutres:=0;
  594. end.