1 // gen-shell - the generic shell
2 // Copyright (c) 2020, Armaan Bhojwani <code@armaanb.net>
6 #include <../Sarge/src/sarge.h>
11 #include <readline/readline.h>
12 #include <readline/history.h>
15 ////////////////////////////////////////////////////////////////////////////////
19 const std::string getResponse(const std::string & prompt) {
20 std::string response {
24 // Display prompt, get input
26 char * line_read = readline(prompt.c_str());
33 add_history(line_read);
35 response = std::string(line_read);
40 std::getline(std::cin, response);
41 if (std::cin.eof() == 1) {
50 ////////////////////////////////////////////////////////////////////////////////
52 int main(int argc, char** argv)
55 // Command line arguments
58 sarge.setArgument("a", "after", "Command to execute before leaving the shell", true);
59 sarge.setArgument("b", "before", "Command to execute before entering the shell", true);
60 sarge.setArgument("c", "command", "Command to convert to shell", true);
61 sarge.setArgument("h", "help", "Get help.", false);
62 sarge.setArgument("p", "prompt", "Define a custom prompt", true);
63 sarge.setArgument("", "no-space", "Dont automatically add spaces after custom prompt and command", true);
64 sarge.setDescription("Make a shell from any executable");
65 sarge.setUsage("gen-shell <options>");
67 if (!sarge.parseArguments(argc, argv)) {
68 std::cerr << "Couldn't parse arguments..." << std::endl;
72 if (sarge.exists("help")) {
78 if (sarge.exists("no-space")) {
82 // Define input command
84 sarge.getFlag("command", arg_cmd);
90 sarge.getFlag("prompt", prompt);
98 // Execute before-command
99 string before_command;
100 sarge.getFlag("before", before_command);
101 system (before_command.c_str ());
103 // Execute after-command
104 string after_command;
105 sarge.getFlag("after", after_command);
109 auto command = getResponse(prompt);
111 if (command == "<EOF>" || command == "exit" || command == "quit" )
113 system (after_command.c_str ());
116 string whole_command = arg_cmd + command;
117 system (whole_command.c_str ());
122 ////////////////////////////////////////////////////////////////////////////////