use of org.jline.terminal.Terminal in project felix by apache.
the class Activator method startShell.
private Runnable startShell(BundleContext context, CommandProcessor processor) throws Exception {
Dictionary<String, Object> dict = new Hashtable<>();
dict.put(CommandProcessor.COMMAND_SCOPE, "gogo");
// register converters
regs.add(context.registerService(Converter.class.getName(), new Converters(context.getBundle(0).getBundleContext()), null));
// register commands
dict.put(CommandProcessor.COMMAND_FUNCTION, Builtin.functions);
regs.add(context.registerService(Builtin.class.getName(), new Builtin(), dict));
dict.put(CommandProcessor.COMMAND_FUNCTION, Procedural.functions);
regs.add(context.registerService(Procedural.class.getName(), new Procedural(), dict));
dict.put(CommandProcessor.COMMAND_FUNCTION, Posix.functions);
regs.add(context.registerService(Posix.class.getName(), new Posix(processor), dict));
Shell shell = new Shell(new ShellContext(), processor);
dict.put(CommandProcessor.COMMAND_FUNCTION, Shell.functions);
regs.add(context.registerService(Shell.class.getName(), shell, dict));
Terminal terminal = TerminalBuilder.builder().name("gogo").system(true).nativeSignals(true).signalHandler(Terminal.SignalHandler.SIG_IGN).build();
CommandSession session = processor.createSession(terminal.input(), terminal.output(), terminal.output());
AtomicBoolean closing = new AtomicBoolean();
Thread thread = new Thread(() -> {
String errorMessage = "gogo: unable to create console";
try {
session.put(Shell.VAR_TERMINAL, terminal);
try {
List<String> args = new ArrayList<>();
args.add("--login");
String argstr = shell.getContext().getProperty("gosh.args");
if (argstr != null) {
Tokenizer tokenizer = new Tokenizer(argstr);
Token token;
while ((token = tokenizer.next()) != null) {
args.add(token.toString());
}
}
shell.gosh(session, args.toArray(new String[args.size()]));
} catch (Throwable e) {
Object loc = session.get(".location");
if (null == loc || !loc.toString().contains(":")) {
loc = "gogo";
}
errorMessage = loc.toString();
throw e;
}
} catch (Throwable e) {
if (!closing.get()) {
System.err.println(errorMessage + e.getClass().getSimpleName() + ": " + e.getMessage());
e.printStackTrace();
}
}
}, "Gogo shell");
// start shell on a separate thread...
thread.start();
return () -> {
closing.set(true);
shell.stop();
try {
terminal.close();
} catch (IOException e) {
// Ignore
}
try {
long t0 = System.currentTimeMillis();
while (thread.isAlive()) {
thread.interrupt();
thread.join(10);
if (System.currentTimeMillis() - t0 > 5000) {
System.err.println("!!! FAILED TO STOP EXECUTOR !!!");
break;
}
}
} catch (InterruptedException e) {
// Restore administration...
Thread.currentThread().interrupt();
}
};
}
use of org.jline.terminal.Terminal in project felix by apache.
the class Builtin method __files.
public List<Candidate> __files(CommandSession session) {
ParsedLine line = Shell.getParsedLine(session);
LineReader reader = Shell.getReader(session);
List<Candidate> candidates = new ArrayList<>();
new FilesCompleter(session.currentDir()) {
@Override
protected String getDisplay(Terminal terminal, Path p) {
return getFileDisplay(session, p);
}
}.complete(reader, line, candidates);
return candidates;
}
use of org.jline.terminal.Terminal in project felix by apache.
the class Builtin method __directories.
public List<Candidate> __directories(CommandSession session) {
ParsedLine line = Shell.getParsedLine(session);
LineReader reader = Shell.getReader(session);
List<Candidate> candidates = new ArrayList<>();
new DirectoriesCompleter(session.currentDir()) {
@Override
protected String getDisplay(Terminal terminal, Path p) {
return getFileDisplay(session, p);
}
}.complete(reader, line, candidates);
return candidates;
}
use of org.jline.terminal.Terminal in project felix by apache.
the class Posix method less.
protected void less(CommandSession session, Process process, String[] argv) throws Exception {
String[] usage = { "less - file pager", "Usage: less [OPTIONS] [FILES]", " -? --help Show help", " -e --quit-at-eof Exit on second EOF", " -E --QUIT-AT-EOF Exit on EOF", " -F --quit-if-one-screen Exit if entire file fits on first screen", " -q --quiet --silent Silent mode", " -Q --QUIET --SILENT Completely silent", " -S --chop-long-lines Do not fold long lines", " -i --ignore-case Search ignores lowercase case", " -I --IGNORE-CASE Search ignores all case", " -x --tabs Set tab stops", " -N --LINE-NUMBERS Display line number for each line", " --no-init Disable terminal initialization", " --no-keypad Disable keypad handling" };
boolean hasExtendedOptions = false;
try {
Less.class.getField("quitIfOneScreen");
hasExtendedOptions = true;
} catch (NoSuchFieldException e) {
List<String> ustrs = new ArrayList<>(Arrays.asList(usage));
ustrs.removeIf(s -> s.contains("--quit-if-one-screen") || s.contains("--no-init") || s.contains("--no-keypad"));
usage = ustrs.toArray(new String[ustrs.size()]);
}
Options opt = parseOptions(session, usage, argv);
List<Source> sources = new ArrayList<>();
if (opt.args().isEmpty()) {
opt.args().add("-");
}
for (String arg : opt.args()) {
if ("-".equals(arg)) {
sources.add(new StdInSource(process));
} else {
sources.add(new PathSource(session.currentDir().resolve(arg), arg));
}
}
if (!process.isTty(1)) {
for (Source source : sources) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(source.read()))) {
cat(process, reader, opt.isSet("LINE-NUMBERS"));
}
}
return;
}
Less less = new Less(Shell.getTerminal(session));
less.quitAtFirstEof = opt.isSet("QUIT-AT-EOF");
less.quitAtSecondEof = opt.isSet("quit-at-eof");
less.quiet = opt.isSet("quiet");
less.veryQuiet = opt.isSet("QUIET");
less.chopLongLines = opt.isSet("chop-long-lines");
less.ignoreCaseAlways = opt.isSet("IGNORE-CASE");
less.ignoreCaseCond = opt.isSet("ignore-case");
if (opt.isSet("tabs")) {
less.tabs = opt.getNumber("tabs");
}
less.printLineNumbers = opt.isSet("LINE-NUMBERS");
if (hasExtendedOptions) {
Less.class.getField("quitIfOneScreen").set(less, opt.isSet("quit-if-one-screen"));
Less.class.getField("noInit").set(less, opt.isSet("no-init"));
Less.class.getField("noKeypad").set(less, opt.isSet("no-keypad"));
}
less.run(sources);
}
use of org.jline.terminal.Terminal in project felix by apache.
the class Telnet method start.
private void start(CommandSession session) throws IOException {
ConnectionManager connectionManager = new ConnectionManager(1000, 5 * 60 * 1000, 5 * 60 * 1000, 60 * 1000, null, null, false) {
@Override
protected Connection createConnection(ThreadGroup threadGroup, ConnectionData newCD) {
return new Connection(threadGroup, newCD) {
TelnetIO telnetIO;
@Override
protected void doRun() throws Exception {
telnetIO = new TelnetIO();
telnetIO.setConnection(this);
telnetIO.initIO();
InputStream in = new InputStream() {
@Override
public int read() throws IOException {
return telnetIO.read();
}
@Override
public int read(byte[] b, int off, int len) throws IOException {
int r = read();
if (r >= 0) {
b[off] = (byte) r;
return 1;
} else {
return -1;
}
}
};
PrintStream out = new PrintStream(new OutputStream() {
@Override
public void write(int b) throws IOException {
telnetIO.write(b);
}
@Override
public void flush() throws IOException {
telnetIO.flush();
}
});
Terminal terminal = TerminalBuilder.builder().type(getConnectionData().getNegotiatedTerminalType().toLowerCase()).streams(in, out).system(false).name("telnet").build();
terminal.setSize(new Size(getConnectionData().getTerminalColumns(), getConnectionData().getTerminalRows()));
terminal.setAttributes(Shell.getTerminal(session).getAttributes());
addConnectionListener(new ConnectionListener() {
@Override
public void connectionIdle(ConnectionEvent ce) {
}
@Override
public void connectionTimedOut(ConnectionEvent ce) {
}
@Override
public void connectionLogoutRequest(ConnectionEvent ce) {
}
@Override
public void connectionSentBreak(ConnectionEvent ce) {
}
@Override
public void connectionTerminalGeometryChanged(ConnectionEvent ce) {
terminal.setSize(new Size(getConnectionData().getTerminalColumns(), getConnectionData().getTerminalRows()));
terminal.raise(Signal.WINCH);
}
});
PrintStream pout = new PrintStream(terminal.output());
CommandSession session = processor.createSession(terminal.input(), pout, pout);
session.put(Shell.VAR_TERMINAL, terminal);
Context context = new Context() {
@Override
public String getProperty(String name) {
return System.getProperty(name);
}
@Override
public void exit() throws Exception {
close();
}
};
new Shell(context, processor).gosh(session, new String[] { "--login" });
}
@Override
protected void doClose() throws Exception {
telnetIO.closeOutput();
telnetIO.closeInput();
}
};
}
};
portListener = new PortListener("gogo", port, 10);
portListener.setConnectionManager(connectionManager);
portListener.start();
}
Aggregations