]> git.armaanb.net Git - gen-shell.git/blob - src/main.cpp
a570cdd792821bfee3f38891b4a2f919c4d41f19
[gen-shell.git] / src / main.cpp
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright 2006 - 2017, Paul Beckingham, Federico Hernandez, 2020 Armaan Bhojwani
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included
13 // in all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 // SOFTWARE.
22 //
23 // http://www.opensource.org/licenses/mit-license.php
24 //
25 ////////////////////////////////////////////////////////////////////////////////
26
27 #include <cmake.h>
28 #include <iostream>
29 #include <vector>
30 #include <string>
31 #include <../Sarge/src/sarge.h>
32 #include <stdlib.h>
33
34 #ifdef HAVE_READLINE
35 #include <readline/readline.h>
36 #include <readline/history.h>
37 #endif
38
39 ////////////////////////////////////////////////////////////////////////////////
40
41 using namespace std;
42
43 static std::vector <std::string> contexts;
44 std::string composeContexts (bool pretty = false);
45
46 int promptClear ()
47 {
48   contexts.clear ();
49   return 0;
50 }
51
52 int promptRemove ()
53 {
54   if (contexts.size ())
55     contexts.pop_back ();
56
57   return 0;
58 }
59
60 int promptAdd (const std::string& context)
61 {
62   contexts.push_back (context);
63   return 0;
64 }
65
66 ////////////////////////////////////////////////////////////////////////////////
67
68 const std::string getResponse(const std::string & prompt) {
69   std::string response {
70     ""
71   };
72
73   // Display prompt, get input
74   #ifdef HAVE_READLINE
75   char * line_read = readline(prompt.c_str());
76   if (!line_read) {
77     std::cout << "\n";
78     response = "<EOF>";
79   } else {
80     // Save history
81     if ( * line_read)
82       add_history(line_read);
83
84     response = std::string(line_read);
85     free(line_read);
86   }
87   #else
88   std::cout << prompt;
89   std::getline(std::cin, response);
90   if (std::cin.eof() == 1) {
91     std::cout << "\n";
92     response = "<EOF>";
93   }
94   #endif
95
96   return response;
97 }
98
99 ////////////////////////////////////////////////////////////////////////////////
100
101 int main(int argc, char** argv)
102 {
103
104   // Command line arguments
105   Sarge sarge;
106
107         sarge.setArgument("a", "after", "Command to execute before leaving the shell", true);
108         sarge.setArgument("b", "before", "Command to execute before entering the shell", true);
109         sarge.setArgument("c", "command", "Command to convert to shell", true);
110         sarge.setArgument("h", "help", "Get help.", false);
111         sarge.setArgument("p", "prompt", "Define a custom prompt", true);
112         sarge.setDescription("Make a shell from any executable");
113         sarge.setUsage("gen-shell <options>");
114
115         if (!sarge.parseArguments(argc, argv)) {
116                 std::cerr << "Couldn't parse arguments..." << std::endl;
117                 return 1;
118         }
119
120         if (sarge.exists("help")) {
121                 sarge.printHelp();
122     return 0;
123         }
124
125   // define input command
126   string arg_cmd;
127   sarge.getFlag("command", arg_cmd);
128   arg_cmd += " ";
129
130   // define prompt
131   string prompt = "";
132   sarge.getFlag("prompt", prompt);
133
134   if ( prompt == "" )
135     prompt = "% ";
136   else
137     prompt += " ";
138
139   // execute before-command
140   string before_command;
141   sarge.getFlag("before", before_command);
142   system (before_command.c_str ());
143
144   // execute after-command
145   string after_command;
146   sarge.getFlag("after", after_command);
147
148   // Main program
149   while (true) {
150     // Display prompt, get input
151     auto command = getResponse(prompt);
152
153     if (command != "")
154     {
155       // Dispatch command
156       if (command == "<EOF>")
157       {
158         system (after_command.c_str ());
159         return 0;
160       }
161       else if (command != "")
162       {
163         string whole_command = arg_cmd + command;
164         system (whole_command.c_str ());
165       }
166     }
167   }
168 }
169
170 ////////////////////////////////////////////////////////////////////////////////