001 /*
002 * Copyright (C) 2012 eXo Platform SAS.
003 *
004 * This is free software; you can redistribute it and/or modify it
005 * under the terms of the GNU Lesser General Public License as
006 * published by the Free Software Foundation; either version 2.1 of
007 * the License, or (at your option) any later version.
008 *
009 * This software is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * You should have received a copy of the GNU Lesser General Public
015 * License along with this software; if not, write to the Free
016 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
017 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
018 */
019
020 package org.crsh.telnet.term;
021
022 import net.wimpi.telnetd.io.BasicTerminalIO;
023 import net.wimpi.telnetd.io.TerminalIO;
024 import net.wimpi.telnetd.net.Connection;
025 import net.wimpi.telnetd.net.ConnectionData;
026 import org.crsh.text.Color;
027 import org.crsh.text.Decoration;
028 import org.crsh.text.Style;
029 import org.crsh.term.*;
030 import org.crsh.term.spi.TermIO;
031
032 import java.io.EOFException;
033 import java.io.IOException;
034 import java.net.SocketException;
035 import java.util.HashMap;
036
037 public class TelnetIO implements TermIO {
038
039 /** . */
040 private final Connection conn;
041
042 /** . */
043 private final BasicTerminalIO termIO;
044
045 public TelnetIO(Connection conn) {
046 this.conn = conn;
047 this.termIO = conn.getTerminalIO();
048 }
049
050 public int read() throws IOException {
051 try {
052 return termIO.read();
053 }
054 catch (EOFException e) {
055 return TerminalIO.HANDLED;
056 }
057 catch (SocketException e) {
058 return TerminalIO.HANDLED;
059 }
060 }
061
062 public int getWidth() {
063 return termIO.getColumns();
064 }
065
066 public int getHeight() {
067 return termIO.getRows();
068 }
069
070 public String getProperty(String name) {
071 ConnectionData data = conn.getConnectionData();
072 if (data != null)
073 {
074 HashMap map = data.getEnvironment();
075 if (map != null) {
076 Object value = map.get(name);
077 if (value != null) {
078 return value.toString();
079 }
080 }
081 }
082 return null;
083 }
084
085 public CodeType decode(int code) {
086 switch (code) {
087 case 1304:
088 return CodeType.BEGINNING_OF_LINE;
089 case 5:
090 return CodeType.END_OF_LINE;
091 case 3:
092 return CodeType.BREAK;
093 case TerminalIO.TABULATOR:
094 return CodeType.TAB;
095 case TerminalIO.DELETE:
096 case TerminalIO.BACKSPACE:
097 return CodeType.BACKSPACE;
098 case TerminalIO.UP:
099 return CodeType.UP;
100 case TerminalIO.DOWN:
101 return CodeType.DOWN;
102 case TerminalIO.RIGHT:
103 return CodeType.RIGHT;
104 case TerminalIO.LEFT:
105 return CodeType.LEFT;
106 case TerminalIO.HANDLED:
107 return CodeType.CLOSE;
108 default:
109 return CodeType.CHAR;
110 }
111 }
112
113 public void close() {
114 conn.close();
115 }
116
117 public void flush() throws IOException {
118 termIO.flush();
119 }
120
121 public void write(CharSequence s) throws IOException {
122 termIO.write(s.toString());
123 }
124
125 public void write(Style style) throws IOException {
126 if (style == Style.reset) {
127 termIO.resetAttributes();
128 termIO.write("");
129 } else {
130 //
131 if (style instanceof Style.Composite) {
132 Style.Composite composite = (Style.Composite)style;
133 if (composite.getBold() != null) {
134 termIO.setBold(composite.getBold());
135 }
136 if (composite.getUnderline() != null) {
137 termIO.setUnderlined(composite.getUnderline());
138 }
139 if (composite.getBlink() != null) {
140 termIO.setBlink(composite.getBlink());
141 }
142 Color fg = composite.getForeground();
143 if (fg != null) {
144 if (fg == Color.def) {
145 termIO.setForegroundColor(BasicTerminalIO.COLORINIT);
146 } else {
147 termIO.setForegroundColor(30 + fg.code);
148 }
149 }
150 Color bg = composite.getBackground();
151 if (bg != null) {
152 if (bg == Color.def) {
153 termIO.setBackgroundColor(BasicTerminalIO.COLORINIT);
154 } else {
155 termIO.setBackgroundColor(30 + bg.code);
156 }
157 }
158 } else {
159 termIO.resetAttributes();
160 }
161 }
162 }
163
164 public void write(char c) throws IOException {
165 termIO.write(c);
166 }
167
168 public void writeDel() throws IOException {
169 termIO.moveLeft(1);
170 termIO.write(' ');
171 termIO.moveLeft(1);
172 termIO.flush();
173 }
174
175 public void writeCRLF() throws IOException {
176 termIO.write("\r\n");
177 }
178
179 public boolean moveRight(char c) throws IOException {
180 termIO.moveRight(1);
181 return true;
182 }
183
184 public boolean moveLeft() throws IOException {
185 termIO.moveLeft(1);
186 return true;
187 }
188
189 public void cls() throws IOException {
190 termIO.eraseScreen();
191 termIO.setCursor(0, 0);
192 }
193 }