system.pp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 2002 by Olle Raab
  5. FreePascal system unit for MacOS.
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. unit System;
  13. interface
  14. {If MAC_SYS_RUNABLE is defined, this file can be included in a
  15. runnable program, but it then lacks lot of features. If not defined
  16. it tries to be faithful to a real system.pp, but it may not be
  17. able to assemble and link. The switch is only temporary, and only for
  18. use when system.pp is developed.}
  19. {$Y-}
  20. {$ifdef MAC_SYS_RUNABLE}
  21. type
  22. integer = -32768 .. 32767;
  23. byte =0..255;
  24. shortint=-128..127;
  25. word=0..65535;
  26. longint=+(-$7FFFFFFF-1)..$7FFFFFFF;
  27. pchar=^char;
  28. {$else}
  29. {At the moment we do not support threadvars}
  30. {$undef HASTHREADVAR}
  31. {$I systemh.inc}
  32. {$I heaph.inc}
  33. {Platform specific information}
  34. const
  35. LineEnding = #13;
  36. LFNSupport = true;
  37. DirectorySeparator = ':';
  38. DriveSeparator = ':';
  39. PathSeparator = ';';
  40. FileNameCaseSensitive = false;
  41. const
  42. UnusedHandle = 0;
  43. StdInputHandle = 0;
  44. StdOutputHandle = 0;
  45. StdErrorHandle = 0;
  46. sLineBreak : string[1] = LineEnding;
  47. DefaultTextLineBreakStyle : TTextLineBreakStyle = tlbsCR;
  48. var
  49. argc : longint;
  50. argv : ppchar;
  51. envp : ppchar;
  52. {$endif}
  53. implementation
  54. {TODO: Perhaps the System unit should check the MacOS version to
  55. ensure it is a supported version. }
  56. {Below is some MacOS API routines needed for internal use.
  57. Note, because the System unit is the most low level, it should not
  58. depend on any other units, and in particcular not the MacOS unit.
  59. Note: Types like Mac_XXX corresponds to the type XXX defined
  60. in MacOS Universal Headers. The prefix is to avoid name clashes
  61. with FPC types.}
  62. type
  63. SignedByte = shortint;
  64. OSErr = Integer;
  65. OSType = Longint;
  66. Mac_Ptr = pointer;
  67. Mac_Handle = ^Mac_Ptr;
  68. Str31 = string[31];
  69. Str32 = string[32];
  70. Str63 = string[63];
  71. FSSpec = record
  72. vRefNum: Integer;
  73. parID: Longint;
  74. name: Str63;
  75. end;
  76. FSSpecPtr = ^FSSpec;
  77. AliasHandle = Mac_Handle;
  78. ScriptCode = Integer;
  79. const
  80. noErr = 0;
  81. fnfErr = -43; //File not found error
  82. fsFromStart = 1;
  83. fsFromLEOF = 2;
  84. function NewPtr(logicalSize: Longint): Mac_Ptr ;
  85. external 'InterfaceLib';
  86. procedure DisposeHandle(hdl: Mac_Handle);
  87. external 'InterfaceLib';
  88. procedure Debugger;
  89. external 'InterfaceLib';
  90. procedure ExitToShell;
  91. external 'InterfaceLib';
  92. function FSpOpenDF(spec: FSSpec; permission: SignedByte;
  93. var refNum: Integer): OSErr;
  94. external 'InterfaceLib';
  95. function FSpCreate(spec: FSSpec; creator, fileType: OSType;
  96. scriptTag: ScriptCode): OSErr;
  97. external 'InterfaceLib';
  98. function FSClose(refNum: Integer): OSErr;
  99. external 'InterfaceLib';
  100. function FSRead(refNum: Integer; var count: Longint; buffPtr: Mac_Ptr): OSErr;
  101. external 'InterfaceLib';
  102. function FSWrite(refNum: Integer; var count: Longint; buffPtr: Mac_Ptr): OSErr;
  103. external 'InterfaceLib';
  104. function GetFPos(refNum: Integer; var filePos: Longint): OSErr;
  105. external 'InterfaceLib';
  106. function SetFPos(refNum: Integer; posMode: Integer; posOff: Longint): OSErr;
  107. external 'InterfaceLib';
  108. function GetEOF(refNum: Integer; var logEOF: Longint): OSErr;
  109. external 'InterfaceLib';
  110. function SetEOF(refNum: Integer; logEOF: Longint): OSErr;
  111. external 'InterfaceLib';
  112. function NewAliasMinimalFromFullPath(fullPathLength: Integer;
  113. fullPath: Mac_Ptr; zoneName: Str32; serverName: Str31;
  114. var alias: AliasHandle):OSErr;
  115. external 'InterfaceLib';
  116. function ResolveAlias(fromFile: FSSpecPtr; alias: AliasHandle;
  117. var target: FSSpec; var wasChanged: Boolean):OSErr;
  118. external 'InterfaceLib';
  119. {$ifdef MAC_SYS_RUNABLE}
  120. procedure do_exit;[public,alias:'FPC_DO_EXIT'];
  121. begin
  122. end;
  123. procedure fpc_initializeunits;[public,alias:'FPC_INITIALIZEUNITS'];
  124. begin
  125. end;
  126. {$else}
  127. {$I system.inc}
  128. {*********************** ??????? *************}
  129. procedure SysInitStdIO;
  130. begin
  131. end;
  132. {*****************************************************************************}
  133. procedure setup_arguments;
  134. begin
  135. end;
  136. procedure setup_environment;
  137. begin
  138. end;
  139. {*****************************************************************************
  140. System Dependent Exit code
  141. *****************************************************************************}
  142. Procedure system_exit;
  143. begin
  144. ExitToShell;
  145. end;
  146. {*****************************************************************************
  147. ParamStr/Randomize
  148. *****************************************************************************}
  149. { number of args }
  150. function paramcount : longint;
  151. begin
  152. {paramcount := argc - 1;}
  153. paramcount:=0;
  154. end;
  155. { argument number l }
  156. function paramstr(l : longint) : string;
  157. begin
  158. {if (l>=0) and (l+1<=argc) then
  159. paramstr:=strpas(argv[l])
  160. else}
  161. paramstr:='';
  162. end;
  163. { set randseed to a new pseudo random value }
  164. procedure randomize;
  165. begin
  166. {regs.realeax:=$2c00;
  167. sysrealintr($21,regs);
  168. hl:=regs.realedx and $ffff;
  169. randseed:=hl*$10000+ (regs.realecx and $ffff);}
  170. randseed:=0;
  171. end;
  172. {*****************************************************************************
  173. Heap Management
  174. *****************************************************************************}
  175. const
  176. theHeapSize = 300000; //TODO: Use heapsize set by user.
  177. var
  178. { Pointer to a block allocated with the MacOS Memory Manager, which
  179. is used as the FPC heap }
  180. theHeap: Mac_Ptr;
  181. { first address of heap }
  182. function getheapstart:pointer;
  183. begin
  184. getheapstart:= theHeap;
  185. end;
  186. { current length of heap }
  187. function getheapsize:longint;
  188. begin
  189. getheapsize:= theHeapSize ;
  190. end;
  191. { function to allocate size bytes more for the program }
  192. { must return the first address of new data space or -1 if fail }
  193. function Sbrk(size : longint):longint;
  194. begin
  195. Sbrk:=-1; //TODO: Allow heap increase.
  196. end;
  197. {$I heap.inc}
  198. {****************************************************************************
  199. Low level File Routines
  200. All these functions can set InOutRes on errors
  201. ****************************************************************************}
  202. { close a file from the handle value }
  203. procedure do_close(handle : longint);
  204. begin
  205. InOutRes:=1;
  206. if handle = UnusedHandle then exit;
  207. if FSClose(handle) = noErr then
  208. InOutRes:=0; //TODO: Is this right ?
  209. end;
  210. procedure do_erase(p : pchar);
  211. begin
  212. InOutRes:=1;
  213. end;
  214. procedure do_rename(p1,p2 : pchar);
  215. begin
  216. InOutRes:=1;
  217. end;
  218. function do_write(h,addr,len : longint) : longint;
  219. begin
  220. InOutRes:=1;
  221. if h = UnusedHandle then exit;
  222. if FSWrite(h, len, Mac_Ptr(addr)) = noErr then
  223. InOutRes:=0; //TODO: Is this right ?
  224. do_write:= len;
  225. end;
  226. function do_read(h,addr,len : longint) : longint;
  227. begin
  228. InOutRes:=1;
  229. if h = UnusedHandle then exit;
  230. if FSread(h, len, Mac_Ptr(addr)) = noErr then
  231. InOutRes:=0; //TODO: Is this right ?
  232. do_read:= len;
  233. end;
  234. function do_filepos(handle : longint) : longint;
  235. var
  236. pos: Longint;
  237. begin
  238. InOutRes:=1;
  239. if handle = UnusedHandle then exit;
  240. if GetFPos(handle, pos) = noErr then
  241. InOutRes:=0; //TODO: Is this right ?
  242. do_filepos:= pos;
  243. end;
  244. procedure do_seek(handle,pos : longint);
  245. begin
  246. InOutRes:=1;
  247. if handle = UnusedHandle then exit;
  248. if SetFPos(handle, fsFromStart, pos) = noErr then
  249. InOutRes:=0; //TODO: Is this right ?
  250. end;
  251. function do_seekend(handle:longint):longint;
  252. begin
  253. InOutRes:=1;
  254. if handle = UnusedHandle then exit;
  255. if SetFPos(handle, fsFromLEOF, 0) = noErr then
  256. InOutRes:=0; //TODO: Is this right ?
  257. end;
  258. function do_filesize(handle : longint) : longint;
  259. var
  260. pos: Longint;
  261. begin
  262. InOutRes:=1;
  263. if handle = UnusedHandle then exit;
  264. if GetEOF(handle, pos) = noErr then
  265. InOutRes:=0; //TODO: Is this right ?
  266. do_filesize:= pos;
  267. end;
  268. { truncate at a given position }
  269. procedure do_truncate (handle,pos:longint);
  270. begin
  271. InOutRes:=1;
  272. do_seek(handle,pos); //TODO: Is this needed (Does the user anticipate the filemarker is at the end?)
  273. if SetEOF(handle, pos) = noErr then
  274. InOutRes:=0; //TODO: Is this right ?
  275. end;
  276. function FSpLocationFromFullPath(fullPathLength: Integer;
  277. fullPath: Mac_Ptr; var spec: FSSpec ):OSErr;
  278. var
  279. alias: AliasHandle;
  280. res: OSErr;
  281. wasChanged: Boolean;
  282. nullString: Str32;
  283. begin
  284. nullString:= '';
  285. res:= NewAliasMinimalFromFullPath(fullPathLength,
  286. fullPath, nullString, nullString, alias);
  287. if res = noErr then
  288. begin
  289. res:= ResolveAlias(nil, alias, spec, wasChanged);
  290. DisposeHandle(Mac_Handle(alias));
  291. end;
  292. FSpLocationFromFullPath:= res;
  293. end;
  294. procedure do_open(var f;p:pchar;flags:longint);
  295. {
  296. filerec and textrec have both handle and mode as the first items so
  297. they could use the same routine for opening/creating.
  298. when (flags and $10) the file will be append
  299. when (flags and $100) the file will be truncate/rewritten
  300. when (flags and $1000) there is no check for close (needed for textfiles)
  301. }
  302. var
  303. spec: FSSpec;
  304. creator, fileType: OSType;
  305. scriptTag: ScriptCode;
  306. refNum: Integer;
  307. res: OSErr;
  308. const
  309. fsCurPerm = 0;
  310. smSystemScript = -1;
  311. begin
  312. InOutRes:=1;
  313. //creator:= $522A6368; {'MPS ' -- MPW}
  314. //creator:= $74747874; {'ttxt'}
  315. creator:= $522A6368; {'R*ch' -- BBEdit}
  316. fileType:= $54455854; {'TEXT'}
  317. { reset file handle }
  318. filerec(f).handle:=UnusedHandle;
  319. res:= FSpLocationFromFullPath(StrLen(p), p, spec);
  320. if (res = noErr) or (res = fnfErr) then
  321. begin
  322. if FSpCreate(spec, creator, fileType, smSystemScript) = noErr then
  323. ;
  324. if FSpOpenDF(spec, fsCurPerm, refNum) = noErr then
  325. begin
  326. filerec(f).handle:= refNum;
  327. InOutRes:=0;
  328. end;
  329. end;
  330. if (filerec(f).handle=UnusedHandle) then
  331. begin
  332. //errno:=GetLastError;
  333. //Errno2InoutRes;
  334. end;
  335. end;
  336. function do_isdevice(handle:longint):boolean;
  337. begin
  338. do_isdevice:=false;
  339. end;
  340. {*****************************************************************************
  341. UnTyped File Handling
  342. *****************************************************************************}
  343. {$i file.inc}
  344. {*****************************************************************************
  345. Typed File Handling
  346. *****************************************************************************}
  347. {$i typefile.inc}
  348. {*****************************************************************************
  349. Text File Handling
  350. *****************************************************************************}
  351. { should we consider #26 as the end of a file ? }
  352. {?? $DEFINE EOF_CTRLZ}
  353. {$i text.inc}
  354. {*****************************************************************************
  355. Directory Handling
  356. *****************************************************************************}
  357. procedure mkdir(const s : string);[IOCheck];
  358. begin
  359. InOutRes:=1;
  360. end;
  361. procedure rmdir(const s : string);[IOCheck];
  362. begin
  363. InOutRes:=1;
  364. end;
  365. procedure chdir(const s : string);[IOCheck];
  366. begin
  367. InOutRes:=1;
  368. end;
  369. procedure GetDir (DriveNr: byte; var Dir: ShortString);
  370. begin
  371. InOutRes := 1;
  372. end;
  373. {*****************************************************************************
  374. SystemUnit Initialization
  375. *****************************************************************************}
  376. Begin
  377. if false then //To save it from the dead code stripper
  378. Debugger; //Included only to make it available for debugging
  379. { To be set if this is a GUI or console application }
  380. IsConsole := TRUE;
  381. { To be set if this is a library and not a program }
  382. IsLibrary := FALSE;
  383. StackBottom := SPtr - StackLength;
  384. ExitCode := 0;
  385. { Setup heap }
  386. theHeap:= NewPtr(theHeapSize);
  387. InitHeap;
  388. { Setup stdin, stdout and stderr }
  389. (* OpenStdIO(Input,fmInput,StdInputHandle);
  390. OpenStdIO(Output,fmOutput,StdOutputHandle);
  391. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  392. OpenStdIO(StdErr,fmOutput,StdErrorHandle);*)
  393. { Setup environment and arguments }
  394. Setup_Environment;
  395. Setup_Arguments;
  396. { Reset IO Error }
  397. InOutRes:=0;
  398. {$endif}
  399. End.
  400. {
  401. $Log$
  402. Revision 1.5 2003-01-13 17:18:55 olle
  403. + added support for rudimentary file handling
  404. Revision 1.4 2002/11/28 10:58:02 olle
  405. + added support for rudimentary heap
  406. Revision 1.3 2002/10/23 15:29:09 olle
  407. + added switch MAC_SYS_RUNABLE
  408. + added include of system.h etc
  409. + added standard globals
  410. + added dummy hook procedures
  411. Revision 1.2 2002/10/10 19:44:05 florian
  412. * changes from Olle to compile/link a simple program
  413. Revision 1.1 2002/10/02 21:34:31 florian
  414. * first dummy implementation
  415. }