1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133
|
/*
* SSHTools - Java SSH2 API
*
* Copyright (C) 2002-2003 Lee David Painter and Contributors.
*
* Contributions made by:
*
* Brett Smith
* Richard Pernavas
* Erwin Bolwidt
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/**
* SSHTools - Java SSH API The contents of this package has been derived from
* the TelnetD library available from http://sourceforge.net/projects/telnetd
* The original license of the source code is as follows: TelnetD library
* (embeddable telnet daemon) Copyright (C) 2000 Dieter Wimberger This library
* is free software; you can either redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License version 2.1,1999 as
* published by the Free Software Foundation (see copy received along with the
* library), or under the terms of the BSD-style license received along with
* this library.
*/
package com.sshtools.daemon.terminal;
import com.sshtools.j2ssh.io.*;
import com.sshtools.j2ssh.session.*;
import java.io.*;
/**
*
*
* @author $author$
* @version $Revision: 1.13 $
*/
public class TerminalIO implements PseudoTerminal {
// implements BasicTerminalIO {
/** */
public static final int EOL_CRLF = 1;
/** */
public static final int EOL_CR = 2;
/** */
public static final int[] HOME = { 0, 0 };
/** */
public static final int IOERROR = -1; //CTRL-D beim login
/** */
public static final int
// Positioning 10xx
UP = 1001; //CTRL-D beim login
/** */
public static final int DOWN = 1002; //CTRL-D beim login
/** */
public static final int RIGHT = 1003; //CTRL-D beim login
/** */
public static final int LEFT = 1004; //CTRL-D beim login
/** */
public static final int STORECURSOR = 1051; //CTRL-D beim login
/** */
public static final int RESTORECURSOR = 1052; //CTRL-D beim login
/** */
public static final int
// Erasing 11xx
EEOL = 1100; //CTRL-D beim login
/** */
public static final int EBOL = 1101; //CTRL-D beim login
/** */
public static final int EEL = 1103; //CTRL-D beim login
/** */
public static final int EEOS = 1104; //CTRL-D beim login
/** */
public static final int EBOS = 1105; //CTRL-D beim login
/** */
public static final int EES = 1106; //CTRL-D beim login
/** */
public static final int
// Escape Sequence-ing 12xx
ESCAPE = 1200; //CTRL-D beim login
/** */
public static final int BYTEMISSING = 1201; //CTRL-D beim login
/** */
public static final int UNRECOGNIZED = 1202; //CTRL-D beim login
/** */
public static final int
// Control Characters 13xx
ENTER = 10; //CTRL-D beim login
/** */
public static final int
//ENTER = 1300, //LF is ENTER at the moment
TABULATOR = 1301; //CTRL-D beim login
/** */
public static final int DELETE = 1302; //CTRL-D beim login
/** */
public static final int BACKSPACE = 1303; //CTRL-D beim login
/** */
public static final int COLORINIT = 1304; //CTRL-D beim login
/** */
public static final int HANDLED = 1305; //CTRL-D beim login
/** */
public static final int LOGOUTREQUEST = 1306; //CTRL-D beim login
/** */
public static final int LineUpdate = 475;
/** */
public static final int CharacterUpdate = 476;
/** */
public static final int ScreenpartUpdate = 477;
/** */
public static final int EditBuffer = 575;
/** */
public static final int LineEditBuffer = 576;
/** */
public static final int BEL = 7;
/** */
public static final int BS = 8;
/** */
public static final int DEL = 127;
/** */
public static final int CR = 13;
/** */
public static final int LF = 10;
/** */
public static final int FCOLOR = 10001;
/** */
public static final int BCOLOR = 10002;
/** */
public static final int STYLE = 10003;
/** */
public static final int RESET = 10004;
/** */
public static final int BOLD = 1;
/** */
public static final int BOLD_OFF = 22;
/** */
public static final int ITALIC = 3;
/** */
public static final int ITALIC_OFF = 23;
/** */
public static final int BLINK = 5;
/** */
public static final int BLINK_OFF = 25;
/** */
public static final int UNDERLINED = 4;
/** */
public static final int UNDERLINED_OFF = 24;
//Constants
/** */
public static final int BLACK = 30;
/** */
public static final int RED = 31;
/** */
public static final int GREEN = 32;
/** */
public static final int YELLOW = 33;
/** */
public static final int BLUE = 34;
/** */
public static final int MAGENTA = 35;
/** */
public static final int CYAN = 36;
/** */
public static final int white = 37;
/** */
public static final String CRLF = "\r\n";
private Terminal terminal;
private DataInputStream in;
private DataOutputStream out;
private boolean closing;
private boolean cr;
private boolean nl;
private boolean acousticSignalling; //flag for accoustic signalling
private boolean autoflush; //flag for autoflushing mode
private int eol = EOL_CRLF;
private int lastByte;
private boolean uselast = false;
private Colorizer color = Colorizer.getReference();
private String term;
private int cols;
private int rows;
private PipedInputStream masterIn;
private PipedOutputStream masterOut;
private InputStream slaveIn;
private OutputStream slaveOut;
private IOStreamConnector ios;
//private OutputStream masterOut;
// private OutputStream slaveOut = new SlaveOutputStream();
public TerminalIO(InputStream in, OutputStream out, String term, int cols,
int rows) throws IOException {
attachStreams(in, out);
this.term = term;
this.rows = rows;
this.cols = cols;
acousticSignalling = true;
masterOut = new PipedOutputStream();
masterIn = new PipedInputStream(masterOut);
autoflush = true;
closing = false;
cr = false;
//set default terminal
setDefaultTerminal();
}
/**
*
*
* @return
*/
public InputStream getMasterInputStream() {
return masterIn;
}
/**
*
*
* @param slaveIn
*/
public void bindSlaveInputStream(InputStream slaveIn) {
this.slaveIn = slaveIn;
this.ios = new IOStreamConnector(slaveIn, masterOut);
}
/**
*
*
* @param slaveOut
*/
public void bindSlaveOutputStream(OutputStream slaveOut) {
this.slaveOut = slaveOut;
}
/**
*
*
* @return
*/
public OutputStream getSlaveOutputStream() {
return slaveOut;
}
/**
*
*
* @return
*/
public int getWidth() {
return 0;
}
/**
*
*
* @return
*/
public int getHeight() {
return 0;
}
/**
*
*
* @return
*/
public String getTerm() {
return terminal.getName();
}
/**
*
*
* @return
*/
public String getEncodedTerminalModes() {
return "";
}
/* public void setMasterOutputStream(OutputStream masterOut) {
this.masterOut = masterOut;
}*/
public InputStream getAttachedInputStream() throws IOException {
if (in == null) {
throw new IOException(
"The teminal is not attached to an InputStream");
}
return in;
}
/**
*
*
* @return
*
* @throws IOException
*/
public OutputStream getAttachedOutputStream() throws IOException {
if (out == null) {
throw new IOException(
"The terminal is not attached to an OutputStream");
}
return out;
}
/**
*
*/
public void detachStreams() {
this.in = null;
this.out = null;
}
/**
*
*
* @return
*/
public int getEOL() {
return eol;
}
/**
*
*
* @return
*/
public String getEOLString() {
return ((eol == EOL_CR) ? "\r" : "\r\n");
}
/**
*
*
* @param eol
*/
public void setEOL(int eol) {
this.eol = eol;
}
/**
*
*
* @param in
* @param out
*/
public void attachStreams(InputStream in, OutputStream out) {
this.in = new DataInputStream(new BufferedInputStream(in));
this.out = new DataOutputStream(new BufferedOutputStream(out));
}
/**
*
*
* @return
*
* @throws IOException
*/
public int read() throws IOException {
int i = stripCRSeq(rawread());
//translate possible control sequences
i = terminal.translateControlCharacter(i);
if ((i > 256) && (i == ESCAPE)) {
i = handleEscapeSequence(i);
}
return i;
}
/**
*
*
* @param ch
*
* @throws IOException
*/
public void write(char ch) throws IOException {
write((byte) ch);
if (autoflush) {
flush();
}
}
/**
*
*
* @param str
*
* @throws IOException
*/
public void write(String str) throws IOException {
write((color.colorize(str, terminal.supportsSGR())).getBytes());
if (autoflush) {
flush();
}
}
/**
*
*
* @param str
*
* @throws IOException
*/
public void println(String str) throws IOException {
write(str);
write(getEOLString().getBytes());
if (autoflush) {
flush();
}
}
/**
*
*
* @throws IOException
*/
public void println() throws IOException {
write(getEOLString().getBytes());
if (autoflush) {
flush();
}
}
/**
*
*
* @throws IOException
*/
public void eraseToEndOfLine() throws IOException {
doErase(EEOL);
}
/**
*
*
* @throws IOException
*/
public void eraseToBeginOfLine() throws IOException {
doErase(EBOL);
}
/**
*
*
* @throws IOException
*/
public void eraseLine() throws IOException {
doErase(EEL);
}
/**
*
*
* @throws IOException
*/
public void eraseToEndOfScreen() throws IOException {
doErase(EEOS);
}
/**
*
*
* @throws IOException
*/
public void eraseToBeginOfScreen() throws IOException {
doErase(EBOS);
}
/**
*
*
* @throws IOException
*/
public void eraseScreen() throws IOException {
doErase(EES);
}
private void doErase(int funcConst) throws IOException {
write(terminal.getEraseSequence(funcConst));
if (autoflush) {
flush();
}
}
/**
*
*
* @param direction
* @param times
*
* @throws IOException
*/
public void moveCursor(int direction, int times) throws IOException {
write(terminal.getCursorMoveSequence(direction, times));
if (autoflush) {
flush();
}
}
/**
*
*
* @param times
*
* @throws IOException
*/
public void moveLeft(int times) throws IOException {
moveCursor(LEFT, times);
}
/**
*
*
* @param times
*
* @throws IOException
*/
public void moveRight(int times) throws IOException {
moveCursor(RIGHT, times);
}
/**
*
*
* @param times
*
* @throws IOException
*/
public void moveUp(int times) throws IOException {
moveCursor(UP, times);
}
/**
*
*
* @param times
*
* @throws IOException
*/
public void moveDown(int times) throws IOException {
moveCursor(DOWN, times);
}
/**
*
*
* @param row
* @param col
*
* @throws IOException
*/
public void setCursor(int row, int col) throws IOException {
int[] pos = new int[2];
pos[0] = row;
pos[1] = col;
write(terminal.getCursorPositioningSequence(pos));
if (autoflush) {
flush();
}
}
/**
*
*
* @throws IOException
*/
public void homeCursor() throws IOException {
write(terminal.getCursorPositioningSequence(HOME));
if (autoflush) {
flush();
}
}
/**
*
*
* @throws IOException
*/
public void storeCursor() throws IOException {
write(terminal.getSpecialSequence(STORECURSOR));
}
/**
*
*
* @throws IOException
*/
public void restoreCursor() throws IOException {
write(terminal.getSpecialSequence(RESTORECURSOR));
}
/**
*
*
* @throws IOException
*/
public void closeInput() throws IOException {
if (in == null) {
throw new IOException(
"The terminal is not attached to an inputstream");
}
in.close();
}
private int read16int() throws IOException {
if (in == null) {
throw new IOException(
"The terminal is not attached to an inputstream");
}
return in.readUnsignedShort();
}
private int rawread() throws IOException {
if (in == null) {
throw new IOException(
"The terminal is not attached to an inputstream");
}
return in.readUnsignedByte();
}
private int stripCRSeq(int input) throws IOException {
if (in == null) {
throw new IOException(
"The terminal is not attached to an inputstream");
}
// Check for CR
if (input == CR) {
// Mark the current position and test for LF
in.mark(1);
// If there is more data to read, check for LF
if (in.available() > 0) {
int next = in.readUnsignedByte();
// If we don't have LF then reset back to the mark position
if (next != LF) {
in.reset();
}
}
return TerminalIO.ENTER;
}
return input;
}
/**
*
*
* @param b
*
* @throws IOException
*/
public void write(byte b) throws IOException {
if (out == null) {
throw new IOException(
"The terminal is not attached to an outputstream");
}
if (eol == EOL_CRLF) {
if (!cr && (b == 10)) {
out.write(13);
//if(masterOut!=null)
// masterOut.write(13);
}
//ensure CRLF(\r\n) is written for CR(\r) to adhere
//to the telnet protocol.
if (cr && (b != 10)) {
out.write(10);
// if(masterOut!=null)
// masterOut.write(10);
}
out.write(b);
// if(masterOut!=null)
// masterOut.write(b);
if (b == 13) {
cr = true;
} else {
cr = false;
}
} else {
out.write(b);
// if(masterOut!=null)
// masterOut.write(b);
}
}
/**
*
*
* @param i
*
* @throws IOException
*/
public void write(int i) throws IOException {
write((byte) i);
}
/**
*
*
* @param sequence
*
* @throws IOException
*/
public void write(byte[] sequence) throws IOException {
for (int z = 0; z < sequence.length; z++) {
write(sequence[z]);
}
}
/**
*
*
* @param sequence
*
* @throws IOException
*/
public void write(int[] sequence) throws IOException {
for (int j = 0; j < sequence.length; j++) {
write((byte) sequence[j]);
}
}
/**
*
*
* @throws IOException
*/
public void flush() throws IOException {
if (out == null) {
throw new IOException(
"The terminal is not attached to an outputstream");
}
// If were attached then flush, else ignore
out.flush();
}
/**
*
*
* @throws IOException
*/
public void closeOutput() throws IOException {
if (out == null) {
throw new IOException(
"The terminal is not attached to an outputstream");
}
closing = true;
out.close();
}
/**
*
*
* @param bool
*/
public void setSignalling(boolean bool) {
acousticSignalling = bool;
}
/**
*
*
* @return
*/
public boolean isSignalling() {
return acousticSignalling;
}
/**
*
*
* @throws IOException
*/
public void bell() throws IOException {
if (acousticSignalling) {
write(BEL);
}
if (autoflush) {
flush();
}
}
/**
*
*
* @param topmargin
* @param bottommargin
*
* @return
*
* @throws IOException
*/
public boolean defineScrollRegion(int topmargin, int bottommargin)
throws IOException {
if (terminal.supportsScrolling()) {
write(terminal.getScrollMarginsSequence(topmargin, bottommargin));
flush();
return true;
} else {
return false;
}
}
/**
*
*
* @param color
*
* @throws IOException
*/
public void setForegroundColor(int color) throws IOException {
if (terminal.supportsSGR()) {
write(terminal.getGRSequence(FCOLOR, color));
if (autoflush) {
flush();
}
}
}
/**
*
*
* @param color
*
* @throws IOException
*/
public void setBackgroundColor(int color) throws IOException {
if (terminal.supportsSGR()) {
//this method adds the offset to the fg color by itself
write(terminal.getGRSequence(BCOLOR, color + 10));
if (autoflush) {
flush();
}
}
}
/**
*
*
* @param b
*
* @throws IOException
*/
public void setBold(boolean b) throws IOException {
if (terminal.supportsSGR()) {
if (b) {
write(terminal.getGRSequence(STYLE, BOLD));
} else {
write(terminal.getGRSequence(STYLE, BOLD_OFF));
}
if (autoflush) {
flush();
}
}
}
/**
*
*
* @param b
*
* @throws IOException
*/
public void setUnderlined(boolean b) throws IOException {
if (terminal.supportsSGR()) {
if (b) {
write(terminal.getGRSequence(STYLE, UNDERLINED));
} else {
write(terminal.getGRSequence(STYLE, UNDERLINED_OFF));
}
if (autoflush) {
flush();
}
}
}
/**
*
*
* @param b
*
* @throws IOException
*/
public void setItalic(boolean b) throws IOException {
if (terminal.supportsSGR()) {
if (b) {
write(terminal.getGRSequence(STYLE, ITALIC));
} else {
write(terminal.getGRSequence(STYLE, ITALIC_OFF));
}
if (autoflush) {
flush();
}
}
}
/**
*
*
* @param b
*
* @throws IOException
*/
public void setBlink(boolean b) throws IOException {
if (terminal.supportsSGR()) {
if (b) {
write(terminal.getGRSequence(STYLE, BLINK));
} else {
write(terminal.getGRSequence(STYLE, BLINK_OFF));
}
if (autoflush) {
flush();
}
}
}
/**
*
*
* @throws IOException
*/
public void resetAttributes() throws IOException {
if (terminal.supportsSGR()) {
write(terminal.getGRSequence(RESET, 0));
}
}
private int handleEscapeSequence(int i) throws IOException {
if (i == ESCAPE) {
int[] bytebuf = new int[terminal.getAtomicSequenceLength()];
//fill atomic length
//FIXME: ensure CAN, broken Escapes etc.
for (int m = 0; m < bytebuf.length; m++) {
bytebuf[m] = read();
}
return terminal.translateEscapeSequence(bytebuf);
}
if (i == BYTEMISSING) {
//FIXME:longer escapes etc...
}
return HANDLED;
}
/**
*
*
* @return
*/
public boolean isAutoflushing() {
return autoflush;
}
/**
*
*
* @param b
*/
public void setAutoflushing(boolean b) {
autoflush = b;
}
/**
*
*
* @throws IOException
*/
public void close() throws IOException {
closeOutput();
}
/**
*
*
* @return
*/
public Terminal getTerminal() {
return terminal;
}
//getTerminal
public void setDefaultTerminal() throws IOException {
//set the terminal passing the negotiated string
setTerminal(term);
}
/**
*
*
* @param terminalName
*
* @throws IOException
*/
public void setTerminal(String terminalName) throws IOException {
terminal = TerminalFactory.newInstance(terminalName);
//Terminal is set we init it....
initTerminal();
}
private void initTerminal() throws IOException {
write(terminal.getInitSequence());
flush();
}
/**
*
*
* @return
*/
public int getRows() {
return rows;
}
/**
*
*
* @return
*/
public int getColumns() {
return cols;
}
}
//class TerminalIO
|