]> git.armaanb.net Git - gen-shell.git/blob - src/main.cpp
d9bc1f4d2f7564af50f764267909325eccfa866f
[gen-shell.git] / src / main.cpp
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // gen-shell, the generic shell
4 //
5 // Copyright 2006 - 2017, Paul Beckingham, Federico Hernandez,
6 // 2020, Armaan Bhojwani
7 //
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:
14 //
15 // The above copyright notice and this permission notice shall be included
16 // in all copies or substantial portions of the Software.
17 //
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
24 // SOFTWARE.
25 //
26 // http://www.opensource.org/licenses/mit-license.php
27 //
28 ////////////////////////////////////////////////////////////////////////////////
29
30
31 #include <cmake.h>
32 #include <iostream>
33 #include <../Sarge/src/sarge.h>
34 #include <stdlib.h>
35
36 #ifdef HAVE_READLINE
37   #include <readline/readline.h>
38   #include <readline/history.h>
39 #endif
40
41 ////////////////////////////////////////////////////////////////////////////////
42
43 using namespace std;
44
45 const std::string getResponse(const std::string & prompt) {
46   std::string response {
47     ""
48   };
49
50   // Display prompt, get input
51   #ifdef HAVE_READLINE
52   char * line_read = readline(prompt.c_str());
53   if (!line_read) {
54     std::cout << "\n";
55     response = "<EOF>";
56   } else {
57     // Save history
58     if ( * line_read)
59       add_history(line_read);
60
61     response = std::string(line_read);
62     free(line_read);
63   }
64   #else
65   std::cout << prompt;
66   std::getline(std::cin, response);
67   if (std::cin.eof() == 1) {
68     std::cout << "\n";
69     response = "<EOF>";
70   }
71   #endif
72
73   return response;
74 }
75
76 ////////////////////////////////////////////////////////////////////////////////
77
78 int main(int argc, char** argv)
79 {
80
81   // Command line arguments
82   Sarge sarge;
83
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>");
92
93         if (!sarge.parseArguments(argc, argv)) {
94                 std::cerr << "Couldn't parse arguments..." << std::endl;
95                 return 1;
96         }
97
98         if (sarge.exists("help")) {
99                 sarge.printHelp();
100     return 0;
101         }
102
103   bool space = true;
104   if (sarge.exists("no-space")) {
105     space = false;
106         }
107
108   // Define input command
109   string arg_cmd;
110   sarge.getFlag("command", arg_cmd);
111   if ( space )
112     arg_cmd += " ";
113
114   // Define prompt
115   string prompt = "";
116   sarge.getFlag("prompt", prompt);
117   if ( prompt == "" )
118   {
119     prompt = "% ";
120   } else if ( space ) {
121     prompt += " ";
122   }
123
124   // Execute before-command
125   string before_command;
126   sarge.getFlag("before", before_command);
127   system (before_command.c_str ());
128
129   // Execute after-command
130   string after_command;
131   sarge.getFlag("after", after_command);
132
133   // Do the stuffs!
134   while (true) {
135     // Display prompt, get input
136     auto command = getResponse(prompt);
137
138     // Dispatch command
139     if (command == "<EOF>" || "exit" || "quit" || ":q" )
140     // if (command == "<EOF>" || command == "exit" )
141     {
142       system (after_command.c_str ());
143       return 0;
144     } else {
145       string whole_command = arg_cmd + command;
146       system (whole_command.c_str ());
147     }
148   }
149 }
150
151 ////////////////////////////////////////////////////////////////////////////////