001 /* 002 * Copyright (C) 2010 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 /** 038 * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a> 039 * @version $Revision$ 040 */ 041 public class TelnetIO implements TermIO { 042 043 /** . */ 044 private final Connection conn; 045 046 /** . */ 047 private final BasicTerminalIO termIO; 048 049 public TelnetIO(Connection conn) { 050 this.conn = conn; 051 this.termIO = conn.getTerminalIO(); 052 } 053 054 public int read() throws IOException { 055 try { 056 return termIO.read(); 057 } 058 catch (EOFException e) { 059 return TerminalIO.HANDLED; 060 } 061 catch (SocketException e) { 062 return TerminalIO.HANDLED; 063 } 064 } 065 066 public int getWidth() { 067 return termIO.getColumns(); 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 3: 088 return CodeType.BREAK; 089 case TerminalIO.TABULATOR: 090 return CodeType.TAB; 091 case TerminalIO.DELETE: 092 case TerminalIO.BACKSPACE: 093 return CodeType.BACKSPACE; 094 case TerminalIO.UP: 095 return CodeType.UP; 096 case TerminalIO.DOWN: 097 return CodeType.DOWN; 098 case TerminalIO.RIGHT: 099 return CodeType.RIGHT; 100 case TerminalIO.LEFT: 101 return CodeType.LEFT; 102 case TerminalIO.HANDLED: 103 return CodeType.CLOSE; 104 default: 105 return CodeType.CHAR; 106 } 107 } 108 109 public void close() { 110 conn.close(); 111 } 112 113 public void flush() throws IOException { 114 termIO.flush(); 115 } 116 117 public void write(String s) throws IOException { 118 termIO.write(s); 119 } 120 121 public void write(Style style) throws IOException { 122 if (style == Style.reset) { 123 termIO.resetAttributes(); 124 termIO.write(""); 125 } else { 126 // 127 Decoration decoration = style.getDecoration(); 128 termIO.setBlink(Decoration.blink == decoration); 129 termIO.setBold(Decoration.bold == decoration); 130 termIO.setUnderlined(Decoration.underline == decoration); 131 132 // 133 Color fg = style.getForeground(); 134 if (fg != null) { 135 termIO.setForegroundColor(fg.code(30)); 136 } else { 137 termIO.setForegroundColor(BasicTerminalIO.COLORINIT); 138 } 139 140 // 141 Color bg = style.getBackground(); 142 if (bg != null) { 143 termIO.setBackgroundColor(bg.code(30)); 144 } 145 else { 146 termIO.setBackgroundColor(BasicTerminalIO.COLORINIT); 147 } 148 } 149 } 150 151 public void write(char c) throws IOException { 152 termIO.write(c); 153 } 154 155 public void writeDel() throws IOException { 156 termIO.moveLeft(1); 157 termIO.write(' '); 158 termIO.moveLeft(1); 159 termIO.flush(); 160 } 161 162 public void writeCRLF() throws IOException { 163 termIO.write("\r\n"); 164 } 165 166 public boolean moveRight(char c) throws IOException { 167 termIO.moveRight(1); 168 return true; 169 } 170 171 public boolean moveLeft() throws IOException { 172 termIO.moveLeft(1); 173 return true; 174 } 175 }