This repository was archived by the owner on Oct 9, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
189 lines (153 loc) · 3.51 KB
/
Copy pathmain.cpp
File metadata and controls
189 lines (153 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <cstring>
#include <fstream>
#include <iostream>
#include <string>
#include <unistd.h>
#include <vector>
#include <sys/wait.h>
namespace
{
/// Data structure responsible to hold the history of ran commands
class CommandHistory
{
private:
size_t _next;
const size_t _size;
char** _end;
char** _commands;
size_t _command_count;
public:
explicit CommandHistory(const size_t size) : _size(size)
{
_commands = new char*[_size]();
_next = 0;
_end = &_commands[size];
_command_count = 0;
}
/// Adds the given command to the history
void add(const char* command)
{
// If buffer filled, shift all
if (_next == _size)
{
for (int i = 0; i < _next - 1; i++)
_commands[i] = _commands[i + 1];
_commands[_next - 1] = nullptr;
_next--;
//add(command);
//return;
}
const auto entry = new char[strlen(command) + 1];
strcpy(entry, command);
_commands[_next] = entry;
_next++;
_command_count++;
}
/// Prints all the commands to the given output
void print(std::ostream& output) const
{
const auto count = std::min(_command_count, _size);
for (auto i = 0; i < _next; i++)
{
output << (count - i);
output << "\t";
output << _commands[i];
output << std::endl;
}
}
~CommandHistory()
{
for (auto i = 0; i < _next; i++)
delete[] _commands[i];
delete[] _commands;
}
};
}
/// Function responsible to execute the given command
static void execute_command(char* command)
{
// Split arguments
std::vector<char*> args;
char* command_name = strtok(command, " ");
char* current_keyword = command_name;
while (current_keyword != nullptr)
{
args.push_back(current_keyword);
current_keyword = strtok(nullptr, " ");
}
// Prepare arguments for execution
const auto argv = new char*[args.size() + 1];
for (int k = 0; k < args.size(); k++)
argv[k] = args[k];
argv[args.size()] = nullptr;
// Run command
const auto code = execvp(command_name, argv);
if (code != 0)
{
const std::string str_command(command);
perror(("Failed to execute the command '" + str_command + "'").c_str());
}
delete[] argv;
}
int main()
{
auto history = CommandHistory(5);
char* command = nullptr;
while (true)
{
std::cout << "GauthierChalons< ";
char user_buffer[128];
std::cin.getline(user_buffer, 128);
command = user_buffer;
// If empty, skip
if (strlen(command) == 0)
continue;
// If stop command, exit
if (strcmp(command, "stop") == 0)
return 0;
// If history command, display command history
if (strcmp(command, "historique") == 0)
{
history.print(std::cout);
std::ofstream output;
output.open("historique.txt");
history.print(output);
output.close();
continue;
}
// Run program
if (command[0] == '.')
{
const auto shell_command = strtok(command, " ");
std::system(shell_command);
continue;
}
bool wait_for_child = true;
// If ends with ampersand, don't wait child
if (command[strlen(command) - 1] == '&')
{
wait_for_child = false;
command[strlen(command) - 1] = '\0';
}
// Fork
const pid_t child_pid = fork();
// If failed, exit
if (child_pid < 0)
{
perror("Could not fork");
return -1;
}
// If as child
if (child_pid == 0)
{
execute_command(command);
return 0;
}
// If waiting for child failed, exit
if (wait_for_child && waitpid(child_pid, nullptr, 0) < 0)
return -1;
std::string history_entry = std::string(command) + "\t" + std::to_string(child_pid);
history.add(history_entry.c_str());
}
return 0;
}