*
* The MIT License (MIT)
*
- * Copyright (C) 2016 Kevin Lamonte
+ * Copyright (C) 2017 Kevin Lamonte
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
+import java.lang.reflect.Field;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
// You cannot run a login shell in a bare Process interactively,
// due to libc's behavior of buffering when stdin/stdout aren't a
- // tty. Use 'script' instead to run a shell in a pty.
- String [] cmdShell = {
+ // tty. Use 'script' instead to run a shell in a pty. And
+ // because BSD and GNU differ on the '-f' vs '-F' flags, we need
+ // two different commands. Lovely.
+ String [] cmdShellGNU = {
"script", "-fqe", "/dev/null"
};
+ String [] cmdShellBSD = {
+ "script", "-q", "-F", "/dev/null"
+ };
// Spawn a shell and pass its I/O to the other constructor.
ProcessBuilder pb;
if (System.getProperty("os.name").startsWith("Windows")) {
pb = new ProcessBuilder(cmdShellWindows);
+ } else if (System.getProperty("os.name").startsWith("Mac")) {
+ pb = new ProcessBuilder(cmdShellBSD);
+ } else if (System.getProperty("os.name").startsWith("Linux")) {
+ pb = new ProcessBuilder(cmdShellGNU);
} else {
- pb = new ProcessBuilder(cmdShell);
+ // When all else fails, assume GNU.
+ pb = new ProcessBuilder(cmdShellGNU);
}
Map<String, String> env = pb.environment();
env.put("TERM", ECMA48.deviceTypeTerm(deviceType));
// Claim the keystrokes the emulator will need.
addShortcutKeys();
+
+ // Add shortcut text
+ newStatusBar("Terminal session executing...");
+ }
+
+ /**
+ * Terminate the child of the 'script' process used on POSIX. This may
+ * or may not work.
+ */
+ private void terminateShellChildProcess() {
+ int pid = -1;
+ if (shell.getClass().getName().equals("java.lang.UNIXProcess")) {
+ /* get the PID on unix/linux systems */
+ try {
+ Field field = shell.getClass().getDeclaredField("pid");
+ field.setAccessible(true);
+ pid = field.getInt(shell);
+ } catch (Throwable e) {
+ // SQUASH, this didn't work. Just bail out quietly.
+ return;
+ }
+ }
+ if (pid != -1) {
+ // shell.destroy() works successfully at killing this side of
+ // 'script'. But we need to make sure the other side (child
+ // process) is also killed.
+ String [] cmdKillIt = {
+ "pkill", "-P", Integer.toString(pid)
+ };
+ try {
+ Runtime.getRuntime().exec(cmdKillIt);
+ } catch (Throwable e) {
+ // SQUASH, this didn't work. Just bail out quietly.
+ return;
+ }
+ }
}
/**
// Claim the keystrokes the emulator will need.
addShortcutKeys();
+
+ // Add shortcut text
+ newStatusBar("Terminal session executing...");
}
/**
/**
* Handle window close.
*/
- @Override public void onClose() {
+ @Override
+ public void onClose() {
emulator.close();
if (shell != null) {
- // System.err.println("shell.destroy()");
+ terminateShellChildProcess();
shell.destroy();
shell = null;
}
shell = null;
emulator.close();
clearShortcutKeypresses();
+ statusBar.setText("Terminal session completed, exit " +
+ "code " + rc + ".");
} catch (IllegalThreadStateException e) {
// The emulator thread has exited, but the shell Process
// hasn't figured that out yet. Do nothing, we will see
shell = null;
emulator.close();
clearShortcutKeypresses();
+ statusBar.setText("Terminal session completed, exit " +
+ "code " + rc + ".");
} catch (IllegalThreadStateException e) {
// The shell is still running, do nothing.
}