1 //
2 // Copyright (c) 2010-2018 Antmicro
3 //
4 // This file is licensed under the MIT License.
5 // Full license text is available in 'licenses/MIT.txt'.
6 //
7 #if PLATFORM_OSX
8 using System.Diagnostics;
9 using Antmicro.Renode.Utilities;
10 using System.IO;
11 using Mono.Unix.Native;
12 
13 namespace Antmicro.Renode.UI
14 {
15     [ConsoleBackendAnalyzerProvider("TerminalApp")]
16     public class TerminalAppProvider : ProcessBasedProvider
17     {
CreateProcess(string consoleName, string command)18         protected override Process CreateProcess(string consoleName, string command)
19         {
20             var script = TemporaryFilesManager.Instance.GetTemporaryFile();
21             File.WriteAllLines(script, new [] {
22                 "#!/usr/bin/env bash",
23                 command
24             });
25 
26             var p = new Process();
27             p.EnableRaisingEvents = true;
28             Syscall.chmod(script, FilePermissions.S_IXUSR | FilePermissions.S_IRUSR | FilePermissions.S_IWUSR);
29 
30             var arguments = $"-a /Applications/Utilities/Terminal.app {script}";
31             p.StartInfo = new ProcessStartInfo("open", arguments)
32             {
33                 UseShellExecute = false,
34                 RedirectStandardError = true,
35                 RedirectStandardOutput = true,
36                 RedirectStandardInput = true
37             };
38             p.Exited += (sender, e) =>
39             {
40                 var proc = sender as Process;
41                 if (proc.ExitCode != 0)
42                 {
43                     LogError("Terminal.app", arguments, proc.ExitCode);
44                 }
45                 // We do not call InnerOnClose here, because the closing routine of the "open" application is counterintuitive.
46                 // In current setup it closes automatically like gnome-terminal. We may add -W or -Wn, but
47                 // then Exited event never gets called on window close, the user must close the app from the
48                 // Dock. This will either force the user to kill all terminals (-W) or it will create multiple
49                 // terminal icons (-Wn) that will stay there unless manually closed. Both options are bad.
50             };
51             return p;
52         }
53     }
54 }
55 #endif
56