Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 1 | /* |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 2 | * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | * contributor license agreements. See the NOTICE file distributed with |
| 4 | * this work for additional information regarding copyright ownership. |
| 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | * (the "License"); you may not use this file except in compliance with |
| 7 | * the License. You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 17 | package org.apache.felix.shell.remote; |
| 18 | |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 19 | import java.io.IOException; |
| 20 | import java.io.InputStream; |
| 21 | import java.io.PrintStream; |
| 22 | import java.io.Reader; |
| 23 | |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 24 | /** |
| 25 | * Class implementing a terminal reader adapter |
| 26 | * originally designed for the BeanShell Interpreter. |
| 27 | * <p/> |
| 28 | * Provides simple line editing (Backspace, Strg-U and a history). |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 29 | */ |
| 30 | class TerminalReader extends Reader |
| 31 | { |
Richard S. Hall | fd9c883 | 2010-09-20 19:28:38 +0000 | [diff] [blame] | 32 | protected final InputStream m_in; |
| 33 | protected final PrintStream m_out; |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 34 | protected boolean m_echo = false; |
| 35 | protected boolean m_eof = false; |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 36 | |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 37 | public TerminalReader(InputStream in, PrintStream out) |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 38 | { |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 39 | m_in = in; |
| 40 | m_out = out; |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 41 | }//TerminalReader |
| 42 | |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 43 | /** |
| 44 | * Tests if this <tt>TerminalReader</tt> will echo |
| 45 | * the character input to the terminal. |
| 46 | * |
| 47 | * @return true if echo, false otherwise. |
| 48 | */ |
| 49 | public boolean isEcho() |
| 50 | { |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 51 | return m_echo; |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 52 | }//isEcho |
| 53 | |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 54 | /** |
| 55 | * Sets if this <tt>TerminalReader</tt> will echo |
| 56 | * the character input to the terminal. |
| 57 | * <p/> |
| 58 | * This only makes sense in character input mode, |
| 59 | * in line mode the terminal will handle editing, |
| 60 | * and this is recommended for using a more complex shell. |
| 61 | * If you implement your own character based editor, you |
| 62 | * might as well change this.<br/> |
| 63 | * (Default is false). |
| 64 | * |
| 65 | * @param echo true if echo, false otherwise. |
| 66 | */ |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 67 | public void setEcho(boolean echo) |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 68 | { |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 69 | m_echo = echo; |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 70 | }//setEcho |
| 71 | |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 72 | public int read(char[] chars, int off, int len) throws IOException |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 73 | { |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 74 | if (m_eof) |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 75 | { |
| 76 | return -1; |
| 77 | } |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 78 | for (int i = off; i < off + len; i++) |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 79 | { |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 80 | int ch = m_in.read(); |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 81 | //shortcut for EOT and simple EOF |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 82 | if (ch == EOT || (i == off && ch == -1)) |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 83 | { |
| 84 | return -1; |
| 85 | } |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 86 | chars[i] = (char) ch; |
| 87 | if (ch == -1 || ch == 10 || ch == 13) |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 88 | { |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 89 | m_eof = ch == -1; //store EOF |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 90 | int read = i - off + 1; |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 91 | if (m_echo) |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 92 | { |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 93 | m_out.write(CRLF); |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 94 | } |
| 95 | return read; |
| 96 | } |
| 97 | //naive backspace handling |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 98 | if (ch == BS || ch == DEL) |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 99 | { |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 100 | if (i > off) |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 101 | { |
| 102 | i = i - 2; |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 103 | moveLeft(1); |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 104 | eraseToEndOfLine(); |
| 105 | } |
| 106 | else |
| 107 | { |
| 108 | i--; |
| 109 | bell(); |
| 110 | } |
| 111 | } |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 112 | else if (ch == CTRL_U) |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 113 | { |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 114 | moveLeft(i - off); |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 115 | eraseToEndOfLine(); |
| 116 | i = off - 1; |
| 117 | } |
| 118 | else |
| 119 | { |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 120 | if (m_echo) |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 121 | { |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 122 | m_out.write(chars[i]); |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | } |
| 126 | return len; |
| 127 | }//read |
| 128 | |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 129 | /** |
| 130 | * Writes the NVT BEL character to the output. |
| 131 | */ |
| 132 | private void bell() |
| 133 | { |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 134 | m_out.write(BEL); |
| 135 | m_out.flush(); |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 136 | }//bell |
| 137 | |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 138 | /** |
| 139 | * Writes the standard vt100/ansi cursor moving code to the output. |
| 140 | * |
| 141 | * @param i the number of times the cursor should be moved left. |
| 142 | * @throws IOException if I/O fails. |
| 143 | */ |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 144 | private void moveLeft(int i) throws IOException |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 145 | { |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 146 | CURSOR_LEFT[2] = Byte.decode(Integer.toString(i)).byteValue(); |
| 147 | m_out.write(CURSOR_LEFT); |
| 148 | m_out.flush(); |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 149 | }//moveLeft |
| 150 | |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 151 | /** |
| 152 | * Writes the standard vt100/ansi sequence for erasing to the end of the current line. |
| 153 | * |
| 154 | * @throws IOException if I/O fails. |
| 155 | */ |
| 156 | private void eraseToEndOfLine() throws IOException |
| 157 | { |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 158 | m_out.write(ERASE_TEOL); |
| 159 | m_out.flush(); |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 160 | }//eraseToEndOfLine |
| 161 | |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 162 | /** |
| 163 | * Closes this reader. |
| 164 | * Note: will close the input, but not the output. |
| 165 | * |
| 166 | * @throws IOException |
| 167 | */ |
| 168 | public void close() throws IOException |
| 169 | { |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 170 | m_in.close(); |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 171 | }//close |
Richard S. Hall | fd9c883 | 2010-09-20 19:28:38 +0000 | [diff] [blame] | 172 | |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 173 | /** |
| 174 | * <b>Bell</b><br> |
| 175 | * The ANSI defined byte code for the NVT bell. |
| 176 | */ |
| 177 | public static final byte BEL = 7; |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 178 | /** |
| 179 | * <b>BackSpace</b><br> |
| 180 | * The ANSI defined byte code of backspace. |
| 181 | */ |
| 182 | public static final byte BS = 8; |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 183 | /** |
| 184 | * <b>Delete</b><br> |
| 185 | * The ANSI defined byte code of delete. |
| 186 | */ |
| 187 | public static final byte DEL = 127; |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 188 | /** |
| 189 | * CTRL-u |
| 190 | */ |
| 191 | public static final byte CTRL_U = 21; |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 192 | /** |
| 193 | * Escape character. |
| 194 | */ |
| 195 | private static byte ESC = 27; |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 196 | /** |
| 197 | * vt100/ansi standard sequence for moving the cursor left. |
| 198 | */ |
| 199 | private static byte[] CURSOR_LEFT = |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 200 | { |
| 201 | ESC, '[', '1', 'D' |
| 202 | }; |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 203 | /** |
| 204 | * vt100/ansi standard sequence for erasing everything from the actual cursor to |
| 205 | * the end of the current line. |
| 206 | */ |
| 207 | private static byte[] ERASE_TEOL = |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 208 | { |
| 209 | ESC, '[', 'K' |
| 210 | }; |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 211 | /** |
| 212 | * Standard NVT line break, which HAS TO BE CR and LF. |
| 213 | */ |
| 214 | private static byte[] CRLF = |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 215 | { |
| 216 | '\r', '\n' |
| 217 | }; |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 218 | /** |
| 219 | * Standard ASCII end of transmission. |
| 220 | */ |
| 221 | private static byte EOT = 4; |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 222 | }//class TerminalReader |