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",
59 "Command to execute before leaving the shell", true);
60 sarge.setArgument("b", "before",
61 "Command to execute before entering the shell", true);
62 sarge.setArgument("c", "command", "Command to convert to shell", true);
63 sarge.setArgument("h", "help", "Get help.", false);
64 sarge.setArgument("p", "prompt", "Define a custom prompt", true);
65 sarge.setArgument("", "no-space",
66 "Dont automatically add spaces after custom prompt and command", true);
67 sarge.setDescription("Make a shell from any executable");
68 sarge.setUsage("gen-shell <options>");
70 if (!sarge.parseArguments(argc, argv)) {
71 std::cerr << "Couldn't parse arguments..." << std::endl;
75 if (sarge.exists("help")) {
81 if (sarge.exists("no-space")) {
85 // Define input command
87 sarge.getFlag("command", arg_cmd);
93 sarge.getFlag("prompt", prompt);
101 // Execute before-command
102 string before_command;
103 sarge.getFlag("before", before_command);
104 system (before_command.c_str ());
106 // Execute after-command
107 string after_command;
108 sarge.getFlag("after", after_command);
112 auto command = getResponse(prompt);
114 if (command == "<EOF>" || command == "exit" || command == "quit" )
116 system (after_command.c_str ());
119 string whole_command = arg_cmd + command;
120 system (whole_command.c_str ());
125 ////////////////////////////////////////////////////////////////////////////////