]> git.armaanb.net Git - gen-shell.git/blob - src/main.cpp
516f22396109081ec12a451262bf87c3d5d99e01
[gen-shell.git] / src / main.cpp
1 // gen-shell - the generic shell
2 // Copyright (c) 2021, Armaan Bhojwani <me@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",
59       "Command to execute before leaving the shell", true);
60   sarge.setArgument("b", "before",
61       "Command to execute before entering the shell", true);
62   sarge.setArgument("c", "command", "Command to convert to shell", true);
63   sarge.setArgument("h", "help", "Get help.", false);
64   sarge.setArgument("p", "prompt", "Define a custom prompt", true);
65   sarge.setArgument("", "no-space",
66       "Dont automatically add spaces after custom prompt and command", true);
67   sarge.setDescription("Make a shell from any executable");
68   sarge.setUsage("gen-shell <options>");
69
70   if (!sarge.parseArguments(argc, argv)) {
71     std::cerr << "Couldn't parse arguments..." << std::endl;
72     return 1;
73   }
74
75   if (sarge.exists("help")) {
76     sarge.printHelp();
77     return 0;
78   }
79
80   bool space = true;
81   if (sarge.exists("no-space")) {
82     space = false;
83   }
84
85   // Define input command
86   string arg_cmd;
87   sarge.getFlag("command", arg_cmd);
88   if ( space )
89     arg_cmd += " ";
90
91   // Define prompt
92   string prompt = "";
93   sarge.getFlag("prompt", prompt);
94   if ( prompt == "" )
95   {
96     prompt = "% ";
97   } else if ( space ) {
98     prompt += " ";
99   }
100
101   // Execute before-command
102   string before_command;
103   sarge.getFlag("before", before_command);
104   system (before_command.c_str ());
105
106   // Execute after-command
107   string after_command;
108   sarge.getFlag("after", after_command);
109
110   // Do the stuffs!
111   while (true) {
112     auto command = getResponse(prompt);
113
114     if (command == "<EOF>" || command == "exit" || command == "quit" )
115     {
116       system (after_command.c_str ());
117       return 0;
118     } else {
119       string whole_command = arg_cmd + command;
120       system (whole_command.c_str ());
121     }
122   }
123 }
124
125 ////////////////////////////////////////////////////////////////////////////////