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..f702cc95837 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; @@ -100,6 +103,27 @@ public class VT100Emulator implements ControlListener { */ 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) * that recognizes ANSI escape sequences. @@ -130,6 +154,51 @@ public class VT100Emulator implements ControlListener { /** 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 ESC 7 saved besides the cursor, one slot per screen as the cursor; and in a + * third, 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. + */ + private static final int SCREEN_SWITCH = 2; + private final TerminalStyle[] savedStyle = new TerminalStyle[3]; + private final boolean[] savedOriginMode = new boolean[3]; + private final boolean[] savedWrapPending = new boolean[2]; + private final boolean[][] savedLineDrawing = new boolean[3][2]; + private final int[] savedInvokedCharset = new int[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; + + /** the last character written, which REP (CSI Ps b) repeats; null after anything else */ + private String lastGraphic; + + /** the private marker of the control sequence being read, '?' or '>' */ + private char ansiMarker; + + /** 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 +280,43 @@ public int getBufferLineLimit() { return text.getBufferLineLimit(); } + /** what DECSC saves besides the cursor, put back: rendition, origin mode, character sets */ + private void restoreModes(int screen) { + if (savedStyle[screen] != null) { + text.setStyle(savedStyle[screen]); + } + originMode = savedOriginMode[screen]; + lineDrawing[0] = savedLineDrawing[screen][0]; + lineDrawing[1] = savedLineDrawing[screen][1]; + invokedCharset = savedInvokedCharset[screen]; + } + private void saveCursor() { int screen = alternateScreen ? 1 : 0; savedCursorLine[screen] = relativeCursorLine(); savedCursorColumn[screen] = getCursorColumn(); + savedWrapPending[screen] = text.isWrapPending(); + saveModes(screen); + } + + /** as DECSC: the rendition, the origin mode and the character sets go with the cursor */ + private void saveModes(int screen) { + savedStyle[screen] = text.getStyle(); + savedOriginMode[screen] = originMode; + savedLineDrawing[screen][0] = lineDrawing[0]; + savedLineDrawing[screen][1] = lineDrawing[1]; + savedInvokedCharset[screen] = invokedCharset; } private void restoreCursor() { int screen = alternateScreen ? 1 : 0; - moveCursor(savedCursorLine[screen], savedCursorColumn[screen]); + restoreModes(screen); + int line = savedCursorLine[screen]; + 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, savedCursorColumn[screen]); + text.setWrapPending(savedWrapPending[screen]); } public void setDimensions(int lines, int cols) { @@ -333,42 +430,29 @@ private void processNewText() throws IOException { while (hasNextChar()) { char character = getNextChar(); + // 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 ((character < '\u0020' || character == '\u007f') && isInSequence()) { + if (character == '\u001b') { + ansiState = ANSISTATE_ESCAPE; + } else { + processControlCharacter(character); + } + continue; + } + 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; - - case '\r': - processCarriageReturn(); // Carriage Return (Control-M) - break; - - case '\u001b': + if (character == '\u001b') { ansiState = ANSISTATE_ESCAPE; // Escape. - break; - - default: + } else if (character < '\u0020' || character == '\u007f') { + processControlCharacter(character); + } else { processNonControlCharacters(character); - break; } break; @@ -380,6 +464,7 @@ private void processNewText() throws IOException { case '[': ansiState = ANSISTATE_EXPECTING_PARAMETER_OR_COMMAND; nextAnsiParameter = 0; + ansiMarker = 0; // Erase the parameter strings in preparation for optional // parameter characters. @@ -402,6 +487,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,17 +540,42 @@ 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 (character >= '\u0020' && character <= '\u002f') { + 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 + // characters `<=>?` mark private commands, before any parameter; DEC's + // '?' ones and the '>' of the secondary device attributes are supported if (character >= '\u003c' && character <= '\u003f') { - ansiState = ANSISTATE_EXPECTING_DEC_PRIVATE_COMMAND; + 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; } @@ -451,6 +585,9 @@ private void processNewText() throws IOException { if (character >= '\u0040' && character < '\u007f') { ansiState = ANSISTATE_INITIAL; processAnsiCommandCharacter(character); + } else if (character >= '\u0020' && character <= '\u002f') { + // an intermediate byte: none of the commands we support has one + ansiState = ANSISTATE_IGNORING_COMMAND; } else { processAnsiParameterCharacter(character); } @@ -484,17 +621,64 @@ private void processNewText() throws IOException { if (character >= '\u0040' && character < '\u007f') { 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 (character >= '\u0020' && character <= '\u002f' + || character >= '\u003c' && character <= '\u003f') { + // 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 (character >= '\u0020' && character <= '\u002f') { + // 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 (character >= '\u0040' && character < '\u007f') { + ansiState = ANSISTATE_INITIAL; + Logger.log("Ignoring unsupported control sequence ending in '" + character + "'"); //$NON-NLS-1$ //$NON-NLS-2$ + } + break; + + case ANSISTATE_IGNORING_ESCAPE: + if (character >= '\u0020' && character <= '\u002f') { + 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 +693,74 @@ private void processNewText() throws IOException { } } + /** + * @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) { + lastGraphic = null; + 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 @@ -521,7 +773,30 @@ private void resetTerminal() { text.setCursor(0, 0); text.setScrollRegion(-1, -1); text.setInsertMode(false); - terminal.enableApplicationCursorKeys(false); + resetModes(); + } + + /** what a full reset and a new connection put back besides the screen */ + private void resetModes() { + text.setAutoWrap(true); + originMode = false; + lineDrawing[0] = false; + lineDrawing[1] = false; + invokedCharset = 0; + tabStops.clear(); + tabStopsColumns = 0; + lastGraphic = 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(savedCursorLine, 0); + Arrays.fill(savedCursorColumn, 0); + Arrays.fill(savedWrapPending, false); + Arrays.fill(savedStyle, null); + Arrays.fill(savedOriginMode, false); + for (boolean[] saved : savedLineDrawing) { + Arrays.fill(saved, false); + } + Arrays.fill(savedInvokedCharset, 0); resetProgramModes(); } @@ -533,6 +808,7 @@ private void resetTerminal() { private void resetProgramModes() { terminal.showCursor(true); terminal.enableBracketedPaste(false); + terminal.enableApplicationCursorKeys(false); } /** @@ -562,6 +838,9 @@ private void processAnsiCommandCharacter(char ansiCommandCharacter) { // throwing an exception (SPR 107450). The display will be messed up, but what // did you user expect by making the terminal so small? + if (ansiCommandCharacter != 'b') { + lastGraphic = null; + } switch (ansiCommandCharacter) { case '@': // Insert character(s). @@ -679,9 +958,65 @@ 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 (lastGraphic != null) { + String c = lastGraphic; + // past a screenful every line written is a line of c scrolled up: whole lines + // of them more change neither the screen nor the cursor, so a huge count + // costs no more than a screenful and a line. A line holds columns / width + // of them: a wide c leaves the last column of an odd width empty + int perLine = Math.max(1, text.getColumns() / Math.max(1, VT100EmulatorBackend.cellWidth(c.codePointAt(0)))); + int screen = text.getLines() * perLine; + int count = getAnsiCount(0); + displayNewText(c.repeat(count <= screen ? count : screen + (count - screen) % perLine)); + 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(); + } 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 +1057,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 +1066,7 @@ private void processAnsiCommand_atsign() { * escape sequence parameter (default 1). */ private void processAnsiCommand_A() { - moveCursorUp(getAnsiParameter(0)); + moveCursorUp(getAnsiCount(0)); } /** @@ -739,7 +1074,7 @@ private void processAnsiCommand_A() { * escape sequence parameter (default 1). */ private void processAnsiCommand_B() { - moveCursorDown(getAnsiParameter(0)); + moveCursorDown(getAnsiCount(0)); } /** @@ -747,7 +1082,7 @@ private void processAnsiCommand_B() { * by the escape sequence parameter (default 1). */ private void processAnsiCommand_C() { - moveCursorForward(getAnsiParameter(0)); + moveCursorForward(getAnsiCount(0)); } /** @@ -755,7 +1090,7 @@ private void processAnsiCommand_C() { * by the escape sequence parameter (default 1). */ private void processAnsiCommand_D() { - moveCursorBackward(getAnsiParameter(0)); + moveCursorBackward(getAnsiCount(0)); } /** @@ -763,7 +1098,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 +1106,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 +1116,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 +1126,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 +1134,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 +1225,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 +1243,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 +1589,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 +1598,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 +1622,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 +1685,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 +1705,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) { + saveModes(SCREEN_SWITCH); + } text.enableAlternateScreen(true, true); alternateScreen = true; break; @@ -1376,6 +1739,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 +1761,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. @@ -1440,6 +1818,22 @@ private int getAnsiParameter(int parameterIndex) { 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 +1866,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 (character < '\u0020' || character == '\u007f') { 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 +1934,42 @@ 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() { + 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 +2070,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()); } /** @@ -1673,7 +2141,7 @@ public void resetState() { text.setStyle(text.getDefaultStyle()); text.setScrollRegion(-1, -1); text.setInsertMode(false); - resetProgramModes(); + resetModes(); } // public OutputStream getOutputStream() { @@ -1730,7 +2198,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 fe9e0b9a2e8..c6229fdff12 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) { @@ -493,7 +496,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 } } @@ -530,10 +534,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); } @@ -576,7 +591,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; @@ -602,6 +617,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 @@ -633,7 +653,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; @@ -681,7 +704,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; } @@ -844,6 +867,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; @@ -864,7 +939,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); } } @@ -874,7 +949,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/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..fb7a5eeb2bd 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 huge count costs a screenful, and leaves the cursor where the whole count would + long start = System.nanoTime(); + run("\033[2J\033[HA\033[99999b"); + assertAll(() -> assertEquals(0, 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