use of org.apache.accumulo.shell.commands.QuotedStringTokenizer in project accumulo by apache.
the class Shell method execCommand.
public void execCommand(String input, boolean ignoreAuthTimeout, boolean echoPrompt) throws IOException {
audit.log(Level.INFO, getDefaultPrompt() + input);
if (echoPrompt) {
reader.print(getDefaultPrompt());
reader.println(input);
}
if (input.startsWith(COMMENT_PREFIX)) {
return;
}
String[] fields;
try {
fields = new QuotedStringTokenizer(input).getTokens();
} catch (BadArgumentException e) {
printException(e);
++exitCode;
return;
}
if (fields.length == 0)
return;
String command = fields[0];
fields = fields.length > 1 ? Arrays.copyOfRange(fields, 1, fields.length) : new String[] {};
Command sc = null;
if (command.length() > 0) {
try {
// Obtain the command from the command table
sc = commandFactory.get(command);
if (sc == null) {
reader.println(String.format("Unknown command \"%s\". Enter \"help\" for a list possible commands.", command));
reader.flush();
return;
}
long duration = System.nanoTime() - lastUserActivity;
if (!(sc instanceof ExitCommand) && !ignoreAuthTimeout && (duration < 0 || duration > authTimeout)) {
reader.println("Shell has been idle for too long. Please re-authenticate.");
boolean authFailed = true;
do {
String pwd = readMaskedLine("Enter current password for '" + connector.whoami() + "': ", '*');
if (pwd == null) {
reader.println();
return;
}
try {
authFailed = !connector.securityOperations().authenticateUser(connector.whoami(), new PasswordToken(pwd));
} catch (Exception e) {
++exitCode;
printException(e);
}
if (authFailed)
reader.print("Invalid password. ");
} while (authFailed);
lastUserActivity = System.nanoTime();
}
// Get the options from the command on how to parse the string
Options parseOpts = sc.getOptionsWithHelp();
// Parse the string using the given options
CommandLine cl = new BasicParser().parse(parseOpts, fields);
int actualArgLen = cl.getArgs().length;
int expectedArgLen = sc.numArgs();
if (cl.hasOption(helpOption)) {
// Display help if asked to; otherwise execute the command
sc.printHelp(this);
} else if (expectedArgLen != NO_FIXED_ARG_LENGTH_CHECK && actualArgLen != expectedArgLen) {
++exitCode;
// Check for valid number of fixed arguments (if not
// negative; negative means it is not checked, for
// vararg-like commands)
printException(new IllegalArgumentException(String.format("Expected %d argument%s. There %s %d.", expectedArgLen, expectedArgLen == 1 ? "" : "s", actualArgLen == 1 ? "was" : "were", actualArgLen)));
sc.printHelp(this);
} else {
int tmpCode = sc.execute(input, cl, this);
exitCode += tmpCode;
reader.flush();
}
} catch (ConstraintViolationException e) {
++exitCode;
printConstraintViolationException(e);
} catch (TableNotFoundException e) {
++exitCode;
if (getTableName().equals(e.getTableName()))
setTableName("");
printException(e);
} catch (ParseException e) {
// option when the user is asking for help
if (!(e instanceof MissingOptionException && (Arrays.asList(fields).contains("-" + helpOption) || Arrays.asList(fields).contains("--" + helpLongOption)))) {
++exitCode;
printException(e);
}
if (sc != null)
sc.printHelp(this);
} catch (UserInterruptException e) {
++exitCode;
} catch (Exception e) {
++exitCode;
printException(e);
}
} else {
++exitCode;
printException(new BadArgumentException("Unrecognized empty command", command, -1));
}
reader.flush();
}
use of org.apache.accumulo.shell.commands.QuotedStringTokenizer in project accumulo by apache.
the class ShellCompletor method _complete.
private int _complete(String fullBuffer, int cursor, List<String> candidates) {
boolean inTableFlag = false, inUserFlag = false, inNamespaceFlag = false;
// Only want to grab the buffer up to the cursor because
// the user could be trying to tab complete in the middle
// of the line
String buffer = fullBuffer.substring(0, cursor);
Token current_command_token = root;
String current_string_token = null;
boolean end_space = buffer.endsWith(" ");
// tabbing with no text
if (buffer.length() == 0) {
candidates.addAll(root.getSubcommandNames());
return 0;
}
String prefix = "";
QuotedStringTokenizer qst = new QuotedStringTokenizer(buffer);
Iterator<String> iter = qst.iterator();
while (iter.hasNext()) {
current_string_token = iter.next();
current_string_token = current_string_token.replaceAll("([\\s'\"])", "\\\\$1");
if (!iter.hasNext()) {
// and not complete the current command.
if (end_space && !current_string_token.endsWith(" ") || buffer.endsWith("\"")) {
// option flags if we're there
if (current_string_token.trim().equals("-" + Shell.tableOption)) {
candidates.addAll(options.get(Shell.Command.CompletionSet.TABLENAMES));
prefix += "-" + Shell.tableOption + " ";
} else if (current_string_token.trim().equals("-" + Shell.userOption)) {
candidates.addAll(options.get(Shell.Command.CompletionSet.USERNAMES));
prefix += "-" + Shell.userOption + " ";
} else if (current_string_token.trim().equals("-" + Shell.namespaceOption)) {
candidates.addAll(options.get(Shell.Command.CompletionSet.NAMESPACES));
prefix += "-" + Shell.namespaceOption + " ";
} else if (current_command_token != null) {
Token next = current_command_token.getSubcommand(current_string_token);
if (next != null) {
current_command_token = next;
if (current_command_token.getCaseSensitive())
prefix += current_string_token + " ";
else
prefix += current_string_token.toUpperCase() + " ";
candidates.addAll(current_command_token.getSubcommandNames());
}
}
Collections.sort(candidates);
return (prefix.length());
}
// if we're in -t <table>, -u <user>, or -tn <namespace> complete those
if (inTableFlag) {
for (String a : options.get(Shell.Command.CompletionSet.TABLENAMES)) if (a.startsWith(current_string_token))
candidates.add(a);
} else if (inUserFlag) {
for (String a : options.get(Shell.Command.CompletionSet.USERNAMES)) if (a.startsWith(current_string_token))
candidates.add(a);
} else if (inNamespaceFlag) {
for (String a : options.get(Shell.Command.CompletionSet.NAMESPACES)) if (a.startsWith(current_string_token))
candidates.add(a);
} else if (current_command_token != null)
candidates.addAll(current_command_token.getSubcommandNames(current_string_token));
Collections.sort(candidates);
return (prefix.length());
}
if (current_string_token.trim().equals("-" + Shell.tableOption))
inTableFlag = true;
else if (current_string_token.trim().equals("-" + Shell.userOption))
inUserFlag = true;
else if (current_string_token.trim().equals("-" + Shell.namespaceOption))
inNamespaceFlag = true;
else
inUserFlag = inTableFlag = inNamespaceFlag = false;
if (current_command_token != null && current_command_token.getCaseSensitive())
prefix += current_string_token + " ";
else
prefix += current_string_token.toUpperCase() + " ";
if (current_command_token != null && current_command_token.getSubcommandNames().contains(current_string_token))
current_command_token = current_command_token.getSubcommand(current_string_token);
}
return 0;
}
Aggregations