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