use of org.apache.karaf.shell.api.console.Command in project karaf by apache.
the class RegistryImpl method unregister.
@Override
public void unregister(Object service) {
synchronized (services) {
services.remove(service);
if (service instanceof Command) {
Command cmd = (Command) service;
String key = cmd.getScope() + ":" + cmd.getName();
List<Command> cmds = commands.get(key);
if (cmds != null) {
cmds.remove(cmd);
if (cmds.isEmpty()) {
commands.remove(key);
}
}
}
}
}
use of org.apache.karaf.shell.api.console.Command in project karaf by apache.
the class RegistryImpl method register.
@Override
public void register(Object service) {
synchronized (services) {
services.put(service, service);
if (service instanceof Command) {
Command cmd = (Command) service;
String key = cmd.getScope() + ":" + cmd.getName();
commands.computeIfAbsent(key, k -> new ArrayList<>()).add(cmd);
}
}
}
use of org.apache.karaf.shell.api.console.Command in project karaf by apache.
the class KarafParser method doParse.
private ParsedLine doParse(String line, int cursor, ParseContext parseContext) throws SyntaxError {
Program program = null;
List<Statement> statements = null;
String repaired = line;
while (program == null) {
try {
org.apache.felix.gogo.runtime.Parser parser = new org.apache.felix.gogo.runtime.Parser(repaired);
program = parser.program();
statements = parser.statements();
} catch (EOFError e) {
// Make sure we don't loop forever
if (parseContext == ParseContext.COMPLETE && repaired.length() < line.length() + 1024) {
repaired = repaired + " " + e.repair();
} else {
throw e;
}
}
}
// Find corresponding statement
Statement statement = null;
for (int i = statements.size() - 1; i >= 0; i--) {
Statement s = statements.get(i);
if (s.start() <= cursor) {
boolean isOk = true;
// check if there are only spaces after the previous statement
if (s.start() + s.length() < cursor) {
for (int j = s.start() + s.length(); isOk && j < cursor; j++) {
isOk = Character.isWhitespace(line.charAt(j));
}
}
statement = s;
break;
}
}
if (statement != null && statement.tokens() != null && !statement.tokens().isEmpty()) {
String cmdName = session.resolveCommand(statement.tokens().get(0).toString());
String[] parts = cmdName.split(":");
Command cmd = parts.length == 2 ? session.getRegistry().getCommand(parts[0], parts[1]) : null;
Parser cmdParser = cmd != null ? cmd.getParser() : null;
if (cmdParser != null) {
final CommandLine cmdLine = cmdParser.parse(session, statement.toString(), cursor - statement.start());
return new ParsedLine() {
@Override
public String word() {
return cmdLine.getCursorArgument();
}
@Override
public int wordCursor() {
return cmdLine.getArgumentPosition();
}
@Override
public int wordIndex() {
return cmdLine.getCursorArgumentIndex();
}
@Override
public List<String> words() {
return Arrays.asList(cmdLine.getArguments());
}
@Override
public String line() {
return cmdLine.getBuffer();
}
@Override
public int cursor() {
return cmdLine.getBufferPosition();
}
};
}
if (repaired != line) {
Token stmt = statement.subSequence(0, line.length() - statement.start());
List<Token> tokens = new ArrayList<>(statement.tokens());
Token last = tokens.get(tokens.size() - 1);
tokens.set(tokens.size() - 1, last.subSequence(0, line.length() - last.start()));
return new ParsedLineImpl(program, stmt, cursor, tokens);
}
return new ParsedLineImpl(program, statement, cursor, statement.tokens());
} else {
// TODO:
return new ParsedLineImpl(program, program, cursor, Collections.singletonList(program));
}
}
use of org.apache.karaf.shell.api.console.Command in project karaf by apache.
the class CommandLineParser method parse.
public static String parse(Session session, String command) {
StringBuilder parsed = new StringBuilder();
int pos = 0;
while (pos < command.length()) {
String rem = command.substring(pos);
GogoParser cmdNameParser = new GogoParser(rem, rem.length());
String name = cmdNameParser.value();
name = session.resolveCommand(name);
Parser cmdParser = null;
for (Command cmd : session.getRegistry().getCommands()) {
if (name.equals(cmd.getScope() + ":" + cmd.getName())) {
cmdParser = cmd.getParser();
break;
}
}
if (cmdParser == null) {
cmdParser = new DefaultParser();
}
CommandLine cmdLine = cmdParser.parse(session, rem, rem.length());
parsed.append(cmdParser.preprocess(session, cmdLine));
int length = cmdLine.getBuffer().length();
if (length < rem.length()) {
char ch = rem.charAt(length);
if (ch == ';' || ch == '|') {
parsed.append(" ");
parsed.append(ch);
parsed.append(" ");
length++;
} else if (ch == '\n') {
parsed.append(ch);
length++;
} else {
throw new IllegalArgumentException("Unrecognized character: '" + ch + "'");
}
}
pos += length;
}
return parsed.toString();
}
use of org.apache.karaf.shell.api.console.Command in project karaf by apache.
the class ConsoleSessionImpl method resolveCommand.
@Override
public String resolveCommand(String name) {
// TODO: optimize
if (!name.contains(":")) {
String[] scopes = ((String) get(Session.SCOPE)).split(":");
List<Command> commands = registry.getCommands();
for (String scope : scopes) {
boolean globalScope = Session.SCOPE_GLOBAL.equals(scope);
for (Command command : commands) {
if ((globalScope || command.getScope().equals(scope)) && command.getName().equals(name)) {
return command.getScope() + ":" + name;
}
}
}
}
return name;
}
Aggregations