2
0

sysos2.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  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. HandleError(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. function getheapsize:longint;assembler;
  184. asm
  185. movl HEAPSIZE,%eax
  186. end ['EAX'];
  187. {$ASMMODE ATT}
  188. {$i heap.inc}
  189. {****************************************************************************
  190. Low Level File Routines
  191. ****************************************************************************}
  192. procedure allowslash(p:Pchar);
  193. {Allow slash as backslash.}
  194. var i:longint;
  195. begin
  196. for i:=0 to strlen(p) do
  197. if p[i]='/' then p[i]:='\';
  198. end;
  199. procedure do_close(h:longint);
  200. begin
  201. asm
  202. movb $0x3e,%ah
  203. mov h,%ebx
  204. call syscall
  205. end;
  206. end;
  207. procedure do_erase(p:Pchar);
  208. begin
  209. allowslash(p);
  210. asm
  211. movl 8(%ebp),%edx
  212. movb $0x41,%ah
  213. call syscall
  214. jnc .LERASE1
  215. movw %ax,inoutres;
  216. .LERASE1:
  217. end;
  218. end;
  219. procedure do_rename(p1,p2:Pchar);
  220. begin
  221. allowslash(p1);
  222. allowslash(p2);
  223. asm
  224. movl 8(%ebp),%edx
  225. movl 12(%ebp),%edi
  226. movb $0x56,%ah
  227. call syscall
  228. jnc .LRENAME1
  229. movw %ax,inoutres;
  230. .LRENAME1:
  231. end;
  232. end;
  233. function do_read(h,addr,len:longint):longint;
  234. begin
  235. asm
  236. movl 16(%ebp),%ecx
  237. movl 12(%ebp),%edx
  238. movl 8(%ebp),%ebx
  239. movb $0x3f,%ah
  240. call syscall
  241. jnc .LDOSREAD1
  242. movw %ax,inoutres;
  243. xorl %eax,%eax
  244. .LDOSREAD1:
  245. leave
  246. ret $12
  247. end;
  248. end;
  249. function do_write(h,addr,len:longint) : longint;
  250. begin
  251. asm
  252. movl 16(%ebp),%ecx
  253. movl 12(%ebp),%edx
  254. movl 8(%ebp),%ebx
  255. movb $0x40,%ah
  256. call syscall
  257. jnc .LDOSWRITE1
  258. movw %ax,inoutres;
  259. .LDOSWRITE1:
  260. movl %eax,-4(%ebp)
  261. end;
  262. end;
  263. function do_filepos(handle:longint):longint;
  264. begin
  265. asm
  266. movw $0x4201,%ax
  267. movl 8(%ebp),%ebx
  268. xorl %edx,%edx
  269. call syscall
  270. jnc .LDOSFILEPOS
  271. movw %ax,inoutres;
  272. xorl %eax,%eax
  273. .LDOSFILEPOS:
  274. leave
  275. ret $4
  276. end;
  277. end;
  278. procedure do_seek(handle,pos:longint);
  279. begin
  280. asm
  281. movw $0x4200,%ax
  282. movl 8(%ebp),%ebx
  283. movl 12(%ebp),%edx
  284. call syscall
  285. jnc .LDOSSEEK1
  286. movw %ax,inoutres;
  287. .LDOSSEEK1:
  288. leave
  289. ret $8
  290. end;
  291. end;
  292. function do_seekend(handle:longint):longint;
  293. begin
  294. asm
  295. movw $0x4202,%ax
  296. movl 8(%ebp),%ebx
  297. xorl %edx,%edx
  298. call syscall
  299. jnc .Lset_at_end1
  300. movw %ax,inoutres;
  301. xorl %eax,%eax
  302. .Lset_at_end1:
  303. leave
  304. ret $4
  305. end;
  306. end;
  307. function do_filesize(handle:longint):longint;
  308. var aktfilepos:longint;
  309. begin
  310. aktfilepos:=do_filepos(handle);
  311. do_filesize:=do_seekend(handle);
  312. do_seek(handle,aktfilepos);
  313. end;
  314. procedure do_truncate(handle,pos:longint);
  315. begin
  316. asm
  317. movl $0x4200,%eax
  318. movl 8(%ebp),%ebx
  319. movl 12(%ebp),%edx
  320. call syscall
  321. jc .LTruncate1
  322. movl 8(%ebp),%ebx
  323. movl 12(%ebp),%edx
  324. movl %ebp,%edx
  325. xorl %ecx,%ecx
  326. movb $0x40,%ah
  327. call syscall
  328. jnc .LTruncate2
  329. .LTruncate1:
  330. movw %ax,inoutres;
  331. .LTruncate2:
  332. leave
  333. ret $8
  334. end;
  335. end;
  336. procedure do_open(var f;p:pchar;flags:longint);
  337. {
  338. filerec and textrec have both handle and mode as the first items so
  339. they could use the same routine for opening/creating.
  340. when (flags and $10) the file will be append
  341. when (flags and $100) the file will be truncate/rewritten
  342. when (flags and $1000) there is no check for close (needed for textfiles)
  343. }
  344. var oflags:byte;
  345. begin
  346. allowslash(p);
  347. { close first if opened }
  348. if ((flags and $1000)=0) then
  349. begin
  350. case filerec(f).mode of
  351. fminput,fmoutput,fminout : Do_Close(filerec(f).handle);
  352. fmclosed:;
  353. else
  354. begin
  355. inoutres:=102; {not assigned}
  356. exit;
  357. end;
  358. end;
  359. end;
  360. { reset file handle }
  361. filerec(f).handle:=high(word);
  362. oflags:=2;
  363. { convert filemode to filerec modes }
  364. case (flags and 3) of
  365. 0 : begin
  366. filerec(f).mode:=fminput;
  367. oflags:=0;
  368. end;
  369. 1 : filerec(f).mode:=fmoutput;
  370. 2 : filerec(f).mode:=fminout;
  371. end;
  372. if (flags and $100)<>0 then
  373. begin
  374. filerec(f).mode:=fmoutput;
  375. oflags:=2;
  376. end
  377. else
  378. if (flags and $10)<>0 then
  379. begin
  380. filerec(f).mode:=fmoutput;
  381. oflags:=2;
  382. end;
  383. { empty name is special }
  384. if p[0]=#0 then
  385. begin
  386. case filerec(f).mode of
  387. fminput:filerec(f).handle:=StdInputHandle;
  388. fmappend,fmoutput : begin
  389. filerec(f).handle:=StdOutputHandle;
  390. filerec(f).mode:=fmoutput; {fool fmappend}
  391. end;
  392. end;
  393. exit;
  394. end;
  395. if (flags and $100)<>0 then
  396. {Use create function.}
  397. asm
  398. movb $0x3c,%ah
  399. movl p,%edx
  400. xorw %cx,%cx
  401. call syscall
  402. jnc .LOPEN1
  403. movw %ax,inoutres;
  404. movw $0xffff,%ax
  405. .LOPEN1:
  406. movl f,%edx
  407. movw %ax,(%edx)
  408. end
  409. else
  410. {Use open function.}
  411. asm
  412. movb $0x3d,%ah
  413. movb oflags,%al
  414. movl p,%edx
  415. call syscall
  416. jnc .LOPEN2
  417. movw %ax,inoutres;
  418. movw $0xffff,%ax
  419. .LOPEN2:
  420. movl f,%edx
  421. movw %ax,(%edx)
  422. end;
  423. if (flags and $10)<>0 then
  424. do_seekend(filerec(f).handle);
  425. end;
  426. function do_isdevice(handle:longint):boolean;
  427. begin
  428. do_isdevice:=(handle<=5);
  429. end;
  430. {*****************************************************************************
  431. UnTyped File Handling
  432. *****************************************************************************}
  433. {$i file.inc}
  434. {*****************************************************************************
  435. Typed File Handling
  436. *****************************************************************************}
  437. {$i typefile.inc}
  438. {*****************************************************************************
  439. Text File Handling
  440. *****************************************************************************}
  441. {$DEFINE EOF_CTRLZ}
  442. {$i text.inc}
  443. {****************************************************************************
  444. Directory related routines.
  445. ****************************************************************************}
  446. {*****************************************************************************
  447. Directory Handling
  448. *****************************************************************************}
  449. procedure dosdir(func:byte;const s:string);
  450. var buffer:array[0..255] of char;
  451. begin
  452. move(s[1],buffer,length(s));
  453. buffer[length(s)]:=#0;
  454. allowslash(Pchar(@buffer));
  455. asm
  456. leal buffer,%edx
  457. movb 8(%ebp),%ah
  458. call syscall
  459. jnc .LDOS_DIRS1
  460. movw %ax,inoutres;
  461. .LDOS_DIRS1:
  462. end;
  463. end;
  464. procedure mkdir(const s : string);
  465. begin
  466. DosDir($39,s);
  467. end;
  468. procedure rmdir(const s : string);
  469. begin
  470. DosDir($3a,s);
  471. end;
  472. procedure chdir(const s : string);
  473. begin
  474. DosDir($3b,s);
  475. end;
  476. procedure getdir(drivenr : byte;var dir : shortstring);
  477. {Written by Michael Van Canneyt.}
  478. var temp:array[0..255] of char;
  479. sof:Pchar;
  480. i:byte;
  481. begin
  482. sof:=pchar(@dir[4]);
  483. { dir[1..3] will contain '[drivenr]:\', but is not }
  484. { supplied by DOS, so we let dos string start at }
  485. { dir[4] }
  486. { Get dir from drivenr : 0=default, 1=A etc... }
  487. asm
  488. movb drivenr,%dl
  489. movl sof,%esi
  490. mov $0x47,%ah
  491. call syscall
  492. end;
  493. { Now Dir should be filled with directory in ASCIIZ, }
  494. { starting from dir[4] }
  495. dir[0]:=#3;
  496. dir[2]:=':';
  497. dir[3]:='\';
  498. i:=4;
  499. {Conversion to Pascal string }
  500. while (dir[i]<>#0) do
  501. begin
  502. { convert path name to DOS }
  503. if dir[i]='/' then
  504. dir[i]:='\';
  505. dir[0]:=char(i);
  506. inc(i);
  507. end;
  508. { upcase the string (FPKPascal function) }
  509. dir:=upcase(dir);
  510. if drivenr<>0 then { Drive was supplied. We know it }
  511. dir[1]:=char(65+drivenr-1)
  512. else
  513. begin
  514. { We need to get the current drive from DOS function 19H }
  515. { because the drive was the default, which can be unknown }
  516. asm
  517. movb $0x19,%ah
  518. call syscall
  519. addb $65,%al
  520. movb %al,i
  521. end;
  522. dir[1]:=char(i);
  523. end;
  524. end;
  525. {****************************************************************************
  526. System unit initialization.
  527. ****************************************************************************}
  528. var pib:Pprocessinfoblock;
  529. tib:Pthreadinfoblock;
  530. begin
  531. {Determine the operating system we are running on.}
  532. asm
  533. movw $0x7f0a,%ax
  534. call syscall
  535. testw $512,%bx {Bit 9 is OS/2 flag.}
  536. setnzl os_mode
  537. testw $4096,%bx
  538. jz .LnoRSX
  539. movl $2,os_mode
  540. .LnoRSX:
  541. end;
  542. {$ASMMODE DIRECT}
  543. {Enable the brk area by initializing it with the initial heap size.}
  544. asm
  545. movw $0x7f01,%ax
  546. movl HEAPSIZE,%edx
  547. addl __heap_base,%edx
  548. call ___SYSCALL
  549. cmpl $-1,%eax
  550. jnz Lheapok
  551. pushl $204
  552. {call RUNERROR$$WORD}
  553. Lheapok:
  554. end;
  555. {$ASMMODE ATT}
  556. {Now request, if we are running under DOS,
  557. read-access to the first meg. of memory.}
  558. if os_mode in [osDOS,osDPMI] then
  559. asm
  560. movw $0x7f13,%ax
  561. xorl %ebx,%ebx
  562. movl $0xfff,%ecx
  563. xorl %edx,%edx
  564. call syscall
  565. movl %eax,first_meg
  566. end
  567. else
  568. first_meg:=nil;
  569. {At 0.9.2, case for enumeration does not work.}
  570. case os_mode of
  571. osDOS:
  572. stackbottom:=0; {In DOS mode, heap_brk is also the
  573. stack bottom.}
  574. osOS2:
  575. begin
  576. dosgetinfoblocks(tib,pib);
  577. stackbottom:=longint(tib^.stack);
  578. end;
  579. osDPMI:
  580. stackbottom:=0; {Not sure how to get it, but seems to be
  581. always zero.}
  582. end;
  583. exitproc:=nil;
  584. {Initialize the heap.}
  585. initheap;
  586. { to test stack depth }
  587. loweststack:=maxlongint;
  588. OpenStdIO(Input,fmInput,StdInputHandle);
  589. OpenStdIO(Output,fmOutput,StdOutputHandle);
  590. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  591. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  592. { kein Ein- Ausgabefehler }
  593. inoutres:=0;
  594. end.