-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbash.cpp
More file actions
308 lines (274 loc) · 9.06 KB
/
bash.cpp
File metadata and controls
308 lines (274 loc) · 9.06 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#include <iostream>
#include <sys/utsname.h>
#include <unistd.h>
#include <termios.h>
#include <cstring>
#include <limits.h>
#include <fcntl.h>
#include <vector>
#include <sys/wait.h>
#include "ls.h"
#include "pinfo.h"
#include "search.h"
#include "ioredirect.h"
#include "handle_pipe.h"
#include "parse.h"
#include <signal.h>
#include "autocomplete.h"
#include "auto_dir.h"
#define HISTORY_FILE "history.txt"
using namespace std;
extern vector<string> autocomplete_words;
struct termios oldt;
char pivot_dir[100];
int foreground_pid = -1;
string home_address = "/home/cypher/Aos_bash";
// void handle_redirect();
void print_basics(){
char buff[30];
// char username[30];
gethostname(buff,30);
char *username = getenv("USER");
struct utsname name;
uname(&name);
string sysname = name.sysname;
char working_dir[100];
// working_dir.resize(PATH_MAX);
getcwd(working_dir,100);
int start = strlen(pivot_dir);
string rel_dir;
for(int i=start;i< strlen(working_dir);i++){
rel_dir+= working_dir[i];
}
cout << username << "@" <<buff << sysname << rel_dir<< "$" << " ~" << " ";
fflush(NULL);
}
vector<string> load_history() {
vector<string> hist;
FILE *file = fopen(HISTORY_FILE, "r");
if (!file) return hist;
char buffer[1024];
while (fgets(buffer, sizeof(buffer), file)) {
size_t len = strlen(buffer);
if (len > 0 && buffer[len - 1] == '\n') {
buffer[len - 1] = '\0'; // strip newline
}
hist.push_back(buffer);
}
fclose(file);
return hist;
}
void save_history(const string &cmd) {
FILE *file = fopen(HISTORY_FILE, "a"); // append mode
if (!file) return;
fprintf(file, "%s\n", cmd.c_str());
fclose(file);
}
vector<int > b_process;
void handle_sigint(int sig) {
if (foreground_pid > 0) {
kill(foreground_pid, SIGINT);
// kill(foreground_pid, SIGCONT);
// forward Ctrl+C to child
}
}
void handle_sigtstp(int sig) {
if (foreground_pid > 0) {
// cout << "reachdeddd here" << endl;
cout << foreground_pid << endl;
b_process.push_back(foreground_pid);
kill(foreground_pid, SIGTSTP);
// kill(b_process.back(), SIGCONT);
// forward Ctrl+Z to child
// Optionally: store in background jobs list
}
}
void sigchld_handler(int signal)
{
int pid;
int status;
while ( (pid = waitpid(-1, &status,WNOHANG)) > 0){
// cout << "The child with pid" << pid << " is being terminataed" << endl;
}
return;
}
vector<char*> tokenise(string &s) {
vector<char*> args;
// string temp = s;
char* start = &s[0];
char *token = strtok(start, " ");
while(token != nullptr){
// cout << token << endl; // this is why you see the 2nd "ls" printed
if (strlen(token) > 0) { // skip empty tokens
args.push_back(token);
}
token = strtok(nullptr, " ");
}
args.push_back(nullptr); // sentinel for execvp
return args;
}
vector<string> history;
int history_index = -1;
int cursor_pos = 0;
void restore_terminal() {
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
}
int main(){
struct termios newt;
signal(SIGINT, handle_sigint); // Ctrl+C
signal(SIGTSTP, handle_sigtstp); // Ctrl+Z
// signal(SIGCHLD, sigchld_handler);
//Save current terminal settings
tcgetattr(STDIN_FILENO, &oldt);
//-------------
history = load_history();
history_index = history.size();
//--------------
newt = oldt;
atexit(restore_terminal);
// 2. Disable canonical mode and echo
newt.c_lflag &= ~(ICANON| ECHO);
// newt.c_iflag &= ~(IXON | ICRNL);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
getcwd(pivot_dir,PATH_MAX);
autocomplete_words.push_back("pinfo");
auto_complete_list();
// autocomplete_words.push_back("")
while(1){
print_basics();
// memset(args,0,100);
// getline(cin,s);
cursor_pos = 0;
char *c = new char;
string s;
while(read(0,c,1)){
// cout << endl;
// int n = read(0,c,1);
// if(n == 0){
// cout << "exiting shell" << endl;
// exit(0);
// }
if(*c == 4){
cout << "exiting terminal" << endl;
exit(0);
}
else if (*c == '\n') {
cout << endl;
if (!s.empty()) {
// avoid saving consecutive duplicates
if (history.empty() || history.back() != s) {
history.push_back(s);
save_history(s);
}
history_index = history.size();
}
break;
}
else if (*c == '\033') {
char seq[2];
read(STDIN_FILENO, &seq[0], 1);
read(STDIN_FILENO, &seq[1], 1);
if (seq[0] == '[') {
switch (seq[1]) {
case 'A': // Up arrow
if (!history.empty() && history_index > 0) {
history_index--;
s = history[history_index];
cout << "\033[2K\r"; // clear line
print_basics();
cout << s << flush;
cursor_pos = s.size();
}
break;
case 'B': // Down arrow
if (!history.empty() && history_index < (int)history.size() - 1) {
history_index++;
s = history[history_index];
} else {
s.clear();
history_index = history.size();
}
cout << "\033[2K\r";
print_basics();
cout << s << flush;
cursor_pos = s.size();
break;
case 'C': // Right arrow
if (cursor_pos < (int)s.size()) {
cout << "\033[C" << flush;
cursor_pos++;
}
break;
case 'D': // Left arrow
if (cursor_pos > 0) {
cout << "\033[D" << flush;
cursor_pos--;
}
break;
}
}
}
else if (*c == '\t') {
auto_dir();
// find last word in s
size_t pos = s.find_last_of(' ');
string prefix = (pos == string::npos) ? "" : s.substr(0, pos + 1);
string lastWord = (pos == string::npos) ? s : s.substr(pos + 1);
// search matches for last word
string str1 = string_search(autocomplete_words, lastWord);
string str2 = search_dir(dir_words, lastWord);
if (!str2.empty()) s = prefix + str2;
else if (!str1.empty()) s = prefix + str1;
cout << "\033[2K\r";
print_basics();
cursor_pos = s.size(); // always reset cursor to end
cout << s << flush;
}
else if (*c == 127) { // Backspace
if (cursor_pos > 0 && s.size() > 0) {
s.erase(cursor_pos - 1, 1);
cursor_pos--;
// move cursor back
cout << "\b";
// print the rest of the string after deletion
cout << s.substr(cursor_pos);
// print one extra space to erase last leftover char
cout << " ";
// move cursor back to the right place
int moves = s.size() - cursor_pos + 1;
for (int i = 0; i < moves; i++) cout << "\b";
cout.flush();
}
}
else{
cout << *c ;
cursor_pos++;
fflush(0);
s+= *c;
}
}
vector<string> commands;
string temp;
for(int i=0;i<s.size();i++){
if(s[i] == ';'){
cout << temp << endl;
commands.push_back(temp);
temp.clear();
}
else temp+= s[i];
}
commands.push_back(temp);
for(int i=0;i<commands.size();i++){
vector<char*> args = tokenise(commands[i]);
parse(args);
args.clear();
}
// cout << endl << flush;
// cout << flush;
// args
// cout << s << endl;
// free(args);
// dup2(1,stdout)
}
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
}