sysos2.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  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. { die betriebssystemunabhangigen Implementationen einfuegen: }
  76. {$I SYSTEM.INC}
  77. procedure dosgetinfoblocks(var Atib:Pthreadinfoblock;
  78. var Apib:Pprocessinfoblock);
  79. external 'DOSCALLS' index 312;
  80. {***************************************************************************
  81. Runtime error checking related routines.
  82. ***************************************************************************}
  83. {$S-}
  84. procedure st1(stack_size:longint);[public,alias: 'STACKCHECK'];
  85. begin
  86. { called when trying to get local stack }
  87. { if the compiler directive $S is set }
  88. asm
  89. movl stack_size,%ebx
  90. movl %esp,%eax
  91. subl %ebx,%eax
  92. {$ifdef SYSTEMDEBUG}
  93. movl U_SYSOS2_LOWESTSTACK,%ebx
  94. cmpl %eax,%ebx
  95. jb _is_not_lowest
  96. movl %eax,U_SYSOS2_LOWESTSTACK
  97. _is_not_lowest:
  98. {$endif SYSTEMDEBUG}
  99. cmpb $2,U_SYSOS2_OS_MODE
  100. jne _running_in_dos
  101. movl U_SYSOS2_STACKBOTTOM,%ebx
  102. jmp _running_in_os2
  103. _running_in_dos:
  104. movl __heap_brk,%ebx
  105. _running_in_os2:
  106. cmpl %eax,%ebx
  107. jae __short_on_stack
  108. leave
  109. ret $4
  110. __short_on_stack:
  111. end ['EAX','EBX'];
  112. { this needs a local variable }
  113. { so the function called itself !! }
  114. { Writeln('low in stack ');}
  115. RunError(202);
  116. end;
  117. {no stack check in system }
  118. {****************************************************************************
  119. Miscelleanious related routines.
  120. ****************************************************************************}
  121. procedure halt(errnum:byte);
  122. begin
  123. asm
  124. movb $0x4c,%ah
  125. movb errnum,%al
  126. call ___syscall
  127. end;
  128. end;
  129. function paramcount:longint;
  130. begin
  131. asm
  132. movl _argc,%eax
  133. decl %eax
  134. leave
  135. ret
  136. end ['EAX'];
  137. end;
  138. function paramstr(l:longint):string;
  139. function args:pointer;
  140. begin
  141. asm
  142. movl _argv,%eax
  143. leave
  144. ret
  145. end ['EAX'];
  146. end;
  147. var p:^Pchar;
  148. begin
  149. if (l>=0) and (l<=paramcount) then
  150. begin
  151. p:=args;
  152. paramstr:=strpas(p[l]);
  153. end
  154. else paramstr:='';
  155. end;
  156. procedure randomize;
  157. var hl:longint;
  158. begin
  159. asm
  160. movb $0x2c,%ah
  161. call ___syscall
  162. movw %cx,-4(%ebp)
  163. movw %dx,-2(%ebp)
  164. end;
  165. randseed:=hl;
  166. end;
  167. {****************************************************************************
  168. Heap management releated routines.
  169. ****************************************************************************}
  170. { this function allows to extend the heap by calling
  171. syscall $7f00 resizes the brk area}
  172. function sbrk(size:longint):longint;
  173. begin
  174. asm
  175. movl size,%edx
  176. movl $0x7f00,%ax
  177. int $0x21
  178. movl %eax,__RESULT
  179. end;
  180. end;
  181. function getheapstart:pointer;
  182. begin
  183. asm
  184. movl __heap_base,%eax
  185. leave
  186. ret
  187. end ['EAX'];
  188. end;
  189. {$i heap.inc}
  190. {****************************************************************************
  191. Low Level File Routines
  192. ****************************************************************************}
  193. procedure allowslash(p:Pchar);
  194. {Allow slash as backslash.}
  195. var i:longint;
  196. begin
  197. for i:=0 to strlen(p) do
  198. if p[i]='/' then p[i]:='\';
  199. end;
  200. procedure do_close(h:longint);
  201. begin
  202. asm
  203. movb $0x3e,%ah
  204. mov h,%ebx
  205. call ___syscall
  206. end;
  207. end;
  208. procedure do_erase(p:Pchar);
  209. begin
  210. allowslash(p);
  211. asm
  212. movl 8(%ebp),%edx
  213. movb $0x41,%ah
  214. call ___syscall
  215. jnc LERASE1
  216. movw %ax,U_SYSOS2_INOUTRES;
  217. LERASE1:
  218. end;
  219. end;
  220. procedure do_rename(p1,p2:Pchar);
  221. begin
  222. allowslash(p1);
  223. allowslash(p2);
  224. asm
  225. movl 8(%ebp),%edx
  226. movl 12(%ebp),%edi
  227. movb $0x56,%ah
  228. call ___syscall
  229. jnc LRENAME1
  230. movw %ax,U_SYSOS2_INOUTRES;
  231. LRENAME1:
  232. end;
  233. end;
  234. function do_read(h,addr,len:longint):longint;
  235. begin
  236. asm
  237. movl 16(%ebp),%ecx
  238. movl 12(%ebp),%edx
  239. movl 8(%ebp),%ebx
  240. movb $0x3f,%ah
  241. call ___syscall
  242. jnc LDOSREAD1
  243. movw %ax,U_SYSOS2_INOUTRES;
  244. xorl %eax,%eax
  245. LDOSREAD1:
  246. leave
  247. ret $12
  248. end;
  249. end;
  250. function do_write(h,addr,len:longint) : longint;
  251. begin
  252. asm
  253. movl 16(%ebp),%ecx
  254. movl 12(%ebp),%edx
  255. movl 8(%ebp),%ebx
  256. movb $0x40,%ah
  257. call ___syscall
  258. jnc LDOSWRITE1
  259. movw %ax,U_SYSOS2_INOUTRES;
  260. LDOSWRITE1:
  261. movl %eax,-4(%ebp)
  262. end;
  263. end;
  264. function do_filepos(handle:longint):longint;
  265. begin
  266. asm
  267. movb $0x42,%ah
  268. movb $0x1,%al
  269. movl 8(%ebp),%ebx
  270. xorl %edx,%edx
  271. call ___syscall
  272. jnc LDOSFILEPOS
  273. movw %ax,U_SYSOS2_INOUTRES;
  274. xorl %eax,%eax
  275. LDOSFILEPOS:
  276. leave
  277. ret $4
  278. end;
  279. end;
  280. procedure do_seek(handle,pos:longint);
  281. begin
  282. asm
  283. movl $0x4200,%eax
  284. movl 8(%ebp),%ebx
  285. movl 12(%ebp),%edx
  286. movl %edx,%ecx
  287. shrl $16,%ecx
  288. call ___syscall
  289. jnc .LDOSSEEK1
  290. movw %ax,U_SYSOS2_INOUTRES;
  291. .LDOSSEEK1:
  292. leave
  293. ret $8
  294. end;
  295. end;
  296. function do_seekend(handle:longint):longint;
  297. begin
  298. asm
  299. movl $0x4202,%eax
  300. movl 8(%ebp),%ebx
  301. xorl %ecx,%ecx
  302. xorl %edx,%edx
  303. call ___syscall
  304. jnc .Lset_at_end1
  305. movw %ax,U_SYSOS2_INOUTRES;
  306. xorl %eax,%eax
  307. jmp .Lset_at_end2
  308. .Lset_at_end1:
  309. shll $16,%edx
  310. movzwl %ax,%eax
  311. orl %edx,%eax
  312. .Lset_at_end2:
  313. leave
  314. ret $4
  315. end;
  316. end;
  317. function do_filesize(handle:longint):longint;
  318. var aktfilepos:longint;
  319. begin
  320. aktfilepos:=do_filepos(handle);
  321. do_filesize:=do_seekend(handle);
  322. do_seek(handle,aktfilepos);
  323. end;
  324. procedure do_truncate(handle,pos:longint);
  325. begin
  326. asm
  327. movl $0x4200,%eax
  328. movl 8(%ebp),%ebx
  329. movl 12(%ebp),%edx
  330. movl %edx,%ecx
  331. shrl $16,%ecx
  332. call ___syscall
  333. jc .LTruncate1
  334. movl 8(%ebp),%ebx
  335. movl 12(%ebp),%edx
  336. movl %ebp,%edx
  337. xorl %ecx,%ecx
  338. movb $0x40,%ah
  339. call ___syscall
  340. jnc .LTruncate2
  341. .LTruncate1:
  342. movw %ax,U_SYSOS2_INOUTRES;
  343. .LTruncate2:
  344. leave
  345. ret $8
  346. end;
  347. end;
  348. procedure do_open(var f;p:pchar;flags:longint);
  349. {
  350. filerec and textrec have both handle and mode as the first items so
  351. they could use the same routine for opening/creating.
  352. when (flags and $10) the file will be append
  353. when (flags and $100) the file will be truncate/rewritten
  354. when (flags and $1000) there is no check for close (needed for textfiles)
  355. }
  356. var oflags:longint;
  357. begin
  358. allowslash(p);
  359. { close first if opened }
  360. if ((flags and $1000)=0) then
  361. begin
  362. case filerec(f).mode of
  363. fminput,fmoutput,fminout : Do_Close(filerec(f).handle);
  364. fmclosed:;
  365. else
  366. begin
  367. inoutres:=102; {not assigned}
  368. exit;
  369. end;
  370. end;
  371. end;
  372. { reset file handle }
  373. filerec(f).handle:=high(word);
  374. oflags:=$8404;
  375. { convert filemode to filerec modes }
  376. case (flags and 3) of
  377. 0 : begin
  378. filerec(f).mode:=fminput;
  379. oflags:=$8001;
  380. end;
  381. 1 : filerec(f).mode:=fmoutput;
  382. 2 : filerec(f).mode:=fminout;
  383. end;
  384. if (flags and $100)<>0 then
  385. begin
  386. filerec(f).mode:=fmoutput;
  387. oflags:=$8302;
  388. end
  389. else
  390. if (flags and $10)<>0 then
  391. begin
  392. filerec(f).mode:=fmoutput;
  393. oflags:=$8404;
  394. end;
  395. { empty name is special }
  396. if p[0]=#0 then
  397. begin
  398. case filerec(f).mode of
  399. fminput:filerec(f).handle:=StdInputHandle;
  400. fmappend,fmoutput : begin
  401. filerec(f).handle:=StdOutputHandle;
  402. filerec(f).mode:=fmoutput; {fool fmappend}
  403. end;
  404. end;
  405. exit;
  406. end;
  407. asm
  408. movl $0xff02,%ax
  409. movl -4(%ebp),%ecx
  410. movl 12(%ebp),%ebx
  411. call ___syscall
  412. jnc .LOPEN1
  413. movw %ax,U_SYSOS2_INOUTRES;
  414. movw $0xffff,%ax
  415. .LOPEN1:
  416. movl 8(%ebp),%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,U_SYSOS2_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. test $512,%bx {Bit 9 is OS/2 flag.}
  537. setnzb U_SYSOS2_OS_MODE
  538. test $4096,%bx
  539. jz _noRSX
  540. movb $2,U_SYSOS2_OS_MODE
  541. _noRSX:
  542. end;
  543. {Enable the brk area by initializing it with the initial heap size.}
  544. asm
  545. mov $0x7f01,%ax
  546. movl HEAPSIZE,%edx
  547. call ___syscall
  548. end;
  549. {Now request, if we are running under DOS,
  550. read-access to the first meg. of memory.}
  551. if os_mode in [osDOS,osDPMI] then
  552. asm
  553. mov $0x7f13,%ax
  554. xor %ebx,%ebx
  555. mov $0xfff,%ecx
  556. xor %edx,%edx
  557. call ___syscall
  558. mov %eax,U_SYSOS2_FIRST_MEG
  559. end
  560. else
  561. first_meg:=nil;
  562. {At 0.9.2, case for enumeration does not work.}
  563. case os_mode of
  564. osDOS:
  565. stackbottom:=0; {In DOS mode, heap_brk is also the
  566. stack bottom.}
  567. osOS2:
  568. begin
  569. dosgetinfoblocks(tib,pib);
  570. stackbottom:=longint(tib^.stack);
  571. end;
  572. osDPMI:
  573. stackbottom:=0; {Not sure how to get it, but seems to be
  574. always zero.}
  575. end;
  576. exitproc:=nil;
  577. {Initialize the heap.}
  578. initheap;
  579. { to test stack depth }
  580. loweststack:=maxlongint;
  581. OpenStdIO(Input,fmInput,StdInputHandle);
  582. OpenStdIO(Output,fmOutput,StdOutputHandle);
  583. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  584. { kein Ein- Ausgabefehler }
  585. inoutres:=0;
  586. end.