sysamiga.pas 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875
  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. { This routine is used to grow the heap. }
  773. { But here we do a trick, we say that the }
  774. { heap cannot be regrown! }
  775. function sbrk( size: longint): longint;
  776. var
  777. { on exit -1 = if fails. }
  778. p: longint;
  779. i: byte;
  780. Begin
  781. p:=0;
  782. { Is the pointer list full }
  783. if pointerlist[8] <> 0 then
  784. begin
  785. { yes, then don't allocate and simply exit }
  786. sbrk:=-1;
  787. exit;
  788. end;
  789. { Allocate best available memory }
  790. p:=AllocVec(size,0);
  791. if p = 0 then
  792. sbrk:=-1
  793. else
  794. Begin
  795. i:=1;
  796. { add it to the list of allocated pointers }
  797. { first find the last pointer in the list }
  798. while (i < 8) and (pointerlist[i] <> 0) do
  799. i:=i+1;
  800. pointerlist[i]:=p;
  801. sbrk:=p;
  802. end;
  803. end;
  804. {$I heap.inc}
  805. {****************************************************************************
  806. Low Level File Routines
  807. ****************************************************************************}
  808. procedure do_close(h : longint);
  809. { We cannot check for CTRL-C because this routine will be called }
  810. { on HALT to close all remaining opened files. Therefore no }
  811. { CTRL-C checking otherwise a recursive call might result! }
  812. {$ifdef debug}
  813. var
  814. buffer: array[0..255] of char;
  815. {$endif}
  816. begin
  817. { check if the file handle is in the list }
  818. { if so the put its field to closed }
  819. { SetClosedList(FileList,h);}
  820. {$ifdef debug}
  821. asm
  822. move.l h,d1
  823. move.l a6,d6
  824. move.l d2,-(sp)
  825. move.l d3,-(sp)
  826. lea buffer,a0
  827. move.l a0,d2
  828. move.l #255,d3
  829. move.l _DosBase,a6
  830. jsr _LVONameFromFH(a6)
  831. move.l d6,a6
  832. move.l (sp)+,d3
  833. move.l (sp)+,d2
  834. end;
  835. WriteLn(Buffer);
  836. {$endif debug}
  837. asm
  838. move.l h,d1
  839. move.l a6,d6 { save a6 }
  840. move.l _DOSBase,a6
  841. jsr _LVOClose(a6)
  842. move.l d6,a6 { restore a6 }
  843. end;
  844. end;
  845. function do_isdevice(handle:longint):boolean;
  846. begin
  847. if (handle=stdoutputhandle) or (handle=stdinputhandle) or
  848. (handle=stderrorhandle) then
  849. do_isdevice:=TRUE
  850. else
  851. do_isdevice:=FALSE;
  852. end;
  853. procedure do_erase(p : pchar);
  854. begin
  855. if (Setsignal(0,0) AND SIGBREAKF_CTRL_C) <> 0 then
  856. Begin
  857. SetSignal(0,SIGBREAKF_CTRL_C);
  858. Halt(CTRL_C);
  859. end;
  860. asm
  861. move.l a6,d6 { save a6 }
  862. move.l p,d1
  863. move.l _DOSBase,a6
  864. jsr _LVODeleteFile(a6)
  865. tst.l d0 { zero = failure }
  866. bne @noerror
  867. jsr _LVOIoErr(a6)
  868. move.w d0,errno
  869. @noerror:
  870. move.l d6,a6 { restore a6 }
  871. end;
  872. if errno <> 0 then
  873. Error2InOut;
  874. end;
  875. procedure do_rename(p1,p2 : pchar);
  876. begin
  877. if (Setsignal(0,0) AND SIGBREAKF_CTRL_C) <> 0 then
  878. Begin
  879. SetSignal(0,SIGBREAKF_CTRL_C);
  880. Halt(CTRL_C);
  881. end;
  882. asm
  883. move.l a6,d6 { save a6 }
  884. move.l d2,-(sp) { save d2 }
  885. move.l p1,d1
  886. move.l p2,d2
  887. move.l _DOSBase,a6
  888. jsr _LVORename(a6)
  889. move.l (sp)+,d2 { restore d2 }
  890. tst.l d0
  891. bne @dosreend { if zero = error }
  892. jsr _LVOIoErr(a6)
  893. move.w d0,errno
  894. @dosreend:
  895. move.l d6,a6 { restore a6 }
  896. end;
  897. if errno <> 0 then
  898. Error2InOut;
  899. end;
  900. function do_write(h,addr,len : longint) : longint;
  901. begin
  902. if (Setsignal(0,0) AND SIGBREAKF_CTRL_C) <> 0 then
  903. Begin
  904. SetSignal(0,SIGBREAKF_CTRL_C);
  905. Halt(CTRL_C);
  906. end;
  907. if len <= 0 then
  908. Begin
  909. do_write:=0;
  910. exit;
  911. end;
  912. asm
  913. move.l a6,d6
  914. movem.l d2/d3,-(sp)
  915. move.l h,d1 { we must of course set up the }
  916. move.l addr,d2 { parameters BEFORE getting }
  917. move.l len,d3 { _DOSBase }
  918. move.l _DOSBase,a6
  919. jsr _LVOWrite(a6)
  920. movem.l (sp)+,d2/d3
  921. cmp.l #-1,d0
  922. bne @doswrend { if -1 = error }
  923. jsr _LVOIoErr(a6)
  924. move.w d0,errno
  925. bra @doswrend2
  926. @doswrend:
  927. { we must restore the base pointer before setting the result }
  928. move.l d6,a6
  929. move.l d0,@RESULT
  930. bra @end
  931. @doswrend2:
  932. move.l d6,a6
  933. @end:
  934. end;
  935. If errno <> 0 then
  936. Error2InOut;
  937. end;
  938. function do_read(h,addr,len : longint) : longint;
  939. begin
  940. if (Setsignal(0,0) AND SIGBREAKF_CTRL_C) <> 0 then
  941. Begin
  942. SetSignal(0,SIGBREAKF_CTRL_C);
  943. Halt(CTRL_C);
  944. end;
  945. if len <= 0 then
  946. Begin
  947. do_read:=0;
  948. exit;
  949. end;
  950. asm
  951. move.l a6,d6
  952. movem.l d2/d3,-(sp)
  953. move.l h,d1 { we must set up aparamters BEFORE }
  954. move.l addr,d2 { setting up a6 for the OS call }
  955. move.l len,d3
  956. move.l _DOSBase,a6
  957. jsr _LVORead(a6)
  958. movem.l (sp)+,d2/d3
  959. cmp.l #-1,d0
  960. bne @doswrend { if -1 = error }
  961. jsr _LVOIoErr(a6)
  962. move.w d0,errno
  963. bra @doswrend2
  964. @doswrend:
  965. { to store a result for the function }
  966. { we must of course first get back the}
  967. { base pointer! }
  968. move.l d6,a6
  969. move.l d0,@RESULT
  970. bra @end
  971. @doswrend2:
  972. move.l d6,a6
  973. @end:
  974. end;
  975. If errno <> 0 then
  976. Error2InOut;
  977. end;
  978. function do_filepos(handle : longint) : longint;
  979. begin
  980. if (Setsignal(0,0) AND SIGBREAKF_CTRL_C) <> 0 then
  981. Begin
  982. { Clear CTRL-C signal }
  983. SetSignal(0,SIGBREAKF_CTRL_C);
  984. Halt(CTRL_C);
  985. end;
  986. asm
  987. move.l a6,d6
  988. move.l handle,d1
  989. move.l d2,-(sp)
  990. move.l d3,-(sp) { save registers }
  991. clr.l d2 { offset 0 }
  992. move.l #0,d3 { OFFSET_CURRENT }
  993. move.l _DOSBase,a6
  994. jsr _LVOSeek(a6)
  995. move.l (sp)+,d3 { restore registers }
  996. move.l (sp)+,d2
  997. cmp.l #-1,d0 { is there a file access error? }
  998. bne @noerr
  999. jsr _LVOIoErr(a6)
  1000. move.w d0,errno
  1001. bra @fposend
  1002. @noerr:
  1003. move.l d6,a6 { restore a6 }
  1004. move.l d0,@Result
  1005. bra @end
  1006. @fposend:
  1007. move.l d6,a6 { restore a6 }
  1008. @end:
  1009. end;
  1010. If errno <> 0 then
  1011. Error2InOut;
  1012. end;
  1013. procedure do_seek(handle,pos : longint);
  1014. begin
  1015. if (Setsignal(0,0) AND SIGBREAKF_CTRL_C) <> 0 then
  1016. Begin
  1017. { Clear CTRL-C signal }
  1018. SetSignal(0,SIGBREAKF_CTRL_C);
  1019. Halt(CTRL_C);
  1020. end;
  1021. asm
  1022. move.l a6,d6
  1023. move.l handle,d1
  1024. move.l d2,-(sp)
  1025. move.l d3,-(sp) { save registers }
  1026. move.l pos,d2
  1027. { -1 }
  1028. move.l #$ffffffff,d3 { OFFSET_BEGINNING }
  1029. move.l _DOSBase,a6
  1030. jsr _LVOSeek(a6)
  1031. move.l (sp)+,d3 { restore registers }
  1032. move.l (sp)+,d2
  1033. cmp.l #-1,d0 { is there a file access error? }
  1034. bne @noerr
  1035. jsr _LVOIoErr(a6)
  1036. move.w d0,errno
  1037. bra @seekend
  1038. @noerr:
  1039. @seekend:
  1040. move.l d6,a6 { restore a6 }
  1041. end;
  1042. If errno <> 0 then
  1043. Error2InOut;
  1044. end;
  1045. function do_seekend(handle:longint):longint;
  1046. begin
  1047. if (Setsignal(0,0) AND SIGBREAKF_CTRL_C) <> 0 then
  1048. Begin
  1049. { Clear CTRL-C signal }
  1050. SetSignal(0,SIGBREAKF_CTRL_C);
  1051. Halt(CTRL_C);
  1052. end;
  1053. asm
  1054. { seek from end of file }
  1055. move.l a6,d6
  1056. move.l handle,d1
  1057. move.l d2,-(sp)
  1058. move.l d3,-(sp) { save registers }
  1059. clr.l d2
  1060. move.l #1,d3 { OFFSET_END }
  1061. move.l _DOSBase,a6
  1062. jsr _LVOSeek(a6)
  1063. move.l (sp)+,d3 { restore registers }
  1064. move.l (sp)+,d2
  1065. cmp.l #-1,d0 { is there a file access error? }
  1066. bne @noerr
  1067. jsr _LVOIoErr(a6)
  1068. move.w d0,errno
  1069. bra @seekend
  1070. @noerr:
  1071. move.l d6,a6 { restore a6 }
  1072. move.l d0,@Result
  1073. bra @end
  1074. @seekend:
  1075. move.l d6,a6 { restore a6 }
  1076. @end:
  1077. end;
  1078. If Errno <> 0 then
  1079. Error2InOut;
  1080. end;
  1081. function do_filesize(handle : longint) : longint;
  1082. var
  1083. aktfilepos : longint;
  1084. begin
  1085. if (Setsignal(0,0) AND SIGBREAKF_CTRL_C) <> 0 then
  1086. Begin
  1087. { Clear CTRL-C signal }
  1088. SetSignal(0,SIGBREAKF_CTRL_C);
  1089. Halt(CTRL_C);
  1090. end;
  1091. aktfilepos:=do_filepos(handle);
  1092. { We have to do this two times, because seek returns the }
  1093. { OLD position }
  1094. do_filesize:=do_seekend(handle);
  1095. do_filesize:=do_seekend(handle);
  1096. do_seek(handle,aktfilepos);
  1097. end;
  1098. procedure do_truncate (handle,pos:longint);
  1099. begin
  1100. { Point to the end of the file }
  1101. { with the new size }
  1102. asm
  1103. @noerr_one: { Seek a second time }
  1104. move.l a6,d6 { Save base pointer }
  1105. move.l handle,d1
  1106. move.l d2,-(sp)
  1107. move.l d3,-(sp) { save registers }
  1108. move.l pos,d2
  1109. move.l #-1,d3 { Setup correct move type }
  1110. move.l _DOSBase,a6 { from beginning of file }
  1111. jsr _LVOSetFileSize(a6)
  1112. move.l (sp)+,d3 { restore registers }
  1113. move.l (sp)+,d2
  1114. cmp.l #-1,d0 { is there a file access error? }
  1115. bne @noerr
  1116. jsr _LVOIoErr(a6)
  1117. move.w d0,errno { Global variable, so no need }
  1118. @noerr: { to restore base pointer now }
  1119. move.l d6,a6 { Restore base pointer }
  1120. end;
  1121. If Errno <> 0 then
  1122. Error2InOut;
  1123. end;
  1124. procedure do_open(var f;p:pchar;flags:longint);
  1125. {
  1126. filerec and textrec have both handle and mode as the first items so
  1127. they could use the same routine for opening/creating.
  1128. when (flags and $10) the file will be append
  1129. when (flags and $100) the file will be truncate/rewritten
  1130. when (flags and $1000) there is no check for close (needed for textfiles)
  1131. }
  1132. var
  1133. i,j : longint;
  1134. oflags: longint;
  1135. path : string;
  1136. buffer : array[0..255] of char;
  1137. index : integer;
  1138. s : string;
  1139. begin
  1140. path:=strpas(p);
  1141. for index:=1 to length(path) do
  1142. if path[index]='\' then path[index]:='/';
  1143. { remove any dot characters and replace by their current }
  1144. { directory equivalent. }
  1145. if pos('../',path) = 1 then
  1146. { look for parent directory }
  1147. Begin
  1148. delete(path,1,3);
  1149. getdir(0,s);
  1150. j:=length(s);
  1151. while (s[j] <> '/') AND (s[j] <> ':') AND (j > 0 ) do
  1152. dec(j);
  1153. if j > 0 then
  1154. s:=copy(s,1,j);
  1155. path:=s+path;
  1156. end
  1157. else
  1158. if pos('./',path) = 1 then
  1159. { look for current directory }
  1160. Begin
  1161. delete(path,1,2);
  1162. getdir(0,s);
  1163. if (s[length(s)] <> '/') and (s[length(s)] <> ':') then
  1164. s:=s+'/';
  1165. path:=s+path;
  1166. end;
  1167. move(path[1],buffer,length(path));
  1168. buffer[length(path)]:=#0;
  1169. { close first if opened }
  1170. if ((flags and $1000)=0) then
  1171. begin
  1172. case filerec(f).mode of
  1173. fminput,fmoutput,fminout : Do_Close(filerec(f).handle);
  1174. fmclosed : ;
  1175. else
  1176. begin
  1177. inoutres:=102; {not assigned}
  1178. exit;
  1179. end;
  1180. end;
  1181. end;
  1182. { reset file handle }
  1183. filerec(f).handle:=UnusedHandle;
  1184. { convert filemode to filerec modes }
  1185. { READ/WRITE on existing file }
  1186. { RESET/APPEND }
  1187. oflags := 1005;
  1188. case (flags and 3) of
  1189. 0 : begin
  1190. filerec(f).mode:=fminput;
  1191. end;
  1192. 1 : filerec(f).mode:=fmoutput;
  1193. 2 : filerec(f).mode:=fminout;
  1194. end;
  1195. { READ/WRITE mode, create file in all cases }
  1196. { REWRITE }
  1197. if (flags and $100)<>0 then
  1198. begin
  1199. filerec(f).mode:=fmoutput;
  1200. oflags := 1006;
  1201. end
  1202. else
  1203. { READ/WRITE mode on existing file }
  1204. { APPEND }
  1205. if (flags and $10)<>0 then
  1206. begin
  1207. filerec(f).mode:=fmoutput;
  1208. oflags := 1005;
  1209. end;
  1210. { empty name is special }
  1211. if p[0]=#0 then
  1212. begin
  1213. case filerec(f).mode of
  1214. fminput : filerec(f).handle:=StdInputHandle;
  1215. fmappend,
  1216. fmoutput : begin
  1217. filerec(f).handle:=StdOutputHandle;
  1218. filerec(f).mode:=fmoutput; {fool fmappend}
  1219. end;
  1220. end;
  1221. exit;
  1222. end;
  1223. asm
  1224. move.l a6,d6 { save a6 }
  1225. move.l d2,-(sp)
  1226. lea buffer,a0
  1227. move.l a0,d1
  1228. move.l oflags,d2 { MODE_READWRITE }
  1229. move.l _DOSBase,a6
  1230. jsr _LVOOpen(a6)
  1231. tst.l d0
  1232. bne @noopenerror { on zero an error occured }
  1233. jsr _LVOIoErr(a6)
  1234. move.w d0,errno
  1235. bra @openend
  1236. @noopenerror:
  1237. move.l (sp)+,d2
  1238. move.l d6,a6 { restore a6 }
  1239. move.l d0,i { we need the base pointer to access this variable }
  1240. bra @end
  1241. @openend:
  1242. move.l d6,a6 { restore a6 }
  1243. move.l (sp)+,d2
  1244. @end:
  1245. end;
  1246. (* if Errno = 0 then*)
  1247. { No error, add file handle to linked list }
  1248. { this must be checked before the call to }
  1249. { Error2InIOut since it resets Errno to 0 }
  1250. (* AddToList(FileList,i);*)
  1251. If Errno <> 0 then
  1252. Error2InOut;
  1253. filerec(f).handle:=i;
  1254. if (flags and $10)<>0 then
  1255. do_seekend(filerec(f).handle);
  1256. end;
  1257. {*****************************************************************************
  1258. UnTyped File Handling
  1259. *****************************************************************************}
  1260. {$i file.inc}
  1261. {*****************************************************************************
  1262. Typed File Handling
  1263. *****************************************************************************}
  1264. {$i typefile.inc}
  1265. {*****************************************************************************
  1266. Text File Handling
  1267. *****************************************************************************}
  1268. {$i text.inc}
  1269. {*****************************************************************************
  1270. Directory Handling
  1271. *****************************************************************************}
  1272. procedure mkdir(const s : string);[IOCheck];
  1273. var
  1274. buffer : array[0..255] of char;
  1275. j: Integer;
  1276. temp : string;
  1277. begin
  1278. { We must check the Ctrl-C before IOChecking of course! }
  1279. if (Setsignal(0,0) AND SIGBREAKF_CTRL_C) <> 0 then
  1280. Begin
  1281. { Clear CTRL-C signal }
  1282. SetSignal(0,SIGBREAKF_CTRL_C);
  1283. Halt(CTRL_C);
  1284. end;
  1285. If InOutRes <> 0 then exit;
  1286. temp:=s;
  1287. for j:=1 to length(temp) do
  1288. if temp[j] = '\' then temp[j] := '/';
  1289. move(temp[1],buffer,length(temp));
  1290. buffer[length(temp)]:=#0;
  1291. asm
  1292. move.l a6,d6
  1293. { we must load the parameters BEFORE setting up the }
  1294. { OS call with a6 }
  1295. lea buffer,a0
  1296. move.l a0,d1
  1297. move.l _DosBase,a6
  1298. jsr _LVOCreateDir(a6)
  1299. tst.l d0
  1300. bne @noerror
  1301. jsr _LVOIoErr(a6)
  1302. move.w d0,errno
  1303. bra @end
  1304. @noerror:
  1305. { Now we must unlock the directory }
  1306. { d0 = lock returned by create dir }
  1307. move.l d0,d1
  1308. jsr _LVOUnlock(a6)
  1309. @end:
  1310. { restore base pointer }
  1311. move.l d6,a6
  1312. end;
  1313. If errno <> 0 then
  1314. Error2InOut;
  1315. end;
  1316. procedure rmdir(const s : string);[IOCheck];
  1317. var
  1318. buffer : array[0..255] of char;
  1319. j : Integer;
  1320. temp : string;
  1321. begin
  1322. { We must check the Ctrl-C before IOChecking of course! }
  1323. if (Setsignal(0,0) AND SIGBREAKF_CTRL_C) <> 0 then
  1324. Begin
  1325. { Clear CTRL-C signal }
  1326. SetSignal(0,SIGBREAKF_CTRL_C);
  1327. Halt(CTRL_C);
  1328. end;
  1329. If InOutRes <> 0 then exit;
  1330. temp:=s;
  1331. for j:=1 to length(temp) do
  1332. if temp[j] = '\' then temp[j] := '/';
  1333. move(temp[1],buffer,length(temp));
  1334. buffer[length(temp)]:=#0;
  1335. do_erase(buffer);
  1336. end;
  1337. procedure chdir(const s : string);[IOCheck];
  1338. var
  1339. buffer : array[0..255] of char;
  1340. alock : longint;
  1341. FIB :pFileInfoBlock;
  1342. j: integer;
  1343. temp : string;
  1344. begin
  1345. if (Setsignal(0,0) AND SIGBREAKF_CTRL_C) <> 0 then
  1346. Begin
  1347. { Clear CTRL-C signal }
  1348. SetSignal(0,SIGBREAKF_CTRL_C);
  1349. Halt(CTRL_C);
  1350. end;
  1351. If InOutRes <> 0 then exit;
  1352. temp:=s;
  1353. for j:=1 to length(temp) do
  1354. if temp[j] = '\' then temp[j] := '/';
  1355. { Return parent directory }
  1356. if s = '..' then
  1357. Begin
  1358. getdir(0,temp);
  1359. j:=length(temp);
  1360. { Look through the previous paths }
  1361. while (temp[j] <> '/') AND (temp[j] <> ':') AND (j > 0 ) do
  1362. dec(j);
  1363. if j > 0 then
  1364. temp:=copy(temp,1,j);
  1365. end;
  1366. alock := 0;
  1367. fib:=nil;
  1368. new(fib);
  1369. move(temp[1],buffer,length(temp));
  1370. buffer[length(temp)]:=#0;
  1371. { Changing the directory is a pretty complicated affair }
  1372. { 1) Obtain a lock on the directory }
  1373. { 2) CurrentDir the lock }
  1374. asm
  1375. lea buffer,a0
  1376. move.l a0,d1 { pointer to buffer in d1 }
  1377. move.l d2,-(sp) { save d2 register }
  1378. move.l #-2,d2 { ACCESS_READ lock }
  1379. move.l a6,d6 { Save base pointer }
  1380. move.l _DosBase,a6
  1381. jsr _LVOLock(a6){ Lock the directory }
  1382. move.l (sp)+,d2 { Restore d2 register }
  1383. tst.l d0 { zero = error! }
  1384. bne @noerror
  1385. jsr _LVOIoErr(a6)
  1386. move.w d0,errno
  1387. move.l d6,a6 { reset base pointer }
  1388. bra @End
  1389. @noerror:
  1390. move.l d6,a6 { reset base pointer }
  1391. move.l d0,alock { save the lock }
  1392. @End:
  1393. end;
  1394. If errno <> 0 then
  1395. Begin
  1396. Error2InOut;
  1397. exit;
  1398. end;
  1399. if (Examine(alock, fib^) = TRUE) AND (fib^.fib_DirEntryType > 0) then
  1400. Begin
  1401. alock := CurrentDir(alock);
  1402. if OrigDir = 0 then
  1403. Begin
  1404. OrigDir := alock;
  1405. alock := 0;
  1406. end;
  1407. end;
  1408. if alock <> 0 then
  1409. Unlock(alock);
  1410. if assigned(fib) then dispose(fib);
  1411. end;
  1412. Procedure GetCwd(var path: string);
  1413. var
  1414. lock: longint;
  1415. fib: PfileInfoBlock;
  1416. len : integer;
  1417. newlock : longint;
  1418. elen : integer;
  1419. Process : PProcess;
  1420. Begin
  1421. len := 0;
  1422. path := '';
  1423. fib := nil;
  1424. { By using a pointer instead of a local variable}
  1425. { we are assured that the pointer is aligned on }
  1426. { a dword boundary. }
  1427. new(fib);
  1428. Process := FindTask(nil);
  1429. if (process^.pr_Task.tc_Node.ln_Type = NT_TASK) then
  1430. Begin
  1431. path:='';
  1432. exit;
  1433. end;
  1434. lock := DupLock(process^.pr_CurrentDir);
  1435. if (Lock = 0) then
  1436. Begin
  1437. path:='';
  1438. exit;
  1439. end;
  1440. While (lock <> 0) and (Examine(lock,FIB^) = TRUE) do
  1441. Begin
  1442. elen := strlen(fib^.fib_FileName);
  1443. if (len + elen + 2 > 255) then
  1444. break;
  1445. newlock := ParentDir(lock);
  1446. if (len <> 0) then
  1447. Begin
  1448. if (newlock <> 0) then
  1449. path:='/'+path
  1450. else
  1451. path:=':'+path;
  1452. path:=strpas(fib^.fib_FileName)+path;
  1453. Inc(len);
  1454. end
  1455. else
  1456. Begin
  1457. path:=strpas(fib^.fib_Filename);
  1458. if (newlock = 0) then
  1459. path:=path+':';
  1460. end;
  1461. len := len + elen;
  1462. UnLock(lock);
  1463. lock := newlock;
  1464. end;
  1465. if (lock <> 0) then
  1466. Begin
  1467. UnLock(lock);
  1468. path := '';
  1469. end;
  1470. if assigned(fib) then dispose(fib);
  1471. end;
  1472. procedure getdir(drivenr : byte;var dir : string);
  1473. begin
  1474. if (Setsignal(0,0) AND SIGBREAKF_CTRL_C) <> 0 then
  1475. Begin
  1476. { Clear CTRL-C signal }
  1477. SetSignal(0,SIGBREAKF_CTRL_C);
  1478. Halt(CTRL_C);
  1479. end;
  1480. GetCwd(dir);
  1481. If errno <> 0 then
  1482. Error2InOut;
  1483. end;
  1484. {*****************************************************************************
  1485. SystemUnit Initialization
  1486. *****************************************************************************}
  1487. Procedure Startup; Assembler;
  1488. asm
  1489. move.l a6,d6 { save a6 }
  1490. move.l (4),a6 { get ExecBase pointer }
  1491. move.l a6,_ExecBase
  1492. suba.l a1,a1
  1493. jsr _LVOFindTask(a6)
  1494. move.l d0,a0
  1495. { Check the stack value }
  1496. { are we running from a CLI? }
  1497. tst.l 172(a0) { 172 = pr_CLI }
  1498. bne @fromCLI
  1499. { we do not support Workbench yet .. }
  1500. move.l d6,a6 { restore a6 }
  1501. move.l #1,d0
  1502. jsr HALT_ERROR
  1503. @fromCLI:
  1504. { Open the following libraries: }
  1505. { Intuition.library }
  1506. { dos.library }
  1507. moveq.l #0,d0
  1508. move.l intuitionname,a1 { directly since it is a pchar }
  1509. jsr _LVOOpenLibrary(a6)
  1510. move.l d0,_IntuitionBase
  1511. beq @exitprg
  1512. moveq.l #0,d0
  1513. move.l utilityname,a1 { directly since it is a pchar }
  1514. jsr _LVOOpenLibrary(a6)
  1515. move.l d0,_UtilityBase
  1516. beq @exitprg
  1517. moveq.l #0,d0
  1518. move.l dosname,a1 { directly since it is a pchar }
  1519. jsr _LVOOpenLibrary(a6)
  1520. move.l d0,_DOSBase
  1521. beq @exitprg
  1522. { Find standard input and output }
  1523. { for CLI }
  1524. @OpenFiles:
  1525. move.l _DOSBase,a6
  1526. jsr _LVOInput(a6) { get standard in }
  1527. move.l d0, StdInputHandle { save standard Input handle }
  1528. { move.l d0,d1 }{ set up for next call }
  1529. { jsr _LVOIsInteractive(a6)}{ is it interactive? }
  1530. { move.l #_Input,a0 }{ get file record again }
  1531. { move.b d0,INTERACTIVE(a0) }{ set flag }
  1532. { beq StdInNotInteractive }{ skip this if not interactive }
  1533. { move.l BUFFER(a0),a1 }{ get buffer address }
  1534. { add.l #1,a1 }{ make end one byte further on }
  1535. { move.l a1,MAX(a0) }{ set buffer size }
  1536. { move.l a1,CURRENT(a0) }{ will need a read }
  1537. bra @OpenStdOutput
  1538. @StdInNotInteractive
  1539. { jsr _p%FillBuffer } { fill the buffer }
  1540. @OpenStdOutput
  1541. jsr _LVOOutput(a6) { get ouput file handle }
  1542. move.l d0,StdOutputHandle { get file record }
  1543. bra @startupend
  1544. { move.l d0,d1 } { set up for call }
  1545. { jsr _LVOIsInteractive(a6) } { is it interactive? }
  1546. { move.l #_Output,a0 } { get file record }
  1547. { move.b d0,INTERACTIVE(a0)} { set flag }
  1548. @exitprg:
  1549. move.l d6,a6 { restore a6 }
  1550. move.l #219,d0
  1551. jsr HALT_ERROR
  1552. @startupend:
  1553. move.l d6,a6 { restore a6 }
  1554. end;
  1555. begin
  1556. errno:= 0;
  1557. FromHalt := FALSE;
  1558. { Initial state is on -- in case of RunErrors before the i/o handles are }
  1559. { ok. }
  1560. Initial:=TRUE;
  1561. { Initialize ExitProc }
  1562. ExitProc:=Nil;
  1563. Startup;
  1564. { to test stack depth }
  1565. loweststack:=maxlongint;
  1566. { Setup heap }
  1567. InitHeap;
  1568. { Setup stdin, stdout and stderr }
  1569. OpenStdIO(Input,fmInput,StdInputHandle);
  1570. OpenStdIO(Output,fmOutput,StdOutputHandle);
  1571. { The Amiga does not seem to have a StdError }
  1572. { handle, therefore make the StdError handle }
  1573. { equal to the StdOutputHandle. }
  1574. StdErrorHandle := StdOutputHandle;
  1575. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  1576. { Now Handles and function handlers are setup }
  1577. { correctly. }
  1578. Initial:=FALSE;
  1579. { Reset IO Error }
  1580. InOutRes:=0;
  1581. { Startup }
  1582. { Only AmigaOS v2.04 or greater is supported }
  1583. If KickVersion < 36 then
  1584. Begin
  1585. WriteLn('v36 or greater of Kickstart required.');
  1586. Halt(1);
  1587. end;
  1588. argc:=GetParamCount(args);
  1589. OrigDir := 0;
  1590. FileList := nil;
  1591. old_Exit:=exitproc;
  1592. Exitproc:=@ExitCall;
  1593. end.
  1594. {
  1595. $Log$
  1596. Revision 1.9 1998-08-17 12:34:22 carl
  1597. * chdir accepts .. characters
  1598. + added ctrl-c checking
  1599. + implemented sbrk
  1600. * exit code was never called if no error was found on exit!
  1601. * register was not saved in do_open
  1602. Revision 1.8 1998/07/13 12:32:18 carl
  1603. * do_truncate works, some cleanup
  1604. Revision 1.6 1998/07/02 12:37:52 carl
  1605. * IOCheck for chdir,rmdir and mkdir as in TP
  1606. Revision 1.5 1998/07/01 14:30:56 carl
  1607. * forgot that includes are case sensitive
  1608. Revision 1.4 1998/07/01 14:13:50 carl
  1609. * do_open bugfix
  1610. * correct conversion of Amiga error codes to TP error codes
  1611. * InoutRes word bugfix
  1612. * parameter counting fixed
  1613. * new stack checking implemented
  1614. + IOCheck for chdir,rmdir,getdir and rmdir
  1615. * do_filepos was wrong
  1616. + chdir correctly implemented
  1617. * getdir correctly implemented
  1618. Revision 1.1.1.1 1998/03/25 11:18:47 root
  1619. * Restored version
  1620. Revision 1.14 1998/03/21 04:20:09 carl
  1621. * correct ExecBase pointer (from Nils Sjoholm)
  1622. * correct OpenLibrary vector (from Nils Sjoholm)
  1623. Revision 1.13 1998/03/14 21:34:32 carl
  1624. * forgot to save a6 in Startup routine
  1625. Revision 1.12 1998/02/24 21:19:42 carl
  1626. *** empty log message ***
  1627. Revision 1.11 1998/02/23 02:22:49 carl
  1628. * bugfix if linking problems
  1629. Revision 1.9 1998/02/06 16:34:32 carl
  1630. + do_open is now standard with other platforms
  1631. Revision 1.8 1998/02/02 15:01:45 carl
  1632. * fixed bug with opening library versions (from Nils Sjoholm)
  1633. Revision 1.7 1998/01/31 19:35:19 carl
  1634. + added opening of utility.library
  1635. Revision 1.6 1998/01/29 23:20:54 peter
  1636. - Removed Backslash convert
  1637. Revision 1.5 1998/01/27 10:55:04 peter
  1638. * Amiga uses / not \, so change AllowSlash -> AllowBackSlash
  1639. Revision 1.4 1998/01/25 21:53:20 peter
  1640. + Universal Handles support for StdIn/StdOut/StdErr
  1641. * Updated layout of sysamiga.pas
  1642. Revision 1.3 1998/01/24 21:09:53 carl
  1643. + added missing input/output function pointers
  1644. Revision 1.2 1998/01/24 14:08:25 carl
  1645. * RunError 217 --> RunError 219 (cannot open lib)
  1646. + Standard Handle names implemented
  1647. Revision 1.1 1998/01/24 05:12:15 carl
  1648. + initial revision, some stuff still missing though.
  1649. (and as you might imagine ... untested :))
  1650. }