2
0

system.pp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2002-2004 by Olle Raab
  4. FreePascal system unit for MacOS.
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. unit System;
  12. interface
  13. { include system-independent routine headers }
  14. {$I systemh.inc}
  15. const
  16. LineEnding = #13;
  17. LFNSupport = true;
  18. DirectorySeparator = ':';
  19. DriveSeparator = ':';
  20. ExtensionSeparator = '.';
  21. PathSeparator = ','; {Is used in MPW and OzTeX}
  22. AllowDirectorySeparators : set of char = [':'];
  23. AllowDriveSeparators : set of char = [':'];
  24. FileNameCaseSensitive = false;
  25. FileNameCasePreserving = true;
  26. CtrlZMarksEOF: boolean = false; (* #26 not considered as end of file *)
  27. maxExitCode = 65535;
  28. MaxPathLen = 256;
  29. AllFilesMask = '*';
  30. const
  31. { Default filehandles }
  32. UnusedHandle : Longint = -1;
  33. StdInputHandle : Longint = 0;
  34. StdOutputHandle : Longint = 1;
  35. StdErrorHandle : Longint = 2;
  36. sLineBreak = LineEnding;
  37. DefaultTextLineBreakStyle : TTextLineBreakStyle = tlbsCR;
  38. var
  39. argc : longint;
  40. argv : ppchar;
  41. envp : ppchar;
  42. {*********************************}
  43. {** MacOS specific functions **}
  44. {*********************************}
  45. {To be called at regular intervals, for lenghty tasks.
  46. Yield might give time for other tasks to run under the cooperative
  47. multitasked macos. For an MPW Tool, it also spinns the cursor.}
  48. procedure Yield;
  49. {To set mac file type and creator codes, to be used for files created
  50. by the FPC runtime library. They must be exactly 4 chars long.}
  51. procedure SetDefaultMacOSFiletype(ftype: ShortString);
  52. procedure SetDefaultMacOSCreator(creator: ShortString);
  53. var
  54. {Whether unix and dos style paths should be translated. Default false}
  55. pathTranslation: Boolean;
  56. {*********************************}
  57. {** Available features on macos **}
  58. {*********************************}
  59. var
  60. macosHasGestalt: Boolean;
  61. macosHasWaitNextEvent: Boolean;
  62. macosHasColorQD: Boolean;
  63. macosHasFPU: Boolean;
  64. macosSystemVersion: Integer;
  65. macosHasSysDebugger: Boolean = false;
  66. macosHasCFM: Boolean;
  67. macosHasAppleEvents: Boolean;
  68. macosHasAliasMgr: Boolean;
  69. macosHasFSSpec: Boolean;
  70. macosHasFindFolder: Boolean;
  71. macosHasScriptMgr: Boolean;
  72. macosNrOfScriptsInstalled: Integer;
  73. macosHasAppearance: Boolean;
  74. macosHasAppearance101: Boolean;
  75. macosHasAppearance11: Boolean;
  76. macosBootVolumeVRefNum: Integer;
  77. macosBootVolumeName: String[31];
  78. {
  79. MacOS paths
  80. ===========
  81. MacOS directory separator is a colon ":" which is the only character not
  82. allowed in filenames.
  83. A path containing no colon or which begins with a colon is a partial path.
  84. E g ":kalle:petter" ":kalle" "kalle"
  85. All other paths are full (absolute) paths. E g "HD:kalle:" "HD:"
  86. When generating paths, one is safe is one ensures that all partial paths
  87. begins with a colon, and all full paths ends with a colon.
  88. In full paths the first name (e g HD above) is the name of a mounted volume.
  89. These names are not unique, because, for instance, two diskettes with the
  90. same names could be inserted. This means that paths on MacOS is not
  91. waterproof. In case of equal names the first volume found will do.
  92. Two colons "::" are the relative path to the parent. Three is to the
  93. grandparent etc.
  94. }
  95. implementation
  96. {
  97. About the implementation
  98. ========================
  99. A MacOS application is assembled and linked by MPW (Macintosh
  100. Programmers Workshop), which nowadays is free to use. For info
  101. and download of MPW and MacOS api, see www.apple.com
  102. It can be linked to either a graphical user interface application,
  103. a standalone text only application (using SIOW) or
  104. to an MPW tool, this is entirely controlled by the linking step.
  105. It requires system 7 and CFM, which is always the case for PowerPC.
  106. If a m68k version would be implemented, it would save a lot
  107. of efforts if it also uses CFM. This System.pp should, with
  108. minor modifications, probably work with m68k.
  109. Initial working directory is the directory of the application,
  110. or for an MPWTool, the working directory as set by the
  111. Directory command in MPW.
  112. Note about working directory. There is a facility in MacOS which
  113. manages a working directory for an application, initially set to
  114. the applications directory, or for an MPWTool, the tool's directory.
  115. However, this requires the application to have a unique application
  116. signature (creator code), to distinguish its working directory
  117. from working directories of other applications. Due to the fact
  118. that casual applications are anonymous in this sense (without an
  119. application signature), this facility will not work. Also, this
  120. working directory facility is not present in Carbon. Hence we
  121. will manage a working directory by our self.
  122. Deviations
  123. ==========
  124. In current implementation, working directory is stored as
  125. directory id. This means there is a possibility the user moves the
  126. working directory or a parent to it, while the application uses it.
  127. Then the path to the wd suddenly changes. This is AFAIK not in
  128. accordance with other OS's. Although this is a minor caveat,
  129. it is mentioned here. To overcome this the wd could be stored
  130. as a path instead, but this imposes translations from fullpath
  131. to directory ID each time the filesystem is accessed.
  132. The initial working directory for an MPWTool, as considered by
  133. FPC, is different from the MacOS working directory facility,
  134. see above.
  135. Possible improvements:
  136. =====================
  137. Perhaps handle readonly filesystems, as in sysunix.inc
  138. }
  139. {******** include system independent routines **********}
  140. {$I system.inc}
  141. {*****************************************************************************
  142. ParamStr/Randomize
  143. *****************************************************************************}
  144. { number of args }
  145. function paramcount : longint;
  146. begin
  147. paramcount := argc - 1;
  148. //paramcount:=0;
  149. end;
  150. { argument number l }
  151. function paramstr(l : longint) : string;
  152. begin
  153. if (l>=0) and (l+1<=argc) then
  154. paramstr:=strpas(argv[l])
  155. else
  156. paramstr:='';
  157. end;
  158. { set randseed to a new pseudo random value }
  159. procedure randomize;
  160. begin
  161. randseed:= Cardinal(TickCount);
  162. end;
  163. {*****************************************************************************
  164. SystemUnit Initialization
  165. *****************************************************************************}
  166. {$ifndef FPC_DARWIN_PASCALMAIN}
  167. procedure pascalmain; external name 'PASCALMAIN';
  168. {Main entry point in C style, needed to capture program parameters.
  169. For this to work, the system unit must be before the main program
  170. in the linking order.}
  171. procedure main(argcparam: Longint; argvparam: ppchar; envpparam: ppchar); cdecl; [public];
  172. {$else FPC_DARWIN_PASCALMAIN}
  173. procedure FPC_SYSTEMMAIN(argcparam: Longint; argvparam: ppchar; envpparam: ppchar); cdecl; [public];
  174. {$endif FPC_DARWIN_PASCALMAIN}
  175. begin
  176. argc:= argcparam;
  177. argv:= argvparam;
  178. envp:= envpparam;
  179. {$ifndef FPC_DARWIN_PASCALMAIN}
  180. pascalmain; {run the pascal main program}
  181. {$endif FPC_DARWIN_PASCALMAIN}
  182. end;
  183. procedure setup_arguments;
  184. begin
  185. {Nothing needs to be done here.}
  186. end;
  187. procedure setup_environment;
  188. begin
  189. end;
  190. { FindSysFolder returns the (real) vRefNum, and the DirID of the current
  191. system folder. It uses the Folder Manager if present, otherwise it falls
  192. back to SysEnvirons. It returns zero on success, otherwise a standard
  193. system error. }
  194. function FindSysFolder(var foundVRefNum: Integer; var foundDirID: Longint): OSErr;
  195. var
  196. gesResponse: Longint;
  197. envRec: SysEnvRec;
  198. myWDPB: WDPBRec;
  199. volName: String[34];
  200. err: OSErr;
  201. begin
  202. foundVRefNum := 0;
  203. foundDirID := 0;
  204. if macosHasGestalt
  205. and (Gestalt (FourCharCodeToLongword(gestaltFindFolderAttr), gesResponse) = noErr)
  206. and BitIsSet (gesResponse, gestaltFindFolderPresent) then
  207. begin { Does Folder Manager exist? }
  208. err := FindFolder (kOnSystemDisk, FourCharCodeToLongword(kSystemFolderType),
  209. kDontCreateFolder, foundVRefNum, foundDirID);
  210. end
  211. else
  212. begin
  213. { Gestalt can't give us the answer, so we resort to SysEnvirons }
  214. err := SysEnvirons (curSysEnvVers, envRec);
  215. if (err = noErr) then
  216. begin
  217. myWDPB.ioVRefNum := envRec.sysVRefNum;
  218. volName := '';
  219. myWDPB.ioNamePtr := @volName;
  220. myWDPB.ioWDIndex := 0;
  221. myWDPB.ioWDProcID := 0;
  222. err := PBGetWDInfoSync (@myWDPB);
  223. if (err = noErr) then
  224. begin
  225. foundVRefNum := myWDPB.ioWDVRefNum;
  226. foundDirID := myWDPB.ioWDDirID;
  227. end;
  228. end;
  229. end;
  230. FindSysFolder:= err;
  231. end;
  232. procedure InvestigateSystem;
  233. {$IFDEF CPUM68K}
  234. const
  235. _GestaltDispatch = $A0AD;
  236. _WaitNextEvent = $A860;
  237. _ScriptUtil = $A8B5;
  238. qdOffscreenTrap = $AB1D;
  239. {$ENDIF}
  240. var
  241. err: Integer;
  242. response: Longint;
  243. {$IFDEF CPUM68K}
  244. environs: SysEnvRec;
  245. {$ENDIF}
  246. {Vi rŠknar med att man kšr pŒ minst system 6.0.5. DŒ finns bŒde Gestalt och GDevice med.}
  247. {Enligt Change Histrory Šr MacOS 6.0.5 mera konsistent mellan maskinmodellerna Šn fšregŒende system}
  248. begin
  249. {$IFDEF CPUM68K}
  250. macosHasGestalt := TrapAvailable(_GestaltDispatch);
  251. {$ELSE}
  252. macosHasGestalt := true; {There is always Gestalt on PowerPC}
  253. {$ENDIF}
  254. if not macosHasGestalt then (* If we don't have Gestalt, then we can't have any System 7 features *)
  255. begin
  256. {$IFDEF CPUM68K}
  257. { Detta kan endast gŠlla pŒ en 68K maskin.}
  258. macosHasScriptMgr := TrapAvailable(_ScriptUtil);
  259. macosNrOfScriptsInstalled := 1; (* assume only Roman script, to start with *)
  260. err := SysEnvirons(1, environs);
  261. if err = noErr then
  262. begin
  263. if environs.machineType < 0 then { gammalt ROM}
  264. macosHasWaitNextEvent := FALSE
  265. else
  266. macosHasWaitNextEvent := TrapAvailable(_WaitNextEvent);
  267. macosHasColorQD := environs.hasColorQD;
  268. macosHasFPU := environs.hasFPU;
  269. macosSystemVersion := environs.systemVersion;
  270. end
  271. else
  272. begin
  273. macosHasWaitNextEvent := FALSE;
  274. macosHasColorQD := FALSE;
  275. macosHasFPU := FALSE;
  276. macosSystemVersion := 0;
  277. end;
  278. macosHasSysDebugger := (LongintPtr(MacJmp)^ <> 0);
  279. macosHasCFM := false;
  280. macosHasAppleEvents := false;
  281. macosHasAliasMgr := false;
  282. macosHasFSSpec := false;
  283. macosHasFindFolder := false;
  284. macosHasAppearance := false;
  285. macosHasAppearance101 := false;
  286. macosHasAppearance11 := false;
  287. {$IFDEF THINK_PASCAL}
  288. if (macosHasScriptMgr) then
  289. macosNrOfScriptsInstalled := GetEnvirons(smEnabled);
  290. {$ELSE}
  291. if (macosHasScriptMgr) then
  292. macosNrOfScriptsInstalled := GetScriptManagerVariable(smEnabled); {Gamla rutinnamnet var GetEnvirons.}
  293. {$ENDIF}
  294. {$ENDIF}
  295. end
  296. else
  297. begin
  298. macosHasScriptMgr := Gestalt(FourCharCodeToLongword(gestaltScriptMgrVersion), response) = noErr; {Fšr att ta reda pŒ om script mgr finns.}
  299. macosNrOfScriptsInstalled := 1; (* assume only Roman script, to start with *)
  300. macosHasWaitNextEvent := true;
  301. if Gestalt(FourCharCodeToLongword(gestaltSystemVersion), response) = noErr then
  302. macosSystemVersion := response
  303. else
  304. macosSystemVersion := 0; {Borde inte kunna hŠnda.}
  305. if Gestalt(FourCharCodeToLongword(gestaltOSAttr), response) = noErr then
  306. macosHasSysDebugger := BitIsSet(response, gestaltSysDebuggerSupport)
  307. else
  308. macosHasSysDebugger := false;
  309. if Gestalt(FourCharCodeToLongword(gestaltQuickdrawVersion), response) = noErr then
  310. macosHasColorQD := (response >= $0100)
  311. else
  312. macosHasColorQD := false;
  313. if Gestalt(FourCharCodeToLongword(gestaltFPUType), response) = noErr then
  314. macosHasFPU := (response <> gestaltNoFPU)
  315. else
  316. macosHasFPU := false;
  317. if Gestalt(FourCharCodeToLongword(gestaltCFMAttr), response) = noErr then
  318. macosHasCFM := BitIsSet(response, gestaltCFMPresent)
  319. else
  320. macosHasCFM := false;
  321. macosHasAppleEvents := Gestalt(FourCharCodeToLongword(gestaltAppleEventsAttr), response) = noErr;
  322. macosHasAliasMgr := Gestalt(FourCharCodeToLongword(gestaltAliasMgrAttr), response) = noErr;
  323. if Gestalt(FourCharCodeToLongword(gestaltFSAttr), response) = noErr then
  324. macosHasFSSpec := BitIsSet(response, gestaltHasFSSpecCalls)
  325. else
  326. macosHasFSSpec := false;
  327. macosHasFindFolder := Gestalt(FourCharCodeToLongword(gestaltFindFolderAttr), response) = noErr;
  328. if macosHasScriptMgr then
  329. begin
  330. err := Gestalt(FourCharCodeToLongword(gestaltScriptCount), response);
  331. if (err = noErr) then
  332. macosNrOfScriptsInstalled := Integer(response);
  333. end;
  334. if (Gestalt(FourCharCodeToLongword(gestaltAppearanceAttr), response) = noErr) then
  335. begin
  336. macosHasAppearance := BitIsSet(response, gestaltAppearanceExists);
  337. if Gestalt(FourCharCodeToLongword(gestaltAppearanceVersion), response) = noErr then
  338. begin
  339. macosHasAppearance101 := (response >= $101);
  340. macosHasAppearance11 := (response >= $110);
  341. end
  342. end
  343. else
  344. begin
  345. macosHasAppearance := false;
  346. macosHasAppearance101 := false;
  347. macosHasAppearance11 := false;
  348. end;
  349. end;
  350. end;
  351. {*****************************************************************************
  352. System Dependent Exit code
  353. *****************************************************************************}
  354. Procedure system_exit;
  355. var
  356. s: ShortString;
  357. begin
  358. if StandAlone <> 0 then
  359. if exitcode <> 0 then
  360. begin
  361. Str(exitcode,s);
  362. if IsConsole then
  363. Writeln( '### Program exited with exit code ' + s)
  364. else if macosHasSysDebugger then
  365. DebugStr('A possible error occured, exit code: ' + s + '. Type "g" and return to continue.')
  366. else
  367. {Be quiet}
  368. end;
  369. {$ifndef MACOS_USE_STDCLIB}
  370. if StandAlone <> 0 then
  371. ExitToShell;
  372. {$else}
  373. c_exit(exitcode); {exitcode is only utilized by an MPW tool}
  374. {$endif}
  375. end;
  376. procedure SysInitStdIO;
  377. begin
  378. { Setup stdin, stdout and stderr }
  379. {$ifdef MACOS_USE_STDCLIB}
  380. OpenStdIO(Input,fmInput,StdInputHandle);
  381. OpenStdIO(Output,fmOutput,StdOutputHandle);
  382. OpenStdIO(ErrOutput,fmOutput,StdErrorHandle);
  383. OpenStdIO(StdOut,fmOutput,StdOutputHandle);
  384. OpenStdIO(StdErr,fmOutput,StdErrorHandle);
  385. {$endif }
  386. end;
  387. function GetProcessID: SizeUInt;
  388. begin
  389. GetProcessID := 1;
  390. {$WARNING To be implemented - using GetProcessInformation???}
  391. end;
  392. function CheckInitialStkLen(stklen : SizeUInt) : SizeUInt;
  393. begin
  394. result := stklen;
  395. end;
  396. var
  397. resHdl: Mac_Handle;
  398. isFolder, hadAlias, leafIsAlias: Boolean;
  399. dirStr: string[2];
  400. err: OSErr;
  401. dummySysFolderDirID: Longint;
  402. begin
  403. InvestigateSystem; {Must be first}
  404. {Check requred features for system.pp to work.}
  405. if not macosHasFSSpec then
  406. Halt(3); //exit code 3 according to MPW
  407. if FindSysFolder(macosBootVolumeVRefNum, dummySysFolderDirID) <> noErr then
  408. Halt(3); //exit code 3 according to MPW
  409. if GetVolumeName(macosBootVolumeVRefNum, macosBootVolumeName) <> noErr then
  410. Halt(3); //exit code 3 according to MPW
  411. { To be set if this is a GUI or console application }
  412. if StandAlone = 0 then
  413. IsConsole := true {Its an MPW tool}
  414. else
  415. begin
  416. resHdl:= Get1Resource(FourCharCodeToLongword('siow'),0);
  417. IsConsole := (resHdl <> nil); {A SIOW app is also a console}
  418. ReleaseResource(resHdl);
  419. end;
  420. { To be set if this is a library and not a program }
  421. IsLibrary := FALSE;
  422. StackLength := CheckInitialStkLen(InitialStkLen);
  423. StackBottom := SPtr - StackLength;
  424. pathTranslation:= false;
  425. { Setup working directory }
  426. if StandAlone <> 0 then
  427. begin
  428. if not GetAppFileLocation(workingDirectorySpec) then
  429. Halt(3); //exit code 3 according to MPW
  430. end
  431. else
  432. begin
  433. { The fictive file x is used to make
  434. FSMakeFSSpec return a FSSpec to a file in the directory.
  435. Then by clearing the name, the FSSpec then
  436. points to the directory. It doesn't matter whether x exists or not.}
  437. dirStr:= ':x';
  438. err:= ResolveFolderAliases(0, 0, @dirStr, true,
  439. workingDirectorySpec, isFolder, hadAlias, leafIsAlias);
  440. workingDirectorySpec.name:='';
  441. if (err <> noErr) and (err <> fnfErr) then
  442. Halt(3); //exit code 3 according to MPW
  443. end;
  444. { Setup heap }
  445. if StandAlone <> 0 then
  446. MaxApplZone;
  447. InitHeap;
  448. SysInitExceptions;
  449. initunicodestringmanager;
  450. SysInitStdIO;
  451. { Setup environment and arguments }
  452. Setup_Environment;
  453. setup_arguments;
  454. { Reset IO Error }
  455. InOutRes:=0;
  456. errno:=0;
  457. InitSystemThreads;
  458. initvariantmanager;
  459. if StandAlone = 0 then
  460. begin
  461. InitGraf(@qd.thePort);
  462. SetFScaleDisable(true);
  463. InitCursorCtl(nil);
  464. end;
  465. end.