first screenshot attempt
[fanfix.git] / src / jexer / tterminal / ECMA48.java
1 /**
2 * Jexer - Java Text User Interface
3 *
4 * License: LGPLv3 or later
5 *
6 * This module is licensed under the GNU Lesser General Public License
7 * Version 3. Please see the file "COPYING" in this directory for more
8 * information about the GNU Lesser General Public License Version 3.
9 *
10 * Copyright (C) 2015 Kevin Lamonte
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public License
14 * as published by the Free Software Foundation; either version 3 of
15 * the License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this program; if not, see
24 * http://www.gnu.org/licenses/, or write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
26 * 02110-1301 USA
27 *
28 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
29 * @version 1
30 */
31 package jexer.tterminal;
32
33 import java.io.InputStream;
34 import java.io.InputStreamReader;
35 import java.io.IOException;
36 import java.io.OutputStream;
37 import java.io.OutputStreamWriter;
38 import java.io.Reader;
39 import java.io.UnsupportedEncodingException;
40 import java.io.Writer;
41 import java.util.ArrayList;
42 import java.util.Collections;
43 import java.util.LinkedList;
44 import java.util.List;
45
46 import jexer.bits.Color;
47 import jexer.bits.Cell;
48 import jexer.bits.CellAttributes;
49 import jexer.TKeypress;
50 import static jexer.TKeypress.*;
51
52 /**
53 * This implements a complex ANSI ECMA-48/ISO 6429/ANSI X3.64 type consoles,
54 * including a scrollback buffer.
55 *
56 * <p>
57 * It currently implements VT100, VT102, VT220, and XTERM with the following
58 * caveats:
59 *
60 * <p>
61 * - Smooth scrolling, printing, keyboard locking, keyboard leds, and tests
62 * from VT100 are not supported.
63 *
64 * <p>
65 * - User-defined keys (DECUDK), downloadable fonts (DECDLD), and VT100/ANSI
66 * compatibility mode (DECSCL) from VT220 are not supported. (Also,
67 * because DECSCL is not supported, it will fail the last part of the
68 * vttest "Test of VT52 mode" if DeviceType is set to VT220.)
69 *
70 * <p>
71 * - Numeric/application keys from the number pad are not supported because
72 * they are not exposed from the TKeypress API.
73 *
74 * <p>
75 * - VT52 HOLD SCREEN mode is not supported.
76 *
77 * <p>
78 * - In VT52 graphics mode, the 3/, 5/, and 7/ characters (fraction
79 * numerators) are not rendered correctly.
80 *
81 * <p>
82 * - All data meant for the 'printer' (CSI Pc ? i) is discarded.
83 */
84 public class ECMA48 implements Runnable {
85
86 /**
87 * The emulator can emulate several kinds of terminals.
88 */
89 public enum DeviceType {
90 /**
91 * DEC VT100 but also including the three VT102 functions.
92 */
93 VT100,
94
95 /**
96 * DEC VT102.
97 */
98 VT102,
99
100 /**
101 * DEC VT220.
102 */
103 VT220,
104
105 /**
106 * A subset of xterm.
107 */
108 XTERM
109 }
110
111 /**
112 * Return the proper primary Device Attributes string.
113 *
114 * @return string to send to remote side that is appropriate for the
115 * this.type
116 */
117 private String deviceTypeResponse() {
118 switch (type) {
119 case VT100:
120 // "I am a VT100 with advanced video option" (often VT102)
121 return "\033[?1;2c";
122
123 case VT102:
124 // "I am a VT102"
125 return "\033[?6c";
126
127 case VT220:
128 // "I am a VT220" - 7 bit version
129 if (!s8c1t) {
130 return "\033[?62;1;6c";
131 }
132 // "I am a VT220" - 8 bit version
133 return "\u009b?62;1;6c";
134 case XTERM:
135 // "I am a VT100 with advanced video option" (often VT102)
136 return "\033[?1;2c";
137 default:
138 throw new IllegalArgumentException("Invalid device type: " + type);
139 }
140 }
141
142 /**
143 * Return the proper TERM environment variable for this device type.
144 *
145 * @return "TERM=vt100", "TERM=xterm", etc.
146 */
147 public String deviceTypeTerm() {
148 switch (type) {
149 case VT100:
150 return "TERM=vt100";
151
152 case VT102:
153 return "TERM=vt102";
154
155 case VT220:
156 return "TERM=vt220";
157
158 case XTERM:
159 return "TERM=xterm";
160
161 default:
162 throw new IllegalArgumentException("Invalid device type: " + type);
163 }
164 }
165
166 /**
167 * Return the proper LANG for this device type. Only XTERM devices know
168 * about UTF-8, the others are defined by their standard to be either
169 * 7-bit or 8-bit characters only.
170 *
171 * @param baseLang a base language without UTF-8 flag such as "C" or
172 * "en_US"
173 * @return "LANG=en_US", "LANG=en_US.UTF-8", etc.
174 */
175 public String deviceTypeLang(final String baseLang) {
176 switch (type) {
177
178 case VT100:
179 return "LANG=" + baseLang;
180
181 case VT102:
182 return "LANG=" + baseLang;
183
184 case VT220:
185 return "LANG=" + baseLang;
186
187 case XTERM:
188 return "LANG=" + baseLang + ".UTF-8";
189
190 default:
191 throw new IllegalArgumentException("Invalid device type: " + type);
192 }
193 }
194
195 /**
196 * Write a string directly to the remote side.
197 *
198 * @param str string to send
199 */
200 private void writeRemote(final String str) {
201 if (stopReaderThread) {
202 // Reader hit EOF, bail out now.
203 close();
204 return;
205 }
206
207 // System.err.printf("writeRemote() '%s'\n", str);
208
209 switch (type) {
210 case VT100:
211 case VT102:
212 case VT220:
213 if (outputStream == null) {
214 return;
215 }
216 try {
217 for (int i = 0; i < str.length(); i++) {
218 outputStream.write(str.charAt(i));
219 }
220 outputStream.flush();
221 } catch (IOException e) {
222 // Assume EOF
223 close();
224 }
225 break;
226 case XTERM:
227 if (output == null) {
228 return;
229 }
230 try {
231 output.write(str);
232 output.flush();
233 } catch (IOException e) {
234 // Assume EOF
235 close();
236 }
237 break;
238 default:
239 throw new IllegalArgumentException("Invalid device type: " + type);
240 }
241 }
242
243 /**
244 * Close the input and output streams and stop the reader thread. Note
245 * that it is safe to call this multiple times.
246 */
247 public final void close() {
248
249 // Synchronize so we don't stomp on the reader thread.
250 synchronized (this) {
251
252 // Close the input stream
253 switch (type) {
254 case VT100:
255 case VT102:
256 case VT220:
257 if (inputStream != null) {
258 try {
259 inputStream.close();
260 } catch (IOException e) {
261 // SQUASH
262 }
263 inputStream = null;
264 }
265 break;
266 case XTERM:
267 if (input != null) {
268 try {
269 input.close();
270 } catch (IOException e) {
271 // SQUASH
272 }
273 input = null;
274 inputStream = null;
275 }
276 break;
277 }
278
279 // Tell the reader thread to stop looking at input.
280 if (stopReaderThread == false) {
281 stopReaderThread = true;
282 try {
283 readerThread.join();
284 } catch (InterruptedException e) {
285 e.printStackTrace();
286 }
287 }
288
289 // Close the output stream.
290 switch (type) {
291 case VT100:
292 case VT102:
293 case VT220:
294 if (outputStream != null) {
295 try {
296 outputStream.close();
297 } catch (IOException e) {
298 // SQUASH
299 }
300 outputStream = null;
301 }
302 break;
303 case XTERM:
304 if (output != null) {
305 try {
306 output.close();
307 } catch (IOException e) {
308 // SQUASH
309 }
310 output = null;
311 }
312 break;
313 default:
314 throw new IllegalArgumentException("Invalid device type: "
315 + type);
316 }
317 } // synchronized (this)
318 }
319
320 /**
321 * When true, the reader thread is expected to exit.
322 */
323 private volatile boolean stopReaderThread = false;
324
325 /**
326 * The reader thread.
327 */
328 private Thread readerThread = null;
329
330 /**
331 * See if the reader thread is still running.
332 *
333 * @return if true, we are still connected to / reading from the remote
334 * side
335 */
336 public final boolean isReading() {
337 return (!stopReaderThread);
338 }
339
340 /**
341 * The type of emulator to be.
342 */
343 private DeviceType type = DeviceType.VT102;
344
345 /**
346 * Obtain a new blank display line for an external user
347 * (e.g. TTerminalWindow).
348 *
349 * @return new blank line
350 */
351 public final DisplayLine getBlankDisplayLine() {
352 return new DisplayLine(currentState.attr);
353 }
354
355 /**
356 * The scrollback buffer characters + attributes.
357 */
358 private volatile List<DisplayLine> scrollback;
359
360 /**
361 * Get the scrollback buffer.
362 *
363 * @return the scrollback buffer
364 */
365 public final List<DisplayLine> getScrollbackBuffer() {
366 return scrollback;
367 }
368
369 /**
370 * The raw display buffer characters + attributes.
371 */
372 private volatile List<DisplayLine> display;
373
374 /**
375 * Get the display buffer.
376 *
377 * @return the display buffer
378 */
379 public final List<DisplayLine> getDisplayBuffer() {
380 return display;
381 }
382
383 /**
384 * The terminal's input. For type == XTERM, this is an InputStreamReader
385 * with UTF-8 encoding.
386 */
387 private Reader input;
388
389 /**
390 * The terminal's raw InputStream. This is used for type != XTERM.
391 */
392 private volatile InputStream inputStream;
393
394 /**
395 * The terminal's output. For type == XTERM, this wraps an
396 * OutputStreamWriter with UTF-8 encoding.
397 */
398 private Writer output;
399
400 /**
401 * The terminal's raw OutputStream. This is used for type != XTERM.
402 */
403 private OutputStream outputStream;
404
405 /**
406 * Parser character scan states.
407 */
408 enum ScanState {
409 GROUND,
410 ESCAPE,
411 ESCAPE_INTERMEDIATE,
412 CSI_ENTRY,
413 CSI_PARAM,
414 CSI_INTERMEDIATE,
415 CSI_IGNORE,
416 DCS_ENTRY,
417 DCS_INTERMEDIATE,
418 DCS_PARAM,
419 DCS_PASSTHROUGH,
420 DCS_IGNORE,
421 SOSPMAPC_STRING,
422 OSC_STRING,
423 VT52_DIRECT_CURSOR_ADDRESS
424 }
425
426 /**
427 * Current scanning state.
428 */
429 private ScanState scanState;
430
431 /**
432 * The selected number pad mode (DECKPAM, DECKPNM). We record this, but
433 * can't really use it in keypress() because we do not see number pad
434 * events from TKeypress.
435 */
436 private enum KeypadMode {
437 Application,
438 Numeric
439 }
440
441 /**
442 * Arrow keys can emit three different sequences (DECCKM or VT52
443 * submode).
444 */
445 private enum ArrowKeyMode {
446 VT52,
447 ANSI,
448 VT100
449 }
450
451 /**
452 * Available character sets for GL, GR, G0, G1, G2, G3.
453 */
454 private enum CharacterSet {
455 US,
456 UK,
457 DRAWING,
458 ROM,
459 ROM_SPECIAL,
460 VT52_GRAPHICS,
461 DEC_SUPPLEMENTAL,
462 NRC_DUTCH,
463 NRC_FINNISH,
464 NRC_FRENCH,
465 NRC_FRENCH_CA,
466 NRC_GERMAN,
467 NRC_ITALIAN,
468 NRC_NORWEGIAN,
469 NRC_SPANISH,
470 NRC_SWEDISH,
471 NRC_SWISS
472 }
473
474 /**
475 * Single-shift states used by the C1 control characters SS2 (0x8E) and
476 * SS3 (0x8F).
477 */
478 private enum Singleshift {
479 NONE,
480 SS2,
481 SS3
482 }
483
484 /**
485 * VT220+ lockshift states.
486 */
487 private enum LockshiftMode {
488 NONE,
489 G1_GR,
490 G2_GR,
491 G2_GL,
492 G3_GR,
493 G3_GL
494 }
495
496 /**
497 * Physical display width. We start at 80x24, but the user can resize us
498 * bigger/smaller.
499 */
500 private int width;
501
502 /**
503 * Get the display width.
504 *
505 * @return the width (usually 80 or 132)
506 */
507 public final int getWidth() {
508 return width;
509 }
510
511 /**
512 * Physical display height. We start at 80x24, but the user can resize
513 * us bigger/smaller.
514 */
515 private int height;
516
517 /**
518 * Get the display height.
519 *
520 * @return the height (usually 24)
521 */
522 public final int getHeight() {
523 return height;
524 }
525
526 /**
527 * Top margin of the scrolling region.
528 */
529 private int scrollRegionTop;
530
531 /**
532 * Bottom margin of the scrolling region.
533 */
534 private int scrollRegionBottom;
535
536 /**
537 * Right margin column number. This can be selected by the remote side
538 * to be 80/132 (rightMargin values 79/131), or it can be (width - 1).
539 */
540 private int rightMargin;
541
542 /**
543 * Last character printed.
544 */
545 private char repCh;
546
547 /**
548 * VT100-style line wrapping: a character is placed in column 80 (or
549 * 132), but the line does NOT wrap until another character is written to
550 * column 1 of the next line, after which the cursor moves to column 2.
551 */
552 private boolean wrapLineFlag;
553
554 /**
555 * VT220 single shift flag.
556 */
557 private Singleshift singleshift = Singleshift.NONE;
558
559 /**
560 * true = insert characters, false = overwrite.
561 */
562 private boolean insertMode = false;
563
564 /**
565 * VT52 mode as selected by DECANM. True means VT52, false means
566 * ANSI. Default is ANSI.
567 */
568 private boolean vt52Mode = false;
569
570 /**
571 * Visible cursor (DECTCEM).
572 */
573 private boolean cursorVisible = true;
574
575 /**
576 * Get visible cursor flag.
577 *
578 * @return if true, the cursor is visible
579 */
580 public final boolean visibleCursor() {
581 return cursorVisible;
582 }
583
584 /**
585 * Screen title as set by the xterm OSC sequence. Lots of applications
586 * send a screenTitle regardless of whether it is an xterm client or not.
587 */
588 private String screenTitle = "";
589
590 /**
591 * Get the screen title as set by the xterm OSC sequence. Lots of
592 * applications send a screenTitle regardless of whether it is an xterm
593 * client or not.
594 *
595 * @return screen title
596 */
597 public final String getScreenTitle() {
598 return screenTitle;
599 }
600
601 /**
602 * Array of flags that have come in, e.g. '?' (DEC private mode), '=',
603 * '>' (&lt;), ...
604 */
605 private List<Character> csiFlags;
606
607 /**
608 * Parameter characters being collected.
609 */
610 private List<Integer> csiParams;
611
612 /**
613 * Non-csi collect buffer.
614 */
615 private List<Character> collectBuffer;
616
617 /**
618 * When true, use the G1 character set.
619 */
620 private boolean shiftOut = false;
621
622 /**
623 * Horizontal tab stop locations.
624 */
625 private List<Integer> tabStops;
626
627 /**
628 * S8C1T. True means 8bit controls, false means 7bit controls.
629 */
630 private boolean s8c1t = false;
631
632 /**
633 * Printer mode. True means send all output to printer, which discards
634 * it.
635 */
636 private boolean printerControllerMode = false;
637
638 /**
639 * LMN line mode. If true, linefeed() puts the cursor on the first
640 * column of the next line. If false, linefeed() puts the cursor one
641 * line down on the current line. The default is false.
642 */
643 private boolean newLineMode = false;
644
645 /**
646 * Whether arrow keys send ANSI, VT100, or VT52 sequences.
647 */
648 private ArrowKeyMode arrowKeyMode;
649
650 /**
651 * Whether number pad keys send VT100 or VT52, application or numeric
652 * sequences.
653 */
654 private KeypadMode keypadMode;
655
656 /**
657 * When true, the terminal is in 132-column mode (DECCOLM).
658 */
659 private boolean columns132 = false;
660
661 /**
662 * true = reverse video. Set by DECSCNM.
663 */
664 private boolean reverseVideo = false;
665
666 /**
667 * false = echo characters locally.
668 */
669 private boolean fullDuplex = true;
670
671 /**
672 * DECSC/DECRC save/restore a subset of the total state. This class
673 * encapsulates those specific flags/modes.
674 */
675 private class SaveableState {
676
677 /**
678 * When true, cursor positions are relative to the scrolling region.
679 */
680 public boolean originMode = false;
681
682 /**
683 * The current editing X position.
684 */
685 public int cursorX = 0;
686
687 /**
688 * The current editing Y position.
689 */
690 public int cursorY = 0;
691
692 /**
693 * Which character set is currently selected in G0.
694 */
695 public CharacterSet g0Charset = CharacterSet.US;
696
697 /**
698 * Which character set is currently selected in G1.
699 */
700 public CharacterSet g1Charset = CharacterSet.DRAWING;
701
702 /**
703 * Which character set is currently selected in G2.
704 */
705 public CharacterSet g2Charset = CharacterSet.US;
706
707 /**
708 * Which character set is currently selected in G3.
709 */
710 public CharacterSet g3Charset = CharacterSet.US;
711
712 /**
713 * Which character set is currently selected in GR.
714 */
715 public CharacterSet grCharset = CharacterSet.DRAWING;
716
717 /**
718 * The current drawing attributes.
719 */
720 public CellAttributes attr;
721
722 /**
723 * GL lockshift mode.
724 */
725 public LockshiftMode glLockshift = LockshiftMode.NONE;
726
727 /**
728 * GR lockshift mode.
729 */
730 public LockshiftMode grLockshift = LockshiftMode.NONE;
731
732 /**
733 * Line wrap.
734 */
735 public boolean lineWrap = true;
736
737 /**
738 * Reset to defaults.
739 */
740 public void reset() {
741 originMode = false;
742 cursorX = 0;
743 cursorY = 0;
744 g0Charset = CharacterSet.US;
745 g1Charset = CharacterSet.DRAWING;
746 g2Charset = CharacterSet.US;
747 g3Charset = CharacterSet.US;
748 grCharset = CharacterSet.DRAWING;
749 attr = new CellAttributes();
750 glLockshift = LockshiftMode.NONE;
751 grLockshift = LockshiftMode.NONE;
752 lineWrap = true;
753 }
754
755 /**
756 * Copy attributes from another instance.
757 *
758 * @param that the other instance to match
759 */
760 public void setTo(final SaveableState that) {
761 this.originMode = that.originMode;
762 this.cursorX = that.cursorX;
763 this.cursorY = that.cursorY;
764 this.g0Charset = that.g0Charset;
765 this.g1Charset = that.g1Charset;
766 this.g2Charset = that.g2Charset;
767 this.g3Charset = that.g3Charset;
768 this.grCharset = that.grCharset;
769 this.attr = new CellAttributes();
770 this.attr.setTo(that.attr);
771 this.glLockshift = that.glLockshift;
772 this.grLockshift = that.grLockshift;
773 this.lineWrap = that.lineWrap;
774 }
775
776 /**
777 * Public constructor.
778 */
779 public SaveableState() {
780 reset();
781 }
782 }
783
784 /**
785 * The current terminal state.
786 */
787 private SaveableState currentState;
788
789 /**
790 * The last saved terminal state.
791 */
792 private SaveableState savedState;
793
794 /**
795 * Clear the CSI parameters and flags.
796 */
797 private void toGround() {
798 csiParams.clear();
799 csiFlags.clear();
800 collectBuffer.clear();
801 scanState = ScanState.GROUND;
802 }
803
804 /**
805 * Reset the tab stops list.
806 */
807 private void resetTabStops() {
808 tabStops.clear();
809 for (int i = 0; (i * 8) <= rightMargin; i++) {
810 tabStops.add(new Integer(i * 8));
811 }
812 }
813
814 /**
815 * Reset the emulation state.
816 */
817 private void reset() {
818
819 currentState = new SaveableState();
820 savedState = new SaveableState();
821 scanState = ScanState.GROUND;
822 width = 80;
823 height = 24;
824 scrollRegionTop = 0;
825 scrollRegionBottom = height - 1;
826 rightMargin = width - 1;
827 newLineMode = false;
828 arrowKeyMode = ArrowKeyMode.ANSI;
829 keypadMode = KeypadMode.Numeric;
830 wrapLineFlag = false;
831
832 // Flags
833 shiftOut = false;
834 vt52Mode = false;
835 insertMode = false;
836 columns132 = false;
837 newLineMode = false;
838 reverseVideo = false;
839 fullDuplex = true;
840 cursorVisible = true;
841
842 // VT220
843 singleshift = Singleshift.NONE;
844 s8c1t = false;
845 printerControllerMode = false;
846
847 // Tab stops
848 resetTabStops();
849
850 // Clear CSI stuff
851 toGround();
852 }
853
854 /**
855 * Public constructor.
856 *
857 * @param type one of the DeviceType constants to select VT100, VT102,
858 * VT220, or XTERM
859 * @param inputStream an InputStream connected to the remote side. For
860 * type == XTERM, inputStrem is converted to a Reader with UTF-8
861 * encoding.
862 * @param outputStream an OutputStream connected to the remote user. For
863 * type == XTERM, outputStream is converted to a Writer with UTF-8
864 * encoding.
865 * @throws UnsupportedEncodingException if an exception is thrown when
866 * creating the InputStreamReader
867 */
868 public ECMA48(final DeviceType type, final InputStream inputStream,
869 final OutputStream outputStream) throws UnsupportedEncodingException {
870
871 assert (inputStream != null);
872 assert (outputStream != null);
873
874 csiFlags = new ArrayList<Character>();
875 csiParams = new ArrayList<Integer>();
876 collectBuffer = new ArrayList<Character>();
877 tabStops = new ArrayList<Integer>();
878 scrollback = new LinkedList<DisplayLine>();
879 display = new LinkedList<DisplayLine>();
880
881 this.type = type;
882 this.inputStream = inputStream;
883 if (type == DeviceType.XTERM) {
884 this.input = new InputStreamReader(inputStream, "UTF-8");
885 this.output = new OutputStreamWriter(outputStream, "UTF-8");
886 this.outputStream = null;
887 } else {
888 this.output = null;
889 this.outputStream = outputStream;
890 }
891
892 reset();
893 for (int i = 0; i < height; i++) {
894 display.add(new DisplayLine(currentState.attr));
895 }
896
897 // Spin up the input reader
898 readerThread = new Thread(this);
899 readerThread.start();
900 }
901
902 /**
903 * Append a new line to the bottom of the display, adding lines off the
904 * top to the scrollback buffer.
905 */
906 private void newDisplayLine() {
907 // Scroll the top line off into the scrollback buffer
908 scrollback.add(display.get(0));
909 display.remove(0);
910 DisplayLine line = new DisplayLine(currentState.attr);
911 line.setReverseColor(reverseVideo);
912 display.add(line);
913 }
914
915 /**
916 * Wraps the current line.
917 */
918 private void wrapCurrentLine() {
919 if (currentState.cursorY == height - 1) {
920 newDisplayLine();
921 }
922 if (currentState.cursorY < height - 1) {
923 currentState.cursorY++;
924 }
925 currentState.cursorX = 0;
926 }
927
928 /**
929 * Handle a carriage return.
930 */
931 private void carriageReturn() {
932 currentState.cursorX = 0;
933 wrapLineFlag = false;
934 }
935
936 /**
937 * Reverse the color of the visible display.
938 */
939 private void invertDisplayColors() {
940 for (DisplayLine line: display) {
941 line.setReverseColor(!line.isReverseColor());
942 }
943 }
944
945 /**
946 * Handle a linefeed.
947 */
948 private void linefeed() {
949 int i;
950
951 if (currentState.cursorY < scrollRegionBottom) {
952 // Increment screen y
953 currentState.cursorY++;
954 } else {
955
956 // Screen y does not increment
957
958 /*
959 * Two cases: either we're inside a scrolling region or not. If
960 * the scrolling region bottom is the bottom of the screen, then
961 * push the top line into the buffer. Else scroll the scrolling
962 * region up.
963 */
964 if ((scrollRegionBottom == height - 1) && (scrollRegionTop == 0)) {
965
966 // We're at the bottom of the scroll region, AND the scroll
967 // region is the entire screen.
968
969 // New line
970 newDisplayLine();
971
972 } else {
973 // We're at the bottom of the scroll region, AND the scroll
974 // region is NOT the entire screen.
975 scrollingRegionScrollUp(scrollRegionTop, scrollRegionBottom, 1);
976 }
977 }
978
979 if (newLineMode) {
980 currentState.cursorX = 0;
981 }
982 wrapLineFlag = false;
983 }
984
985 /**
986 * Prints one character to the display buffer.
987 *
988 * @param ch character to display
989 */
990 private void printCharacter(final char ch) {
991 int rightMargin = this.rightMargin;
992
993 // Check if we have double-width, and if so chop at 40/66 instead of
994 // 80/132
995 if (display.get(currentState.cursorY).isDoubleWidth()) {
996 rightMargin = ((rightMargin + 1) / 2) - 1;
997 }
998
999 // Check the unusually-complicated line wrapping conditions...
1000 if (currentState.cursorX == rightMargin) {
1001
1002 if (currentState.lineWrap == true) {
1003 /*
1004 * This case happens when: the cursor was already on the
1005 * right margin (either through printing or by an explicit
1006 * placement command), and a character was printed.
1007 *
1008 * The line wraps only when a new character arrives AND the
1009 * cursor is already on the right margin AND has placed a
1010 * character in its cell. Easier to see than to explain.
1011 */
1012 if (wrapLineFlag == false) {
1013 /*
1014 * This block marks the case that we are in the margin
1015 * and the first character has been received and printed.
1016 */
1017 wrapLineFlag = true;
1018 } else {
1019 /*
1020 * This block marks the case that we are in the margin
1021 * and the second character has been received and
1022 * printed.
1023 */
1024 wrapLineFlag = false;
1025 wrapCurrentLine();
1026 }
1027 }
1028 } else if (currentState.cursorX <= rightMargin) {
1029 /*
1030 * This is the normal case: a character came in and was printed
1031 * to the left of the right margin column.
1032 */
1033
1034 // Turn off VT100 special-case flag
1035 wrapLineFlag = false;
1036 }
1037
1038 // "Print" the character
1039 Cell newCell = new Cell(ch);
1040 CellAttributes newCellAttributes = (CellAttributes) newCell;
1041 newCellAttributes.setTo(currentState.attr);
1042 DisplayLine line = display.get(currentState.cursorY);
1043 // Insert mode special case
1044 if (insertMode == true) {
1045 line.insert(currentState.cursorX, newCell);
1046 } else {
1047 // Replace an existing character
1048 line.replace(currentState.cursorX, newCell);
1049 }
1050
1051 // Increment horizontal
1052 if (wrapLineFlag == false) {
1053 currentState.cursorX++;
1054 if (currentState.cursorX > rightMargin) {
1055 currentState.cursorX--;
1056 }
1057 }
1058 }
1059
1060 /**
1061 * Translate the keyboard press to a VT100, VT220, or XTERM sequence and
1062 * send to the remote side.
1063 *
1064 * @param keypress keypress received from the local user
1065 */
1066 public void keypress(final TKeypress keypress) {
1067 writeRemote(keypressToString(keypress));
1068 }
1069
1070 /**
1071 * Translate the keyboard press to a VT100, VT220, or XTERM sequence.
1072 *
1073 * @param keypress keypress received from the local user
1074 * @return string to transmit to the remote side
1075 */
1076 private String keypressToString(final TKeypress keypress) {
1077
1078 if ((fullDuplex == false) && (!keypress.getIsKey())) {
1079 /*
1080 * If this is a control character, process it like it came from
1081 * the remote side.
1082 */
1083 if (keypress.getCh() < 0x20) {
1084 handleControlChar(keypress.getCh());
1085 } else {
1086 // Local echo for everything else
1087 printCharacter(keypress.getCh());
1088 }
1089 }
1090
1091 if ((newLineMode == true) && (keypress.equals(kbEnter))) {
1092 // NLM: send CRLF
1093 return "\015\012";
1094 }
1095
1096 // Handle control characters
1097 if ((keypress.getCtrl()) && (!keypress.getIsKey())) {
1098 StringBuilder sb = new StringBuilder();
1099 char ch = keypress.getCh();
1100 ch -= 0x40;
1101 sb.append(ch);
1102 return sb.toString();
1103 }
1104
1105 // Handle alt characters
1106 if ((keypress.getAlt()) && (!keypress.getIsKey())) {
1107 StringBuilder sb = new StringBuilder("\033");
1108 char ch = keypress.getCh();
1109 sb.append(ch);
1110 return sb.toString();
1111 }
1112
1113 if (keypress.equals(kbBackspace)) {
1114 switch (type) {
1115 case VT100:
1116 return "\010";
1117 case VT102:
1118 return "\010";
1119 case VT220:
1120 return "\177";
1121 case XTERM:
1122 return "\177";
1123 }
1124 }
1125
1126 if (keypress.equals(kbLeft)) {
1127 switch (arrowKeyMode) {
1128 case ANSI:
1129 return "\033[D";
1130 case VT52:
1131 return "\033D";
1132 case VT100:
1133 return "\033OD";
1134 }
1135 }
1136
1137 if (keypress.equals(kbRight)) {
1138 switch (arrowKeyMode) {
1139 case ANSI:
1140 return "\033[C";
1141 case VT52:
1142 return "\033C";
1143 case VT100:
1144 return "\033OC";
1145 }
1146 }
1147
1148 if (keypress.equals(kbUp)) {
1149 switch (arrowKeyMode) {
1150 case ANSI:
1151 return "\033[A";
1152 case VT52:
1153 return "\033A";
1154 case VT100:
1155 return "\033OA";
1156 }
1157 }
1158
1159 if (keypress.equals(kbDown)) {
1160 switch (arrowKeyMode) {
1161 case ANSI:
1162 return "\033[B";
1163 case VT52:
1164 return "\033B";
1165 case VT100:
1166 return "\033OB";
1167 }
1168 }
1169
1170 if (keypress.equals(kbHome)) {
1171 switch (arrowKeyMode) {
1172 case ANSI:
1173 return "\033[H";
1174 case VT52:
1175 return "\033H";
1176 case VT100:
1177 return "\033OH";
1178 }
1179 }
1180
1181 if (keypress.equals(kbEnd)) {
1182 switch (arrowKeyMode) {
1183 case ANSI:
1184 return "\033[F";
1185 case VT52:
1186 return "\033F";
1187 case VT100:
1188 return "\033OF";
1189 }
1190 }
1191
1192 if (keypress.equals(kbF1)) {
1193 // PF1
1194 if (vt52Mode) {
1195 return "\033P";
1196 }
1197 return "\033OP";
1198 }
1199
1200 if (keypress.equals(kbF2)) {
1201 // PF2
1202 if (vt52Mode) {
1203 return "\033Q";
1204 }
1205 return "\033OQ";
1206 }
1207
1208 if (keypress.equals(kbF3)) {
1209 // PF3
1210 if (vt52Mode) {
1211 return "\033R";
1212 }
1213 return "\033OR";
1214 }
1215
1216 if (keypress.equals(kbF4)) {
1217 // PF4
1218 if (vt52Mode) {
1219 return "\033S";
1220 }
1221 return "\033OS";
1222 }
1223
1224 if (keypress.equals(kbF5)) {
1225 switch (type) {
1226 case VT100:
1227 return "\033Ot";
1228 case VT102:
1229 return "\033Ot";
1230 case VT220:
1231 return "\033[15~";
1232 case XTERM:
1233 return "\033[15~";
1234 }
1235 }
1236
1237 if (keypress.equals(kbF6)) {
1238 switch (type) {
1239 case VT100:
1240 return "\033Ou";
1241 case VT102:
1242 return "\033Ou";
1243 case VT220:
1244 return "\033[17~";
1245 case XTERM:
1246 return "\033[17~";
1247 }
1248 }
1249
1250 if (keypress.equals(kbF7)) {
1251 switch (type) {
1252 case VT100:
1253 return "\033Ov";
1254 case VT102:
1255 return "\033Ov";
1256 case VT220:
1257 return "\033[18~";
1258 case XTERM:
1259 return "\033[18~";
1260 }
1261 }
1262
1263 if (keypress.equals(kbF8)) {
1264 switch (type) {
1265 case VT100:
1266 return "\033Ol";
1267 case VT102:
1268 return "\033Ol";
1269 case VT220:
1270 return "\033[19~";
1271 case XTERM:
1272 return "\033[19~";
1273 }
1274 }
1275
1276 if (keypress.equals(kbF9)) {
1277 switch (type) {
1278 case VT100:
1279 return "\033Ow";
1280 case VT102:
1281 return "\033Ow";
1282 case VT220:
1283 return "\033[20~";
1284 case XTERM:
1285 return "\033[20~";
1286 }
1287 }
1288
1289 if (keypress.equals(kbF10)) {
1290 switch (type) {
1291 case VT100:
1292 return "\033Ox";
1293 case VT102:
1294 return "\033Ox";
1295 case VT220:
1296 return "\033[21~";
1297 case XTERM:
1298 return "\033[21~";
1299 }
1300 }
1301
1302 if (keypress.equals(kbF11)) {
1303 return "\033[23~";
1304 }
1305
1306 if (keypress.equals(kbF12)) {
1307 return "\033[24~";
1308 }
1309
1310 if (keypress.equals(kbShiftF1)) {
1311 // Shifted PF1
1312 if (vt52Mode) {
1313 return "\0332P";
1314 }
1315 return "\033O2P";
1316 }
1317
1318 if (keypress.equals(kbShiftF2)) {
1319 // Shifted PF2
1320 if (vt52Mode) {
1321 return "\0332Q";
1322 }
1323 return "\033O2Q";
1324 }
1325
1326 if (keypress.equals(kbShiftF3)) {
1327 // Shifted PF3
1328 if (vt52Mode) {
1329 return "\0332R";
1330 }
1331 return "\033O2R";
1332 }
1333
1334 if (keypress.equals(kbShiftF4)) {
1335 // Shifted PF4
1336 if (vt52Mode) {
1337 return "\0332S";
1338 }
1339 return "\033O2S";
1340 }
1341
1342 if (keypress.equals(kbShiftF5)) {
1343 // Shifted F5
1344 return "\033[15;2~";
1345 }
1346
1347 if (keypress.equals(kbShiftF6)) {
1348 // Shifted F6
1349 return "\033[17;2~";
1350 }
1351
1352 if (keypress.equals(kbShiftF7)) {
1353 // Shifted F7
1354 return "\033[18;2~";
1355 }
1356
1357 if (keypress.equals(kbShiftF8)) {
1358 // Shifted F8
1359 return "\033[19;2~";
1360 }
1361
1362 if (keypress.equals(kbShiftF9)) {
1363 // Shifted F9
1364 return "\033[20;2~";
1365 }
1366
1367 if (keypress.equals(kbShiftF10)) {
1368 // Shifted F10
1369 return "\033[21;2~";
1370 }
1371
1372 if (keypress.equals(kbShiftF11)) {
1373 // Shifted F11
1374 return "\033[23;2~";
1375 }
1376
1377 if (keypress.equals(kbShiftF12)) {
1378 // Shifted F12
1379 return "\033[24;2~";
1380 }
1381
1382 if (keypress.equals(kbCtrlF1)) {
1383 // Control PF1
1384 if (vt52Mode) {
1385 return "\0335P";
1386 }
1387 return "\033O5P";
1388 }
1389
1390 if (keypress.equals(kbCtrlF2)) {
1391 // Control PF2
1392 if (vt52Mode) {
1393 return "\0335Q";
1394 }
1395 return "\033O5Q";
1396 }
1397
1398 if (keypress.equals(kbCtrlF3)) {
1399 // Control PF3
1400 if (vt52Mode) {
1401 return "\0335R";
1402 }
1403 return "\033O5R";
1404 }
1405
1406 if (keypress.equals(kbCtrlF4)) {
1407 // Control PF4
1408 if (vt52Mode) {
1409 return "\0335S";
1410 }
1411 return "\033O5S";
1412 }
1413
1414 if (keypress.equals(kbCtrlF5)) {
1415 // Control F5
1416 return "\033[15;5~";
1417 }
1418
1419 if (keypress.equals(kbCtrlF6)) {
1420 // Control F6
1421 return "\033[17;5~";
1422 }
1423
1424 if (keypress.equals(kbCtrlF7)) {
1425 // Control F7
1426 return "\033[18;5~";
1427 }
1428
1429 if (keypress.equals(kbCtrlF8)) {
1430 // Control F8
1431 return "\033[19;5~";
1432 }
1433
1434 if (keypress.equals(kbCtrlF9)) {
1435 // Control F9
1436 return "\033[20;5~";
1437 }
1438
1439 if (keypress.equals(kbCtrlF10)) {
1440 // Control F10
1441 return "\033[21;5~";
1442 }
1443
1444 if (keypress.equals(kbCtrlF11)) {
1445 // Control F11
1446 return "\033[23;5~";
1447 }
1448
1449 if (keypress.equals(kbCtrlF12)) {
1450 // Control F12
1451 return "\033[24;5~";
1452 }
1453
1454 if (keypress.equals(kbPgUp)) {
1455 // Page Up
1456 return "\033[5~";
1457 }
1458
1459 if (keypress.equals(kbPgDn)) {
1460 // Page Down
1461 return "\033[6~";
1462 }
1463
1464 if (keypress.equals(kbIns)) {
1465 // Ins
1466 return "\033[2~";
1467 }
1468
1469 if (keypress.equals(kbShiftIns)) {
1470 // This is what xterm sends for SHIFT-INS
1471 return "\033[2;2~";
1472 // This is what xterm sends for CTRL-INS
1473 // return "\033[2;5~";
1474 }
1475
1476 if (keypress.equals(kbShiftDel)) {
1477 // This is what xterm sends for SHIFT-DEL
1478 return "\033[3;2~";
1479 // This is what xterm sends for CTRL-DEL
1480 // return "\033[3;5~";
1481 }
1482
1483 if (keypress.equals(kbDel)) {
1484 // Delete sends real delete for VTxxx
1485 return "\177";
1486 // return "\033[3~";
1487 }
1488
1489 if (keypress.equals(kbEnter)) {
1490 return "\015";
1491 }
1492
1493 if (keypress.equals(kbEsc)) {
1494 return "\033";
1495 }
1496
1497 if (keypress.equals(kbAltEsc)) {
1498 return "\033\033";
1499 }
1500
1501 if (keypress.equals(kbTab)) {
1502 return "\011";
1503 }
1504
1505 // Non-alt, non-ctrl characters
1506 if (!keypress.getIsKey()) {
1507 StringBuilder sb = new StringBuilder();
1508 sb.append(keypress.getCh());
1509 return sb.toString();
1510 }
1511 return "";
1512 }
1513
1514 /**
1515 * Map a symbol in any one of the VT100/VT220 character sets to a Unicode
1516 * symbol.
1517 *
1518 * @param ch 8-bit character from the remote side
1519 * @param charsetGl character set defined for GL
1520 * @param charsetGr character set defined for GR
1521 * @return character to display on the screen
1522 */
1523 private char mapCharacterCharset(final char ch,
1524 final CharacterSet charsetGl,
1525 final CharacterSet charsetGr) {
1526
1527 int lookupChar = ch;
1528 CharacterSet lookupCharset = charsetGl;
1529
1530 if (ch >= 0x80) {
1531 assert ((type == DeviceType.VT220) || (type == DeviceType.XTERM));
1532 lookupCharset = charsetGr;
1533 lookupChar &= 0x7F;
1534 }
1535
1536 switch (lookupCharset) {
1537
1538 case DRAWING:
1539 return DECCharacterSets.SPECIAL_GRAPHICS[lookupChar];
1540
1541 case UK:
1542 return DECCharacterSets.UK[lookupChar];
1543
1544 case US:
1545 return DECCharacterSets.US_ASCII[lookupChar];
1546
1547 case NRC_DUTCH:
1548 return DECCharacterSets.NL[lookupChar];
1549
1550 case NRC_FINNISH:
1551 return DECCharacterSets.FI[lookupChar];
1552
1553 case NRC_FRENCH:
1554 return DECCharacterSets.FR[lookupChar];
1555
1556 case NRC_FRENCH_CA:
1557 return DECCharacterSets.FR_CA[lookupChar];
1558
1559 case NRC_GERMAN:
1560 return DECCharacterSets.DE[lookupChar];
1561
1562 case NRC_ITALIAN:
1563 return DECCharacterSets.IT[lookupChar];
1564
1565 case NRC_NORWEGIAN:
1566 return DECCharacterSets.NO[lookupChar];
1567
1568 case NRC_SPANISH:
1569 return DECCharacterSets.ES[lookupChar];
1570
1571 case NRC_SWEDISH:
1572 return DECCharacterSets.SV[lookupChar];
1573
1574 case NRC_SWISS:
1575 return DECCharacterSets.SWISS[lookupChar];
1576
1577 case DEC_SUPPLEMENTAL:
1578 return DECCharacterSets.DEC_SUPPLEMENTAL[lookupChar];
1579
1580 case VT52_GRAPHICS:
1581 return DECCharacterSets.VT52_SPECIAL_GRAPHICS[lookupChar];
1582
1583 case ROM:
1584 return DECCharacterSets.US_ASCII[lookupChar];
1585
1586 case ROM_SPECIAL:
1587 return DECCharacterSets.US_ASCII[lookupChar];
1588
1589 default:
1590 throw new IllegalArgumentException("Invalid character set value: "
1591 + lookupCharset);
1592 }
1593 }
1594
1595 /**
1596 * Map an 8-bit byte into a printable character.
1597 *
1598 * @param ch either 8-bit or Unicode character from the remote side
1599 * @return character to display on the screen
1600 */
1601 private char mapCharacter(final char ch) {
1602 if (ch >= 0x100) {
1603 // Unicode character, just return it
1604 return ch;
1605 }
1606
1607 CharacterSet charsetGl = currentState.g0Charset;
1608 CharacterSet charsetGr = currentState.grCharset;
1609
1610 if (vt52Mode == true) {
1611 if (shiftOut == true) {
1612 // Shifted out character, pull from VT52 graphics
1613 charsetGl = currentState.g1Charset;
1614 charsetGr = CharacterSet.US;
1615 } else {
1616 // Normal
1617 charsetGl = currentState.g0Charset;
1618 charsetGr = CharacterSet.US;
1619 }
1620
1621 // Pull the character
1622 return mapCharacterCharset(ch, charsetGl, charsetGr);
1623 }
1624
1625 // shiftOout
1626 if (shiftOut == true) {
1627 // Shifted out character, pull from G1
1628 charsetGl = currentState.g1Charset;
1629 charsetGr = currentState.grCharset;
1630
1631 // Pull the character
1632 return mapCharacterCharset(ch, charsetGl, charsetGr);
1633 }
1634
1635 // SS2
1636 if (singleshift == Singleshift.SS2) {
1637
1638 singleshift = Singleshift.NONE;
1639
1640 // Shifted out character, pull from G2
1641 charsetGl = currentState.g2Charset;
1642 charsetGr = currentState.grCharset;
1643 }
1644
1645 // SS3
1646 if (singleshift == Singleshift.SS3) {
1647
1648 singleshift = Singleshift.NONE;
1649
1650 // Shifted out character, pull from G3
1651 charsetGl = currentState.g3Charset;
1652 charsetGr = currentState.grCharset;
1653 }
1654
1655 if ((type == DeviceType.VT220) || (type == DeviceType.XTERM)) {
1656 // Check for locking shift
1657
1658 switch (currentState.glLockshift) {
1659
1660 case G1_GR:
1661 assert (false);
1662
1663 case G2_GR:
1664 assert (false);
1665
1666 case G3_GR:
1667 assert (false);
1668
1669 case G2_GL:
1670 // LS2
1671 charsetGl = currentState.g2Charset;
1672 break;
1673
1674 case G3_GL:
1675 // LS3
1676 charsetGl = currentState.g3Charset;
1677 break;
1678
1679 case NONE:
1680 // Normal
1681 charsetGl = currentState.g0Charset;
1682 break;
1683 }
1684
1685 switch (currentState.grLockshift) {
1686
1687 case G2_GL:
1688 assert (false);
1689
1690 case G3_GL:
1691 assert (false);
1692
1693 case G1_GR:
1694 // LS1R
1695 charsetGr = currentState.g1Charset;
1696 break;
1697
1698 case G2_GR:
1699 // LS2R
1700 charsetGr = currentState.g2Charset;
1701 break;
1702
1703 case G3_GR:
1704 // LS3R
1705 charsetGr = currentState.g3Charset;
1706 break;
1707
1708 case NONE:
1709 // Normal
1710 charsetGr = CharacterSet.DEC_SUPPLEMENTAL;
1711 break;
1712 }
1713
1714
1715 }
1716
1717 // Pull the character
1718 return mapCharacterCharset(ch, charsetGl, charsetGr);
1719 }
1720
1721 /**
1722 * Scroll the text within a scrolling region up n lines.
1723 *
1724 * @param regionTop top row of the scrolling region
1725 * @param regionBottom bottom row of the scrolling region
1726 * @param n number of lines to scroll
1727 */
1728 private void scrollingRegionScrollUp(final int regionTop,
1729 final int regionBottom, final int n) {
1730
1731 if (regionTop >= regionBottom) {
1732 return;
1733 }
1734
1735 // Sanity check: see if there will be any characters left after the
1736 // scroll
1737 if (regionBottom + 1 - regionTop <= n) {
1738 // There won't be anything left in the region, so just call
1739 // eraseScreen() and return.
1740 eraseScreen(regionTop, 0, regionBottom, width - 1, false);
1741 return;
1742 }
1743
1744 int remaining = regionBottom + 1 - regionTop - n;
1745 List<DisplayLine> displayTop = display.subList(0, regionTop);
1746 List<DisplayLine> displayBottom = display.subList(regionBottom + 1,
1747 display.size());
1748 List<DisplayLine> displayMiddle = display.subList(regionBottom + 1
1749 - remaining, regionBottom + 1);
1750 display = new LinkedList<DisplayLine>(displayTop);
1751 display.addAll(displayMiddle);
1752 for (int i = 0; i < n; i++) {
1753 DisplayLine line = new DisplayLine(currentState.attr);
1754 line.setReverseColor(reverseVideo);
1755 display.add(line);
1756 }
1757 display.addAll(displayBottom);
1758
1759 assert (display.size() == height);
1760 }
1761
1762 /**
1763 * Scroll the text within a scrolling region down n lines.
1764 *
1765 * @param regionTop top row of the scrolling region
1766 * @param regionBottom bottom row of the scrolling region
1767 * @param n number of lines to scroll
1768 */
1769 private void scrollingRegionScrollDown(final int regionTop,
1770 final int regionBottom, final int n) {
1771
1772 if (regionTop >= regionBottom) {
1773 return;
1774 }
1775
1776 // Sanity check: see if there will be any characters left after the
1777 // scroll
1778 if (regionBottom + 1 - regionTop <= n) {
1779 // There won't be anything left in the region, so just call
1780 // eraseScreen() and return.
1781 eraseScreen(regionTop, 0, regionBottom, width - 1, false);
1782 return;
1783 }
1784
1785 int remaining = regionBottom + 1 - regionTop - n;
1786 List<DisplayLine> displayTop = display.subList(0, regionTop);
1787 List<DisplayLine> displayBottom = display.subList(regionBottom + 1,
1788 display.size());
1789 List<DisplayLine> displayMiddle = display.subList(regionTop,
1790 regionTop + remaining);
1791 display = new LinkedList<DisplayLine>(displayTop);
1792 for (int i = 0; i < n; i++) {
1793 DisplayLine line = new DisplayLine(currentState.attr);
1794 line.setReverseColor(reverseVideo);
1795 display.add(line);
1796 }
1797 display.addAll(displayMiddle);
1798 display.addAll(displayBottom);
1799
1800 assert (display.size() == height);
1801 }
1802
1803 /**
1804 * Process a control character.
1805 *
1806 * @param ch 8-bit character from the remote side
1807 */
1808 private void handleControlChar(final char ch) {
1809 assert ((ch <= 0x1F) || ((ch >= 0x7F) && (ch <= 0x9F)));
1810
1811 switch (ch) {
1812
1813 case 0x00:
1814 // NUL - discard
1815 return;
1816
1817 case 0x05:
1818 // ENQ
1819
1820 // Transmit the answerback message.
1821 // Not supported
1822 break;
1823
1824 case 0x07:
1825 // BEL
1826 // Not supported
1827 break;
1828
1829 case 0x08:
1830 // BS
1831 cursorLeft(1, false);
1832 break;
1833
1834 case 0x09:
1835 // HT
1836 advanceToNextTabStop();
1837 break;
1838
1839 case 0x0A:
1840 // LF
1841 linefeed();
1842 break;
1843
1844 case 0x0B:
1845 // VT
1846 linefeed();
1847 break;
1848
1849 case 0x0C:
1850 // FF
1851 linefeed();
1852 break;
1853
1854 case 0x0D:
1855 // CR
1856 carriageReturn();
1857 break;
1858
1859 case 0x0E:
1860 // SO
1861 shiftOut = true;
1862 currentState.glLockshift = LockshiftMode.NONE;
1863 break;
1864
1865 case 0x0F:
1866 // SI
1867 shiftOut = false;
1868 currentState.glLockshift = LockshiftMode.NONE;
1869 break;
1870
1871 case 0x84:
1872 // IND
1873 ind();
1874 break;
1875
1876 case 0x85:
1877 // NEL
1878 nel();
1879 break;
1880
1881 case 0x88:
1882 // HTS
1883 hts();
1884 break;
1885
1886 case 0x8D:
1887 // RI
1888 ri();
1889 break;
1890
1891 case 0x8E:
1892 // SS2
1893 singleshift = Singleshift.SS2;
1894 break;
1895
1896 case 0x8F:
1897 // SS3
1898 singleshift = Singleshift.SS3;
1899 break;
1900
1901 default:
1902 break;
1903 }
1904
1905 }
1906
1907 /**
1908 * Advance the cursor to the next tab stop.
1909 */
1910 private void advanceToNextTabStop() {
1911 if (tabStops.size() == 0) {
1912 // Go to the rightmost column
1913 cursorRight(rightMargin - currentState.cursorX, false);
1914 return;
1915 }
1916 for (Integer stop: tabStops) {
1917 if (stop > currentState.cursorX) {
1918 cursorRight(stop - currentState.cursorX, false);
1919 return;
1920 }
1921 }
1922 /*
1923 * We got here, meaning there isn't a tab stop beyond the current
1924 * cursor position. Place the cursor of the right-most edge of the
1925 * screen.
1926 */
1927 cursorRight(rightMargin - currentState.cursorX, false);
1928 }
1929
1930 /**
1931 * Save a character into the collect buffer.
1932 *
1933 * @param ch character to save
1934 */
1935 private void collect(final char ch) {
1936 collectBuffer.add(new Character(ch));
1937 }
1938
1939 /**
1940 * Save a byte into the CSI parameters buffer.
1941 *
1942 * @param ch byte to save
1943 */
1944 private void param(final byte ch) {
1945 if (csiParams.size() == 0) {
1946 csiParams.add(new Integer(0));
1947 }
1948 Integer x = csiParams.get(csiParams.size() - 1);
1949 if ((ch >= '0') && (ch <= '9')) {
1950 x *= 10;
1951 x += (ch - '0');
1952 csiParams.set(csiParams.size() - 1, x);
1953 }
1954
1955 if (ch == ';') {
1956 csiParams.add(new Integer(0));
1957 }
1958 }
1959
1960 /**
1961 * Get a CSI parameter value, with a default.
1962 *
1963 * @param position parameter index. 0 is the first parameter.
1964 * @param defaultValue value to use if csiParams[position] doesn't exist
1965 * @return parameter value
1966 */
1967 private int getCsiParam(final int position, final int defaultValue) {
1968 if (csiParams.size() < position + 1) {
1969 return defaultValue;
1970 }
1971 return csiParams.get(position).intValue();
1972 }
1973
1974 /**
1975 * Get a CSI parameter value, clamped to within min/max.
1976 *
1977 * @param position parameter index. 0 is the first parameter.
1978 * @param defaultValue value to use if csiParams[position] doesn't exist
1979 * @param minValue minimum value inclusive
1980 * @param maxValue maximum value inclusive
1981 * @return parameter value
1982 */
1983 private int getCsiParam(final int position, final int defaultValue,
1984 final int minValue, final int maxValue) {
1985
1986 assert (minValue <= maxValue);
1987 int value = getCsiParam(position, defaultValue);
1988 if (value < minValue) {
1989 value = minValue;
1990 }
1991 if (value > maxValue) {
1992 value = maxValue;
1993 }
1994 return value;
1995 }
1996
1997 /**
1998 * Set or unset a toggle. value is 'true' for set ('h'), false for reset
1999 * ('l').
2000 */
2001 private void setToggle(final boolean value) {
2002 boolean decPrivateModeFlag = false;
2003 for (Character ch: collectBuffer) {
2004 if (ch == '?') {
2005 decPrivateModeFlag = true;
2006 }
2007 }
2008
2009 for (Integer i: csiParams) {
2010
2011 switch (i) {
2012
2013 case 1:
2014 if (decPrivateModeFlag == true) {
2015 // DECCKM
2016 if (value == true) {
2017 // Use application arrow keys
2018 arrowKeyMode = ArrowKeyMode.VT100;
2019 } else {
2020 // Use ANSI arrow keys
2021 arrowKeyMode = ArrowKeyMode.ANSI;
2022 }
2023 }
2024 break;
2025 case 2:
2026 if (decPrivateModeFlag == true) {
2027 if (value == false) {
2028
2029 // DECANM
2030 vt52Mode = true;
2031 arrowKeyMode = ArrowKeyMode.VT52;
2032
2033 /*
2034 * From the VT102 docs: "You use ANSI mode to select
2035 * most terminal features; the terminal uses the same
2036 * features when it switches to VT52 mode. You
2037 * cannot, however, change most of these features in
2038 * VT52 mode."
2039 *
2040 * In other words, do not reset any other attributes
2041 * when switching between VT52 submode and ANSI.
2042 *
2043 * HOWEVER, the real vt100 does switch the character
2044 * set according to Usenet.
2045 */
2046 currentState.g0Charset = CharacterSet.US;
2047 currentState.g1Charset = CharacterSet.DRAWING;
2048 shiftOut = false;
2049
2050 if ((type == DeviceType.VT220)
2051 || (type == DeviceType.XTERM)) {
2052
2053 // VT52 mode is explicitly 7-bit
2054 s8c1t = false;
2055 singleshift = Singleshift.NONE;
2056 }
2057 }
2058 } else {
2059 // KAM
2060 if (value == true) {
2061 // Turn off keyboard
2062 // Not supported
2063 } else {
2064 // Turn on keyboard
2065 // Not supported
2066 }
2067 }
2068 break;
2069 case 3:
2070 if (decPrivateModeFlag == true) {
2071 // DECCOLM
2072 if (value == true) {
2073 // 132 columns
2074 columns132 = true;
2075 rightMargin = 131;
2076 } else {
2077 // 80 columns
2078 columns132 = false;
2079 rightMargin = 79;
2080 }
2081 width = rightMargin + 1;
2082 // Entire screen is cleared, and scrolling region is
2083 // reset
2084 eraseScreen(0, 0, height - 1, width - 1, false);
2085 scrollRegionTop = 0;
2086 scrollRegionBottom = height - 1;
2087 // Also home the cursor
2088 cursorPosition(0, 0);
2089 }
2090 break;
2091 case 4:
2092 if (decPrivateModeFlag == true) {
2093 // DECSCLM
2094 if (value == true) {
2095 // Smooth scroll
2096 // Not supported
2097 } else {
2098 // Jump scroll
2099 // Not supported
2100 }
2101 } else {
2102 // IRM
2103 if (value == true) {
2104 insertMode = true;
2105 } else {
2106 insertMode = false;
2107 }
2108 }
2109 break;
2110 case 5:
2111 if (decPrivateModeFlag == true) {
2112 // DECSCNM
2113 if (value == true) {
2114 /*
2115 * Set selects reverse screen, a white screen
2116 * background with black characters.
2117 */
2118 if (reverseVideo != true) {
2119 /*
2120 * If in normal video, switch it back
2121 */
2122 invertDisplayColors();
2123 }
2124 reverseVideo = true;
2125 } else {
2126 /*
2127 * Reset selects normal screen, a black screen
2128 * background with white characters.
2129 */
2130 if (reverseVideo == true) {
2131 /*
2132 * If in reverse video already, switch it back
2133 */
2134 invertDisplayColors();
2135 }
2136 reverseVideo = false;
2137 }
2138 }
2139 break;
2140 case 6:
2141 if (decPrivateModeFlag == true) {
2142 // DECOM
2143 if (value == true) {
2144 // Origin is relative to scroll region cursor.
2145 // Cursor can NEVER leave scrolling region.
2146 currentState.originMode = true;
2147 cursorPosition(0, 0);
2148 } else {
2149 // Origin is absolute to entire screen. Cursor can
2150 // leave the scrolling region via cup() and hvp().
2151 currentState.originMode = false;
2152 cursorPosition(0, 0);
2153 }
2154 }
2155 break;
2156 case 7:
2157 if (decPrivateModeFlag == true) {
2158 // DECAWM
2159 if (value == true) {
2160 // Turn linewrap on
2161 currentState.lineWrap = true;
2162 } else {
2163 // Turn linewrap off
2164 currentState.lineWrap = false;
2165 }
2166 }
2167 break;
2168 case 8:
2169 if (decPrivateModeFlag == true) {
2170 // DECARM
2171 if (value == true) {
2172 // Keyboard auto-repeat on
2173 // Not supported
2174 } else {
2175 // Keyboard auto-repeat off
2176 // Not supported
2177 }
2178 }
2179 break;
2180 case 12:
2181 if (decPrivateModeFlag == false) {
2182 // SRM
2183 if (value == true) {
2184 // Local echo off
2185 fullDuplex = true;
2186 } else {
2187 // Local echo on
2188 fullDuplex = false;
2189 }
2190 }
2191 break;
2192 case 18:
2193 if (decPrivateModeFlag == true) {
2194 // DECPFF
2195 // Not supported
2196 }
2197 break;
2198 case 19:
2199 if (decPrivateModeFlag == true) {
2200 // DECPEX
2201 // Not supported
2202 }
2203 break;
2204 case 20:
2205 if (decPrivateModeFlag == false) {
2206 // LNM
2207 if (value == true) {
2208 /*
2209 * Set causes a received linefeed, form feed, or
2210 * vertical tab to move cursor to first column of
2211 * next line. RETURN transmits both a carriage return
2212 * and linefeed. This selection is also called new
2213 * line option.
2214 */
2215 newLineMode = true;
2216 } else {
2217 /*
2218 * Reset causes a received linefeed, form feed, or
2219 * vertical tab to move cursor to next line in
2220 * current column. RETURN transmits a carriage
2221 * return.
2222 */
2223 newLineMode = false;
2224 }
2225 }
2226 break;
2227
2228 case 25:
2229 if ((type == DeviceType.VT220) || (type == DeviceType.XTERM)) {
2230 if (decPrivateModeFlag == true) {
2231 // DECTCEM
2232 if (value == true) {
2233 // Visible cursor
2234 cursorVisible = true;
2235 } else {
2236 // Invisible cursor
2237 cursorVisible = false;
2238 }
2239 }
2240 }
2241 break;
2242
2243 case 42:
2244 if ((type == DeviceType.VT220) || (type == DeviceType.XTERM)) {
2245 if (decPrivateModeFlag == true) {
2246 // DECNRCM
2247 if (value == true) {
2248 // Select national mode NRC
2249 // Not supported
2250 } else {
2251 // Select multi-national mode
2252 // Not supported
2253 }
2254 }
2255 }
2256
2257 break;
2258
2259 default:
2260 break;
2261
2262 }
2263 }
2264 }
2265
2266 /**
2267 * DECSC - Save cursor.
2268 */
2269 private void decsc() {
2270 savedState.setTo(currentState);
2271 }
2272
2273 /**
2274 * DECRC - Restore cursor.
2275 */
2276 private void decrc() {
2277 currentState.setTo(savedState);
2278 }
2279
2280 /**
2281 * IND - Index.
2282 */
2283 private void ind() {
2284 // Move the cursor and scroll if necessary. If at the bottom line
2285 // already, a scroll up is supposed to be performed.
2286 if (currentState.cursorY == scrollRegionBottom) {
2287 scrollingRegionScrollUp(scrollRegionTop, scrollRegionBottom, 1);
2288 }
2289 cursorDown(1, true);
2290 }
2291
2292 /**
2293 * RI - Reverse index.
2294 */
2295 private void ri() {
2296 // Move the cursor and scroll if necessary. If at the top line
2297 // already, a scroll down is supposed to be performed.
2298 if (currentState.cursorY == scrollRegionTop) {
2299 scrollingRegionScrollDown(scrollRegionTop, scrollRegionBottom, 1);
2300 }
2301 cursorUp(1, true);
2302 }
2303
2304 /**
2305 * NEL - Next line.
2306 */
2307 private void nel() {
2308 // Move the cursor and scroll if necessary. If at the bottom line
2309 // already, a scroll up is supposed to be performed.
2310 if (currentState.cursorY == scrollRegionBottom) {
2311 scrollingRegionScrollUp(scrollRegionTop, scrollRegionBottom, 1);
2312 }
2313 cursorDown(1, true);
2314
2315 // Reset to the beginning of the next line
2316 currentState.cursorX = 0;
2317 }
2318
2319 /**
2320 * DECKPAM - Keypad application mode.
2321 */
2322 private void deckpam() {
2323 keypadMode = KeypadMode.Application;
2324 }
2325
2326 /**
2327 * DECKPNM - Keypad numeric mode.
2328 */
2329 private void deckpnm() {
2330 keypadMode = KeypadMode.Numeric;
2331 }
2332
2333 /**
2334 * Move up n spaces.
2335 *
2336 * @param n number of spaces to move
2337 * @param honorScrollRegion if true, then do nothing if the cursor is
2338 * outside the scrolling region
2339 */
2340 private void cursorUp(final int n, final boolean honorScrollRegion) {
2341 int top;
2342
2343 /*
2344 * Special case: if a user moves the cursor from the right margin, we
2345 * have to reset the VT100 right margin flag.
2346 */
2347 if (n > 0) {
2348 wrapLineFlag = false;
2349 }
2350
2351 for (int i = 0; i < n; i++) {
2352 if (honorScrollRegion == true) {
2353 // Honor the scrolling region
2354 if ((currentState.cursorY < scrollRegionTop)
2355 || (currentState.cursorY > scrollRegionBottom)
2356 ) {
2357 // Outside region, do nothing
2358 return;
2359 }
2360 // Inside region, go up
2361 top = scrollRegionTop;
2362 } else {
2363 // Non-scrolling case
2364 top = 0;
2365 }
2366
2367 if (currentState.cursorY > top) {
2368 currentState.cursorY--;
2369 }
2370 }
2371 }
2372
2373 /**
2374 * Move down n spaces.
2375 *
2376 * @param n number of spaces to move
2377 * @param honorScrollRegion if true, then do nothing if the cursor is
2378 * outside the scrolling region
2379 */
2380 private void cursorDown(final int n, final boolean honorScrollRegion) {
2381 int bottom;
2382
2383 /*
2384 * Special case: if a user moves the cursor from the right margin, we
2385 * have to reset the VT100 right margin flag.
2386 */
2387 if (n > 0) {
2388 wrapLineFlag = false;
2389 }
2390
2391 for (int i = 0; i < n; i++) {
2392
2393 if (honorScrollRegion == true) {
2394 // Honor the scrolling region
2395 if (currentState.cursorY > scrollRegionBottom) {
2396 // Outside region, do nothing
2397 return;
2398 }
2399 // Inside region, go down
2400 bottom = scrollRegionBottom;
2401 } else {
2402 // Non-scrolling case
2403 bottom = height - 1;
2404 }
2405
2406 if (currentState.cursorY < bottom) {
2407 currentState.cursorY++;
2408 }
2409 }
2410 }
2411
2412 /**
2413 * Move left n spaces.
2414 *
2415 * @param n number of spaces to move
2416 * @param honorScrollRegion if true, then do nothing if the cursor is
2417 * outside the scrolling region
2418 */
2419 private void cursorLeft(final int n, final boolean honorScrollRegion) {
2420 /*
2421 * Special case: if a user moves the cursor from the right margin, we
2422 * have to reset the VT100 right margin flag.
2423 */
2424 if (n > 0) {
2425 wrapLineFlag = false;
2426 }
2427
2428 for (int i = 0; i < n; i++) {
2429 if (honorScrollRegion == true) {
2430 // Honor the scrolling region
2431 if ((currentState.cursorY < scrollRegionTop)
2432 || (currentState.cursorY > scrollRegionBottom)
2433 ) {
2434 // Outside region, do nothing
2435 return;
2436 }
2437 }
2438
2439 if (currentState.cursorX > 0) {
2440 currentState.cursorX--;
2441 }
2442 }
2443 }
2444
2445 /**
2446 * Move right n spaces.
2447 *
2448 * @param n number of spaces to move
2449 * @param honorScrollRegion if true, then do nothing if the cursor is
2450 * outside the scrolling region
2451 */
2452 private void cursorRight(final int n, final boolean honorScrollRegion) {
2453 int rightMargin = this.rightMargin;
2454
2455 /*
2456 * Special case: if a user moves the cursor from the right margin, we
2457 * have to reset the VT100 right margin flag.
2458 */
2459 if (n > 0) {
2460 wrapLineFlag = false;
2461 }
2462
2463 if (display.get(currentState.cursorY).isDoubleWidth()) {
2464 rightMargin = ((rightMargin + 1) / 2) - 1;
2465 }
2466
2467 for (int i = 0; i < n; i++) {
2468 if (honorScrollRegion == true) {
2469 // Honor the scrolling region
2470 if ((currentState.cursorY < scrollRegionTop)
2471 || (currentState.cursorY > scrollRegionBottom)
2472 ) {
2473 // Outside region, do nothing
2474 return;
2475 }
2476 }
2477
2478 if (currentState.cursorX < rightMargin) {
2479 currentState.cursorX++;
2480 }
2481 }
2482 }
2483
2484 /**
2485 * Move cursor to (col, row) where (0, 0) is the top-left corner.
2486 *
2487 * @param row row to move to
2488 * @param col column to move to
2489 */
2490 private void cursorPosition(int row, final int col) {
2491 int rightMargin = this.rightMargin;
2492
2493 assert (col >= 0);
2494 assert (row >= 0);
2495
2496 if (display.get(currentState.cursorY).isDoubleWidth()) {
2497 rightMargin = ((rightMargin + 1) / 2) - 1;
2498 }
2499
2500 // Set column number
2501 currentState.cursorX = col;
2502
2503 // Sanity check, bring column back to margin.
2504 if (currentState.cursorX > rightMargin) {
2505 currentState.cursorX = rightMargin;
2506 }
2507
2508 // Set row number
2509 if (currentState.originMode == true) {
2510 row += scrollRegionTop;
2511 }
2512 if (currentState.cursorY < row) {
2513 cursorDown(row - currentState.cursorY, false);
2514 } else if (currentState.cursorY > row) {
2515 cursorUp(currentState.cursorY - row, false);
2516 }
2517
2518 wrapLineFlag = false;
2519 }
2520
2521 /**
2522 * HTS - Horizontal tabulation set.
2523 */
2524 private void hts() {
2525 for (Integer stop: tabStops) {
2526 if (stop == currentState.cursorX) {
2527 // Already have a tab stop here
2528 return;
2529 }
2530 }
2531
2532 // Append a tab stop to the end of the array and resort them
2533 tabStops.add(currentState.cursorX);
2534 Collections.sort(tabStops);
2535 }
2536
2537 /**
2538 * DECSWL - Single-width line.
2539 */
2540 private void decswl() {
2541 display.get(currentState.cursorY).setDoubleWidth(false);
2542 display.get(currentState.cursorY).setDoubleHeight(0);
2543 }
2544
2545 /**
2546 * DECDWL - Double-width line.
2547 */
2548 private void decdwl() {
2549 display.get(currentState.cursorY).setDoubleWidth(true);
2550 display.get(currentState.cursorY).setDoubleHeight(0);
2551 }
2552
2553 /**
2554 * DECHDL - Double-height + double-width line.
2555 *
2556 * @param topHalf if true, this sets the row to be the top half row of a
2557 * double-height row
2558 */
2559 private void dechdl(final boolean topHalf) {
2560 display.get(currentState.cursorY).setDoubleWidth(true);
2561 if (topHalf == true) {
2562 display.get(currentState.cursorY).setDoubleHeight(1);
2563 } else {
2564 display.get(currentState.cursorY).setDoubleHeight(2);
2565 }
2566 }
2567
2568 /**
2569 * DECALN - Screen alignment display.
2570 */
2571 private void decaln() {
2572 Cell newCell = new Cell();
2573 newCell.setChar('E');
2574 for (DisplayLine line: display) {
2575 for (int i = 0; i < line.length(); i++) {
2576 line.replace(i, newCell);
2577 }
2578 }
2579 }
2580
2581 /**
2582 * DECSCL - Compatibility level.
2583 */
2584 private void decscl() {
2585 int i = getCsiParam(0, 0);
2586 int j = getCsiParam(1, 0);
2587
2588 if (i == 61) {
2589 // Reset fonts
2590 currentState.g0Charset = CharacterSet.US;
2591 currentState.g1Charset = CharacterSet.DRAWING;
2592 s8c1t = false;
2593 } else if (i == 62) {
2594
2595 if ((j == 0) || (j == 2)) {
2596 s8c1t = true;
2597 } else if (j == 1) {
2598 s8c1t = false;
2599 }
2600 }
2601 }
2602
2603 /**
2604 * CUD - Cursor down.
2605 */
2606 private void cud() {
2607 cursorDown(getCsiParam(0, 1, 1, height), true);
2608 }
2609
2610 /**
2611 * CUF - Cursor forward.
2612 */
2613 private void cuf() {
2614 cursorRight(getCsiParam(0, 1, 1, rightMargin + 1), true);
2615 }
2616
2617 /**
2618 * CUB - Cursor backward.
2619 */
2620 private void cub() {
2621 cursorLeft(getCsiParam(0, 1, 1, currentState.cursorX + 1), true);
2622 }
2623
2624 /**
2625 * CUU - Cursor up.
2626 */
2627 private void cuu() {
2628 cursorUp(getCsiParam(0, 1, 1, currentState.cursorY + 1), true);
2629 }
2630
2631 /**
2632 * CUP - Cursor position.
2633 */
2634 private void cup() {
2635 cursorPosition(getCsiParam(0, 1, 1, height) - 1,
2636 getCsiParam(1, 1, 1, rightMargin + 1) - 1);
2637 }
2638
2639 /**
2640 * CNL - Cursor down and to column 1.
2641 */
2642 private void cnl() {
2643 cursorDown(getCsiParam(0, 1, 1, height), true);
2644 // To column 0
2645 cursorLeft(currentState.cursorX, true);
2646 }
2647
2648 /**
2649 * CPL - Cursor up and to column 1.
2650 */
2651 private void cpl() {
2652 cursorUp(getCsiParam(0, 1, 1, currentState.cursorY + 1), true);
2653 // To column 0
2654 cursorLeft(currentState.cursorX, true);
2655 }
2656
2657 /**
2658 * CHA - Cursor to column # in current row.
2659 */
2660 private void cha() {
2661 cursorPosition(currentState.cursorY,
2662 getCsiParam(0, 1, 1, rightMargin + 1) - 1);
2663 }
2664
2665 /**
2666 * VPA - Cursor to row #, same column.
2667 */
2668 private void vpa() {
2669 cursorPosition(getCsiParam(0, 1, 1, height) - 1,
2670 currentState.cursorX);
2671 }
2672
2673 /**
2674 * ED - Erase in display.
2675 */
2676 private void ed() {
2677 boolean honorProtected = false;
2678 boolean decPrivateModeFlag = false;
2679
2680 for (Character ch: collectBuffer) {
2681 if (ch == '?') {
2682 decPrivateModeFlag = true;
2683 }
2684 }
2685
2686 if (((type == DeviceType.VT220) || (type == DeviceType.XTERM))
2687 && (decPrivateModeFlag == true)
2688 ) {
2689 honorProtected = true;
2690 }
2691
2692 int i = getCsiParam(0, 0);
2693
2694 if (i == 0) {
2695 // Erase from here to end of screen
2696 if (currentState.cursorY < height - 1) {
2697 eraseScreen(currentState.cursorY + 1, 0, height - 1, width - 1,
2698 honorProtected);
2699 }
2700 eraseLine(currentState.cursorX, width - 1, honorProtected);
2701 } else if (i == 1) {
2702 // Erase from beginning of screen to here
2703 eraseScreen(0, 0, currentState.cursorY - 1, width - 1,
2704 honorProtected);
2705 eraseLine(0, currentState.cursorX, honorProtected);
2706 } else if (i == 2) {
2707 // Erase entire screen
2708 eraseScreen(0, 0, height - 1, width - 1, honorProtected);
2709 }
2710 }
2711
2712 /**
2713 * EL - Erase in line.
2714 */
2715 private void el() {
2716 boolean honorProtected = false;
2717 boolean decPrivateModeFlag = false;
2718
2719 for (Character ch: collectBuffer) {
2720 if (ch == '?') {
2721 decPrivateModeFlag = true;
2722 }
2723 }
2724
2725 if (((type == DeviceType.VT220) || (type == DeviceType.XTERM))
2726 && (decPrivateModeFlag == true)
2727 ) {
2728 honorProtected = true;
2729 }
2730
2731 int i = getCsiParam(0, 0);
2732
2733 if (i == 0) {
2734 // Erase from here to end of line
2735 eraseLine(currentState.cursorX, width - 1, honorProtected);
2736 } else if (i == 1) {
2737 // Erase from beginning of line to here
2738 eraseLine(0, currentState.cursorX, honorProtected);
2739 } else if (i == 2) {
2740 // Erase entire line
2741 eraseLine(0, width - 1, honorProtected);
2742 }
2743 }
2744
2745 /**
2746 * ECH - Erase # of characters in current row.
2747 */
2748 private void ech() {
2749 int i = getCsiParam(0, 1, 1, width);
2750
2751 // Erase from here to i characters
2752 eraseLine(currentState.cursorX, currentState.cursorX + i - 1, false);
2753 }
2754
2755 /**
2756 * IL - Insert line.
2757 */
2758 private void il() {
2759 int i = getCsiParam(0, 1);
2760
2761 if ((currentState.cursorY >= scrollRegionTop)
2762 && (currentState.cursorY <= scrollRegionBottom)
2763 ) {
2764
2765 // I can get the same effect with a scroll-down
2766 scrollingRegionScrollDown(currentState.cursorY,
2767 scrollRegionBottom, i);
2768 }
2769 }
2770
2771 /**
2772 * DCH - Delete char.
2773 */
2774 private void dch() {
2775 int n = getCsiParam(0, 1);
2776 DisplayLine line = display.get(currentState.cursorY);
2777 Cell blank = new Cell();
2778 for (int i = 0; i < n; i++) {
2779 line.delete(currentState.cursorX, blank);
2780 }
2781 }
2782
2783 /**
2784 * ICH - Insert blank char at cursor.
2785 */
2786 private void ich() {
2787 int n = getCsiParam(0, 1);
2788 DisplayLine line = display.get(currentState.cursorY);
2789 Cell blank = new Cell();
2790 for (int i = 0; i < n; i++) {
2791 line.insert(currentState.cursorX, blank);
2792 }
2793 }
2794
2795 /**
2796 * DL - Delete line.
2797 */
2798 private void dl() {
2799 int i = getCsiParam(0, 1);
2800
2801 if ((currentState.cursorY >= scrollRegionTop)
2802 && (currentState.cursorY <= scrollRegionBottom)) {
2803
2804 // I can get the same effect with a scroll-down
2805 scrollingRegionScrollUp(currentState.cursorY,
2806 scrollRegionBottom, i);
2807 }
2808 }
2809
2810 /**
2811 * HVP - Horizontal and vertical position.
2812 */
2813 private void hvp() {
2814 cup();
2815 }
2816
2817 /**
2818 * REP - Repeat character.
2819 */
2820 private void rep() {
2821 int n = getCsiParam(0, 1);
2822 for (int i = 0; i < n; i++) {
2823 printCharacter(repCh);
2824 }
2825 }
2826
2827 /**
2828 * SU - Scroll up.
2829 */
2830 private void su() {
2831 scrollingRegionScrollUp(scrollRegionTop, scrollRegionBottom,
2832 getCsiParam(0, 1, 1, height));
2833 }
2834
2835 /**
2836 * SD - Scroll down.
2837 */
2838 private void sd() {
2839 scrollingRegionScrollDown(scrollRegionTop, scrollRegionBottom,
2840 getCsiParam(0, 1, 1, height));
2841 }
2842
2843 /**
2844 * CBT - Go back X tab stops.
2845 */
2846 private void cbt() {
2847 int tabsToMove = getCsiParam(0, 1);
2848 int tabI;
2849
2850 for (int i = 0; i < tabsToMove; i++) {
2851 int j = currentState.cursorX;
2852 for (tabI = 0; tabI < tabStops.size(); tabI++) {
2853 if (tabStops.get(tabI) >= currentState.cursorX) {
2854 break;
2855 }
2856 }
2857 tabI--;
2858 if (tabI <= 0) {
2859 j = 0;
2860 } else {
2861 j = tabStops.get(tabI);
2862 }
2863 cursorPosition(currentState.cursorY, j);
2864 }
2865 }
2866
2867 /**
2868 * CHT - Advance X tab stops.
2869 */
2870 private void cht() {
2871 int n = getCsiParam(0, 1);
2872 for (int i = 0; i < n; i++) {
2873 advanceToNextTabStop();
2874 }
2875 }
2876
2877 /**
2878 * SGR - Select graphics rendition.
2879 */
2880 private void sgr() {
2881
2882 if (csiParams.size() == 0) {
2883 currentState.attr.reset();
2884 return;
2885 }
2886
2887 for (Integer i: csiParams) {
2888
2889 switch (i) {
2890
2891 case 0:
2892 // Normal
2893 currentState.attr.reset();
2894 break;
2895
2896 case 1:
2897 // Bold
2898 currentState.attr.setBold(true);
2899 break;
2900
2901 case 4:
2902 // Underline
2903 currentState.attr.setUnderline(true);
2904 break;
2905
2906 case 5:
2907 // Blink
2908 currentState.attr.setBlink(true);
2909 break;
2910
2911 case 7:
2912 // Reverse
2913 currentState.attr.setReverse(true);
2914 break;
2915
2916 default:
2917 break;
2918 }
2919
2920 if (type == DeviceType.XTERM) {
2921
2922 switch (i) {
2923
2924 case 8:
2925 // Invisible
2926 // TODO
2927 break;
2928
2929 default:
2930 break;
2931 }
2932 }
2933
2934 if ((type == DeviceType.VT220)
2935 || (type == DeviceType.XTERM)) {
2936
2937 switch (i) {
2938
2939 case 22:
2940 // Normal intensity
2941 currentState.attr.setBold(false);
2942 break;
2943
2944 case 24:
2945 // No underline
2946 currentState.attr.setUnderline(false);
2947 break;
2948
2949 case 25:
2950 // No blink
2951 currentState.attr.setBlink(false);
2952 break;
2953
2954 case 27:
2955 // Un-reverse
2956 currentState.attr.setReverse(false);
2957 break;
2958
2959 default:
2960 break;
2961 }
2962 }
2963
2964 // A true VT100/102/220 does not support color, however everyone
2965 // is used to their terminal emulator supporting color so we will
2966 // unconditionally support color for all DeviceType's.
2967
2968 switch (i) {
2969
2970 case 30:
2971 // Set black foreground
2972 currentState.attr.setForeColor(Color.BLACK);
2973 break;
2974 case 31:
2975 // Set red foreground
2976 currentState.attr.setForeColor(Color.RED);
2977 break;
2978 case 32:
2979 // Set green foreground
2980 currentState.attr.setForeColor(Color.GREEN);
2981 break;
2982 case 33:
2983 // Set yellow foreground
2984 currentState.attr.setForeColor(Color.YELLOW);
2985 break;
2986 case 34:
2987 // Set blue foreground
2988 currentState.attr.setForeColor(Color.BLUE);
2989 break;
2990 case 35:
2991 // Set magenta foreground
2992 currentState.attr.setForeColor(Color.MAGENTA);
2993 break;
2994 case 36:
2995 // Set cyan foreground
2996 currentState.attr.setForeColor(Color.CYAN);
2997 break;
2998 case 37:
2999 // Set white foreground
3000 currentState.attr.setForeColor(Color.WHITE);
3001 break;
3002 case 38:
3003 // Underscore on, default foreground color
3004 currentState.attr.setUnderline(true);
3005 currentState.attr.setForeColor(Color.WHITE);
3006 break;
3007 case 39:
3008 // Underscore off, default foreground color
3009 currentState.attr.setUnderline(false);
3010 currentState.attr.setForeColor(Color.WHITE);
3011 break;
3012 case 40:
3013 // Set black background
3014 currentState.attr.setBackColor(Color.BLACK);
3015 break;
3016 case 41:
3017 // Set red background
3018 currentState.attr.setBackColor(Color.RED);
3019 break;
3020 case 42:
3021 // Set green background
3022 currentState.attr.setBackColor(Color.GREEN);
3023 break;
3024 case 43:
3025 // Set yellow background
3026 currentState.attr.setBackColor(Color.YELLOW);
3027 break;
3028 case 44:
3029 // Set blue background
3030 currentState.attr.setBackColor(Color.BLUE);
3031 break;
3032 case 45:
3033 // Set magenta background
3034 currentState.attr.setBackColor(Color.MAGENTA);
3035 break;
3036 case 46:
3037 // Set cyan background
3038 currentState.attr.setBackColor(Color.CYAN);
3039 break;
3040 case 47:
3041 // Set white background
3042 currentState.attr.setBackColor(Color.WHITE);
3043 break;
3044 case 49:
3045 // Default background
3046 currentState.attr.setBackColor(Color.BLACK);
3047 break;
3048
3049 default:
3050 break;
3051 }
3052 }
3053 }
3054
3055 /**
3056 * DA - Device attributes.
3057 */
3058 private void da() {
3059 int extendedFlag = 0;
3060 int i = 0;
3061 Character [] chars = collectBuffer.toArray(new Character[0]);
3062 StringBuilder args = new StringBuilder();
3063 for (int j = 1; j < chars.length; j++) {
3064 args.append(chars[j]);
3065 }
3066
3067 if (chars.length > 0) {
3068 if (chars[0] == '>') {
3069 extendedFlag = 1;
3070 if (chars.length >= 2) {
3071 i = Integer.parseInt(args.toString());
3072 }
3073 } else if (chars[0] == '=') {
3074 extendedFlag = 2;
3075 if (chars.length >= 2) {
3076 i = Integer.parseInt(args.toString());
3077 }
3078 } else {
3079 // Unknown code, bail out
3080 return;
3081 }
3082 }
3083
3084 if ((i != 0) && (i != 1)) {
3085 return;
3086 }
3087
3088 if ((extendedFlag == 0) && (i == 0)) {
3089 // Send string directly to remote side
3090 writeRemote(deviceTypeResponse());
3091 return;
3092 }
3093
3094 if ((type == DeviceType.VT220) || (type == DeviceType.XTERM)) {
3095
3096 if ((extendedFlag == 1) && (i == 0)) {
3097 /*
3098 * Request "What type of terminal are you, what is your
3099 * firmware version, and what hardware options do you have
3100 * installed?"
3101 *
3102 * Respond: "I am a VT220 (identification code of 1), my
3103 * firmware version is _____ (Pv), and I have _____ Po
3104 * options installed."
3105 *
3106 * (Same as xterm)
3107 *
3108 */
3109
3110 if (s8c1t == true) {
3111 writeRemote("\u009b>1;10;0c");
3112 } else {
3113 writeRemote("\033[>1;10;0c");
3114 }
3115 }
3116 }
3117
3118 // VT420 and up
3119 if ((extendedFlag == 2) && (i == 0)) {
3120
3121 /*
3122 * Request "What is your unit ID?"
3123 *
3124 * Respond: "I was manufactured at site 00 and have a unique ID
3125 * number of 123."
3126 *
3127 */
3128 writeRemote("\033P!|00010203\033\\");
3129 }
3130 }
3131
3132 /**
3133 * DECSTBM - Set top and bottom margins.
3134 */
3135 private void decstbm() {
3136 int top = getCsiParam(0, 1, 1, height) - 1;
3137 int bottom = getCsiParam(1, height, 1, height) - 1;
3138
3139 if (top > bottom) {
3140 top = bottom;
3141 }
3142 scrollRegionTop = top;
3143 scrollRegionBottom = bottom;
3144
3145 // Home cursor
3146 cursorPosition(0, 0);
3147 }
3148
3149 /**
3150 * DECREQTPARM - Request terminal parameters.
3151 */
3152 private void decreqtparm() {
3153 int i = getCsiParam(0, 0);
3154
3155 if ((i != 0) && (i != 1)) {
3156 return;
3157 }
3158
3159 String str = "";
3160
3161 /*
3162 * Request terminal parameters.
3163 *
3164 * Respond with:
3165 *
3166 * Parity NONE, 8 bits, xmitspeed 38400, recvspeed 38400.
3167 * (CLoCk MULtiplier = 1, STP option flags = 0)
3168 *
3169 * (Same as xterm)
3170 */
3171 if (((type == DeviceType.VT220) || (type == DeviceType.XTERM))
3172 && (s8c1t == true)
3173 ) {
3174 str = String.format("\u009b%d;1;1;128;128;1;0x", i + 2);
3175 } else {
3176 str = String.format("\033[%d;1;1;128;128;1;0x", i + 2);
3177 }
3178 writeRemote(str);
3179 }
3180
3181 /**
3182 * DECSCA - Select Character Attributes.
3183 */
3184 private void decsca() {
3185 int i = getCsiParam(0, 0);
3186
3187 if ((i == 0) || (i == 2)) {
3188 // Protect mode OFF
3189 currentState.attr.setProtect(false);
3190 }
3191 if (i == 1) {
3192 // Protect mode ON
3193 currentState.attr.setProtect(true);
3194 }
3195 }
3196
3197 /**
3198 * DECSTR - Soft Terminal Reset.
3199 */
3200 private void decstr() {
3201 // Do exactly like RIS - Reset to initial state
3202 reset();
3203 // Do I clear screen too? I think so...
3204 eraseScreen(0, 0, height - 1, width - 1, false);
3205 cursorPosition(0, 0);
3206 }
3207
3208 /**
3209 * DSR - Device status report.
3210 */
3211 private void dsr() {
3212 boolean decPrivateModeFlag = false;
3213
3214 for (Character ch: collectBuffer) {
3215 if (ch == '?') {
3216 decPrivateModeFlag = true;
3217 }
3218 }
3219
3220 int i = getCsiParam(0, 0);
3221
3222 switch (i) {
3223
3224 case 5:
3225 // Request status report. Respond with "OK, no malfunction."
3226
3227 // Send string directly to remote side
3228 if (((type == DeviceType.VT220) || (type == DeviceType.XTERM))
3229 && (s8c1t == true)
3230 ) {
3231 writeRemote("\u009b0n");
3232 } else {
3233 writeRemote("\033[0n");
3234 }
3235 break;
3236
3237 case 6:
3238 // Request cursor position. Respond with current position.
3239 String str = "";
3240 if (((type == DeviceType.VT220) || (type == DeviceType.XTERM))
3241 && (s8c1t == true)
3242 ) {
3243 str = String.format("\u009b%d;%dR",
3244 currentState.cursorY + 1, currentState.cursorX + 1);
3245 } else {
3246 str = String.format("\033[%d;%dR",
3247 currentState.cursorY + 1, currentState.cursorX + 1);
3248 }
3249
3250 // Send string directly to remote side
3251 writeRemote(str);
3252 break;
3253
3254 case 15:
3255 if (decPrivateModeFlag == true) {
3256
3257 // Request printer status report. Respond with "Printer not
3258 // connected."
3259
3260 if (((type == DeviceType.VT220) || (type == DeviceType.XTERM))
3261 && (s8c1t == true)) {
3262 writeRemote("\u009b?13n");
3263 } else {
3264 writeRemote("\033[?13n");
3265 }
3266 }
3267 break;
3268
3269 case 25:
3270 if (((type == DeviceType.VT220) || (type == DeviceType.XTERM))
3271 && (decPrivateModeFlag == true)
3272 ) {
3273
3274 // Request user-defined keys are locked or unlocked. Respond
3275 // with "User-defined keys are locked."
3276
3277 if (s8c1t == true) {
3278 writeRemote("\u009b?21n");
3279 } else {
3280 writeRemote("\033[?21n");
3281 }
3282 }
3283 break;
3284
3285 case 26:
3286 if (((type == DeviceType.VT220) || (type == DeviceType.XTERM))
3287 && (decPrivateModeFlag == true)
3288 ) {
3289
3290 // Request keyboard language. Respond with "Keyboard
3291 // language is North American."
3292
3293 if (s8c1t == true) {
3294 writeRemote("\u009b?27;1n");
3295 } else {
3296 writeRemote("\033[?27;1n");
3297 }
3298
3299 }
3300 break;
3301
3302 default:
3303 // Some other option, ignore
3304 break;
3305 }
3306 }
3307
3308 /**
3309 * TBC - Tabulation clear.
3310 */
3311 private void tbc() {
3312 int i = getCsiParam(0, 0);
3313 if (i == 0) {
3314 List<Integer> newStops = new ArrayList<Integer>();
3315 for (Integer stop: tabStops) {
3316 if (stop == currentState.cursorX) {
3317 continue;
3318 }
3319 newStops.add(stop);
3320 }
3321 tabStops = newStops;
3322 }
3323 if (i == 3) {
3324 tabStops.clear();
3325 }
3326 }
3327
3328 /**
3329 * Erase the characters in the current line from the start column to the
3330 * end column, inclusive.
3331 *
3332 * @param start starting column to erase (between 0 and width - 1)
3333 * @param end ending column to erase (between 0 and width - 1)
3334 * @param honorProtected if true, do not erase characters with the
3335 * protected attribute set
3336 */
3337 private void eraseLine(int start, int end, final boolean honorProtected) {
3338
3339 if (start > end) {
3340 return;
3341 }
3342 if (end > width - 1) {
3343 end = width - 1;
3344 }
3345 if (start < 0) {
3346 start = 0;
3347 }
3348
3349 for (int i = start; i <= end; i++) {
3350 DisplayLine line = display.get(currentState.cursorY);
3351 if ((!honorProtected)
3352 || ((honorProtected) && (!line.charAt(i).getProtect()))) {
3353
3354 switch (type) {
3355 case VT100:
3356 case VT102:
3357 case VT220:
3358 /*
3359 * From the VT102 manual:
3360 *
3361 * Erasing a character also erases any character
3362 * attribute of the character.
3363 */
3364 line.setBlank(i);
3365 break;
3366 case XTERM:
3367 /*
3368 * Erase with the current color a.k.a. back-color erase
3369 * (bce).
3370 */
3371 line.setChar(i, ' ');
3372 line.setAttr(i, currentState.attr);
3373 break;
3374 }
3375 }
3376 }
3377 }
3378
3379 /**
3380 * Erase a rectangular section of the screen, inclusive. end column,
3381 * inclusive.
3382 *
3383 * @param startRow starting row to erase (between 0 and height - 1)
3384 * @param startCol starting column to erase (between 0 and width - 1)
3385 * @param endRow ending row to erase (between 0 and height - 1)
3386 * @param endCol ending column to erase (between 0 and width - 1)
3387 * @param honorProtected if true, do not erase characters with the
3388 * protected attribute set
3389 */
3390 private void eraseScreen(final int startRow, final int startCol,
3391 final int endRow, final int endCol, final boolean honorProtected) {
3392
3393 int oldCursorY;
3394
3395 if ((startRow < 0)
3396 || (startCol < 0)
3397 || (endRow < 0)
3398 || (endCol < 0)
3399 || (endRow < startRow)
3400 || (endCol < startCol)
3401 ) {
3402 return;
3403 }
3404
3405 oldCursorY = currentState.cursorY;
3406 for (int i = startRow; i <= endRow; i++) {
3407 currentState.cursorY = i;
3408 eraseLine(startCol, endCol, honorProtected);
3409
3410 // Erase display clears the double attributes
3411 display.get(i).setDoubleWidth(false);
3412 display.get(i).setDoubleHeight(0);
3413 }
3414 currentState.cursorY = oldCursorY;
3415 }
3416
3417 /**
3418 * VT220 printer functions. All of these are parsed, but won't do
3419 * anything.
3420 */
3421 private void printerFunctions() {
3422 boolean decPrivateModeFlag = false;
3423 for (Character ch: collectBuffer) {
3424 if (ch == '?') {
3425 decPrivateModeFlag = true;
3426 }
3427 }
3428
3429 int i = getCsiParam(0, 0);
3430
3431 switch (i) {
3432
3433 case 0:
3434 if (decPrivateModeFlag == false) {
3435 // Print screen
3436 }
3437 break;
3438
3439 case 1:
3440 if (decPrivateModeFlag == true) {
3441 // Print cursor line
3442 }
3443 break;
3444
3445 case 4:
3446 if (decPrivateModeFlag == true) {
3447 // Auto print mode OFF
3448 } else {
3449 // Printer controller OFF
3450
3451 // Characters re-appear on the screen
3452 printerControllerMode = false;
3453 }
3454 break;
3455
3456 case 5:
3457 if (decPrivateModeFlag == true) {
3458 // Auto print mode
3459
3460 } else {
3461 // Printer controller
3462
3463 // Characters get sucked into oblivion
3464 printerControllerMode = true;
3465 }
3466 break;
3467
3468 default:
3469 break;
3470
3471 }
3472 }
3473
3474 /**
3475 * Handle the SCAN_OSC_STRING state. Handle this in VT100 because lots
3476 * of remote systems will send an XTerm title sequence even if TERM isn't
3477 * xterm.
3478 *
3479 * @param xtermChar the character received from the remote side
3480 */
3481 private void oscPut(final char xtermChar) {
3482 // Collect first
3483 collectBuffer.add(new Character(xtermChar));
3484
3485 // Xterm cases...
3486 if (xtermChar == 0x07) {
3487 Character [] chars = collectBuffer.toArray(new Character[0]);
3488 StringBuilder args = new StringBuilder();
3489 for (int j = 0; j < chars.length - 1; j++) {
3490 args.append(chars[j]);
3491 }
3492 String [] p = args.toString().split(";");
3493 if (p.length > 0) {
3494 if ((p[0].equals("0")) || (p[0].equals("2"))) {
3495 if (p.length > 1) {
3496 // Screen title
3497 screenTitle = p[1];
3498 }
3499 }
3500 }
3501
3502 // Go to SCAN_GROUND state
3503 toGround();
3504 return;
3505 }
3506 }
3507
3508 /**
3509 * Run this input character through the ECMA48 state machine.
3510 *
3511 * @param ch character from the remote side
3512 */
3513 public void consume(char ch) {
3514
3515 // DEBUG
3516 // System.err.printf("%c", ch);
3517
3518 // Special case for VT10x: 7-bit characters only
3519 if ((type == DeviceType.VT100) || (type == DeviceType.VT102)) {
3520 ch = (char)(ch & 0x7F);
3521 }
3522
3523 // Special "anywhere" states
3524
3525 // 18, 1A --> execute, then switch to SCAN_GROUND
3526 if ((ch == 0x18) || (ch == 0x1A)) {
3527 // CAN and SUB abort escape sequences
3528 toGround();
3529 return;
3530 }
3531
3532 // 80-8F, 91-97, 99, 9A, 9C --> execute, then switch to SCAN_GROUND
3533
3534 // 0x1B == ESCAPE
3535 if ((ch == 0x1B)
3536 && (scanState != ScanState.DCS_ENTRY)
3537 && (scanState != ScanState.DCS_INTERMEDIATE)
3538 && (scanState != ScanState.DCS_IGNORE)
3539 && (scanState != ScanState.DCS_PARAM)
3540 && (scanState != ScanState.DCS_PASSTHROUGH)
3541 ) {
3542
3543 scanState = ScanState.ESCAPE;
3544 return;
3545 }
3546
3547 // 0x9B == CSI 8-bit sequence
3548 if (ch == 0x9B) {
3549 scanState = ScanState.CSI_ENTRY;
3550 return;
3551 }
3552
3553 // 0x9D goes to ScanState.OSC_STRING
3554 if (ch == 0x9D) {
3555 scanState = ScanState.OSC_STRING;
3556 return;
3557 }
3558
3559 // 0x90 goes to DCS_ENTRY
3560 if (ch == 0x90) {
3561 scanState = ScanState.DCS_ENTRY;
3562 return;
3563 }
3564
3565 // 0x98, 0x9E, and 0x9F go to SOSPMAPC_STRING
3566 if ((ch == 0x98) || (ch == 0x9E) || (ch == 0x9F)) {
3567 scanState = ScanState.SOSPMAPC_STRING;
3568 return;
3569 }
3570
3571 // 0x7F (DEL) is always discarded
3572 if (ch == 0x7F) {
3573 return;
3574 }
3575
3576 switch (scanState) {
3577
3578 case GROUND:
3579 // 00-17, 19, 1C-1F --> execute
3580 // 80-8F, 91-9A, 9C --> execute
3581 if ((ch <= 0x1F) || ((ch >= 0x80) && (ch <= 0x9F))) {
3582 handleControlChar(ch);
3583 }
3584
3585 // 20-7F --> print
3586 if (((ch >= 0x20) && (ch <= 0x7F))
3587 || (ch >= 0xA0)
3588 ) {
3589
3590 // VT220 printer --> trash bin
3591 if (((type == DeviceType.VT220)
3592 || (type == DeviceType.XTERM))
3593 && (printerControllerMode == true)
3594 ) {
3595 return;
3596 }
3597
3598 // Hang onto this character
3599 repCh = mapCharacter(ch);
3600
3601 // Print this character
3602 printCharacter(repCh);
3603 }
3604 return;
3605
3606 case ESCAPE:
3607 // 00-17, 19, 1C-1F --> execute
3608 if (ch <= 0x1F) {
3609 handleControlChar(ch);
3610 return;
3611 }
3612
3613 // 20-2F --> collect, then switch to ESCAPE_INTERMEDIATE
3614 if ((ch >= 0x20) && (ch <= 0x2F)) {
3615 collect(ch);
3616 scanState = ScanState.ESCAPE_INTERMEDIATE;
3617 return;
3618 }
3619
3620 // 30-4F, 51-57, 59, 5A, 5C, 60-7E --> dispatch, then switch to GROUND
3621 if ((ch >= 0x30) && (ch <= 0x4F)) {
3622 switch (ch) {
3623 case '0':
3624 case '1':
3625 case '2':
3626 case '3':
3627 case '4':
3628 case '5':
3629 case '6':
3630 break;
3631 case '7':
3632 // DECSC - Save cursor
3633 // Note this code overlaps both ANSI and VT52 mode
3634 decsc();
3635 break;
3636
3637 case '8':
3638 // DECRC - Restore cursor
3639 // Note this code overlaps both ANSI and VT52 mode
3640 decrc();
3641 break;
3642
3643 case '9':
3644 case ':':
3645 case ';':
3646 break;
3647 case '<':
3648 if (vt52Mode == true) {
3649 // DECANM - Enter ANSI mode
3650 vt52Mode = false;
3651 arrowKeyMode = ArrowKeyMode.VT100;
3652
3653 /*
3654 * From the VT102 docs: "You use ANSI mode to select
3655 * most terminal features; the terminal uses the same
3656 * features when it switches to VT52 mode. You
3657 * cannot, however, change most of these features in
3658 * VT52 mode."
3659 *
3660 * In other words, do not reset any other attributes
3661 * when switching between VT52 submode and ANSI.
3662 */
3663
3664 // Reset fonts
3665 currentState.g0Charset = CharacterSet.US;
3666 currentState.g1Charset = CharacterSet.DRAWING;
3667 s8c1t = false;
3668 singleshift = Singleshift.NONE;
3669 currentState.glLockshift = LockshiftMode.NONE;
3670 currentState.grLockshift = LockshiftMode.NONE;
3671 }
3672 break;
3673 case '=':
3674 // DECKPAM - Keypad application mode
3675 // Note this code overlaps both ANSI and VT52 mode
3676 deckpam();
3677 break;
3678 case '>':
3679 // DECKPNM - Keypad numeric mode
3680 // Note this code overlaps both ANSI and VT52 mode
3681 deckpnm();
3682 break;
3683 case '?':
3684 case '@':
3685 break;
3686 case 'A':
3687 if (vt52Mode == true) {
3688 // Cursor up, and stop at the top without scrolling
3689 cursorUp(1, false);
3690 }
3691 break;
3692 case 'B':
3693 if (vt52Mode == true) {
3694 // Cursor down, and stop at the bottom without scrolling
3695 cursorDown(1, false);
3696 }
3697 break;
3698 case 'C':
3699 if (vt52Mode == true) {
3700 // Cursor right, and stop at the right without scrolling
3701 cursorRight(1, false);
3702 }
3703 break;
3704 case 'D':
3705 if (vt52Mode == true) {
3706 // Cursor left, and stop at the left without scrolling
3707 cursorLeft(1, false);
3708 } else {
3709 // IND - Index
3710 ind();
3711 }
3712 break;
3713 case 'E':
3714 if (vt52Mode == true) {
3715 // Nothing
3716 } else {
3717 // NEL - Next line
3718 nel();
3719 }
3720 break;
3721 case 'F':
3722 if (vt52Mode == true) {
3723 // G0 --> Special graphics
3724 currentState.g0Charset = CharacterSet.VT52_GRAPHICS;
3725 }
3726 break;
3727 case 'G':
3728 if (vt52Mode == true) {
3729 // G0 --> ASCII set
3730 currentState.g0Charset = CharacterSet.US;
3731 }
3732 break;
3733 case 'H':
3734 if (vt52Mode == true) {
3735 // Cursor to home
3736 cursorPosition(0, 0);
3737 } else {
3738 // HTS - Horizontal tabulation set
3739 hts();
3740 }
3741 break;
3742 case 'I':
3743 if (vt52Mode == true) {
3744 // Reverse line feed. Same as RI.
3745 ri();
3746 }
3747 break;
3748 case 'J':
3749 if (vt52Mode == true) {
3750 // Erase to end of screen
3751 eraseLine(currentState.cursorX, width - 1, false);
3752 eraseScreen(currentState.cursorY + 1, 0, height - 1,
3753 width - 1, false);
3754 }
3755 break;
3756 case 'K':
3757 if (vt52Mode == true) {
3758 // Erase to end of line
3759 eraseLine(currentState.cursorX, width - 1, false);
3760 }
3761 break;
3762 case 'L':
3763 break;
3764 case 'M':
3765 if (vt52Mode == true) {
3766 // Nothing
3767 } else {
3768 // RI - Reverse index
3769 ri();
3770 }
3771 break;
3772 case 'N':
3773 if (vt52Mode == false) {
3774 // SS2
3775 singleshift = Singleshift.SS2;
3776 }
3777 break;
3778 case 'O':
3779 if (vt52Mode == false) {
3780 // SS3
3781 singleshift = Singleshift.SS3;
3782 }
3783 break;
3784 }
3785 toGround();
3786 return;
3787 }
3788 if ((ch >= 0x51) && (ch <= 0x57)) {
3789 switch (ch) {
3790 case 'Q':
3791 case 'R':
3792 case 'S':
3793 case 'T':
3794 case 'U':
3795 case 'V':
3796 case 'W':
3797 break;
3798 }
3799 toGround();
3800 return;
3801 }
3802 if (ch == 0x59) {
3803 // 'Y'
3804 if (vt52Mode == true) {
3805 scanState = ScanState.VT52_DIRECT_CURSOR_ADDRESS;
3806 } else {
3807 toGround();
3808 }
3809 return;
3810 }
3811 if (ch == 0x5A) {
3812 // 'Z'
3813 if (vt52Mode == true) {
3814 // Identify
3815 // Send string directly to remote side
3816 writeRemote("\033/Z");
3817 } else {
3818 // DECID
3819 // Send string directly to remote side
3820 writeRemote(deviceTypeResponse());
3821 }
3822 toGround();
3823 return;
3824 }
3825 if (ch == 0x5C) {
3826 // '\'
3827 toGround();
3828 return;
3829 }
3830
3831 // VT52 cannot get to any of these other states
3832 if (vt52Mode == true) {
3833 toGround();
3834 return;
3835 }
3836
3837 if ((ch >= 0x60) && (ch <= 0x7E)) {
3838 switch (ch) {
3839 case '`':
3840 case 'a':
3841 case 'b':
3842 break;
3843 case 'c':
3844 // RIS - Reset to initial state
3845 reset();
3846 // Do I clear screen too? I think so...
3847 eraseScreen(0, 0, height - 1, width - 1, false);
3848 cursorPosition(0, 0);
3849 break;
3850 case 'd':
3851 case 'e':
3852 case 'f':
3853 case 'g':
3854 case 'h':
3855 case 'i':
3856 case 'j':
3857 case 'k':
3858 case 'l':
3859 case 'm':
3860 break;
3861 case 'n':
3862 if ((type == DeviceType.VT220)
3863 || (type == DeviceType.XTERM)) {
3864
3865 // VT220 lockshift G2 into GL
3866 currentState.glLockshift = LockshiftMode.G2_GL;
3867 shiftOut = false;
3868 }
3869 break;
3870 case 'o':
3871 if ((type == DeviceType.VT220)
3872 || (type == DeviceType.XTERM)) {
3873
3874 // VT220 lockshift G3 into GL
3875 currentState.glLockshift = LockshiftMode.G3_GL;
3876 shiftOut = false;
3877 }
3878 break;
3879 case 'p':
3880 case 'q':
3881 case 'r':
3882 case 's':
3883 case 't':
3884 case 'u':
3885 case 'v':
3886 case 'w':
3887 case 'x':
3888 case 'y':
3889 case 'z':
3890 case '{':
3891 break;
3892 case '|':
3893 if ((type == DeviceType.VT220)
3894 || (type == DeviceType.XTERM)) {
3895
3896 // VT220 lockshift G3 into GR
3897 currentState.grLockshift = LockshiftMode.G3_GR;
3898 shiftOut = false;
3899 }
3900 break;
3901 case '}':
3902 if ((type == DeviceType.VT220)
3903 || (type == DeviceType.XTERM)) {
3904
3905 // VT220 lockshift G2 into GR
3906 currentState.grLockshift = LockshiftMode.G2_GR;
3907 shiftOut = false;
3908 }
3909 break;
3910
3911 case '~':
3912 if ((type == DeviceType.VT220)
3913 || (type == DeviceType.XTERM)) {
3914
3915 // VT220 lockshift G1 into GR
3916 currentState.grLockshift = LockshiftMode.G1_GR;
3917 shiftOut = false;
3918 }
3919 break;
3920 }
3921 toGround();
3922 }
3923
3924 // 7F --> ignore
3925
3926 // 0x5B goes to CSI_ENTRY
3927 if (ch == 0x5B) {
3928 scanState = ScanState.CSI_ENTRY;
3929 }
3930
3931 // 0x5D goes to OSC_STRING
3932 if (ch == 0x5D) {
3933 scanState = ScanState.OSC_STRING;
3934 }
3935
3936 // 0x50 goes to DCS_ENTRY
3937 if (ch == 0x50) {
3938 scanState = ScanState.DCS_ENTRY;
3939 }
3940
3941 // 0x58, 0x5E, and 0x5F go to SOSPMAPC_STRING
3942 if ((ch == 0x58) || (ch == 0x5E) || (ch == 0x5F)) {
3943 scanState = ScanState.SOSPMAPC_STRING;
3944 }
3945
3946 return;
3947
3948 case ESCAPE_INTERMEDIATE:
3949 // 00-17, 19, 1C-1F --> execute
3950 if (ch <= 0x1F) {
3951 handleControlChar(ch);
3952 }
3953
3954 // 20-2F --> collect
3955 if ((ch >= 0x20) && (ch <= 0x2F)) {
3956 collect(ch);
3957 }
3958
3959 // 30-7E --> dispatch, then switch to GROUND
3960 if ((ch >= 0x30) && (ch <= 0x7E)) {
3961 switch (ch) {
3962 case '0':
3963 if ((collectBuffer.size() == 1)
3964 && (collectBuffer.get(0) == '(')) {
3965 // G0 --> Special graphics
3966 currentState.g0Charset = CharacterSet.DRAWING;
3967 }
3968 if ((collectBuffer.size() == 1)
3969 && (collectBuffer.get(0) == ')')) {
3970 // G1 --> Special graphics
3971 currentState.g1Charset = CharacterSet.DRAWING;
3972 }
3973 if ((type == DeviceType.VT220)
3974 || (type == DeviceType.XTERM)) {
3975
3976 if ((collectBuffer.size() == 1)
3977 && (collectBuffer.get(0) == '*')) {
3978 // G2 --> Special graphics
3979 currentState.g2Charset = CharacterSet.DRAWING;
3980 }
3981 if ((collectBuffer.size() == 1)
3982 && (collectBuffer.get(0) == '+')) {
3983 // G3 --> Special graphics
3984 currentState.g3Charset = CharacterSet.DRAWING;
3985 }
3986 }
3987 break;
3988 case '1':
3989 if ((collectBuffer.size() == 1)
3990 && (collectBuffer.get(0) == '(')) {
3991 // G0 --> Alternate character ROM standard character set
3992 currentState.g0Charset = CharacterSet.ROM;
3993 }
3994 if ((collectBuffer.size() == 1)
3995 && (collectBuffer.get(0) == ')')) {
3996 // G1 --> Alternate character ROM standard character set
3997 currentState.g1Charset = CharacterSet.ROM;
3998 }
3999 break;
4000 case '2':
4001 if ((collectBuffer.size() == 1)
4002 && (collectBuffer.get(0) == '(')) {
4003 // G0 --> Alternate character ROM special graphics
4004 currentState.g0Charset = CharacterSet.ROM_SPECIAL;
4005 }
4006 if ((collectBuffer.size() == 1)
4007 && (collectBuffer.get(0) == ')')) {
4008 // G1 --> Alternate character ROM special graphics
4009 currentState.g1Charset = CharacterSet.ROM_SPECIAL;
4010 }
4011 break;
4012 case '3':
4013 if ((collectBuffer.size() == 1)
4014 && (collectBuffer.get(0) == '#')) {
4015 // DECDHL - Double-height line (top half)
4016 dechdl(true);
4017 }
4018 break;
4019 case '4':
4020 if ((collectBuffer.size() == 1)
4021 && (collectBuffer.get(0) == '#')) {
4022 // DECDHL - Double-height line (bottom half)
4023 dechdl(false);
4024 }
4025 if ((type == DeviceType.VT220)
4026 || (type == DeviceType.XTERM)) {
4027
4028 if ((collectBuffer.size() == 1)
4029 && (collectBuffer.get(0) == '(')) {
4030 // G0 --> DUTCH
4031 currentState.g0Charset = CharacterSet.NRC_DUTCH;
4032 }
4033 if ((collectBuffer.size() == 1)
4034 && (collectBuffer.get(0) == ')')) {
4035 // G1 --> DUTCH
4036 currentState.g1Charset = CharacterSet.NRC_DUTCH;
4037 }
4038 if ((collectBuffer.size() == 1)
4039 && (collectBuffer.get(0) == '*')) {
4040 // G2 --> DUTCH
4041 currentState.g2Charset = CharacterSet.NRC_DUTCH;
4042 }
4043 if ((collectBuffer.size() == 1)
4044 && (collectBuffer.get(0) == '+')) {
4045 // G3 --> DUTCH
4046 currentState.g3Charset = CharacterSet.NRC_DUTCH;
4047 }
4048 }
4049 break;
4050 case '5':
4051 if ((collectBuffer.size() == 1)
4052 && (collectBuffer.get(0) == '#')) {
4053 // DECSWL - Single-width line
4054 decswl();
4055 }
4056 if ((type == DeviceType.VT220)
4057 || (type == DeviceType.XTERM)) {
4058
4059 if ((collectBuffer.size() == 1)
4060 && (collectBuffer.get(0) == '(')) {
4061 // G0 --> FINNISH
4062 currentState.g0Charset = CharacterSet.NRC_FINNISH;
4063 }
4064 if ((collectBuffer.size() == 1)
4065 && (collectBuffer.get(0) == ')')) {
4066 // G1 --> FINNISH
4067 currentState.g1Charset = CharacterSet.NRC_FINNISH;
4068 }
4069 if ((collectBuffer.size() == 1)
4070 && (collectBuffer.get(0) == '*')) {
4071 // G2 --> FINNISH
4072 currentState.g2Charset = CharacterSet.NRC_FINNISH;
4073 }
4074 if ((collectBuffer.size() == 1)
4075 && (collectBuffer.get(0) == '+')) {
4076 // G3 --> FINNISH
4077 currentState.g3Charset = CharacterSet.NRC_FINNISH;
4078 }
4079 }
4080 break;
4081 case '6':
4082 if ((collectBuffer.size() == 1)
4083 && (collectBuffer.get(0) == '#')) {
4084 // DECDWL - Double-width line
4085 decdwl();
4086 }
4087 if ((type == DeviceType.VT220)
4088 || (type == DeviceType.XTERM)) {
4089
4090 if ((collectBuffer.size() == 1)
4091 && (collectBuffer.get(0) == '(')) {
4092 // G0 --> NORWEGIAN
4093 currentState.g0Charset = CharacterSet.NRC_NORWEGIAN;
4094 }
4095 if ((collectBuffer.size() == 1)
4096 && (collectBuffer.get(0) == ')')) {
4097 // G1 --> NORWEGIAN
4098 currentState.g1Charset = CharacterSet.NRC_NORWEGIAN;
4099 }
4100 if ((collectBuffer.size() == 1)
4101 && (collectBuffer.get(0) == '*')) {
4102 // G2 --> NORWEGIAN
4103 currentState.g2Charset = CharacterSet.NRC_NORWEGIAN;
4104 }
4105 if ((collectBuffer.size() == 1)
4106 && (collectBuffer.get(0) == '+')) {
4107 // G3 --> NORWEGIAN
4108 currentState.g3Charset = CharacterSet.NRC_NORWEGIAN;
4109 }
4110 }
4111 break;
4112 case '7':
4113 if ((type == DeviceType.VT220)
4114 || (type == DeviceType.XTERM)) {
4115
4116 if ((collectBuffer.size() == 1)
4117 && (collectBuffer.get(0) == '(')) {
4118 // G0 --> SWEDISH
4119 currentState.g0Charset = CharacterSet.NRC_SWEDISH;
4120 }
4121 if ((collectBuffer.size() == 1)
4122 && (collectBuffer.get(0) == ')')) {
4123 // G1 --> SWEDISH
4124 currentState.g1Charset = CharacterSet.NRC_SWEDISH;
4125 }
4126 if ((collectBuffer.size() == 1)
4127 && (collectBuffer.get(0) == '*')) {
4128 // G2 --> SWEDISH
4129 currentState.g2Charset = CharacterSet.NRC_SWEDISH;
4130 }
4131 if ((collectBuffer.size() == 1)
4132 && (collectBuffer.get(0) == '+')) {
4133 // G3 --> SWEDISH
4134 currentState.g3Charset = CharacterSet.NRC_SWEDISH;
4135 }
4136 }
4137 break;
4138 case '8':
4139 if ((collectBuffer.size() == 1)
4140 && (collectBuffer.get(0) == '#')) {
4141 // DECALN - Screen alignment display
4142 decaln();
4143 }
4144 break;
4145 case '9':
4146 case ':':
4147 case ';':
4148 break;
4149 case '<':
4150 if ((type == DeviceType.VT220)
4151 || (type == DeviceType.XTERM)) {
4152
4153 if ((collectBuffer.size() == 1)
4154 && (collectBuffer.get(0) == '(')) {
4155 // G0 --> DEC_SUPPLEMENTAL
4156 currentState.g0Charset = CharacterSet.DEC_SUPPLEMENTAL;
4157 }
4158 if ((collectBuffer.size() == 1)
4159 && (collectBuffer.get(0) == ')')) {
4160 // G1 --> DEC_SUPPLEMENTAL
4161 currentState.g1Charset = CharacterSet.DEC_SUPPLEMENTAL;
4162 }
4163 if ((collectBuffer.size() == 1)
4164 && (collectBuffer.get(0) == '*')) {
4165 // G2 --> DEC_SUPPLEMENTAL
4166 currentState.g2Charset = CharacterSet.DEC_SUPPLEMENTAL;
4167 }
4168 if ((collectBuffer.size() == 1)
4169 && (collectBuffer.get(0) == '+')) {
4170 // G3 --> DEC_SUPPLEMENTAL
4171 currentState.g3Charset = CharacterSet.DEC_SUPPLEMENTAL;
4172 }
4173 }
4174 break;
4175 case '=':
4176 if ((type == DeviceType.VT220)
4177 || (type == DeviceType.XTERM)) {
4178
4179 if ((collectBuffer.size() == 1)
4180 && (collectBuffer.get(0) == '(')) {
4181 // G0 --> SWISS
4182 currentState.g0Charset = CharacterSet.NRC_SWISS;
4183 }
4184 if ((collectBuffer.size() == 1)
4185 && (collectBuffer.get(0) == ')')) {
4186 // G1 --> SWISS
4187 currentState.g1Charset = CharacterSet.NRC_SWISS;
4188 }
4189 if ((collectBuffer.size() == 1)
4190 && (collectBuffer.get(0) == '*')) {
4191 // G2 --> SWISS
4192 currentState.g2Charset = CharacterSet.NRC_SWISS;
4193 }
4194 if ((collectBuffer.size() == 1)
4195 && (collectBuffer.get(0) == '+')) {
4196 // G3 --> SWISS
4197 currentState.g3Charset = CharacterSet.NRC_SWISS;
4198 }
4199 }
4200 break;
4201 case '>':
4202 case '?':
4203 case '@':
4204 break;
4205 case 'A':
4206 if ((collectBuffer.size() == 1)
4207 && (collectBuffer.get(0) == '(')) {
4208 // G0 --> United Kingdom set
4209 currentState.g0Charset = CharacterSet.UK;
4210 }
4211 if ((collectBuffer.size() == 1)
4212 && (collectBuffer.get(0) == ')')) {
4213 // G1 --> United Kingdom set
4214 currentState.g1Charset = CharacterSet.UK;
4215 }
4216 if ((type == DeviceType.VT220)
4217 || (type == DeviceType.XTERM)) {
4218
4219 if ((collectBuffer.size() == 1)
4220 && (collectBuffer.get(0) == '*')) {
4221 // G2 --> United Kingdom set
4222 currentState.g2Charset = CharacterSet.UK;
4223 }
4224 if ((collectBuffer.size() == 1)
4225 && (collectBuffer.get(0) == '+')) {
4226 // G3 --> United Kingdom set
4227 currentState.g3Charset = CharacterSet.UK;
4228 }
4229 }
4230 break;
4231 case 'B':
4232 if ((collectBuffer.size() == 1)
4233 && (collectBuffer.get(0) == '(')) {
4234 // G0 --> ASCII set
4235 currentState.g0Charset = CharacterSet.US;
4236 }
4237 if ((collectBuffer.size() == 1)
4238 && (collectBuffer.get(0) == ')')) {
4239 // G1 --> ASCII set
4240 currentState.g1Charset = CharacterSet.US;
4241 }
4242 if ((type == DeviceType.VT220)
4243 || (type == DeviceType.XTERM)) {
4244
4245 if ((collectBuffer.size() == 1)
4246 && (collectBuffer.get(0) == '*')) {
4247 // G2 --> ASCII
4248 currentState.g2Charset = CharacterSet.US;
4249 }
4250 if ((collectBuffer.size() == 1)
4251 && (collectBuffer.get(0) == '+')) {
4252 // G3 --> ASCII
4253 currentState.g3Charset = CharacterSet.US;
4254 }
4255 }
4256 break;
4257 case 'C':
4258 if ((type == DeviceType.VT220)
4259 || (type == DeviceType.XTERM)) {
4260
4261 if ((collectBuffer.size() == 1)
4262 && (collectBuffer.get(0) == '(')) {
4263 // G0 --> FINNISH
4264 currentState.g0Charset = CharacterSet.NRC_FINNISH;
4265 }
4266 if ((collectBuffer.size() == 1)
4267 && (collectBuffer.get(0) == ')')) {
4268 // G1 --> FINNISH
4269 currentState.g1Charset = CharacterSet.NRC_FINNISH;
4270 }
4271 if ((collectBuffer.size() == 1)
4272 && (collectBuffer.get(0) == '*')) {
4273 // G2 --> FINNISH
4274 currentState.g2Charset = CharacterSet.NRC_FINNISH;
4275 }
4276 if ((collectBuffer.size() == 1)
4277 && (collectBuffer.get(0) == '+')) {
4278 // G3 --> FINNISH
4279 currentState.g3Charset = CharacterSet.NRC_FINNISH;
4280 }
4281 }
4282 break;
4283 case 'D':
4284 break;
4285 case 'E':
4286 if ((type == DeviceType.VT220)
4287 || (type == DeviceType.XTERM)) {
4288
4289 if ((collectBuffer.size() == 1)
4290 && (collectBuffer.get(0) == '(')) {
4291 // G0 --> NORWEGIAN
4292 currentState.g0Charset = CharacterSet.NRC_NORWEGIAN;
4293 }
4294 if ((collectBuffer.size() == 1)
4295 && (collectBuffer.get(0) == ')')) {
4296 // G1 --> NORWEGIAN
4297 currentState.g1Charset = CharacterSet.NRC_NORWEGIAN;
4298 }
4299 if ((collectBuffer.size() == 1)
4300 && (collectBuffer.get(0) == '*')) {
4301 // G2 --> NORWEGIAN
4302 currentState.g2Charset = CharacterSet.NRC_NORWEGIAN;
4303 }
4304 if ((collectBuffer.size() == 1)
4305 && (collectBuffer.get(0) == '+')) {
4306 // G3 --> NORWEGIAN
4307 currentState.g3Charset = CharacterSet.NRC_NORWEGIAN;
4308 }
4309 }
4310 break;
4311 case 'F':
4312 if ((type == DeviceType.VT220)
4313 || (type == DeviceType.XTERM)) {
4314
4315 if ((collectBuffer.size() == 1)
4316 && (collectBuffer.get(0) == ' ')) {
4317 // S7C1T
4318 s8c1t = false;
4319 }
4320 }
4321 break;
4322 case 'G':
4323 if ((type == DeviceType.VT220)
4324 || (type == DeviceType.XTERM)) {
4325
4326 if ((collectBuffer.size() == 1)
4327 && (collectBuffer.get(0) == ' ')) {
4328 // S8C1T
4329 s8c1t = true;
4330 }
4331 }
4332 break;
4333 case 'H':
4334 if ((type == DeviceType.VT220)
4335 || (type == DeviceType.XTERM)) {
4336
4337 if ((collectBuffer.size() == 1)
4338 && (collectBuffer.get(0) == '(')) {
4339 // G0 --> SWEDISH
4340 currentState.g0Charset = CharacterSet.NRC_SWEDISH;
4341 }
4342 if ((collectBuffer.size() == 1)
4343 && (collectBuffer.get(0) == ')')) {
4344 // G1 --> SWEDISH
4345 currentState.g1Charset = CharacterSet.NRC_SWEDISH;
4346 }
4347 if ((collectBuffer.size() == 1)
4348 && (collectBuffer.get(0) == '*')) {
4349 // G2 --> SWEDISH
4350 currentState.g2Charset = CharacterSet.NRC_SWEDISH;
4351 }
4352 if ((collectBuffer.size() == 1)
4353 && (collectBuffer.get(0) == '+')) {
4354 // G3 --> SWEDISH
4355 currentState.g3Charset = CharacterSet.NRC_SWEDISH;
4356 }
4357 }
4358 break;
4359 case 'I':
4360 case 'J':
4361 break;
4362 case 'K':
4363 if ((type == DeviceType.VT220)
4364 || (type == DeviceType.XTERM)) {
4365
4366 if ((collectBuffer.size() == 1)
4367 && (collectBuffer.get(0) == '(')) {
4368 // G0 --> GERMAN
4369 currentState.g0Charset = CharacterSet.NRC_GERMAN;
4370 }
4371 if ((collectBuffer.size() == 1)
4372 && (collectBuffer.get(0) == ')')) {
4373 // G1 --> GERMAN
4374 currentState.g1Charset = CharacterSet.NRC_GERMAN;
4375 }
4376 if ((collectBuffer.size() == 1)
4377 && (collectBuffer.get(0) == '*')) {
4378 // G2 --> GERMAN
4379 currentState.g2Charset = CharacterSet.NRC_GERMAN;
4380 }
4381 if ((collectBuffer.size() == 1)
4382 && (collectBuffer.get(0) == '+')) {
4383 // G3 --> GERMAN
4384 currentState.g3Charset = CharacterSet.NRC_GERMAN;
4385 }
4386 }
4387 break;
4388 case 'L':
4389 case 'M':
4390 case 'N':
4391 case 'O':
4392 case 'P':
4393 break;
4394 case 'Q':
4395 if ((type == DeviceType.VT220)
4396 || (type == DeviceType.XTERM)) {
4397
4398 if ((collectBuffer.size() == 1)
4399 && (collectBuffer.get(0) == '(')) {
4400 // G0 --> FRENCH_CA
4401 currentState.g0Charset = CharacterSet.NRC_FRENCH_CA;
4402 }
4403 if ((collectBuffer.size() == 1)
4404 && (collectBuffer.get(0) == ')')) {
4405 // G1 --> FRENCH_CA
4406 currentState.g1Charset = CharacterSet.NRC_FRENCH_CA;
4407 }
4408 if ((collectBuffer.size() == 1)
4409 && (collectBuffer.get(0) == '*')) {
4410 // G2 --> FRENCH_CA
4411 currentState.g2Charset = CharacterSet.NRC_FRENCH_CA;
4412 }
4413 if ((collectBuffer.size() == 1)
4414 && (collectBuffer.get(0) == '+')) {
4415 // G3 --> FRENCH_CA
4416 currentState.g3Charset = CharacterSet.NRC_FRENCH_CA;
4417 }
4418 }
4419 break;
4420 case 'R':
4421 if ((type == DeviceType.VT220)
4422 || (type == DeviceType.XTERM)) {
4423
4424 if ((collectBuffer.size() == 1)
4425 && (collectBuffer.get(0) == '(')) {
4426 // G0 --> FRENCH
4427 currentState.g0Charset = CharacterSet.NRC_FRENCH;
4428 }
4429 if ((collectBuffer.size() == 1)
4430 && (collectBuffer.get(0) == ')')) {
4431 // G1 --> FRENCH
4432 currentState.g1Charset = CharacterSet.NRC_FRENCH;
4433 }
4434 if ((collectBuffer.size() == 1)
4435 && (collectBuffer.get(0) == '*')) {
4436 // G2 --> FRENCH
4437 currentState.g2Charset = CharacterSet.NRC_FRENCH;
4438 }
4439 if ((collectBuffer.size() == 1)
4440 && (collectBuffer.get(0) == '+')) {
4441 // G3 --> FRENCH
4442 currentState.g3Charset = CharacterSet.NRC_FRENCH;
4443 }
4444 }
4445 break;
4446 case 'S':
4447 case 'T':
4448 case 'U':
4449 case 'V':
4450 case 'W':
4451 case 'X':
4452 break;
4453 case 'Y':
4454 if ((type == DeviceType.VT220)
4455 || (type == DeviceType.XTERM)) {
4456
4457 if ((collectBuffer.size() == 1)
4458 && (collectBuffer.get(0) == '(')) {
4459 // G0 --> ITALIAN
4460 currentState.g0Charset = CharacterSet.NRC_ITALIAN;
4461 }
4462 if ((collectBuffer.size() == 1)
4463 && (collectBuffer.get(0) == ')')) {
4464 // G1 --> ITALIAN
4465 currentState.g1Charset = CharacterSet.NRC_ITALIAN;
4466 }
4467 if ((collectBuffer.size() == 1)
4468 && (collectBuffer.get(0) == '*')) {
4469 // G2 --> ITALIAN
4470 currentState.g2Charset = CharacterSet.NRC_ITALIAN;
4471 }
4472 if ((collectBuffer.size() == 1)
4473 && (collectBuffer.get(0) == '+')) {
4474 // G3 --> ITALIAN
4475 currentState.g3Charset = CharacterSet.NRC_ITALIAN;
4476 }
4477 }
4478 break;
4479 case 'Z':
4480 if ((type == DeviceType.VT220)
4481 || (type == DeviceType.XTERM)) {
4482
4483 if ((collectBuffer.size() == 1)
4484 && (collectBuffer.get(0) == '(')) {
4485 // G0 --> SPANISH
4486 currentState.g0Charset = CharacterSet.NRC_SPANISH;
4487 }
4488 if ((collectBuffer.size() == 1)
4489 && (collectBuffer.get(0) == ')')) {
4490 // G1 --> SPANISH
4491 currentState.g1Charset = CharacterSet.NRC_SPANISH;
4492 }
4493 if ((collectBuffer.size() == 1)
4494 && (collectBuffer.get(0) == '*')) {
4495 // G2 --> SPANISH
4496 currentState.g2Charset = CharacterSet.NRC_SPANISH;
4497 }
4498 if ((collectBuffer.size() == 1)
4499 && (collectBuffer.get(0) == '+')) {
4500 // G3 --> SPANISH
4501 currentState.g3Charset = CharacterSet.NRC_SPANISH;
4502 }
4503 }
4504 break;
4505 case '[':
4506 case '\\':
4507 case ']':
4508 case '^':
4509 case '_':
4510 case '`':
4511 case 'a':
4512 case 'b':
4513 case 'c':
4514 case 'd':
4515 case 'e':
4516 case 'f':
4517 case 'g':
4518 case 'h':
4519 case 'i':
4520 case 'j':
4521 case 'k':
4522 case 'l':
4523 case 'm':
4524 case 'n':
4525 case 'o':
4526 case 'p':
4527 case 'q':
4528 case 'r':
4529 case 's':
4530 case 't':
4531 case 'u':
4532 case 'v':
4533 case 'w':
4534 case 'x':
4535 case 'y':
4536 case 'z':
4537 case '{':
4538 case '|':
4539 case '}':
4540 case '~':
4541 break;
4542 }
4543 toGround();
4544 }
4545
4546 // 7F --> ignore
4547
4548 // 0x9C goes to GROUND
4549 if (ch == 0x9C) {
4550 toGround();
4551 }
4552
4553 return;
4554
4555 case CSI_ENTRY:
4556 // 00-17, 19, 1C-1F --> execute
4557 if (ch <= 0x1F) {
4558 handleControlChar(ch);
4559 }
4560
4561 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
4562 if ((ch >= 0x20) && (ch <= 0x2F)) {
4563 collect(ch);
4564 scanState = ScanState.CSI_INTERMEDIATE;
4565 }
4566
4567 // 30-39, 3B --> param, then switch to CSI_PARAM
4568 if ((ch >= '0') && (ch <= '9')) {
4569 param((byte) ch);
4570 scanState = ScanState.CSI_PARAM;
4571 }
4572 if (ch == ';') {
4573 param((byte) ch);
4574 scanState = ScanState.CSI_PARAM;
4575 }
4576
4577 // 3C-3F --> collect, then switch to CSI_PARAM
4578 if ((ch >= 0x3C) && (ch <= 0x3F)) {
4579 collect(ch);
4580 scanState = ScanState.CSI_PARAM;
4581 }
4582
4583 // 40-7E --> dispatch, then switch to GROUND
4584 if ((ch >= 0x40) && (ch <= 0x7E)) {
4585 switch (ch) {
4586 case '@':
4587 // ICH - Insert character
4588 ich();
4589 break;
4590 case 'A':
4591 // CUU - Cursor up
4592 cuu();
4593 break;
4594 case 'B':
4595 // CUD - Cursor down
4596 cud();
4597 break;
4598 case 'C':
4599 // CUF - Cursor forward
4600 cuf();
4601 break;
4602 case 'D':
4603 // CUB - Cursor backward
4604 cub();
4605 break;
4606 case 'E':
4607 // CNL - Cursor down and to column 1
4608 if (type == DeviceType.XTERM) {
4609 cnl();
4610 }
4611 break;
4612 case 'F':
4613 // CPL - Cursor up and to column 1
4614 if (type == DeviceType.XTERM) {
4615 cpl();
4616 }
4617 break;
4618 case 'G':
4619 // CHA - Cursor to column # in current row
4620 if (type == DeviceType.XTERM) {
4621 cha();
4622 }
4623 break;
4624 case 'H':
4625 // CUP - Cursor position
4626 cup();
4627 break;
4628 case 'I':
4629 // CHT - Cursor forward X tab stops (default 1)
4630 if (type == DeviceType.XTERM) {
4631 cht();
4632 }
4633 break;
4634 case 'J':
4635 // ED - Erase in display
4636 ed();
4637 break;
4638 case 'K':
4639 // EL - Erase in line
4640 el();
4641 break;
4642 case 'L':
4643 // IL - Insert line
4644 il();
4645 break;
4646 case 'M':
4647 // DL - Delete line
4648 dl();
4649 break;
4650 case 'N':
4651 case 'O':
4652 break;
4653 case 'P':
4654 // DCH - Delete character
4655 dch();
4656 break;
4657 case 'Q':
4658 case 'R':
4659 break;
4660 case 'S':
4661 // Scroll up X lines (default 1)
4662 if (type == DeviceType.XTERM) {
4663 su();
4664 }
4665 break;
4666 case 'T':
4667 // Scroll down X lines (default 1)
4668 if (type == DeviceType.XTERM) {
4669 sd();
4670 }
4671 break;
4672 case 'U':
4673 case 'V':
4674 case 'W':
4675 break;
4676 case 'X':
4677 if ((type == DeviceType.VT220)
4678 || (type == DeviceType.XTERM)) {
4679
4680 // ECH - Erase character
4681 ech();
4682 }
4683 break;
4684 case 'Y':
4685 break;
4686 case 'Z':
4687 // CBT - Cursor backward X tab stops (default 1)
4688 if (type == DeviceType.XTERM) {
4689 cbt();
4690 }
4691 break;
4692 case '[':
4693 case '\\':
4694 case ']':
4695 case '^':
4696 case '_':
4697 break;
4698 case '`':
4699 // HPA - Cursor to column # in current row. Same as CHA
4700 if (type == DeviceType.XTERM) {
4701 cha();
4702 }
4703 break;
4704 case 'a':
4705 // HPR - Cursor right. Same as CUF
4706 if (type == DeviceType.XTERM) {
4707 cuf();
4708 }
4709 break;
4710 case 'b':
4711 // REP - Repeat last char X times
4712 if (type == DeviceType.XTERM) {
4713 rep();
4714 }
4715 break;
4716 case 'c':
4717 // DA - Device attributes
4718 da();
4719 break;
4720 case 'd':
4721 // VPA - Cursor to row, current column.
4722 if (type == DeviceType.XTERM) {
4723 vpa();
4724 }
4725 break;
4726 case 'e':
4727 // VPR - Cursor down. Same as CUD
4728 if (type == DeviceType.XTERM) {
4729 cud();
4730 }
4731 break;
4732 case 'f':
4733 // HVP - Horizontal and vertical position
4734 hvp();
4735 break;
4736 case 'g':
4737 // TBC - Tabulation clear
4738 tbc();
4739 break;
4740 case 'h':
4741 // Sets an ANSI or DEC private toggle
4742 setToggle(true);
4743 break;
4744 case 'i':
4745 if ((type == DeviceType.VT220)
4746 || (type == DeviceType.XTERM)) {
4747
4748 // Printer functions
4749 printerFunctions();
4750 }
4751 break;
4752 case 'j':
4753 case 'k':
4754 break;
4755 case 'l':
4756 // Sets an ANSI or DEC private toggle
4757 setToggle(false);
4758 break;
4759 case 'm':
4760 // SGR - Select graphics rendition
4761 sgr();
4762 break;
4763 case 'n':
4764 // DSR - Device status report
4765 dsr();
4766 break;
4767 case 'o':
4768 case 'p':
4769 break;
4770 case 'q':
4771 // DECLL - Load leds
4772 // Not supported
4773 break;
4774 case 'r':
4775 // DECSTBM - Set top and bottom margins
4776 decstbm();
4777 break;
4778 case 's':
4779 // Save cursor (ANSI.SYS)
4780 if (type == DeviceType.XTERM) {
4781 savedState.cursorX = currentState.cursorX;
4782 savedState.cursorY = currentState.cursorY;
4783 }
4784 break;
4785 case 't':
4786 break;
4787 case 'u':
4788 // Restore cursor (ANSI.SYS)
4789 if (type == DeviceType.XTERM) {
4790 cursorPosition(savedState.cursorY, savedState.cursorX);
4791 }
4792 break;
4793 case 'v':
4794 case 'w':
4795 break;
4796 case 'x':
4797 // DECREQTPARM - Request terminal parameters
4798 decreqtparm();
4799 break;
4800 case 'y':
4801 case 'z':
4802 case '{':
4803 case '|':
4804 case '}':
4805 case '~':
4806 break;
4807 }
4808 toGround();
4809 }
4810
4811 // 7F --> ignore
4812
4813 // 0x9C goes to GROUND
4814 if (ch == 0x9C) {
4815 toGround();
4816 }
4817
4818 // 0x3A goes to CSI_IGNORE
4819 if (ch == 0x3A) {
4820 scanState = ScanState.CSI_IGNORE;
4821 }
4822 return;
4823
4824 case CSI_PARAM:
4825 // 00-17, 19, 1C-1F --> execute
4826 if (ch <= 0x1F) {
4827 handleControlChar(ch);
4828 }
4829
4830 // 20-2F --> collect, then switch to CSI_INTERMEDIATE
4831 if ((ch >= 0x20) && (ch <= 0x2F)) {
4832 collect(ch);
4833 scanState = ScanState.CSI_INTERMEDIATE;
4834 }
4835
4836 // 30-39, 3B --> param
4837 if ((ch >= '0') && (ch <= '9')) {
4838 param((byte) ch);
4839 }
4840 if (ch == ';') {
4841 param((byte) ch);
4842 }
4843
4844 // 0x3A goes to CSI_IGNORE
4845 if (ch == 0x3A) {
4846 scanState = ScanState.CSI_IGNORE;
4847 }
4848 // 0x3C-3F goes to CSI_IGNORE
4849 if ((ch >= 0x3C) && (ch <= 0x3F)) {
4850 scanState = ScanState.CSI_IGNORE;
4851 }
4852
4853 // 40-7E --> dispatch, then switch to GROUND
4854 if ((ch >= 0x40) && (ch <= 0x7E)) {
4855 switch (ch) {
4856 case '@':
4857 // ICH - Insert character
4858 ich();
4859 break;
4860 case 'A':
4861 // CUU - Cursor up
4862 cuu();
4863 break;
4864 case 'B':
4865 // CUD - Cursor down
4866 cud();
4867 break;
4868 case 'C':
4869 // CUF - Cursor forward
4870 cuf();
4871 break;
4872 case 'D':
4873 // CUB - Cursor backward
4874 cub();
4875 break;
4876 case 'E':
4877 // CNL - Cursor down and to column 1
4878 if (type == DeviceType.XTERM) {
4879 cnl();
4880 }
4881 break;
4882 case 'F':
4883 // CPL - Cursor up and to column 1
4884 if (type == DeviceType.XTERM) {
4885 cpl();
4886 }
4887 break;
4888 case 'G':
4889 // CHA - Cursor to column # in current row
4890 if (type == DeviceType.XTERM) {
4891 cha();
4892 }
4893 break;
4894 case 'H':
4895 // CUP - Cursor position
4896 cup();
4897 break;
4898 case 'I':
4899 // CHT - Cursor forward X tab stops (default 1)
4900 if (type == DeviceType.XTERM) {
4901 cht();
4902 }
4903 break;
4904 case 'J':
4905 // ED - Erase in display
4906 ed();
4907 break;
4908 case 'K':
4909 // EL - Erase in line
4910 el();
4911 break;
4912 case 'L':
4913 // IL - Insert line
4914 il();
4915 break;
4916 case 'M':
4917 // DL - Delete line
4918 dl();
4919 break;
4920 case 'N':
4921 case 'O':
4922 break;
4923 case 'P':
4924 // DCH - Delete character
4925 dch();
4926 break;
4927 case 'Q':
4928 case 'R':
4929 break;
4930 case 'S':
4931 // Scroll up X lines (default 1)
4932 if (type == DeviceType.XTERM) {
4933 su();
4934 }
4935 break;
4936 case 'T':
4937 // Scroll down X lines (default 1)
4938 if (type == DeviceType.XTERM) {
4939 sd();
4940 }
4941 break;
4942 case 'U':
4943 case 'V':
4944 case 'W':
4945 break;
4946 case 'X':
4947 if ((type == DeviceType.VT220)
4948 || (type == DeviceType.XTERM)) {
4949
4950 // ECH - Erase character
4951 ech();
4952 }
4953 break;
4954 case 'Y':
4955 break;
4956 case 'Z':
4957 // CBT - Cursor backward X tab stops (default 1)
4958 if (type == DeviceType.XTERM) {
4959 cbt();
4960 }
4961 break;
4962 case '[':
4963 case '\\':
4964 case ']':
4965 case '^':
4966 case '_':
4967 break;
4968 case '`':
4969 // HPA - Cursor to column # in current row. Same as CHA
4970 if (type == DeviceType.XTERM) {
4971 cha();
4972 }
4973 break;
4974 case 'a':
4975 // HPR - Cursor right. Same as CUF
4976 if (type == DeviceType.XTERM) {
4977 cuf();
4978 }
4979 break;
4980 case 'b':
4981 // REP - Repeat last char X times
4982 if (type == DeviceType.XTERM) {
4983 rep();
4984 }
4985 break;
4986 case 'c':
4987 // DA - Device attributes
4988 da();
4989 break;
4990 case 'd':
4991 // VPA - Cursor to row, current column.
4992 if (type == DeviceType.XTERM) {
4993 vpa();
4994 }
4995 break;
4996 case 'e':
4997 // VPR - Cursor down. Same as CUD
4998 if (type == DeviceType.XTERM) {
4999 cud();
5000 }
5001 break;
5002 case 'f':
5003 // HVP - Horizontal and vertical position
5004 hvp();
5005 break;
5006 case 'g':
5007 // TBC - Tabulation clear
5008 tbc();
5009 break;
5010 case 'h':
5011 // Sets an ANSI or DEC private toggle
5012 setToggle(true);
5013 break;
5014 case 'i':
5015 if ((type == DeviceType.VT220)
5016 || (type == DeviceType.XTERM)) {
5017
5018 // Printer functions
5019 printerFunctions();
5020 }
5021 break;
5022 case 'j':
5023 case 'k':
5024 break;
5025 case 'l':
5026 // Sets an ANSI or DEC private toggle
5027 setToggle(false);
5028 break;
5029 case 'm':
5030 // SGR - Select graphics rendition
5031 sgr();
5032 break;
5033 case 'n':
5034 // DSR - Device status report
5035 dsr();
5036 break;
5037 case 'o':
5038 case 'p':
5039 break;
5040 case 'q':
5041 // DECLL - Load leds
5042 // Not supported
5043 break;
5044 case 'r':
5045 // DECSTBM - Set top and bottom margins
5046 decstbm();
5047 break;
5048 case 's':
5049 case 't':
5050 case 'u':
5051 case 'v':
5052 case 'w':
5053 break;
5054 case 'x':
5055 // DECREQTPARM - Request terminal parameters
5056 decreqtparm();
5057 break;
5058 case 'y':
5059 case 'z':
5060 case '{':
5061 case '|':
5062 case '}':
5063 case '~':
5064 break;
5065 }
5066 toGround();
5067 }
5068
5069 // 7F --> ignore
5070 return;
5071
5072 case CSI_INTERMEDIATE:
5073 // 00-17, 19, 1C-1F --> execute
5074 if (ch <= 0x1F) {
5075 handleControlChar(ch);
5076 }
5077
5078 // 20-2F --> collect
5079 if ((ch >= 0x20) && (ch <= 0x2F)) {
5080 collect(ch);
5081 }
5082
5083 // 0x30-3F goes to CSI_IGNORE
5084 if ((ch >= 0x30) && (ch <= 0x3F)) {
5085 scanState = ScanState.CSI_IGNORE;
5086 }
5087
5088 // 40-7E --> dispatch, then switch to GROUND
5089 if ((ch >= 0x40) && (ch <= 0x7E)) {
5090 switch (ch) {
5091 case '@':
5092 case 'A':
5093 case 'B':
5094 case 'C':
5095 case 'D':
5096 case 'E':
5097 case 'F':
5098 case 'G':
5099 case 'H':
5100 case 'I':
5101 case 'J':
5102 case 'K':
5103 case 'L':
5104 case 'M':
5105 case 'N':
5106 case 'O':
5107 case 'P':
5108 case 'Q':
5109 case 'R':
5110 case 'S':
5111 case 'T':
5112 case 'U':
5113 case 'V':
5114 case 'W':
5115 case 'X':
5116 case 'Y':
5117 case 'Z':
5118 case '[':
5119 case '\\':
5120 case ']':
5121 case '^':
5122 case '_':
5123 case '`':
5124 case 'a':
5125 case 'b':
5126 case 'c':
5127 case 'd':
5128 case 'e':
5129 case 'f':
5130 case 'g':
5131 case 'h':
5132 case 'i':
5133 case 'j':
5134 case 'k':
5135 case 'l':
5136 case 'm':
5137 case 'n':
5138 case 'o':
5139 break;
5140 case 'p':
5141 if (((type == DeviceType.VT220)
5142 || (type == DeviceType.XTERM))
5143 && (collectBuffer.get(collectBuffer.size() - 1) == '\"')
5144 ) {
5145 // DECSCL - compatibility level
5146 decscl();
5147 }
5148 if ((type == DeviceType.XTERM)
5149 && (collectBuffer.get(collectBuffer.size() - 1) == '!')
5150 ) {
5151 // DECSTR - Soft terminal reset
5152 decstr();
5153 }
5154 break;
5155 case 'q':
5156 if (((type == DeviceType.VT220)
5157 || (type == DeviceType.XTERM))
5158 && (collectBuffer.get(collectBuffer.size() - 1) == '\"')
5159 ) {
5160 // DECSCA
5161 decsca();
5162 }
5163 break;
5164 case 'r':
5165 case 's':
5166 case 't':
5167 case 'u':
5168 case 'v':
5169 case 'w':
5170 case 'x':
5171 case 'y':
5172 case 'z':
5173 case '{':
5174 case '|':
5175 case '}':
5176 case '~':
5177 break;
5178 }
5179 toGround();
5180 }
5181
5182 // 7F --> ignore
5183 return;
5184
5185 case CSI_IGNORE:
5186 // 00-17, 19, 1C-1F --> execute
5187 if (ch <= 0x1F) {
5188 handleControlChar(ch);
5189 }
5190
5191 // 20-2F --> collect
5192 if ((ch >= 0x20) && (ch <= 0x2F)) {
5193 collect(ch);
5194 }
5195
5196 // 40-7E --> ignore, then switch to GROUND
5197 if ((ch >= 0x40) && (ch <= 0x7E)) {
5198 toGround();
5199 }
5200
5201 // 20-3F, 7F --> ignore
5202
5203 return;
5204
5205 case DCS_ENTRY:
5206
5207 // 0x9C goes to GROUND
5208 if (ch == 0x9C) {
5209 toGround();
5210 }
5211
5212 // 0x1B 0x5C goes to GROUND
5213 if (ch == 0x1B) {
5214 collect(ch);
5215 }
5216 if (ch == 0x5C) {
5217 if ((collectBuffer.size() > 0)
5218 && (collectBuffer.get(collectBuffer.size() - 1) == 0x1B)) {
5219 toGround();
5220 }
5221 }
5222
5223 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
5224 if ((ch >= 0x20) && (ch <= 0x2F)) {
5225 collect(ch);
5226 scanState = ScanState.DCS_INTERMEDIATE;
5227 }
5228
5229 // 30-39, 3B --> param, then switch to DCS_PARAM
5230 if ((ch >= '0') && (ch <= '9')) {
5231 param((byte) ch);
5232 scanState = ScanState.DCS_PARAM;
5233 }
5234 if (ch == ';') {
5235 param((byte) ch);
5236 scanState = ScanState.DCS_PARAM;
5237 }
5238
5239 // 3C-3F --> collect, then switch to DCS_PARAM
5240 if ((ch >= 0x3C) && (ch <= 0x3F)) {
5241 collect(ch);
5242 scanState = ScanState.DCS_PARAM;
5243 }
5244
5245 // 00-17, 19, 1C-1F, 7F --> ignore
5246
5247 // 0x3A goes to DCS_IGNORE
5248 if (ch == 0x3F) {
5249 scanState = ScanState.DCS_IGNORE;
5250 }
5251
5252 // 0x40-7E goes to DCS_PASSTHROUGH
5253 if ((ch >= 0x40) && (ch <= 0x7E)) {
5254 scanState = ScanState.DCS_PASSTHROUGH;
5255 }
5256 return;
5257
5258 case DCS_INTERMEDIATE:
5259
5260 // 0x9C goes to GROUND
5261 if (ch == 0x9C) {
5262 toGround();
5263 }
5264
5265 // 0x1B 0x5C goes to GROUND
5266 if (ch == 0x1B) {
5267 collect(ch);
5268 }
5269 if (ch == 0x5C) {
5270 if ((collectBuffer.size() > 0) &&
5271 (collectBuffer.get(collectBuffer.size() - 1) == 0x1B)) {
5272 toGround();
5273 }
5274 }
5275
5276 // 0x30-3F goes to DCS_IGNORE
5277 if ((ch >= 0x30) && (ch <= 0x3F)) {
5278 scanState = ScanState.DCS_IGNORE;
5279 }
5280
5281 // 0x40-7E goes to DCS_PASSTHROUGH
5282 if ((ch >= 0x40) && (ch <= 0x7E)) {
5283 scanState = ScanState.DCS_PASSTHROUGH;
5284 }
5285
5286 // 00-17, 19, 1C-1F, 7F --> ignore
5287 return;
5288
5289 case DCS_PARAM:
5290
5291 // 0x9C goes to GROUND
5292 if (ch == 0x9C) {
5293 toGround();
5294 }
5295
5296 // 0x1B 0x5C goes to GROUND
5297 if (ch == 0x1B) {
5298 collect(ch);
5299 }
5300 if (ch == 0x5C) {
5301 if ((collectBuffer.size() > 0) &&
5302 (collectBuffer.get(collectBuffer.size() - 1) == 0x1B)) {
5303 toGround();
5304 }
5305 }
5306
5307 // 20-2F --> collect, then switch to DCS_INTERMEDIATE
5308 if ((ch >= 0x20) && (ch <= 0x2F)) {
5309 collect(ch);
5310 scanState = ScanState.DCS_INTERMEDIATE;
5311 }
5312
5313 // 30-39, 3B --> param
5314 if ((ch >= '0') && (ch <= '9')) {
5315 param((byte) ch);
5316 }
5317 if (ch == ';') {
5318 param((byte) ch);
5319 }
5320
5321 // 00-17, 19, 1C-1F, 7F --> ignore
5322
5323 // 0x3A, 3C-3F goes to DCS_IGNORE
5324 if (ch == 0x3F) {
5325 scanState = ScanState.DCS_IGNORE;
5326 }
5327 if ((ch >= 0x3C) && (ch <= 0x3F)) {
5328 scanState = ScanState.DCS_IGNORE;
5329 }
5330
5331 // 0x40-7E goes to DCS_PASSTHROUGH
5332 if ((ch >= 0x40) && (ch <= 0x7E)) {
5333 scanState = ScanState.DCS_PASSTHROUGH;
5334 }
5335 return;
5336
5337 case DCS_PASSTHROUGH:
5338 // 0x9C goes to GROUND
5339 if (ch == 0x9C) {
5340 toGround();
5341 }
5342
5343 // 0x1B 0x5C goes to GROUND
5344 if (ch == 0x1B) {
5345 collect(ch);
5346 }
5347 if (ch == 0x5C) {
5348 if ((collectBuffer.size() > 0)
5349 && (collectBuffer.get(collectBuffer.size() - 1) == 0x1B)
5350 ) {
5351 toGround();
5352 }
5353 }
5354
5355 // 00-17, 19, 1C-1F, 20-7E --> put
5356 // TODO
5357 if (ch <= 0x17) {
5358 return;
5359 }
5360 if (ch == 0x19) {
5361 return;
5362 }
5363 if ((ch >= 0x1C) && (ch <= 0x1F)) {
5364 return;
5365 }
5366 if ((ch >= 0x20) && (ch <= 0x7E)) {
5367 return;
5368 }
5369
5370 // 7F --> ignore
5371
5372 return;
5373
5374 case DCS_IGNORE:
5375 // 00-17, 19, 1C-1F, 20-7F --> ignore
5376
5377 // 0x9C goes to GROUND
5378 if (ch == 0x9C) {
5379 toGround();
5380 }
5381
5382 return;
5383
5384 case SOSPMAPC_STRING:
5385 // 00-17, 19, 1C-1F, 20-7F --> ignore
5386
5387 // 0x9C goes to GROUND
5388 if (ch == 0x9C) {
5389 toGround();
5390 }
5391
5392 return;
5393
5394 case OSC_STRING:
5395 // Special case for Xterm: OSC can pass control characters
5396 if ((ch == 0x9C) || (ch <= 0x07)) {
5397 oscPut(ch);
5398 }
5399
5400 // 00-17, 19, 1C-1F --> ignore
5401
5402 // 20-7F --> osc_put
5403 if ((ch >= 0x20) && (ch <= 0x7F)) {
5404 oscPut(ch);
5405 }
5406
5407 // 0x9C goes to GROUND
5408 if (ch == 0x9C) {
5409 toGround();
5410 }
5411
5412 return;
5413
5414 case VT52_DIRECT_CURSOR_ADDRESS:
5415 // This is a special case for the VT52 sequence "ESC Y l c"
5416 if (collectBuffer.size() == 0) {
5417 collect(ch);
5418 } else if (collectBuffer.size() == 1) {
5419 // We've got the two characters, one in the buffer and the
5420 // other in ch.
5421 cursorPosition(collectBuffer.get(0) - '\040', ch - '\040');
5422 toGround();
5423 }
5424 return;
5425 }
5426
5427 }
5428
5429 /**
5430 * Expose current cursor X to outside world.
5431 *
5432 * @return current cursor X
5433 */
5434 public final int getCursorX() {
5435 return currentState.cursorX;
5436 }
5437
5438 /**
5439 * Expose current cursor Y to outside world.
5440 *
5441 * @return current cursor Y
5442 */
5443 public final int getCursorY() {
5444 return currentState.cursorY;
5445 }
5446
5447 /**
5448 * Read function runs on a separate thread.
5449 */
5450 public void run() {
5451 boolean utf8 = false;
5452 boolean done = false;
5453
5454 if (type == DeviceType.XTERM) {
5455 utf8 = true;
5456 }
5457
5458 // available() will often return > 1, so we need to read in chunks to
5459 // stay caught up.
5460 char [] readBufferUTF8 = null;
5461 byte [] readBuffer = null;
5462 if (utf8) {
5463 readBufferUTF8 = new char[128];
5464 } else {
5465 readBuffer = new byte[128];
5466 }
5467
5468 while (!done && !stopReaderThread) {
5469 try {
5470 int n = inputStream.available();
5471 // System.err.printf("available() %d\n", n); System.err.flush();
5472 if (utf8) {
5473 if (readBufferUTF8.length < n) {
5474 // The buffer wasn't big enough, make it huger
5475 int newSizeHalf = Math.max(readBufferUTF8.length, n);
5476
5477 readBufferUTF8 = new char[newSizeHalf * 2];
5478 }
5479 } else {
5480 if (readBuffer.length < n) {
5481 // The buffer wasn't big enough, make it huger
5482 int newSizeHalf = Math.max(readBuffer.length, n);
5483 readBuffer = new byte[newSizeHalf * 2];
5484 }
5485 }
5486
5487 int rc = -1;
5488 if (utf8) {
5489 rc = input.read(readBufferUTF8, 0,
5490 readBufferUTF8.length);
5491 } else {
5492 rc = inputStream.read(readBuffer, 0,
5493 readBuffer.length);
5494 }
5495 // System.err.printf("read() %d\n", rc); System.err.flush();
5496 if (rc == -1) {
5497 // This is EOF
5498 done = true;
5499 } else {
5500 for (int i = 0; i < rc; i++) {
5501 int ch = 0;
5502 if (utf8) {
5503 ch = readBufferUTF8[i];
5504 } else {
5505 ch = readBuffer[i];
5506 }
5507 // Don't step on UI events
5508 synchronized (this) {
5509 consume((char)ch);
5510 }
5511 }
5512 }
5513 // System.err.println("end while loop"); System.err.flush();
5514 } catch (IOException e) {
5515 e.printStackTrace();
5516 done = true;
5517 }
5518 } // while ((done == false) && (stopReaderThread == false))
5519
5520 // Let the rest of the world know that I am done.
5521 stopReaderThread = true;
5522
5523 // System.err.println("*** run() exiting..."); System.err.flush();
5524 }
5525
5526 }