001/*
002 * Copyright (C) 2012 eXo Platform SAS.
003 *
004 * This is free software; you can redistribute it and/or modify it
005 * under the terms of the GNU Lesser General Public License as
006 * published by the Free Software Foundation; either version 2.1 of
007 * the License, or (at your option) any later version.
008 *
009 * This software is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * You should have received a copy of the GNU Lesser General Public
015 * License along with this software; if not, write to the Free
016 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
017 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
018 */
019package org.crsh.lang.script;
020
021import org.crsh.cli.impl.Delimiter;
022import org.crsh.cli.impl.completion.CompletionMatch;
023import org.crsh.cli.spi.Completion;
024import org.crsh.command.BaseRuntimeContext;
025import org.crsh.command.CommandCreationException;
026import org.crsh.command.CommandInvoker;
027import org.crsh.command.ShellCommand;
028import org.crsh.plugin.ResourceKind;
029import org.crsh.shell.ShellResponse;
030import org.crsh.repl.EvalResponse;
031import org.crsh.repl.REPL;
032import org.crsh.repl.REPLSession;
033import org.crsh.text.Chunk;
034import org.crsh.util.Utils;
035
036import java.util.logging.Level;
037import java.util.logging.Logger;
038
039/** @author Julien Viet */
040public class ScriptREPL implements REPL {
041
042  /** . */
043  private static final ScriptREPL instance = new ScriptREPL();
044
045  /** . */
046  static final Logger log = Logger.getLogger(ScriptREPL.class.getName());
047
048  public static ScriptREPL getInstance() {
049    return instance;
050  }
051
052  private ScriptREPL() {
053  }
054
055  public String getName() {
056    return "script";
057  }
058
059  public EvalResponse eval(REPLSession session, String request) {
060    PipeLineParser parser = new PipeLineParser(request);
061    final PipeLineFactory factory = parser.parse();
062    if (factory != null) {
063      try {
064        CommandInvoker<Void, Chunk> invoker = factory.create(session);
065        return new EvalResponse.Invoke(invoker);
066      }
067      catch (CommandCreationException e) {
068        log.log(Level.FINER, "Could not create command", e);
069        return new EvalResponse.Response(ShellResponse.unknownCommand(e.getCommandName()));
070      }
071    } else {
072      return new EvalResponse.Response(ShellResponse.noCommand());
073    }
074  }
075
076  public CompletionMatch complete(REPLSession session, String prefix) {
077    PipeLineFactory ast = new PipeLineParser(prefix).parse();
078    String termPrefix;
079    if (ast != null) {
080      PipeLineFactory last = ast.getLast();
081      termPrefix = Utils.trimLeft(last.getLine());
082    } else {
083      termPrefix = "";
084    }
085
086    //
087    log.log(Level.FINE, "Retained term prefix is " + prefix);
088    CompletionMatch completion;
089    int pos = termPrefix.indexOf(' ');
090    if (pos == -1) {
091      Completion.Builder builder = Completion.builder(prefix);
092      for (String name : session.getCommandNames()) {
093        if (name.startsWith(termPrefix)) {
094          builder.add(name.substring(termPrefix.length()), true);
095        }
096      }
097      completion = new CompletionMatch(Delimiter.EMPTY, builder.build());
098    } else {
099      String commandName = termPrefix.substring(0, pos);
100      termPrefix = termPrefix.substring(pos);
101      try {
102        ShellCommand command = session.getCommand(commandName);
103        if (command != null) {
104          completion = command.complete(new BaseRuntimeContext(session, session.getContext().getAttributes()), termPrefix);
105        } else {
106          completion = new CompletionMatch(Delimiter.EMPTY, Completion.create());
107        }
108      }
109      catch (CommandCreationException e) {
110        log.log(Level.FINE, "Could not create command for completion of " + prefix, e);
111        completion = new CompletionMatch(Delimiter.EMPTY, Completion.create());
112      }
113    }
114
115    //
116    return completion;
117  }
118}