]> git.armaanb.net Git - gen-shell.git/blob - src/main.cpp
3d0e2c65d6d3c9e5fed8eadc22a129743b3f9328
[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 std::string promptCompose();
43
44 static std::vector <std::string> contexts;
45 std::string composeContexts (bool pretty = false);
46
47 int promptClear ()
48 {
49   contexts.clear ();
50   return 0;
51 }
52
53 int promptRemove ()
54 {
55   if (contexts.size ())
56     contexts.pop_back ();
57
58   return 0;
59 }
60
61 int promptAdd (const std::string& context)
62 {
63   contexts.push_back (context);
64   return 0;
65 }
66
67 std::string promptCompose ()
68 {
69   return "% ";
70 }
71
72 ////////////////////////////////////////////////////////////////////////////////
73
74 const std::string getResponse(const std::string & prompt) {
75   std::string response {
76     ""
77   };
78
79   // Display prompt, get input
80   #ifdef HAVE_READLINE
81   char * line_read = readline(prompt.c_str());
82   if (!line_read) {
83     std::cout << "\n";
84     response = "<EOF>";
85   } else {
86     // Save history
87     if ( * line_read)
88       add_history(line_read);
89
90     response = std::string(line_read);
91     free(line_read);
92   }
93   #else
94   std::cout << prompt;
95   std::getline(std::cin, response);
96   if (std::cin.eof() == 1) {
97     std::cout << "\n";
98     response = "<EOF>";
99   }
100   #endif
101
102   return response;
103 }
104
105 ////////////////////////////////////////////////////////////////////////////////
106
107 int main(int argc, char** argv)
108 {
109
110   // Command line arguments
111   Sarge sarge;
112
113         sarge.setArgument("h", "help", "Get help.", false);
114         sarge.setArgument("c", "cmd", "Command to execute before entering the shell", true);
115         sarge.setDescription("Make a shell from any command");
116         sarge.setUsage("gen-shell <options> <command>");
117
118         if (!sarge.parseArguments(argc, argv)) {
119                 std::cerr << "Couldn't parse arguments..." << std::endl;
120                 return 1;
121         }
122
123         if (sarge.exists("help")) {
124                 sarge.printHelp();
125     return 0;
126         }
127
128   string arg_cmd;
129   sarge.getFlag("cmd", arg_cmd);
130   arg_cmd += " ";
131
132   // Main program
133   while (true) {
134     // Compose the prompt
135     auto prompt = promptCompose();
136
137     // Display prompt, get input
138     auto command = getResponse(prompt);
139
140     if (command != "")
141     {
142       // Dispatch command
143       if (command == "<EOF>")
144       {
145         return 0;
146       }
147       else if (command != "")
148       {
149         string whole_command = arg_cmd + command;
150         system (whole_command.c_str ());
151       }
152     }
153   }
154 }
155
156 ////////////////////////////////////////////////////////////////////////////////