-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.c
More file actions
202 lines (167 loc) · 5.4 KB
/
shell.c
File metadata and controls
202 lines (167 loc) · 5.4 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
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#define clear() printf("\033[H\033[J")
char* mread(char* ustring) {
char buf[2] = { 0, 0 };
while (buf[0] != '\n') {
read(STDIN_FILENO, buf, 1);
if (buf[0] != '\n') {
strcat(ustring, buf);
}
}
return ustring;
}
void introduction(char* user) {
clear();
char* hello="Hello ";
char* hello2 = " ! Welcome to our BasicShell\n\n";
write(STDOUT_FILENO, hello, strlen(hello));
write(STDOUT_FILENO, user, strlen(user));
write(STDOUT_FILENO, hello2, strlen(hello2));
char* help = "*If you need more informations about how to use this shell just write 'help' in command line. \n";
write(STDOUT_FILENO, help, strlen(help));
// sleep(3);
clear();
}
void showUserName_and_Dir(char* user){
char* dir= malloc(100 * sizeof(char));
getcwd(dir, 100*sizeof(char));
char* space= " :~ ";
write(STDOUT_FILENO, user, strlen(user));
write(STDOUT_FILENO, space, strlen(space));
write(STDOUT_FILENO, dir, strlen(dir));
write(STDOUT_FILENO, " ", 1);
}
char** readLine(){
size_t line_size = 2;
char** line = (char**)malloc(line_size * sizeof(char*));
line[0]=(char*)malloc(1000*sizeof(char));
size_t temp = getline(&line[0], &line_size, stdin);
// zrbic obsluge bledu
char* temp_line = line[0];
write(STDOUT_FILENO,temp_line, strlen(temp_line));
return line;
}
void write_token (char **token){
char buf[2] = { 0, 0 };
int i=0;
while (buf != NULL ){
read(token[i], buf, 1);
write(STDOUT_FILENO, buf, 1);
if(buf == '\n') i++;
}
}
char** tokenize(char** old_tokens, char delimeter) {
char** token=(char**)malloc(50*sizeof(char*));
for(int i=0; i<50;i++){
token[i]=(char*)malloc(20*sizeof(char));
}
char buf[2] = { 0, 0 };
int tmp=0;
for(int j=0; j<50; j++){
if(buf == NULL) j++;
read(old_tokens[j], buf, 1);
if (buf[0] != delimeter) {
strcat(token[tmp], buf);
}
else {
strcat(token[tmp++], buf);
tmp++;
}
}
return token;
// delimeter = ' ', old_tokens = ["ls -1 | grep .c|xargs wc -l|sort >>file.txt"]
// return ["ls", " ", "-1", " ", "|", " ", "grep", " ", ".c|xargs", " ", "wc", " ","-l|sort", " ", ">>file.txt"]
//
// delimeter = '|', old_tokens = ["ls", " ", "-1", " ", "|", " ", "grep", " ", ".c|xargs", " ", "wc", " ","-l|sort", " ", ">>file.txt"]
// return ["ls", " ", "-1", " ", "|", " ", "grep", " ", ".c", "|", "xargs", " ", "wc", " ","-l", "|", "sort", " ", ">>file.txt"]
//
// delimeter = '>', old_tokens = ["ls", " ", "-1", " ", "|", " ", "grep", " ", ".c", "|", "xargs", " ", "wc", " ","-l", "|", "sort", " ", ">>file.txt"]
// return ["ls", " ", "-1", " ", "|", " ", "grep", " ", ".c", "|", "xargs", " ", "wc", " ","-l", "|", "sort", " ", ">", ">", "file.txt"]
}
int parse_commands(char* line, char** commands){
int count_of_commands=5;
//while(!=eol) SPRAWDZIC!
for(int i=0;i<count_of_commands;i++){
//podział po "|", ">", "#", "&", " "
//jesli koniec linii to zlicza ile komend i przekazuje returnem
}
}
void handle_commands(char* user//, char** first_command, char** second_command
){
//tu deklaracje
char** line;
char* commands[4];
int one_or_two = 0;
int sum=0;
showUserName_and_Dir(user);
while(1){
showUserName_and_Dir(user);
line = readLine();
char** tok = (char**)malloc(50*sizeof(char*));
for(int i=0; i<50;i++){
tok[i]=(char*)malloc(20*sizeof(char));
}
tok =tokenize(line, ' ');
write_token(tok);
/*
one_or_two = parse_commands(line, commands); //return 0 lub 2
while(counter!=0){
split(commands[counter], commands[counter][0]);
counter--;
}
if(one_or_two==2){
split(commands[1],second_command);
}
split(commands[0],first_command);
while()
//assert <- sprawdzic
for(int i=0;i<2;i++){
sum+=Library_of_Commands(/*komenda którą będziemy sprawdzać); //moze te pierwsze wyrazy z komend przechowywac
//tez w tablicy wtedy bedzie mozna wiecej komend pisac w jednej sciezce?
}
how_many_commands(sum); // i tu podział na to ile execow trzeba zrobic w zaleznosci od sumy
*/
}
}
void function(char** token){
int i=0;
char buf[2] = { 0, 0 };
char* tmp = (char*)malloc(5*sizeof(char));
while(buf != NULL){
if(buf !=' '){
read(token[i], buf, 1);
strcat(tmp, buf);
}
else i++;
function_switch(tmp);
}
}
void function_switch(char* tmp){
// switch(tmp){
// }
}
int main(){
char* user = malloc(16 * sizeof(char));
write(STDOUT_FILENO, "Your user name: ", 15);
mread(user);
introduction(user);
//handle_commands(user);
showUserName_and_Dir(user);
char** line = (char**)malloc(50*sizeof(char*));
for(int i=0; i<50;i++){
line[i]=(char*)malloc(20*sizeof(char));
}
line = readLine();
showUserName_and_Dir(user);
char** tok = (char**)malloc(50*sizeof(char*));
for(int i=0; i<50;i++){
tok[i]=(char*)malloc(20*sizeof(char));
}
tok =tokenize(line, ' ');
write_token(tok);
}