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 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 (CommandCreationException e) { 058 log.log(Level.FINER, "Could not create command", e); 059 return new EvalResponse.Response(ShellResponse.unknownCommand(e.getCommandName())); 060 } 061 } else { 062 return new EvalResponse.Response(ShellResponse.noCommand()); 063 } 064 } 065 066 public CompletionMatch complete(REPLSession session, String prefix) { 067 PipeLineFactory ast = new PipeLineParser(prefix).parse(); 068 String termPrefix; 069 if (ast != null) { 070 PipeLineFactory last = ast.getLast(); 071 termPrefix = Utils.trimLeft(last.getLine()); 072 } else { 073 termPrefix = ""; 074 } 075 076 // 077 log.log(Level.FINE, "Retained term prefix is " + prefix); 078 CompletionMatch completion; 079 int pos = termPrefix.indexOf(' '); 080 if (pos == -1) { 081 Completion.Builder builder = Completion.builder(prefix); 082 for (String name : session.getCommandNames()) { 083 if (name.startsWith(termPrefix)) { 084 builder.add(name.substring(termPrefix.length()), true); 085 } 086 } 087 completion = new CompletionMatch(Delimiter.EMPTY, builder.build()); 088 } else { 089 String commandName = termPrefix.substring(0, pos); 090 termPrefix = termPrefix.substring(pos); 091 try { 092 ShellCommand command = session.getCommand(commandName); 093 if (command != null) { 094 completion = command.complete(new BaseRuntimeContext(session, session.getContext().getAttributes()), termPrefix); 095 } else { 096 completion = new CompletionMatch(Delimiter.EMPTY, Completion.create()); 097 } 098 } 099 catch (CommandCreationException e) { 100 log.log(Level.FINE, "Could not create command for completion of " + prefix, e); 101 completion = new CompletionMatch(Delimiter.EMPTY, Completion.create()); 102 } 103 } 104 105 // 106 return completion; 107 } 108}