sysamiga.pas 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1993,98 by Carl Eric Codere
  5. Some parts taken from
  6. Marcel Timmermans - Modula 2 Compiler
  7. Nils Sjoholm - Amiga porter
  8. Matthew Dillon - Dice C (with his kind permission)
  9. [email protected]
  10. See the file COPYING.FPC, included in this distribution,
  11. for details about the copyright.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. **********************************************************************}
  16. unit sysamiga;
  17. {--------------------------------------------------------------------}
  18. { LEFT TO DO: }
  19. {--------------------------------------------------------------------}
  20. { o GetDir with different drive numbers }
  21. {--------------------------------------------------------------------}
  22. {$I os.inc}
  23. { AmigaOS uses character #10 as eoln only }
  24. {$DEFINE SHORT_LINEBREAK}
  25. interface
  26. { used for single computations }
  27. const BIAS4 = $7f-1;
  28. {$I systemh.inc}
  29. {$I heaph.inc}
  30. const
  31. UnusedHandle : longint = -1;
  32. StdInputHandle : longint = 0;
  33. StdOutputHandle : longint = 0;
  34. StdErrorHandle : longint = 0;
  35. _ExecBase:longint = $4;
  36. _WorkbenchMsg : longint = 0;
  37. _IntuitionBase : pointer = nil; { intuition library pointer }
  38. _DosBase : pointer = nil; { DOS library pointer }
  39. _UtilityBase : pointer = nil; { utiity library pointer }
  40. { Required for crt unit }
  41. function do_read(h,addr,len : longint) : longint;
  42. function do_write(h,addr,len : longint) : longint;
  43. implementation
  44. const
  45. intuitionname : pchar = 'intuition.library';
  46. dosname : pchar = 'dos.library';
  47. utilityname : pchar = 'utility.library';
  48. argc : longint = 0;
  49. { AmigaOS does not autoamtically deallocate memory on program termination }
  50. { therefore we have to handle this manually. This is a list of allocated }
  51. { pointers from the OS, we cannot use a linked list, because the linked }
  52. { list itself uses the HEAP! }
  53. pointerlist : array[1..8] of longint =
  54. (0,0,0,0,0,0,0,0);
  55. {$I exec.inc}
  56. TYPE
  57. TDateStamp = packed record
  58. ds_Days : Longint; { Number of days since Jan. 1, 1978 }
  59. ds_Minute : Longint; { Number of minutes past midnight }
  60. ds_Tick : Longint; { Number of ticks past minute }
  61. end;
  62. PDateStamp = ^TDateStamp;
  63. PFileInfoBlock = ^TfileInfoBlock;
  64. TFileInfoBlock = packed record
  65. fib_DiskKey : Longint;
  66. fib_DirEntryType : Longint;
  67. { Type of Directory. If < 0, then a plain file.
  68. If > 0 a directory }
  69. fib_FileName : Array [0..107] of Char;
  70. { Null terminated. Max 30 chars used for now }
  71. fib_Protection : Longint;
  72. { bit mask of protection, rwxd are 3-0. }
  73. fib_EntryType : Longint;
  74. fib_Size : Longint; { Number of bytes in file }
  75. fib_NumBlocks : Longint; { Number of blocks in file }
  76. fib_Date : TDateStamp; { Date file last changed }
  77. fib_Comment : Array [0..79] of Char;
  78. { Null terminated comment associated with file }
  79. fib_Reserved : Array [0..35] of Char;
  80. end;
  81. TProcess = packed record
  82. pr_Task : TTask;
  83. pr_MsgPort : TMsgPort; { This is BPTR address from DOS functions }
  84. {126} pr_Pad : Word; { Remaining variables on 4 byte boundaries }
  85. {128} pr_SegList : Pointer; { Array of seg lists used by this process }
  86. {132} pr_StackSize : Longint; { Size of process stack in bytes }
  87. {136} pr_GlobVec : Pointer; { Global vector for this process (BCPL) }
  88. {140} pr_TaskNum : Longint; { CLI task number of zero if not a CLI }
  89. {144} pr_StackBase : BPTR; { Ptr to high memory end of process stack }
  90. {148} pr_Result2 : Longint; { Value of secondary result from last call }
  91. {152} pr_CurrentDir : BPTR; { Lock associated with current directory }
  92. {156} pr_CIS : BPTR; { Current CLI Input Stream }
  93. {160} pr_COS : BPTR; { Current CLI Output Stream }
  94. {164} pr_ConsoleTask : Pointer; { Console handler process for current window}
  95. {168} pr_FileSystemTask : Pointer; { File handler process for current drive }
  96. {172} pr_CLI : BPTR; { pointer to ConsoleLineInterpreter }
  97. pr_ReturnAddr : Pointer; { pointer to previous stack frame }
  98. pr_PktWait : Pointer; { Function to be called when awaiting msg }
  99. pr_WindowPtr : Pointer; { Window for error printing }
  100. { following definitions are new with 2.0 }
  101. pr_HomeDir : BPTR; { Home directory of executing program }
  102. pr_Flags : Longint; { flags telling dos about process }
  103. pr_ExitCode : Pointer; { code to call on exit of program OR NULL }
  104. pr_ExitData : Longint; { Passed as an argument to pr_ExitCode. }
  105. pr_Arguments : PChar; { Arguments passed to the process at start }
  106. pr_LocalVars : TMinList; { Local environment variables }
  107. pr_ShellPrivate : Longint; { for the use of the current shell }
  108. pr_CES : BPTR; { Error stream - IF NULL, use pr_COS }
  109. end;
  110. PProcess = ^TProcess;
  111. { AmigaOS does not automatically close opened files on exit back to }
  112. { the operating system, therefore as a precuation we close all files }
  113. { manually on exit. }
  114. PFileList = ^TFileList;
  115. TFileList = record { no packed, must be correctly aligned }
  116. Handle: longint; { Handle to file }
  117. next: pfilelist; { Next file in list }
  118. closed: boolean; { TRUE=file already closed }
  119. end;
  120. Const
  121. CTRL_C = 20; { Error code on CTRL-C press }
  122. SIGBREAKF_CTRL_C = $1000; { CTRL-C signal flags }
  123. _LVOFindTask = -294;
  124. _LVOWaitPort = -384;
  125. _LVOGetMsg = -372;
  126. _LVOOpenLibrary = -552;
  127. _LVOCloseLibrary = -414;
  128. _LVOClose = -36;
  129. _LVOOpen = -30;
  130. _LVOIoErr = -132;
  131. _LVOSeek = -66;
  132. _LVODeleteFile = -72;
  133. _LVORename = -78;
  134. _LVOWrite = -48;
  135. _LVORead = -42;
  136. _LVOCreateDir = -120;
  137. _LVOSetCurrentDirName = -558;
  138. _LVOGetCurrentDirName = -564;
  139. _LVOInput = -54;
  140. _LVOOutput = -60;
  141. _LVOUnLock = -90;
  142. _LVOLock = -84;
  143. _LVOCurrentDir = -126;
  144. _LVONameFromLock = -402;
  145. _LVONameFromFH = -408;
  146. _LVOGetProgramName = -576;
  147. _LVOGetProgramDir = -600;
  148. _LVODupLock = -96;
  149. _LVOExamine = -102;
  150. _LVOParentDir = -210;
  151. _LVOSetFileSize = -456;
  152. _LVOSetSignal = -306;
  153. _LVOAllocVec = -684;
  154. _LVOFreeVec = -690;
  155. { Errors from IoErr(), etc. }
  156. ERROR_NO_FREE_STORE = 103;
  157. ERROR_TASK_TABLE_FULL = 105;
  158. ERROR_BAD_TEMPLATE = 114;
  159. ERROR_BAD_NUMBER = 115;
  160. ERROR_REQUIRED_ARG_MISSING = 116;
  161. ERROR_KEY_NEEDS_ARG = 117;
  162. ERROR_TOO_MANY_ARGS = 118;
  163. ERROR_UNMATCHED_QUOTES = 119;
  164. ERROR_LINE_TOO_LONG = 120;
  165. ERROR_FILE_NOT_OBJECT = 121;
  166. ERROR_INVALID_RESIDENT_LIBRARY = 122;
  167. ERROR_NO_DEFAULT_DIR = 201;
  168. ERROR_OBJECT_IN_USE = 202;
  169. ERROR_OBJECT_EXISTS = 203;
  170. ERROR_DIR_NOT_FOUND = 204;
  171. ERROR_OBJECT_NOT_FOUND = 205;
  172. ERROR_BAD_STREAM_NAME = 206;
  173. ERROR_OBJECT_TOO_LARGE = 207;
  174. ERROR_ACTION_NOT_KNOWN = 209;
  175. ERROR_INVALID_COMPONENT_NAME = 210;
  176. ERROR_INVALID_LOCK = 211;
  177. ERROR_OBJECT_WRONG_TYPE = 212;
  178. ERROR_DISK_NOT_VALIDATED = 213;
  179. ERROR_DISK_WRITE_PROTECTED = 214;
  180. ERROR_RENAME_ACROSS_DEVICES = 215;
  181. ERROR_DIRECTORY_NOT_EMPTY = 216;
  182. ERROR_TOO_MANY_LEVELS = 217;
  183. ERROR_DEVICE_NOT_MOUNTED = 218;
  184. ERROR_SEEK_ERROR = 219;
  185. ERROR_COMMENT_TOO_BIG = 220;
  186. ERROR_DISK_FULL = 221;
  187. ERROR_DELETE_PROTECTED = 222;
  188. ERROR_WRITE_PROTECTED = 223;
  189. ERROR_READ_PROTECTED = 224;
  190. ERROR_NOT_A_DOS_DISK = 225;
  191. ERROR_NO_DISK = 226;
  192. ERROR_NO_MORE_ENTRIES = 232;
  193. { added for 1.4 }
  194. ERROR_IS_SOFT_LINK = 233;
  195. ERROR_OBJECT_LINKED = 234;
  196. ERROR_BAD_HUNK = 235;
  197. ERROR_NOT_IMPLEMENTED = 236;
  198. ERROR_RECORD_NOT_LOCKED = 240;
  199. ERROR_LOCK_COLLISION = 241;
  200. ERROR_LOCK_TIMEOUT = 242;
  201. ERROR_UNLOCK_ERROR = 243;
  202. var
  203. Initial: boolean; { Have successfully opened Std I/O }
  204. errno : word; { AmigaOS IO Error number }
  205. FileList : pFileList; { Linked list of opened files }
  206. old_exit: Pointer;
  207. FromHalt : boolean;
  208. OrigDir : Longint; { Current lock on original startup directory }
  209. {$I system.inc}
  210. {$I lowmath.inc}
  211. { ************************ AMIGAOS STUB ROUTINES ************************* }
  212. procedure DateStamp(var ds : tDateStamp);
  213. begin
  214. asm
  215. MOVE.L A6,-(A7)
  216. MOVE.L ds,d1
  217. { LAST THING TO SETUP SHOULD BE A6, otherwise you can }
  218. { not accept local variable, nor any parameters! :) }
  219. MOVE.L _DOSBase,A6
  220. JSR -192(A6)
  221. MOVE.L (A7)+,A6
  222. end;
  223. end;
  224. { UNLOCK the BPTR pointed to in L }
  225. Procedure Unlock(alock: longint);
  226. Begin
  227. asm
  228. move.l alock,d1
  229. move.l a6,d6 { save base pointer }
  230. move.l _DosBase,a6
  231. jsr _LVOUnlock(a6)
  232. move.l d6,a6 { restore base pointer }
  233. end;
  234. end;
  235. { Change to the directory pointed to in the lock }
  236. Function CurrentDir(alock : longint) : longint;
  237. Begin
  238. asm
  239. move.l alock,d1
  240. move.l a6,d6 { save base pointer }
  241. move.l _DosBase,a6
  242. jsr _LVOCurrentDir(a6)
  243. move.l d6,a6 { restore base pointer }
  244. move.l d0,@Result
  245. end;
  246. end;
  247. { Duplicate a lock }
  248. Function DupLock(alock: longint): Longint;
  249. Begin
  250. asm
  251. move.l alock,d1
  252. move.l a6,d6 { save base pointer }
  253. move.l _DosBase,a6
  254. jsr _LVODupLock(a6)
  255. move.l d6,a6 { restore base pointer }
  256. move.l d0,@Result
  257. end;
  258. end;
  259. { Returns a lock on the directory was loaded from }
  260. Function GetProgramLock: longint;
  261. Begin
  262. asm
  263. move.l a6,d6 { save base pointer }
  264. move.l _DosBase,a6
  265. jsr _LVOGetProgramDir(a6)
  266. move.l d6,a6 { restore base pointer }
  267. move.l d0,@Result
  268. end;
  269. end;
  270. Function Examine(alock :longint; var fib: TFileInfoBlock) : Boolean;
  271. Begin
  272. asm
  273. move.l d2,-(sp)
  274. move.l fib,d2 { pointer to FIB }
  275. move.l alock,d1
  276. move.l a6,d6 { save base pointer }
  277. move.l _DosBase,a6
  278. jsr _LVOExamine(a6)
  279. move.l d6,a6 { restore base pointer }
  280. tst.l d0
  281. bne @success
  282. bra @end
  283. @success:
  284. move.b #1,d0
  285. @end:
  286. move.b d0,@Result
  287. move.l (sp)+,d2
  288. end;
  289. end;
  290. { Returns the parent directory of a lock }
  291. Function ParentDir(alock : longint): longint;
  292. Begin
  293. asm
  294. move.l alock,d1
  295. move.l a6,d6 { save base pointer }
  296. move.l _DosBase,a6
  297. jsr _LVOParentDir(a6)
  298. move.l d6,a6 { restore base pointer }
  299. move.l d0,@Result
  300. end;
  301. end;
  302. Function FindTask(p : PChar): PProcess;
  303. Begin
  304. asm
  305. move.l a6,d6 { Save base pointer }
  306. move.l p,d0
  307. move.l d0,a1
  308. move.l _ExecBase,a6
  309. jsr _LVOFindTask(a6)
  310. move.l d6,a6 { Restore base pointer }
  311. move.l d0,@Result
  312. end;
  313. end;
  314. {$S-}
  315. Procedure stack_check; assembler;
  316. { Check for local variable allocation }
  317. { On Entry -> d0 : size of local stack we are trying to allocate }
  318. asm
  319. XDEF STACKCHECK
  320. move.l sp,d1 { get value of stack pointer }
  321. { We must add some security, because Writing the RunError strings }
  322. { requires a LOT of stack space (at least 1030 bytes!) }
  323. add.l #2048,d0
  324. sub.l d0,d1 { sp - stack_size }
  325. move.l _ExecBase,a0
  326. move.l 276(A0),A0 { ExecBase.thisTask }
  327. { if allocated stack_pointer - splower <= 0 then stack_ovf }
  328. cmp.l 58(A0),D1 { Task.SpLower }
  329. bgt @Ok
  330. move.l #202,d0
  331. jsr HALT_ERROR { stack overflow }
  332. @Ok:
  333. end;
  334. { This routine from EXEC determines if the Ctrl-C key has }
  335. { been used since the last call to I/O routines. }
  336. { Use to halt the program. }
  337. { Returns the state of the old signals. }
  338. Function SetSignal(newSignal: longint; SignalMask: longint): longint;
  339. Begin
  340. asm
  341. move.l newSignal,d0
  342. move.l SignalMask,d1
  343. move.l a6,d6 { save Base pointer into scratch register }
  344. move.l _ExecBase,a6
  345. jsr _LVOSetSignal(a6)
  346. move.l d6,a6
  347. move.l d0,@Result
  348. end;
  349. end;
  350. Function AllocVec(bytesize: longint; attributes: longint):longint;
  351. Begin
  352. asm
  353. move.l bytesize,d0
  354. move.l attributes,d1
  355. move.l a6,d6 { save Base pointer into scratch register }
  356. move.l _ExecBase,a6
  357. jsr _LVOAllocVec(a6)
  358. move.l d6,a6
  359. move.l d0,@Result
  360. end;
  361. end;
  362. Procedure FreeVec(p: longint);
  363. Begin
  364. asm
  365. move.l p,a1
  366. move.l a6,d6 { save Base pointer into scratch register }
  367. move.l _ExecBase,a6
  368. jsr _LVOFreeVec(a6)
  369. move.l d6,a6
  370. end;
  371. end;
  372. { Converts an AMIGAOS error code to a TP compatible error code }
  373. Procedure Error2InOut;
  374. Begin
  375. case errno of
  376. ERROR_BAD_NUMBER,
  377. ERROR_ACTION_NOT_KNOWN,
  378. ERROR_NOT_IMPLEMENTED : InOutRes := 1;
  379. ERROR_OBJECT_NOT_FOUND : InOutRes := 2;
  380. ERROR_DIR_NOT_FOUND : InOutRes := 3;
  381. ERROR_DISK_WRITE_PROTECTED : InOutRes := 150;
  382. ERROR_OBJECT_WRONG_TYPE : InOutRes := 151;
  383. ERROR_OBJECT_EXISTS,
  384. ERROR_DELETE_PROTECTED,
  385. ERROR_WRITE_PROTECTED,
  386. ERROR_READ_PROTECTED,
  387. ERROR_OBJECT_IN_USE,
  388. ERROR_DIRECTORY_NOT_EMPTY : InOutRes := 5;
  389. ERROR_NO_MORE_ENTRIES : InOutRes := 18;
  390. ERROR_RENAME_ACROSS_DEVICES : InOutRes := 17;
  391. ERROR_DISK_FULL : InOutRes := 101;
  392. ERROR_INVALID_RESIDENT_LIBRARY : InoutRes := 153;
  393. ERROR_BAD_HUNK : InOutRes := 153;
  394. ERROR_NOT_A_DOS_DISK : InOutRes := 157;
  395. ERROR_NO_DISK,
  396. ERROR_DISK_NOT_VALIDATED,
  397. ERROR_DEVICE_NOT_MOUNTED : InOutRes := 152;
  398. ERROR_SEEK_ERROR : InOutRes := 156;
  399. ERROR_LOCK_COLLISION,
  400. ERROR_LOCK_TIMEOUT,
  401. ERROR_UNLOCK_ERROR,
  402. ERROR_INVALID_LOCK,
  403. ERROR_INVALID_COMPONENT_NAME,
  404. ERROR_BAD_STREAM_NAME,
  405. ERROR_FILE_NOT_OBJECT : InOutRes := 6;
  406. else
  407. InOutres := errno;
  408. end;
  409. errno:=0;
  410. end;
  411. procedure CloseLibrary(lib : pointer);
  412. { Close the library pointed to in lib }
  413. Begin
  414. asm
  415. MOVE.L A6,-(A7)
  416. MOVE.L lib,a1
  417. MOVE.L _ExecBase,A6
  418. JSR _LVOCloseLibrary(A6)
  419. MOVE.L (A7)+,A6
  420. end;
  421. end;
  422. Function KickVersion: word; assembler;
  423. asm
  424. move.l _ExecBase, a0 { Get Exec Base }
  425. move.w 20(a0), d0 { Return version - version at this offset }
  426. end;
  427. { ************************ AMIGAOS SUPP ROUTINES ************************* }
  428. (* Procedure CloseList(p: pFileList);*)
  429. (***********************************************************************)
  430. (* PROCEDURE CloseList *)
  431. (* Description: This routine each time the program is about to *)
  432. (* terminate, it closes all opened file handles, as this is not *)
  433. (* handled by the operating system. *)
  434. (* p -> Start of linked list of opened files *)
  435. (***********************************************************************)
  436. (* var
  437. hp: pFileList;
  438. hp1: pFileList;
  439. h: longint;
  440. Begin
  441. hp:=p;
  442. while Assigned(hp) do
  443. Begin
  444. if NOT hp^.closed then
  445. Begin
  446. h:=hp^.handle;
  447. if (h <> StdInputHandle) and (h <> StdOutputHandle) and (h <> StdErrorHandle) then
  448. Begin
  449. { directly close file here, it is faster then doing }
  450. { it do_close. }
  451. asm
  452. move.l h,d1
  453. move.l a6,d6 { save a6 }
  454. move.l _DOSBase,a6
  455. jsr _LVOClose(a6)
  456. move.l d6,a6 { restore a6 }
  457. end;
  458. end;
  459. end;
  460. hp1:=hp;
  461. hp:=hp^.next;
  462. dispose(hp1);
  463. end;
  464. end;*)
  465. (* Procedure AddToList(var p: pFileList; h: longint);*)
  466. (***********************************************************************)
  467. (* PROCEDURE AddToList *)
  468. (* Description: Adds a node to the linked list of files. *)
  469. (* *)
  470. (* p -> Start of File list linked list, if not allocated allocates *)
  471. (* it for you. *)
  472. (* h -> handle of file to add *)
  473. (***********************************************************************)
  474. (* var
  475. hp: pFileList;
  476. hp1: pFileList;
  477. Begin
  478. if p = nil then
  479. Begin
  480. new(p);
  481. p^.handle:=h;
  482. p^.closed := FALSE;
  483. p^.next := nil;
  484. exit;
  485. end;
  486. hp:=p;
  487. { Find last list in entry }
  488. while assigned(hp) do
  489. Begin
  490. if hp^.next = nil then break;
  491. hp:=hp^.next;
  492. end;
  493. { Found last list in entry then add it to the list }
  494. new(hp1);
  495. hp^.next:=hp1;
  496. hp1^.next:=nil;
  497. hp1^.handle:=h;
  498. hp1^.closed:=FALSE;
  499. end;
  500. Procedure SetClosedList(var p: pFileList; h: longint);
  501. { Set the file flag to closed if the file is being closed }
  502. var
  503. hp: pFileList;
  504. Begin
  505. hp:=p;
  506. while assigned(hp) do
  507. Begin
  508. if hp^.handle = h then
  509. Begin
  510. hp^.closed:=TRUE;
  511. break;
  512. end;
  513. hp:=hp^.next;
  514. end;
  515. end;*)
  516. Procedure ExitCall;
  517. var
  518. i: byte;
  519. Begin
  520. { We must remove the CTRL-C FALG here because halt }
  521. { may call I/O routines, which in turn might call }
  522. { halt, so a recursive stack crash }
  523. IF (SetSignal(0,0) AND SIGBREAKF_CTRL_C) <> 0 THEN
  524. SetSignal(0,SIGBREAKF_CTRL_C);
  525. { Close remaining opened files }
  526. { CloseList(FileList); }
  527. if (OrigDir <> 0) then
  528. Begin
  529. Unlock(CurrentDir(OrigDir));
  530. OrigDir := 0;
  531. end;
  532. { Is this a normal exit - YES, close libs }
  533. IF NOT FromHalt then
  534. Begin
  535. { close the libraries }
  536. If _UtilityBase <> nil then
  537. CloseLibrary(_UtilityBase);
  538. If _DosBase <> nil then
  539. CloseLibrary(_DosBase);
  540. If _IntuitionBase <> nil then
  541. CloseLibrary(_IntuitionBase);
  542. _UtilityBase := nil;
  543. _DosBase := nil;
  544. _IntuitionBase := nil;
  545. end;
  546. { Dispose of extraneous allocated pointers }
  547. for I:=1 to 8 do
  548. Begin
  549. if pointerlist[i] <> 0 then FreeVec(pointerlist[i]);
  550. end;
  551. exitproc:=old_exit;
  552. end;
  553. procedure halt(errnum : byte);
  554. begin
  555. { Indicate to the SYSTEM EXIT procedure that we are calling it }
  556. { from halt, and that its library will be closed HERE and not }
  557. { in the exit procedure. }
  558. FromHalt:=TRUE;
  559. { We must remove the CTRL-C FALG here because halt }
  560. { may call I/O routines, which in turn might call }
  561. { halt, so a recursive stack crash }
  562. IF (SetSignal(0,0) AND SIGBREAKF_CTRL_C) <> 0 THEN
  563. SetSignal(0,SIGBREAKF_CTRL_C);
  564. { WE can only FLUSH the stdio }
  565. { if the handles have correctly }
  566. { been set. }
  567. { No exit procedures exist }
  568. { if in initial state }
  569. If NOT Initial then
  570. Begin
  571. do_exit;
  572. flush(stderr);
  573. end;
  574. { close the libraries }
  575. If _UtilityBase <> nil then
  576. CloseLibrary(_UtilityBase);
  577. If _DosBase <> nil then
  578. CloseLibrary(_DosBase);
  579. If _IntuitionBase <> nil then
  580. CloseLibrary(_IntuitionBase);
  581. _UtilityBase := nil;
  582. _DosBase := nil;
  583. _IntuitionBase := nil;
  584. asm
  585. clr.l d0
  586. move.b errnum,d0
  587. move.l STKPTR,sp
  588. rts
  589. end;
  590. end;
  591. { ************************ PARAMCOUNT/PARAMSTR *************************** }
  592. function paramcount : longint;
  593. Begin
  594. paramcount := argc;
  595. end;
  596. function args : pointer; assembler;
  597. asm
  598. move.l __ARGS,d0
  599. end;
  600. Function GetParamCount(const p: pchar): longint;
  601. var
  602. i: word;
  603. count: word;
  604. Begin
  605. i:=0;
  606. count:=0;
  607. while p[count] <> #0 do
  608. Begin
  609. if (p[count] <> ' ') and (p[count] <> #9) and (p[count] <> #0) then
  610. Begin
  611. i:=i+1;
  612. while (p[count] <> ' ') and (p[count] <> #9) and (p[count] <> #0) do
  613. count:=count+1;
  614. end;
  615. if p[count] = #0 then break;
  616. count:=count+1;
  617. end;
  618. GetParamCount:=longint(i);
  619. end;
  620. Function GetParam(index: word; const p : pchar): string;
  621. { On Entry: index = string index to correct parameter }
  622. { On exit: = correct character index into pchar array }
  623. { Returns correct index to command line argument }
  624. var
  625. count: word;
  626. localindex: word;
  627. l: byte;
  628. temp: string;
  629. Begin
  630. temp:='';
  631. count := 0;
  632. { first index is one }
  633. localindex := 1;
  634. l:=0;
  635. While p[count] <> #0 do
  636. Begin
  637. if (p[count] <> ' ') and (p[count] <> #9) then
  638. Begin
  639. if localindex = index then
  640. Begin
  641. while (p[count] <> #0) and (p[count] <> ' ') and (p[count] <> #9) and (l < 256) do
  642. Begin
  643. temp:=temp+p[count];
  644. l:=l+1;
  645. count:=count+1;
  646. end;
  647. temp[0]:=char(l);
  648. GetParam:=temp;
  649. exit;
  650. end;
  651. { Point to next argument in list }
  652. while (p[count] <> #0) and (p[count] <> ' ') and (p[count] <> #9) do
  653. Begin
  654. count:=count+1;
  655. end;
  656. localindex:=localindex+1;
  657. end;
  658. if p[count] = #0 then break;
  659. count:=count+1;
  660. end;
  661. GetParam:=temp;
  662. end;
  663. Function GetProgramDir : String;
  664. var
  665. s1: string;
  666. alock: longint;
  667. counter : byte;
  668. Begin
  669. FillChar(@s1,255,#0);
  670. { GetLock of program directory }
  671. asm
  672. move.l a6,d6 { save a6 }
  673. move.l _DOSBase,a6
  674. jsr _LVOGetProgramDir(a6)
  675. move.l d6,a6 { restore a6 }
  676. move.l d0,alock { save the lock }
  677. end;
  678. if alock <> 0 then
  679. Begin
  680. { Get the name from the lock! }
  681. asm
  682. movem.l d2/d3,-(sp) { save used registers }
  683. move.l alock,d1
  684. lea s1,a0 { Get pointer to string! }
  685. move.l a0,d2
  686. add.l #1,d2 { let us point past the length byte! }
  687. move.l #255,d3
  688. move.l a6,d6 { save a6 }
  689. move.l _DOSBase,a6
  690. jsr _LVONameFromLock(a6)
  691. move.l d6,a6 { restore a6 }
  692. movem.l (sp)+,d2/d3
  693. end;
  694. { no check out the length of the string }
  695. counter := 1;
  696. while s1[counter] <> #0 do
  697. Inc(counter);
  698. s1[0] := char(counter-1);
  699. GetProgramDir := s1;
  700. end
  701. else
  702. GetProgramDir := '';
  703. end;
  704. Function GetProgramName : string;
  705. { Returns ONLY the program name }
  706. { There seems to be a bug in v39 since if the program is not }
  707. { called from its home directory the program name will also }
  708. { contain the path! }
  709. var
  710. s1: string;
  711. counter : byte;
  712. Begin
  713. FillChar(@s1,255,#0);
  714. asm
  715. move.l d2,-(sp) { Save used register }
  716. lea s1,a0 { Get pointer to string! }
  717. move.l a0,d1
  718. add.l #1,d1 { point to correct offset }
  719. move.l #255,d2
  720. move.l a6,d6 { save a6 }
  721. move.l _DOSBase,a6
  722. jsr _LVOGetProgramName(a6)
  723. move.l d6,a6 { restore a6 }
  724. move.l (sp)+,d2 { restore saved register }
  725. end;
  726. { no check out and assign the length of the string }
  727. counter := 1;
  728. while s1[counter] <> #0 do
  729. Inc(counter);
  730. s1[0] := char(counter-1);
  731. { now remove any component path which should not be there }
  732. for counter:=length(s1) downto 1 do
  733. if (s1[counter] = '/') or (s1[counter] = ':') then break;
  734. { readjust counterv to point to character }
  735. if counter <> 1 then
  736. Inc(counter);
  737. GetProgramName:=copy(s1,counter,length(s1));
  738. end;
  739. function paramstr(l : longint) : string;
  740. var
  741. p : pchar;
  742. s1 : string;
  743. begin
  744. { -> Call AmigaOS GetProgramName }
  745. if l = 0 then
  746. Begin
  747. s1 := GetProgramDir;
  748. { If this is a root, then simply don't add '/' }
  749. if s1[length(s1)] = ':' then
  750. paramstr:=s1+GetProgramName
  751. else
  752. { add backslash directory }
  753. paramstr:=s1+'/'+GetProgramName
  754. end
  755. else
  756. if (l>0) and (l<=paramcount) then
  757. begin
  758. p:=args;
  759. paramstr:=GetParam(word(l),p);
  760. end
  761. else paramstr:='';
  762. end;
  763. { ************************************************************************ }
  764. procedure randomize;
  765. var
  766. hl : longint;
  767. time : TDateStamp;
  768. begin
  769. DateStamp(time);
  770. randseed:=time.ds_tick;
  771. end;
  772. function getheapstart:pointer;assembler;
  773. asm
  774. lea.l HEAP,a0
  775. move.l a0,d0
  776. end;
  777. function getheapsize:longint;assembler;
  778. asm
  779. move.l HEAP_SIZE,d0
  780. end ['D0'];
  781. { This routine is used to grow the heap. }
  782. { But here we do a trick, we say that the }
  783. { heap cannot be regrown! }
  784. function sbrk( size: longint): longint;
  785. var
  786. { on exit -1 = if fails. }
  787. p: longint;
  788. i: byte;
  789. Begin
  790. p:=0;
  791. { Is the pointer list full }
  792. if pointerlist[8] <> 0 then
  793. begin
  794. { yes, then don't allocate and simply exit }
  795. sbrk:=-1;
  796. exit;
  797. end;
  798. { Allocate best available memory }
  799. p:=AllocVec(size,0);
  800. if p = 0 then
  801. sbrk:=-1
  802. else
  803. Begin
  804. i:=1;
  805. { add it to the list of allocated pointers }
  806. { first find the last pointer in the list }
  807. while (i < 8) and (pointerlist[i] <> 0) do
  808. i:=i+1;
  809. pointerlist[i]:=p;
  810. sbrk:=p;
  811. end;
  812. end;
  813. {$I heap.inc}
  814. {****************************************************************************
  815. Low Level File Routines
  816. ****************************************************************************}
  817. procedure do_close(h : longint);
  818. { We cannot check for CTRL-C because this routine will be called }
  819. { on HALT to close all remaining opened files. Therefore no }
  820. { CTRL-C checking otherwise a recursive call might result! }
  821. {$ifdef debug}
  822. var
  823. buffer: array[0..255] of char;
  824. {$endif}
  825. begin
  826. { check if the file handle is in the list }
  827. { if so the put its field to closed }
  828. { SetClosedList(FileList,h);}
  829. {$ifdef debug}
  830. asm
  831. move.l h,d1
  832. move.l a6,d6
  833. move.l d2,-(sp)
  834. move.l d3,-(sp)
  835. lea buffer,a0
  836. move.l a0,d2
  837. move.l #255,d3
  838. move.l _DosBase,a6
  839. jsr _LVONameFromFH(a6)
  840. move.l d6,a6
  841. move.l (sp)+,d3
  842. move.l (sp)+,d2
  843. end;
  844. WriteLn(Buffer);
  845. {$endif debug}
  846. asm
  847. move.l h,d1
  848. move.l a6,d6 { save a6 }
  849. move.l _DOSBase,a6
  850. jsr _LVOClose(a6)
  851. move.l d6,a6 { restore a6 }
  852. end;
  853. end;
  854. function do_isdevice(handle:longint):boolean;
  855. begin
  856. if (handle=stdoutputhandle) or (handle=stdinputhandle) or
  857. (handle=stderrorhandle) then
  858. do_isdevice:=TRUE
  859. else
  860. do_isdevice:=FALSE;
  861. end;
  862. procedure do_erase(p : pchar);
  863. begin
  864. if (Setsignal(0,0) AND SIGBREAKF_CTRL_C) <> 0 then
  865. Begin
  866. SetSignal(0,SIGBREAKF_CTRL_C);
  867. Halt(CTRL_C);
  868. end;
  869. asm
  870. move.l a6,d6 { save a6 }
  871. move.l p,d1
  872. move.l _DOSBase,a6
  873. jsr _LVODeleteFile(a6)
  874. tst.l d0 { zero = failure }
  875. bne @noerror
  876. jsr _LVOIoErr(a6)
  877. move.w d0,errno
  878. @noerror:
  879. move.l d6,a6 { restore a6 }
  880. end;
  881. if errno <> 0 then
  882. Error2InOut;
  883. end;
  884. procedure do_rename(p1,p2 : pchar);
  885. begin
  886. if (Setsignal(0,0) AND SIGBREAKF_CTRL_C) <> 0 then
  887. Begin
  888. SetSignal(0,SIGBREAKF_CTRL_C);
  889. Halt(CTRL_C);
  890. end;
  891. asm
  892. move.l a6,d6 { save a6 }
  893. move.l d2,-(sp) { save d2 }
  894. move.l p1,d1
  895. move.l p2,d2
  896. move.l _DOSBase,a6
  897. jsr _LVORename(a6)
  898. move.l (sp)+,d2 { restore d2 }
  899. tst.l d0
  900. bne @dosreend { if zero = error }
  901. jsr _LVOIoErr(a6)
  902. move.w d0,errno
  903. @dosreend:
  904. move.l d6,a6 { restore a6 }
  905. end;
  906. if errno <> 0 then
  907. Error2InOut;
  908. end;
  909. function do_write(h,addr,len : longint) : longint;
  910. begin
  911. if (Setsignal(0,0) AND SIGBREAKF_CTRL_C) <> 0 then
  912. Begin
  913. SetSignal(0,SIGBREAKF_CTRL_C);
  914. Halt(CTRL_C);
  915. end;
  916. if len <= 0 then
  917. Begin
  918. do_write:=0;
  919. exit;
  920. end;
  921. asm
  922. move.l a6,d6
  923. movem.l d2/d3,-(sp)
  924. move.l h,d1 { we must of course set up the }
  925. move.l addr,d2 { parameters BEFORE getting }
  926. move.l len,d3 { _DOSBase }
  927. move.l _DOSBase,a6
  928. jsr _LVOWrite(a6)
  929. movem.l (sp)+,d2/d3
  930. cmp.l #-1,d0
  931. bne @doswrend { if -1 = error }
  932. jsr _LVOIoErr(a6)
  933. move.w d0,errno
  934. bra @doswrend2
  935. @doswrend:
  936. { we must restore the base pointer before setting the result }
  937. move.l d6,a6
  938. move.l d0,@RESULT
  939. bra @end
  940. @doswrend2:
  941. move.l d6,a6
  942. @end:
  943. end;
  944. If errno <> 0 then
  945. Error2InOut;
  946. end;
  947. function do_read(h,addr,len : longint) : longint;
  948. begin
  949. if (Setsignal(0,0) AND SIGBREAKF_CTRL_C) <> 0 then
  950. Begin
  951. SetSignal(0,SIGBREAKF_CTRL_C);
  952. Halt(CTRL_C);
  953. end;
  954. if len <= 0 then
  955. Begin
  956. do_read:=0;
  957. exit;
  958. end;
  959. asm
  960. move.l a6,d6
  961. movem.l d2/d3,-(sp)
  962. move.l h,d1 { we must set up aparamters BEFORE }
  963. move.l addr,d2 { setting up a6 for the OS call }
  964. move.l len,d3
  965. move.l _DOSBase,a6
  966. jsr _LVORead(a6)
  967. movem.l (sp)+,d2/d3
  968. cmp.l #-1,d0
  969. bne @doswrend { if -1 = error }
  970. jsr _LVOIoErr(a6)
  971. move.w d0,errno
  972. bra @doswrend2
  973. @doswrend:
  974. { to store a result for the function }
  975. { we must of course first get back the}
  976. { base pointer! }
  977. move.l d6,a6
  978. move.l d0,@RESULT
  979. bra @end
  980. @doswrend2:
  981. move.l d6,a6
  982. @end:
  983. end;
  984. If errno <> 0 then
  985. Error2InOut;
  986. end;
  987. function do_filepos(handle : longint) : longint;
  988. begin
  989. if (Setsignal(0,0) AND SIGBREAKF_CTRL_C) <> 0 then
  990. Begin
  991. { Clear CTRL-C signal }
  992. SetSignal(0,SIGBREAKF_CTRL_C);
  993. Halt(CTRL_C);
  994. end;
  995. asm
  996. move.l a6,d6
  997. move.l handle,d1
  998. move.l d2,-(sp)
  999. move.l d3,-(sp) { save registers }
  1000. clr.l d2 { offset 0 }
  1001. move.l #0,d3 { OFFSET_CURRENT }
  1002. move.l _DOSBase,a6
  1003. jsr _LVOSeek(a6)
  1004. move.l (sp)+,d3 { restore registers }
  1005. move.l (sp)+,d2
  1006. cmp.l #-1,d0 { is there a file access error? }
  1007. bne @noerr
  1008. jsr _LVOIoErr(a6)
  1009. move.w d0,errno
  1010. bra @fposend
  1011. @noerr:
  1012. move.l d6,a6 { restore a6 }
  1013. move.l d0,@Result
  1014. bra @end
  1015. @fposend:
  1016. move.l d6,a6 { restore a6 }
  1017. @end:
  1018. end;
  1019. If errno <> 0 then
  1020. Error2InOut;
  1021. end;
  1022. procedure do_seek(handle,pos : longint);
  1023. begin
  1024. if (Setsignal(0,0) AND SIGBREAKF_CTRL_C) <> 0 then
  1025. Begin
  1026. { Clear CTRL-C signal }
  1027. SetSignal(0,SIGBREAKF_CTRL_C);
  1028. Halt(CTRL_C);
  1029. end;
  1030. asm
  1031. move.l a6,d6
  1032. move.l handle,d1
  1033. move.l d2,-(sp)
  1034. move.l d3,-(sp) { save registers }
  1035. move.l pos,d2
  1036. { -1 }
  1037. move.l #$ffffffff,d3 { OFFSET_BEGINNING }
  1038. move.l _DOSBase,a6
  1039. jsr _LVOSeek(a6)
  1040. move.l (sp)+,d3 { restore registers }
  1041. move.l (sp)+,d2
  1042. cmp.l #-1,d0 { is there a file access error? }
  1043. bne @noerr
  1044. jsr _LVOIoErr(a6)
  1045. move.w d0,errno
  1046. bra @seekend
  1047. @noerr:
  1048. @seekend:
  1049. move.l d6,a6 { restore a6 }
  1050. end;
  1051. If errno <> 0 then
  1052. Error2InOut;
  1053. end;
  1054. function do_seekend(handle:longint):longint;
  1055. begin
  1056. if (Setsignal(0,0) AND SIGBREAKF_CTRL_C) <> 0 then
  1057. Begin
  1058. { Clear CTRL-C signal }
  1059. SetSignal(0,SIGBREAKF_CTRL_C);
  1060. Halt(CTRL_C);
  1061. end;
  1062. asm
  1063. { seek from end of file }
  1064. move.l a6,d6
  1065. move.l handle,d1
  1066. move.l d2,-(sp)
  1067. move.l d3,-(sp) { save registers }
  1068. clr.l d2
  1069. move.l #1,d3 { OFFSET_END }
  1070. move.l _DOSBase,a6
  1071. jsr _LVOSeek(a6)
  1072. move.l (sp)+,d3 { restore registers }
  1073. move.l (sp)+,d2
  1074. cmp.l #-1,d0 { is there a file access error? }
  1075. bne @noerr
  1076. jsr _LVOIoErr(a6)
  1077. move.w d0,errno
  1078. bra @seekend
  1079. @noerr:
  1080. move.l d6,a6 { restore a6 }
  1081. move.l d0,@Result
  1082. bra @end
  1083. @seekend:
  1084. move.l d6,a6 { restore a6 }
  1085. @end:
  1086. end;
  1087. If Errno <> 0 then
  1088. Error2InOut;
  1089. end;
  1090. function do_filesize(handle : longint) : longint;
  1091. var
  1092. aktfilepos : longint;
  1093. begin
  1094. if (Setsignal(0,0) AND SIGBREAKF_CTRL_C) <> 0 then
  1095. Begin
  1096. { Clear CTRL-C signal }
  1097. SetSignal(0,SIGBREAKF_CTRL_C);
  1098. Halt(CTRL_C);
  1099. end;
  1100. aktfilepos:=do_filepos(handle);
  1101. { We have to do this two times, because seek returns the }
  1102. { OLD position }
  1103. do_filesize:=do_seekend(handle);
  1104. do_filesize:=do_seekend(handle);
  1105. do_seek(handle,aktfilepos);
  1106. end;
  1107. procedure do_truncate (handle,pos:longint);
  1108. begin
  1109. { Point to the end of the file }
  1110. { with the new size }
  1111. asm
  1112. @noerr_one: { Seek a second time }
  1113. move.l a6,d6 { Save base pointer }
  1114. move.l handle,d1
  1115. move.l d2,-(sp)
  1116. move.l d3,-(sp) { save registers }
  1117. move.l pos,d2
  1118. move.l #-1,d3 { Setup correct move type }
  1119. move.l _DOSBase,a6 { from beginning of file }
  1120. jsr _LVOSetFileSize(a6)
  1121. move.l (sp)+,d3 { restore registers }
  1122. move.l (sp)+,d2
  1123. cmp.l #-1,d0 { is there a file access error? }
  1124. bne @noerr
  1125. jsr _LVOIoErr(a6)
  1126. move.w d0,errno { Global variable, so no need }
  1127. @noerr: { to restore base pointer now }
  1128. move.l d6,a6 { Restore base pointer }
  1129. end;
  1130. If Errno <> 0 then
  1131. Error2InOut;
  1132. end;
  1133. procedure do_open(var f;p:pchar;flags:longint);
  1134. {
  1135. filerec and textrec have both handle and mode as the first items so
  1136. they could use the same routine for opening/creating.
  1137. when (flags and $10) the file will be append
  1138. when (flags and $100) the file will be truncate/rewritten
  1139. when (flags and $1000) there is no check for close (needed for textfiles)
  1140. }
  1141. var
  1142. i,j : longint;
  1143. oflags: longint;
  1144. path : string;
  1145. buffer : array[0..255] of char;
  1146. index : integer;
  1147. s : string;
  1148. begin
  1149. path:=strpas(p);
  1150. for index:=1 to length(path) do
  1151. if path[index]='\' then path[index]:='/';
  1152. { remove any dot characters and replace by their current }
  1153. { directory equivalent. }
  1154. if pos('../',path) = 1 then
  1155. { look for parent directory }
  1156. Begin
  1157. delete(path,1,3);
  1158. getdir(0,s);
  1159. j:=length(s);
  1160. while (s[j] <> '/') AND (s[j] <> ':') AND (j > 0 ) do
  1161. dec(j);
  1162. if j > 0 then
  1163. s:=copy(s,1,j);
  1164. path:=s+path;
  1165. end
  1166. else
  1167. if pos('./',path) = 1 then
  1168. { look for current directory }
  1169. Begin
  1170. delete(path,1,2);
  1171. getdir(0,s);
  1172. if (s[length(s)] <> '/') and (s[length(s)] <> ':') then
  1173. s:=s+'/';
  1174. path:=s+path;
  1175. end;
  1176. move(path[1],buffer,length(path));
  1177. buffer[length(path)]:=#0;
  1178. { close first if opened }
  1179. if ((flags and $1000)=0) then
  1180. begin
  1181. case filerec(f).mode of
  1182. fminput,fmoutput,fminout : Do_Close(filerec(f).handle);
  1183. fmclosed : ;
  1184. else
  1185. begin
  1186. inoutres:=102; {not assigned}
  1187. exit;
  1188. end;
  1189. end;
  1190. end;
  1191. { reset file handle }
  1192. filerec(f).handle:=UnusedHandle;
  1193. { convert filemode to filerec modes }
  1194. { READ/WRITE on existing file }
  1195. { RESET/APPEND }
  1196. oflags := 1005;
  1197. case (flags and 3) of
  1198. 0 : begin
  1199. filerec(f).mode:=fminput;
  1200. end;
  1201. 1 : filerec(f).mode:=fmoutput;
  1202. 2 : filerec(f).mode:=fminout;
  1203. end;
  1204. { READ/WRITE mode, create file in all cases }
  1205. { REWRITE }
  1206. if (flags and $100)<>0 then
  1207. begin
  1208. filerec(f).mode:=fmoutput;
  1209. oflags := 1006;
  1210. end
  1211. else
  1212. { READ/WRITE mode on existing file }
  1213. { APPEND }
  1214. if (flags and $10)<>0 then
  1215. begin
  1216. filerec(f).mode:=fmoutput;
  1217. oflags := 1005;
  1218. end;
  1219. { empty name is special }
  1220. if p[0]=#0 then
  1221. begin
  1222. case filerec(f).mode of
  1223. fminput : filerec(f).handle:=StdInputHandle;
  1224. fmappend,
  1225. fmoutput : begin
  1226. filerec(f).handle:=StdOutputHandle;
  1227. filerec(f).mode:=fmoutput; {fool fmappend}
  1228. end;
  1229. end;
  1230. exit;
  1231. end;
  1232. asm
  1233. move.l a6,d6 { save a6 }
  1234. move.l d2,-(sp)
  1235. lea buffer,a0
  1236. move.l a0,d1
  1237. move.l oflags,d2 { MODE_READWRITE }
  1238. move.l _DOSBase,a6
  1239. jsr _LVOOpen(a6)
  1240. tst.l d0
  1241. bne @noopenerror { on zero an error occured }
  1242. jsr _LVOIoErr(a6)
  1243. move.w d0,errno
  1244. bra @openend
  1245. @noopenerror:
  1246. move.l (sp)+,d2
  1247. move.l d6,a6 { restore a6 }
  1248. move.l d0,i { we need the base pointer to access this variable }
  1249. bra @end
  1250. @openend:
  1251. move.l d6,a6 { restore a6 }
  1252. move.l (sp)+,d2
  1253. @end:
  1254. end;
  1255. (* if Errno = 0 then*)
  1256. { No error, add file handle to linked list }
  1257. { this must be checked before the call to }
  1258. { Error2InIOut since it resets Errno to 0 }
  1259. (* AddToList(FileList,i);*)
  1260. If Errno <> 0 then
  1261. Error2InOut;
  1262. filerec(f).handle:=i;
  1263. if (flags and $10)<>0 then
  1264. do_seekend(filerec(f).handle);
  1265. end;
  1266. {*****************************************************************************
  1267. UnTyped File Handling
  1268. *****************************************************************************}
  1269. {$i file.inc}
  1270. {*****************************************************************************
  1271. Typed File Handling
  1272. *****************************************************************************}
  1273. {$i typefile.inc}
  1274. {*****************************************************************************
  1275. Text File Handling
  1276. *****************************************************************************}
  1277. {$i text.inc}
  1278. {*****************************************************************************
  1279. Directory Handling
  1280. *****************************************************************************}
  1281. procedure mkdir(const s : string);[IOCheck];
  1282. var
  1283. buffer : array[0..255] of char;
  1284. j: Integer;
  1285. temp : string;
  1286. begin
  1287. { We must check the Ctrl-C before IOChecking of course! }
  1288. if (Setsignal(0,0) AND SIGBREAKF_CTRL_C) <> 0 then
  1289. Begin
  1290. { Clear CTRL-C signal }
  1291. SetSignal(0,SIGBREAKF_CTRL_C);
  1292. Halt(CTRL_C);
  1293. end;
  1294. If InOutRes <> 0 then exit;
  1295. temp:=s;
  1296. for j:=1 to length(temp) do
  1297. if temp[j] = '\' then temp[j] := '/';
  1298. move(temp[1],buffer,length(temp));
  1299. buffer[length(temp)]:=#0;
  1300. asm
  1301. move.l a6,d6
  1302. { we must load the parameters BEFORE setting up the }
  1303. { OS call with a6 }
  1304. lea buffer,a0
  1305. move.l a0,d1
  1306. move.l _DosBase,a6
  1307. jsr _LVOCreateDir(a6)
  1308. tst.l d0
  1309. bne @noerror
  1310. jsr _LVOIoErr(a6)
  1311. move.w d0,errno
  1312. bra @end
  1313. @noerror:
  1314. { Now we must unlock the directory }
  1315. { d0 = lock returned by create dir }
  1316. move.l d0,d1
  1317. jsr _LVOUnlock(a6)
  1318. @end:
  1319. { restore base pointer }
  1320. move.l d6,a6
  1321. end;
  1322. If errno <> 0 then
  1323. Error2InOut;
  1324. end;
  1325. procedure rmdir(const s : string);[IOCheck];
  1326. var
  1327. buffer : array[0..255] of char;
  1328. j : Integer;
  1329. temp : string;
  1330. begin
  1331. { We must check the Ctrl-C before IOChecking of course! }
  1332. if (Setsignal(0,0) AND SIGBREAKF_CTRL_C) <> 0 then
  1333. Begin
  1334. { Clear CTRL-C signal }
  1335. SetSignal(0,SIGBREAKF_CTRL_C);
  1336. Halt(CTRL_C);
  1337. end;
  1338. If InOutRes <> 0 then exit;
  1339. temp:=s;
  1340. for j:=1 to length(temp) do
  1341. if temp[j] = '\' then temp[j] := '/';
  1342. move(temp[1],buffer,length(temp));
  1343. buffer[length(temp)]:=#0;
  1344. do_erase(buffer);
  1345. end;
  1346. procedure chdir(const s : string);[IOCheck];
  1347. var
  1348. buffer : array[0..255] of char;
  1349. alock : longint;
  1350. FIB :pFileInfoBlock;
  1351. j: integer;
  1352. temp : string;
  1353. begin
  1354. if (Setsignal(0,0) AND SIGBREAKF_CTRL_C) <> 0 then
  1355. Begin
  1356. { Clear CTRL-C signal }
  1357. SetSignal(0,SIGBREAKF_CTRL_C);
  1358. Halt(CTRL_C);
  1359. end;
  1360. If InOutRes <> 0 then exit;
  1361. temp:=s;
  1362. for j:=1 to length(temp) do
  1363. if temp[j] = '\' then temp[j] := '/';
  1364. { Return parent directory }
  1365. if s = '..' then
  1366. Begin
  1367. getdir(0,temp);
  1368. j:=length(temp);
  1369. { Look through the previous paths }
  1370. while (temp[j] <> '/') AND (temp[j] <> ':') AND (j > 0 ) do
  1371. dec(j);
  1372. if j > 0 then
  1373. temp:=copy(temp,1,j);
  1374. end;
  1375. alock := 0;
  1376. fib:=nil;
  1377. new(fib);
  1378. move(temp[1],buffer,length(temp));
  1379. buffer[length(temp)]:=#0;
  1380. { Changing the directory is a pretty complicated affair }
  1381. { 1) Obtain a lock on the directory }
  1382. { 2) CurrentDir the lock }
  1383. asm
  1384. lea buffer,a0
  1385. move.l a0,d1 { pointer to buffer in d1 }
  1386. move.l d2,-(sp) { save d2 register }
  1387. move.l #-2,d2 { ACCESS_READ lock }
  1388. move.l a6,d6 { Save base pointer }
  1389. move.l _DosBase,a6
  1390. jsr _LVOLock(a6){ Lock the directory }
  1391. move.l (sp)+,d2 { Restore d2 register }
  1392. tst.l d0 { zero = error! }
  1393. bne @noerror
  1394. jsr _LVOIoErr(a6)
  1395. move.w d0,errno
  1396. move.l d6,a6 { reset base pointer }
  1397. bra @End
  1398. @noerror:
  1399. move.l d6,a6 { reset base pointer }
  1400. move.l d0,alock { save the lock }
  1401. @End:
  1402. end;
  1403. If errno <> 0 then
  1404. Begin
  1405. Error2InOut;
  1406. exit;
  1407. end;
  1408. if (Examine(alock, fib^) = TRUE) AND (fib^.fib_DirEntryType > 0) then
  1409. Begin
  1410. alock := CurrentDir(alock);
  1411. if OrigDir = 0 then
  1412. Begin
  1413. OrigDir := alock;
  1414. alock := 0;
  1415. end;
  1416. end;
  1417. if alock <> 0 then
  1418. Unlock(alock);
  1419. if assigned(fib) then dispose(fib);
  1420. end;
  1421. Procedure GetCwd(var path: string);
  1422. var
  1423. lock: longint;
  1424. fib: PfileInfoBlock;
  1425. len : integer;
  1426. newlock : longint;
  1427. elen : integer;
  1428. Process : PProcess;
  1429. Begin
  1430. len := 0;
  1431. path := '';
  1432. fib := nil;
  1433. { By using a pointer instead of a local variable}
  1434. { we are assured that the pointer is aligned on }
  1435. { a dword boundary. }
  1436. new(fib);
  1437. Process := FindTask(nil);
  1438. if (process^.pr_Task.tc_Node.ln_Type = NT_TASK) then
  1439. Begin
  1440. path:='';
  1441. exit;
  1442. end;
  1443. lock := DupLock(process^.pr_CurrentDir);
  1444. if (Lock = 0) then
  1445. Begin
  1446. path:='';
  1447. exit;
  1448. end;
  1449. While (lock <> 0) and (Examine(lock,FIB^) = TRUE) do
  1450. Begin
  1451. elen := strlen(fib^.fib_FileName);
  1452. if (len + elen + 2 > 255) then
  1453. break;
  1454. newlock := ParentDir(lock);
  1455. if (len <> 0) then
  1456. Begin
  1457. if (newlock <> 0) then
  1458. path:='/'+path
  1459. else
  1460. path:=':'+path;
  1461. path:=strpas(fib^.fib_FileName)+path;
  1462. Inc(len);
  1463. end
  1464. else
  1465. Begin
  1466. path:=strpas(fib^.fib_Filename);
  1467. if (newlock = 0) then
  1468. path:=path+':';
  1469. end;
  1470. len := len + elen;
  1471. UnLock(lock);
  1472. lock := newlock;
  1473. end;
  1474. if (lock <> 0) then
  1475. Begin
  1476. UnLock(lock);
  1477. path := '';
  1478. end;
  1479. if assigned(fib) then dispose(fib);
  1480. end;
  1481. procedure getdir(drivenr : byte;var dir : string);
  1482. begin
  1483. if (Setsignal(0,0) AND SIGBREAKF_CTRL_C) <> 0 then
  1484. Begin
  1485. { Clear CTRL-C signal }
  1486. SetSignal(0,SIGBREAKF_CTRL_C);
  1487. Halt(CTRL_C);
  1488. end;
  1489. GetCwd(dir);
  1490. If errno <> 0 then
  1491. Error2InOut;
  1492. end;
  1493. {*****************************************************************************
  1494. SystemUnit Initialization
  1495. *****************************************************************************}
  1496. Procedure Startup; Assembler;
  1497. asm
  1498. move.l a6,d6 { save a6 }
  1499. move.l (4),a6 { get ExecBase pointer }
  1500. move.l a6,_ExecBase
  1501. suba.l a1,a1
  1502. jsr _LVOFindTask(a6)
  1503. move.l d0,a0
  1504. { Check the stack value }
  1505. { are we running from a CLI? }
  1506. tst.l 172(a0) { 172 = pr_CLI }
  1507. bne @fromCLI
  1508. { we do not support Workbench yet .. }
  1509. move.l d6,a6 { restore a6 }
  1510. move.l #1,d0
  1511. jsr HALT_ERROR
  1512. @fromCLI:
  1513. { Open the following libraries: }
  1514. { Intuition.library }
  1515. { dos.library }
  1516. moveq.l #0,d0
  1517. move.l intuitionname,a1 { directly since it is a pchar }
  1518. jsr _LVOOpenLibrary(a6)
  1519. move.l d0,_IntuitionBase
  1520. beq @exitprg
  1521. moveq.l #0,d0
  1522. move.l utilityname,a1 { directly since it is a pchar }
  1523. jsr _LVOOpenLibrary(a6)
  1524. move.l d0,_UtilityBase
  1525. beq @exitprg
  1526. moveq.l #0,d0
  1527. move.l dosname,a1 { directly since it is a pchar }
  1528. jsr _LVOOpenLibrary(a6)
  1529. move.l d0,_DOSBase
  1530. beq @exitprg
  1531. { Find standard input and output }
  1532. { for CLI }
  1533. @OpenFiles:
  1534. move.l _DOSBase,a6
  1535. jsr _LVOInput(a6) { get standard in }
  1536. move.l d0, StdInputHandle { save standard Input handle }
  1537. { move.l d0,d1 }{ set up for next call }
  1538. { jsr _LVOIsInteractive(a6)}{ is it interactive? }
  1539. { move.l #_Input,a0 }{ get file record again }
  1540. { move.b d0,INTERACTIVE(a0) }{ set flag }
  1541. { beq StdInNotInteractive }{ skip this if not interactive }
  1542. { move.l BUFFER(a0),a1 }{ get buffer address }
  1543. { add.l #1,a1 }{ make end one byte further on }
  1544. { move.l a1,MAX(a0) }{ set buffer size }
  1545. { move.l a1,CURRENT(a0) }{ will need a read }
  1546. bra @OpenStdOutput
  1547. @StdInNotInteractive
  1548. { jsr _p%FillBuffer } { fill the buffer }
  1549. @OpenStdOutput
  1550. jsr _LVOOutput(a6) { get ouput file handle }
  1551. move.l d0,StdOutputHandle { get file record }
  1552. bra @startupend
  1553. { move.l d0,d1 } { set up for call }
  1554. { jsr _LVOIsInteractive(a6) } { is it interactive? }
  1555. { move.l #_Output,a0 } { get file record }
  1556. { move.b d0,INTERACTIVE(a0)} { set flag }
  1557. @exitprg:
  1558. move.l d6,a6 { restore a6 }
  1559. move.l #219,d0
  1560. jsr HALT_ERROR
  1561. @startupend:
  1562. move.l d6,a6 { restore a6 }
  1563. end;
  1564. begin
  1565. errno:= 0;
  1566. FromHalt := FALSE;
  1567. { Initial state is on -- in case of RunErrors before the i/o handles are }
  1568. { ok. }
  1569. Initial:=TRUE;
  1570. { Initialize ExitProc }
  1571. ExitProc:=Nil;
  1572. Startup;
  1573. { to test stack depth }
  1574. loweststack:=maxlongint;
  1575. { Setup heap }
  1576. InitHeap;
  1577. { Setup stdin, stdout and stderr }
  1578. OpenStdIO(Input,fmInput,StdInputHandle);
  1579. OpenStdIO(Output,fmOutput,StdOutputHandle);
  1580. { The Amiga does not seem to have a StdError }
  1581. { handle, therefore make the StdError handle }
  1582. { equal to the StdOutputHandle. }
  1583. StdErrorHandle := StdOutputHandle;
  1584. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  1585. { Now Handles and function handlers are setup }
  1586. { correctly. }
  1587. Initial:=FALSE;
  1588. { Reset IO Error }
  1589. InOutRes:=0;
  1590. { Startup }
  1591. { Only AmigaOS v2.04 or greater is supported }
  1592. If KickVersion < 36 then
  1593. Begin
  1594. WriteLn('v36 or greater of Kickstart required.');
  1595. Halt(1);
  1596. end;
  1597. argc:=GetParamCount(args);
  1598. OrigDir := 0;
  1599. FileList := nil;
  1600. old_Exit:=exitproc;
  1601. Exitproc:=@ExitCall;
  1602. end.
  1603. {
  1604. $Log$
  1605. Revision 1.10 1998-09-14 10:48:00 peter
  1606. * FPC_ names
  1607. * Heap manager is now system independent
  1608. Revision 1.9 1998/08/17 12:34:22 carl
  1609. * chdir accepts .. characters
  1610. + added ctrl-c checking
  1611. + implemented sbrk
  1612. * exit code was never called if no error was found on exit!
  1613. * register was not saved in do_open
  1614. Revision 1.8 1998/07/13 12:32:18 carl
  1615. * do_truncate works, some cleanup
  1616. Revision 1.6 1998/07/02 12:37:52 carl
  1617. * IOCheck for chdir,rmdir and mkdir as in TP
  1618. Revision 1.5 1998/07/01 14:30:56 carl
  1619. * forgot that includes are case sensitive
  1620. Revision 1.4 1998/07/01 14:13:50 carl
  1621. * do_open bugfix
  1622. * correct conversion of Amiga error codes to TP error codes
  1623. * InoutRes word bugfix
  1624. * parameter counting fixed
  1625. * new stack checking implemented
  1626. + IOCheck for chdir,rmdir,getdir and rmdir
  1627. * do_filepos was wrong
  1628. + chdir correctly implemented
  1629. * getdir correctly implemented
  1630. Revision 1.1.1.1 1998/03/25 11:18:47 root
  1631. * Restored version
  1632. Revision 1.14 1998/03/21 04:20:09 carl
  1633. * correct ExecBase pointer (from Nils Sjoholm)
  1634. * correct OpenLibrary vector (from Nils Sjoholm)
  1635. Revision 1.13 1998/03/14 21:34:32 carl
  1636. * forgot to save a6 in Startup routine
  1637. Revision 1.12 1998/02/24 21:19:42 carl
  1638. *** empty log message ***
  1639. Revision 1.11 1998/02/23 02:22:49 carl
  1640. * bugfix if linking problems
  1641. Revision 1.9 1998/02/06 16:34:32 carl
  1642. + do_open is now standard with other platforms
  1643. Revision 1.8 1998/02/02 15:01:45 carl
  1644. * fixed bug with opening library versions (from Nils Sjoholm)
  1645. Revision 1.7 1998/01/31 19:35:19 carl
  1646. + added opening of utility.library
  1647. Revision 1.6 1998/01/29 23:20:54 peter
  1648. - Removed Backslash convert
  1649. Revision 1.5 1998/01/27 10:55:04 peter
  1650. * Amiga uses / not \, so change AllowSlash -> AllowBackSlash
  1651. Revision 1.4 1998/01/25 21:53:20 peter
  1652. + Universal Handles support for StdIn/StdOut/StdErr
  1653. * Updated layout of sysamiga.pas
  1654. Revision 1.3 1998/01/24 21:09:53 carl
  1655. + added missing input/output function pointers
  1656. Revision 1.2 1998/01/24 14:08:25 carl
  1657. * RunError 217 --> RunError 219 (cannot open lib)
  1658. + Standard Handle names implemented
  1659. Revision 1.1 1998/01/24 05:12:15 carl
  1660. + initial revision, some stuff still missing though.
  1661. (and as you might imagine ... untested :))
  1662. }