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     */
019    package org.crsh.lang.script;
020    
021    import org.crsh.cli.impl.Delimiter;
022    import org.crsh.cli.impl.completion.CompletionMatch;
023    import org.crsh.cli.spi.Completion;
024    import org.crsh.command.BaseRuntimeContext;
025    import org.crsh.command.CommandInvoker;
026    import org.crsh.command.NoSuchCommandException;
027    import org.crsh.command.ShellCommand;
028    import org.crsh.plugin.ResourceKind;
029    import org.crsh.shell.ShellResponse;
030    import org.crsh.repl.EvalResponse;
031    import org.crsh.repl.REPL;
032    import org.crsh.repl.REPLSession;
033    import org.crsh.text.Chunk;
034    import org.crsh.util.Utils;
035    
036    import java.util.logging.Level;
037    import java.util.logging.Logger;
038    
039    /** @author Julien Viet */
040    public class ScriptREPL implements REPL {
041    
042      /** . */
043      static final Logger log = Logger.getLogger(ScriptREPL.class.getName());
044    
045      public String getName() {
046        return "script";
047      }
048    
049      public EvalResponse eval(REPLSession session, String request) {
050        PipeLineParser parser = new PipeLineParser(request);
051        final PipeLineFactory factory = parser.parse();
052        if (factory != null) {
053          try {
054            CommandInvoker<Void, Chunk> invoker = factory.create(session);
055            return new EvalResponse.Invoke(invoker);
056          }
057          catch (NoSuchCommandException e) {
058            return new EvalResponse.Response(ShellResponse.unknownCommand(e.getCommandName()));
059          }
060        } else {
061          return new EvalResponse.Response(ShellResponse.noCommand());
062        }
063      }
064    
065      public CompletionMatch complete(REPLSession session, String prefix) {
066        PipeLineFactory ast = new PipeLineParser(prefix).parse();
067        String termPrefix;
068        if (ast != null) {
069          PipeLineFactory last = ast.getLast();
070          termPrefix = Utils.trimLeft(last.getLine());
071        } else {
072          termPrefix = "";
073        }
074    
075        //
076        log.log(Level.FINE, "Retained term prefix is " + prefix);
077        CompletionMatch completion;
078        int pos = termPrefix.indexOf(' ');
079        if (pos == -1) {
080          Completion.Builder builder = Completion.builder(prefix);
081          for (String resourceId : session.getContext().listResourceId(ResourceKind.COMMAND)) {
082            if (resourceId.startsWith(termPrefix)) {
083              builder.add(resourceId.substring(termPrefix.length()), true);
084            }
085          }
086          completion = new CompletionMatch(Delimiter.EMPTY, builder.build());
087        } else {
088          String commandName = termPrefix.substring(0, pos);
089          termPrefix = termPrefix.substring(pos);
090          try {
091            ShellCommand command = session.getCommand(commandName);
092            if (command != null) {
093              completion = command.complete(new BaseRuntimeContext(session, session.getContext().getAttributes()), termPrefix);
094            } else {
095              completion = new CompletionMatch(Delimiter.EMPTY, Completion.create());
096            }
097          }
098          catch (NoSuchCommandException e) {
099            log.log(Level.FINE, "Could not create command for completion of " + prefix, e);
100            completion = new CompletionMatch(Delimiter.EMPTY, Completion.create());
101          }
102        }
103    
104        //
105        return completion;
106      }
107    }