system.pp 17 KB

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