1 ////////////////////////////////////////////////////////////////////////////////
3 // gen-shell, the generic shell
5 // Copyright 2006 - 2017, Paul Beckingham, Federico Hernandez,
6 // 2020, Armaan Bhojwani
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
15 // The above copyright notice and this permission notice shall be included
16 // in all copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 // http://www.opensource.org/licenses/mit-license.php
28 ////////////////////////////////////////////////////////////////////////////////
33 #include <../Sarge/src/sarge.h>
37 #include <readline/readline.h>
38 #include <readline/history.h>
41 ////////////////////////////////////////////////////////////////////////////////
45 const std::string getResponse(const std::string & prompt) {
46 std::string response {
50 // Display prompt, get input
52 char * line_read = readline(prompt.c_str());
59 add_history(line_read);
61 response = std::string(line_read);
66 std::getline(std::cin, response);
67 if (std::cin.eof() == 1) {
76 ////////////////////////////////////////////////////////////////////////////////
78 int main(int argc, char** argv)
81 // Command line arguments
84 sarge.setArgument("a", "after", "Command to execute before leaving the shell", true);
85 sarge.setArgument("b", "before", "Command to execute before entering the shell", true);
86 sarge.setArgument("c", "command", "Command to convert to shell", true);
87 sarge.setArgument("h", "help", "Get help.", false);
88 sarge.setArgument("p", "prompt", "Define a custom prompt", true);
89 sarge.setArgument("", "no-space", "Dont automatically add spaces after custom prompt and command", true);
90 sarge.setDescription("Make a shell from any executable");
91 sarge.setUsage("gen-shell <options>");
93 if (!sarge.parseArguments(argc, argv)) {
94 std::cerr << "Couldn't parse arguments..." << std::endl;
98 if (sarge.exists("help")) {
104 if (sarge.exists("no-space")) {
108 // Define input command
110 sarge.getFlag("command", arg_cmd);
116 sarge.getFlag("prompt", prompt);
120 } else if ( space ) {
124 // Execute before-command
125 string before_command;
126 sarge.getFlag("before", before_command);
127 system (before_command.c_str ());
129 // Execute after-command
130 string after_command;
131 sarge.getFlag("after", after_command);
135 // Display prompt, get input
136 auto command = getResponse(prompt);
139 if (command == "<EOF>" || "exit" || "quit" || ":q" )
140 // if (command == "<EOF>" || command == "exit" )
142 system (after_command.c_str ());
145 string whole_command = arg_cmd + command;
146 system (whole_command.c_str ());
151 ////////////////////////////////////////////////////////////////////////////////