1 // gen-shell - the generic shell
2 // Copyright (c) 2020, Armaan Bhojwani <code@armaanb.net>
6 #include <../Sarge/src/sarge.h>
12 #include <readline/readline.h>
13 #include <readline/history.h>
16 ////////////////////////////////////////////////////////////////////////////////
19 using std::filesystem::exists;
21 const std::string getResponse(const std::string & prompt) {
22 std::string response {
26 // Display prompt, get input
28 char * line_read = readline(prompt.c_str());
35 add_history(line_read);
37 response = std::string(line_read);
42 std::getline(std::cin, response);
43 if (std::cin.eof() == 1) {
52 ////////////////////////////////////////////////////////////////////////////////
54 int main(int argc, char** argv)
57 // Command line arguments
60 sarge.setArgument("a", "after", "Command to execute before leaving the shell", true);
61 sarge.setArgument("b", "before", "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", "Dont automatically add spaces after custom prompt and command", true);
66 sarge.setDescription("Make a shell from any executable");
67 sarge.setUsage("gen-shell <options>");
69 if (!sarge.parseArguments(argc, argv)) {
70 std::cerr << "Couldn't parse arguments..." << std::endl;
74 if (sarge.exists("help")) {
80 if (sarge.exists("no-space")) {
84 // Define input command
86 sarge.getFlag("command", arg_cmd);
92 sarge.getFlag("prompt", prompt);
100 // Execute before-command
101 string before_command;
102 sarge.getFlag("before", before_command);
103 system (before_command.c_str ());
105 // Execute after-command
106 string after_command;
107 sarge.getFlag("after", after_command);
110 vector<string> alias_files = {"$XDG_CONFIG_HOME/gen-shell/aliases",
111 "$HOME/.config/gen-shell/aliases"};
112 for(const auto &file : alias_files)
123 auto command = getResponse(prompt);
125 if (command == "<EOF>" || command == "exit" || command == "quit" )
127 system (after_command.c_str ());
130 string whole_command = arg_cmd + command;
131 system (whole_command.c_str ());
136 ////////////////////////////////////////////////////////////////////////////////