]> git.armaanb.net Git - gen-shell.git/commitdiff
made usable!
authorArmaan Bhojwani <3fb650a9-b47e-4604-a282-1dd91953b2ee@anonaddy.me>
Thu, 29 Oct 2020 22:07:08 +0000 (18:07 -0400)
committerArmaan Bhojwani <3fb650a9-b47e-4604-a282-1dd91953b2ee@anonaddy.me>
Thu, 29 Oct 2020 22:07:08 +0000 (18:07 -0400)
README.md
src/main.cpp
src/main.cpp.bak [new file with mode: 0644]
src/prompt.cpp

index 72a4662cfbbc6c001606f5f2d9f9738468f14955..d48c1564e07e2b190b12cafda40ace1795c060df 100644 (file)
--- a/README.md
+++ b/README.md
@@ -1,6 +1,10 @@
 # gen-shell
 # gen-shell
-![Jenkins](https://img.shields.io/jenkins/build?jobUrl=https%3A%2F%2Fbuild.bhojwani.org%2Fjob%2Fgen-shell%2F) 
-A work in progress generic shell. This is a very reduced fork of [taskshell](https://github.com/GothenburgBitFactory/taskshell)
+![Jenkins](https://img.shields.io/jenkins/build?jobUrl=https%3A%2F%2Fbuild.bhojwani.org%2Fjob%2Fgen-shell%2F)  
+=== WORK IN PROGRSS ===
+A simple way to turn any command into a shell with arrow key/history suppoert. This is a fork of [taskshell](https://github.com/GothenburgBitFactory/taskshell) with a highly reduce codebase.
+
+## Usage
+Just put the command that you want to repeat as the argument
 
 ## Installation
 Binaries can be downloaded [from here](https://build.bhojwani.org/job/gen-shell/lastSuccessfulBuild/artifact/build/src/gen-shell). Note that these binaries are compiled against libreadline7, so if your system only has libreadline8, you should symlink 8 to 7 with `sudo ln -s /usr/lib/x86_64-linux-gnu/libreadline.so.8.0 /usr/lib/x86_64-linux-gnu/libreadline.so.7`. This isn't a great solution, but it gets the job done.
 
 ## Installation
 Binaries can be downloaded [from here](https://build.bhojwani.org/job/gen-shell/lastSuccessfulBuild/artifact/build/src/gen-shell). Note that these binaries are compiled against libreadline7, so if your system only has libreadline8, you should symlink 8 to 7 with `sudo ln -s /usr/lib/x86_64-linux-gnu/libreadline.so.8.0 /usr/lib/x86_64-linux-gnu/libreadline.so.7`. This isn't a great solution, but it gets the job done.
@@ -10,10 +14,11 @@ If you would like to avoid this, you can build from source. Doing so requires:
   - g++
   - libreadline development files
 
   - g++
   - libreadline development files
 
-```
+```bash
 git clone https://codeberg.org/armaan/gen-shell
 cd gen-shell
 git clone https://codeberg.org/armaan/gen-shell
 cd gen-shell
-cmake --build .
+cmake .
+make
 sudo cp src/gen-shell /usr/bin/
 ```
 Or if you don't have root access, you can subsitute the last line with
 sudo cp src/gen-shell /usr/bin/
 ```
 Or if you don't have root access, you can subsitute the last line with
index fa4bad3cc07418bc11af7ac30c746acdab166af3..5849c28dbd17cc8cf132fb8606cc412420e288da 100644 (file)
 #include <readline/history.h>
 #endif
 
 #include <readline/history.h>
 #endif
 
-std::string promptCompose ();
+using namespace std;
+std::string promptCompose();
 
 
-const std::string getResponse (const std::string& prompt)
-{
-  std::string response {""};
+const std::string getResponse(const std::string & prompt) {
+  std::string response {
+    ""
+  };
 
   // Display prompt, get input.
 
   // Display prompt, get input.
-#ifdef HAVE_READLINE
-  char *line_read = readline (prompt.c_str ());
-  if (! line_read)
-  {
+  #ifdef HAVE_READLINE
+  char * line_read = readline(prompt.c_str());
+  if (!line_read) {
     std::cout << "\n";
     response = "<EOF>";
     std::cout << "\n";
     response = "<EOF>";
-  }
-  else
-  {
+  } else {
     // Save history.
     // Save history.
-    if (*line_read)
-      add_history (line_read);
+    if ( * line_read)
+      add_history(line_read);
 
 
-    response = std::string (line_read);
-    free (line_read);
+    response = std::string(line_read);
+    free(line_read);
   }
   }
-#else
+  #else
   std::cout << prompt;
   std::cout << prompt;
-  std::getline (std::cin, response);
-  if (std::cin.eof () == 1)
-  {
+  std::getline(std::cin, response);
+  if (std::cin.eof() == 1) {
     std::cout << "\n";
     response = "<EOF>";
   }
     std::cout << "\n";
     response = "<EOF>";
   }
-#endif
+  #endif
 
   return response;
 }
 
 
   return response;
 }
 
-////////////////////////////////////////////////////////////////////////////////
-static int commandLoop (bool autoClear)
-{
-  // Compose the prompt.
-  auto prompt = promptCompose ();
-
-  // Display prompt, get input.
-  auto command = getResponse (prompt);
-
-  if (autoClear)
-    std::cout << "\033[2J\033[0;0H";
-
-  int status = 0;
-  if (! isatty (fileno (stdin)) && command == "")
-  {
-    status = -1;
-  }
-  else if (command != "")
-  {
-    // Dispatch command.
-    if (command == "<EOF>")                      status = -1;
-    else if (command != "")
-    {
-      command = command;
-      std::cout << "[" << command << "]\n";
-      system (command.c_str ());
-    }
-  }
-
-  return status;
-}
-
-////////////////////////////////////////////////////////////////////////////////
-int main (int argc, const char** argv)
+int main(int argc, char** argv)
 {
   int status = 0;
 
 {
   int status = 0;
 
-  // Lightweight version checking that doesn't require initialization or any I/O.
-  if (argc == 2 && !strcmp (argv[1], "--version"))
-  {
-    std::cout << VERSION << "\n";
+  string root_cmd;
+  for (int i = 1; i < argc; ++i) {
+    root_cmd +=argv[i];
   }
   }
-  else
-  {
-    try
-    {
-      bool autoClear = false;
-      std::string input;
-      std::string output;
-      autoClear = (output == "true\n" ||
-                   output == "1\n"    ||
-                   output == "y\n"    ||
-                   output == "yes\n"  ||
-                   output == "on\n");
 
 
-      while ((status = commandLoop (autoClear)) == 0)
-        ;
-    }
+  while (status == 0) {
+    // Compose the prompt.
+    auto prompt = promptCompose();
 
 
-    catch (const std::string& error)
-    {
-      std::cerr << error << "\n";
-      status = -1;
-    }
+    // Display prompt, get input.
+    auto command = getResponse(prompt);
 
 
-    catch (...)
+    int status = 0;
+    if (command != "")
     {
     {
-      std::cerr << "Unknown error." << "\n";
-      status = -2;
+      // Dispatch command.
+      if (command == "<EOF>")                      status = 1;
+      else if (command != "")
+      {
+        if (argc == 0) {
+          string whole_command=command;
+          std::cout << "[" << command << "]\n";
+          system (command.c_str ());
+          }
+        else {
+          string whole_command = root_cmd + " " + command;
+          std::cout << "[" << whole_command << "]\n";
+          system (whole_command.c_str ());
+        }
+      }
     }
     }
+    if (status == 1)
+      return 0;
   }
   }
-
-  // Returning -1 drops out of the command loop, but gets translated to 0 here,
-  // so that there is a clean way to exit.
-  return status == -1 ? 0 : status;
 }
 
 ////////////////////////////////////////////////////////////////////////////////
 }
 
 ////////////////////////////////////////////////////////////////////////////////
diff --git a/src/main.cpp.bak b/src/main.cpp.bak
new file mode 100644 (file)
index 0000000..e39b131
--- /dev/null
@@ -0,0 +1,117 @@
+////////////////////////////////////////////////////////////////////////////////
+//
+// Copyright 2006 - 2017, Paul Beckingham, Federico Hernandez, 2020 Armaan Bhojwani
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+// SOFTWARE.
+//
+// http://www.opensource.org/licenses/mit-license.php
+//
+////////////////////////////////////////////////////////////////////////////////
+
+#include <cmake.h>
+#include <iostream>
+#include <vector>
+#include <string>
+#include <cstring>
+#include <cstdio>
+#include <stdlib.h>
+#include <unistd.h>
+
+#ifdef HAVE_READLINE
+#include <readline/readline.h>
+#include <readline/history.h>
+#endif
+
+using namespace std;
+std::string promptCompose ();
+
+const std::string getResponse (const std::string& prompt)
+{
+  std::string response {""};
+
+  // Display prompt, get input.
+#ifdef HAVE_READLINE
+  char *line_read = readline (prompt.c_str ());
+  if (! line_read)
+  {
+    std::cout << "\n";
+    response = "<EOF>";
+  }
+  else
+  {
+    // Save history.
+    if (*line_read)
+      add_history (line_read);
+
+    response = std::string (line_read);
+    free (line_read);
+  }
+#else
+  std::cout << prompt;
+  std::getline (std::cin, response);
+  if (std::cin.eof () == 1)
+  {
+    std::cout << "\n";
+    response = "<EOF>";
+  }
+#endif
+
+  return response;
+}
+
+int commandLoop ()
+{
+  // Compose the prompt.
+  auto prompt = promptCompose ();
+
+  // Display prompt, get input.
+  auto command = getResponse (prompt);
+
+  int status = 0;
+  if (! isatty (fileno (stdin)) && command == "")
+  {
+    status = -1;
+  }
+  else if (command != "")
+  {
+    // Dispatch command.
+    if (command == "<EOF>")                      status = -1;
+    else if (command != "")
+    {
+      command = command;
+      std::cout << "[" << command << "]\n";
+      system (command.c_str ());
+    }
+  }
+
+  return status;
+}
+
+int main ()
+// int main (int argc, const char** argv)
+{
+  int status = 0;
+
+  while (status == 0)
+    commandLoop();
+
+  return status == -1 ? 0 : status;
+}
+
+////////////////////////////////////////////////////////////////////////////////
index 5b594983ade912ae5068b4ab2ddfd83386305559..93654d4653855c4c58bbd6fe749f77023983abfb 100644 (file)
@@ -86,7 +86,7 @@ std::string composeContexts (bool pretty /* = false */)
 ////////////////////////////////////////////////////////////////////////////////
 std::string promptCompose ()
 {
 ////////////////////////////////////////////////////////////////////////////////
 std::string promptCompose ()
 {
-  return ">>>";
+  return "";
 }
 
 ////////////////////////////////////////////////////////////////////////////////
 }
 
 ////////////////////////////////////////////////////////////////////////////////