]> git.armaanb.net Git - gen-shell.git/blob - src/main.cpp
392ecf96bee09ed63fc363efe947889e2901754a
[gen-shell.git] / src / main.cpp
1 // gen-shell - the generic shell
2 // Copyright (c) 2020, Armaan Bhojwani <code@armaanb.net>
3
4 #include <cmake.h>
5 #include <iostream>
6 #include <../Sarge/src/sarge.h>
7 #include <vector>
8 #include <string>
9
10 #ifdef HAVE_READLINE
11   #include <readline/readline.h>
12   #include <readline/history.h>
13 #endif
14
15 ////////////////////////////////////////////////////////////////////////////////
16
17 using namespace std;
18
19 const std::string getResponse(const std::string & prompt) {
20   std::string response {
21     ""
22   };
23
24   // Display prompt, get input
25   #ifdef HAVE_READLINE
26   char * line_read = readline(prompt.c_str());
27   if (!line_read) {
28     std::cout << "\n";
29     response = "<EOF>";
30   } else {
31     // Save history
32     if ( * line_read)
33       add_history(line_read);
34
35     response = std::string(line_read);
36     free(line_read);
37   }
38   #else
39   std::cout << prompt;
40   std::getline(std::cin, response);
41   if (std::cin.eof() == 1) {
42     std::cout << "\n";
43     response = "<EOF>";
44   }
45   #endif
46
47   return response;
48 }
49
50 ////////////////////////////////////////////////////////////////////////////////
51
52 int main(int argc, char** argv)
53 {
54
55   // Command line arguments
56   Sarge sarge;
57
58         sarge.setArgument("a", "after", "Command to execute before leaving the shell", true);
59   sarge.setArgument("b", "before", "Command to execute before entering the shell", true);
60   sarge.setArgument("c", "command", "Command to convert to shell", true);
61   sarge.setArgument("h", "help", "Get help.", false);
62   sarge.setArgument("p", "prompt", "Define a custom prompt", true);
63   sarge.setArgument("", "no-space", "Dont automatically add spaces after custom prompt and command", true);
64   sarge.setDescription("Make a shell from any executable");
65   sarge.setUsage("gen-shell <options>");
66
67         if (!sarge.parseArguments(argc, argv)) {
68                 std::cerr << "Couldn't parse arguments..." << std::endl;
69                 return 1;
70         }
71
72         if (sarge.exists("help")) {
73                 sarge.printHelp();
74     return 0;
75         }
76
77   bool space = true;
78   if (sarge.exists("no-space")) {
79     space = false;
80         }
81
82   // Define input command
83   string arg_cmd;
84   sarge.getFlag("command", arg_cmd);
85   if ( space )
86     arg_cmd += " ";
87
88   // Define prompt
89   string prompt = "";
90   sarge.getFlag("prompt", prompt);
91   if ( prompt == "" )
92   {
93     prompt = "% ";
94   } else if ( space ) {
95     prompt += " ";
96   }
97
98   // Execute before-command
99   string before_command;
100   sarge.getFlag("before", before_command);
101   system (before_command.c_str ());
102
103   // Execute after-command
104   string after_command;
105   sarge.getFlag("after", after_command);
106
107   // Do the stuffs!
108   while (true) {
109     auto command = getResponse(prompt);
110
111     if (command == "<EOF>" || command == "exit" || command == "quit" )
112     {
113       system (after_command.c_str ());
114       return 0;
115     } else {
116       string whole_command = arg_cmd + command;
117       system (whole_command.c_str ());
118     }
119   }
120 }
121
122 ////////////////////////////////////////////////////////////////////////////////