system.pp 17 KB

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