From d3485e9f6d13fd520c7eeedc7fd1e9dc1eef1379 Mon Sep 17 00:00:00 2001 From: crschnick Date: Thu, 11 May 2023 09:53:54 +0000 Subject: [PATCH] Attempt to prevent double init file execution when terminal opens --- .../xpipe/app/prefs/ExternalTerminalType.java | 29 +++++++++++-------- .../io/xpipe/app/util/TerminalHelper.java | 6 ++-- .../io/xpipe/core/process/ShellDialect.java | 2 ++ 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/io/xpipe/app/prefs/ExternalTerminalType.java b/app/src/main/java/io/xpipe/app/prefs/ExternalTerminalType.java index cef5b4ba..aab5c178 100644 --- a/app/src/main/java/io/xpipe/app/prefs/ExternalTerminalType.java +++ b/app/src/main/java/io/xpipe/app/prefs/ExternalTerminalType.java @@ -30,12 +30,12 @@ public interface ExternalTerminalType extends PrefsChoiceValue { } }; - public static final ExternalTerminalType POWERSHELL = + public static final ExternalTerminalType POWERSHELL_WINDOWS = new SimpleType("powershell", "powershell", "PowerShell") { @Override protected String toCommand(String name, String file) { - return "-ExecutionPolicy Bypass -Command cmd /C '" + file + "'"; + return "-ExecutionPolicy Bypass -NoProfile -Command " + noInit(file); } @Override @@ -44,13 +44,13 @@ public interface ExternalTerminalType extends PrefsChoiceValue { } }; - public static final ExternalTerminalType PWSH = new SimpleType("pwsh", "pwsh", "PowerShell Core") { + public static final ExternalTerminalType PWSH_WINDOWS = new SimpleType("pwsh", "pwsh", "PowerShell Core") { @Override protected String toCommand(String name, String file) { // Fix for https://github.com/PowerShell/PowerShell/issues/18530#issuecomment-1325691850 - var script = ScriptHelper.createLocalExecScript("set \"PSModulePath=\"\r\n\"" + file + "\"\npause"); - return "-ExecutionPolicy Bypass -Command cmd /C '" +script + "'"; + var script = ScriptHelper.createLocalExecScript("set \"PSModulePath=\"\r\n\"" + noInit(file) + "\"\npause"); + return "-ExecutionPolicy Bypass -NoProfile -Command " + noInit(script); } @Override @@ -68,7 +68,7 @@ public interface ExternalTerminalType extends PrefsChoiceValue { // backslash of a filepath to escape the closing quote in the title argument // So just remove that slash var fixedName = FileNames.removeTrailingSlash(name); - return "-w 1 nt --title \"" + fixedName + "\" \"" + file + "\""; + return "-w 1 nt --title \"" + fixedName + "\" " + noInit(file); } @Override @@ -89,7 +89,7 @@ public interface ExternalTerminalType extends PrefsChoiceValue { // In order to fix this bug which also affects us: // https://askubuntu.com/questions/1148475/launching-gnome-terminal-from-vscode toExecute = - "GNOME_TERMINAL_SCREEN=\"\" nohup " + toExecute + " /dev/null & disown"; + "GNOME_TERMINAL_SCREEN=\"\" nohup " + noInit(file) + " /dev/null & disown"; pc.executeSimpleCommand(toExecute); } } @@ -109,7 +109,7 @@ public interface ExternalTerminalType extends PrefsChoiceValue { @Override protected String toCommand(String name, String file) { - return "--new-tab -e \"" + file + "\""; + return "--new-tab -e " + noInit(file); } @Override @@ -122,7 +122,7 @@ public interface ExternalTerminalType extends PrefsChoiceValue { @Override protected String toCommand(String name, String file) { - return "--tab --title \"" + name + "\" --command \"" + file + "\""; + return "--tab --title \"" + name + "\" --command " + noInit(file); } @Override @@ -141,8 +141,8 @@ public interface ExternalTerminalType extends PrefsChoiceValue { public static final List ALL = Stream.of( WINDOWS_TERMINAL, - PWSH, - POWERSHELL, + PWSH_WINDOWS, + POWERSHELL_WINDOWS, CMD, KONSOLE, XFCE, @@ -164,6 +164,10 @@ public interface ExternalTerminalType extends PrefsChoiceValue { public abstract void launch(String name, String file, boolean elevated) throws Exception; + default String noInit(String file) { + return ShellDialects.getPlatformDefault().executeWithNoInitFiles(ShellDialects.getPlatformDefault(), file); + } + static class MacOsTerminalType extends ExternalApplicationType.MacApplication implements ExternalTerminalType { public MacOsTerminalType() { @@ -232,6 +236,7 @@ public interface ExternalTerminalType extends PrefsChoiceValue { @Override public void launch(String name, String file, boolean elevated) throws Exception { + var toExecute = noInit(file).replaceAll("\"", "\\\\\""); try (ShellControl pc = LocalStore.getShell()) { var cmd = String.format( """ @@ -252,7 +257,7 @@ public interface ExternalTerminalType extends PrefsChoiceValue { end tell end if EOF""", - file.replaceAll("\"", "\\\\\""), file.replaceAll("\"", "\\\\\"")); + toExecute, toExecute); pc.executeSimpleCommand(cmd); } } diff --git a/app/src/main/java/io/xpipe/app/util/TerminalHelper.java b/app/src/main/java/io/xpipe/app/util/TerminalHelper.java index d8374c83..45ff24ce 100644 --- a/app/src/main/java/io/xpipe/app/util/TerminalHelper.java +++ b/app/src/main/java/io/xpipe/app/util/TerminalHelper.java @@ -14,15 +14,13 @@ public class TerminalHelper { } public static void open(String title, String command) throws Exception { - if (command.contains("\n") || command.contains(" ") || command.contains("\"") || command.contains("'")) { - command = ScriptHelper.createLocalExecScript(command); - } - var type = AppPrefs.get().terminalType().getValue(); if (type == null) { throw new IllegalStateException(AppI18n.get("noTerminalSet")); } + command = ScriptHelper.createLocalExecScript(command); + try { type.launch(title, command, false); } catch (Exception ex) { diff --git a/core/src/main/java/io/xpipe/core/process/ShellDialect.java b/core/src/main/java/io/xpipe/core/process/ShellDialect.java index 34ef64f8..72adb6c6 100644 --- a/core/src/main/java/io/xpipe/core/process/ShellDialect.java +++ b/core/src/main/java/io/xpipe/core/process/ShellDialect.java @@ -20,6 +20,8 @@ public interface ShellDialect { String fileArgument(String s); + String executeWithNoInitFiles(ShellDialect parentDialect, String file); + void prepareDumbTerminalCommands(ShellControl sc) throws Exception; String prepareProperTerminalCommands();