-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
95 lines (88 loc) · 3.62 KB
/
main.cpp
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
#include <cstdlib>
#include <iostream>
#include <vector>
#include <filesystem>
#include "cd.h"
#include "execute.h"
#include "inputParser.h"
#include "redirections.h"
#include "pipes.h"
using namespace std;
string getPathFromHome(){
int i=0;
string str = filesystem::current_path();
int count = 0;
while(i < str.size() && count != 3){
if(str[i] == '/') count++;
i++;
}
string dir = str.substr(i);
return dir;
}
void banner(){
cout<<endl;
cout<<" ███████╗██╗ ██╗███████╗██╗ ██╗ "<<endl;
cout<<" ██╔════╝██║ ██║██╔════╝██║ ██║ "<<endl;
cout<<" ███████╗███████║█████╗ ██║ ██║ █████╗"<<endl;
cout<<" ╚════██║██╔══██║██╔══╝ ██║ ██║ ╚════╝"<<endl;
cout<<" ███████║██║ ██║███████╗███████╗███████╗ "<<endl;
cout<<" ╚══════╝╚═╝ ╚═╝╚══════╝╚══════╝╚══════╝ "<<endl;
cout<<" "<<endl;
cout<<" ██████╗ ██╗ ██╗███╗ ██╗██╗ ██╗ "<<endl;
cout<<" ██╔══██╗██║ ██║████╗ ██║██║ ██╔╝ "<<endl;
cout<<" ██████╔╝██║ ██║██╔██╗ ██║█████╔╝ "<<endl;
cout<<" ██╔═══╝ ██║ ██║██║╚██╗██║██╔═██╗ "<<endl;
cout<<" ██║ ╚██████╔╝██║ ╚████║██║ ██╗ "<<endl;
cout<<" ╚═╝ ╚═════╝ ╚═╝ ╚═══╝╚═╝ ╚═╝ "<<endl;
cout<<endl;
return;
}
void showHelp() {
banner();
cout << "===== Shell Help =====\n";
cout << "This shell supports the following commands and features:\n\n";
cout << " cd <dir> - Change directory to <dir>\n";
cout << " help - Display this help message\n";
cout << " checkbg - Check the status of background processes\n";
cout << " <cmd> & - Run <cmd> in the background\n";
cout << " <cmd> | <cmd> - Pipe output of one command to another\n";
cout << " <cmd> > file - Redirect output to file (overwrite)\n";
cout << " <cmd> >> file - Redirect output to file (append)\n";
cout << " exit - Exits the shell\n";
cout << "======================\n";
}
string handleCommand(vector<string> input){
if(input.empty()) return "";
if(input[0] == "cd") {
if(input.size()==1) return "Path not provided...\n";
changeDir(input[1]);
return "";
}
if(input[0] == "checkbg"){
checkBackgroundProcesses();
return "";
}
if(input[0] == "help"){
showHelp();
return "";
}
if(handleRedirections(input)) return "Data written to file... \n";
if(input[input.size()-1] == "&") return executeCommand(input, true);
string output = handlePipes(input);
return output;
}
int main (int argc, char *argv[]) {
banner();
while(true){
cout<<"[~/"<<getPathFromHome()<<"]$ ";
string input;
getline(cin, input);
if(input == "exit") {
cout<<"BYE..."<<endl;
return 0;
}
vector<string> parsedInput = parser(input);
cout << handleCommand(parsedInput);
}
return 0;
}