1 55 package org.lateralnz.panther.util; 56 57 import java.io.FilterOutputStream ; 58 import java.io.IOException ; 59 import java.io.OutputStream ; 60 import java.net.Socket ; 61 import java.util.ArrayList ; 62 import java.util.Iterator ; 63 64 import org.lateralnz.common.telnet.BaseHandler; 65 import org.lateralnz.common.telnet.Constants; 66 import org.lateralnz.common.telnet.Server; 67 import org.lateralnz.common.util.InteractiveInterpreter; 68 69 import org.python.core.PyDictionary; 70 import org.python.core.PySystemState; 71 72 public class ConsoleInterface extends BaseHandler { 73 74 private ConsoleConnector parent; 75 private ArrayList func; 76 private ArrayList history = new ArrayList (); 77 int historyIdx = -1; 78 private InteractiveInterpreter interp; 79 private StringBuffer current = new StringBuffer (); 80 81 public ConsoleInterface(Server parent, Socket sock) throws IOException { 82 super(parent, sock); 83 84 this.parent = (ConsoleConnector)parent; 85 86 PySystemState.initialize(); 87 PySystemState ss = new PySystemState(); 88 this.interp = new InteractiveInterpreter(new PyDictionary(), ss); 89 this.interp.setOut(new LinefeedFilterStream(out)); 90 this.interp.setErr(new LinefeedFilterStream(out)); 91 92 init(); 93 } 94 95 private void init() { 96 func = (ArrayList )this.parent.functions.clone(); 97 Iterator iter = func.iterator(); 98 while (iter.hasNext()) { 99 String s = (String )iter.next(); 100 101 interp.exec(s); 102 } 103 } 104 105 public boolean handleControlChar(int c) throws IOException { 106 if (super.handleControlChar(c)) { 107 return true; 108 } 109 else if (c == 18) { 110 this.parent.init(); 111 init(); 112 return true; 113 } 114 else { 115 return false; 116 } 117 } 118 119 public boolean handleEscape(int esc, int mod) throws IOException { 120 if (super.handleEscape(esc, mod)) { 121 return true; 122 } 123 if (esc == Constants.ESC_UP) { 124 while (deleteChar()); 125 if (historyIdx > 0) { 126 historyIdx--; 127 } 128 if (historyIdx < history.size() && historyIdx >= 0) { 129 line.append((String )history.get(historyIdx)); 130 out.write(line.toString().getBytes()); 131 } 132 } 133 else if (esc == Constants.ESC_DOWN) { 134 while (deleteChar()); 135 if (historyIdx < history.size()-1) { 136 historyIdx++; 137 } 138 if (historyIdx < history.size() && historyIdx >= 0) { 139 line.append((String )history.get(historyIdx)); 140 out.write(line.toString().getBytes()); 141 } 142 } 143 return true; 144 } 145 146 public boolean handleInput(String line) throws IOException { 147 interp.resetbuffer(); 148 current.append(line); 149 history.add(line); 150 historyIdx = history.size()-1; 151 if (interp.runsource(current.toString())) { 152 current.append("\n"); 153 } 154 else { 155 current.delete(0, current.length()); 156 } 157 return true; 158 } 159 160 class LinefeedFilterStream extends FilterOutputStream { 163 164 public LinefeedFilterStream(OutputStream out) { 165 super(out); 166 } 167 168 public void write(int i) throws IOException { 169 super.write(i); 170 if (i == 10) { 171 super.write(13); 172 } 173 } 174 } 175 } | Popular Tags |