- /**
- * Convert a string array to a whitespace-separated string.
- *
- * @param array the string array
- * @return a single string
- */
- private String stringArrayToString(final String [] array) {
- StringBuilder sb = new StringBuilder(array[0].length());
- for (int i = 0; i < array.length; i++) {
- sb.append(array[i]);
- if (i < array.length - 1) {
- sb.append(' ');
- }
- }
- return sb.toString();
- }
-
- /**
- * Spawn the shell.
- *
- * @param command the command line to execute
- */
- private void spawnShell(final String [] command) {
-
- /*
- System.err.printf("spawnShell(): '%s'\n",
- stringArrayToString(command));
- */
-
- vScroller = new TVScroller(this, getWidth() - 2, 0, getHeight() - 2);
- setBottomValue(0);
-
- // Assume XTERM
- ECMA48.DeviceType deviceType = ECMA48.DeviceType.XTERM;
-
- try {
- ProcessBuilder pb = new ProcessBuilder(command);
- Map<String, String> env = pb.environment();
- env.put("TERM", ECMA48.deviceTypeTerm(deviceType));
- env.put("LANG", ECMA48.deviceTypeLang(deviceType, "en"));
- env.put("COLUMNS", "80");
- env.put("LINES", "24");
- pb.redirectErrorStream(true);
- shell = pb.start();
- emulator = new ECMA48(deviceType, shell.getInputStream(),
- shell.getOutputStream(), this);
- } catch (IOException e) {
- messageBox(i18n.getString("errorLaunchingShellTitle"),
- MessageFormat.format(i18n.getString("errorLaunchingShellText"),
- e.getMessage()));
- }
-
- // Setup the scroll bars
- onResize(new TResizeEvent(TResizeEvent.Type.WIDGET, getWidth(),
- getHeight()));
-
- // Claim the keystrokes the emulator will need.
- addShortcutKeys();
-
- // Add shortcut text
- newStatusBar(i18n.getString("statusBarRunning"));
- }
-
- /**
- * 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;
- }
- }
- }
-
- /**
- * Called by emulator when fresh data has come in.
- */
- public void displayChanged() {
- getApplication().postEvent(new TMenuEvent(TMenu.MID_REPAINT));
- }
-
- /**
- * Function to call to obtain the display width.
- *
- * @return the number of columns in the display
- */
- public int getDisplayWidth() {
- if (ptypipe) {
- return getWidth() - 2;
- }
- return 80;
- }
-
- /**
- * Function to call to obtain the display height.
- *
- * @return the number of rows in the display
- */
- public int getDisplayHeight() {
- if (ptypipe) {
- return getHeight() - 2;
- }
- return 24;
- }
-