Repository navigation
Expand file tree
/
Copy pathexecutor.cpp
More file actions
125 lines (117 loc) · 3.07 KB
/
Copy pathexecutor.cpp
File metadata and controls
125 lines (117 loc) · 3.07 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
#include "headers.h"
bool foregroundJobRunning = false;
int foregroundJobPid = 0;
void handleCtrlZ(int signum)
{
if (foregroundJobRunning)
{
kill(foregroundJobPid, SIGSTOP);
cout << "Foreground process stopped and moved to the background." << std::endl;
foregroundJobRunning = false;
}
}
void handleCtrlC(int signum)
{
if (foregroundJobRunning)
{
kill(foregroundJobPid, SIGINT);
cout << "Foreground process interrupted with Ctrl-C." << std::endl;
foregroundJobRunning = false;
}
}
void executeCommand(const string &input)
{
vector<string> args = tokenize(input);
if (args.empty())
return;
string command = args[0];
transform(command.begin(), command.end(), command.begin(), ::tolower);
if (args.back() == "&")
{
args.pop_back();
bool runInBackground = true;
executeBackgroundProcess(args, runInBackground, foregroundJobPid, foregroundJobRunning);
}
else if (command == "exit")
{
addToCommandHistory(input);
exit(0);
}
else if (command == "cd")
{
changeDirectory(vector<string>(args.begin() + 1, args.end()));
}
else if (command == "pwd")
{
pwd();
}
else if (command == "echo")
{
echo(args);
}
else if (command == "ls")
{
listDirectory(args);
}
else if (command == "pinfo")
{
printProcessInfo(args);
}
else if (command == "search")
{
search(args);
}
else if (command == "history")
{
addToCommandHistory(input);
int numberOfRecords = 10;
if (args.size() > 1)
{
try
{
numberOfRecords = stoi(args[1]);
}
catch (const exception &)
{
cerr << "history: '" << args[1] << "' is not a valid number" << endl;
return;
}
}
if (numberOfRecords >= 0 && numberOfRecords <= 20)
{
displayCommandHistory(numberOfRecords);
}
else
{
cerr << "Provide a number between 0 to 20!" << endl;
}
}
else
{
bool runInBackground = false;
executeBackgroundProcess(args, runInBackground, foregroundJobPid, foregroundJobRunning);
}
}
void executeMultipleCommands(const string &input)
{
vector<string> tokenizeCommands = tokenizeForColon(input);
for (const string &singleCommandInput : tokenizeCommands)
{
if (singleCommandInput == ";")
continue;
if (singleCommandInput.find('|') != string::npos)
{
vector<string> args = tokenizeForPipe(singleCommandInput);
executePipes(args);
}
else if (singleCommandInput.find('>') != string::npos || singleCommandInput.find('<') != string::npos)
{
vector<string> args = tokenizeForRedirection(singleCommandInput);
redirection(args);
}
else if (!singleCommandInput.empty())
{
executeCommand(singleCommandInput);
}
}
}