Skip to content
Snippets Groups Projects
Commit 96669cb2 authored by Ludovic Apvrille's avatar Ludovic Apvrille
Browse files

Update on terminal

parent c3dfdef7
No related branches found
No related tags found
No related merge requests found
......@@ -145,8 +145,31 @@ public class Command implements CommandInterface {
level --;
}
return ret;
}
public Vector<Command> findCommands(String[] split, int index) {
if (split == null) {
return null;
}
if (index >= split.length) {
return null;
}
String s = split[index];
Vector<Command> couldBe = new Vector<>();
// Search of all compatible commands starting with s
for (Command c: subcommands) {
if (c.getShortCommand().startsWith(s) || c.getCommand().startsWith(s)) {
Vector<Command> others = c.findCommands(split, index+1);
if (others != null) {
couldBe.addAll(others);
}
}
}
return couldBe;
}
}
......@@ -60,6 +60,7 @@ import java.util.*;
* @author Ludovic APVRILLE
*/
public class Interpreter implements Runnable, TerminalProviderInterface {
public final static Command[] commands = {new Help(), new Quit(), new Action(),
new Set(), new Wait(), new Print(), new History(), new TestSpecific()};
......@@ -94,8 +95,6 @@ public class Interpreter implements Runnable, TerminalProviderInterface {
variables = new HashMap<>();
this.show = show;
formerCommands = new Vector<>();
term = new Terminal();
term.setTerminalProvider(this);
}
@Override
......@@ -317,8 +316,50 @@ public class Interpreter implements Runnable, TerminalProviderInterface {
return "> ";
}
public void tabAction(String buffer) {
public boolean tabAction(String buffer) {
// Print all possibilities from current buffer
String buf = Conversion.replaceAllString(buffer, " ", " ");
String[] split = buf.split(" ");
// From the split, determine commands already entered and completes it
Vector<Command> listOfCommands = findCommands(split, 0);
if (listOfCommands.size()== 0) {
return false;
}
for(Command c: listOfCommands) {
System.out.println(""+c.getCommand());
return true;
}
return true;
}
public Vector<Command> findCommands(String[] split, int index) {
if (split == null) {
return null;
}
if (index >= split.length) {
return null;
}
String s = split[index];
Vector<Command> couldBe = new Vector<>();
// Search of all compatible commands starting with s
for (Command c: commands) {
if (c.getShortCommand().startsWith(s) || c.getCommand().startsWith(s)) {
Vector<Command> others = c.findCommands(split, index+1);
if (others != null) {
couldBe.addAll(others);
}
}
}
return couldBe;
}
......
......@@ -201,6 +201,7 @@ private static int readUnix (boolean wait) throws IOException {
setTerminalAttrs(stdinFd, rawTermios); // switch off canonical mode, echo and signals
try {
if (!wait && System.in.available() == 0) {
System.out.println("Not ready");
return -2; } // no input available
return readSingleCharFromByteStream(System.in); }
finally {
......@@ -220,8 +221,10 @@ private static void setTerminalAttrs (int fd, Termios termios) throws IOExceptio
try {
int rc = libc.tcsetattr(fd, LibcDefs.TCSANOW, termios);
if (rc != 0) {
System.out.println("Exception in term tcset");
throw new RuntimeException("tcsetattr() failed."); }}
catch (LastErrorException e) {
System.out.println("Exception in term tcset");
throw new IOException("tcsetattr() failed.", e); }}
private static int readSingleCharFromByteStream (InputStream inputStream) throws IOException {
......@@ -257,13 +260,17 @@ private static synchronized void initUnix() throws IOException {
stdinIsConsole = libc.isatty(stdinFd) == 1;
charsetDecoder = Charset.defaultCharset().newDecoder();
if (stdinIsConsole) {
//System.out.println("IsConsole=" + stdinIsConsole);
originalTermios = getTerminalAttrs(stdinFd);
rawTermios = new Termios(originalTermios);
rawTermios.c_lflag &= ~(LibcDefs.ICANON | LibcDefs.ECHO | LibcDefs.ECHONL | LibcDefs.ISIG);
//rawTermios.c_lflag &= ~(LibcDefs.ICANON | LibcDefs.ECHO | LibcDefs.ECHONL);
intermediateTermios = new Termios(rawTermios);
intermediateTermios.c_lflag |= LibcDefs.ICANON;
//intermediateTermios.c_lflag |= LibcDefs.ICANON;
// Canonical mode can be switched off between the read() calls, but echo must remain disabled.
registerShutdownHook(); }
registerShutdownHook();
System.out.println("New console");
}
initDone = true; }
private static void resetConsoleModeUnix() throws IOException {
......
......@@ -60,17 +60,23 @@ public class Terminal {
private final static int ESC = 27;
private final static int TAB = 9;
private Vector<String> buffer;
private int maxbufferSize = MAX_BUFFER_SIZE;
private TerminalProviderInterface terminalProvider;
private int cpt;
private String sequence;
private String os;
public Terminal() {
buffer = new Vector<>();
cpt = 0;
os = System.getProperty("os.name").toLowerCase();
System.out.println("Detected OS:" + os);
os = os.split(" ")[0];
}
public void setTerminalProvider(TerminalProviderInterface tp) {
......@@ -79,7 +85,7 @@ public class Terminal {
public String getNextCommand() {
char x = 110;
char x;
int val = 0;
......@@ -123,6 +129,7 @@ public class Terminal {
}
// Usual CHAR
if ((sequence == null) && (val != -1)) {
if (val == CR) {
if (currentBuf.length() == 0) {
......@@ -135,8 +142,18 @@ public class Terminal {
}
}
//BACKSPACE
if ((val == BACKSPACE) || (val == DEL)) {
currentBuf = del(currentBuf);
//TAB
} else if (val == TAB) {
System.out.println("TAB");
if (terminalProvider != null) {
boolean b =terminalProvider.tabAction(currentBuf);
if (b) {
printPrompt(cpt);
}
}
} else if (val >= 32) {
//System.out.print("" + x + "(val=" + val + ");");
myPrint("" + x);
......@@ -164,7 +181,10 @@ public class Terminal {
}
public void myPrint(String s) {
System.out.print(s);
if (os.compareTo("mac") != 0) {
System.out.print(s);
}
//System.out.flush();
}
public void printHistory() {
......
......@@ -54,7 +54,7 @@ public interface TerminalProviderInterface {
public String getMidPrompt();
public void tabAction(String buffer);
public boolean tabAction(String buffer);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment