]> git.armaanb.net Git - gen-shell.git/blobdiff - src/main.cpp
added more CLI options!
[gen-shell.git] / src / main.cpp
index fa4bad3cc07418bc11af7ac30c746acdab166af3..a570cdd792821bfee3f38891b4a2f919c4d41f19 100644 (file)
 #include <iostream>
 #include <vector>
 #include <string>
-#include <cstring>
-#include <cstdio>
+#include <../Sarge/src/sarge.h>
 #include <stdlib.h>
-#include <unistd.h>
 
 #ifdef HAVE_READLINE
 #include <readline/readline.h>
 #include <readline/history.h>
 #endif
 
-std::string promptCompose ();
+////////////////////////////////////////////////////////////////////////////////
+
+using namespace std;
+
+static std::vector <std::string> contexts;
+std::string composeContexts (bool pretty = false);
 
-const std::string getResponse (const std::string& prompt)
+int promptClear ()
 {
-  std::string response {""};
+  contexts.clear ();
+  return 0;
+}
 
-  // 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);
+int promptRemove ()
+{
+  if (contexts.size ())
+    contexts.pop_back ();
 
-    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 0;
+}
 
-  return response;
+int promptAdd (const std::string& context)
+{
+  contexts.push_back (context);
+  return 0;
 }
 
 ////////////////////////////////////////////////////////////////////////////////
-static int commandLoop (bool autoClear)
-{
-  // Compose the prompt.
-  auto prompt = promptCompose ();
 
-  // Display prompt, get input.
-  auto command = getResponse (prompt);
+const std::string getResponse(const std::string & prompt) {
+  std::string response {
+    ""
+  };
 
-  if (autoClear)
-    std::cout << "\033[2J\033[0;0H";
+  // 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);
 
-  int status = 0;
-  if (! isatty (fileno (stdin)) && command == "")
-  {
-    status = -1;
+    response = std::string(line_read);
+    free(line_read);
   }
-  else if (command != "")
-  {
-    // Dispatch command.
-    if (command == "<EOF>")                      status = -1;
-    else if (command != "")
-    {
-      command = command;
-      std::cout << "[" << command << "]\n";
-      system (command.c_str ());
-    }
+  #else
+  std::cout << prompt;
+  std::getline(std::cin, response);
+  if (std::cin.eof() == 1) {
+    std::cout << "\n";
+    response = "<EOF>";
   }
+  #endif
 
-  return status;
+  return response;
 }
 
 ////////////////////////////////////////////////////////////////////////////////
-int main (int argc, const char** argv)
+
+int main(int argc, char** argv)
 {
-  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";
-  }
+  // Command line arguments
+  Sarge sarge;
+
+       sarge.setArgument("a", "after", "Command to execute before leaving the shell", true);
+       sarge.setArgument("b", "before", "Command to execute before entering the shell", true);
+       sarge.setArgument("c", "command", "Command to convert to shell", true);
+       sarge.setArgument("h", "help", "Get help.", false);
+       sarge.setArgument("p", "prompt", "Define a custom prompt", true);
+       sarge.setDescription("Make a shell from any executable");
+       sarge.setUsage("gen-shell <options>");
+
+       if (!sarge.parseArguments(argc, argv)) {
+               std::cerr << "Couldn't parse arguments..." << std::endl;
+               return 1;
+       }
+
+       if (sarge.exists("help")) {
+               sarge.printHelp();
+    return 0;
+       }
+
+  // define input command
+  string arg_cmd;
+  sarge.getFlag("command", arg_cmd);
+  arg_cmd += " ";
+
+  // define prompt
+  string prompt = "";
+  sarge.getFlag("prompt", prompt);
+
+  if ( prompt == "" )
+    prompt = "% ";
   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)
-        ;
-    }
+    prompt += " ";
 
-    catch (const std::string& error)
-    {
-      std::cerr << error << "\n";
-      status = -1;
-    }
+  // execute before-command
+  string before_command;
+  sarge.getFlag("before", before_command);
+  system (before_command.c_str ());
+
+  // execute after-command
+  string after_command;
+  sarge.getFlag("after", after_command);
 
-    catch (...)
+  // Main program
+  while (true) {
+    // Display prompt, get input
+    auto command = getResponse(prompt);
+
+    if (command != "")
     {
-      std::cerr << "Unknown error." << "\n";
-      status = -2;
+      // Dispatch command
+      if (command == "<EOF>")
+      {
+        system (after_command.c_str ());
+        return 0;
+      }
+      else if (command != "")
+      {
+        string whole_command = arg_cmd + command;
+        system (whole_command.c_str ());
+      }
     }
   }
-
-  // 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;
 }
 
 ////////////////////////////////////////////////////////////////////////////////