diff --git a/terminal/bundles/org.eclipse.terminal.control/META-INF/MANIFEST.MF b/terminal/bundles/org.eclipse.terminal.control/META-INF/MANIFEST.MF index 9ace9ed4bee..70e66a2a2df 100644 --- a/terminal/bundles/org.eclipse.terminal.control/META-INF/MANIFEST.MF +++ b/terminal/bundles/org.eclipse.terminal.control/META-INF/MANIFEST.MF @@ -2,7 +2,7 @@ Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: %pluginName Bundle-SymbolicName: org.eclipse.terminal.control; singleton:=true -Bundle-Version: 1.2.0.qualifier +Bundle-Version: 1.2.100.qualifier Bundle-Activator: org.eclipse.terminal.internal.control.impl.TerminalPlugin Bundle-Vendor: %providerName Bundle-Localization: plugin diff --git a/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/emulator/IVT100EmulatorBackend.java b/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/emulator/IVT100EmulatorBackend.java index 7f1d0af4c16..a48d11b577b 100644 --- a/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/emulator/IVT100EmulatorBackend.java +++ b/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/emulator/IVT100EmulatorBackend.java @@ -223,6 +223,28 @@ default void enableAlternateScreen(boolean enable) { */ void setVT100LineWrapping(boolean enable); + /** + * Auto wrap mode (DECAWM, CSI ? 7 h/l), on by default. Off, a character + * written at the right margin replaces the one there and the cursor stays. + */ + void setAutoWrap(boolean enable); + + /** + * @return the first line of the scroll region, in screen lines + */ + int getScrollRegionTop(); + + /** + * @return the last line of the scroll region, in screen lines + */ + int getScrollRegionBottom(); + + /** + * Fills every cell of the screen with the character in the default rendition, + * as the screen alignment test (DECALN, ESC # 8) does. The cursor does not move. + */ + void fillScreen(char c); + /** * @return whether VT100 line wrapping mode is enabled */ @@ -271,4 +293,16 @@ default void enableAlternateScreen(boolean enable) { * @param n number of characters to replace */ void eraseCharacters(int n); + + /** + * @return whether the cursor is on the last column with a wrap pending: the + * next character goes to the next line + */ + boolean isWrapPending(); + + /** + * Puts back a pending wrap, as restoring a saved cursor (DECRC) does; it only + * holds with the cursor on the last column. + */ + void setWrapPending(boolean pending); } diff --git a/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/emulator/VT100BackendTraceDecorator.java b/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/emulator/VT100BackendTraceDecorator.java index 7fa3c6f6ad9..914bba49093 100644 --- a/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/emulator/VT100BackendTraceDecorator.java +++ b/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/emulator/VT100BackendTraceDecorator.java @@ -198,6 +198,39 @@ public boolean isVT100LineWrapping() { return fBackend.isVT100LineWrapping(); } + @Override + public void setAutoWrap(boolean enable) { + fWriter.println("setAutoWrap(" + enable + ")"); //$NON-NLS-1$ //$NON-NLS-2$ + fBackend.setAutoWrap(enable); + } + + @Override + public int getScrollRegionTop() { + return fBackend.getScrollRegionTop(); + } + + @Override + public int getScrollRegionBottom() { + return fBackend.getScrollRegionBottom(); + } + + @Override + public boolean isWrapPending() { + return fBackend.isWrapPending(); + } + + @Override + public void setWrapPending(boolean pending) { + fWriter.println("setWrapPending(" + pending + ")"); //$NON-NLS-1$ //$NON-NLS-2$ + fBackend.setWrapPending(pending); + } + + @Override + public void fillScreen(char c) { + fWriter.println("fillScreen(" + c + ")"); //$NON-NLS-1$ //$NON-NLS-2$ + fBackend.fillScreen(c); + } + @Override public void setInsertMode(boolean enable) { fWriter.println("setInsertMode(" + enable + ")"); //$NON-NLS-1$ //$NON-NLS-2$ diff --git a/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/emulator/VT100Emulator.java b/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/emulator/VT100Emulator.java index 832d6e628dd..ae31960c580 100644 --- a/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/emulator/VT100Emulator.java +++ b/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/emulator/VT100Emulator.java @@ -39,8 +39,11 @@ import static org.eclipse.terminal.model.TerminalColor.YELLOW; import java.io.IOException; +import java.io.OutputStream; import java.io.Reader; +import java.nio.charset.StandardCharsets; import java.util.Arrays; +import java.util.BitSet; import java.util.function.IntConsumer; import org.eclipse.core.runtime.Platform; @@ -64,6 +67,9 @@ */ public class VT100Emulator implements ControlListener { /** This is a character processing state: Initial state. */ + /** the largest value a parameter of a control sequence takes, as in xterm (MAX_I_PARAM) */ + private static final int MAX_PARAMETER = 65535; + private static final int ANSISTATE_INITIAL = 0; /** This is a character processing state: We've seen an escape character. */ @@ -86,6 +92,7 @@ public class VT100Emulator implements ControlListener { * This is a character processing state: We've seen a '[?' after an escape * character. Expecting a parameter character or a command character next. */ + private static final int ANSISTATE_EXPECTING_OS_COMMAND_END = 6; private static final int ANSISTATE_EXPECTING_DEC_PRIVATE_COMMAND = 4; /** @@ -98,7 +105,27 @@ public class VT100Emulator implements ControlListener { * For cases where the OSC (OS Command) ends with a multi-byte ST (i.e. ESC \) * we use this extra state. */ - private static final int ANSISTATE_EXPECTING_OS_COMMAND_END = 6; + + /** + * This is a character processing state: a control sequence we do not support + * (one with an intermediate byte or a private marker other than '?'). Its + * characters are dropped up to its final character. + */ + private static final int ANSISTATE_IGNORING_COMMAND = 7; + + /** + * This is a character processing state: an escape with intermediate bytes + * (0x20-0x2F), such as "ESC # 8". Its characters are dropped up to its final + * character. + */ + private static final int ANSISTATE_IGNORING_ESCAPE = 8; + + /** + * This is a character processing state: a DCS, SOS, PM or APC string ("ESC P", + * "ESC X", "ESC ^", "ESC _"). None is supported, so the string is dropped up + * to the ESC of its terminating ST. + */ + private static final int ANSISTATE_IGNORING_STRING = 9; /** * This field holds the current state of the Finite TerminalState Automaton (FSA) @@ -119,17 +146,71 @@ public class VT100Emulator implements ControlListener { * display text to the user. */ final private IVT100EmulatorBackend text; - /** - * The cursor saved by "ESC 7" (and CSI ? 1048 h) for "ESC 8", one slot per screen - * as in xterm: [0] on the normal screen, [1] on the alternate one, so a full screen - * program saving its cursor does not overwrite the one the shell saved. - */ - private final int[] savedCursorLine = new int[2]; - private final int[] savedCursorColumn = new int[2]; /** whether a program asked for the alternate screen (47, 1047 or 1049) */ private boolean alternateScreen; + /** + * Origin mode (DECOM, CSI ? 6 h): line numbers in cursor positions and reports + * count from the top of the scroll region, and the cursor stays in it. + */ + private boolean originMode; + + /** + * Character sets, as far as they are supported: whether G0 and G1 hold DEC's + * special graphics (line drawing, "ESC ( 0", "ESC ) 0") rather than ASCII, and + * which of the two SI and SO have invoked (GL). + */ + private final boolean[] lineDrawing = new boolean[2]; + private int invokedCharset; + + /** G0 or G1, while an "ESC (" or "ESC )" waits for its final character; -1 for the others */ + private int designatedCharset; + /** whether the set being designated has an intermediate byte in its name */ + private boolean namedCharset; + + /** + * What DECSC (ESC 7) saves, all of it together: the cursor, a pending wrap, the + * rendition, the origin mode and the character sets. Saved whole and restored + * whole, so that nothing of it can be left behind by a reset. + */ + private record Saved(int line, int column, boolean wrapPending, TerminalStyle style, boolean originMode, + boolean lineDrawing0, boolean lineDrawing1, int invokedCharset) { + } + + /** + * The slots of ESC 7, one per screen as in xterm: [0] on the normal screen, [1] on + * the alternate one, so a full screen program saving its cursor does not overwrite + * the one the shell saved; and [2], what CSI ? 1049 h saved, the cursor of which + * the backend keeps: leaving the alternate screen puts the normal screen's modes + * back without touching what ESC 7 saved there. Null for nothing saved. + */ + private static final int SCREEN_SWITCH = 2; + private final Saved[] saved = new Saved[3]; + + /** the tab stops, one bit per column; one every 8 columns until a program sets its own */ + private final BitSet tabStops = new BitSet(); + private int tabStopsColumns; + /** whether a program cleared every stop (TBC 3): the columns the terminal gains then get none */ + private boolean programTabStops; + + /** the last character written; null after anything else */ + private String lastGraphic; + /** what REP (CSI Ps b) repeats: the last character written before the ESC of its sequence */ + private String repeatable; + + /** the private marker of the control sequence being read, '?' or '>' */ + private char ansiMarker; + + /** + * the intermediate byte of the control sequence being ignored, when it is its only + * byte besides the final one, as the '!' of DECSTR (CSI ! p); 0 otherwise + */ + private char csiIntermediate; + + /** the first intermediate byte of the escape being read, as the '#' of "ESC # 8" */ + private char escapeIntermediate; + /** * This field holds an array of StringBuffer objects, each of which is one * parameter from the current ANSI escape sequence. For example, when @@ -211,15 +292,48 @@ public int getBufferLineLimit() { return text.getBufferLineLimit(); } + /** @return the ESC 7 slot of the screen in use */ + private int cursorSlot() { + return alternateScreen ? 1 : 0; + } + + private void save(int slot) { + saved[slot] = new Saved(relativeCursorLine(), getCursorColumn(), text.isWrapPending(), text.getStyle(), + originMode, lineDrawing[0], lineDrawing[1], invokedCharset); + } + + /** + * @return what the slot holds, or what ESC 8 brings back when nothing was saved + * there: the home position and the defaults, the rendition too (xterm) + */ + private Saved saved(int slot) { + return saved[slot] != null ? saved[slot] + : new Saved(0, 0, false, text.getDefaultStyle(), false, false, false, 0); + } + + /** what DECSC saves besides the cursor, put back: rendition, origin mode, character sets */ + private void restoreModes(int slot) { + Saved s = saved(slot); + text.setStyle(s.style()); + originMode = s.originMode(); + lineDrawing[0] = s.lineDrawing0(); + lineDrawing[1] = s.lineDrawing1(); + invokedCharset = s.invokedCharset(); + } + private void saveCursor() { - int screen = alternateScreen ? 1 : 0; - savedCursorLine[screen] = relativeCursorLine(); - savedCursorColumn[screen] = getCursorColumn(); + save(cursorSlot()); } private void restoreCursor() { - int screen = alternateScreen ? 1 : 0; - moveCursor(savedCursorLine[screen], savedCursorColumn[screen]); + Saved s = saved(cursorSlot()); + restoreModes(cursorSlot()); + int line = s.line(); + if (originMode) { // in origin mode the cursor comes back inside the region, as in xterm + line = Math.max(text.getScrollRegionTop(), Math.min(text.getScrollRegionBottom(), line)); + } + moveCursor(line, s.column()); + text.setWrapPending(s.wrapPending()); } public void setDimensions(int lines, int cols) { @@ -333,42 +447,36 @@ private void processNewText() throws IOException { while (hasNextChar()) { char character = getNextChar(); - switch (ansiState) { - case ANSISTATE_INITIAL: - switch (character) { - case '\u0000': - break; // NUL character. Ignore it. - - case '\u0007': - processBEL(); // BEL (Control-G) - break; - - case '\b': - processBackspace(); // Backspace - break; - - case '\t': - processTab(); // Tab. - break; - - case '\n': - processNewline(); // Newline (Control-J) - if (fCrAfterNewLine) { - processCarriageReturn(); // Carriage Return (Control-M) - } - break; + // as in DEC's terminals: CAN and SUB cancel a sequence, and inside an escape + // or control sequence ESC starts a new one and the other controls run in place + if (character == '\u0018' || character == '\u001a') { + ansiState = ANSISTATE_INITIAL; + continue; + } + if (isControl(character) && isInSequence()) { + if (character == '\u001b') { + ansiState = ANSISTATE_ESCAPE; + } else { + processControlCharacter(character); + } + continue; + } - case '\r': - processCarriageReturn(); // Carriage Return (Control-M) - break; + if (ansiState == ANSISTATE_INITIAL && isControl(character)) { + // the one place a character stops being the last one written: a control, or + // the ESC of a sequence, which may be the REP repeating it + repeatable = lastGraphic; + lastGraphic = null; + } - case '\u001b': + switch (ansiState) { + case ANSISTATE_INITIAL: + if (character == '\u001b') { ansiState = ANSISTATE_ESCAPE; // Escape. - break; - - default: + } else if (isControl(character)) { + processControlCharacter(character); + } else { processNonControlCharacters(character); - break; } break; @@ -380,6 +488,8 @@ private void processNewText() throws IOException { case '[': ansiState = ANSISTATE_EXPECTING_PARAMETER_OR_COMMAND; nextAnsiParameter = 0; + ansiMarker = 0; + csiIntermediate = 0; // Erase the parameter strings in preparation for optional // parameter characters. @@ -402,6 +512,30 @@ private void processNewText() throws IOException { case '.': case '/': ansiState = ANSISTATE_EXPECTING_CHARSET_DESIGNATION; + // ESC - designates a 96-character set into G1: it replaces G1, but none of + // those sets is line drawing + designatedCharset = character == '(' ? 0 : character == ')' || character == '-' ? 1 : -1; + namedCharset = character == '-'; + break; + + case 'D': + // Index (IND): down a line, scrolling at the bottom margin + ansiState = ANSISTATE_INITIAL; + processNewline(); + break; + + case 'E': + // Next Line (NEL) + ansiState = ANSISTATE_INITIAL; + processNewline(); + processCarriageReturn(); + break; + + case 'H': + // Horizontal Tab Set (HTS) + ansiState = ANSISTATE_INITIAL; + ensureTabStops(); + tabStops.set(getCursorColumn()); break; case '7': @@ -431,50 +565,82 @@ private void processNewText() throws IOException { text.processReverseLineFeed(); break; - default: - Logger.log("Unsupported escape sequence: escape '" + character + "'"); //$NON-NLS-1$ //$NON-NLS-2$ + case 'P': // DCS + case 'X': // SOS + case '^': // PM + case '_': // APC + ansiState = ANSISTATE_IGNORING_STRING; + Logger.log("Ignoring unsupported string: escape '" + character + "'"); //$NON-NLS-1$ //$NON-NLS-2$ + break; + + case '\\': + // ST, the end of a string we have dropped ansiState = ANSISTATE_INITIAL; break; + + default: + if (isIntermediate(character)) { + ansiState = ANSISTATE_IGNORING_ESCAPE; + escapeIntermediate = character; + } else { + ansiState = ANSISTATE_INITIAL; + Logger.log("Unsupported escape sequence: escape '" + character + "'"); //$NON-NLS-1$ //$NON-NLS-2$ + } + break; } break; case ANSISTATE_EXPECTING_PARAMETER_OR_COMMAND: - // characters `<=>?` mark private commands - if (character >= '\u003c' && character <= '\u003f') { - ansiState = ANSISTATE_EXPECTING_DEC_PRIVATE_COMMAND; + // characters `<=>?` mark private commands, before any parameter; DEC's + // '?' ones and the '>' of the secondary device attributes are supported + if (isPrivateMarker(character)) { + boolean first = nextAnsiParameter == 0 && ansiParameters[0].length() == 0; + if (first && (character == '?' || character == '>')) { + ansiState = ANSISTATE_EXPECTING_DEC_PRIVATE_COMMAND; + ansiMarker = character; + } else { + ansiState = ANSISTATE_IGNORING_COMMAND; + } break; } // Parameters can appear after the '[' in an escape sequence, but they // are optional. - if (character >= '\u0040' && character < '\u007f') { + if (isFinal(character)) { ansiState = ANSISTATE_INITIAL; processAnsiCommandCharacter(character); + } else if (isIntermediate(character)) { + // an intermediate byte: of the commands with one, only DECSTR is run + ansiState = ANSISTATE_IGNORING_COMMAND; + boolean noParameters = nextAnsiParameter == 0 && ansiParameters[0].length() == 0; + csiIntermediate = noParameters ? character : 0; } else { processAnsiParameterCharacter(character); } break; case ANSISTATE_EXPECTING_OS_COMMAND: - // A BEL (\u0007) or ESC \ ('\e\\') character marks the end of the OSC sequence. - + // BEL or ST (ESC \\) ends it; the other controls in it are dropped if (character == '\u0007') { ansiState = ANSISTATE_INITIAL; processAnsiOsCommand(); } else if (character == '\u001b') { ansiState = ANSISTATE_EXPECTING_OS_COMMAND_END; - } else { + } else if (!isControl(character)) { ansiOsCommand.append(character); } break; case ANSISTATE_EXPECTING_OS_COMMAND_END: - ansiState = ANSISTATE_INITIAL; if (character == '\\') { + ansiState = ANSISTATE_INITIAL; processAnsiOsCommand(); } else { - Logger.log("Unsupported escape sequence: escape '" + ansiOsCommand + " \\e" + character + "'"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ + // not an ST: the command is dropped, and the ESC starts a new sequence, + // as in the strings that are ignored + ansiState = ANSISTATE_ESCAPE; + pushBackChar(character); } break; @@ -482,19 +648,72 @@ private void processNewText() throws IOException { // Parameters can appear after the '[?' in an escape sequence, but they // are optional. - if (character >= '\u0040' && character < '\u007f') { + if (isFinal(character)) { ansiState = ANSISTATE_INITIAL; - processDecPrivateCommandCharacter(character); + if (ansiMarker == '?') { + processDecPrivateCommandCharacter(character); + } else if (character == 'c' && isAnsiParameterDefault(0)) { + // Secondary Device Attributes (DA2), CSI > c or CSI > 0 c + sendReply("\u001b[>0;0;0c"); //$NON-NLS-1$ + } else { + Logger.log("Ignoring unsupported control sequence: CSI >" + character); //$NON-NLS-1$ + } + } else if (isIntermediate(character) + || isPrivateMarker(character)) { + // an intermediate byte (as in DECRQM, "CSI ? Ps $ p") or a misplaced marker + ansiState = ANSISTATE_IGNORING_COMMAND; } else { processAnsiParameterCharacter(character); } break; case ANSISTATE_EXPECTING_CHARSET_DESIGNATION: - if (character != '%') { + if (isIntermediate(character)) { + // an intermediate byte: a set with a longer name (ESC ( % 5, ESC ( " 4), + // none of them line drawing + namedCharset = true; + } else { ansiState = ANSISTATE_INITIAL; + // only DEC's special graphics differ from ASCII here; the national + // sets are taken as ASCII, and so is the slot a longer name designates + if (designatedCharset >= 0) { + lineDrawing[designatedCharset] = character == '0' && !namedCharset; + } + } + break; + + case ANSISTATE_IGNORING_COMMAND: + if (isFinal(character)) { + ansiState = ANSISTATE_INITIAL; + if (csiIntermediate == '!' && character == 'p') { + softReset(); // DECSTR + } else { + Logger.log("Ignoring unsupported control sequence ending in '" + character + "'"); //$NON-NLS-1$ //$NON-NLS-2$ + } + } else { + csiIntermediate = 0; // more than CSI ! p + } + break; + + case ANSISTATE_IGNORING_ESCAPE: + if (isIntermediate(character)) { + escapeIntermediate = 0; // a second intermediate byte: no sequence we run + } else if (character >= '\u0030' && character < '\u007f') { + ansiState = ANSISTATE_INITIAL; + if (escapeIntermediate == '#' && character == '8') { + screenAlignmentTest(); + } else { + Logger.log("Unsupported escape sequence: escape '" + escapeIntermediate + character + "'"); //$NON-NLS-1$ //$NON-NLS-2$ + } + } + break; + + case ANSISTATE_IGNORING_STRING: + if (character == '\u001b') { + // the ESC of the ST; a "\\" after it ends the string, anything else + // starts a new sequence + ansiState = ANSISTATE_ESCAPE; } - // Character set designation commands are ignored break; default: @@ -509,6 +728,93 @@ private void processNewText() throws IOException { } } + /** @return whether it is a C0 control or DEL, which no sequence or text holds */ + private static boolean isControl(char c) { + return c < '\u0020' || c == '\u007f'; + } + + /** @return whether it is an intermediate byte, between the start and the end of a sequence */ + private static boolean isIntermediate(char c) { + return c >= '\u0020' && c <= '\u002f'; + } + + /** @return whether it is a final byte, which ends a control sequence */ + private static boolean isFinal(char c) { + return c >= '\u0040' && c <= '\u007e'; + } + + /** @return whether it is a private marker, which can open a control sequence's parameters */ + private static boolean isPrivateMarker(char c) { + return c >= '\u003c' && c <= '\u003f'; + } + + /** + * @return whether we are inside an escape or control sequence, where a control + * character runs without ending it. Strings (OSC and the ignored ones) + * are not sequences in this sense. + */ + private boolean isInSequence() { + return ansiState == ANSISTATE_ESCAPE || ansiState == ANSISTATE_EXPECTING_PARAMETER_OR_COMMAND + || ansiState == ANSISTATE_EXPECTING_DEC_PRIVATE_COMMAND + || ansiState == ANSISTATE_EXPECTING_CHARSET_DESIGNATION || ansiState == ANSISTATE_IGNORING_COMMAND + || ansiState == ANSISTATE_IGNORING_ESCAPE; + } + + /** + * Runs a C0 control character (or DEL). The ones without a meaning here, DEL + * among them, are dropped, as xterm does, rather than shown. + */ + private void processControlCharacter(char character) { + switch (character) { + case '\u0007': + processBEL(); // BEL (Control-G) + break; + + case '\b': + processBackspace(); // Backspace + break; + + case '\t': + processTab(); // Tab. + break; + + case '\n': + case '\u000b': // VT and FF move down as LF does, as in xterm + case '\u000c': + processNewline(); // Newline (Control-J) + if (fCrAfterNewLine) { + processCarriageReturn(); // Carriage Return (Control-M) + } + break; + + case '\r': + processCarriageReturn(); // Carriage Return (Control-M) + break; + + case '\u000e': + invokedCharset = 1; // SO: G1 + break; + + case '\u000f': + invokedCharset = 0; // SI: G0 + break; + + default: + break; // NUL and the others. Ignore them. + } + } + + /** + * DECALN (ESC # 8): the screen filled with E's, the margins reset and the + * cursor home. + */ + private void screenAlignmentTest() { + text.setScrollRegion(-1, -1); + originMode = false; + text.fillScreen('E'); + moveCursor(0, 0); + } + private void resetTerminal() { // a full reset (ESC c) puts the normal screen back first, history and all, as a // reset before a connection does; else the history stays capped away and a @@ -519,19 +825,39 @@ private void resetTerminal() { text.setStyle(text.getDefaultStyle()); text.eraseAll(); text.setCursor(0, 0); - text.setScrollRegion(-1, -1); - text.setInsertMode(false); - terminal.enableApplicationCursorKeys(false); - resetProgramModes(); + resetModes(); } /** - * The modes a program asked for end with a full reset and with its connection: a - * program that hid the cursor and was cut off must not leave the next shell - * without one. + * A soft reset (DECSTR, CSI ! p), as xterm does it: the modes a program changes + * go back to their defaults, the screen and the history stay. A full reset does + * all of this and more. */ - private void resetProgramModes() { + private void softReset() { + text.setAutoWrap(true); + text.setInsertMode(false); + text.setScrollRegion(-1, -1); + text.setStyle(text.getDefaultStyle()); + originMode = false; + lineDrawing[0] = false; + lineDrawing[1] = false; + invokedCharset = 0; + saved[cursorSlot()] = null; terminal.showCursor(true); + terminal.enableApplicationCursorKeys(false); + } + + /** what a full reset and a new connection put back besides the screen */ + private void resetModes() { + softReset(); + tabStops.clear(); + tabStopsColumns = 0; + programTabStops = false; + lastGraphic = null; + repeatable = null; + // a later ESC 8 must not bring back a cursor, a pending wrap, a rendition or a + // character set from before: every slot is as if nothing had been saved + Arrays.fill(saved, null); terminal.enableBracketedPaste(false); } @@ -541,7 +867,7 @@ private void resetProgramModes() { * title. */ private void processAnsiOsCommand() { - if (ansiOsCommand.charAt(0) != '0' || ansiOsCommand.charAt(1) != ';') { + if (ansiOsCommand.length() < 2 || ansiOsCommand.charAt(0) != '0' || ansiOsCommand.charAt(1) != ';') { Logger.log("Ignoring unsupported ANSI OSC sequence: '" + ansiOsCommand + "'"); //$NON-NLS-1$ //$NON-NLS-2$ return; } @@ -679,9 +1005,61 @@ private void processAnsiCommandCharacter(char ansiCommandCharacter) { break; case 'Z': - // Cursor back tab. - // Emacs, vi, and GNU readline don't seem to use this command, so we ignore - // it for now. + // Cursor Backward Tabulation (CBT) + for (int i = Math.min(getAnsiCount(0), text.getColumns()); i > 0; i--) { + moveCursor(relativeCursorLine(), previousTabStop(getCursorColumn())); + } + break; + + case 'I': + // Cursor Forward Tabulation (CHT) + for (int i = Math.min(getAnsiCount(0), text.getColumns()); i > 0; i--) { + processTab(); + } + break; + + case 'b': + // Repeat the preceding graphic character (REP) + if (repeatable != null) { + String c = repeatable; + // written as often as asked, as in xterm: what scrolls off goes into the + // history. The count is MAX_PARAMETER at most + displayNewText(c.repeat(getAnsiCount(0))); + lastGraphic = c; + } + break; + + case 'c': + // Primary Device Attributes (DA1): a VT100 with advanced video, as xterm in VT100 mode + if (isAnsiParameterDefault(0)) { + sendReply("\u001b[?1;2c"); //$NON-NLS-1$ + } + break; + + case 'f': + // Horizontal and Vertical Position (HVP), as CUP + processAnsiCommand_H(); + break; + + case 'g': + // Tab Clear (TBC): 0 the stop at the cursor, 3 all of them + ensureTabStops(); + if (getAnsiParameter(0) == 3) { + tabStops.clear(); + programTabStops = true; + } else if (isAnsiParameterDefault(0)) { + tabStops.clear(getCursorColumn()); + } + break; + + case 's': + // Save cursor (SCOSC), as ESC 7 + saveCursor(); + break; + + case 'u': + // Restore cursor (SCORC), as ESC 8 + restoreCursor(); break; default: @@ -722,7 +1100,7 @@ private void processDecPrivateCommandCharacter(char commandCharacter) { * of the line. */ private void processAnsiCommand_atsign() { - int charactersToInsert = getAnsiParameter(0); + int charactersToInsert = getAnsiCount(0); text.insertCharacters(charactersToInsert); } @@ -731,7 +1109,7 @@ private void processAnsiCommand_atsign() { * escape sequence parameter (default 1). */ private void processAnsiCommand_A() { - moveCursorUp(getAnsiParameter(0)); + moveCursorUp(getAnsiCount(0)); } /** @@ -739,7 +1117,7 @@ private void processAnsiCommand_A() { * escape sequence parameter (default 1). */ private void processAnsiCommand_B() { - moveCursorDown(getAnsiParameter(0)); + moveCursorDown(getAnsiCount(0)); } /** @@ -747,7 +1125,7 @@ private void processAnsiCommand_B() { * by the escape sequence parameter (default 1). */ private void processAnsiCommand_C() { - moveCursorForward(getAnsiParameter(0)); + moveCursorForward(getAnsiCount(0)); } /** @@ -755,7 +1133,7 @@ private void processAnsiCommand_C() { * by the escape sequence parameter (default 1). */ private void processAnsiCommand_D() { - moveCursorBackward(getAnsiParameter(0)); + moveCursorBackward(getAnsiCount(0)); } /** @@ -763,7 +1141,7 @@ private void processAnsiCommand_D() { */ private void processAnsiCommand_d() { // Line Position Absolute [row] (default = [1,column]) (VPA). - text.setCursorLine(getAnsiParameter(0) - 1); + moveCursor(originLine(getAnsiCount(0) - 1), getCursorColumn()); } /** @@ -771,9 +1149,9 @@ private void processAnsiCommand_d() { * where N is specified by the ANSI parameter (default 1). */ private void processAnsiCommand_E() { - int linesToMove = getAnsiParameter(0); + int linesToMove = getAnsiCount(0); - moveCursor(relativeCursorLine() + linesToMove, 0); + moveCursor(lineDown(linesToMove), 0); } /** @@ -781,9 +1159,9 @@ private void processAnsiCommand_E() { * line, where N is specified by the ANSI parameter (default 1). */ private void processAnsiCommand_F() { - int linesToMove = getAnsiParameter(0); + int linesToMove = getAnsiCount(0); - moveCursor(relativeCursorLine() - linesToMove, 0); + moveCursor(lineUp(linesToMove), 0); } /** @@ -791,7 +1169,7 @@ private void processAnsiCommand_F() { * specified by the ANSI parameter (default is column 1). */ private void processAnsiCommand_G() { - moveCursor(relativeCursorLine(), getAnsiParameter(0) - 1); + moveCursor(relativeCursorLine(), getAnsiCount(0) - 1); } /** @@ -799,7 +1177,7 @@ private void processAnsiCommand_G() { * sequence parameters (default is the upper left corner of the screen). */ private void processAnsiCommand_H() { - moveCursor(getAnsiParameter(0) - 1, getAnsiParameter(1) - 1); + moveCursor(originLine(getAnsiCount(0) - 1), getAnsiCount(1) - 1); } /** @@ -890,7 +1268,7 @@ private void processAnsiCommand_K() { * that falls off the bottom of the screen is deleted. */ private void processAnsiCommand_L() { - text.insertLines(getAnsiParameter(0)); + text.insertLines(getAnsiCount(0)); } /** @@ -908,7 +1286,7 @@ private void processAnsiCommand_l() { * up, which we implement by appending newlines to the end of the text. */ private void processAnsiCommand_M() { - text.deleteLines(getAnsiParameter(0)); + text.deleteLines(getAnsiCount(0)); } /** @@ -1254,7 +1632,8 @@ private void processAnsiCommand_n() { } else if (getAnsiParameter(0) == 6) { // Send the ANSI cursor position (which is 1-based) to the remote // endpoint. - reply = "\u001b[" + (relativeCursorLine() + 1) + ";" + //$NON-NLS-1$ //$NON-NLS-2$ + int line = relativeCursorLine() - (originMode ? text.getScrollRegionTop() : 0); + reply = "\u001b[" + (line + 1) + ";" + //$NON-NLS-1$ //$NON-NLS-2$ (getCursorColumn() + 1) + "R"; //$NON-NLS-1$ } else { @@ -1262,9 +1641,18 @@ private void processAnsiCommand_n() { return; } + sendReply(reply); + } + + /** Sends a report (DSR, DA) to the remote endpoint. */ + private void sendReply(String reply) { + OutputStream out = terminal.getOutputStream(); + if (out == null) { + return; // not connected + } try { - terminal.getOutputStream().write(reply.getBytes("UTF-8")); //$NON-NLS-1$ - terminal.getOutputStream().flush(); + out.write(reply.getBytes(StandardCharsets.UTF_8)); + out.flush(); } catch (IOException ex) { Logger.log("Caught IOException!"); //$NON-NLS-1$ } @@ -1277,41 +1665,44 @@ private void processAnsiCommand_n() { * right of the cursor column, no text is deleted. */ private void processAnsiCommand_P() { - text.deleteCharacters(getAnsiParameter(0)); + text.deleteCharacters(getAnsiCount(0)); } /** * Set Scrolling Region [top;bottom] (default = full size of window) (DECSTBM). */ private void processAnsiCommand_r() { - int top = 0; - int bottom = 0; - if (ansiParameters[0].length() > 0 && ansiParameters[1].length() > 0) { - top = getAnsiParameter(0); - bottom = getAnsiParameter(1); + // either may be left out (or 0) for the top or bottom of the screen, as in xterm + int lines = text.getLines(); + int top = isAnsiParameterDefault(0) ? 1 : getAnsiParameter(0); + int bottom = isAnsiParameterDefault(1) || getAnsiParameter(1) > lines ? lines : getAnsiParameter(1); + if (bottom <= top) { + return; // no region: ignored } - text.setScrollRegion(top - 1, bottom - 1); + text.setScrollRegion(top == 1 && bottom == lines ? -1 : top - 1, bottom - 1); + // the cursor goes home, as on a VT100: the top of the region in origin mode + moveCursor(originLine(0), 0); } /** * Scroll up n lines (default = 1 line). */ private void processAnsiCommand_S() { - text.scrollUp(getAnsiParameter(0)); + text.scrollUp(getAnsiCount(0)); } /** * Scroll down n lines (default = 1 line). */ private void processAnsiCommand_T() { - text.scrollDown(getAnsiParameter(0)); + text.scrollDown(getAnsiCount(0)); } /** * Erases n characters from cursor (default = 1 character) */ private void processAnsiCommand_X() { - text.eraseCharacters(getAnsiParameter(0)); + text.eraseCharacters(getAnsiCount(0)); } /** @@ -1337,6 +1728,15 @@ private void setDecPrivateMode(int param) { // Enable Application Cursor Keys (DECCKM) terminal.enableApplicationCursorKeys(true); break; + case 6: + // Origin Mode (DECOM): the cursor goes to the top of the scroll region + originMode = true; + moveCursor(originLine(0), 0); + break; + case 7: + // Auto Wrap Mode (DECAWM) + text.setAutoWrap(true); + break; case 25: // Show cursor (DECTCEM). terminal.showCursor(true); @@ -1348,7 +1748,13 @@ private void setDecPrivateMode(int param) { alternateScreen = true; break; case 1049: - // Save the cursor as DECSC does, then use Alternate Screen Buffer. + // Save the cursor as DECSC does, then use Alternate Screen Buffer: the + // rendition, the origin mode and the character sets go with it, as with ESC 7, + // but into a slot of their own, so that ESC 8 on the normal screen still + // brings back what ESC 7 saved there. + if (!alternateScreen) { + save(SCREEN_SWITCH); + } text.enableAlternateScreen(true, true); alternateScreen = true; break; @@ -1376,6 +1782,15 @@ private void resetDecPrivateMode(int param) { // Enable Normal Cursor Keys (DECCKM) terminal.enableApplicationCursorKeys(false); break; + case 6: + // Origin Mode (DECOM) off: the cursor goes to the top of the screen + originMode = false; + moveCursor(0, 0); + break; + case 7: + // Auto Wrap Mode (DECAWM) off + text.setAutoWrap(false); + break; case 25: // Hide cursor (DECTCEM). terminal.showCursor(false); @@ -1389,9 +1804,15 @@ private void resetDecPrivateMode(int param) { alternateScreen = false; break; case 1049: - // Use Normal Screen Buffer and restore the cursor saved on the way in. + // Use Normal Screen Buffer and restore the cursor saved on the way in, as + // DECRC does, with all that went with it. + boolean wasAlternate = alternateScreen; text.enableAlternateScreen(false, true); alternateScreen = false; + if (wasAlternate) { + // the backend has put the cursor back, where a resize meanwhile left it + restoreModes(SCREEN_SWITCH); + } break; case 1048: // Restore cursor, as ESC 8 does. @@ -1410,8 +1831,9 @@ private void resetDecPrivateMode(int param) { * This method returns one of the numeric ANSI parameters received in the * most recent escape sequence. * - * @return The parameterIndexth numeric ANSI parameter or -1 if the - * index is out of range or 1 if parse failed (1 is also a legitimate value) + * @return The parameterIndexth numeric ANSI parameter, at most + * MAX_PARAMETER, or -1 if the index is out of range or 1 if it is not a + * number (1 is also a legitimate value) */ private int getAnsiParameter(int parameterIndex) { if (parameterIndex < 0 || parameterIndex >= ansiParameters.length) { @@ -1419,27 +1841,42 @@ private int getAnsiParameter(int parameterIndex) { return -1; } - String parameter = ansiParameters[parameterIndex].toString(); + CharSequence parameter = ansiParameters[parameterIndex]; if (parameter.length() == 0) { return 1; } - // return 1 on failed parseInt - int parameterValue = 1; - - // Don't trust the remote endpoint to send well formed numeric - // parameters. - - try { - parameterValue = Integer.parseInt(parameter); - } catch (NumberFormatException ex) { - parameterValue = 1; + // Don't trust the remote endpoint to send well formed numeric parameters: + // 1 for one that is not a number, and one past MAX_PARAMETER is + // MAX_PARAMETER, as in xterm, so that no count or position runs over + int parameterValue = 0; + for (int i = 0; i < parameter.length(); i++) { + char digit = parameter.charAt(i); + if (digit < '0' || digit > '9') { + return 1; + } + parameterValue = Math.min(parameterValue * 10 + digit - '0', MAX_PARAMETER); } - return parameterValue; } + /** + * @return the parameter as a count or a position, where 0 means the same as + * leaving it out: 1 (ECMA-48) + */ + private int getAnsiCount(int parameterIndex) { + return Math.max(1, getAnsiParameter(parameterIndex)); + } + + /** + * @return whether the parameter is left out or 0, the default of a selective + * parameter such as the one of DA or TBC + */ + private boolean isAnsiParameterDefault(int parameterIndex) { + return ansiParameters[parameterIndex].length() == 0 || getAnsiParameter(parameterIndex) == 0; + } + /** * This method processes a single parameter character in an ANSI escape * sequence. Parameters are the (optional) characters between the leading @@ -1472,20 +1909,40 @@ private void processNonControlCharacters(char character) throws IOException { // firstNonControlCharacterIndex in newText. while (hasNextChar()) { character = getNextChar(); - if (character == '\u0000' || character == '\b' || character == '\t' || character == '\u0007' - || character == '\n' || character == '\r' || character == '\u001b') { + if (isControl(character)) { pushBackChar(character); break; } buffer.append(character); } + if (lineDrawing[invokedCharset]) { + for (int i = 0; i < buffer.length(); i++) { + char c = buffer.charAt(i); + if (c >= '\u005f' && c <= '\u007e') { + buffer.setCharAt(i, LINE_DRAWING.charAt(c - '\u005f')); + } + } + } + // Now insert the sequence of non-control characters in the StyledText widget // at the location of the cursor. - displayNewText(buffer.toString()); + String run = buffer.toString(); + displayNewText(run); + lastGraphic = run.substring(run.offsetByCodePoints(run.length(), -1)); } + /** + * DEC's special graphics for 0x5F-0x7E, as xterm maps them to Unicode: blank, + * diamond, checkerboard, HT FF CR LF symbols, degree, plus-minus, NL VT + * symbols, the box corners and crossing, the scan lines, the tees, the + * lines, less and greater or equal, pi, not equal, pound and centered dot. + */ + private static final String LINE_DRAWING = "\u00a0\u25c6\u2592\u2409\u240c\u240d\u240a\u00b0\u00b1\u2424\u240b" //$NON-NLS-1$ + + "\u2518\u2510\u250c\u2514\u253c\u23ba\u23bb\u2500\u23bc\u23bd\u251c\u2524\u2534\u252c\u2502" //$NON-NLS-1$ + + "\u2264\u2265\u03c0\u2260\u00a3\u00b7"; //$NON-NLS-1$ + /** * This method displays a subset of the newly-received text in the Terminal * view, wrapping text at the right edge of the screen and overwriting text @@ -1520,10 +1977,45 @@ private void processBackspace() { * Process a tab (Control-I) character. We don't insert a tab character into * the StyledText widget. Instead, we move the cursor forward to the next * tab stop, without altering any of the text. Tab stops are every 8 - * columns. The cursor will never move past the rightmost column. + * columns until a program sets its own (HTS, TBC). Without a stop ahead the + * cursor goes to the rightmost column, never past it. */ private void processTab() { - moveCursorForward(8 - (getCursorColumn() % 8)); + ensureTabStops(); + int next = tabStops.nextSetBit(getCursorColumn() + 1); + // no stop ahead, or only ones a narrowed screen keeps past its margin: the margin + int column = next < 0 || next >= text.getColumns() ? text.getColumns() - 1 : next; + if (column != getCursorColumn()) { // staying put keeps a pending wrap, as in xterm + moveCursor(relativeCursorLine(), column); + } + } + + private int previousTabStop(int column) { + ensureTabStops(); + return Math.max(0, tabStops.previousSetBit(column - 1)); + } + + /** one stop every 8 columns on the columns the terminal has gained */ + private void ensureTabStops() { + if (programTabStops) { + return; + } + int columns = text.getColumns(); + for (int c = (tabStopsColumns + 7) / 8 * 8; c < columns; c += 8) { + tabStops.set(c); + } + tabStopsColumns = Math.max(tabStopsColumns, columns); + } + + /** + * @return the screen line of a line number counted in origin mode from the top + * of the scroll region, and kept in it + */ + private int originLine(int line) { + if (!originMode) { + return line; + } + return Math.min(text.getScrollRegionTop() + line, text.getScrollRegionBottom()); } /** @@ -1624,20 +2116,42 @@ private void moveCursor(int targetLine, int targetColumn) { /** * This method moves the cursor down lines lines, but won't move the - * cursor past the bottom of the screen. This method does not cause any - * scrolling. + * cursor past the bottom of the screen, or past the bottom margin from inside + * the scroll region. This method does not cause any scrolling. */ private void moveCursorDown(int lines) { - moveCursor(relativeCursorLine() + lines, getCursorColumn()); + moveCursor(lineDown(lines), getCursorColumn()); + } + + /** + * @return the line a cursor movement down reaches: inside the scroll region it + * stops at the bottom margin, as on a VT100 + */ + private int lineDown(int lines) { + int line = relativeCursorLine(); + int bottom = text.getScrollRegionBottom(); + int limit = line <= bottom ? bottom : text.getLines() - 1; + return Math.min(line + Math.min(lines, text.getLines()), limit); + } + + /** + * @return the line a cursor movement up reaches: inside the scroll region it + * stops at the top margin, as on a VT100 + */ + private int lineUp(int lines) { + int line = relativeCursorLine(); + int top = text.getScrollRegionTop(); + int limit = line >= top ? top : 0; + return Math.max(line - Math.min(lines, text.getLines()), limit); } /** * This method moves the cursor up lines lines, but won't move the - * cursor past the top of the screen. This method does not cause any - * scrolling. + * cursor past the top of the screen, or past the top margin from inside the + * scroll region. This method does not cause any scrolling. */ private void moveCursorUp(int lines) { - moveCursor(relativeCursorLine() - lines, getCursorColumn()); + moveCursor(lineUp(lines), getCursorColumn()); } /** @@ -1670,10 +2184,7 @@ public void resetState() { // back the shell's rendition, which the reset then resets text.enableAlternateScreen(false); alternateScreen = false; - text.setStyle(text.getDefaultStyle()); - text.setScrollRegion(-1, -1); - text.setInsertMode(false); - resetProgramModes(); + resetModes(); } // public OutputStream getOutputStream() { @@ -1730,7 +2241,11 @@ public void setCrAfterNewLine(boolean crAfterNewLine) { fCrAfterNewLine = crAfterNewLine; } - void setVT100LineWrapping(boolean enable) { + /** + * Public for the tests, which run in another bundle: deferred wrapping at the + * right margin, as the connectors set it. + */ + public void setVT100LineWrapping(boolean enable) { text.setVT100LineWrapping(enable); } diff --git a/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/emulator/VT100EmulatorBackend.java b/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/emulator/VT100EmulatorBackend.java index 41c28de6c10..c6a43533149 100644 --- a/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/emulator/VT100EmulatorBackend.java +++ b/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/emulator/VT100EmulatorBackend.java @@ -14,6 +14,8 @@ *******************************************************************************/ package org.eclipse.terminal.internal.emulator; +import java.util.Arrays; + import org.eclipse.terminal.internal.model.CharWidth; import org.eclipse.terminal.internal.model.TerminalTextDataStore; import org.eclipse.terminal.model.ITerminalTextData; @@ -111,6 +113,7 @@ int getHeight() { /** the rendition the shell had selected: 1049 saves it with the cursor, as DECSC does */ private TerminalStyle fNormalStyle; private boolean fVT100LineWrapping; + private boolean fAutoWrap = true; private ScrollRegion fScrollRegion = ScrollRegion.FULL_WINDOW; public VT100EmulatorBackend(ITerminalTextData terminal) { @@ -499,7 +502,8 @@ public void insertLines(int n) { assert n > 0; int line = toAbsoluteLine(fCursorLine); int nLines = Math.min(fTerminal.getHeight() - line, fScrollRegion.getBottomLine() - fCursorLine + 1); - fTerminal.scroll(line, nLines, n); + scrollRegion(line, nLines, n); + setCursorColumn(0); // as on a VT100: the cursor goes to the left margin } } @@ -538,10 +542,21 @@ public void deleteLines(int n) { assert n > 0; int line = toAbsoluteLine(fCursorLine); int nLines = Math.min(fTerminal.getHeight() - line, fScrollRegion.getBottomLine() - fCursorLine + 1); - fTerminal.scroll(line, nLines, -n); + scrollRegion(line, nLines, -n); + setCursorColumn(0); // as on a VT100: the cursor goes to the left margin } } + /** + * Moves {@code size} lines from {@code line} by {@code shift}. A count past the + * region moves everything out of it and stops there, as in xterm: the model + * would otherwise clean as many lines as the count, past the region's end. + * MUST be called from a synchronized block! + */ + private void scrollRegion(int line, int size, int shift) { + fTerminal.scroll(line, size, Math.max(-size, Math.min(shift, size))); + } + private boolean isCusorInScrollingRegion() { return fScrollRegion.contains(fCursorLine); } @@ -584,7 +599,7 @@ public void appendString(String buffer) { int line = toAbsoluteLine(fCursorLine); int i = 0; while (i < chars.length) { - if (fWrapPending) { + if (fWrapPending && fAutoWrap) { // a wrap restored with auto wrap off is not done (xterm) line = doLineWrap(); } int room = fColumns - fCursorColumn; @@ -610,6 +625,11 @@ public void appendString(String buffer) { continue; } if (width > room) { + if (!fAutoWrap && width <= fColumns) { + // no wrap: the character goes in the last cells it fits in + setCursorColumn(fColumns - width); + continue; + } if (fCursorColumn > 0) { // a wide character is never split across the right margin, // and the cell it leaves unused there is erased @@ -641,7 +661,10 @@ public void appendString(String buffer) { } // wrap needed? if (col == fColumns) { - if (fVT100LineWrapping) { + if (!fAutoWrap) { + // the next character replaces this one + setCursorColumn(col - 1); + } else if (fVT100LineWrapping) { // deferred line wrapping (eat_newline_glitch) setCursorColumn(col - 1); fWrapPending = true; @@ -689,7 +712,7 @@ private void breakWideChar(int line, int col) { * printable character beyond the BMP always takes two, since its two chars * cannot share a cell. */ - private static int cellWidth(int codePoint) { + static int cellWidth(int codePoint) { int width = CharWidth.of(codePoint); return width != 0 && Character.charCount(codePoint) == 2 ? 2 : width; } @@ -878,6 +901,58 @@ public boolean isVT100LineWrapping() { return fVT100LineWrapping; } + @Override + public void setAutoWrap(boolean enable) { + synchronized (fTerminal) { + fAutoWrap = enable; + fWrapPending = false; + } + } + + @Override + public int getScrollRegionTop() { + synchronized (fTerminal) { + return Math.min(fScrollRegion.getTopLine(), fLines - 1); + } + } + + @Override + public int getScrollRegionBottom() { + synchronized (fTerminal) { + return Math.min(fScrollRegion.getBottomLine(), fLines - 1); + } + } + + @Override + public boolean isWrapPending() { + synchronized (fTerminal) { + return fWrapPending; + } + } + + @Override + public void setWrapPending(boolean pending) { + synchronized (fTerminal) { + fWrapPending = pending && fCursorColumn == fColumns - 1; + } + } + + @Override + public void fillScreen(char c) { + synchronized (fTerminal) { + char[] row = new char[fColumns]; + Arrays.fill(row, c); + for (int line = 0; line < fLines; line++) { + int absolute = toAbsoluteLine(line); + if (absolute >= 0) { + fTerminal.cleanLine(absolute); + fTerminal.setChars(absolute, 0, row, null); + } + } + fWrapPending = false; + } + } + @Override public void setInsertMode(boolean enable) { fInsertMode = enable; @@ -898,7 +973,7 @@ public void scrollUp(int n) { synchronized (fTerminal) { int line = toAbsoluteLine(fScrollRegion.getTopLine()); int nLines = Math.min(fTerminal.getHeight() - line, fScrollRegion.getHeight()); - fTerminal.scroll(line, nLines, -n); + scrollRegion(line, nLines, -n); } } @@ -908,7 +983,7 @@ public void scrollDown(int n) { synchronized (fTerminal) { int line = toAbsoluteLine(fScrollRegion.getTopLine()); int nLines = Math.min(fTerminal.getHeight() - line, fScrollRegion.getHeight()); - fTerminal.scroll(line, nLines, n); + scrollRegion(line, nLines, n); } } diff --git a/terminal/features/org.eclipse.terminal.feature/feature.xml b/terminal/features/org.eclipse.terminal.feature/feature.xml index 276e237b4b3..73775fa6ee8 100644 --- a/terminal/features/org.eclipse.terminal.feature/feature.xml +++ b/terminal/features/org.eclipse.terminal.feature/feature.xml @@ -2,7 +2,7 @@ diff --git a/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/emulator/MockTerminalControlForText.java b/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/emulator/MockTerminalControlForText.java index 6601d6e8de5..943490385d4 100644 --- a/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/emulator/MockTerminalControlForText.java +++ b/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/emulator/MockTerminalControlForText.java @@ -9,7 +9,9 @@ *******************************************************************************/ package org.eclipse.terminal.internal.emulator; +import java.io.ByteArrayOutputStream; import java.io.OutputStream; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -50,8 +52,16 @@ public ITerminalConnector getTerminalConnector() { @Override public OutputStream getOutputStream() { - throw new UnsupportedOperationException(); + return output; + } + + private final ByteArrayOutputStream output = new ByteArrayOutputStream(); + /** @return what the emulator sent back (reports), and forgets it */ + public String takeOutput() { + String sent = output.toString(StandardCharsets.UTF_8); + output.reset(); + return sent; } private boolean cursorShown = true; diff --git a/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/emulator/VT100EmulatorTest.java b/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/emulator/VT100EmulatorTest.java index 32366a9f172..65ee1ac0d94 100644 --- a/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/emulator/VT100EmulatorTest.java +++ b/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/emulator/VT100EmulatorTest.java @@ -12,6 +12,7 @@ import static org.junit.jupiter.api.Assertions.assertAll; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; import java.io.IOException; @@ -364,6 +365,9 @@ public void testScrollReverseScrollingRegion() { run(CURSOR_POSITION_TOP_LEFT + "\n"); assertAll(() -> assertCursorLocation(2, 0), () -> assertTextEquals(expected)); run(SCROLL_REGION(2, WINDOW_LINES)); + // setting the margins puts the cursor home, as on a VT100; back to the top margin + assertCursorLocation(1, 0); + run(CURSOR_POSITION(2, 1)); run(SCROLL_REVERSE); expected.add(2, ""); assertAll(() -> assertCursorLocation(2, 0), () -> assertTextEquals(expected)); @@ -399,6 +403,387 @@ public void testMalformedStringTerminator() { () -> assertEquals(List.of("TITLE1", "TITLE2"), control.getAllTitles())); } + @Test + public void testUnsupportedStringsAreNotShown() { + // DCS (an XTGETTCAP query), SOS, PM and APC, each up to its ST + run("a\033P+q544e\033\\b\033Xsos\033\\c\033^pm\033\\d\033_apc\033\\e"); + assertTextEquals("abcde"); + // an ESC inside the string that is not an ST starts a new sequence + run("\033Pdcs\033[2Cf"); + assertTextEquals("abcde f"); + } + + @Test + public void testUnsupportedEscapesAreNotShown() { + // ESC # 6 (double width line), ESC SP F (7-bit controls) and ESC % G (UTF-8) + // have intermediate bytes + run("\033#6a\033 Fb\033%Gc"); + assertTextEquals("abc"); + } + + @Test + public void testUnsupportedControlSequencesAreNotShownOrRun() { + // intermediate bytes: CSI 2 SP q (cursor style), and CSI 1 SP @ (shift left) is not ICH + run("ab", "\033[2 q", "\033[1;1H", "\033[1 @"); + assertAll(() -> assertCursorLocation(0, 0), () -> assertTextEquals("ab")); + // private markers other than '?' are not DEC private modes: CSI = 1049 h + run("\033[>4;1m\033[=1049hc"); + assertTextEquals("cb"); + } + + @Test + public void testControlCharactersRunInsideASequence() { + // as vttest checks: the BS runs, then CSI 2 C goes on + run("ABCD\r", "\033[2\bC", "x"); + assertTextEquals("ABxD"); + // a CR and a LF inside, then CSI 1 A + run("\033[1\r\nAy"); + assertAll(() -> assertTextEquals("yBxD"), () -> assertCursorLocation(0, 1)); + // ESC inside a sequence starts a new one + run("\033[3\033[2Cz"); + assertTextEquals("yBxz"); + } + + @Test + public void testCancelAndSubstituteEndASequence() { + // CSI 12 CAN X: the X is a character, not ECH + run("ab\r\033[12\u0018X"); + assertTextEquals("Xb"); + run("\033]0;never\u001aY"); + assertAll(() -> assertTextEquals("XY"), () -> assertEquals(List.of(), control.getAllTitles())); + } + + @Test + public void testOtherControlCharactersAreNotShown() { + // SO, SI, ENQ and DEL are not characters on the screen; VT and FF move down as LF + run("a\u000eb\u000fc\u0005d\u007fe\u000bf\u000cg"); + assertTextEquals("abcde", " f", " g"); + } + + @Test + public void testDeviceAttributes() { + // DA1: a VT100 with advanced video, as xterm in VT100 mode; DA2: type 0, version 0 + run("\033[c\033[0c\033[>c\033[>0c\033[1c"); + assertEquals("\033[?1;2c\033[?1;2c\033[>0;0;0c\033[>0;0;0c", control.takeOutput()); + } + + @Test + public void testZeroParameterMeansOne() { + // ECMA-48: a count or a position of 0 is the same as leaving it out + run("abcdef\r\033[0C\033[0P"); + assertAll(() -> assertTextEquals("acdef"), () -> assertCursorLocation(0, 1)); + run("\033[0@"); + assertTextEquals("a cdef"); + run("\033[0C\033[0X"); + assertTextEquals("a def"); + run("\033[3;0H\033[0A"); + assertCursorLocation(1, 0); + run("\033[0;0H"); + assertCursorLocation(0, 0); + } + + @Test + public void testIndexAndNextLine() { + // IND goes down a line in the same column, NEL to the start of the next one + run("A\033DB\033EC"); + assertTextEquals("A", " B", "C"); + } + + @Test + public void testTabStops() { + // one every 8 columns to begin with + run("\t1"); + // TBC 3 clears them all, HTS sets one at column 21 + run("\033[3g\033[1;21H\033H\r\t2"); + // CBT goes back to it, CHT past the last stop goes to the right margin + run("\033[Z3\033[1;1H\033[2I4"); + assertTextEquals(" ".repeat(8) + "1" + " ".repeat(11) + "3" + " ".repeat(58) + "4"); + // TBC 0 clears the stop at the cursor only, so a tab goes to the right margin + run("\033[1;21H\033[0g\r\t5"); + assertTextEquals(" ".repeat(8) + "1" + " ".repeat(11) + "3" + " ".repeat(58) + "5"); + } + + @Test + public void testRepeatPrecedingCharacter() { + run("A\033[3b"); + assertTextEquals("AAAA"); + // after a control there is no preceding character to repeat + run("\r\n\033[3bx\033[0b"); + assertTextEquals("AAAA", "xx"); + } + + @Test + public void testHvpAndScoSaveRestore() { + run("\033[3;4fF"); + assertCursorLocation(2, 4); + run("\033[2;2H\033[s\033[10;10H\033[uS"); + assertAll(() -> assertTextEquals("", " S", " F"), () -> assertCursorLocation(1, 2)); + } + + @Test + public void testScreenAlignmentTest() { + data.setMaxHeight(1000); + run("\033[5;10r\033[3;3H\033#8"); + List expected = new ArrayList<>(); + for (int i = 0; i < WINDOW_LINES; i++) { + expected.add("E".repeat(WINDOW_COLUMNS)); + } + assertAll(() -> assertTextEquals(expected), () -> assertCursorLocation(0, 0)); + // the margins are reset: a line feed at the bottom scrolls the whole screen + run(CURSOR_POSITION(WINDOW_LINES, 1) + "\nx"); + expected.add("x"); + assertTextEquals(expected); + } + + @Test + public void testAutoWrapOff() { + // DECAWM off: what goes past the right margin replaces the last character + run("\033[?7l", "x".repeat(WINDOW_COLUMNS + 5), "Y"); + assertAll(() -> assertTextEquals("x".repeat(WINDOW_COLUMNS - 1) + "Y"), + () -> assertCursorLocation(0, WINDOW_COLUMNS - 1)); + // on again, the next character wraps + run("\033[?7hZz"); + assertTextEquals("x".repeat(WINDOW_COLUMNS - 1) + "Z", "z"); + } + + @Test + public void testOriginMode() { + // DECOM: positions count from the top margin and stay inside the region + run("\033[10;12r\033[?6h"); + assertCursorLocation(9, 0); + run("\033[1;1HO\033[5;1HP\033[6n"); + assertAll(() -> assertCursorLocation(11, 1), () -> assertEquals("\033[3;2R", control.takeOutput())); + // off, the cursor goes to the top of the screen + run("\033[?6l"); + assertCursorLocation(0, 0); + run("\033[r"); + List expected = new ArrayList<>(); + for (int i = 0; i < 12; i++) { + expected.add(i == 9 ? "O" : i == 11 ? "P" : ""); + } + assertTextEquals(expected); + } + + @Test + public void testLineDrawing() { + // ESC ( 0 puts DEC's special graphics in G0, ESC ( B ASCII again + run("\033(0lqk\033(B x"); + assertTextEquals("┌─┐ x"); + // SO and SI switch between G1 and G0 + run("\033)0\u000eq\u000fq"); + assertTextEquals("┌─┐ x─q"); + // a full reset puts ASCII back + run("\033(0\033c\033[3Jq"); + assertTextEquals("q"); + } + + @Test + public void testSavedRenditionIsPerScreen() { + // ESC 7 saves the rendition with the cursor, one slot per screen as the cursor + run("\033[31mR\0337\033[0m"); + run(ALTERNATE_SCREEN_ON + "\033[32mG\0337\033[0m\0338g"); + int alternate = data.getHeight() - WINDOW_LINES; + assertEquals(data.getStyle(alternate, 0), data.getStyle(alternate, 1)); + // back on the normal screen, its own slot: red, not the alternate screen's green + run(ALTERNATE_SCREEN_OFF + "\033[0m\0338r"); + int normal = data.getHeight() - WINDOW_LINES; + assertAll(() -> assertTextEquals("Rr"), + () -> assertEquals(data.getStyle(normal, 0), data.getStyle(normal, 1))); + } + + @Test + public void testFullResetForgetsTheSavedRenditionAndCharacterSet() { + run("\033[31m\033(0\0337\033c\0338q\033[0mz"); + int line = data.getHeight() - WINDOW_LINES; + assertAll(() -> assertTextEquals("qz"), () -> assertEquals(data.getStyle(line, 1), data.getStyle(line, 0))); + } + + @Test + public void testNewCursorMovesForgetAPendingWrap() { + emulator.setVT100LineWrapping(true); + // each fills the first line, leaving a wrap pending, then moves: the next + // character goes where the move put it and the line is not marked wrapped + String full = "\033[1;1H" + "w".repeat(WINDOW_COLUMNS); + run(full, "\033[3dv"); // VPA + run(full, "\033[4;80fh"); // HVP + run(full, "\033[5;10r\033[r", "s"); // DECSTBM, the cursor home + String blank = " ".repeat(WINDOW_COLUMNS - 1); + assertAll(() -> assertTextEquals("s" + "w".repeat(WINDOW_COLUMNS - 1), "", blank + "v", blank + "h"), + () -> assertFalse(data.isWrappedLine(0))); + } + + @Test + public void testScreenAlignmentIgnoresTheBackground() { + // DECALN writes in the default rendition, not in the current background + run("\033[42m\033#8"); + assertAll(() -> assertNull(data.getStyle(0, 0)), + () -> assertNull(data.getStyle(WINDOW_LINES - 1, WINDOW_COLUMNS - 1))); + } + + @Test + public void testWideCharactersWithAutoWrapOffAndRepeat() { + // with auto wrap off a wide character at the margin takes the last two cells + run("\033[?7l", "x".repeat(WINDOW_COLUMNS - 1), "한"); + assertTextEquals("x".repeat(WINDOW_COLUMNS - 2) + "한"); + // REP repeats a whole wide character, and a whole surrogate pair + run("\033[?7h\033[2;1H한\033[2b\r\n😀\033[1b"); + assertTextEquals("x".repeat(WINDOW_COLUMNS - 2) + "한", "한 한 한", "😀😀"); + } + + @Test + public void testLineDrawingTakesOneCell() { + run("\033(0lqk\033(B"); + assertCursorLocation(0, 3); + } + + @Test + public void testLineCountsPastTheRegionStayInIt() { + // IL, DL, SU and SD with a count past the scroll region empty the region and + // leave the lines outside it alone, as in xterm + for (String command : List.of("\033[30L", "\033[30M", "\033[30S", "\033[30T", "\033[23L\033[23M")) { + List expected = new ArrayList<>(); + StringBuilder numbered = new StringBuilder("\033[r"); + for (int i = 1; i <= WINDOW_LINES; i++) { + numbered.append(CURSOR_POSITION(i, 1)).append(i); + expected.add(i == 1 || i == WINDOW_LINES ? String.valueOf(i) : ""); + } + run(numbered.toString(), SCROLL_REGION(2, WINDOW_LINES - 1), CURSOR_POSITION(2, 1), command); + assertTextEquals(expected); + } + } + + @Test + public void testCursorMovesStopAtTheMargins() { + run(SCROLL_REGION(5, 10)); + // from inside the region CUD, CUU, CNL and CPL stop at its margins + run(CURSOR_POSITION(7, 3), "\033[20B"); + assertCursorLocation(9, 2); + run("\033[20A"); + assertCursorLocation(4, 2); + run("\033[20E"); + assertCursorLocation(9, 0); + run("\033[20F"); + assertCursorLocation(4, 0); + // from outside it they stop at the screen's edges only + run(CURSOR_POSITION(12, 1), "\033[20B"); + assertCursorLocation(WINDOW_LINES - 1, 0); + run(CURSOR_POSITION(3, 1), "\033[20A"); + assertCursorLocation(0, 0); + } + + @Test + public void testAlternateScreenKeepsTheNormalScreensModes() { + // 1049 saves and restores as DECSC: line drawing and origin mode set on the + // alternate screen do not stay on the normal one + run(ALTERNATE_SCREEN_ON, "\033(0\033[?6h", ALTERNATE_SCREEN_OFF, "\033[1;1Hq"); + assertAll(() -> assertTextEquals("q"), () -> assertCursorLocation(0, 1)); + } + + @Test + public void testLongerCharsetNamesAreNotLineDrawing() { + // ESC ( % 5 and ESC ( " 4 designate sets with longer names: G0 is no longer line drawing + run("\033(0q\033(%5q\033(0q\033(\"4q"); + assertTextEquals("\u2500q\u2500q"); + } + + @Test + public void testOnlyEscHash8IsScreenAlignment() { + run("ab\033# 8c"); + assertTextEquals("abc"); + } + + @Test + public void testFullResetForgetsTheSavedCursor() { + emulator.setVT100LineWrapping(true); + // a full row leaves a wrap pending; ESC 8 after a reset must not bring it back + run("w".repeat(WINDOW_COLUMNS), "\0337\033c\0338x"); + assertAll(() -> assertTextEquals("x"), () -> assertCursorLocation(0, 1)); + } + + @Test + public void testRepeatPastAScreenful() { + data.setMaxHeight(1000); + // 1 + 1921 A's: 24 full rows and 2 more, the cursor after them + run("A\033[1921b"); + assertAll(() -> assertEquals(2, data.getCursorColumn()), + () -> assertEquals('A', data.getChar(data.getCursorLine(), 1))); + // a count past 65535 is 65535, as in xterm: 1 + 65535 A's end 16 into a line + long start = System.nanoTime(); + run("\033[2J\033[HA\033[99999b"); + assertAll(() -> assertEquals(16, data.getCursorColumn()), + () -> assertTrue(System.nanoTime() - start < 5_000_000_000L)); + } + + @Test + public void testRepeatAWideCharacterPastAScreenfulOnAnOddWidth() { + emulator.setVT100LineWrapping(true); + emulator.setDimensions(5, 5); + // two per line, the last column left empty: 31 of them end one into the 16th line + run("\u4e2d\033[30b"); + assertAll(() -> assertEquals(2, data.getCursorColumn()), + () -> assertEquals('\u4e2d', data.getChar(data.getCursorLine(), 0)), + () -> assertEquals('\u4e2d', data.getChar(data.getCursorLine() - 1, 2))); + } + + @Test + public void testNinetySixCharacterSetReplacesG1() { + // ESC - designates into G1: the line drawing set before it is gone + run("\033)0\033-A\016q\017"); + assertTextEquals("q"); + } + + @Test + public void testSavedCursorSurvivesTheAlternateScreen() { + // what ESC 7 saved on the normal screen is still there after a 1049 round trip + run("\033(0\033[3;4H\0337\033(B\033[H", ALTERNATE_SCREEN_ON, "\033(B\033[5;5H\0337", ALTERNATE_SCREEN_OFF, + "\0338q"); + assertAll(() -> assertEquals('\u2500', data.getChar(2, 3)), () -> assertCursorLocation(2, 4)); + } + + @Test + public void testTabStopsPastANarrowedScreen() { + emulator.setVT100LineWrapping(true); + run("\t\r"); // the stops are set on 80 columns + emulator.setDimensions(WINDOW_LINES, 10); + // at the margin with a wrap pending, a TAB to a stop past it stays put and keeps + // the wrap: the next character goes on the next line + run("\033[1;1H" + "x".repeat(10), "\ty"); + assertAll(() -> assertEquals(1, data.getCursorColumn()), + () -> assertEquals('y', data.getChar(data.getCursorLine(), 0))); + run("\033[1;1H" + "x".repeat(10), "\033[2Iz"); + assertEquals('z', data.getChar(data.getCursorLine(), 0)); + } + + @Test + public void testLineEditsTabsAndSavedCursorAsInXterm() { + emulator.setVT100LineWrapping(true); + // IL and DL put the cursor on the left margin + run(CURSOR_POSITION(2, 5), "\033[Lx"); + assertCursorLocation(1, 1); + run(CURSOR_POSITION(2, 5), "\033[My"); + assertCursorLocation(1, 1); + // a TAB with no stop ahead keeps a pending wrap, and restoring the cursor puts one back + String full = CURSOR_POSITION(4, 1) + "t".repeat(WINDOW_COLUMNS); + run(full, "\ta"); + assertCursorLocation(4, 1); + run(full, "\0337", CURSOR_POSITION(1, 1), "\0338", "b"); + assertCursorLocation(4, 1); + // but with auto wrap off the next character takes the last column + run(full, "\0337\033[?7l\0338", "c\033[?7h"); + assertAll(() -> assertCursorLocation(3, WINDOW_COLUMNS - 1), + () -> assertEquals('c', data.getChar(3, WINDOW_COLUMNS - 1))); + // in origin mode a saved cursor comes back inside the region + run("\033[r\033[?6h", CURSOR_POSITION(20, 1), "\0337\033[5;10r\0338"); + assertCursorLocation(9, 0); + run("\033[?6l\033[r"); + // DECSTBM: a margin left out is the edge of the screen; a region of one line or none is ignored + run(CURSOR_POSITION(10, 10), "\033[9;3r"); + assertCursorLocation(9, 9); + run(CURSOR_POSITION(8, 1), "above", CURSOR_POSITION(9, 1), "top", "\033[9r", CURSOR_POSITION(9, 1), SCROLL_REVERSE); + assertAll(() -> assertEquals('a', data.getChar(7, 0)), () -> assertEquals('\000', data.getChar(8, 0)), + () -> assertEquals('t', data.getChar(9, 0))); + } + @Test public void testAlternateScreen() { data.setMaxHeight(1000); @@ -631,13 +1016,72 @@ public void testDecPrivateModes() { assertAll(() -> assertTrue(control.isCursorShown()), () -> assertTrue(control.isBracketedPaste())); } + /** + * Both resets, ESC c and the one before a connection, forget everything a program + * set: with every DEC private mode (1 to 9999) and ANSI mode set, then with every + * one reset, and the other state a program can change, a reset emulator must look + * and act as a new one. A mode added later is covered without touching this test. + */ + @Test + public void testResetsForgetEverythingAProgramSet() { + List fresh = probe(); + for (char mode : new char[] { 'h', 'l' }) { + for (String reset : new String[] { "ESC c", "resetState" }) { + before(); + StringBuilder set = new StringBuilder(); + for (int n = 1; n < 10000; n++) { + set.append("\u001b[?").append(n).append(mode); + } + for (int n = 1; n < 100; n++) { + set.append("\u001b[").append(n).append(mode); + } + set.append("\u001b[5;10r\u001b[1;31;4m\u001b(0\u001b)0\u000e"); + set.append("\u001b[3g\u001b[1;5H\u001bH\u001b[2;3H\u001b7\u001b[1;9Hq"); + run(set.toString()); + if (reset.equals("ESC c")) { + run("\u001bc"); + } else { + emulator.resetState(); + } + assertEquals(fresh, probe(), "CSI ? n " + mode + ", then " + reset); + } + } + } + + /** What the emulator shows and tells the control after some text: its state, as far as it shows */ + private List probe() { + run("\u001b[2J\u001b[H\u001b[3b", "aq\u4e2d\tb", "\u001b[1;" + (WINDOW_COLUMNS - 1) + "Hxyz", + "\u001b[3;1Hcd\u001b[3;1He", "\u001b[" + WINDOW_LINES + ";1H\n\n", "\u001b8s"); + List state = new ArrayList<>(); + for (int line = 0; line < data.getHeight(); line++) { + state.add(line + ": " + new String(data.getChars(line) == null ? new char[0] : data.getChars(line))); + for (int column = 0; column < data.getWidth(); column++) { + state.add(data.getStyle(line, column)); + } + } + state.add("cursor " + data.getCursorLine() + "," + data.getCursorColumn()); + for (java.lang.reflect.Method getter : MockTerminalControlForText.class.getDeclaredMethods()) { + Class type = getter.getReturnType(); + if (getter.getParameterCount() == 0 && (type == boolean.class || type == int.class)) { + try { + state.add(getter.getName() + " " + getter.invoke(control)); + } catch (ReflectiveOperationException e) { + throw new AssertionError(e); + } + } + } + return state; + } + @Test public void testResetStateEndsDecPrivateModes() { - run("\u001b[?25l\u001b[?2004h"); - assertAll(() -> assertFalse(control.isCursorShown()), () -> assertTrue(control.isBracketedPaste())); + run("\u001b[?25l\u001b[?2004h\u001b[?1h"); + assertAll(() -> assertFalse(control.isCursorShown()), () -> assertTrue(control.isBracketedPaste()), + () -> assertTrue(control.isApplicationCursorKeys())); // the connection ends while the program has them set; the next one starts clean emulator.resetState(); - assertAll(() -> assertTrue(control.isCursorShown()), () -> assertFalse(control.isBracketedPaste())); + assertAll(() -> assertTrue(control.isCursorShown()), () -> assertFalse(control.isBracketedPaste()), + () -> assertFalse(control.isApplicationCursorKeys())); } @Test @@ -678,4 +1122,108 @@ public void testPasteText() { assertEquals(start + "[20[201~1~x" + end, VT100TerminalControl.pasteText("\033[20\033[201~1~x", true)); assertEquals(start + "201~x" + end, VT100TerminalControl.pasteText("\u009b201~x", true)); } + + @Test + public void testEscEndsAnOscStringAsItEndsTheOthers() { + // an ESC that is not an ST drops the command and starts a new sequence: the SGR + // runs instead of showing as text + run("\033]0;T\033[1mX"); + assertTextEquals("X"); + // and ESC 7 after it saves the cursor, ESC 8 brings it back + run("\033]0;U\0337AB\0338C"); + assertTextEquals("XCB"); + // the controls in a string are dropped, an empty or short one is ignored + run("\033]0;a\nb\007\033]\007\033]0\007D"); + assertEquals(List.of("ab"), control.getAllTitles()); + assertTextEquals("XCD"); + } + + @Test + public void testSoftResetPutsBackTheModes() { + // DECSTR: auto wrap back on, origin mode and margins off + run("\033[?7l\033[2;4r\033[?6h\033[!p", CURSOR_POSITION(1, 1), "X"); + assertCursorLocation(0, 1); + run(CURSOR_POSITION(1, WINDOW_COLUMNS), "YZ"); + assertTextEquals("X" + " ".repeat(WINDOW_COLUMNS - 2) + "Y", "Z"); + // with parameters or another intermediate it is not DECSTR + run("\033[?7l\033[1!p\033[!!p", CURSOR_POSITION(3, WINDOW_COLUMNS), "UV"); + assertTextEquals("X" + " ".repeat(WINDOW_COLUMNS - 2) + "Y", "Z", " ".repeat(WINDOW_COLUMNS - 1) + "V"); + } + + @Test + public void testRepeatOnlyRightAfterTheCharacter() { + // any sequence between the character and REP leaves nothing to repeat + run("A\0337\033[3b", "\r\nB\033[?25h\033[3b", "\r\nC\033]0;x\007\033[3b", "\r\nD\033(0\033(B\033[3b"); + assertTextEquals("A", "B", "C", "D"); + // REP after REP repeats again + run("\r\nE\033[1b\033[1b"); + assertTextEquals("A", "B", "C", "D", "EEE"); + } + + @Test + public void testTabStopsClearedStayClearedOnWiderScreens() { + emulator.setDimensions(WINDOW_LINES, 20); + run("\033[3g"); + emulator.setDimensions(WINDOW_LINES, 40); + // no stop left: the tab goes to the last column + run("\tX"); + assertTextEquals(" ".repeat(39) + "X"); + } + @Test + public void testRestoreWithNothingSavedGivesTheDefaultRendition() { + // ESC 8 with nothing saved brings back the defaults, the rendition too (xterm) + int line = data.getHeight() - WINDOW_LINES; + run("\033[31m\0338x"); + TerminalStyle x = data.getStyle(line, 0); + // and after a full reset, which forgets what was saved + run("\0337\033c\033[31m\0338y"); + assertAll(() -> assertTextEquals("y"), () -> assertEquals(TerminalStyle.getDefaultStyle(), x), + () -> assertEquals(TerminalStyle.getDefaultStyle(), data.getStyle(line, 0))); + } + + /** @return the lines, the cursor and the height that writing the input leaves */ + private static String screenAfter(boolean immediateWrap, String input) throws IOException { + ITerminalTextData text = TerminalTextDataFactory.makeTerminalTextData(); + VT100Emulator e = new VT100Emulator(text, new MockTerminalControlForText(), null); + e.resetState(); + e.setDimensions(4, 5); + text.setMaxHeight(9); + e.setVT100LineWrapping(!immediateWrap); + Reader reader = new StringReader(input); + e.setInputStreamReader(reader); + e.processText(); + StringJoiner lines = new StringJoiner("|"); + for (int i = 0; i < text.getHeight(); i++) { + char[] chars = text.getChars(i); + lines.add(chars == null ? "" : new String(chars)); + } + return lines + " cursor " + text.getCursorLine() + "," + text.getCursorColumn(); + } + + @Test + public void testRepeatWritesWhatTheCharactersWould() throws IOException { + // REP is the character written that many times more: the history and what it evicts too + for (boolean immediate : new boolean[] { false, true }) { + for (String c : new String[] { "A", "\u4e2d" }) { + for (int count = 1; count < 60; count++) { + String history = "h1\r\nh2\r\nh3\r\nh4\r\nh5\r\n"; + assertEquals(screenAfter(immediate, history + c.repeat(count + 1)), + screenAfter(immediate, history + c + "\033[" + count + "b"), c + " " + count + " " + immediate); + } + } + } + } + + @Test + public void testParametersStopAt65535() { + // as in xterm, a parameter past 65535 is 65535: no count or position runs over + String max = Integer.toString(Integer.MAX_VALUE); + run("\033[1;1H\033[" + max + "CX", "\033[2;1H\033[99999999999CY", + "\033[3;1Habcdef\033[3;3H\033[" + max + "@", "\033[4;1Habcdef\033[4;3H\033[" + max + "P", + "\033[5;1Habcdef\033[5;3H\033[" + max + "X"); + assertTextEquals(" ".repeat(WINDOW_COLUMNS - 1) + "X", " ".repeat(WINDOW_COLUMNS - 1) + "Y", "ab", "ab", "ab"); + // in origin mode a far line is the last one of the region + run("\033[10;12r\033[?6h\033[" + max + ";1H\033[6n"); + assertEquals("\033[3;1R", control.takeOutput()); + } }