/** * Cmd action to ask user and get a response * * @param argArray arguments in interpreter buffer * @return what's left of arguments */ public ArgArray CmdCommentQuote(ArgArray argArray) { setDataDest(DataSink.fromSinkName("NULL:")); while (argArray.hasDashCommand()) { String dashCommand = argArray.parseDashCommand(); switch (dashCommand) { case "-to": setDataDestfromArgArray(argArray); break; default: unknownDashCommand(dashCommand); } } if (havingUnknownDashCommand()) { setCommandResult(COMMANDRESULT.FAILURE); } else if (argArray.size() < 1) { // no text following logArgArrayTooShortError(argArray); setCommandResult(COMMANDRESULT.FAILURE); } else { String quote = argArray.nextMaybeQuotation(); try { put(quote); } catch (SQLException | IOException | AS400SecurityException | ErrorCompletingRequestException | InterruptedException | ObjectDoesNotExistException | RequestNotSupportedException ex) { getLogger() .log( Level.SEVERE, "Could not put comment quote \"" + quote + "\" in " + getNameAndDescription(), ex); setCommandResult(COMMANDRESULT.FAILURE); } } return argArray; }
/** * Manage history file * * @param args the remainder of the command stream * @return the new remainder of the command stream */ public ArgArray history(ArgArray args) { String historyFileName = History.DEFAULT_HISTORY_FILENAME; int numLines = 0; int doLine = 0; int firstLine = -1; int lastLine = -1; String change_from = null; String change_to = null; while (args.hasDashCommand()) { String dashCommand = args.parseDashCommand(); switch (dashCommand) { case "-to": setDataDestfromArgArray(args); break; case "-off": setFunction(FUNCTIONS.OFF); break; case "-on": setFunction(FUNCTIONS.ON); break; case "-onfile": setFunction(FUNCTIONS.ONFILE); historyFileName = args.next(); break; case "-range": setFunction(FUNCTIONS.RANGE); firstLine = args.nextInt(); lastLine = args.nextInt(); break; case "-show": setFunction(FUNCTIONS.SHOW); break; case "-head": setFunction(FUNCTIONS.HEAD); numLines = args.nextInt(); break; case "-tail": setFunction(FUNCTIONS.TAIL); numLines = args.nextInt(); break; case "-name": setFunction(FUNCTIONS.NAME); break; case "-do": setFunction(FUNCTIONS.DO); doLine = args.nextInt() - 1; // from one's-based history // to 0-based index into history array break; case "-change": // not a function, just a suboption to DO change_from = args.nextMaybeQuotation(); change_to = args.nextMaybeQuotation(); break; default: unknownDashCommand(dashCommand); } } if (havingUnknownDashCommand()) { setCommandResult(COMMANDRESULT.FAILURE); } else { History h = getInterpreter().getHistory(); boolean withLineNumbers = getDataDest().getType() == DataSink.SINKTYPE.STD; switch (getFunction()) { case OFF: getInterpreter().closeHistory(); break; case ON: getInterpreter().instanceHistory(); break; case ONFILE: getInterpreter().setHistoryFileName(historyFileName); getInterpreter().instanceHistory(); break; case SHOW: if (h != null) { try { put(h.show(withLineNumbers)); } catch (IOException | RequestNotSupportedException | SQLException | AS400SecurityException | ErrorCompletingRequestException | InterruptedException | ObjectDoesNotExistException ex) { getLogger().log(Level.SEVERE, "Couldn't put history", ex); setCommandResult(COMMANDRESULT.FAILURE); } } else { getLogger().log(Level.SEVERE, "History is not enabled (try history -on)"); setCommandResult(COMMANDRESULT.FAILURE); } break; case HEAD: if (h != null) { try { put(h.head(numLines, withLineNumbers)); } catch (IOException | RequestNotSupportedException | SQLException | AS400SecurityException | ErrorCompletingRequestException | InterruptedException | ObjectDoesNotExistException ex) { getLogger().log(Level.SEVERE, "Couldn't put history", ex); setCommandResult(COMMANDRESULT.FAILURE); } } else { getLogger().log(Level.SEVERE, "History is not enabled (try history -on)"); setCommandResult(COMMANDRESULT.FAILURE); } break; case TAIL: if (h != null) { try { put(h.tail(numLines, withLineNumbers)); } catch (IOException | RequestNotSupportedException | SQLException | AS400SecurityException | ErrorCompletingRequestException | InterruptedException | ObjectDoesNotExistException ex) { getLogger().log(Level.SEVERE, "Couldn't put history", ex); setCommandResult(COMMANDRESULT.FAILURE); } } else { getLogger().log(Level.SEVERE, "History is not enabled (try history -on)"); setCommandResult(COMMANDRESULT.FAILURE); } break; case NAME: if (h != null) { try { put(h.getHistoryFileName()); } catch (IOException | RequestNotSupportedException | SQLException | AS400SecurityException | ErrorCompletingRequestException | InterruptedException | ObjectDoesNotExistException ex) { getLogger().log(Level.SEVERE, "Couldn't put history name", ex); setCommandResult(COMMANDRESULT.FAILURE); } } else { getLogger().log(Level.SEVERE, "History is not enabled (try history -on)"); setCommandResult(COMMANDRESULT.FAILURE); } break; case DO: if (h != null) { if (doLine >= 0) { try { String nthLine = h.nth(doLine); if (nthLine != null) { if (change_from != null && change_to != null) { nthLine = nthLine.replace(change_from, change_to); } System.out.println(nthLine); ArgArray newArgs = new Parser(getInterpreter(), nthLine).parseAnArgArray(); newArgs.addAll(args); args = newArgs; } else { getLogger() .log(Level.SEVERE, "Line {0} does not exist in history buffer", doLine + 1); setCommandResult(COMMANDRESULT.FAILURE); } } catch (IOException ex) { getLogger().log(Level.SEVERE, null, ex); setCommandResult(COMMANDRESULT.FAILURE); } } else { getLogger() .log(Level.SEVERE, "Line '{'0'}' does not exist in history buffer", doLine + 1); setCommandResult(COMMANDRESULT.FAILURE); } } else { getLogger().log(Level.SEVERE, "History is not enabled (try history -on)"); setCommandResult(COMMANDRESULT.FAILURE); } break; case RANGE: if (h != null) { try { put(h.range(firstLine - 1, lastLine - 1, withLineNumbers)); } catch (IOException | RequestNotSupportedException | SQLException | AS400SecurityException | ErrorCompletingRequestException | InterruptedException | ObjectDoesNotExistException ex) { getLogger().log(Level.SEVERE, "Couldn't put history range", ex); setCommandResult(COMMANDRESULT.FAILURE); } } else { getLogger().log(Level.SEVERE, "History is not enabled (try history -on)"); setCommandResult(COMMANDRESULT.FAILURE); } break; } } return args; }