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