-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
163 lines (144 loc) · 3.74 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
Functionalities status:
No. Name Status
- (01) Basic -------- OK
- (02) Basic -------- OK
- (03) Basic -------- OK
- (04) Basic -------- OK
- (05) Basic -------- OK
- (06) Basic -------- OK
- (07) Basic -------- OK
- (08) Basic -------- OK
- (09) Multi-pipe --- OK
- (10) Background ---
- (11) Spaces ------- OK
- (12) History ------ OK
- (13) Ctrl+C -------
- (14) Chain -------- OK
- (15) If ----------- OK
- (16) Multi-If ----- OK
- (17) Help --------- OK
- (18) Variables ----
*/
#include "commands.h"
#include "debug.h"
#include "./parser/parser.h"
#include "./parser/ast_structs.h"
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <string.h>
#define TRUE 1
#define FALSE 0
#define SHELL_NAME "shello"
#define LINE_BUFF_SIZE 500
#define CLEAN_BUFFER_SIZE 200
#define UP_ARROW_KEY "[A"
#define DOWN_ARROW_KEY "[B"
#define RIGHT_ARROW_KEY "[C"
#define LEFT_ARROW_KEY "[D"
typedef char* string;
char cwd[500];
char* line_buff;
void printPrompt(){
printc(BOLD_GREEN, "%s", SHELL_NAME);
print(":");
printc(BOLD_BLUE, "%s", cwd);
print("$ ");
}
void printIntro(){
FILE* f = fopen("intro", "r");
char* buff = (char*)malloc(sizeof(char));
while (read(f->_fileno,buff,1) > 0)
print("%c", *buff);
print("\n");
fclose(f);
}
char getch() {
char buf = 0;
struct termios old = {0};
if (tcgetattr(0, &old) < 0)
perror("tcsetattr()");
old.c_lflag &= ~ICANON;
old.c_lflag &= ~ECHO;
old.c_cc[VMIN] = 1;
old.c_cc[VTIME] = 0;
if (tcsetattr(0, TCSANOW, &old) < 0)
perror("tcsetattr ICANON");
if (read(0, &buf, 1) < 0)
perror ("read()");
old.c_lflag |= ICANON;
old.c_lflag |= ECHO;
if (tcsetattr(0, TCSADRAIN, &old) < 0)
perror ("tcsetattr ~ICANON");
return (buf);
}
int ends_with(char* a, char* b){
int lena = strlen(a);
int lenb = strlen(b);
if (lenb > lena)
return FALSE;
return strcmp(a + (lena - lenb), b) == 0;
}
void reset_line(int len){
for (int i = 0; i < len; i++)
print("\b \b");
}
int main(int argc, char **args)
{
setbuf(stdout, NULL);
setbuf(stdin, NULL);
printIntro();
line_buff = (char*)malloc(LINE_BUFF_SIZE * sizeof(char));
int buffer_index = 0;
char current = 0;
int reading = TRUE;
getcwd(cwd, 500);
set_init_path(strdup(cwd));
while (TRUE)
{
getcwd(cwd, 500);
printPrompt();
reading = TRUE;
buffer_index = 0;
while (reading){
switch (current = getch())
{
case '\n':
print("%c", current);
reading = FALSE;
break;
case 9:
reading = FALSE;
case 127:
if (buffer_index){
print("\b \b");
buffer_index--;
}
break;
default:
line_buff[buffer_index++] = current;
print("%c", current);
break;
}
if (ends_with(line_buff, UP_ARROW_KEY) || current == 9){
// free(line_buff);
line_buff = (char*)malloc(LINE_BUFF_SIZE*sizeof(char));
reset_line(buffer_index);
buffer_index = 0;
}
}
if (buffer_index == 0)
continue;
line_buff[buffer_index++] = ' ';
line_buff[buffer_index] = 0;
execute_shell_line(line_buff);
}
}