-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
141 lines (116 loc) · 4.12 KB
/
main.c
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
#include "headers.h"
#define USER_LENGTH 257 // max size of username in linux + 1 for NULL character
char home_dir[PATH_MAX]; // beginning path
int child_pids[1000] = {-1}; // pids of child processes
int child_current_idx = 0; // index of the current child id
void die(const char *s) {
perror(s);
exit(1);
}
struct termios orig_termios;
void disableRawMode() {
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &orig_termios) == -1)
die("tcsetattr");
}
void enableRawMode() {
if (tcgetattr(STDIN_FILENO, &orig_termios) == -1)
die("tcgetattr");
atexit(disableRawMode);
struct termios raw = orig_termios;
raw.c_lflag &= ~(ICANON | ECHO);
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw) == -1)
die("tcsetattr");
}
// forground
int foreground = 0; // if process running on the foreground
int main() {
printf("Welcome to aksh -- akshit shell\n\n");
past pastevents = init_past();
char username[USER_LENGTH];
char hostname[USER_LENGTH];
getlogin_r(username, USER_LENGTH);
gethostname(hostname, USER_LENGTH);
getcwd(home_dir, PATH_MAX);
// input stuff
char *input = malloc(sizeof(char) * 100);
char c;
char last[100] = "None";
// child processes list
child main_process = create_node(getpid(), "main");
int index_cmd = 0; // index of the command
// Keep accepting commands
// only run if no command in foreground
while (1) {
if (getpid() == main_process->pid) {
setbuf(stdout, NULL);
enableRawMode();
// when no foreground process
char *save_ptr;
int tabbed = 0;
char *rel_pathname = getrpath(home_dir);
// Print appropriate prompt with username, systemname and
// directory before accepting input
// printf("asd");
prompt(username, hostname, rel_pathname);
memset(input, '\0', 100);
int i = 0;
int pt = 0;
while (read(STDIN_FILENO, &c, 1) == 1) {
if (iscntrl(c)) {
if (c == 10)
break;
else if (c == 27) {
char buf[3];
buf[2] = 0;
if (read(STDIN_FILENO, buf, 2) ==
2) { // length of escape code
printf("");
}
} else if (c == 127) { // backspace
if (pt > 0) {
if (input[pt - 1] == 9) {
for (int i = 0; i < 7; i++) {
printf("\b");
}
}
input[--pt] = '\0';
printf("\b \b");
}
} else if (c == 9) { // TAB character
input[pt++] = c;
tabbed = 1;
} else if (c == 4) {
exit(0);
} else {
printf("%d\n", c);
}
} else {
input[pt++] = c;
printf("%c", c);
}
}
disableRawMode();
if (tabbed == 0) {
add_event_past(pastevents, input);
stripspace(input);
// check if ";"
char *semicolon_ptr;
char *expr = strtok_r(input, ";",
&semicolon_ptr); // the first expression
// run for every segment
while (expr != NULL) {
run(expr, save_ptr, home_dir, pastevents, &foreground,
main_process, username, hostname, rel_pathname);
expr = strtok_r(NULL, ";", &semicolon_ptr);
}
}
strcpy(last, input);
// reset input
strcpy(input, "");
// next prompt
fputc('\n', stdout);
// foreground = 0;
}
}
// in child process
}