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 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(CharSequence s) throws IOException {
118        termIO.write(s.toString());
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          if (style instanceof Style.Composite) {
128            Style.Composite composite = (Style.Composite)style;
129            if (composite.getBold() != null) {
130              termIO.setBold(composite.getBold());
131            }
132            if (composite.getUnderline() != null) {
133              termIO.setUnderlined(composite.getUnderline());
134            }
135            if (composite.getBlink() != null) {
136              termIO.setBlink(composite.getBlink());
137            }
138            Color fg = composite.getForeground();
139            if (fg != null) {
140              if (fg == Color.def) {
141                termIO.setForegroundColor(BasicTerminalIO.COLORINIT);
142              } else {
143                termIO.setForegroundColor(30 + fg.code);
144              }
145            }
146            Color bg = composite.getBackground();
147            if (bg != null) {
148              if (bg == Color.def) {
149                termIO.setBackgroundColor(BasicTerminalIO.COLORINIT);
150              } else {
151                termIO.setBackgroundColor(30 + bg.code);
152              }
153            }
154          } else {
155            termIO.resetAttributes();
156          }
157        }
158      }
159    
160      public void write(char c) throws IOException {
161        termIO.write(c);
162      }
163    
164      public void writeDel() throws IOException {
165        termIO.moveLeft(1);
166        termIO.write(' ');
167        termIO.moveLeft(1);
168        termIO.flush();
169      }
170    
171      public void writeCRLF() throws IOException {
172        termIO.write("\r\n");
173      }
174    
175      public boolean moveRight(char c) throws IOException {
176        termIO.moveRight(1);
177        return true;
178      }
179    
180      public boolean moveLeft() throws IOException {
181        termIO.moveLeft(1);
182        return true;
183      }
184    
185      public void cls() throws IOException {
186        termIO.eraseScreen();
187        termIO.setCursor(0, 0);
188      }
189    }