|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright 2026 Espressif Systems (Shanghai) PTE LTD. All rights reserved. |
| 3 | + * Use is subject to license terms. |
| 4 | + *******************************************************************************/ |
| 5 | +package com.espressif.idf.ui.tools.eim.terminal; |
| 6 | + |
| 7 | +import java.io.IOException; |
| 8 | +import java.io.OutputStream; |
| 9 | +import java.nio.charset.StandardCharsets; |
| 10 | + |
| 11 | +import org.eclipse.core.runtime.Platform; |
| 12 | +import org.eclipse.terminal.connector.ISettingsStore; |
| 13 | +import org.eclipse.terminal.connector.ITerminalControl; |
| 14 | +import org.eclipse.terminal.connector.process.ProcessConnector; |
| 15 | + |
| 16 | +import com.espressif.idf.core.logging.Logger; |
| 17 | + |
| 18 | +/** |
| 19 | + * Opens a login shell, sends the EIM CLI executable as the first command, and notifies |
| 20 | + * {@link EimCliTerminalLaunchSupport} when the process exits. |
| 21 | + * |
| 22 | + * @author Ali Azam Rana <ali.azamrana@espressif.com> |
| 23 | + */ |
| 24 | +public final class EimCliTerminalConnector extends ProcessConnector |
| 25 | +{ |
| 26 | + /** |
| 27 | + * Key used to round-trip the EIM executable path through the settings store. |
| 28 | + * Must match the key used in {@link EimCliTerminalLauncherDelegate#createTerminalConnector}. |
| 29 | + */ |
| 30 | + static final String SETTINGS_KEY_EIM_PATH = "eim.cli.executable"; //$NON-NLS-1$ |
| 31 | + |
| 32 | + private String eimExecutablePath; |
| 33 | + |
| 34 | + @Override |
| 35 | + public void load(ISettingsStore store) |
| 36 | + { |
| 37 | + super.load(store); |
| 38 | + eimExecutablePath = store.get(SETTINGS_KEY_EIM_PATH, null); |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public void connect(ITerminalControl control) |
| 43 | + { |
| 44 | + super.connect(control); |
| 45 | + var process = getProcess(); |
| 46 | + if (process == null) |
| 47 | + { |
| 48 | + Logger.log("EIM CLI terminal: no process after connect"); //$NON-NLS-1$ |
| 49 | + EimCliTerminalLaunchSupport.invokeCompletionIfArmed(); |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + if (eimExecutablePath != null && !eimExecutablePath.isBlank()) |
| 54 | + { |
| 55 | + sendEimCommand(process.getOutputStream()); |
| 56 | + } |
| 57 | + |
| 58 | + Thread waiter = new Thread(() -> { |
| 59 | + try |
| 60 | + { |
| 61 | + int code = process.waitFor(); |
| 62 | + Logger.log("EIM CLI terminal process exited with code " + code); //$NON-NLS-1$ |
| 63 | + } |
| 64 | + catch (InterruptedException e) |
| 65 | + { |
| 66 | + Thread.currentThread().interrupt(); |
| 67 | + } |
| 68 | + finally |
| 69 | + { |
| 70 | + EimCliTerminalLaunchSupport.invokeCompletionIfArmed(); |
| 71 | + } |
| 72 | + }, "EimCliTerminalWait"); //$NON-NLS-1$ |
| 73 | + waiter.setDaemon(true); |
| 74 | + waiter.start(); |
| 75 | + } |
| 76 | + |
| 77 | + private void sendEimCommand(OutputStream out) |
| 78 | + { |
| 79 | + String command; |
| 80 | + if (Platform.OS_WIN32.equals(Platform.getOS())) |
| 81 | + { |
| 82 | + String escaped = eimExecutablePath.replace("'", "''"); //$NON-NLS-1$ //$NON-NLS-2$ |
| 83 | + command = "& '" + escaped + "' wizard; exit\r\n"; //$NON-NLS-1$ //$NON-NLS-2$ |
| 84 | + } |
| 85 | + else |
| 86 | + { |
| 87 | + String quoted = "'" + eimExecutablePath.replace("'", "'\\''") + "'"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
| 88 | + command = quoted + " wizard; exit\r\n"; //$NON-NLS-1$ |
| 89 | + } |
| 90 | + try |
| 91 | + { |
| 92 | + out.write(command.getBytes(StandardCharsets.UTF_8)); |
| 93 | + out.flush(); |
| 94 | + Logger.log("EIM CLI terminal sent command: " + command.trim()); //$NON-NLS-1$ |
| 95 | + } |
| 96 | + catch (IOException e) |
| 97 | + { |
| 98 | + Logger.log(e); |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments