FELIX-1524: tac command

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@807535 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/karaf/gshell/gshell-commands/src/main/java/org/apache/felix/karaf/gshell/commands/TacAction.java b/karaf/gshell/gshell-commands/src/main/java/org/apache/felix/karaf/gshell/commands/TacAction.java
new file mode 100644
index 0000000..77432d6
--- /dev/null
+++ b/karaf/gshell/gshell-commands/src/main/java/org/apache/felix/karaf/gshell/commands/TacAction.java
@@ -0,0 +1,73 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.felix.karaf.gshell.commands;
+
+import java.io.StringWriter;
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+import java.io.File;
+import java.io.OutputStream;
+import java.io.BufferedWriter;
+import java.io.BufferedOutputStream;
+import java.io.FileOutputStream;
+import java.io.OutputStreamWriter;
+import java.io.Writer;
+
+import org.apache.felix.karaf.gshell.console.OsgiCommandSupport;
+import org.apache.felix.gogo.commands.Command;
+import org.apache.felix.gogo.commands.Option;
+
+/**
+ * Grab the text from the standard input and return it as a string.
+ * Also write this text to a file if specified
+ *
+ * @version $Rev: 593392 $ $Date: 2007-11-09 03:14:15 +0100 (Fri, 09 Nov 2007) $
+ */
+@Command(scope = "shell", name = "tac", description = "Capture the stdin and return it as a string. Optionally write the content to a file")
+public class TacAction extends OsgiCommandSupport {
+
+    @Option(name = "-f", description = "Specify a file to write the content to")
+    private File file;
+
+    protected Object doExecute() throws Exception {
+        StringWriter sw = new StringWriter();
+        Writer[] writers;
+        if (file != null) {
+            writers = new Writer[] { sw, new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file))) };
+        } else {
+            writers = new Writer[] { sw };
+        }
+        BufferedReader rdr = new BufferedReader(new InputStreamReader(System.in));
+        String s = rdr.readLine();
+        boolean first = true;
+        while (s != null)
+        {
+            for (Writer w : writers) {
+                if (!first) {
+                    w.write("\n");
+                }
+                w.write(s);
+            }
+            first = false;
+            s = rdr.readLine();
+        }
+        for (Writer w : writers) {
+            w.close();
+        }
+        return sw.toString();
+    }
+}
diff --git a/karaf/gshell/gshell-commands/src/main/resources/OSGI-INF/blueprint/gshell-commands.xml b/karaf/gshell/gshell-commands/src/main/resources/OSGI-INF/blueprint/gshell-commands.xml
index f7a83f9..a8f01e4 100644
--- a/karaf/gshell/gshell-commands/src/main/resources/OSGI-INF/blueprint/gshell-commands.xml
+++ b/karaf/gshell/gshell-commands/src/main/resources/OSGI-INF/blueprint/gshell-commands.xml
@@ -62,6 +62,9 @@
             <action class="org.apache.felix.karaf.gshell.commands.SourceAction"/>
         </command>
         -->
+        <command name="shell/tac">
+            <action class="org.apache.felix.karaf.gshell.commands.TacAction"/>
+        </command>
     </command-bundle>
 
 </blueprint>